OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 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/instant/instant_controller.h" | 9 #include "chrome/browser/instant/instant_controller.h" |
10 #include "chrome/browser/prefs/pref_service.h" | 10 #include "chrome/browser/prefs/pref_service.h" |
11 #include "chrome/browser/profiles/profile.h" | 11 #include "chrome/browser/profiles/profile.h" |
| 12 #include "chrome/browser/themes/theme_service.h" |
| 13 #include "chrome/browser/themes/theme_service_factory.h" |
12 #include "chrome/browser/ui/browser.h" | 14 #include "chrome/browser/ui/browser.h" |
13 #include "chrome/browser/ui/browser_tabstrip.h" | 15 #include "chrome/browser/ui/browser_tabstrip.h" |
14 #include "chrome/browser/ui/browser_window.h" | 16 #include "chrome/browser/ui/browser_window.h" |
15 #include "chrome/browser/ui/omnibox/location_bar.h" | 17 #include "chrome/browser/ui/omnibox/location_bar.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/tabs/tab_strip_model.h" | 20 #include "chrome/browser/ui/tabs/tab_strip_model.h" |
19 #include "chrome/browser/ui/tab_contents/tab_contents.h" | 21 #include "chrome/browser/ui/tab_contents/tab_contents.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 { | 32 namespace { |
27 | 33 |
28 // Returns true iff the search model for |tab| is in an NTP state, that is, a | 34 // Returns true iff the search model for |tab| is in an NTP state, that is, a |
29 // state in which the Instant overlay may be showing custom NTP content in | 35 // state in which the Instant overlay may be showing custom NTP content in |
30 // EXTENDED mode. | 36 // EXTENDED mode. |
31 bool IsSearchModelNTP(content::WebContents* tab) { | 37 bool IsSearchModelNTP(content::WebContents* tab) { |
32 if (!tab) | 38 if (!tab) |
33 return false; | 39 return false; |
34 chrome::search::SearchModel* model = | 40 chrome::search::SearchModel* model = |
35 chrome::search::SearchTabHelper::FromWebContents(tab)->model(); | 41 chrome::search::SearchTabHelper::FromWebContents(tab)->model(); |
36 return model && model->mode().is_ntp(); | 42 return model && model->mode().is_ntp(); |
37 } | 43 } |
38 | 44 |
39 } // namespace | 45 } // namespace |
40 | 46 |
41 namespace chrome { | 47 namespace chrome { |
42 | 48 |
43 //////////////////////////////////////////////////////////////////////////////// | 49 //////////////////////////////////////////////////////////////////////////////// |
44 // BrowserInstantController, public: | 50 // BrowserInstantController, public: |
45 | 51 |
46 BrowserInstantController::BrowserInstantController(Browser* browser) | 52 BrowserInstantController::BrowserInstantController(Browser* browser) |
47 : browser_(browser), | 53 : browser_(browser), |
48 instant_unload_handler_(browser) { | 54 instant_unload_handler_(browser), |
| 55 initialized_theme_info_(false), |
| 56 theme_area_height_(0) { |
49 profile_pref_registrar_.Init(browser_->profile()->GetPrefs()); | 57 profile_pref_registrar_.Init(browser_->profile()->GetPrefs()); |
50 profile_pref_registrar_.Add(prefs::kInstantEnabled, this); | 58 profile_pref_registrar_.Add(prefs::kInstantEnabled, this); |
51 ResetInstant(); | 59 ResetInstant(); |
52 browser_->tab_strip_model()->AddObserver(this); | 60 browser_->tab_strip_model()->AddObserver(this); |
53 browser_->search_model()->AddObserver(this); | 61 browser_->search_model()->AddObserver(this); |
| 62 |
| 63 #if defined(ENABLE_THEMES) |
| 64 // Listen for theme installation. |
| 65 registrar_.Add(this, chrome::NOTIFICATION_BROWSER_THEME_CHANGED, |
| 66 content::Source<ThemeService>( |
| 67 ThemeServiceFactory::GetForProfile(browser_->profile()))); |
| 68 #endif // defined(ENABLE_THEMES) |
54 } | 69 } |
55 | 70 |
56 BrowserInstantController::~BrowserInstantController() { | 71 BrowserInstantController::~BrowserInstantController() { |
57 browser_->tab_strip_model()->RemoveObserver(this); | 72 browser_->tab_strip_model()->RemoveObserver(this); |
58 browser_->search_model()->RemoveObserver(this); | 73 browser_->search_model()->RemoveObserver(this); |
59 } | 74 } |
60 | 75 |
61 bool BrowserInstantController::OpenInstant(WindowOpenDisposition disposition) { | 76 bool BrowserInstantController::OpenInstant(WindowOpenDisposition disposition) { |
62 // NEW_BACKGROUND_TAB results in leaving the omnibox open, so we don't attempt | 77 // NEW_BACKGROUND_TAB results in leaving the omnibox open, so we don't attempt |
63 // to use the Instant preview. | 78 // to use the Instant preview. |
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
112 void BrowserInstantController::InstantPreviewFocused() { | 127 void BrowserInstantController::InstantPreviewFocused() { |
113 // NOTE: This is only invoked on aura. | 128 // NOTE: This is only invoked on aura. |
114 browser_->window()->WebContentsFocused( | 129 browser_->window()->WebContentsFocused( |
115 instant_->GetPreviewContents()->web_contents()); | 130 instant_->GetPreviewContents()->web_contents()); |
116 } | 131 } |
117 | 132 |
118 TabContents* BrowserInstantController::GetActiveTabContents() const { | 133 TabContents* BrowserInstantController::GetActiveTabContents() const { |
119 return chrome::GetActiveTabContents(browser_); | 134 return chrome::GetActiveTabContents(browser_); |
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 DCHECK_EQ(std::string(prefs::kInstantEnabled), pref_name); | 157 DCHECK_EQ(std::string(prefs::kInstantEnabled), pref_name); |
129 ResetInstant(); | 158 ResetInstant(); |
130 } | 159 } |
131 | 160 |
(...skipping 17 matching lines...) Expand all Loading... |
149 | 178 |
150 void BrowserInstantController::TabStripEmpty() { | 179 void BrowserInstantController::TabStripEmpty() { |
151 instant_.reset(); | 180 instant_.reset(); |
152 } | 181 } |
153 | 182 |
154 //////////////////////////////////////////////////////////////////////////////// | 183 //////////////////////////////////////////////////////////////////////////////// |
155 // BrowserInstantController, search::SearchModelObserver implementation: | 184 // BrowserInstantController, search::SearchModelObserver implementation: |
156 | 185 |
157 void BrowserInstantController::ModeChanged(const search::Mode& old_mode, | 186 void BrowserInstantController::ModeChanged(const search::Mode& old_mode, |
158 const search::Mode& new_mode) { | 187 const search::Mode& new_mode) { |
159 if (instant()) | 188 if (instant()) { |
160 instant_->OnActiveTabModeChanged(new_mode); | 189 instant_->OnActiveTabModeChanged(new_mode); |
| 190 |
| 191 // If mode is now |NTP|, send theme-related information to instant. |
| 192 if (new_mode.is_ntp()) |
| 193 UpdateThemeInfoForPreview(); |
| 194 } |
161 } | 195 } |
162 | 196 |
163 //////////////////////////////////////////////////////////////////////////////// | 197 //////////////////////////////////////////////////////////////////////////////// |
164 // BrowserInstantController, private: | 198 // BrowserInstantController, private: |
165 | 199 |
| 200 void BrowserInstantController::Observe( |
| 201 int type, |
| 202 const content::NotificationSource& source, |
| 203 const content::NotificationDetails& details) { |
| 204 #if defined(ENABLE_THEMES) |
| 205 DCHECK_EQ(chrome::NOTIFICATION_BROWSER_THEME_CHANGED, type); |
| 206 OnThemeChanged(content::Source<ThemeService>(source).ptr()); |
| 207 #endif // defined(ENABLE_THEMES) |
| 208 } |
| 209 |
166 void BrowserInstantController::ResetInstant() { | 210 void BrowserInstantController::ResetInstant() { |
167 instant_.reset( | 211 instant_.reset( |
168 !browser_shutdown::ShuttingDownWithoutClosingBrowsers() && | 212 !browser_shutdown::ShuttingDownWithoutClosingBrowsers() && |
169 browser_->is_type_tabbed() ? | 213 browser_->is_type_tabbed() ? |
170 InstantController::CreateInstant(browser_->profile(), this) : NULL); | 214 InstantController::CreateInstant(browser_->profile(), this) : NULL); |
171 | 215 |
172 // Notify any observers that they need to reset. | 216 // Notify any observers that they need to reset. |
173 content::NotificationService::current()->Notify( | 217 content::NotificationService::current()->Notify( |
174 chrome::NOTIFICATION_BROWSER_INSTANT_RESET, | 218 chrome::NOTIFICATION_BROWSER_INSTANT_RESET, |
175 content::Source<BrowserInstantController>(this), | 219 content::Source<BrowserInstantController>(this), |
176 content::NotificationService::NoDetails()); | 220 content::NotificationService::NoDetails()); |
177 } | 221 } |
178 | 222 |
| 223 void BrowserInstantController::OnThemeChanged(ThemeService* theme_service) { |
| 224 if (theme_service) { // Get theme information from theme service. |
| 225 theme_info_ = ThemeBackgroundInfo(); |
| 226 |
| 227 // Set theme background color. |
| 228 SkColor background_color = |
| 229 theme_service->GetColor(ThemeService::COLOR_NTP_BACKGROUND); |
| 230 if (gfx::IsInvertedColorScheme()) |
| 231 background_color = color_utils::InvertColor(background_color); |
| 232 theme_info_.color_r = SkColorGetR(background_color); |
| 233 theme_info_.color_g = SkColorGetG(background_color); |
| 234 theme_info_.color_b = SkColorGetB(background_color); |
| 235 theme_info_.color_a = SkColorGetA(background_color); |
| 236 |
| 237 if (theme_service->HasCustomImage(IDR_THEME_NTP_BACKGROUND)) { |
| 238 // Set theme id for theme background image url. |
| 239 theme_info_.theme_id = theme_service->GetThemeID(); |
| 240 |
| 241 // Set theme background image alignment. |
| 242 theme_service->GetDisplayProperty(ThemeService::NTP_BACKGROUND_ALIGNMENT, |
| 243 &theme_info_.image_alignment); |
| 244 |
| 245 // Set theme background image tiling. |
| 246 theme_service->GetDisplayProperty(ThemeService::NTP_BACKGROUND_TILING, |
| 247 &theme_info_.image_tiling); |
| 248 |
| 249 // Set theme background image height. |
| 250 gfx::ImageSkia* image = theme_service->GetImageSkiaNamed( |
| 251 IDR_THEME_NTP_BACKGROUND); |
| 252 DCHECK(image); |
| 253 theme_info_.image_height = image->height(); |
| 254 } |
| 255 |
| 256 initialized_theme_info_ = true; |
| 257 } |
| 258 |
| 259 DCHECK(initialized_theme_info_); |
| 260 |
| 261 if (instant() && browser_->search_model()->mode().is_ntp()) { |
| 262 instant_->ThemeChanged(theme_info_); |
| 263 |
| 264 // Theme area height is only sent to preview for non-top-aligned images; |
| 265 // new theme may have a different alignment that requires preview to know |
| 266 // theme area height. |
| 267 OnThemeAreaHeightChanged(theme_area_height_); |
| 268 } |
| 269 } |
| 270 |
| 271 void BrowserInstantController::OnThemeAreaHeightChanged(int height) { |
| 272 theme_area_height_ = height; |
| 273 |
| 274 // Notify preview only if mode is |NTP| and theme background image is not top- |
| 275 // aligned; top-aligned images don't need theme area height to determine which |
| 276 // part of the image overlay should draw, 'cos the origin is top-left. |
| 277 if (!instant() || !browser_->search_model()->mode().is_ntp() || |
| 278 theme_info_.theme_id.empty() || |
| 279 theme_info_.image_alignment & ThemeService::ALIGN_TOP) { |
| 280 return; |
| 281 } |
| 282 instant_->ThemeAreaHeightChanged(theme_area_height_); |
| 283 } |
| 284 |
179 } // namespace chrome | 285 } // namespace chrome |
OLD | NEW |