Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(913)

Side by Side Diff: chrome/browser/ui/browser_instant_controller.cc

Issue 16413002: Moved theme related state from BrowserInstantController to InstantService. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Rebase Created 7 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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 "base/bind.h" 7 #include "base/bind.h"
8 #include "base/prefs/pref_service.h" 8 #include "base/prefs/pref_service.h"
9 #include "chrome/browser/extensions/extension_service.h" 9 #include "chrome/browser/extensions/extension_service.h"
10 #include "chrome/browser/extensions/extension_web_ui.h" 10 #include "chrome/browser/extensions/extension_web_ui.h"
11 #include "chrome/browser/profiles/profile.h" 11 #include "chrome/browser/profiles/profile.h"
12 #include "chrome/browser/search/instant_service.h" 12 #include "chrome/browser/search/instant_service.h"
13 #include "chrome/browser/search/instant_service_factory.h" 13 #include "chrome/browser/search/instant_service_factory.h"
14 #include "chrome/browser/search/search.h" 14 #include "chrome/browser/search/search.h"
15 #include "chrome/browser/search_engines/template_url.h" 15 #include "chrome/browser/search_engines/template_url.h"
16 #include "chrome/browser/search_engines/template_url_service.h" 16 #include "chrome/browser/search_engines/template_url_service.h"
17 #include "chrome/browser/search_engines/template_url_service_factory.h" 17 #include "chrome/browser/search_engines/template_url_service_factory.h"
18 #include "chrome/browser/themes/theme_properties.h"
19 #include "chrome/browser/themes/theme_service.h"
20 #include "chrome/browser/themes/theme_service_factory.h"
21 #include "chrome/browser/ui/browser.h" 18 #include "chrome/browser/ui/browser.h"
22 #include "chrome/browser/ui/browser_window.h" 19 #include "chrome/browser/ui/browser_window.h"
23 #include "chrome/browser/ui/omnibox/location_bar.h" 20 #include "chrome/browser/ui/omnibox/location_bar.h"
24 #include "chrome/browser/ui/omnibox/omnibox_view.h" 21 #include "chrome/browser/ui/omnibox/omnibox_view.h"
25 #include "chrome/browser/ui/search/search_model.h" 22 #include "chrome/browser/ui/search/search_model.h"
26 #include "chrome/browser/ui/search/search_tab_helper.h" 23 #include "chrome/browser/ui/search/search_tab_helper.h"
27 #include "chrome/browser/ui/tabs/tab_strip_model.h" 24 #include "chrome/browser/ui/tabs/tab_strip_model.h"
28 #include "chrome/browser/ui/webui/ntp/app_launcher_handler.h" 25 #include "chrome/browser/ui/webui/ntp/app_launcher_handler.h"
29 #include "chrome/common/chrome_notification_types.h"
30 #include "chrome/common/pref_names.h" 26 #include "chrome/common/pref_names.h"
31 #include "chrome/common/url_constants.h" 27 #include "chrome/common/url_constants.h"
32 #include "components/user_prefs/pref_registry_syncable.h" 28 #include "components/user_prefs/pref_registry_syncable.h"
33 #include "content/public/browser/notification_service.h"
34 #include "content/public/browser/render_process_host.h" 29 #include "content/public/browser/render_process_host.h"
35 #include "content/public/browser/user_metrics.h" 30 #include "content/public/browser/user_metrics.h"
36 #include "content/public/browser/web_contents.h" 31 #include "content/public/browser/web_contents.h"
37 #include "grit/theme_resources.h"
38 #include "ui/gfx/color_utils.h"
39 #include "ui/gfx/sys_color_change_listener.h"
40 32
41 using content::UserMetricsAction; 33 using content::UserMetricsAction;
42 34
43 //////////////////////////////////////////////////////////////////////////////// 35 ////////////////////////////////////////////////////////////////////////////////
44 // BrowserInstantController, public: 36 // BrowserInstantController, public:
45 37
46 BrowserInstantController::BrowserInstantController(Browser* browser) 38 BrowserInstantController::BrowserInstantController(Browser* browser)
47 : browser_(browser), 39 : browser_(browser),
48 instant_(this, 40 instant_(this,
49 chrome::IsInstantExtendedAPIEnabled()), 41 chrome::IsInstantExtendedAPIEnabled()),
50 instant_unload_handler_(browser), 42 instant_unload_handler_(browser) {
51 initialized_theme_info_(false) {
52 43
53 // TODO(sreeram): Perhaps this can be removed, if field trial info is 44 // TODO(sreeram): Perhaps this can be removed, if field trial info is
54 // available before we need to register the pref. 45 // available before we need to register the pref.
55 chrome::SetInstantExtendedPrefDefault(profile()); 46 chrome::SetInstantExtendedPrefDefault(profile());
56 47
57 profile_pref_registrar_.Init(profile()->GetPrefs()); 48 profile_pref_registrar_.Init(profile()->GetPrefs());
58 profile_pref_registrar_.Add( 49 profile_pref_registrar_.Add(
59 prefs::kSearchInstantEnabled, 50 prefs::kSearchInstantEnabled,
60 base::Bind(&BrowserInstantController::ResetInstant, 51 base::Bind(&BrowserInstantController::ResetInstant,
61 base::Unretained(this))); 52 base::Unretained(this)));
62 profile_pref_registrar_.Add( 53 profile_pref_registrar_.Add(
63 prefs::kSearchSuggestEnabled, 54 prefs::kSearchSuggestEnabled,
64 base::Bind(&BrowserInstantController::ResetInstant, 55 base::Bind(&BrowserInstantController::ResetInstant,
65 base::Unretained(this))); 56 base::Unretained(this)));
66 profile_pref_registrar_.Add( 57 profile_pref_registrar_.Add(
67 prefs::kDefaultSearchProviderID, 58 prefs::kDefaultSearchProviderID,
68 base::Bind(&BrowserInstantController::OnDefaultSearchProviderChanged, 59 base::Bind(&BrowserInstantController::OnDefaultSearchProviderChanged,
69 base::Unretained(this))); 60 base::Unretained(this)));
70 ResetInstant(std::string()); 61 ResetInstant(std::string());
71 browser_->search_model()->AddObserver(this); 62 browser_->search_model()->AddObserver(this);
72
73 #if defined(ENABLE_THEMES)
74 // Listen for theme installation.
75 registrar_.Add(this, chrome::NOTIFICATION_BROWSER_THEME_CHANGED,
76 content::Source<ThemeService>(
77 ThemeServiceFactory::GetForProfile(profile())));
78 #endif // defined(ENABLE_THEMES)
79 } 63 }
80 64
81 BrowserInstantController::~BrowserInstantController() { 65 BrowserInstantController::~BrowserInstantController() {
82 browser_->search_model()->RemoveObserver(this); 66 browser_->search_model()->RemoveObserver(this);
83 } 67 }
84 68
85 bool BrowserInstantController::MaybeSwapInInstantNTPContents( 69 bool BrowserInstantController::MaybeSwapInInstantNTPContents(
86 const GURL& url, 70 const GURL& url,
87 content::WebContents* source_contents, 71 content::WebContents* source_contents,
88 content::WebContents** target_contents) { 72 content::WebContents** target_contents) {
(...skipping 138 matching lines...) Expand 10 before | Expand all | Expand 10 after
227 } 211 }
228 212
229 void BrowserInstantController::ActiveTabChanged() { 213 void BrowserInstantController::ActiveTabChanged() {
230 instant_.ActiveTabChanged(); 214 instant_.ActiveTabChanged();
231 } 215 }
232 216
233 void BrowserInstantController::TabDeactivated(content::WebContents* contents) { 217 void BrowserInstantController::TabDeactivated(content::WebContents* contents) {
234 instant_.TabDeactivated(contents); 218 instant_.TabDeactivated(contents);
235 } 219 }
236 220
237 void BrowserInstantController::UpdateThemeInfo() {
238 // Update theme background info.
239 // Initialize |theme_info| if necessary.
240 if (!initialized_theme_info_)
241 OnThemeChanged(ThemeServiceFactory::GetForProfile(profile()));
242 else
243 OnThemeChanged(NULL);
244 }
245
246 void BrowserInstantController::OpenURL( 221 void BrowserInstantController::OpenURL(
247 const GURL& url, 222 const GURL& url,
248 content::PageTransition transition, 223 content::PageTransition transition,
249 WindowOpenDisposition disposition) { 224 WindowOpenDisposition disposition) {
250 browser_->OpenURL(content::OpenURLParams(url, 225 browser_->OpenURL(content::OpenURLParams(url,
251 content::Referrer(), 226 content::Referrer(),
252 disposition, 227 disposition,
253 transition, 228 transition,
254 false)); 229 false));
255 } 230 }
(...skipping 26 matching lines...) Expand all
282 if (chrome::IsInstantExtendedAPIEnabled()) { 257 if (chrome::IsInstantExtendedAPIEnabled()) {
283 // Record some actions corresponding to the mode change. Note that to get 258 // Record some actions corresponding to the mode change. Note that to get
284 // the full story, it's necessary to look at other UMA actions as well, 259 // the full story, it's necessary to look at other UMA actions as well,
285 // such as tab switches. 260 // such as tab switches.
286 if (new_mode.is_search_results()) 261 if (new_mode.is_search_results())
287 content::RecordAction(UserMetricsAction("InstantExtended.ShowSRP")); 262 content::RecordAction(UserMetricsAction("InstantExtended.ShowSRP"));
288 else if (new_mode.is_ntp()) 263 else if (new_mode.is_ntp())
289 content::RecordAction(UserMetricsAction("InstantExtended.ShowNTP")); 264 content::RecordAction(UserMetricsAction("InstantExtended.ShowNTP"));
290 } 265 }
291 266
292 // If mode is now |NTP|, send theme-related information to Instant.
293 if (new_mode.is_ntp())
294 UpdateThemeInfo();
295
296 instant_.SearchModeChanged(old_state.mode, new_mode); 267 instant_.SearchModeChanged(old_state.mode, new_mode);
297 } 268 }
298 269
299 if (old_state.instant_support != new_state.instant_support) 270 if (old_state.instant_support != new_state.instant_support)
300 instant_.InstantSupportChanged(new_state.instant_support); 271 instant_.InstantSupportChanged(new_state.instant_support);
301 } 272 }
302 273
303 ////////////////////////////////////////////////////////////////////////////////
304 // BrowserInstantController, content::NotificationObserver implementation:
305
306 void BrowserInstantController::Observe(
307 int type,
308 const content::NotificationSource& source,
309 const content::NotificationDetails& details) {
310 #if defined(ENABLE_THEMES)
311 DCHECK_EQ(chrome::NOTIFICATION_BROWSER_THEME_CHANGED, type);
312 OnThemeChanged(content::Source<ThemeService>(source).ptr());
313 #endif // defined(ENABLE_THEMES)
314 }
315
316 void BrowserInstantController::OnThemeChanged(ThemeService* theme_service) {
317 if (theme_service) { // Get theme information from theme service.
318 theme_info_ = ThemeBackgroundInfo();
319
320 // Set theme background color.
321 SkColor background_color =
322 theme_service->GetColor(ThemeProperties::COLOR_NTP_BACKGROUND);
323 if (gfx::IsInvertedColorScheme())
324 background_color = color_utils::InvertColor(background_color);
325 theme_info_.color_r = SkColorGetR(background_color);
326 theme_info_.color_g = SkColorGetG(background_color);
327 theme_info_.color_b = SkColorGetB(background_color);
328 theme_info_.color_a = SkColorGetA(background_color);
329
330 if (theme_service->HasCustomImage(IDR_THEME_NTP_BACKGROUND)) {
331 // Set theme id for theme background image url.
332 theme_info_.theme_id = theme_service->GetThemeID();
333
334 // Set theme background image horizontal alignment.
335 int alignment = 0;
336 theme_service->GetDisplayProperty(
337 ThemeProperties::NTP_BACKGROUND_ALIGNMENT, &alignment);
338 if (alignment & ThemeProperties::ALIGN_LEFT) {
339 theme_info_.image_horizontal_alignment = THEME_BKGRND_IMAGE_ALIGN_LEFT;
340 } else if (alignment & ThemeProperties::ALIGN_RIGHT) {
341 theme_info_.image_horizontal_alignment = THEME_BKGRND_IMAGE_ALIGN_RIGHT;
342 } else { // ALIGN_CENTER
343 theme_info_.image_horizontal_alignment =
344 THEME_BKGRND_IMAGE_ALIGN_CENTER;
345 }
346
347 // Set theme background image vertical alignment.
348 if (alignment & ThemeProperties::ALIGN_TOP)
349 theme_info_.image_vertical_alignment = THEME_BKGRND_IMAGE_ALIGN_TOP;
350 else if (alignment & ThemeProperties::ALIGN_BOTTOM)
351 theme_info_.image_vertical_alignment = THEME_BKGRND_IMAGE_ALIGN_BOTTOM;
352 else // ALIGN_CENTER
353 theme_info_.image_vertical_alignment = THEME_BKGRND_IMAGE_ALIGN_CENTER;
354
355 // Set theme background image tiling.
356 int tiling = 0;
357 theme_service->GetDisplayProperty(ThemeProperties::NTP_BACKGROUND_TILING,
358 &tiling);
359 switch (tiling) {
360 case ThemeProperties::NO_REPEAT:
361 theme_info_.image_tiling = THEME_BKGRND_IMAGE_NO_REPEAT;
362 break;
363 case ThemeProperties::REPEAT_X:
364 theme_info_.image_tiling = THEME_BKGRND_IMAGE_REPEAT_X;
365 break;
366 case ThemeProperties::REPEAT_Y:
367 theme_info_.image_tiling = THEME_BKGRND_IMAGE_REPEAT_Y;
368 break;
369 case ThemeProperties::REPEAT:
370 theme_info_.image_tiling = THEME_BKGRND_IMAGE_REPEAT;
371 break;
372 }
373
374 // Set theme background image height.
375 gfx::ImageSkia* image = theme_service->GetImageSkiaNamed(
376 IDR_THEME_NTP_BACKGROUND);
377 DCHECK(image);
378 theme_info_.image_height = image->height();
379
380 theme_info_.has_attribution =
381 theme_service->HasCustomImage(IDR_THEME_NTP_ATTRIBUTION);
382 }
383
384 initialized_theme_info_ = true;
385 }
386
387 DCHECK(initialized_theme_info_);
388
389 if (browser_->search_model()->mode().is_ntp())
390 instant_.ThemeChanged(theme_info_);
391 }
392
393 void BrowserInstantController::OnDefaultSearchProviderChanged( 274 void BrowserInstantController::OnDefaultSearchProviderChanged(
394 const std::string& pref_name) { 275 const std::string& pref_name) {
395 DCHECK_EQ(pref_name, std::string(prefs::kDefaultSearchProviderID)); 276 DCHECK_EQ(pref_name, std::string(prefs::kDefaultSearchProviderID));
396 277
397 Profile* browser_profile = profile(); 278 Profile* browser_profile = profile();
398 const TemplateURL* template_url = 279 const TemplateURL* template_url =
399 TemplateURLServiceFactory::GetForProfile(browser_profile)-> 280 TemplateURLServiceFactory::GetForProfile(browser_profile)->
400 GetDefaultSearchProvider(); 281 GetDefaultSearchProvider();
401 if (!template_url) { 282 if (!template_url) {
402 // A NULL |template_url| could mean either this notification is sent during 283 // A NULL |template_url| could mean either this notification is sent during
(...skipping 28 matching lines...) Expand all
431 if (!instant_service->IsInstantProcess( 312 if (!instant_service->IsInstantProcess(
432 contents->GetRenderProcessHost()->GetID())) 313 contents->GetRenderProcessHost()->GetID()))
433 continue; 314 continue;
434 315
435 // Reload the contents to ensure that it gets assigned to a non-priviledged 316 // Reload the contents to ensure that it gets assigned to a non-priviledged
436 // renderer. 317 // renderer.
437 contents->GetController().Reload(false); 318 contents->GetController().Reload(false);
438 } 319 }
439 instant_.OnDefaultSearchProviderChanged(); 320 instant_.OnDefaultSearchProviderChanged();
440 } 321 }
OLDNEW
« no previous file with comments | « chrome/browser/ui/browser_instant_controller.h ('k') | chrome/browser/ui/search/instant_controller.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698