blob: 2cc3a960d572783140b4bd3b3d3ebb6b4685718f [file] [edit]
// Copyright 2012 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome/browser/extensions/theme_installed_infobar_delegate.h"
#include <stddef.h>
#include <string>
#include <utility>
#include "base/memory/ptr_util.h"
#include "base/strings/utf_string_conversions.h"
#include "build/build_config.h"
#include "chrome/app/vector_icons/vector_icons.h"
#include "chrome/browser/infobars/confirm_infobar_creator.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/themes/theme_service.h"
#include "chrome/browser/themes/theme_service_factory.h"
#include "chrome/browser/ui/browser.h"
#include "chrome/browser/ui/browser_window/public/browser_window_interface.h"
#include "chrome/browser/ui/browser_window/public/profile_browser_collection.h"
#include "chrome/browser/ui/tabs/tab_strip_model.h"
#include "chrome/grit/generated_resources.h"
#include "components/infobars/content/content_infobar_manager.h"
#include "components/infobars/core/infobar.h"
#include "extensions/browser/extension_system.h"
#include "ui/base/l10n/l10n_util.h"
#include "ui/base/ui_base_features.h"
// static
void ThemeInstalledInfoBarDelegate::CreateForLastActiveTab(
Profile* profile,
const std::string& theme_name,
const std::string& theme_id,
std::unique_ptr<ThemeService::ThemeReinstaller> prev_theme_reinstaller) {
// A theme install in either a normal or incognito window for a profile
// affects all windows for that profile, so search the original profile's
// browser collection.
BrowserWindowInterface* browser =
ProfileBrowserCollection::GetForProfile(profile)->FindTabbedBrowser(
/*match_original_profiles=*/true);
if (browser) {
content::WebContents* web_contents =
browser->GetTabStripModel()->GetActiveWebContents();
if (web_contents) {
ThemeInstalledInfoBarDelegate::Create(
infobars::ContentInfoBarManager::FromWebContents(web_contents),
ThemeServiceFactory::GetForProfile(profile), theme_name, theme_id,
std::move(prev_theme_reinstaller));
}
}
}
// static
void ThemeInstalledInfoBarDelegate::Create(
infobars::ContentInfoBarManager* infobar_manager,
ThemeService* theme_service,
const std::string& theme_name,
const std::string& theme_id,
std::unique_ptr<ThemeService::ThemeReinstaller> prev_theme_reinstaller) {
// Create the new infobar.
std::unique_ptr<infobars::InfoBar> new_infobar(
CreateConfirmInfoBar(base::WrapUnique<ConfirmInfoBarDelegate>(
new ThemeInstalledInfoBarDelegate(
theme_service, theme_name, theme_id,
std::move(prev_theme_reinstaller)))));
// If there's a previous theme infobar, just replace that instead of adding a
// new one.
for (infobars::InfoBar* old_infobar : infobar_manager->infobars()) {
ThemeInstalledInfoBarDelegate* theme_infobar =
old_infobar->delegate()->AsThemePreviewInfobarDelegate();
if (theme_infobar) {
// If the user installed the same theme twice, ignore the second install
// and keep the first install info bar, so that they can easily undo to
// get back the previous theme.
// |theme_id| is not defined for autogenerated themes, but since those
// don't show an infobar, it's valid in this case.
if (theme_infobar->theme_id_ != theme_id) {
infobar_manager->ReplaceInfoBar(old_infobar, std::move(new_infobar));
}
return;
}
}
// No previous theme infobar, so add this.
infobar_manager->AddInfoBar(std::move(new_infobar));
}
ThemeInstalledInfoBarDelegate::ThemeInstalledInfoBarDelegate(
ThemeService* theme_service,
const std::string& theme_name,
const std::string& theme_id,
std::unique_ptr<ThemeService::ThemeReinstaller> prev_theme_reinstaller)
: ConfirmInfoBarDelegate(),
theme_name_(theme_name),
theme_id_(theme_id),
prev_theme_reinstaller_(std::move(prev_theme_reinstaller)) {
theme_observation_.Observe(theme_service);
}
ThemeInstalledInfoBarDelegate::~ThemeInstalledInfoBarDelegate() = default;
infobars::InfoBarDelegate::InfoBarIdentifier
ThemeInstalledInfoBarDelegate::GetIdentifier() const {
return THEME_INSTALLED_INFOBAR_DELEGATE;
}
const gfx::VectorIcon& ThemeInstalledInfoBarDelegate::GetVectorIcon() const {
return features::IsRoundedIconsEnabled() ? kBrushFilledIcon
: kPaintbrushOldIcon;
}
ThemeInstalledInfoBarDelegate*
ThemeInstalledInfoBarDelegate::AsThemePreviewInfobarDelegate() {
return this;
}
std::u16string ThemeInstalledInfoBarDelegate::GetMessageText() const {
return l10n_util::GetStringFUTF16(IDS_THEME_INSTALL_INFOBAR_LABEL,
base::UTF8ToUTF16(theme_name_));
}
int ThemeInstalledInfoBarDelegate::GetButtons() const {
return BUTTON_CANCEL;
}
std::u16string ThemeInstalledInfoBarDelegate::GetButtonLabel(
InfoBarButton button) const {
DCHECK_EQ(BUTTON_CANCEL, button);
return l10n_util::GetStringUTF16(IDS_THEME_INSTALL_INFOBAR_UNDO_BUTTON);
}
bool ThemeInstalledInfoBarDelegate::Cancel() {
if (prev_theme_reinstaller_) {
prev_theme_reinstaller_->Reinstall();
}
return false; // The theme change will close us.
}
void ThemeInstalledInfoBarDelegate::OnThemeChanged() {
// If the new theme is different from what this info bar is associated with,
// close this info bar since it is no longer relevant.
CHECK(theme_observation_.IsObserving());
if (theme_id_ != theme_observation_.GetSource()->GetThemeID()) {
infobar()->RemoveSelf();
}
}