[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) { |
[email protected] | 474f851 | 2013-05-31 22:31:16 | [diff] [blame] | 103 | // If the Instant NTP hasn't yet committed an entry, we can't call |
| 104 | // CopyStateFromAndPrune. Instead, load the Local NTP URL directly in the |
| 105 | // source contents. |
| 106 | // TODO(sreeram): Always using the local URL is wrong in the case of the |
| 107 | // first tab in a window where we might want to use the remote URL. Fix. |
| 108 | if (!instant_ntp->GetController().CanPruneAllButVisible()) { |
| 109 | source_contents->GetController().LoadURL(chrome::GetLocalInstantURL( |
| 110 | profile()), content::Referrer(), content::PAGE_TRANSITION_GENERATED, |
| 111 | std::string()); |
| 112 | *target_contents = source_contents; |
| 113 | } else { |
| 114 | instant_ntp->GetController().CopyStateFromAndPrune( |
| 115 | &source_contents->GetController()); |
| 116 | ReplaceWebContentsAt( |
| 117 | browser_->tab_strip_model()->GetIndexOfWebContents(source_contents), |
| 118 | instant_ntp.Pass()); |
| 119 | } |
[email protected] | 0c940663 | 2013-02-08 01:13:33 | [diff] [blame] | 120 | } else { |
[email protected] | 474f851 | 2013-05-31 22:31:16 | [diff] [blame] | 121 | // If the Instant NTP hasn't yet committed an entry, we can't call |
| 122 | // PruneAllButVisible. In that case, there shouldn't be any entries to |
| 123 | // prune anyway. |
| 124 | if (instant_ntp->GetController().CanPruneAllButVisible()) |
| 125 | instant_ntp->GetController().PruneAllButVisible(); |
| 126 | else |
| 127 | CHECK(!instant_ntp->GetController().GetLastCommittedEntry()); |
| 128 | |
[email protected] | 0c940663 | 2013-02-08 01:13:33 | [diff] [blame] | 129 | // If |source_contents| is NULL, then the caller is responsible for |
| 130 | // inserting instant_ntp into the tabstrip and will take ownership. |
| 131 | ignore_result(instant_ntp.release()); |
| 132 | } |
[email protected] | 0c940663 | 2013-02-08 01:13:33 | [diff] [blame] | 133 | return true; |
| 134 | } |
| 135 | |
[email protected] | 413558cb | 2013-06-10 16:44:45 | [diff] [blame^] | 136 | bool BrowserInstantController::OpenInstant(WindowOpenDisposition disposition, |
| 137 | const GURL& url) { |
[email protected] | e41982a7 | 2012-11-20 07:16:51 | [diff] [blame] | 138 | // Unsupported dispositions. |
[email protected] | 413558cb | 2013-06-10 16:44:45 | [diff] [blame^] | 139 | if (disposition == NEW_BACKGROUND_TAB || disposition == NEW_WINDOW || |
| 140 | disposition == NEW_FOREGROUND_TAB) |
[email protected] | 7acfaf9 | 2012-07-11 15:51:59 | [diff] [blame] | 141 | return false; |
[email protected] | 7acfaf9 | 2012-07-11 15:51:59 | [diff] [blame] | 142 | |
[email protected] | 7acfaf9 | 2012-07-11 15:51:59 | [diff] [blame] | 143 | // The omnibox currently doesn't use other dispositions, so we don't attempt |
[email protected] | c72226c8 | 2012-10-01 21:02:32 | [diff] [blame] | 144 | // 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] | 145 | // support for the new disposition. |
[email protected] | 413558cb | 2013-06-10 16:44:45 | [diff] [blame^] | 146 | DCHECK(disposition == CURRENT_TAB) << disposition; |
[email protected] | c72226c8 | 2012-10-01 21:02:32 | [diff] [blame] | 147 | |
[email protected] | 413558cb | 2013-06-10 16:44:45 | [diff] [blame^] | 148 | // If we will not be replacing search terms from this URL, don't send to |
| 149 | // InstantController. |
| 150 | const string16& search_terms = |
| 151 | chrome::GetSearchTermsFromURL(browser_->profile(), url); |
| 152 | if (search_terms.empty()) |
| 153 | return false; |
| 154 | |
| 155 | return instant_.SubmitQuery(search_terms); |
[email protected] | 7acfaf9 | 2012-07-11 15:51:59 | [diff] [blame] | 156 | } |
| 157 | |
[email protected] | 0c940663 | 2013-02-08 01:13:33 | [diff] [blame] | 158 | Profile* BrowserInstantController::profile() const { |
| 159 | return browser_->profile(); |
| 160 | } |
| 161 | |
| 162 | void BrowserInstantController::CommitInstant( |
[email protected] | ddd28ae | 2013-03-04 21:11:54 | [diff] [blame] | 163 | scoped_ptr<content::WebContents> overlay, |
[email protected] | 0c940663 | 2013-02-08 01:13:33 | [diff] [blame] | 164 | bool in_new_tab) { |
[email protected] | 79f17ef46 | 2013-03-24 00:28:18 | [diff] [blame] | 165 | const extensions::Extension* extension = |
| 166 | profile()->GetExtensionService()->GetInstalledApp(overlay->GetURL()); |
| 167 | if (extension) { |
[email protected] | 0c940663 | 2013-02-08 01:13:33 | [diff] [blame] | 168 | AppLauncherHandler::RecordAppLaunchType( |
[email protected] | 79f17ef46 | 2013-03-24 00:28:18 | [diff] [blame] | 169 | extension_misc::APP_LAUNCH_OMNIBOX_INSTANT, |
| 170 | extension->GetType()); |
[email protected] | 0c940663 | 2013-02-08 01:13:33 | [diff] [blame] | 171 | } |
[email protected] | c72226c8 | 2012-10-01 21:02:32 | [diff] [blame] | 172 | if (in_new_tab) { |
[email protected] | ddd28ae | 2013-03-04 21:11:54 | [diff] [blame] | 173 | // TabStripModel takes ownership of |overlay|. |
| 174 | browser_->tab_strip_model()->AddWebContents(overlay.release(), -1, |
[email protected] | e41982a7 | 2012-11-20 07:16:51 | [diff] [blame] | 175 | instant_.last_transition_type(), TabStripModel::ADD_ACTIVE); |
[email protected] | c72226c8 | 2012-10-01 21:02:32 | [diff] [blame] | 176 | } else { |
[email protected] | 9bc3f24 | 2013-04-05 23:27:36 | [diff] [blame] | 177 | content::WebContents* contents = overlay.get(); |
[email protected] | 0c940663 | 2013-02-08 01:13:33 | [diff] [blame] | 178 | ReplaceWebContentsAt( |
| 179 | browser_->tab_strip_model()->active_index(), |
[email protected] | ddd28ae | 2013-03-04 21:11:54 | [diff] [blame] | 180 | overlay.Pass()); |
[email protected] | 9bc3f24 | 2013-04-05 23:27:36 | [diff] [blame] | 181 | browser_->window()->GetLocationBar()->SaveStateToContents(contents); |
[email protected] | 7acfaf9 | 2012-07-11 15:51:59 | [diff] [blame] | 182 | } |
| 183 | } |
| 184 | |
[email protected] | 0c940663 | 2013-02-08 01:13:33 | [diff] [blame] | 185 | void BrowserInstantController::ReplaceWebContentsAt( |
| 186 | int index, |
| 187 | scoped_ptr<content::WebContents> new_contents) { |
| 188 | DCHECK_NE(TabStripModel::kNoTab, index); |
[email protected] | d572bfd | 2013-02-14 06:14:20 | [diff] [blame] | 189 | scoped_ptr<content::WebContents> old_contents(browser_->tab_strip_model()-> |
| 190 | ReplaceWebContentsAt(index, new_contents.release())); |
| 191 | instant_unload_handler_.RunUnloadListenersOrDestroy(old_contents.Pass(), |
| 192 | index); |
[email protected] | 0c940663 | 2013-02-08 01:13:33 | [diff] [blame] | 193 | } |
| 194 | |
[email protected] | 93b7383 | 2012-10-18 20:18:38 | [diff] [blame] | 195 | void BrowserInstantController::SetInstantSuggestion( |
| 196 | const InstantSuggestion& suggestion) { |
[email protected] | c18cb67 | 2012-12-05 04:42:12 | [diff] [blame] | 197 | browser_->window()->GetLocationBar()->SetInstantSuggestion(suggestion); |
[email protected] | 7acfaf9 | 2012-07-11 15:51:59 | [diff] [blame] | 198 | } |
| 199 | |
[email protected] | 7acfaf9 | 2012-07-11 15:51:59 | [diff] [blame] | 200 | gfx::Rect BrowserInstantController::GetInstantBounds() { |
| 201 | return browser_->window()->GetInstantBounds(); |
| 202 | } |
| 203 | |
[email protected] | ddd28ae | 2013-03-04 21:11:54 | [diff] [blame] | 204 | void BrowserInstantController::InstantOverlayFocused() { |
[email protected] | 7acfaf9 | 2012-07-11 15:51:59 | [diff] [blame] | 205 | // NOTE: This is only invoked on aura. |
[email protected] | ddd28ae | 2013-03-04 21:11:54 | [diff] [blame] | 206 | browser_->window()->WebContentsFocused(instant_.GetOverlayContents()); |
[email protected] | 7acfaf9 | 2012-07-11 15:51:59 | [diff] [blame] | 207 | } |
| 208 | |
[email protected] | 10aec59 | 2013-03-06 20:19:00 | [diff] [blame] | 209 | void BrowserInstantController::FocusOmnibox(bool caret_visibility) { |
[email protected] | c18cb67 | 2012-12-05 04:42:12 | [diff] [blame] | 210 | OmniboxView* omnibox_view = browser_->window()->GetLocationBar()-> |
| 211 | GetLocationEntry(); |
| 212 | omnibox_view->SetFocus(); |
[email protected] | 10aec59 | 2013-03-06 20:19:00 | [diff] [blame] | 213 | omnibox_view->model()->SetCaretVisibility(caret_visibility); |
[email protected] | c18cb67 | 2012-12-05 04:42:12 | [diff] [blame] | 214 | } |
| 215 | |
[email protected] | cd533bf | 2012-12-04 19:14:59 | [diff] [blame] | 216 | content::WebContents* BrowserInstantController::GetActiveWebContents() const { |
| 217 | return browser_->tab_strip_model()->GetActiveWebContents(); |
[email protected] | 7acfaf9 | 2012-07-11 15:51:59 | [diff] [blame] | 218 | } |
| 219 | |
[email protected] | e41982a7 | 2012-11-20 07:16:51 | [diff] [blame] | 220 | void BrowserInstantController::ActiveTabChanged() { |
| 221 | instant_.ActiveTabChanged(); |
| 222 | } |
| 223 | |
[email protected] | 3d6a895 | 2012-12-14 03:18:07 | [diff] [blame] | 224 | void BrowserInstantController::TabDeactivated(content::WebContents* contents) { |
| 225 | instant_.TabDeactivated(contents); |
| 226 | } |
| 227 | |
[email protected] | 3870363e | 2013-03-06 20:11:23 | [diff] [blame] | 228 | void BrowserInstantController::UpdateThemeInfo() { |
[email protected] | e4a379c | 2013-02-15 19:27:19 | [diff] [blame] | 229 | // Update theme background info. |
[email protected] | 3870363e | 2013-03-06 20:11:23 | [diff] [blame] | 230 | // Initialize |theme_info| if necessary. |
| 231 | if (!initialized_theme_info_) |
[email protected] | 0c940663 | 2013-02-08 01:13:33 | [diff] [blame] | 232 | OnThemeChanged(ThemeServiceFactory::GetForProfile(profile())); |
[email protected] | a682765 | 2012-11-20 23:41:08 | [diff] [blame] | 233 | else |
| 234 | OnThemeChanged(NULL); |
| 235 | } |
| 236 | |
[email protected] | 3c3acca | 2013-02-26 03:07:07 | [diff] [blame] | 237 | void BrowserInstantController::OpenURL( |
[email protected] | e3033eb | 2012-12-13 23:46:08 | [diff] [blame] | 238 | const GURL& url, |
[email protected] | 3c3acca | 2013-02-26 03:07:07 | [diff] [blame] | 239 | content::PageTransition transition, |
| 240 | WindowOpenDisposition disposition) { |
[email protected] | e3033eb | 2012-12-13 23:46:08 | [diff] [blame] | 241 | browser_->OpenURL(content::OpenURLParams(url, |
| 242 | content::Referrer(), |
[email protected] | 3c3acca | 2013-02-26 03:07:07 | [diff] [blame] | 243 | disposition, |
[email protected] | e3033eb | 2012-12-13 23:46:08 | [diff] [blame] | 244 | transition, |
| 245 | false)); |
| 246 | } |
| 247 | |
[email protected] | fcde79a | 2013-02-28 02:25:09 | [diff] [blame] | 248 | void BrowserInstantController::SetOmniboxBounds(const gfx::Rect& bounds) { |
| 249 | instant_.SetOmniboxBounds(bounds); |
[email protected] | ec4aad54 | 2012-12-14 01:11:04 | [diff] [blame] | 250 | } |
| 251 | |
[email protected] | 3473ae0 | 2013-06-07 00:28:08 | [diff] [blame] | 252 | void BrowserInstantController::ToggleVoiceSearch() { |
| 253 | instant_.ToggleVoiceSearch(); |
| 254 | } |
| 255 | |
[email protected] | 5fac378 | 2013-03-06 09:32:31 | [diff] [blame] | 256 | void BrowserInstantController::ResetInstant(const std::string& pref_name) { |
[email protected] | a1a83ef6 | 2013-04-29 19:15:43 | [diff] [blame] | 257 | bool instant_checkbox_checked = chrome::IsInstantCheckboxChecked(profile()); |
| 258 | bool use_local_overlay_only = |
[email protected] | 07fd48a | 2013-04-13 02:53:27 | [diff] [blame] | 259 | chrome::IsLocalOnlyInstantExtendedAPIEnabled() || |
[email protected] | a1a83ef6 | 2013-04-29 19:15:43 | [diff] [blame] | 260 | !chrome::IsInstantCheckboxEnabled(profile()); |
| 261 | instant_.SetInstantEnabled(instant_checkbox_checked, use_local_overlay_only); |
[email protected] | 749ce88 | 2012-11-16 04:18:01 | [diff] [blame] | 262 | } |
| 263 | |
[email protected] | 7acfaf9 | 2012-07-11 15:51:59 | [diff] [blame] | 264 | //////////////////////////////////////////////////////////////////////////////// |
[email protected] | 165fe42 | 2013-03-27 06:34:03 | [diff] [blame] | 265 | // BrowserInstantController, SearchModelObserver implementation: |
[email protected] | 0b10c9ff | 2012-10-09 17:31:55 | [diff] [blame] | 266 | |
[email protected] | 5ee671f | 2013-03-19 11:23:05 | [diff] [blame] | 267 | void BrowserInstantController::ModelChanged( |
[email protected] | 165fe42 | 2013-03-27 06:34:03 | [diff] [blame] | 268 | const SearchModel::State& old_state, |
| 269 | const SearchModel::State& new_state) { |
[email protected] | 5ee671f | 2013-03-19 11:23:05 | [diff] [blame] | 270 | if (old_state.mode == new_state.mode) |
| 271 | return; |
| 272 | |
[email protected] | 165fe42 | 2013-03-27 06:34:03 | [diff] [blame] | 273 | const SearchMode& new_mode = new_state.mode; |
[email protected] | 5ee671f | 2013-03-19 11:23:05 | [diff] [blame] | 274 | |
[email protected] | 165fe42 | 2013-03-27 06:34:03 | [diff] [blame] | 275 | if (chrome::IsInstantExtendedAPIEnabled()) { |
[email protected] | c19ba104 | 2013-03-11 17:17:13 | [diff] [blame] | 276 | // Record some actions corresponding to the mode change. Note that to get |
| 277 | // the full story, it's necessary to look at other UMA actions as well, |
| 278 | // such as tab switches. |
| 279 | if (new_mode.is_search_results()) |
| 280 | content::RecordAction(UserMetricsAction("InstantExtended.ShowSRP")); |
| 281 | else if (new_mode.is_ntp()) |
| 282 | content::RecordAction(UserMetricsAction("InstantExtended.ShowNTP")); |
| 283 | } |
| 284 | |
[email protected] | ddd28ae | 2013-03-04 21:11:54 | [diff] [blame] | 285 | // If mode is now |NTP|, send theme-related information to Instant. |
[email protected] | a682765 | 2012-11-20 23:41:08 | [diff] [blame] | 286 | if (new_mode.is_ntp()) |
[email protected] | 3870363e | 2013-03-06 20:11:23 | [diff] [blame] | 287 | UpdateThemeInfo(); |
[email protected] | a682765 | 2012-11-20 23:41:08 | [diff] [blame] | 288 | |
[email protected] | 5ee671f | 2013-03-19 11:23:05 | [diff] [blame] | 289 | instant_.SearchModeChanged(old_state.mode, new_mode); |
[email protected] | 7acfaf9 | 2012-07-11 15:51:59 | [diff] [blame] | 290 | } |
| 291 | |
[email protected] | a682765 | 2012-11-20 23:41:08 | [diff] [blame] | 292 | //////////////////////////////////////////////////////////////////////////////// |
| 293 | // BrowserInstantController, content::NotificationObserver implementation: |
| 294 | |
| 295 | void BrowserInstantController::Observe( |
| 296 | int type, |
| 297 | const content::NotificationSource& source, |
| 298 | const content::NotificationDetails& details) { |
| 299 | #if defined(ENABLE_THEMES) |
| 300 | DCHECK_EQ(chrome::NOTIFICATION_BROWSER_THEME_CHANGED, type); |
| 301 | OnThemeChanged(content::Source<ThemeService>(source).ptr()); |
| 302 | #endif // defined(ENABLE_THEMES) |
| 303 | } |
| 304 | |
| 305 | void BrowserInstantController::OnThemeChanged(ThemeService* theme_service) { |
| 306 | if (theme_service) { // Get theme information from theme service. |
| 307 | theme_info_ = ThemeBackgroundInfo(); |
| 308 | |
| 309 | // Set theme background color. |
| 310 | SkColor background_color = |
[email protected] | e119b80 | 2013-02-18 18:55:39 | [diff] [blame] | 311 | theme_service->GetColor(ThemeProperties::COLOR_NTP_BACKGROUND); |
[email protected] | a682765 | 2012-11-20 23:41:08 | [diff] [blame] | 312 | if (gfx::IsInvertedColorScheme()) |
| 313 | background_color = color_utils::InvertColor(background_color); |
| 314 | theme_info_.color_r = SkColorGetR(background_color); |
| 315 | theme_info_.color_g = SkColorGetG(background_color); |
| 316 | theme_info_.color_b = SkColorGetB(background_color); |
| 317 | theme_info_.color_a = SkColorGetA(background_color); |
| 318 | |
| 319 | if (theme_service->HasCustomImage(IDR_THEME_NTP_BACKGROUND)) { |
| 320 | // Set theme id for theme background image url. |
| 321 | theme_info_.theme_id = theme_service->GetThemeID(); |
| 322 | |
| 323 | // Set theme background image horizontal alignment. |
| 324 | int alignment = 0; |
[email protected] | e119b80 | 2013-02-18 18:55:39 | [diff] [blame] | 325 | theme_service->GetDisplayProperty( |
| 326 | ThemeProperties::NTP_BACKGROUND_ALIGNMENT, &alignment); |
| 327 | if (alignment & ThemeProperties::ALIGN_LEFT) { |
[email protected] | a682765 | 2012-11-20 23:41:08 | [diff] [blame] | 328 | theme_info_.image_horizontal_alignment = THEME_BKGRND_IMAGE_ALIGN_LEFT; |
[email protected] | e119b80 | 2013-02-18 18:55:39 | [diff] [blame] | 329 | } else if (alignment & ThemeProperties::ALIGN_RIGHT) { |
[email protected] | a682765 | 2012-11-20 23:41:08 | [diff] [blame] | 330 | theme_info_.image_horizontal_alignment = THEME_BKGRND_IMAGE_ALIGN_RIGHT; |
| 331 | } else { // ALIGN_CENTER |
| 332 | theme_info_.image_horizontal_alignment = |
| 333 | THEME_BKGRND_IMAGE_ALIGN_CENTER; |
| 334 | } |
| 335 | |
| 336 | // Set theme background image vertical alignment. |
[email protected] | 3870363e | 2013-03-06 20:11:23 | [diff] [blame] | 337 | if (alignment & ThemeProperties::ALIGN_TOP) |
[email protected] | a682765 | 2012-11-20 23:41:08 | [diff] [blame] | 338 | theme_info_.image_vertical_alignment = THEME_BKGRND_IMAGE_ALIGN_TOP; |
[email protected] | 3870363e | 2013-03-06 20:11:23 | [diff] [blame] | 339 | else if (alignment & ThemeProperties::ALIGN_BOTTOM) |
[email protected] | a682765 | 2012-11-20 23:41:08 | [diff] [blame] | 340 | theme_info_.image_vertical_alignment = THEME_BKGRND_IMAGE_ALIGN_BOTTOM; |
[email protected] | 3870363e | 2013-03-06 20:11:23 | [diff] [blame] | 341 | else // ALIGN_CENTER |
[email protected] | a682765 | 2012-11-20 23:41:08 | [diff] [blame] | 342 | theme_info_.image_vertical_alignment = THEME_BKGRND_IMAGE_ALIGN_CENTER; |
[email protected] | a682765 | 2012-11-20 23:41:08 | [diff] [blame] | 343 | |
| 344 | // Set theme background image tiling. |
| 345 | int tiling = 0; |
[email protected] | e119b80 | 2013-02-18 18:55:39 | [diff] [blame] | 346 | theme_service->GetDisplayProperty(ThemeProperties::NTP_BACKGROUND_TILING, |
[email protected] | a682765 | 2012-11-20 23:41:08 | [diff] [blame] | 347 | &tiling); |
| 348 | switch (tiling) { |
[email protected] | e119b80 | 2013-02-18 18:55:39 | [diff] [blame] | 349 | case ThemeProperties::NO_REPEAT: |
[email protected] | a682765 | 2012-11-20 23:41:08 | [diff] [blame] | 350 | theme_info_.image_tiling = THEME_BKGRND_IMAGE_NO_REPEAT; |
| 351 | break; |
[email protected] | e119b80 | 2013-02-18 18:55:39 | [diff] [blame] | 352 | case ThemeProperties::REPEAT_X: |
[email protected] | a682765 | 2012-11-20 23:41:08 | [diff] [blame] | 353 | theme_info_.image_tiling = THEME_BKGRND_IMAGE_REPEAT_X; |
| 354 | break; |
[email protected] | e119b80 | 2013-02-18 18:55:39 | [diff] [blame] | 355 | case ThemeProperties::REPEAT_Y: |
[email protected] | a682765 | 2012-11-20 23:41:08 | [diff] [blame] | 356 | theme_info_.image_tiling = THEME_BKGRND_IMAGE_REPEAT_Y; |
| 357 | break; |
[email protected] | e119b80 | 2013-02-18 18:55:39 | [diff] [blame] | 358 | case ThemeProperties::REPEAT: |
[email protected] | a682765 | 2012-11-20 23:41:08 | [diff] [blame] | 359 | theme_info_.image_tiling = THEME_BKGRND_IMAGE_REPEAT; |
| 360 | break; |
| 361 | } |
| 362 | |
| 363 | // Set theme background image height. |
| 364 | gfx::ImageSkia* image = theme_service->GetImageSkiaNamed( |
| 365 | IDR_THEME_NTP_BACKGROUND); |
| 366 | DCHECK(image); |
| 367 | theme_info_.image_height = image->height(); |
[email protected] | 5189fa1 | 2013-03-20 23:00:05 | [diff] [blame] | 368 | |
| 369 | theme_info_.has_attribution = |
| 370 | theme_service->HasCustomImage(IDR_THEME_NTP_ATTRIBUTION); |
[email protected] | a682765 | 2012-11-20 23:41:08 | [diff] [blame] | 371 | } |
| 372 | |
| 373 | initialized_theme_info_ = true; |
| 374 | } |
| 375 | |
| 376 | DCHECK(initialized_theme_info_); |
| 377 | |
[email protected] | e4a379c | 2013-02-15 19:27:19 | [diff] [blame] | 378 | if (browser_->search_model()->mode().is_ntp()) |
[email protected] | a682765 | 2012-11-20 23:41:08 | [diff] [blame] | 379 | instant_.ThemeChanged(theme_info_); |
[email protected] | a682765 | 2012-11-20 23:41:08 | [diff] [blame] | 380 | } |
[email protected] | 0a46856e | 2013-04-24 00:33:02 | [diff] [blame] | 381 | |
| 382 | void BrowserInstantController::OnDefaultSearchProviderChanged( |
| 383 | const std::string& pref_name) { |
| 384 | DCHECK_EQ(pref_name, std::string(prefs::kDefaultSearchProviderID)); |
| 385 | |
| 386 | Profile* browser_profile = profile(); |
| 387 | const TemplateURL* template_url = |
| 388 | TemplateURLServiceFactory::GetForProfile(browser_profile)-> |
| 389 | GetDefaultSearchProvider(); |
| 390 | if (!template_url) { |
| 391 | // A NULL |template_url| could mean either this notification is sent during |
| 392 | // the browser start up operation or the user now has no default search |
| 393 | // provider. There is no way for the user to reach this state using the |
| 394 | // Chrome settings. Only explicitly poking at the DB or bugs in the Sync |
| 395 | // could cause that, neither of which we support. |
| 396 | return; |
| 397 | } |
| 398 | |
| 399 | InstantService* instant_service = |
| 400 | InstantServiceFactory::GetForProfile(browser_profile); |
| 401 | if (!instant_service) |
| 402 | return; |
| 403 | |
| 404 | TabStripModel* tab_model = browser_->tab_strip_model(); |
| 405 | int count = tab_model->count(); |
| 406 | for (int index = 0; index < count; ++index) { |
| 407 | content::WebContents* contents = tab_model->GetWebContentsAt(index); |
| 408 | if (!contents) |
| 409 | continue; |
| 410 | |
| 411 | // A Local NTP always runs in the Instant process, so reloading it is |
| 412 | // neither useful nor necessary. However, the Local NTP does not reflect |
| 413 | // whether Google is the default search engine or not. This is achieved |
| 414 | // through a URL parameter, so reloading the existing URL won't fix that |
| 415 | // (i.e., the Local NTP may now show an incorrect search engine logo). |
| 416 | // TODO(kmadhusu): Fix. |
| 417 | if (contents->GetURL() == GURL(chrome::kChromeSearchLocalNtpUrl)) |
| 418 | continue; |
| 419 | |
| 420 | if (!instant_service->IsInstantProcess( |
| 421 | contents->GetRenderProcessHost()->GetID())) |
| 422 | continue; |
| 423 | |
| 424 | // Reload the contents to ensure that it gets assigned to a non-priviledged |
| 425 | // renderer. |
| 426 | contents->GetController().Reload(false); |
| 427 | } |
| 428 | instant_.OnDefaultSearchProviderChanged(); |
| 429 | } |