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