Chromium Code Reviews| 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 f8fd4f5cc36a119230041402b6cfec1fa90d45d4..f59797bd32f63f93d2e60c14b281ff5780a05f9d 100644 |
| --- a/chrome/browser/ui/browser_instant_controller.cc |
| +++ b/chrome/browser/ui/browser_instant_controller.cc |
| @@ -8,6 +8,8 @@ |
| #include "chrome/browser/extensions/extension_service.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,6 +24,10 @@ |
| #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/gfx/color_utils.h" |
| +#include "ui/gfx/sys_color_change_listener.h" |
| + |
| namespace chrome { |
| @@ -32,11 +38,20 @@ BrowserInstantController::BrowserInstantController(Browser* browser) |
| : browser_(browser), |
| instant_(ALLOW_THIS_IN_INITIALIZER_LIST(this), |
| chrome::search::IsInstantExtendedAPIEnabled(browser->profile())), |
| - instant_unload_handler_(browser) { |
| + instant_unload_handler_(browser), |
| + initialized_theme_info_(false), |
| + theme_area_height_(0) { |
| profile_pref_registrar_.Init(browser_->profile()->GetPrefs()); |
| profile_pref_registrar_.Add(prefs::kInstantEnabled, this); |
| instant_.SetInstantEnabled(IsInstantEnabled(browser_->profile())); |
| 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 +134,20 @@ void BrowserInstantController::ActiveTabChanged() { |
| instant_.ActiveTabChanged(); |
| } |
| +void BrowserInstantController::SetContentHeight(int height) { |
| + OnThemeAreaHeightChanged(height); |
| +} |
| + |
| +void BrowserInstantController::UpdateThemeInfoForPreview() { |
| + // Update theme background info and theme area height. |
| + // Initialize |theme_info| if necessary. |
| + // |OnThemeChanged| also updates theme area height if necessary. |
| + if (!initialized_theme_info_) |
| + OnThemeChanged(ThemeServiceFactory::GetForProfile(browser_->profile())); |
| + else |
| + OnThemeChanged(NULL); |
| +} |
| + |
| //////////////////////////////////////////////////////////////////////////////// |
| // BrowserInstantController, PrefObserver implementation: |
| @@ -134,6 +163,117 @@ void BrowserInstantController::OnPreferenceChanged( |
| void BrowserInstantController::ModeChanged(const search::Mode& old_mode, |
| const search::Mode& new_mode) { |
| instant_.SearchModeChanged(old_mode, new_mode); |
| + |
| + // If mode is now |NTP|, send theme-related information to instant. |
| + if (new_mode.is_ntp()) |
| + UpdateThemeInfoForPreview(); |
|
sreeram
2012/11/20 17:37:58
Wait, why do we need to send the theme info the pr
kuan
2012/11/20 17:52:24
i don't send the theme-related info when the mode
sreeram
2012/11/20 17:55:11
I think that's wasteful. The common case will not
kuan
2012/11/20 18:07:36
the structure i'm sending over consists of 4 short
sreeram
2012/11/20 18:16:29
Ok, never mind then. (I thought incorrectly that y
kuan
2012/11/20 18:50:48
Done.
|
| +} |
| + |
| +//////////////////////////////////////////////////////////////////////////////// |
| +// BrowserInstantController, content::NotificationObserver implementation: |
| + |
| +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::OnThemeChanged(ThemeService* theme_service) { |
| + if (theme_service) { // Get theme information from theme service. |
| + theme_info_ = ThemeBackgroundInfo(); |
| + |
| + // Set theme background color. |
| + SkColor background_color = |
| + theme_service->GetColor(ThemeService::COLOR_NTP_BACKGROUND); |
| + if (gfx::IsInvertedColorScheme()) |
| + background_color = color_utils::InvertColor(background_color); |
| + theme_info_.color_r = SkColorGetR(background_color); |
| + theme_info_.color_g = SkColorGetG(background_color); |
| + theme_info_.color_b = SkColorGetB(background_color); |
| + theme_info_.color_a = SkColorGetA(background_color); |
| + |
| + if (theme_service->HasCustomImage(IDR_THEME_NTP_BACKGROUND)) { |
| + // Set theme id for theme background image url. |
| + theme_info_.theme_id = theme_service->GetThemeID(); |
| + |
| + // Set theme background image horizontal alignment. |
| + int alignment = 0; |
| + theme_service->GetDisplayProperty(ThemeService::NTP_BACKGROUND_ALIGNMENT, |
| + &alignment); |
| + if (alignment & ThemeService::ALIGN_LEFT) { |
| + theme_info_.image_horizontal_alignment = THEME_BKGRND_IMAGE_ALIGN_LEFT; |
| + } else if (alignment & ThemeService::ALIGN_RIGHT) { |
| + theme_info_.image_horizontal_alignment = THEME_BKGRND_IMAGE_ALIGN_RIGHT; |
| + } else { // ALIGN_CENTER |
| + theme_info_.image_horizontal_alignment = |
| + THEME_BKGRND_IMAGE_ALIGN_CENTER; |
| + } |
| + |
| + // Set theme background image vertical alignment. |
| + if (alignment & ThemeService::ALIGN_TOP) |
| + theme_info_.image_vertical_alignment = THEME_BKGRND_IMAGE_ALIGN_TOP; |
| + else if (alignment & ThemeService::ALIGN_BOTTOM) |
| + theme_info_.image_vertical_alignment = THEME_BKGRND_IMAGE_ALIGN_BOTTOM; |
| + else // ALIGN_CENTER |
| + theme_info_.image_vertical_alignment = THEME_BKGRND_IMAGE_ALIGN_CENTER; |
| + |
| + // Set theme background image tiling. |
| + int tiling = 0; |
| + theme_service->GetDisplayProperty(ThemeService::NTP_BACKGROUND_TILING, |
| + &tiling); |
| + switch (tiling) { |
| + case ThemeService::NO_REPEAT: |
| + theme_info_.image_tiling = THEME_BKGRND_IMAGE_NO_REPEAT; |
| + break; |
| + case ThemeService::REPEAT_X: |
| + theme_info_.image_tiling = THEME_BKGRND_IMAGE_REPEAT_X; |
| + break; |
| + case ThemeService::REPEAT_Y: |
| + theme_info_.image_tiling = THEME_BKGRND_IMAGE_REPEAT_Y; |
| + break; |
| + case ThemeService::REPEAT: |
| + theme_info_.image_tiling = THEME_BKGRND_IMAGE_REPEAT; |
| + break; |
| + } |
| + |
| + // Set theme background image height. |
| + gfx::ImageSkia* image = theme_service->GetImageSkiaNamed( |
| + IDR_THEME_NTP_BACKGROUND); |
| + DCHECK(image); |
| + theme_info_.image_height = image->height(); |
| + } |
| + |
| + initialized_theme_info_ = true; |
| + } |
| + |
| + DCHECK(initialized_theme_info_); |
| + |
| + if (browser_->search_model()->mode().is_ntp()) { |
| + instant_.ThemeChanged(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 only 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 (!browser_->search_model()->mode().is_ntp() || |
| + theme_info_.theme_id.empty() || |
| + theme_info_.image_vertical_alignment == THEME_BKGRND_IMAGE_ALIGN_TOP) { |
| + return; |
| + } |
| + instant_.ThemeAreaHeightChanged(theme_area_height_); |
| } |
| } // namespace chrome |