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

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: Added ui and unit tests Created 7 years, 4 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/most_visited_tiles_experiment.h" 14 #include "chrome/browser/history/most_visited_tiles_experiment.h"
14 #include "chrome/browser/history/top_sites.h" 15 #include "chrome/browser/history/top_sites.h"
15 #include "chrome/browser/profiles/profile.h" 16 #include "chrome/browser/profiles/profile.h"
16 #include "chrome/browser/search/instant_io_context.h" 17 #include "chrome/browser/search/instant_io_context.h"
17 #include "chrome/browser/search/instant_service_factory.h" 18 #include "chrome/browser/search/instant_service_factory.h"
18 #include "chrome/browser/search/instant_service_observer.h" 19 #include "chrome/browser/search/instant_service_observer.h"
19 #include "chrome/browser/search/local_ntp_source.h" 20 #include "chrome/browser/search/local_ntp_source.h"
20 #include "chrome/browser/search/most_visited_iframe_source.h" 21 #include "chrome/browser/search/most_visited_iframe_source.h"
21 #include "chrome/browser/search/search.h" 22 #include "chrome/browser/search/search.h"
23 #include "chrome/browser/search_engines/template_url.h"
24 #include "chrome/browser/search_engines/template_url_service.h"
25 #include "chrome/browser/search_engines/template_url_service_factory.h"
22 #include "chrome/browser/themes/theme_properties.h" 26 #include "chrome/browser/themes/theme_properties.h"
23 #include "chrome/browser/themes/theme_service.h" 27 #include "chrome/browser/themes/theme_service.h"
24 #include "chrome/browser/themes/theme_service_factory.h" 28 #include "chrome/browser/themes/theme_service_factory.h"
25 #include "chrome/browser/ui/webui/favicon_source.h" 29 #include "chrome/browser/ui/webui/favicon_source.h"
26 #include "chrome/browser/ui/webui/ntp/thumbnail_source.h" 30 #include "chrome/browser/ui/webui/ntp/thumbnail_source.h"
27 #include "chrome/browser/ui/webui/theme_source.h" 31 #include "chrome/browser/ui/webui/theme_source.h"
32 #include "chrome/common/pref_names.h"
28 #include "content/public/browser/browser_thread.h" 33 #include "content/public/browser/browser_thread.h"
34 #include "content/public/browser/notification_details.h"
29 #include "content/public/browser/notification_service.h" 35 #include "content/public/browser/notification_service.h"
36 #include "content/public/browser/notification_source.h"
30 #include "content/public/browser/notification_types.h" 37 #include "content/public/browser/notification_types.h"
31 #include "content/public/browser/render_process_host.h" 38 #include "content/public/browser/render_process_host.h"
32 #include "content/public/browser/url_data_source.h" 39 #include "content/public/browser/url_data_source.h"
33 #include "grit/theme_resources.h" 40 #include "grit/theme_resources.h"
41 #include "net/base/net_util.h"
34 #include "net/url_request/url_request.h" 42 #include "net/url_request/url_request.h"
35 #include "ui/gfx/color_utils.h" 43 #include "ui/gfx/color_utils.h"
36 #include "ui/gfx/image/image_skia.h" 44 #include "ui/gfx/image/image_skia.h"
37 #include "ui/gfx/sys_color_change_listener.h" 45 #include "ui/gfx/sys_color_change_listener.h"
38 #include "url/gurl.h" 46 #include "url/gurl.h"
39 47
40 using content::BrowserThread; 48 using content::BrowserThread;
41 49
42 namespace { 50 namespace {
43 51
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after
83 profile->GetResourceContext(), instant_io_context_)); 91 profile->GetResourceContext(), instant_io_context_));
84 } 92 }
85 93
86 // Set up the data sources that Instant uses on the NTP. 94 // Set up the data sources that Instant uses on the NTP.
87 #if defined(ENABLE_THEMES) 95 #if defined(ENABLE_THEMES)
88 // Listen for theme installation. 96 // Listen for theme installation.
89 registrar_.Add(this, chrome::NOTIFICATION_BROWSER_THEME_CHANGED, 97 registrar_.Add(this, chrome::NOTIFICATION_BROWSER_THEME_CHANGED,
90 content::Source<ThemeService>( 98 content::Source<ThemeService>(
91 ThemeServiceFactory::GetForProfile(profile_))); 99 ThemeServiceFactory::GetForProfile(profile_)));
92 100
93 content::URLDataSource::Add(profile, new ThemeSource(profile)); 101 content::URLDataSource::Add(profile_, new ThemeSource(profile_));
94 #endif // defined(ENABLE_THEMES) 102 #endif // defined(ENABLE_THEMES)
95 103
96 content::URLDataSource::Add(profile, new ThumbnailSource(profile)); 104 registrar_.Add(this,
97 content::URLDataSource::Add(profile, new FaviconSource( 105 chrome::NOTIFICATION_GOOGLE_URL_UPDATED,
98 profile, FaviconSource::FAVICON)); 106 content::Source<Profile>(profile_->GetOriginalProfile()));
99 content::URLDataSource::Add(profile, new LocalNtpSource(profile)); 107
100 content::URLDataSource::Add(profile, new MostVisitedIframeSource()); 108 content::URLDataSource::Add(profile_, new ThumbnailSource(profile_));
109 content::URLDataSource::Add(
110 profile_, new FaviconSource(profile_, FaviconSource::FAVICON));
111 content::URLDataSource::Add(profile_, new LocalNtpSource(profile_));
112 content::URLDataSource::Add(profile_, new MostVisitedIframeSource());
113
114 profile_pref_registrar_.Init(profile_->GetPrefs());
115 profile_pref_registrar_.Add(
116 prefs::kDefaultSearchProviderID,
117 base::Bind(&InstantService::OnDefaultSearchProviderChanged,
118 base::Unretained(this)));
119
120 AddObserver(ntp_prerenderer());
samarth 2013/08/02 21:51:15 Objects should add themselves as observers rather
Anuj 2013/08/09 07:22:00 Done.
101 } 121 }
102 122
103 InstantService::~InstantService() { 123 InstantService::~InstantService() {
104 } 124 }
105 125
106 void InstantService::AddInstantProcess(int process_id) { 126 void InstantService::AddInstantProcess(int process_id) {
107 process_ids_.insert(process_id); 127 process_ids_.insert(process_id);
108 128
109 if (instant_io_context_.get()) { 129 if (instant_io_context_.get()) {
110 BrowserThread::PostTask(BrowserThread::IO, 130 BrowserThread::PostTask(BrowserThread::IO,
(...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after
185 return ntp_prerenderer_.GetNTPContents(); 205 return ntp_prerenderer_.GetNTPContents();
186 } 206 }
187 207
188 void InstantService::OnBrowserInstantControllerCreated() { 208 void InstantService::OnBrowserInstantControllerCreated() {
189 if (profile_->IsOffTheRecord()) 209 if (profile_->IsOffTheRecord())
190 return; 210 return;
191 211
192 ++browser_instant_controller_object_count_; 212 ++browser_instant_controller_object_count_;
193 213
194 if (browser_instant_controller_object_count_ == 1) 214 if (browser_instant_controller_object_count_ == 1)
195 ntp_prerenderer_.PreloadInstantNTP(); 215 ntp_prerenderer_.ReloadInstantNTP();
196 } 216 }
197 217
198 void InstantService::OnBrowserInstantControllerDestroyed() { 218 void InstantService::OnBrowserInstantControllerDestroyed() {
199 if (profile_->IsOffTheRecord()) 219 if (profile_->IsOffTheRecord())
200 return; 220 return;
201 221
202 DCHECK_GT(browser_instant_controller_object_count_, 0U); 222 DCHECK_GT(browser_instant_controller_object_count_, 0U);
203 --browser_instant_controller_object_count_; 223 --browser_instant_controller_object_count_;
204 224
205 // All browser windows have closed, so release the InstantNTP resources to 225 // All browser windows have closed, so release the InstantNTP resources to
(...skipping 29 matching lines...) Expand all
235 weak_ptr_factory_.GetWeakPtr())); 255 weak_ptr_factory_.GetWeakPtr()));
236 } 256 }
237 break; 257 break;
238 } 258 }
239 #if defined(ENABLE_THEMES) 259 #if defined(ENABLE_THEMES)
240 case chrome::NOTIFICATION_BROWSER_THEME_CHANGED: { 260 case chrome::NOTIFICATION_BROWSER_THEME_CHANGED: {
241 OnThemeChanged(content::Source<ThemeService>(source).ptr()); 261 OnThemeChanged(content::Source<ThemeService>(source).ptr());
242 break; 262 break;
243 } 263 }
244 #endif // defined(ENABLE_THEMES) 264 #endif // defined(ENABLE_THEMES)
265 case chrome::NOTIFICATION_GOOGLE_URL_UPDATED: {
266 OnGoogleURLUpdated(
267 content::Source<Profile>(source).ptr(),
268 content::Details<GoogleURLTracker::UpdatedDetails>(details).ptr());
269 break;
270 }
245 default: 271 default:
246 NOTREACHED() << "Unexpected notification type in InstantService."; 272 NOTREACHED() << "Unexpected notification type in InstantService.";
247 } 273 }
248 } 274 }
249 275
250 void InstantService::OnMostVisitedItemsReceived( 276 void InstantService::OnMostVisitedItemsReceived(
251 const history::MostVisitedURLList& data) { 277 const history::MostVisitedURLList& data) {
252 history::MostVisitedURLList reordered_data(data); 278 history::MostVisitedURLList reordered_data(data);
253 history::MostVisitedTilesExperiment::MaybeShuffle(&reordered_data); 279 history::MostVisitedTilesExperiment::MaybeShuffle(&reordered_data);
254 280
(...skipping 121 matching lines...) Expand 10 before | Expand all | Expand 10 after
376 theme_info_->image_height = image->height(); 402 theme_info_->image_height = image->height();
377 403
378 theme_info_->has_attribution = 404 theme_info_->has_attribution =
379 theme_service->HasCustomImage(IDR_THEME_NTP_ATTRIBUTION); 405 theme_service->HasCustomImage(IDR_THEME_NTP_ATTRIBUTION);
380 } 406 }
381 407
382 FOR_EACH_OBSERVER(InstantServiceObserver, observers_, 408 FOR_EACH_OBSERVER(InstantServiceObserver, observers_,
383 ThemeInfoChanged(*theme_info_)); 409 ThemeInfoChanged(*theme_info_));
384 } 410 }
385 411
412 void InstantService::OnGoogleURLUpdated(
413 Profile* profile,
414 GoogleURLTracker::UpdatedDetails* details) {
415 GURL last_prompted_url(
416 profile->GetPrefs()->GetString(prefs::kLastPromptedGoogleURL));
417
418 // See GoogleURLTracker::OnURLFetchComplete.
419 // last_prompted_url.is_empty indicated very first run of Chrome. So there is
420 // no need to tamper with Instant resources.
421 if (last_prompted_url.is_empty())
422 return;
423
424 // Google URL change is just the scheme change (http <-> https). Since this
samarth 2013/08/02 21:51:15 nit: How about: Only the scheme changed. Ignore it
Anuj 2013/08/09 07:22:00 Done.
425 // does not trigger yellow infobar prompting the search url change, the ntp
426 // url refresh is also not triggered.
427 if (net::StripWWWFromHost(details->first) ==
428 net::StripWWWFromHost(details->second))
429 return;
430
431 FOR_EACH_OBSERVER(InstantServiceObserver, observers_, GoogleURLUpdated());
432 }
433
434 void InstantService::OnDefaultSearchProviderChanged(
435 const std::string& pref_name) {
436 DCHECK_EQ(pref_name, std::string(prefs::kDefaultSearchProviderID));
437 const TemplateURL* template_url = TemplateURLServiceFactory::GetForProfile(
438 profile_)->GetDefaultSearchProvider();
439 if (!template_url) {
440 // A NULL |template_url| could mean either this notification is sent during
441 // the browser start up operation or the user now has no default search
442 // provider. There is no way for the user to reach this state using the
443 // Chrome settings. Only explicitly poking at the DB or bugs in the Sync
444 // could cause that, neither of which we support.
445 return;
446 }
447 FOR_EACH_OBSERVER(
448 InstantServiceObserver, observers_, DefaultSearchProviderChanged());
449 }
450
386 InstantNTPPrerenderer* InstantService::ntp_prerenderer() { 451 InstantNTPPrerenderer* InstantService::ntp_prerenderer() {
387 return &ntp_prerenderer_; 452 return &ntp_prerenderer_;
388 } 453 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698