| Index: chrome/browser/ui/browser_instant_controller.cc
|
| diff --git a/chrome/browser/ui/browser_instant_controller.cc b/chrome/browser/ui/browser_instant_controller.cc
|
| index 1ad6c25b6b27792411ac6e05b15e8896b8ca0f24..465fc09459221b8f64c61ddc08a42a08401fb88d 100644
|
| --- a/chrome/browser/ui/browser_instant_controller.cc
|
| +++ b/chrome/browser/ui/browser_instant_controller.cc
|
| @@ -4,11 +4,16 @@
|
|
|
| #include "chrome/browser/ui/browser_instant_controller.h"
|
|
|
| +#include "base/string_number_conversions.h"
|
| +#include "base/stringprintf.h"
|
| +#include "base/utf_string_conversions.h"
|
| #include "chrome/browser/browser_shutdown.h"
|
| #include "chrome/browser/extensions/extension_service.h"
|
| #include "chrome/browser/instant/instant_controller.h"
|
| #include "chrome/browser/prefs/pref_service.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_tabstrip.h"
|
| #include "chrome/browser/ui/browser_window.h"
|
| @@ -22,9 +27,19 @@
|
| #include "chrome/common/pref_names.h"
|
| #include "content/public/browser/notification_service.h"
|
| #include "content/public/browser/web_contents.h"
|
| +#include "grit/theme_resources.h"
|
| +#include "ui/base/theme_provider.h"
|
| +#include "ui/gfx/color_utils.h"
|
| +#include "ui/gfx/sys_color_change_listener.h"
|
| +
|
|
|
| namespace {
|
|
|
| +const char kCSSBackgroundImageFormat[] =
|
| + "-webkit-image-set(url(chrome://theme/IDR_THEME_BACKGROUND?%s) 1x)";
|
| +
|
| +const char kCSSBackgroundColorFormat[] = "rgba(%d,%d,%d,%s)";
|
| +
|
| // Returns true iff the search model for |tab| is in an NTP state, that is, a
|
| // state in which the Instant overlay may be showing custom NTP content in
|
| // EXTENDED mode.
|
| @@ -45,12 +60,21 @@ namespace chrome {
|
|
|
| BrowserInstantController::BrowserInstantController(Browser* browser)
|
| : browser_(browser),
|
| - instant_unload_handler_(browser) {
|
| + instant_unload_handler_(browser),
|
| + theme_image_alignment_(0),
|
| + theme_area_height_(0) {
|
| profile_pref_registrar_.Init(browser_->profile()->GetPrefs());
|
| profile_pref_registrar_.Add(prefs::kInstantEnabled, this);
|
| ResetInstant();
|
| browser_->tab_strip_model()->AddObserver(this);
|
| browser_->search_model()->AddObserver(this);
|
| +
|
| +#if defined(ENABLE_THEMES)
|
| + // Listen for theme installation.
|
| + registrar_.Add(this, chrome::NOTIFICATION_BROWSER_THEME_CHANGED,
|
| + content::Source<ThemeService>(
|
| + ThemeServiceFactory::GetForProfile(browser_->profile())));
|
| +#endif // defined(ENABLE_THEMES)
|
| }
|
|
|
| BrowserInstantController::~BrowserInstantController() {
|
| @@ -119,6 +143,23 @@ TabContents* BrowserInstantController::GetActiveTabContents() const {
|
| return chrome::GetActiveTabContents(browser_);
|
| }
|
|
|
| +void BrowserInstantController::SetContentHeight(int height) {
|
| + OnThemeAreaHeightChanged(height);
|
| +}
|
| +
|
| +void BrowserInstantController::UpdateThemeRelatedInfoForPreview() {
|
| + // Update theme info.
|
| + // |theme_info_.color_rgba| must always be filled, so if it's empty,
|
| + // |theme_info_| hasn't been initialized.
|
| + if (theme_info_.color_rgba.length() == 0)
|
| + OnThemeChanged(ThemeServiceFactory::GetForProfile(browser_->profile()));
|
| + else
|
| + instant_->OnThemeChanged(theme_info_);
|
| +
|
| + // Update theme area height.
|
| + OnThemeAreaHeightChanged(theme_area_height_);
|
| +}
|
| +
|
| ////////////////////////////////////////////////////////////////////////////////
|
| // BrowserInstantController, PrefObserver implementation:
|
|
|
| @@ -152,13 +193,30 @@ void BrowserInstantController::ActiveTabChanged(
|
|
|
| void BrowserInstantController::ModeChanged(const search::Mode& old_mode,
|
| const search::Mode& new_mode) {
|
| - if (instant() && old_mode.is_ntp() != new_mode.is_ntp())
|
| + if (instant() && old_mode.is_ntp() != new_mode.is_ntp()) {
|
| instant_->OnActiveTabModeChanged(new_mode.is_ntp());
|
| +
|
| + // If mode is now |NTP|, send theme-related information to instant.
|
| + if (new_mode.is_ntp()) {
|
| + instant_->OnThemeChanged(theme_info_);
|
| + OnThemeAreaHeightChanged(theme_area_height_);
|
| + }
|
| + }
|
| }
|
|
|
| ////////////////////////////////////////////////////////////////////////////////
|
| // BrowserInstantController, private:
|
|
|
| +void BrowserInstantController::Observe(
|
| + int type,
|
| + const content::NotificationSource& source,
|
| + const content::NotificationDetails& details) {
|
| +#if defined(ENABLE_THEMES)
|
| + DCHECK_EQ(chrome::NOTIFICATION_BROWSER_THEME_CHANGED, type);
|
| + OnThemeChanged(content::Source<ThemeService>(source).ptr());
|
| +#endif // defined(ENABLE_THEMES)
|
| +}
|
| +
|
| void BrowserInstantController::ResetInstant() {
|
| instant_.reset(
|
| !browser_shutdown::ShuttingDownWithoutClosingBrowsers() &&
|
| @@ -172,4 +230,93 @@ void BrowserInstantController::ResetInstant() {
|
| content::NotificationService::NoDetails());
|
| }
|
|
|
| +void BrowserInstantController::OnThemeChanged(
|
| + ui::ThemeProvider* theme_provider) {
|
| + theme_info_ = ThemeBackgroundInfo();
|
| + theme_image_alignment_ = 0;
|
| +
|
| + // Set theme background color.
|
| + SkColor background_color =
|
| + theme_provider->GetColor(ThemeService::COLOR_NTP_BACKGROUND);
|
| + if (gfx::IsInvertedColorScheme())
|
| + background_color = color_utils::InvertColor(background_color);
|
| + theme_info_.color_rgba = UTF8ToUTF16(
|
| + // Convert the alpha using DoubleToString because StringPrintf will use
|
| + // locale specific formatters (e.g., use , instead of . in German).
|
| + base::StringPrintf(
|
| + kCSSBackgroundColorFormat,
|
| + SkColorGetR(background_color),
|
| + SkColorGetG(background_color),
|
| + SkColorGetB(background_color),
|
| + base::DoubleToString(SkColorGetA(background_color) / 255.0).c_str()));
|
| +
|
| + if (theme_provider->HasCustomImage(IDR_THEME_NTP_BACKGROUND)) {
|
| + // Set theme background image url.
|
| + theme_info_.image_url = UTF8ToUTF16(
|
| + base::StringPrintf(kCSSBackgroundImageFormat,
|
| + browser_->profile()->GetPrefs()->GetString(
|
| + prefs::kCurrentThemeID).c_str()));
|
| +
|
| + // Set theme background image horizontal alignment.
|
| + theme_provider->GetDisplayProperty(
|
| + ThemeService::NTP_BACKGROUND_ALIGNMENT, &theme_image_alignment_);
|
| + std::string alignment;
|
| + if (theme_image_alignment_ & ThemeService::ALIGN_LEFT)
|
| + alignment = ThemeService::kAlignmentLeft;
|
| + else if (theme_image_alignment_& ThemeService::ALIGN_RIGHT)
|
| + alignment = ThemeService::kAlignmentRight;
|
| + else // ALIGN_CENTER
|
| + alignment = ThemeService::kAlignmentCenter;
|
| + theme_info_.image_horizontal_alignment = UTF8ToUTF16(alignment);
|
| +
|
| + // Set theme background image vertical alignment.
|
| + if (theme_image_alignment_& ThemeService::ALIGN_TOP)
|
| + alignment = ThemeService::kAlignmentTop;
|
| + else if (theme_image_alignment_& ThemeService::ALIGN_BOTTOM)
|
| + alignment = ThemeService::kAlignmentBottom;
|
| + else // ALIGN_CENTER
|
| + alignment = ThemeService::kAlignmentCenter;
|
| + theme_info_.image_vertical_alignment = UTF8ToUTF16(alignment);
|
| +
|
| + // Set tiling of theme background image.
|
| + int repeat_mode = 0;
|
| + theme_provider->GetDisplayProperty(
|
| + ThemeService::NTP_BACKGROUND_TILING, &repeat_mode);
|
| + theme_info_.image_tiling = UTF8ToUTF16(
|
| + ThemeService::TilingToString(repeat_mode));
|
| + }
|
| +
|
| + if (instant() && browser_->search_model()->mode().is_ntp()) {
|
| + instant()->OnThemeChanged(theme_info_);
|
| +
|
| + // Theme area height is only sent to preview for non-top-aligned images;
|
| + // new theme may have a different alignment that requires preview to know
|
| + // theme area height.
|
| + OnThemeAreaHeightChanged(theme_area_height_);
|
| + }
|
| +}
|
| +
|
| +void BrowserInstantController::OnThemeAreaHeightChanged(int height) {
|
| + theme_area_height_ = height;
|
| +
|
| + // Notify preview if mode is |NTP| and theme background image is not top-
|
| + // aligned; top-aligned images don't need theme area height to determine which
|
| + // part of the image overlay should draw, 'cos the origin is top-left.
|
| +
|
| + if (!(instant() && browser_->search_model()->mode().is_ntp() &&
|
| + theme_info_.image_url.length() > 0)) {
|
| + return;
|
| + }
|
| +
|
| + bool notify = false;
|
| + if (theme_image_alignment_ & ThemeService::ALIGN_TOP)
|
| + notify = false;
|
| + else if (theme_image_alignment_ & ThemeService::ALIGN_BOTTOM)
|
| + notify = true;
|
| + else // ALIGN_CENTER
|
| + notify = true;
|
| + if (notify)
|
| + instant()->OnThemeAreaHeightChanged(theme_area_height_);
|
| +}
|
| +
|
| } // namespace chrome
|
|
|