OLD | NEW |
1 // Copyright 2012 The Chromium Authors. All rights reserved. | 1 // Copyright 2012 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/browser/ui/browser_instant_controller.h" | 5 #include "chrome/browser/ui/browser_instant_controller.h" |
6 | 6 |
7 #include "chrome/browser/browser_shutdown.h" | 7 #include "chrome/browser/browser_shutdown.h" |
8 #include "chrome/browser/extensions/extension_service.h" | 8 #include "chrome/browser/extensions/extension_service.h" |
9 #include "chrome/browser/prefs/pref_service.h" | 9 #include "chrome/browser/prefs/pref_service.h" |
10 #include "chrome/browser/profiles/profile.h" | 10 #include "chrome/browser/profiles/profile.h" |
| 11 #include "chrome/browser/themes/theme_service.h" |
| 12 #include "chrome/browser/themes/theme_service_factory.h" |
11 #include "chrome/browser/ui/browser.h" | 13 #include "chrome/browser/ui/browser.h" |
12 #include "chrome/browser/ui/browser_tabstrip.h" | 14 #include "chrome/browser/ui/browser_tabstrip.h" |
13 #include "chrome/browser/ui/browser_window.h" | 15 #include "chrome/browser/ui/browser_window.h" |
14 #include "chrome/browser/ui/omnibox/location_bar.h" | 16 #include "chrome/browser/ui/omnibox/location_bar.h" |
15 #include "chrome/browser/ui/search/search.h" | 17 #include "chrome/browser/ui/search/search.h" |
16 #include "chrome/browser/ui/search/search_model.h" | 18 #include "chrome/browser/ui/search/search_model.h" |
17 #include "chrome/browser/ui/search/search_tab_helper.h" | 19 #include "chrome/browser/ui/search/search_tab_helper.h" |
18 #include "chrome/browser/ui/tab_contents/tab_contents.h" | 20 #include "chrome/browser/ui/tab_contents/tab_contents.h" |
19 #include "chrome/browser/ui/tabs/tab_strip_model.h" | 21 #include "chrome/browser/ui/tabs/tab_strip_model.h" |
20 #include "chrome/browser/ui/webui/ntp/app_launcher_handler.h" | 22 #include "chrome/browser/ui/webui/ntp/app_launcher_handler.h" |
21 #include "chrome/common/chrome_notification_types.h" | 23 #include "chrome/common/chrome_notification_types.h" |
22 #include "chrome/common/pref_names.h" | 24 #include "chrome/common/pref_names.h" |
23 #include "content/public/browser/notification_service.h" | 25 #include "content/public/browser/notification_service.h" |
24 #include "content/public/browser/web_contents.h" | 26 #include "content/public/browser/web_contents.h" |
| 27 #include "grit/theme_resources.h" |
| 28 #include "ui/gfx/color_utils.h" |
| 29 #include "ui/gfx/sys_color_change_listener.h" |
| 30 |
25 | 31 |
26 namespace chrome { | 32 namespace chrome { |
27 | 33 |
28 //////////////////////////////////////////////////////////////////////////////// | 34 //////////////////////////////////////////////////////////////////////////////// |
29 // BrowserInstantController, public: | 35 // BrowserInstantController, public: |
30 | 36 |
31 BrowserInstantController::BrowserInstantController(Browser* browser) | 37 BrowserInstantController::BrowserInstantController(Browser* browser) |
32 : browser_(browser), | 38 : browser_(browser), |
33 instant_(ALLOW_THIS_IN_INITIALIZER_LIST(this), | 39 instant_(ALLOW_THIS_IN_INITIALIZER_LIST(this), |
34 chrome::search::IsInstantExtendedAPIEnabled(browser->profile())), | 40 chrome::search::IsInstantExtendedAPIEnabled(browser->profile())), |
35 instant_unload_handler_(browser) { | 41 instant_unload_handler_(browser), |
| 42 initialized_theme_info_(false), |
| 43 theme_area_height_(0) { |
36 profile_pref_registrar_.Init(browser_->profile()->GetPrefs()); | 44 profile_pref_registrar_.Init(browser_->profile()->GetPrefs()); |
37 profile_pref_registrar_.Add(prefs::kInstantEnabled, this); | 45 profile_pref_registrar_.Add(prefs::kInstantEnabled, this); |
38 instant_.SetInstantEnabled(IsInstantEnabled(browser_->profile())); | 46 instant_.SetInstantEnabled(IsInstantEnabled(browser_->profile())); |
39 browser_->search_model()->AddObserver(this); | 47 browser_->search_model()->AddObserver(this); |
| 48 |
| 49 #if defined(ENABLE_THEMES) |
| 50 // Listen for theme installation. |
| 51 registrar_.Add(this, chrome::NOTIFICATION_BROWSER_THEME_CHANGED, |
| 52 content::Source<ThemeService>( |
| 53 ThemeServiceFactory::GetForProfile(browser_->profile()))); |
| 54 #endif // defined(ENABLE_THEMES) |
40 } | 55 } |
41 | 56 |
42 BrowserInstantController::~BrowserInstantController() { | 57 BrowserInstantController::~BrowserInstantController() { |
43 browser_->search_model()->RemoveObserver(this); | 58 browser_->search_model()->RemoveObserver(this); |
44 } | 59 } |
45 | 60 |
46 bool BrowserInstantController::IsInstantEnabled(Profile* profile) { | 61 bool BrowserInstantController::IsInstantEnabled(Profile* profile) { |
47 return profile && !profile->IsOffTheRecord() && profile->GetPrefs() && | 62 return profile && !profile->IsOffTheRecord() && profile->GetPrefs() && |
48 profile->GetPrefs()->GetBoolean(prefs::kInstantEnabled); | 63 profile->GetPrefs()->GetBoolean(prefs::kInstantEnabled); |
49 } | 64 } |
(...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
112 } | 127 } |
113 | 128 |
114 TabContents* BrowserInstantController::GetActiveTabContents() const { | 129 TabContents* BrowserInstantController::GetActiveTabContents() const { |
115 return browser_->tab_strip_model()->GetActiveTabContents(); | 130 return browser_->tab_strip_model()->GetActiveTabContents(); |
116 } | 131 } |
117 | 132 |
118 void BrowserInstantController::ActiveTabChanged() { | 133 void BrowserInstantController::ActiveTabChanged() { |
119 instant_.ActiveTabChanged(); | 134 instant_.ActiveTabChanged(); |
120 } | 135 } |
121 | 136 |
| 137 void BrowserInstantController::SetContentHeight(int height) { |
| 138 OnThemeAreaHeightChanged(height); |
| 139 } |
| 140 |
| 141 void BrowserInstantController::UpdateThemeInfoForPreview() { |
| 142 // Update theme background info and theme area height. |
| 143 // Initialize |theme_info| if necessary. |
| 144 // |OnThemeChanged| also updates theme area height if necessary. |
| 145 if (!initialized_theme_info_) |
| 146 OnThemeChanged(ThemeServiceFactory::GetForProfile(browser_->profile())); |
| 147 else |
| 148 OnThemeChanged(NULL); |
| 149 } |
| 150 |
122 //////////////////////////////////////////////////////////////////////////////// | 151 //////////////////////////////////////////////////////////////////////////////// |
123 // BrowserInstantController, PrefObserver implementation: | 152 // BrowserInstantController, PrefObserver implementation: |
124 | 153 |
125 void BrowserInstantController::OnPreferenceChanged( | 154 void BrowserInstantController::OnPreferenceChanged( |
126 PrefServiceBase* service, | 155 PrefServiceBase* service, |
127 const std::string& pref_name) { | 156 const std::string& pref_name) { |
128 instant_.SetInstantEnabled(IsInstantEnabled(browser_->profile())); | 157 instant_.SetInstantEnabled(IsInstantEnabled(browser_->profile())); |
129 } | 158 } |
130 | 159 |
131 //////////////////////////////////////////////////////////////////////////////// | 160 //////////////////////////////////////////////////////////////////////////////// |
132 // BrowserInstantController, search::SearchModelObserver implementation: | 161 // BrowserInstantController, search::SearchModelObserver implementation: |
133 | 162 |
134 void BrowserInstantController::ModeChanged(const search::Mode& old_mode, | 163 void BrowserInstantController::ModeChanged(const search::Mode& old_mode, |
135 const search::Mode& new_mode) { | 164 const search::Mode& new_mode) { |
136 instant_.SearchModeChanged(old_mode, new_mode); | 165 instant_.SearchModeChanged(old_mode, new_mode); |
| 166 |
| 167 // If mode is now |NTP|, send theme-related information to instant. |
| 168 if (new_mode.is_ntp()) |
| 169 UpdateThemeInfoForPreview(); |
| 170 } |
| 171 |
| 172 //////////////////////////////////////////////////////////////////////////////// |
| 173 // BrowserInstantController, content::NotificationObserver implementation: |
| 174 |
| 175 void BrowserInstantController::Observe( |
| 176 int type, |
| 177 const content::NotificationSource& source, |
| 178 const content::NotificationDetails& details) { |
| 179 #if defined(ENABLE_THEMES) |
| 180 DCHECK_EQ(chrome::NOTIFICATION_BROWSER_THEME_CHANGED, type); |
| 181 OnThemeChanged(content::Source<ThemeService>(source).ptr()); |
| 182 #endif // defined(ENABLE_THEMES) |
| 183 } |
| 184 |
| 185 void BrowserInstantController::OnThemeChanged(ThemeService* theme_service) { |
| 186 if (theme_service) { // Get theme information from theme service. |
| 187 theme_info_ = ThemeBackgroundInfo(); |
| 188 |
| 189 // Set theme background color. |
| 190 SkColor background_color = |
| 191 theme_service->GetColor(ThemeService::COLOR_NTP_BACKGROUND); |
| 192 if (gfx::IsInvertedColorScheme()) |
| 193 background_color = color_utils::InvertColor(background_color); |
| 194 theme_info_.color_r = SkColorGetR(background_color); |
| 195 theme_info_.color_g = SkColorGetG(background_color); |
| 196 theme_info_.color_b = SkColorGetB(background_color); |
| 197 theme_info_.color_a = SkColorGetA(background_color); |
| 198 |
| 199 if (theme_service->HasCustomImage(IDR_THEME_NTP_BACKGROUND)) { |
| 200 // Set theme id for theme background image url. |
| 201 theme_info_.theme_id = theme_service->GetThemeID(); |
| 202 |
| 203 // Set theme background image horizontal alignment. |
| 204 int alignment = 0; |
| 205 theme_service->GetDisplayProperty(ThemeService::NTP_BACKGROUND_ALIGNMENT, |
| 206 &alignment); |
| 207 if (alignment & ThemeService::ALIGN_LEFT) { |
| 208 theme_info_.image_horizontal_alignment = THEME_BKGRND_IMAGE_ALIGN_LEFT; |
| 209 } else if (alignment & ThemeService::ALIGN_RIGHT) { |
| 210 theme_info_.image_horizontal_alignment = THEME_BKGRND_IMAGE_ALIGN_RIGHT; |
| 211 } else { // ALIGN_CENTER |
| 212 theme_info_.image_horizontal_alignment = |
| 213 THEME_BKGRND_IMAGE_ALIGN_CENTER; |
| 214 } |
| 215 |
| 216 // Set theme background image vertical alignment. |
| 217 if (alignment & ThemeService::ALIGN_TOP) |
| 218 theme_info_.image_vertical_alignment = THEME_BKGRND_IMAGE_ALIGN_TOP; |
| 219 else if (alignment & ThemeService::ALIGN_BOTTOM) |
| 220 theme_info_.image_vertical_alignment = THEME_BKGRND_IMAGE_ALIGN_BOTTOM; |
| 221 else // ALIGN_CENTER |
| 222 theme_info_.image_vertical_alignment = THEME_BKGRND_IMAGE_ALIGN_CENTER; |
| 223 |
| 224 // Set theme background image tiling. |
| 225 int tiling = 0; |
| 226 theme_service->GetDisplayProperty(ThemeService::NTP_BACKGROUND_TILING, |
| 227 &tiling); |
| 228 switch (tiling) { |
| 229 case ThemeService::NO_REPEAT: |
| 230 theme_info_.image_tiling = THEME_BKGRND_IMAGE_NO_REPEAT; |
| 231 break; |
| 232 case ThemeService::REPEAT_X: |
| 233 theme_info_.image_tiling = THEME_BKGRND_IMAGE_REPEAT_X; |
| 234 break; |
| 235 case ThemeService::REPEAT_Y: |
| 236 theme_info_.image_tiling = THEME_BKGRND_IMAGE_REPEAT_Y; |
| 237 break; |
| 238 case ThemeService::REPEAT: |
| 239 theme_info_.image_tiling = THEME_BKGRND_IMAGE_REPEAT; |
| 240 break; |
| 241 } |
| 242 |
| 243 // Set theme background image height. |
| 244 gfx::ImageSkia* image = theme_service->GetImageSkiaNamed( |
| 245 IDR_THEME_NTP_BACKGROUND); |
| 246 DCHECK(image); |
| 247 theme_info_.image_height = image->height(); |
| 248 } |
| 249 |
| 250 initialized_theme_info_ = true; |
| 251 } |
| 252 |
| 253 DCHECK(initialized_theme_info_); |
| 254 |
| 255 if (browser_->search_model()->mode().is_ntp()) { |
| 256 instant_.ThemeChanged(theme_info_); |
| 257 |
| 258 // Theme area height is only sent to preview for non-top-aligned images; |
| 259 // new theme may have a different alignment that requires preview to know |
| 260 // theme area height. |
| 261 OnThemeAreaHeightChanged(theme_area_height_); |
| 262 } |
| 263 } |
| 264 |
| 265 void BrowserInstantController::OnThemeAreaHeightChanged(int height) { |
| 266 theme_area_height_ = height; |
| 267 |
| 268 // Notify preview only if mode is |NTP| and theme background image is not top- |
| 269 // aligned; top-aligned images don't need theme area height to determine which |
| 270 // part of the image overlay should draw, 'cos the origin is top-left. |
| 271 if (!browser_->search_model()->mode().is_ntp() || |
| 272 theme_info_.theme_id.empty() || |
| 273 theme_info_.image_vertical_alignment == THEME_BKGRND_IMAGE_ALIGN_TOP) { |
| 274 return; |
| 275 } |
| 276 instant_.ThemeAreaHeightChanged(theme_area_height_); |
137 } | 277 } |
138 | 278 |
139 } // namespace chrome | 279 } // namespace chrome |
OLD | NEW |