blob: cacd42f622a36ad8629604cc05ffa81f678f4262 [file] [log] [blame]
[email protected]e41982a72012-11-20 07:16:511// Copyright 2012 The Chromium Authors. All rights reserved.
[email protected]7acfaf92012-07-11 15:51:592// 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]0a46856e2013-04-24 00:33:027#include "base/bind.h"
[email protected]3853a4c2013-02-11 17:15:578#include "base/prefs/pref_service.h"
[email protected]7acfaf92012-07-11 15:51:599#include "chrome/browser/extensions/extension_service.h"
[email protected]26015ac2013-02-27 02:50:1310#include "chrome/browser/extensions/extension_web_ui.h"
[email protected]7acfaf92012-07-11 15:51:5911#include "chrome/browser/profiles/profile.h"
[email protected]0a46856e2013-04-24 00:33:0212#include "chrome/browser/search/instant_service.h"
13#include "chrome/browser/search/instant_service_factory.h"
[email protected]a7b8e43d2013-03-18 18:52:4314#include "chrome/browser/search/search.h"
[email protected]0a46856e2013-04-24 00:33:0215#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]e119b802013-02-18 18:55:3918#include "chrome/browser/themes/theme_properties.h"
[email protected]a6827652012-11-20 23:41:0819#include "chrome/browser/themes/theme_service.h"
20#include "chrome/browser/themes/theme_service_factory.h"
[email protected]7acfaf92012-07-11 15:51:5921#include "chrome/browser/ui/browser.h"
[email protected]7acfaf92012-07-11 15:51:5922#include "chrome/browser/ui/browser_window.h"
23#include "chrome/browser/ui/omnibox/location_bar.h"
[email protected]c18cb672012-12-05 04:42:1224#include "chrome/browser/ui/omnibox/omnibox_view.h"
[email protected]9d3d11702012-11-08 01:01:1225#include "chrome/browser/ui/search/search_tab_helper.h"
[email protected]e41982a72012-11-20 07:16:5126#include "chrome/browser/ui/tabs/tab_strip_model.h"
[email protected]7acfaf92012-07-11 15:51:5927#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]0c9406632013-02-08 01:13:3330#include "chrome/common/url_constants.h"
[email protected]75fee372013-03-06 00:42:4431#include "components/user_prefs/pref_registry_syncable.h"
[email protected]7acfaf92012-07-11 15:51:5932#include "content/public/browser/notification_service.h"
[email protected]0a46856e2013-04-24 00:33:0233#include "content/public/browser/render_process_host.h"
[email protected]233f0f962013-02-27 21:14:1934#include "content/public/browser/user_metrics.h"
[email protected]0a46856e2013-04-24 00:33:0235#include "content/public/browser/web_contents.h"
[email protected]a6827652012-11-20 23:41:0836#include "grit/theme_resources.h"
37#include "ui/gfx/color_utils.h"
38#include "ui/gfx/sys_color_change_listener.h"
39
[email protected]233f0f962013-02-27 21:14:1940using content::UserMetricsAction;
41
[email protected]7acfaf92012-07-11 15:51:5942////////////////////////////////////////////////////////////////////////////////
43// BrowserInstantController, public:
44
45BrowserInstantController::BrowserInstantController(Browser* browser)
[email protected]8a236702012-09-28 13:30:5746 : browser_(browser),
[email protected]d4b2d232013-04-30 21:14:2347 instant_(this,
[email protected]165fe422013-03-27 06:34:0348 chrome::IsInstantExtendedAPIEnabled()),
[email protected]a6827652012-11-20 23:41:0849 instant_unload_handler_(browser),
[email protected]e4a379c2013-02-15 19:27:1950 initialized_theme_info_(false) {
[email protected]5879cef2013-03-02 17:02:2551
[email protected]d2bb50e2013-05-09 12:38:3452 // TODO(sreeram): Perhaps this can be removed, if field trial info is
53 // available before we need to register the pref.
[email protected]165fe422013-03-27 06:34:0354 chrome::SetInstantExtendedPrefDefault(profile());
[email protected]5879cef2013-03-02 17:02:2555
[email protected]5fac3782013-03-06 09:32:3156 profile_pref_registrar_.Init(profile()->GetPrefs());
[email protected]0b3fa502012-11-21 13:57:4657 profile_pref_registrar_.Add(
[email protected]d2bb50e2013-05-09 12:38:3458 prefs::kSearchInstantEnabled,
[email protected]0b3fa502012-11-21 13:57:4659 base::Bind(&BrowserInstantController::ResetInstant,
60 base::Unretained(this)));
[email protected]411c9c02013-02-07 04:55:5961 profile_pref_registrar_.Add(
62 prefs::kSearchSuggestEnabled,
63 base::Bind(&BrowserInstantController::ResetInstant,
64 base::Unretained(this)));
[email protected]0a46856e2013-04-24 00:33:0265 profile_pref_registrar_.Add(
66 prefs::kDefaultSearchProviderID,
67 base::Bind(&BrowserInstantController::OnDefaultSearchProviderChanged,
68 base::Unretained(this)));
[email protected]5fac3782013-03-06 09:32:3169 ResetInstant(std::string());
[email protected]0b10c9ff2012-10-09 17:31:5570 browser_->search_model()->AddObserver(this);
[email protected]a6827652012-11-20 23:41:0871
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]0c9406632013-02-08 01:13:3376 ThemeServiceFactory::GetForProfile(profile())));
[email protected]a6827652012-11-20 23:41:0877#endif // defined(ENABLE_THEMES)
[email protected]7acfaf92012-07-11 15:51:5978}
79
80BrowserInstantController::~BrowserInstantController() {
[email protected]0b10c9ff2012-10-09 17:31:5581 browser_->search_model()->RemoveObserver(this);
[email protected]7acfaf92012-07-11 15:51:5982}
83
[email protected]0c9406632013-02-08 01:13:3384bool 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]26015ac2013-02-27 02:50:1391 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]0c9406632013-02-08 01:13:3397 scoped_ptr<content::WebContents> instant_ntp = instant_.ReleaseNTPContents();
98 if (!instant_ntp)
99 return false;
100
101 *target_contents = instant_ntp.get();
[email protected]0c9406632013-02-08 01:13:33102 if (source_contents) {
[email protected]474f8512013-05-31 22:31:16103 // 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]0c9406632013-02-08 01:13:33120 } else {
[email protected]474f8512013-05-31 22:31:16121 // 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]0c9406632013-02-08 01:13:33129 // 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]0c9406632013-02-08 01:13:33133 return true;
134}
135
[email protected]413558cb2013-06-10 16:44:45136bool BrowserInstantController::OpenInstant(WindowOpenDisposition disposition,
137 const GURL& url) {
[email protected]e41982a72012-11-20 07:16:51138 // Unsupported dispositions.
[email protected]413558cb2013-06-10 16:44:45139 if (disposition == NEW_BACKGROUND_TAB || disposition == NEW_WINDOW ||
140 disposition == NEW_FOREGROUND_TAB)
[email protected]7acfaf92012-07-11 15:51:59141 return false;
[email protected]7acfaf92012-07-11 15:51:59142
[email protected]7acfaf92012-07-11 15:51:59143 // The omnibox currently doesn't use other dispositions, so we don't attempt
[email protected]c72226c82012-10-01 21:02:32144 // to handle them. If you hit this DCHECK file a bug and I'll (sky) add
[email protected]7acfaf92012-07-11 15:51:59145 // support for the new disposition.
[email protected]413558cb2013-06-10 16:44:45146 DCHECK(disposition == CURRENT_TAB) << disposition;
[email protected]c72226c82012-10-01 21:02:32147
[email protected]413558cb2013-06-10 16:44:45148 // 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]7acfaf92012-07-11 15:51:59156}
157
[email protected]0c9406632013-02-08 01:13:33158Profile* BrowserInstantController::profile() const {
159 return browser_->profile();
160}
161
162void BrowserInstantController::CommitInstant(
[email protected]ddd28ae2013-03-04 21:11:54163 scoped_ptr<content::WebContents> overlay,
[email protected]0c9406632013-02-08 01:13:33164 bool in_new_tab) {
[email protected]79f17ef462013-03-24 00:28:18165 const extensions::Extension* extension =
166 profile()->GetExtensionService()->GetInstalledApp(overlay->GetURL());
167 if (extension) {
[email protected]0c9406632013-02-08 01:13:33168 AppLauncherHandler::RecordAppLaunchType(
[email protected]79f17ef462013-03-24 00:28:18169 extension_misc::APP_LAUNCH_OMNIBOX_INSTANT,
170 extension->GetType());
[email protected]0c9406632013-02-08 01:13:33171 }
[email protected]c72226c82012-10-01 21:02:32172 if (in_new_tab) {
[email protected]ddd28ae2013-03-04 21:11:54173 // TabStripModel takes ownership of |overlay|.
174 browser_->tab_strip_model()->AddWebContents(overlay.release(), -1,
[email protected]e41982a72012-11-20 07:16:51175 instant_.last_transition_type(), TabStripModel::ADD_ACTIVE);
[email protected]c72226c82012-10-01 21:02:32176 } else {
[email protected]9bc3f242013-04-05 23:27:36177 content::WebContents* contents = overlay.get();
[email protected]0c9406632013-02-08 01:13:33178 ReplaceWebContentsAt(
179 browser_->tab_strip_model()->active_index(),
[email protected]ddd28ae2013-03-04 21:11:54180 overlay.Pass());
[email protected]9bc3f242013-04-05 23:27:36181 browser_->window()->GetLocationBar()->SaveStateToContents(contents);
[email protected]7acfaf92012-07-11 15:51:59182 }
183}
184
[email protected]0c9406632013-02-08 01:13:33185void BrowserInstantController::ReplaceWebContentsAt(
186 int index,
187 scoped_ptr<content::WebContents> new_contents) {
188 DCHECK_NE(TabStripModel::kNoTab, index);
[email protected]d572bfd2013-02-14 06:14:20189 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]0c9406632013-02-08 01:13:33193}
194
[email protected]93b73832012-10-18 20:18:38195void BrowserInstantController::SetInstantSuggestion(
196 const InstantSuggestion& suggestion) {
[email protected]c18cb672012-12-05 04:42:12197 browser_->window()->GetLocationBar()->SetInstantSuggestion(suggestion);
[email protected]7acfaf92012-07-11 15:51:59198}
199
[email protected]7acfaf92012-07-11 15:51:59200gfx::Rect BrowserInstantController::GetInstantBounds() {
201 return browser_->window()->GetInstantBounds();
202}
203
[email protected]ddd28ae2013-03-04 21:11:54204void BrowserInstantController::InstantOverlayFocused() {
[email protected]7acfaf92012-07-11 15:51:59205 // NOTE: This is only invoked on aura.
[email protected]ddd28ae2013-03-04 21:11:54206 browser_->window()->WebContentsFocused(instant_.GetOverlayContents());
[email protected]7acfaf92012-07-11 15:51:59207}
208
[email protected]10aec592013-03-06 20:19:00209void BrowserInstantController::FocusOmnibox(bool caret_visibility) {
[email protected]c18cb672012-12-05 04:42:12210 OmniboxView* omnibox_view = browser_->window()->GetLocationBar()->
211 GetLocationEntry();
212 omnibox_view->SetFocus();
[email protected]10aec592013-03-06 20:19:00213 omnibox_view->model()->SetCaretVisibility(caret_visibility);
[email protected]c18cb672012-12-05 04:42:12214}
215
[email protected]cd533bf2012-12-04 19:14:59216content::WebContents* BrowserInstantController::GetActiveWebContents() const {
217 return browser_->tab_strip_model()->GetActiveWebContents();
[email protected]7acfaf92012-07-11 15:51:59218}
219
[email protected]e41982a72012-11-20 07:16:51220void BrowserInstantController::ActiveTabChanged() {
221 instant_.ActiveTabChanged();
222}
223
[email protected]3d6a8952012-12-14 03:18:07224void BrowserInstantController::TabDeactivated(content::WebContents* contents) {
225 instant_.TabDeactivated(contents);
226}
227
[email protected]3870363e2013-03-06 20:11:23228void BrowserInstantController::UpdateThemeInfo() {
[email protected]e4a379c2013-02-15 19:27:19229 // Update theme background info.
[email protected]3870363e2013-03-06 20:11:23230 // Initialize |theme_info| if necessary.
231 if (!initialized_theme_info_)
[email protected]0c9406632013-02-08 01:13:33232 OnThemeChanged(ThemeServiceFactory::GetForProfile(profile()));
[email protected]a6827652012-11-20 23:41:08233 else
234 OnThemeChanged(NULL);
235}
236
[email protected]3c3acca2013-02-26 03:07:07237void BrowserInstantController::OpenURL(
[email protected]e3033eb2012-12-13 23:46:08238 const GURL& url,
[email protected]3c3acca2013-02-26 03:07:07239 content::PageTransition transition,
240 WindowOpenDisposition disposition) {
[email protected]e3033eb2012-12-13 23:46:08241 browser_->OpenURL(content::OpenURLParams(url,
242 content::Referrer(),
[email protected]3c3acca2013-02-26 03:07:07243 disposition,
[email protected]e3033eb2012-12-13 23:46:08244 transition,
245 false));
246}
247
[email protected]fcde79a2013-02-28 02:25:09248void BrowserInstantController::SetOmniboxBounds(const gfx::Rect& bounds) {
249 instant_.SetOmniboxBounds(bounds);
[email protected]ec4aad542012-12-14 01:11:04250}
251
[email protected]3473ae02013-06-07 00:28:08252void BrowserInstantController::ToggleVoiceSearch() {
253 instant_.ToggleVoiceSearch();
254}
255
[email protected]5fac3782013-03-06 09:32:31256void BrowserInstantController::ResetInstant(const std::string& pref_name) {
[email protected]a1a83ef62013-04-29 19:15:43257 bool instant_checkbox_checked = chrome::IsInstantCheckboxChecked(profile());
258 bool use_local_overlay_only =
[email protected]07fd48a2013-04-13 02:53:27259 chrome::IsLocalOnlyInstantExtendedAPIEnabled() ||
[email protected]a1a83ef62013-04-29 19:15:43260 !chrome::IsInstantCheckboxEnabled(profile());
261 instant_.SetInstantEnabled(instant_checkbox_checked, use_local_overlay_only);
[email protected]749ce882012-11-16 04:18:01262}
263
[email protected]7acfaf92012-07-11 15:51:59264////////////////////////////////////////////////////////////////////////////////
[email protected]165fe422013-03-27 06:34:03265// BrowserInstantController, SearchModelObserver implementation:
[email protected]0b10c9ff2012-10-09 17:31:55266
[email protected]5ee671f2013-03-19 11:23:05267void BrowserInstantController::ModelChanged(
[email protected]165fe422013-03-27 06:34:03268 const SearchModel::State& old_state,
269 const SearchModel::State& new_state) {
[email protected]5ee671f2013-03-19 11:23:05270 if (old_state.mode == new_state.mode)
271 return;
272
[email protected]165fe422013-03-27 06:34:03273 const SearchMode& new_mode = new_state.mode;
[email protected]5ee671f2013-03-19 11:23:05274
[email protected]165fe422013-03-27 06:34:03275 if (chrome::IsInstantExtendedAPIEnabled()) {
[email protected]c19ba1042013-03-11 17:17:13276 // 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]ddd28ae2013-03-04 21:11:54285 // If mode is now |NTP|, send theme-related information to Instant.
[email protected]a6827652012-11-20 23:41:08286 if (new_mode.is_ntp())
[email protected]3870363e2013-03-06 20:11:23287 UpdateThemeInfo();
[email protected]a6827652012-11-20 23:41:08288
[email protected]5ee671f2013-03-19 11:23:05289 instant_.SearchModeChanged(old_state.mode, new_mode);
[email protected]7acfaf92012-07-11 15:51:59290}
291
[email protected]a6827652012-11-20 23:41:08292////////////////////////////////////////////////////////////////////////////////
293// BrowserInstantController, content::NotificationObserver implementation:
294
295void 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
305void 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]e119b802013-02-18 18:55:39311 theme_service->GetColor(ThemeProperties::COLOR_NTP_BACKGROUND);
[email protected]a6827652012-11-20 23:41:08312 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]e119b802013-02-18 18:55:39325 theme_service->GetDisplayProperty(
326 ThemeProperties::NTP_BACKGROUND_ALIGNMENT, &alignment);
327 if (alignment & ThemeProperties::ALIGN_LEFT) {
[email protected]a6827652012-11-20 23:41:08328 theme_info_.image_horizontal_alignment = THEME_BKGRND_IMAGE_ALIGN_LEFT;
[email protected]e119b802013-02-18 18:55:39329 } else if (alignment & ThemeProperties::ALIGN_RIGHT) {
[email protected]a6827652012-11-20 23:41:08330 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]3870363e2013-03-06 20:11:23337 if (alignment & ThemeProperties::ALIGN_TOP)
[email protected]a6827652012-11-20 23:41:08338 theme_info_.image_vertical_alignment = THEME_BKGRND_IMAGE_ALIGN_TOP;
[email protected]3870363e2013-03-06 20:11:23339 else if (alignment & ThemeProperties::ALIGN_BOTTOM)
[email protected]a6827652012-11-20 23:41:08340 theme_info_.image_vertical_alignment = THEME_BKGRND_IMAGE_ALIGN_BOTTOM;
[email protected]3870363e2013-03-06 20:11:23341 else // ALIGN_CENTER
[email protected]a6827652012-11-20 23:41:08342 theme_info_.image_vertical_alignment = THEME_BKGRND_IMAGE_ALIGN_CENTER;
[email protected]a6827652012-11-20 23:41:08343
344 // Set theme background image tiling.
345 int tiling = 0;
[email protected]e119b802013-02-18 18:55:39346 theme_service->GetDisplayProperty(ThemeProperties::NTP_BACKGROUND_TILING,
[email protected]a6827652012-11-20 23:41:08347 &tiling);
348 switch (tiling) {
[email protected]e119b802013-02-18 18:55:39349 case ThemeProperties::NO_REPEAT:
[email protected]a6827652012-11-20 23:41:08350 theme_info_.image_tiling = THEME_BKGRND_IMAGE_NO_REPEAT;
351 break;
[email protected]e119b802013-02-18 18:55:39352 case ThemeProperties::REPEAT_X:
[email protected]a6827652012-11-20 23:41:08353 theme_info_.image_tiling = THEME_BKGRND_IMAGE_REPEAT_X;
354 break;
[email protected]e119b802013-02-18 18:55:39355 case ThemeProperties::REPEAT_Y:
[email protected]a6827652012-11-20 23:41:08356 theme_info_.image_tiling = THEME_BKGRND_IMAGE_REPEAT_Y;
357 break;
[email protected]e119b802013-02-18 18:55:39358 case ThemeProperties::REPEAT:
[email protected]a6827652012-11-20 23:41:08359 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]5189fa12013-03-20 23:00:05368
369 theme_info_.has_attribution =
370 theme_service->HasCustomImage(IDR_THEME_NTP_ATTRIBUTION);
[email protected]a6827652012-11-20 23:41:08371 }
372
373 initialized_theme_info_ = true;
374 }
375
376 DCHECK(initialized_theme_info_);
377
[email protected]e4a379c2013-02-15 19:27:19378 if (browser_->search_model()->mode().is_ntp())
[email protected]a6827652012-11-20 23:41:08379 instant_.ThemeChanged(theme_info_);
[email protected]a6827652012-11-20 23:41:08380}
[email protected]0a46856e2013-04-24 00:33:02381
382void 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}