Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(9)

Side by Side Diff: chrome/browser/search/instant_service.cc

Issue 20388003: Reload Instant NTP and Instant-process tabs on search url change (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 7 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2013 The Chromium Authors. All rights reserved. 1 // Copyright 2013 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/search/instant_service.h" 5 #include "chrome/browser/search/instant_service.h"
6 6
7 #include <vector> 7 #include <vector>
8 8
9 #include "base/logging.h" 9 #include "base/logging.h"
10 #include "base/prefs/pref_service.h"
10 #include "base/strings/string_number_conversions.h" 11 #include "base/strings/string_number_conversions.h"
11 #include "chrome/browser/chrome_notification_types.h" 12 #include "chrome/browser/chrome_notification_types.h"
12 #include "chrome/browser/history/history_notifications.h" 13 #include "chrome/browser/history/history_notifications.h"
13 #include "chrome/browser/history/top_sites.h" 14 #include "chrome/browser/history/top_sites.h"
14 #include "chrome/browser/profiles/profile.h" 15 #include "chrome/browser/profiles/profile.h"
15 #include "chrome/browser/search/instant_io_context.h" 16 #include "chrome/browser/search/instant_io_context.h"
16 #include "chrome/browser/search/instant_service_factory.h" 17 #include "chrome/browser/search/instant_service_factory.h"
17 #include "chrome/browser/search/instant_service_observer.h" 18 #include "chrome/browser/search/instant_service_observer.h"
18 #include "chrome/browser/search/local_ntp_source.h" 19 #include "chrome/browser/search/local_ntp_source.h"
19 #include "chrome/browser/search/most_visited_iframe_source.h" 20 #include "chrome/browser/search/most_visited_iframe_source.h"
20 #include "chrome/browser/search/search.h" 21 #include "chrome/browser/search/search.h"
22 #include "chrome/browser/search_engines/template_url.h"
23 #include "chrome/browser/search_engines/template_url_service.h"
24 #include "chrome/browser/search_engines/template_url_service_factory.h"
21 #include "chrome/browser/themes/theme_properties.h" 25 #include "chrome/browser/themes/theme_properties.h"
22 #include "chrome/browser/themes/theme_service.h" 26 #include "chrome/browser/themes/theme_service.h"
23 #include "chrome/browser/themes/theme_service_factory.h" 27 #include "chrome/browser/themes/theme_service_factory.h"
24 #include "chrome/browser/ui/webui/favicon_source.h" 28 #include "chrome/browser/ui/webui/favicon_source.h"
25 #include "chrome/browser/ui/webui/ntp/thumbnail_source.h" 29 #include "chrome/browser/ui/webui/ntp/thumbnail_source.h"
26 #include "chrome/browser/ui/webui/theme_source.h" 30 #include "chrome/browser/ui/webui/theme_source.h"
31 #include "chrome/common/pref_names.h"
27 #include "content/public/browser/browser_thread.h" 32 #include "content/public/browser/browser_thread.h"
33 #include "content/public/browser/notification_details.h"
28 #include "content/public/browser/notification_service.h" 34 #include "content/public/browser/notification_service.h"
35 #include "content/public/browser/notification_source.h"
29 #include "content/public/browser/notification_types.h" 36 #include "content/public/browser/notification_types.h"
30 #include "content/public/browser/render_process_host.h" 37 #include "content/public/browser/render_process_host.h"
31 #include "content/public/browser/url_data_source.h" 38 #include "content/public/browser/url_data_source.h"
32 #include "grit/theme_resources.h" 39 #include "grit/theme_resources.h"
40 #include "net/base/net_util.h"
33 #include "net/url_request/url_request.h" 41 #include "net/url_request/url_request.h"
34 #include "ui/gfx/color_utils.h" 42 #include "ui/gfx/color_utils.h"
35 #include "ui/gfx/image/image_skia.h" 43 #include "ui/gfx/image/image_skia.h"
36 #include "ui/gfx/sys_color_change_listener.h" 44 #include "ui/gfx/sys_color_change_listener.h"
37 #include "url/gurl.h" 45 #include "url/gurl.h"
38 46
39 using content::BrowserThread; 47 using content::BrowserThread;
40 48
41 namespace { 49 namespace {
42 50
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after
82 profile->GetResourceContext(), instant_io_context_)); 90 profile->GetResourceContext(), instant_io_context_));
83 } 91 }
84 92
85 // Set up the data sources that Instant uses on the NTP. 93 // Set up the data sources that Instant uses on the NTP.
86 #if defined(ENABLE_THEMES) 94 #if defined(ENABLE_THEMES)
87 // Listen for theme installation. 95 // Listen for theme installation.
88 registrar_.Add(this, chrome::NOTIFICATION_BROWSER_THEME_CHANGED, 96 registrar_.Add(this, chrome::NOTIFICATION_BROWSER_THEME_CHANGED,
89 content::Source<ThemeService>( 97 content::Source<ThemeService>(
90 ThemeServiceFactory::GetForProfile(profile_))); 98 ThemeServiceFactory::GetForProfile(profile_)));
91 99
92 content::URLDataSource::Add(profile, new ThemeSource(profile)); 100 content::URLDataSource::Add(profile_, new ThemeSource(profile_));
93 #endif // defined(ENABLE_THEMES) 101 #endif // defined(ENABLE_THEMES)
94 102
95 content::URLDataSource::Add(profile, new ThumbnailSource(profile)); 103 content::URLDataSource::Add(profile_, new ThumbnailSource(profile_));
96 content::URLDataSource::Add(profile, new FaviconSource( 104 content::URLDataSource::Add(
97 profile, FaviconSource::FAVICON)); 105 profile_, new FaviconSource(profile_, FaviconSource::FAVICON));
98 content::URLDataSource::Add(profile, new LocalNtpSource(profile)); 106 content::URLDataSource::Add(profile_, new LocalNtpSource(profile_));
99 content::URLDataSource::Add(profile, new MostVisitedIframeSource()); 107 content::URLDataSource::Add(profile_, new MostVisitedIframeSource());
108
109 profile_pref_registrar_.Init(profile_->GetPrefs());
110 profile_pref_registrar_.Add(
111 prefs::kDefaultSearchProviderID,
112 base::Bind(&InstantService::OnDefaultSearchProviderChanged,
113 base::Unretained(this)));
114
115 registrar_.Add(this,
samarth 2013/07/26 23:33:15 Move this up next to the other notification regist
Anuj 2013/07/31 07:16:18 Done.
116 chrome::NOTIFICATION_GOOGLE_URL_UPDATED,
117 content::Source<Profile>(profile_->GetOriginalProfile()));
100 } 118 }
101 119
102 InstantService::~InstantService() { 120 InstantService::~InstantService() {
103 } 121 }
104 122
105 void InstantService::AddInstantProcess(int process_id) { 123 void InstantService::AddInstantProcess(int process_id) {
106 process_ids_.insert(process_id); 124 process_ids_.insert(process_id);
107 125
108 if (instant_io_context_.get()) { 126 if (instant_io_context_.get()) {
109 BrowserThread::PostTask(BrowserThread::IO, 127 BrowserThread::PostTask(BrowserThread::IO,
(...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after
184 return ntp_prerenderer_.GetNTPContents(); 202 return ntp_prerenderer_.GetNTPContents();
185 } 203 }
186 204
187 void InstantService::OnBrowserInstantControllerCreated() { 205 void InstantService::OnBrowserInstantControllerCreated() {
188 if (profile_->IsOffTheRecord()) 206 if (profile_->IsOffTheRecord())
189 return; 207 return;
190 208
191 ++browser_instant_controller_object_count_; 209 ++browser_instant_controller_object_count_;
192 210
193 if (browser_instant_controller_object_count_ == 1) 211 if (browser_instant_controller_object_count_ == 1)
194 ntp_prerenderer_.PreloadInstantNTP(); 212 ntp_prerenderer_.ReloadInstantNTP();
195 } 213 }
196 214
197 void InstantService::OnBrowserInstantControllerDestroyed() { 215 void InstantService::OnBrowserInstantControllerDestroyed() {
198 if (profile_->IsOffTheRecord()) 216 if (profile_->IsOffTheRecord())
199 return; 217 return;
200 218
201 DCHECK_GT(browser_instant_controller_object_count_, 0U); 219 DCHECK_GT(browser_instant_controller_object_count_, 0U);
202 --browser_instant_controller_object_count_; 220 --browser_instant_controller_object_count_;
203 221
204 // All browser windows have closed, so release the InstantNTP resources to 222 // All browser windows have closed, so release the InstantNTP resources to
(...skipping 29 matching lines...) Expand all
234 weak_ptr_factory_.GetWeakPtr())); 252 weak_ptr_factory_.GetWeakPtr()));
235 } 253 }
236 break; 254 break;
237 } 255 }
238 #if defined(ENABLE_THEMES) 256 #if defined(ENABLE_THEMES)
239 case chrome::NOTIFICATION_BROWSER_THEME_CHANGED: { 257 case chrome::NOTIFICATION_BROWSER_THEME_CHANGED: {
240 OnThemeChanged(content::Source<ThemeService>(source).ptr()); 258 OnThemeChanged(content::Source<ThemeService>(source).ptr());
241 break; 259 break;
242 } 260 }
243 #endif // defined(ENABLE_THEMES) 261 #endif // defined(ENABLE_THEMES)
262 case chrome::NOTIFICATION_GOOGLE_URL_UPDATED: {
263 OnGoogleURLUpdated(
264 content::Source<Profile>(source).ptr(),
265 content::Details<GoogleURLTracker::UpdatedDetails>(details).ptr());
266 break;
267 }
244 default: 268 default:
245 NOTREACHED() << "Unexpected notification type in InstantService."; 269 NOTREACHED() << "Unexpected notification type in InstantService.";
246 } 270 }
247 } 271 }
248 272
249 void InstantService::OnMostVisitedItemsReceived( 273 void InstantService::OnMostVisitedItemsReceived(
250 const history::MostVisitedURLList& data) { 274 const history::MostVisitedURLList& data) {
251 history::MostVisitedURLList reordered_data(data); 275 history::MostVisitedURLList reordered_data(data);
252 history::TopSites::MaybeShuffle(&reordered_data); 276 history::TopSites::MaybeShuffle(&reordered_data);
253 277
(...skipping 121 matching lines...) Expand 10 before | Expand all | Expand 10 after
375 theme_info_->image_height = image->height(); 399 theme_info_->image_height = image->height();
376 400
377 theme_info_->has_attribution = 401 theme_info_->has_attribution =
378 theme_service->HasCustomImage(IDR_THEME_NTP_ATTRIBUTION); 402 theme_service->HasCustomImage(IDR_THEME_NTP_ATTRIBUTION);
379 } 403 }
380 404
381 FOR_EACH_OBSERVER(InstantServiceObserver, observers_, 405 FOR_EACH_OBSERVER(InstantServiceObserver, observers_,
382 ThemeInfoChanged(*theme_info_)); 406 ThemeInfoChanged(*theme_info_));
383 } 407 }
384 408
409 void InstantService::OnGoogleURLUpdated(
410 Profile* profile,
411 GoogleURLTracker::UpdatedDetails* details) {
412 GURL last_prompted_url(
413 profile->GetPrefs()->GetString(prefs::kLastPromptedGoogleURL));
414
415 // See GoogleURLTracker::OnURLFetchComplete.
416
417 // last_prompted_url.is_empty indicated very first run of Chrome. So there is
418 // no need to temper with instant resources.
samarth 2013/07/26 23:33:15 s/temper/tamper/ s/instant/Instant/
Anuj 2013/07/31 07:16:18 Done.
419 if (last_prompted_url.is_empty())
420 return;
421
422 // Google URL change is just the scheme change (http <-> https).
samarth 2013/07/26 23:33:15 This matters. Instant Extended should only happen
Anuj 2013/07/31 07:16:18 As discussed and commented above, this imitates th
423 if (net::StripWWWFromHost(details->first) ==
424 net::StripWWWFromHost(details->second))
425 return;
426
427 FOR_EACH_OBSERVER(InstantServiceObserver, observers_, GoogleURLUpdated());
428 }
429
430 void InstantService::OnDefaultSearchProviderChanged(
431 const std::string& pref_name) {
432 DCHECK_EQ(pref_name, std::string(prefs::kDefaultSearchProviderID));
433 const TemplateURL* template_url = TemplateURLServiceFactory::GetForProfile(
434 profile_)->GetDefaultSearchProvider();
435 if (!template_url) {
436 // A NULL |template_url| could mean either this notification is sent during
437 // the browser start up operation or the user now has no default search
438 // provider. There is no way for the user to reach this state using the
439 // Chrome settings. Only explicitly poking at the DB or bugs in the Sync
440 // could cause that, neither of which we support.
441 return;
442 }
443 FOR_EACH_OBSERVER(
444 InstantServiceObserver, observers_, DefaultSearchProviderChanged());
445 }
446
385 InstantNTPPrerenderer* InstantService::ntp_prerenderer() { 447 InstantNTPPrerenderer* InstantService::ntp_prerenderer() {
386 return &ntp_prerenderer_; 448 return &ntp_prerenderer_;
387 } 449 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698