[email protected] | e41982a7 | 2012-11-20 07:16:51 | [diff] [blame] | 1 | // Copyright 2012 The Chromium Authors. All rights reserved. |
[email protected] | 7acfaf9 | 2012-07-11 15:51:59 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #include "chrome/browser/ui/browser_instant_controller.h" |
| 6 | |
[email protected] | 0a46856e | 2013-04-24 00:33:02 | [diff] [blame] | 7 | #include "base/bind.h" |
[email protected] | 3853a4c | 2013-02-11 17:15:57 | [diff] [blame] | 8 | #include "base/prefs/pref_service.h" |
[email protected] | 7acfaf9 | 2012-07-11 15:51:59 | [diff] [blame] | 9 | #include "chrome/browser/extensions/extension_service.h" |
[email protected] | 26015ac | 2013-02-27 02:50:13 | [diff] [blame] | 10 | #include "chrome/browser/extensions/extension_web_ui.h" |
[email protected] | 7acfaf9 | 2012-07-11 15:51:59 | [diff] [blame] | 11 | #include "chrome/browser/profiles/profile.h" |
[email protected] | 0a46856e | 2013-04-24 00:33:02 | [diff] [blame] | 12 | #include "chrome/browser/search/instant_service.h" |
| 13 | #include "chrome/browser/search/instant_service_factory.h" |
[email protected] | a7b8e43d | 2013-03-18 18:52:43 | [diff] [blame] | 14 | #include "chrome/browser/search/search.h" |
[email protected] | 0a46856e | 2013-04-24 00:33:02 | [diff] [blame] | 15 | #include "chrome/browser/search_engines/template_url.h" |
| 16 | #include "chrome/browser/search_engines/template_url_service.h" |
| 17 | #include "chrome/browser/search_engines/template_url_service_factory.h" |
[email protected] | e119b80 | 2013-02-18 18:55:39 | [diff] [blame] | 18 | #include "chrome/browser/themes/theme_properties.h" |
[email protected] | a682765 | 2012-11-20 23:41:08 | [diff] [blame] | 19 | #include "chrome/browser/themes/theme_service.h" |
| 20 | #include "chrome/browser/themes/theme_service_factory.h" |
[email protected] | 7acfaf9 | 2012-07-11 15:51:59 | [diff] [blame] | 21 | #include "chrome/browser/ui/browser.h" |
[email protected] | 7acfaf9 | 2012-07-11 15:51:59 | [diff] [blame] | 22 | #include "chrome/browser/ui/browser_window.h" |
| 23 | #include "chrome/browser/ui/omnibox/location_bar.h" |
[email protected] | c18cb67 | 2012-12-05 04:42:12 | [diff] [blame] | 24 | #include "chrome/browser/ui/omnibox/omnibox_view.h" |
[email protected] | 9d3d1170 | 2012-11-08 01:01:12 | [diff] [blame] | 25 | #include "chrome/browser/ui/search/search_tab_helper.h" |
[email protected] | e41982a7 | 2012-11-20 07:16:51 | [diff] [blame] | 26 | #include "chrome/browser/ui/tabs/tab_strip_model.h" |
[email protected] | 7acfaf9 | 2012-07-11 15:51:59 | [diff] [blame] | 27 | #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" |
[email protected] | 0c940663 | 2013-02-08 01:13:33 | [diff] [blame] | 30 | #include "chrome/common/url_constants.h" |
[email protected] | 75fee37 | 2013-03-06 00:42:44 | [diff] [blame] | 31 | #include "components/user_prefs/pref_registry_syncable.h" |
[email protected] | 7acfaf9 | 2012-07-11 15:51:59 | [diff] [blame] | 32 | #include "content/public/browser/notification_service.h" |
[email protected] | 0a46856e | 2013-04-24 00:33:02 | [diff] [blame] | 33 | #include "content/public/browser/render_process_host.h" |
[email protected] | 233f0f96 | 2013-02-27 21:14:19 | [diff] [blame] | 34 | #include "content/public/browser/user_metrics.h" |
[email protected] | 0a46856e | 2013-04-24 00:33:02 | [diff] [blame] | 35 | #include "content/public/browser/web_contents.h" |
[email protected] | a682765 | 2012-11-20 23:41:08 | [diff] [blame] | 36 | #include "grit/theme_resources.h" |
| 37 | #include "ui/gfx/color_utils.h" |
| 38 | #include "ui/gfx/sys_color_change_listener.h" |
| 39 | |
[email protected] | 233f0f96 | 2013-02-27 21:14:19 | [diff] [blame] | 40 | using content::UserMetricsAction; |
| 41 | |
[email protected] | 7acfaf9 | 2012-07-11 15:51:59 | [diff] [blame] | 42 | //////////////////////////////////////////////////////////////////////////////// |
| 43 | // BrowserInstantController, public: |
| 44 | |
| 45 | BrowserInstantController::BrowserInstantController(Browser* browser) |
[email protected] | 8a23670 | 2012-09-28 13:30:57 | [diff] [blame] | 46 | : browser_(browser), |
[email protected] | d4b2d23 | 2013-04-30 21:14:23 | [diff] [blame] | 47 | instant_(this, |
[email protected] | 165fe42 | 2013-03-27 06:34:03 | [diff] [blame] | 48 | chrome::IsInstantExtendedAPIEnabled()), |
[email protected] | a682765 | 2012-11-20 23:41:08 | [diff] [blame] | 49 | instant_unload_handler_(browser), |
[email protected] | e4a379c | 2013-02-15 19:27:19 | [diff] [blame] | 50 | initialized_theme_info_(false) { |
[email protected] | 5879cef | 2013-03-02 17:02:25 | [diff] [blame] | 51 | |
[email protected] | d2bb50e | 2013-05-09 12:38:34 | [diff] [blame] | 52 | // TODO(sreeram): Perhaps this can be removed, if field trial info is |
| 53 | // available before we need to register the pref. |
[email protected] | 165fe42 | 2013-03-27 06:34:03 | [diff] [blame] | 54 | chrome::SetInstantExtendedPrefDefault(profile()); |
[email protected] | 5879cef | 2013-03-02 17:02:25 | [diff] [blame] | 55 | |
[email protected] | 5fac378 | 2013-03-06 09:32:31 | [diff] [blame] | 56 | profile_pref_registrar_.Init(profile()->GetPrefs()); |
[email protected] | 0b3fa50 | 2012-11-21 13:57:46 | [diff] [blame] | 57 | profile_pref_registrar_.Add( |
[email protected] | d2bb50e | 2013-05-09 12:38:34 | [diff] [blame] | 58 | prefs::kSearchInstantEnabled, |
[email protected] | 0b3fa50 | 2012-11-21 13:57:46 | [diff] [blame] | 59 | base::Bind(&BrowserInstantController::ResetInstant, |
| 60 | base::Unretained(this))); |
[email protected] | 411c9c0 | 2013-02-07 04:55:59 | [diff] [blame] | 61 | profile_pref_registrar_.Add( |
| 62 | prefs::kSearchSuggestEnabled, |
| 63 | base::Bind(&BrowserInstantController::ResetInstant, |
| 64 | base::Unretained(this))); |
[email protected] | 0a46856e | 2013-04-24 00:33:02 | [diff] [blame] | 65 | profile_pref_registrar_.Add( |
| 66 | prefs::kDefaultSearchProviderID, |
| 67 | base::Bind(&BrowserInstantController::OnDefaultSearchProviderChanged, |
| 68 | base::Unretained(this))); |
[email protected] | 5fac378 | 2013-03-06 09:32:31 | [diff] [blame] | 69 | ResetInstant(std::string()); |
[email protected] | 0b10c9ff | 2012-10-09 17:31:55 | [diff] [blame] | 70 | browser_->search_model()->AddObserver(this); |
[email protected] | a682765 | 2012-11-20 23:41:08 | [diff] [blame] | 71 | |
| 72 | #if defined(ENABLE_THEMES) |
| 73 | // Listen for theme installation. |
| 74 | registrar_.Add(this, chrome::NOTIFICATION_BROWSER_THEME_CHANGED, |
| 75 | content::Source<ThemeService>( |
[email protected] | 0c940663 | 2013-02-08 01:13:33 | [diff] [blame] | 76 | ThemeServiceFactory::GetForProfile(profile()))); |
[email protected] | a682765 | 2012-11-20 23:41:08 | [diff] [blame] | 77 | #endif // defined(ENABLE_THEMES) |
[email protected] | 7acfaf9 | 2012-07-11 15:51:59 | [diff] [blame] | 78 | } |
| 79 | |
| 80 | BrowserInstantController::~BrowserInstantController() { |
[email protected] | 0b10c9ff | 2012-10-09 17:31:55 | [diff] [blame] | 81 | browser_->search_model()->RemoveObserver(this); |
[email protected] | 7acfaf9 | 2012-07-11 15:51:59 | [diff] [blame] | 82 | } |
| 83 | |
[email protected] | 0c940663 | 2013-02-08 01:13:33 | [diff] [blame] | 84 | bool BrowserInstantController::MaybeSwapInInstantNTPContents( |
| 85 | const GURL& url, |
| 86 | content::WebContents* source_contents, |
| 87 | content::WebContents** target_contents) { |
| 88 | if (url != GURL(chrome::kChromeUINewTabURL)) |
| 89 | return false; |
| 90 | |
[email protected] | 26015ac | 2013-02-27 02:50:13 | [diff] [blame] | 91 | GURL extension_url(url); |
| 92 | if (ExtensionWebUI::HandleChromeURLOverride(&extension_url, profile())) { |
| 93 | // If there is an extension overriding the NTP do not use the Instant NTP. |
| 94 | return false; |
| 95 | } |
| 96 | |
[email protected] | 0c940663 | 2013-02-08 01:13:33 | [diff] [blame] | 97 | scoped_ptr<content::WebContents> instant_ntp = instant_.ReleaseNTPContents(); |
| 98 | if (!instant_ntp) |
| 99 | return false; |
| 100 | |
| 101 | *target_contents = instant_ntp.get(); |
[email protected] | 0c940663 | 2013-02-08 01:13:33 | [diff] [blame] | 102 | if (source_contents) { |
| 103 | instant_ntp->GetController().CopyStateFromAndPrune( |
| 104 | &source_contents->GetController()); |
| 105 | ReplaceWebContentsAt( |
| 106 | browser_->tab_strip_model()->GetIndexOfWebContents(source_contents), |
| 107 | instant_ntp.Pass()); |
| 108 | } else { |
[email protected] | 57b58ca1 | 2013-02-26 23:41:25 | [diff] [blame] | 109 | instant_ntp->GetController().PruneAllButActive(); |
[email protected] | 0c940663 | 2013-02-08 01:13:33 | [diff] [blame] | 110 | // If |source_contents| is NULL, then the caller is responsible for |
| 111 | // inserting instant_ntp into the tabstrip and will take ownership. |
| 112 | ignore_result(instant_ntp.release()); |
| 113 | } |
[email protected] | 0c940663 | 2013-02-08 01:13:33 | [diff] [blame] | 114 | return true; |
| 115 | } |
| 116 | |
[email protected] | 7acfaf9 | 2012-07-11 15:51:59 | [diff] [blame] | 117 | bool BrowserInstantController::OpenInstant(WindowOpenDisposition disposition) { |
[email protected] | e41982a7 | 2012-11-20 07:16:51 | [diff] [blame] | 118 | // Unsupported dispositions. |
| 119 | if (disposition == NEW_BACKGROUND_TAB || disposition == NEW_WINDOW) |
[email protected] | 7acfaf9 | 2012-07-11 15:51:59 | [diff] [blame] | 120 | return false; |
[email protected] | 7acfaf9 | 2012-07-11 15:51:59 | [diff] [blame] | 121 | |
[email protected] | 7acfaf9 | 2012-07-11 15:51:59 | [diff] [blame] | 122 | // The omnibox currently doesn't use other dispositions, so we don't attempt |
[email protected] | c72226c8 | 2012-10-01 21:02:32 | [diff] [blame] | 123 | // to handle them. If you hit this DCHECK file a bug and I'll (sky) add |
[email protected] | 7acfaf9 | 2012-07-11 15:51:59 | [diff] [blame] | 124 | // support for the new disposition. |
[email protected] | c72226c8 | 2012-10-01 21:02:32 | [diff] [blame] | 125 | DCHECK(disposition == CURRENT_TAB || |
| 126 | disposition == NEW_FOREGROUND_TAB) << disposition; |
| 127 | |
[email protected] | 221e920 | 2012-12-08 00:59:09 | [diff] [blame] | 128 | return instant_.CommitIfPossible(disposition == CURRENT_TAB ? |
[email protected] | c72226c8 | 2012-10-01 21:02:32 | [diff] [blame] | 129 | INSTANT_COMMIT_PRESSED_ENTER : INSTANT_COMMIT_PRESSED_ALT_ENTER); |
[email protected] | 7acfaf9 | 2012-07-11 15:51:59 | [diff] [blame] | 130 | } |
| 131 | |
[email protected] | 0c940663 | 2013-02-08 01:13:33 | [diff] [blame] | 132 | Profile* BrowserInstantController::profile() const { |
| 133 | return browser_->profile(); |
| 134 | } |
| 135 | |
| 136 | void BrowserInstantController::CommitInstant( |
[email protected] | ddd28ae | 2013-03-04 21:11:54 | [diff] [blame] | 137 | scoped_ptr<content::WebContents> overlay, |
[email protected] | 0c940663 | 2013-02-08 01:13:33 | [diff] [blame] | 138 | bool in_new_tab) { |
[email protected] | 79f17ef46 | 2013-03-24 00:28:18 | [diff] [blame] | 139 | const extensions::Extension* extension = |
| 140 | profile()->GetExtensionService()->GetInstalledApp(overlay->GetURL()); |
| 141 | if (extension) { |
[email protected] | 0c940663 | 2013-02-08 01:13:33 | [diff] [blame] | 142 | AppLauncherHandler::RecordAppLaunchType( |
[email protected] | 79f17ef46 | 2013-03-24 00:28:18 | [diff] [blame] | 143 | extension_misc::APP_LAUNCH_OMNIBOX_INSTANT, |
| 144 | extension->GetType()); |
[email protected] | 0c940663 | 2013-02-08 01:13:33 | [diff] [blame] | 145 | } |
[email protected] | c72226c8 | 2012-10-01 21:02:32 | [diff] [blame] | 146 | if (in_new_tab) { |
[email protected] | ddd28ae | 2013-03-04 21:11:54 | [diff] [blame] | 147 | // TabStripModel takes ownership of |overlay|. |
| 148 | browser_->tab_strip_model()->AddWebContents(overlay.release(), -1, |
[email protected] | e41982a7 | 2012-11-20 07:16:51 | [diff] [blame] | 149 | instant_.last_transition_type(), TabStripModel::ADD_ACTIVE); |
[email protected] | c72226c8 | 2012-10-01 21:02:32 | [diff] [blame] | 150 | } else { |
[email protected] | 9bc3f24 | 2013-04-05 23:27:36 | [diff] [blame] | 151 | content::WebContents* contents = overlay.get(); |
[email protected] | 0c940663 | 2013-02-08 01:13:33 | [diff] [blame] | 152 | ReplaceWebContentsAt( |
| 153 | browser_->tab_strip_model()->active_index(), |
[email protected] | ddd28ae | 2013-03-04 21:11:54 | [diff] [blame] | 154 | overlay.Pass()); |
[email protected] | 9bc3f24 | 2013-04-05 23:27:36 | [diff] [blame] | 155 | browser_->window()->GetLocationBar()->SaveStateToContents(contents); |
[email protected] | 7acfaf9 | 2012-07-11 15:51:59 | [diff] [blame] | 156 | } |
| 157 | } |
| 158 | |
[email protected] | 0c940663 | 2013-02-08 01:13:33 | [diff] [blame] | 159 | void BrowserInstantController::ReplaceWebContentsAt( |
| 160 | int index, |
| 161 | scoped_ptr<content::WebContents> new_contents) { |
| 162 | DCHECK_NE(TabStripModel::kNoTab, index); |
[email protected] | d572bfd | 2013-02-14 06:14:20 | [diff] [blame] | 163 | scoped_ptr<content::WebContents> old_contents(browser_->tab_strip_model()-> |
| 164 | ReplaceWebContentsAt(index, new_contents.release())); |
| 165 | instant_unload_handler_.RunUnloadListenersOrDestroy(old_contents.Pass(), |
| 166 | index); |
[email protected] | 0c940663 | 2013-02-08 01:13:33 | [diff] [blame] | 167 | } |
| 168 | |
[email protected] | 93b7383 | 2012-10-18 20:18:38 | [diff] [blame] | 169 | void BrowserInstantController::SetInstantSuggestion( |
| 170 | const InstantSuggestion& suggestion) { |
[email protected] | c18cb67 | 2012-12-05 04:42:12 | [diff] [blame] | 171 | browser_->window()->GetLocationBar()->SetInstantSuggestion(suggestion); |
[email protected] | 7acfaf9 | 2012-07-11 15:51:59 | [diff] [blame] | 172 | } |
| 173 | |
[email protected] | 7acfaf9 | 2012-07-11 15:51:59 | [diff] [blame] | 174 | gfx::Rect BrowserInstantController::GetInstantBounds() { |
| 175 | return browser_->window()->GetInstantBounds(); |
| 176 | } |
| 177 | |
[email protected] | ddd28ae | 2013-03-04 21:11:54 | [diff] [blame] | 178 | void BrowserInstantController::InstantOverlayFocused() { |
[email protected] | 7acfaf9 | 2012-07-11 15:51:59 | [diff] [blame] | 179 | // NOTE: This is only invoked on aura. |
[email protected] | ddd28ae | 2013-03-04 21:11:54 | [diff] [blame] | 180 | browser_->window()->WebContentsFocused(instant_.GetOverlayContents()); |
[email protected] | 7acfaf9 | 2012-07-11 15:51:59 | [diff] [blame] | 181 | } |
| 182 | |
[email protected] | 10aec59 | 2013-03-06 20:19:00 | [diff] [blame] | 183 | void BrowserInstantController::FocusOmnibox(bool caret_visibility) { |
[email protected] | c18cb67 | 2012-12-05 04:42:12 | [diff] [blame] | 184 | OmniboxView* omnibox_view = browser_->window()->GetLocationBar()-> |
| 185 | GetLocationEntry(); |
| 186 | omnibox_view->SetFocus(); |
[email protected] | 10aec59 | 2013-03-06 20:19:00 | [diff] [blame] | 187 | omnibox_view->model()->SetCaretVisibility(caret_visibility); |
[email protected] | c18cb67 | 2012-12-05 04:42:12 | [diff] [blame] | 188 | } |
| 189 | |
[email protected] | cd533bf | 2012-12-04 19:14:59 | [diff] [blame] | 190 | content::WebContents* BrowserInstantController::GetActiveWebContents() const { |
| 191 | return browser_->tab_strip_model()->GetActiveWebContents(); |
[email protected] | 7acfaf9 | 2012-07-11 15:51:59 | [diff] [blame] | 192 | } |
| 193 | |
[email protected] | e41982a7 | 2012-11-20 07:16:51 | [diff] [blame] | 194 | void BrowserInstantController::ActiveTabChanged() { |
| 195 | instant_.ActiveTabChanged(); |
| 196 | } |
| 197 | |
[email protected] | 3d6a895 | 2012-12-14 03:18:07 | [diff] [blame] | 198 | void BrowserInstantController::TabDeactivated(content::WebContents* contents) { |
| 199 | instant_.TabDeactivated(contents); |
| 200 | } |
| 201 | |
[email protected] | 3870363e | 2013-03-06 20:11:23 | [diff] [blame] | 202 | void BrowserInstantController::UpdateThemeInfo() { |
[email protected] | e4a379c | 2013-02-15 19:27:19 | [diff] [blame] | 203 | // Update theme background info. |
[email protected] | 3870363e | 2013-03-06 20:11:23 | [diff] [blame] | 204 | // Initialize |theme_info| if necessary. |
| 205 | if (!initialized_theme_info_) |
[email protected] | 0c940663 | 2013-02-08 01:13:33 | [diff] [blame] | 206 | OnThemeChanged(ThemeServiceFactory::GetForProfile(profile())); |
[email protected] | a682765 | 2012-11-20 23:41:08 | [diff] [blame] | 207 | else |
| 208 | OnThemeChanged(NULL); |
| 209 | } |
| 210 | |
[email protected] | 3c3acca | 2013-02-26 03:07:07 | [diff] [blame] | 211 | void BrowserInstantController::OpenURL( |
[email protected] | e3033eb | 2012-12-13 23:46:08 | [diff] [blame] | 212 | const GURL& url, |
[email protected] | 3c3acca | 2013-02-26 03:07:07 | [diff] [blame] | 213 | content::PageTransition transition, |
| 214 | WindowOpenDisposition disposition) { |
[email protected] | e3033eb | 2012-12-13 23:46:08 | [diff] [blame] | 215 | browser_->OpenURL(content::OpenURLParams(url, |
| 216 | content::Referrer(), |
[email protected] | 3c3acca | 2013-02-26 03:07:07 | [diff] [blame] | 217 | disposition, |
[email protected] | e3033eb | 2012-12-13 23:46:08 | [diff] [blame] | 218 | transition, |
| 219 | false)); |
| 220 | } |
| 221 | |
[email protected] | fcde79a | 2013-02-28 02:25:09 | [diff] [blame] | 222 | void BrowserInstantController::SetOmniboxBounds(const gfx::Rect& bounds) { |
| 223 | instant_.SetOmniboxBounds(bounds); |
[email protected] | ec4aad54 | 2012-12-14 01:11:04 | [diff] [blame] | 224 | } |
| 225 | |
[email protected] | 5fac378 | 2013-03-06 09:32:31 | [diff] [blame] | 226 | void BrowserInstantController::ResetInstant(const std::string& pref_name) { |
[email protected] | a1a83ef6 | 2013-04-29 19:15:43 | [diff] [blame] | 227 | bool instant_checkbox_checked = chrome::IsInstantCheckboxChecked(profile()); |
| 228 | bool use_local_overlay_only = |
[email protected] | 07fd48a | 2013-04-13 02:53:27 | [diff] [blame] | 229 | chrome::IsLocalOnlyInstantExtendedAPIEnabled() || |
[email protected] | a1a83ef6 | 2013-04-29 19:15:43 | [diff] [blame] | 230 | !chrome::IsInstantCheckboxEnabled(profile()); |
| 231 | instant_.SetInstantEnabled(instant_checkbox_checked, use_local_overlay_only); |
[email protected] | 749ce88 | 2012-11-16 04:18:01 | [diff] [blame] | 232 | } |
| 233 | |
[email protected] | 7acfaf9 | 2012-07-11 15:51:59 | [diff] [blame] | 234 | //////////////////////////////////////////////////////////////////////////////// |
[email protected] | 165fe42 | 2013-03-27 06:34:03 | [diff] [blame] | 235 | // BrowserInstantController, SearchModelObserver implementation: |
[email protected] | 0b10c9ff | 2012-10-09 17:31:55 | [diff] [blame] | 236 | |
[email protected] | 5ee671f | 2013-03-19 11:23:05 | [diff] [blame] | 237 | void BrowserInstantController::ModelChanged( |
[email protected] | 165fe42 | 2013-03-27 06:34:03 | [diff] [blame] | 238 | const SearchModel::State& old_state, |
| 239 | const SearchModel::State& new_state) { |
[email protected] | 5ee671f | 2013-03-19 11:23:05 | [diff] [blame] | 240 | if (old_state.mode == new_state.mode) |
| 241 | return; |
| 242 | |
[email protected] | 165fe42 | 2013-03-27 06:34:03 | [diff] [blame] | 243 | const SearchMode& new_mode = new_state.mode; |
[email protected] | 5ee671f | 2013-03-19 11:23:05 | [diff] [blame] | 244 | |
[email protected] | 165fe42 | 2013-03-27 06:34:03 | [diff] [blame] | 245 | if (chrome::IsInstantExtendedAPIEnabled()) { |
[email protected] | c19ba104 | 2013-03-11 17:17:13 | [diff] [blame] | 246 | // Record some actions corresponding to the mode change. Note that to get |
| 247 | // the full story, it's necessary to look at other UMA actions as well, |
| 248 | // such as tab switches. |
| 249 | if (new_mode.is_search_results()) |
| 250 | content::RecordAction(UserMetricsAction("InstantExtended.ShowSRP")); |
| 251 | else if (new_mode.is_ntp()) |
| 252 | content::RecordAction(UserMetricsAction("InstantExtended.ShowNTP")); |
| 253 | } |
| 254 | |
[email protected] | ddd28ae | 2013-03-04 21:11:54 | [diff] [blame] | 255 | // If mode is now |NTP|, send theme-related information to Instant. |
[email protected] | a682765 | 2012-11-20 23:41:08 | [diff] [blame] | 256 | if (new_mode.is_ntp()) |
[email protected] | 3870363e | 2013-03-06 20:11:23 | [diff] [blame] | 257 | UpdateThemeInfo(); |
[email protected] | a682765 | 2012-11-20 23:41:08 | [diff] [blame] | 258 | |
[email protected] | 5ee671f | 2013-03-19 11:23:05 | [diff] [blame] | 259 | instant_.SearchModeChanged(old_state.mode, new_mode); |
[email protected] | 7acfaf9 | 2012-07-11 15:51:59 | [diff] [blame] | 260 | } |
| 261 | |
[email protected] | a682765 | 2012-11-20 23:41:08 | [diff] [blame] | 262 | //////////////////////////////////////////////////////////////////////////////// |
| 263 | // BrowserInstantController, content::NotificationObserver implementation: |
| 264 | |
| 265 | void BrowserInstantController::Observe( |
| 266 | int type, |
| 267 | const content::NotificationSource& source, |
| 268 | const content::NotificationDetails& details) { |
| 269 | #if defined(ENABLE_THEMES) |
| 270 | DCHECK_EQ(chrome::NOTIFICATION_BROWSER_THEME_CHANGED, type); |
| 271 | OnThemeChanged(content::Source<ThemeService>(source).ptr()); |
| 272 | #endif // defined(ENABLE_THEMES) |
| 273 | } |
| 274 | |
| 275 | void BrowserInstantController::OnThemeChanged(ThemeService* theme_service) { |
| 276 | if (theme_service) { // Get theme information from theme service. |
| 277 | theme_info_ = ThemeBackgroundInfo(); |
| 278 | |
| 279 | // Set theme background color. |
| 280 | SkColor background_color = |
[email protected] | e119b80 | 2013-02-18 18:55:39 | [diff] [blame] | 281 | theme_service->GetColor(ThemeProperties::COLOR_NTP_BACKGROUND); |
[email protected] | a682765 | 2012-11-20 23:41:08 | [diff] [blame] | 282 | if (gfx::IsInvertedColorScheme()) |
| 283 | background_color = color_utils::InvertColor(background_color); |
| 284 | theme_info_.color_r = SkColorGetR(background_color); |
| 285 | theme_info_.color_g = SkColorGetG(background_color); |
| 286 | theme_info_.color_b = SkColorGetB(background_color); |
| 287 | theme_info_.color_a = SkColorGetA(background_color); |
| 288 | |
| 289 | if (theme_service->HasCustomImage(IDR_THEME_NTP_BACKGROUND)) { |
| 290 | // Set theme id for theme background image url. |
| 291 | theme_info_.theme_id = theme_service->GetThemeID(); |
| 292 | |
| 293 | // Set theme background image horizontal alignment. |
| 294 | int alignment = 0; |
[email protected] | e119b80 | 2013-02-18 18:55:39 | [diff] [blame] | 295 | theme_service->GetDisplayProperty( |
| 296 | ThemeProperties::NTP_BACKGROUND_ALIGNMENT, &alignment); |
| 297 | if (alignment & ThemeProperties::ALIGN_LEFT) { |
[email protected] | a682765 | 2012-11-20 23:41:08 | [diff] [blame] | 298 | theme_info_.image_horizontal_alignment = THEME_BKGRND_IMAGE_ALIGN_LEFT; |
[email protected] | e119b80 | 2013-02-18 18:55:39 | [diff] [blame] | 299 | } else if (alignment & ThemeProperties::ALIGN_RIGHT) { |
[email protected] | a682765 | 2012-11-20 23:41:08 | [diff] [blame] | 300 | theme_info_.image_horizontal_alignment = THEME_BKGRND_IMAGE_ALIGN_RIGHT; |
| 301 | } else { // ALIGN_CENTER |
| 302 | theme_info_.image_horizontal_alignment = |
| 303 | THEME_BKGRND_IMAGE_ALIGN_CENTER; |
| 304 | } |
| 305 | |
| 306 | // Set theme background image vertical alignment. |
[email protected] | 3870363e | 2013-03-06 20:11:23 | [diff] [blame] | 307 | if (alignment & ThemeProperties::ALIGN_TOP) |
[email protected] | a682765 | 2012-11-20 23:41:08 | [diff] [blame] | 308 | theme_info_.image_vertical_alignment = THEME_BKGRND_IMAGE_ALIGN_TOP; |
[email protected] | 3870363e | 2013-03-06 20:11:23 | [diff] [blame] | 309 | else if (alignment & ThemeProperties::ALIGN_BOTTOM) |
[email protected] | a682765 | 2012-11-20 23:41:08 | [diff] [blame] | 310 | theme_info_.image_vertical_alignment = THEME_BKGRND_IMAGE_ALIGN_BOTTOM; |
[email protected] | 3870363e | 2013-03-06 20:11:23 | [diff] [blame] | 311 | else // ALIGN_CENTER |
[email protected] | a682765 | 2012-11-20 23:41:08 | [diff] [blame] | 312 | theme_info_.image_vertical_alignment = THEME_BKGRND_IMAGE_ALIGN_CENTER; |
[email protected] | a682765 | 2012-11-20 23:41:08 | [diff] [blame] | 313 | |
| 314 | // Set theme background image tiling. |
| 315 | int tiling = 0; |
[email protected] | e119b80 | 2013-02-18 18:55:39 | [diff] [blame] | 316 | theme_service->GetDisplayProperty(ThemeProperties::NTP_BACKGROUND_TILING, |
[email protected] | a682765 | 2012-11-20 23:41:08 | [diff] [blame] | 317 | &tiling); |
| 318 | switch (tiling) { |
[email protected] | e119b80 | 2013-02-18 18:55:39 | [diff] [blame] | 319 | case ThemeProperties::NO_REPEAT: |
[email protected] | a682765 | 2012-11-20 23:41:08 | [diff] [blame] | 320 | theme_info_.image_tiling = THEME_BKGRND_IMAGE_NO_REPEAT; |
| 321 | break; |
[email protected] | e119b80 | 2013-02-18 18:55:39 | [diff] [blame] | 322 | case ThemeProperties::REPEAT_X: |
[email protected] | a682765 | 2012-11-20 23:41:08 | [diff] [blame] | 323 | theme_info_.image_tiling = THEME_BKGRND_IMAGE_REPEAT_X; |
| 324 | break; |
[email protected] | e119b80 | 2013-02-18 18:55:39 | [diff] [blame] | 325 | case ThemeProperties::REPEAT_Y: |
[email protected] | a682765 | 2012-11-20 23:41:08 | [diff] [blame] | 326 | theme_info_.image_tiling = THEME_BKGRND_IMAGE_REPEAT_Y; |
| 327 | break; |
[email protected] | e119b80 | 2013-02-18 18:55:39 | [diff] [blame] | 328 | case ThemeProperties::REPEAT: |
[email protected] | a682765 | 2012-11-20 23:41:08 | [diff] [blame] | 329 | theme_info_.image_tiling = THEME_BKGRND_IMAGE_REPEAT; |
| 330 | break; |
| 331 | } |
| 332 | |
| 333 | // Set theme background image height. |
| 334 | gfx::ImageSkia* image = theme_service->GetImageSkiaNamed( |
| 335 | IDR_THEME_NTP_BACKGROUND); |
| 336 | DCHECK(image); |
| 337 | theme_info_.image_height = image->height(); |
[email protected] | 5189fa1 | 2013-03-20 23:00:05 | [diff] [blame] | 338 | |
| 339 | theme_info_.has_attribution = |
| 340 | theme_service->HasCustomImage(IDR_THEME_NTP_ATTRIBUTION); |
[email protected] | a682765 | 2012-11-20 23:41:08 | [diff] [blame] | 341 | } |
| 342 | |
| 343 | initialized_theme_info_ = true; |
| 344 | } |
| 345 | |
| 346 | DCHECK(initialized_theme_info_); |
| 347 | |
[email protected] | e4a379c | 2013-02-15 19:27:19 | [diff] [blame] | 348 | if (browser_->search_model()->mode().is_ntp()) |
[email protected] | a682765 | 2012-11-20 23:41:08 | [diff] [blame] | 349 | instant_.ThemeChanged(theme_info_); |
[email protected] | a682765 | 2012-11-20 23:41:08 | [diff] [blame] | 350 | } |
[email protected] | 0a46856e | 2013-04-24 00:33:02 | [diff] [blame] | 351 | |
| 352 | void BrowserInstantController::OnDefaultSearchProviderChanged( |
| 353 | const std::string& pref_name) { |
| 354 | DCHECK_EQ(pref_name, std::string(prefs::kDefaultSearchProviderID)); |
| 355 | |
| 356 | Profile* browser_profile = profile(); |
| 357 | const TemplateURL* template_url = |
| 358 | TemplateURLServiceFactory::GetForProfile(browser_profile)-> |
| 359 | GetDefaultSearchProvider(); |
| 360 | if (!template_url) { |
| 361 | // A NULL |template_url| could mean either this notification is sent during |
| 362 | // the browser start up operation or the user now has no default search |
| 363 | // provider. There is no way for the user to reach this state using the |
| 364 | // Chrome settings. Only explicitly poking at the DB or bugs in the Sync |
| 365 | // could cause that, neither of which we support. |
| 366 | return; |
| 367 | } |
| 368 | |
| 369 | InstantService* instant_service = |
| 370 | InstantServiceFactory::GetForProfile(browser_profile); |
| 371 | if (!instant_service) |
| 372 | return; |
| 373 | |
| 374 | TabStripModel* tab_model = browser_->tab_strip_model(); |
| 375 | int count = tab_model->count(); |
| 376 | for (int index = 0; index < count; ++index) { |
| 377 | content::WebContents* contents = tab_model->GetWebContentsAt(index); |
| 378 | if (!contents) |
| 379 | continue; |
| 380 | |
| 381 | // A Local NTP always runs in the Instant process, so reloading it is |
| 382 | // neither useful nor necessary. However, the Local NTP does not reflect |
| 383 | // whether Google is the default search engine or not. This is achieved |
| 384 | // through a URL parameter, so reloading the existing URL won't fix that |
| 385 | // (i.e., the Local NTP may now show an incorrect search engine logo). |
| 386 | // TODO(kmadhusu): Fix. |
| 387 | if (contents->GetURL() == GURL(chrome::kChromeSearchLocalNtpUrl)) |
| 388 | continue; |
| 389 | |
| 390 | if (!instant_service->IsInstantProcess( |
| 391 | contents->GetRenderProcessHost()->GetID())) |
| 392 | continue; |
| 393 | |
| 394 | // Reload the contents to ensure that it gets assigned to a non-priviledged |
| 395 | // renderer. |
| 396 | contents->GetController().Reload(false); |
| 397 | } |
| 398 | instant_.OnDefaultSearchProviderChanged(); |
| 399 | } |