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" | |
9 #include "chrome/browser/extensions/extension_service.h" | 8 #include "chrome/browser/extensions/extension_service.h" |
10 #include "chrome/browser/extensions/extension_web_ui.h" | 9 #include "chrome/browser/extensions/extension_web_ui.h" |
11 #include "chrome/browser/profiles/profile.h" | 10 #include "chrome/browser/profiles/profile.h" |
12 #include "chrome/browser/search/instant_service.h" | 11 #include "chrome/browser/search/instant_service.h" |
13 #include "chrome/browser/search/instant_service_factory.h" | 12 #include "chrome/browser/search/instant_service_factory.h" |
14 #include "chrome/browser/search/search.h" | 13 #include "chrome/browser/search/search.h" |
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" | |
18 #include "chrome/browser/ui/browser.h" | 14 #include "chrome/browser/ui/browser.h" |
19 #include "chrome/browser/ui/browser_window.h" | 15 #include "chrome/browser/ui/browser_window.h" |
20 #include "chrome/browser/ui/omnibox/location_bar.h" | 16 #include "chrome/browser/ui/omnibox/location_bar.h" |
21 #include "chrome/browser/ui/omnibox/omnibox_popup_model.h" | 17 #include "chrome/browser/ui/omnibox/omnibox_popup_model.h" |
22 #include "chrome/browser/ui/omnibox/omnibox_view.h" | 18 #include "chrome/browser/ui/omnibox/omnibox_view.h" |
23 #include "chrome/browser/ui/search/instant_ntp.h" | 19 #include "chrome/browser/ui/search/instant_ntp.h" |
24 #include "chrome/browser/ui/search/search_model.h" | 20 #include "chrome/browser/ui/search/search_model.h" |
25 #include "chrome/browser/ui/search/search_tab_helper.h" | 21 #include "chrome/browser/ui/search/search_tab_helper.h" |
26 #include "chrome/browser/ui/tabs/tab_strip_model.h" | 22 #include "chrome/browser/ui/tabs/tab_strip_model.h" |
27 #include "chrome/browser/ui/webui/ntp/app_launcher_handler.h" | 23 #include "chrome/browser/ui/webui/ntp/app_launcher_handler.h" |
28 #include "chrome/common/pref_names.h" | |
29 #include "chrome/common/url_constants.h" | 24 #include "chrome/common/url_constants.h" |
30 #include "components/user_prefs/pref_registry_syncable.h" | |
31 #include "content/public/browser/render_process_host.h" | 25 #include "content/public/browser/render_process_host.h" |
32 #include "content/public/browser/user_metrics.h" | 26 #include "content/public/browser/user_metrics.h" |
33 #include "content/public/browser/web_contents.h" | 27 #include "content/public/browser/web_contents.h" |
34 #include "content/public/browser/web_contents_view.h" | 28 #include "content/public/browser/web_contents_view.h" |
35 | 29 |
36 using content::UserMetricsAction; | 30 using content::UserMetricsAction; |
37 | 31 |
38 //////////////////////////////////////////////////////////////////////////////// | 32 //////////////////////////////////////////////////////////////////////////////// |
39 // BrowserInstantController, public: | 33 // BrowserInstantController, public: |
40 | 34 |
41 BrowserInstantController::BrowserInstantController(Browser* browser) | 35 BrowserInstantController::BrowserInstantController(Browser* browser) |
42 : browser_(browser), | 36 : browser_(browser), |
43 instant_(this), | 37 instant_(this), |
44 instant_unload_handler_(browser) { | 38 instant_unload_handler_(browser) { |
45 profile_pref_registrar_.Init(profile()->GetPrefs()); | |
46 profile_pref_registrar_.Add( | |
47 prefs::kDefaultSearchProviderID, | |
48 base::Bind(&BrowserInstantController::OnDefaultSearchProviderChanged, | |
49 base::Unretained(this))); | |
50 browser_->search_model()->AddObserver(this); | 39 browser_->search_model()->AddObserver(this); |
51 | 40 |
52 InstantService* instant_service = | 41 InstantService* instant_service = |
53 InstantServiceFactory::GetForProfile(profile()); | 42 InstantServiceFactory::GetForProfile(profile()); |
54 instant_service->OnBrowserInstantControllerCreated(); | 43 instant_service->OnBrowserInstantControllerCreated(); |
| 44 instant_service->AddObserver(this); |
55 } | 45 } |
56 | 46 |
57 BrowserInstantController::~BrowserInstantController() { | 47 BrowserInstantController::~BrowserInstantController() { |
58 browser_->search_model()->RemoveObserver(this); | 48 browser_->search_model()->RemoveObserver(this); |
59 | 49 |
60 InstantService* instant_service = | 50 InstantService* instant_service = |
61 InstantServiceFactory::GetForProfile(profile()); | 51 InstantServiceFactory::GetForProfile(profile()); |
| 52 instant_service->RemoveObserver(this); |
62 instant_service->OnBrowserInstantControllerDestroyed(); | 53 instant_service->OnBrowserInstantControllerDestroyed(); |
63 } | 54 } |
64 | 55 |
65 bool BrowserInstantController::MaybeSwapInInstantNTPContents( | 56 bool BrowserInstantController::MaybeSwapInInstantNTPContents( |
66 const GURL& url, | 57 const GURL& url, |
67 content::WebContents* source_contents, | 58 content::WebContents* source_contents, |
68 content::WebContents** target_contents) { | 59 content::WebContents** target_contents) { |
69 if (url != GURL(chrome::kChromeUINewTabURL)) | 60 if (url != GURL(chrome::kChromeUINewTabURL)) |
70 return false; | 61 return false; |
71 | 62 |
(...skipping 194 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
266 else if (new_mode.is_ntp()) | 257 else if (new_mode.is_ntp()) |
267 content::RecordAction(UserMetricsAction("InstantExtended.ShowNTP")); | 258 content::RecordAction(UserMetricsAction("InstantExtended.ShowNTP")); |
268 | 259 |
269 instant_.SearchModeChanged(old_state.mode, new_mode); | 260 instant_.SearchModeChanged(old_state.mode, new_mode); |
270 } | 261 } |
271 | 262 |
272 if (old_state.instant_support != new_state.instant_support) | 263 if (old_state.instant_support != new_state.instant_support) |
273 instant_.InstantSupportChanged(new_state.instant_support); | 264 instant_.InstantSupportChanged(new_state.instant_support); |
274 } | 265 } |
275 | 266 |
276 void BrowserInstantController::OnDefaultSearchProviderChanged( | 267 //////////////////////////////////////////////////////////////////////////////// |
277 const std::string& pref_name) { | 268 // BrowserInstantController, InstantServiceObserver implementation: |
278 DCHECK_EQ(pref_name, std::string(prefs::kDefaultSearchProviderID)); | |
279 | 269 |
280 Profile* browser_profile = profile(); | 270 void BrowserInstantController::DefaultSearchProviderChanged() { |
281 const TemplateURL* template_url = | 271 ReloadTabsInInstantProcess(); |
282 TemplateURLServiceFactory::GetForProfile(browser_profile)-> | 272 } |
283 GetDefaultSearchProvider(); | |
284 if (!template_url) { | |
285 // A NULL |template_url| could mean either this notification is sent during | |
286 // the browser start up operation or the user now has no default search | |
287 // provider. There is no way for the user to reach this state using the | |
288 // Chrome settings. Only explicitly poking at the DB or bugs in the Sync | |
289 // could cause that, neither of which we support. | |
290 return; | |
291 } | |
292 | 273 |
| 274 void BrowserInstantController::GoogleURLUpdated() { |
| 275 ReloadTabsInInstantProcess(); |
| 276 } |
| 277 |
| 278 void BrowserInstantController::ReloadTabsInInstantProcess() { |
293 InstantService* instant_service = | 279 InstantService* instant_service = |
294 InstantServiceFactory::GetForProfile(browser_profile); | 280 InstantServiceFactory::GetForProfile(profile()); |
295 if (!instant_service) | 281 if (!instant_service) |
296 return; | 282 return; |
297 | 283 |
298 TabStripModel* tab_model = browser_->tab_strip_model(); | 284 TabStripModel* tab_model = browser_->tab_strip_model(); |
299 int count = tab_model->count(); | 285 int count = tab_model->count(); |
300 for (int index = 0; index < count; ++index) { | 286 for (int index = 0; index < count; ++index) { |
301 content::WebContents* contents = tab_model->GetWebContentsAt(index); | 287 content::WebContents* contents = tab_model->GetWebContentsAt(index); |
302 if (!contents) | 288 if (!contents) |
303 continue; | 289 continue; |
304 | 290 |
305 if (!instant_service->IsInstantProcess( | 291 if (!instant_service->IsInstantProcess( |
306 contents->GetRenderProcessHost()->GetID())) | 292 contents->GetRenderProcessHost()->GetID())) |
307 continue; | 293 continue; |
308 | 294 |
309 // Reload the contents to ensure that it gets assigned to a non-priviledged | 295 // Reload the contents to ensure that it gets assigned to a non-priviledged |
310 // renderer. | 296 // renderer. |
311 contents->GetController().Reload(false); | 297 contents->GetController().Reload(false); |
312 } | 298 } |
313 } | 299 } |
OLD | NEW |