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 "base/string_number_conversions.h" | |
8 #include "base/stringprintf.h" | |
9 #include "base/utf_string_conversions.h" | |
7 #include "chrome/browser/browser_shutdown.h" | 10 #include "chrome/browser/browser_shutdown.h" |
8 #include "chrome/browser/extensions/extension_service.h" | 11 #include "chrome/browser/extensions/extension_service.h" |
9 #include "chrome/browser/instant/instant_controller.h" | 12 #include "chrome/browser/instant/instant_controller.h" |
10 #include "chrome/browser/prefs/pref_service.h" | 13 #include "chrome/browser/prefs/pref_service.h" |
11 #include "chrome/browser/profiles/profile.h" | 14 #include "chrome/browser/profiles/profile.h" |
15 #include "chrome/browser/themes/theme_service.h" | |
16 #include "chrome/browser/themes/theme_service_factory.h" | |
12 #include "chrome/browser/ui/browser.h" | 17 #include "chrome/browser/ui/browser.h" |
13 #include "chrome/browser/ui/browser_tabstrip.h" | 18 #include "chrome/browser/ui/browser_tabstrip.h" |
14 #include "chrome/browser/ui/browser_window.h" | 19 #include "chrome/browser/ui/browser_window.h" |
15 #include "chrome/browser/ui/omnibox/location_bar.h" | 20 #include "chrome/browser/ui/omnibox/location_bar.h" |
16 #include "chrome/browser/ui/search/search_model.h" | 21 #include "chrome/browser/ui/search/search_model.h" |
17 #include "chrome/browser/ui/search/search_tab_helper.h" | 22 #include "chrome/browser/ui/search/search_tab_helper.h" |
18 #include "chrome/browser/ui/tabs/tab_strip_model.h" | 23 #include "chrome/browser/ui/tabs/tab_strip_model.h" |
19 #include "chrome/browser/ui/tab_contents/tab_contents.h" | 24 #include "chrome/browser/ui/tab_contents/tab_contents.h" |
20 #include "chrome/browser/ui/webui/ntp/app_launcher_handler.h" | 25 #include "chrome/browser/ui/webui/ntp/app_launcher_handler.h" |
21 #include "chrome/common/chrome_notification_types.h" | 26 #include "chrome/common/chrome_notification_types.h" |
22 #include "chrome/common/pref_names.h" | 27 #include "chrome/common/pref_names.h" |
23 #include "content/public/browser/notification_service.h" | 28 #include "content/public/browser/notification_service.h" |
24 #include "content/public/browser/web_contents.h" | 29 #include "content/public/browser/web_contents.h" |
30 #include "grit/theme_resources.h" | |
31 #include "ui/base/theme_provider.h" | |
32 #include "ui/gfx/color_utils.h" | |
33 #include "ui/gfx/sys_color_change_listener.h" | |
34 | |
25 | 35 |
26 namespace { | 36 namespace { |
27 | 37 |
38 const char kCSSBackgroundImageFormat[] = | |
39 "-webkit-image-set(url(chrome://theme/IDR_THEME_BACKGROUND?%s) 1x)"; | |
palmer
2012/11/19 18:26:51
String interpolation is generally a security dange
kuan
2012/11/19 22:06:07
Done.
| |
40 | |
41 const char kCSSBackgroundColorFormat[] = "rgba(%d,%d,%d,%s)"; | |
42 | |
28 // Returns true iff the search model for |tab| is in an NTP state, that is, a | 43 // 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 | 44 // state in which the Instant overlay may be showing custom NTP content in |
30 // EXTENDED mode. | 45 // EXTENDED mode. |
31 bool IsSearchModelNTP(content::WebContents* tab) { | 46 bool IsSearchModelNTP(content::WebContents* tab) { |
32 if (!tab) | 47 if (!tab) |
33 return false; | 48 return false; |
34 chrome::search::SearchModel* model = | 49 chrome::search::SearchModel* model = |
35 chrome::search::SearchTabHelper::FromWebContents(tab)->model(); | 50 chrome::search::SearchTabHelper::FromWebContents(tab)->model(); |
36 return model && model->mode().is_ntp(); | 51 return model && model->mode().is_ntp(); |
37 } | 52 } |
38 | 53 |
39 } // namespace | 54 } // namespace |
40 | 55 |
41 namespace chrome { | 56 namespace chrome { |
42 | 57 |
43 //////////////////////////////////////////////////////////////////////////////// | 58 //////////////////////////////////////////////////////////////////////////////// |
44 // BrowserInstantController, public: | 59 // BrowserInstantController, public: |
45 | 60 |
46 BrowserInstantController::BrowserInstantController(Browser* browser) | 61 BrowserInstantController::BrowserInstantController(Browser* browser) |
47 : browser_(browser), | 62 : browser_(browser), |
48 instant_unload_handler_(browser) { | 63 instant_unload_handler_(browser), |
64 theme_image_alignment_(0), | |
65 theme_area_height_(0) { | |
49 profile_pref_registrar_.Init(browser_->profile()->GetPrefs()); | 66 profile_pref_registrar_.Init(browser_->profile()->GetPrefs()); |
50 profile_pref_registrar_.Add(prefs::kInstantEnabled, this); | 67 profile_pref_registrar_.Add(prefs::kInstantEnabled, this); |
51 ResetInstant(); | 68 ResetInstant(); |
52 browser_->tab_strip_model()->AddObserver(this); | 69 browser_->tab_strip_model()->AddObserver(this); |
53 browser_->search_model()->AddObserver(this); | 70 browser_->search_model()->AddObserver(this); |
71 | |
72 #if defined(ENABLE_THEMES) | |
73 // Listen for theme installation. | |
74 registrar_.Add(this, chrome::NOTIFICATION_BROWSER_THEME_CHANGED, | |
75 content::Source<ThemeService>( | |
76 ThemeServiceFactory::GetForProfile(browser_->profile()))); | |
77 #endif // defined(ENABLE_THEMES) | |
54 } | 78 } |
55 | 79 |
56 BrowserInstantController::~BrowserInstantController() { | 80 BrowserInstantController::~BrowserInstantController() { |
57 browser_->tab_strip_model()->RemoveObserver(this); | 81 browser_->tab_strip_model()->RemoveObserver(this); |
58 browser_->search_model()->RemoveObserver(this); | 82 browser_->search_model()->RemoveObserver(this); |
59 } | 83 } |
60 | 84 |
61 bool BrowserInstantController::OpenInstant(WindowOpenDisposition disposition) { | 85 bool BrowserInstantController::OpenInstant(WindowOpenDisposition disposition) { |
62 // NEW_BACKGROUND_TAB results in leaving the omnibox open, so we don't attempt | 86 // NEW_BACKGROUND_TAB results in leaving the omnibox open, so we don't attempt |
63 // to use the Instant preview. | 87 // to use the Instant preview. |
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
112 void BrowserInstantController::InstantPreviewFocused() { | 136 void BrowserInstantController::InstantPreviewFocused() { |
113 // NOTE: This is only invoked on aura. | 137 // NOTE: This is only invoked on aura. |
114 browser_->window()->WebContentsFocused( | 138 browser_->window()->WebContentsFocused( |
115 instant_->GetPreviewContents()->web_contents()); | 139 instant_->GetPreviewContents()->web_contents()); |
116 } | 140 } |
117 | 141 |
118 TabContents* BrowserInstantController::GetActiveTabContents() const { | 142 TabContents* BrowserInstantController::GetActiveTabContents() const { |
119 return chrome::GetActiveTabContents(browser_); | 143 return chrome::GetActiveTabContents(browser_); |
120 } | 144 } |
121 | 145 |
146 void BrowserInstantController::SetContentHeight(int height) { | |
147 OnThemeAreaHeightChanged(height); | |
148 } | |
149 | |
150 void BrowserInstantController::UpdateThemeRelatedInfoForPreview() { | |
151 // Update theme background info and theme area height. | |
152 // |theme_info_.color_rgba| must always be filled, so if it's empty, | |
153 // |theme_info_| hasn't been initialized. | |
154 // |OnThemeChanged| also updates theme area height. | |
155 if (theme_info_.color_rgba.length() == 0) | |
156 OnThemeChanged(ThemeServiceFactory::GetForProfile(browser_->profile())); | |
157 else | |
158 OnThemeChanged(NULL); | |
159 } | |
160 | |
122 //////////////////////////////////////////////////////////////////////////////// | 161 //////////////////////////////////////////////////////////////////////////////// |
123 // BrowserInstantController, PrefObserver implementation: | 162 // BrowserInstantController, PrefObserver implementation: |
124 | 163 |
125 void BrowserInstantController::OnPreferenceChanged( | 164 void BrowserInstantController::OnPreferenceChanged( |
126 PrefServiceBase* service, | 165 PrefServiceBase* service, |
127 const std::string& pref_name) { | 166 const std::string& pref_name) { |
128 DCHECK_EQ(std::string(prefs::kInstantEnabled), pref_name); | 167 DCHECK_EQ(std::string(prefs::kInstantEnabled), pref_name); |
129 ResetInstant(); | 168 ResetInstant(); |
130 } | 169 } |
131 | 170 |
(...skipping 17 matching lines...) Expand all Loading... | |
149 | 188 |
150 void BrowserInstantController::TabStripEmpty() { | 189 void BrowserInstantController::TabStripEmpty() { |
151 instant_.reset(); | 190 instant_.reset(); |
152 } | 191 } |
153 | 192 |
154 //////////////////////////////////////////////////////////////////////////////// | 193 //////////////////////////////////////////////////////////////////////////////// |
155 // BrowserInstantController, search::SearchModelObserver implementation: | 194 // BrowserInstantController, search::SearchModelObserver implementation: |
156 | 195 |
157 void BrowserInstantController::ModeChanged(const search::Mode& old_mode, | 196 void BrowserInstantController::ModeChanged(const search::Mode& old_mode, |
158 const search::Mode& new_mode) { | 197 const search::Mode& new_mode) { |
159 if (instant()) | 198 if (instant()) { |
160 instant_->OnActiveTabModeChanged(new_mode); | 199 instant_->OnActiveTabModeChanged(new_mode); |
200 | |
201 // If mode is now |NTP|, send theme-related information to instant. | |
202 if (new_mode.is_ntp()) | |
203 OnThemeChanged(NULL); | |
204 } | |
161 } | 205 } |
162 | 206 |
163 //////////////////////////////////////////////////////////////////////////////// | 207 //////////////////////////////////////////////////////////////////////////////// |
164 // BrowserInstantController, private: | 208 // BrowserInstantController, private: |
165 | 209 |
210 void BrowserInstantController::Observe( | |
211 int type, | |
212 const content::NotificationSource& source, | |
213 const content::NotificationDetails& details) { | |
214 #if defined(ENABLE_THEMES) | |
215 DCHECK_EQ(chrome::NOTIFICATION_BROWSER_THEME_CHANGED, type); | |
216 OnThemeChanged(content::Source<ThemeService>(source).ptr()); | |
217 #endif // defined(ENABLE_THEMES) | |
218 } | |
219 | |
166 void BrowserInstantController::ResetInstant() { | 220 void BrowserInstantController::ResetInstant() { |
167 instant_.reset( | 221 instant_.reset( |
168 !browser_shutdown::ShuttingDownWithoutClosingBrowsers() && | 222 !browser_shutdown::ShuttingDownWithoutClosingBrowsers() && |
169 browser_->is_type_tabbed() ? | 223 browser_->is_type_tabbed() ? |
170 InstantController::CreateInstant(browser_->profile(), this) : NULL); | 224 InstantController::CreateInstant(browser_->profile(), this) : NULL); |
171 | 225 |
172 // Notify any observers that they need to reset. | 226 // Notify any observers that they need to reset. |
173 content::NotificationService::current()->Notify( | 227 content::NotificationService::current()->Notify( |
174 chrome::NOTIFICATION_BROWSER_INSTANT_RESET, | 228 chrome::NOTIFICATION_BROWSER_INSTANT_RESET, |
175 content::Source<BrowserInstantController>(this), | 229 content::Source<BrowserInstantController>(this), |
176 content::NotificationService::NoDetails()); | 230 content::NotificationService::NoDetails()); |
177 } | 231 } |
178 | 232 |
233 void BrowserInstantController::OnThemeChanged( | |
234 ui::ThemeProvider* theme_provider) { | |
235 if (theme_provider) { // Get theme information from theme provider. | |
236 theme_info_ = ThemeBackgroundInfo(); | |
237 theme_image_alignment_ = 0; | |
238 | |
239 // Set theme background color. | |
240 SkColor background_color = | |
241 theme_provider->GetColor(ThemeService::COLOR_NTP_BACKGROUND); | |
242 if (gfx::IsInvertedColorScheme()) | |
243 background_color = color_utils::InvertColor(background_color); | |
244 theme_info_.color_rgba = UTF8ToUTF16( | |
245 // Convert the alpha using DoubleToString because StringPrintf will use | |
246 // locale specific formatters (e.g., use , instead of . in German). | |
247 base::StringPrintf( | |
248 kCSSBackgroundColorFormat, | |
249 SkColorGetR(background_color), | |
250 SkColorGetG(background_color), | |
251 SkColorGetB(background_color), | |
252 base::DoubleToString(SkColorGetA(background_color) / 255.0). | |
palmer
2012/11/19 18:26:51
This is you creating an honest message, but genera
kuan
2012/11/19 22:06:07
Done.
| |
253 c_str())); | |
254 | |
255 if (theme_provider->HasCustomImage(IDR_THEME_NTP_BACKGROUND)) { | |
256 // Set theme background image url. | |
257 theme_info_.image_url = UTF8ToUTF16( | |
258 base::StringPrintf(kCSSBackgroundImageFormat, | |
palmer
2012/11/19 18:26:51
For example:
-webkit-image-set(url(chrome://theme
kuan
2012/11/19 22:06:07
Done.
| |
259 browser_->profile()->GetPrefs()->GetString( | |
260 prefs::kCurrentThemeID).c_str())); | |
261 | |
262 // Set theme background image horizontal alignment. | |
263 theme_provider->GetDisplayProperty( | |
264 ThemeService::NTP_BACKGROUND_ALIGNMENT, &theme_image_alignment_); | |
265 std::string alignment; | |
266 if (theme_image_alignment_ & ThemeService::ALIGN_LEFT) | |
267 alignment = ThemeService::kAlignmentLeft; | |
268 else if (theme_image_alignment_& ThemeService::ALIGN_RIGHT) | |
269 alignment = ThemeService::kAlignmentRight; | |
270 else // ALIGN_CENTER | |
271 alignment = ThemeService::kAlignmentCenter; | |
272 theme_info_.image_horizontal_alignment = UTF8ToUTF16(alignment); | |
palmer
2012/11/19 18:26:51
These conversions are another efficiency reason to
kuan
2012/11/19 22:06:07
Done.
| |
273 | |
274 // Set theme background image vertical alignment. | |
275 if (theme_image_alignment_& ThemeService::ALIGN_TOP) | |
palmer
2012/11/19 18:26:51
Oh, you've already got enums for this. So ignore m
kuan
2012/11/19 22:06:07
i did as per ur suggestion. it works, EXCEPT when
| |
276 alignment = ThemeService::kAlignmentTop; | |
277 else if (theme_image_alignment_& ThemeService::ALIGN_BOTTOM) | |
278 alignment = ThemeService::kAlignmentBottom; | |
279 else // ALIGN_CENTER | |
280 alignment = ThemeService::kAlignmentCenter; | |
281 theme_info_.image_vertical_alignment = UTF8ToUTF16(alignment); | |
282 | |
283 // Set tiling of theme background image. | |
284 int repeat_mode = 0; | |
285 theme_provider->GetDisplayProperty( | |
286 ThemeService::NTP_BACKGROUND_TILING, &repeat_mode); | |
287 theme_info_.image_tiling = UTF8ToUTF16( | |
288 ThemeService::TilingToString(repeat_mode)); | |
289 | |
290 // Set theme background image height. | |
291 gfx::ImageSkia* image = theme_provider->GetImageSkiaNamed( | |
292 IDR_THEME_NTP_BACKGROUND); | |
293 DCHECK(image); | |
294 theme_info_.image_height = image->height(); | |
295 } | |
296 } | |
297 | |
298 if (instant() && browser_->search_model()->mode().is_ntp()) { | |
299 instant()->OnThemeChanged(theme_info_); | |
sreeram
2012/11/19 18:12:02
Nit: instant() -> instant_ (it works on both lines
kuan
2012/11/19 22:06:07
Done.
| |
300 | |
301 // Theme area height is only sent to preview for non-top-aligned images; | |
302 // new theme may have a different alignment that requires preview to know | |
303 // theme area height. | |
304 OnThemeAreaHeightChanged(theme_area_height_); | |
305 } | |
306 } | |
307 | |
308 void BrowserInstantController::OnThemeAreaHeightChanged(int height) { | |
309 theme_area_height_ = height; | |
310 | |
311 // Notify preview if mode is |NTP| and theme background image is not top- | |
312 // aligned; top-aligned images don't need theme area height to determine which | |
313 // part of the image overlay should draw, 'cos the origin is top-left. | |
314 bool notify = instant() && browser_->search_model()->mode().is_ntp() && | |
315 theme_info_.image_url.length() > 0; | |
316 if (!notify) | |
317 return; | |
318 if (theme_image_alignment_ & ThemeService::ALIGN_TOP) | |
319 notify = false; | |
320 else if (theme_image_alignment_ & ThemeService::ALIGN_BOTTOM) | |
321 notify = true; | |
322 else // ALIGN_CENTER | |
323 notify = true; | |
324 if (notify) | |
325 instant()->OnThemeAreaHeightChanged(theme_area_height_); | |
sreeram
2012/11/19 18:12:02
Why bother with the notify variable? Why not just
kuan
2012/11/19 22:06:07
Done.
| |
326 } | |
327 | |
179 } // namespace chrome | 328 } // namespace chrome |
OLD | NEW |