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

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

Issue 273153004: Revert of Handle TemplateURLService load failure better, and make some test correctness fixes that will be ne… (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 6 years, 7 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 | Annotate | Revision Log
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>
8
9 #include "base/logging.h"
10 #include "base/prefs/pref_service.h"
11 #include "base/strings/string_number_conversions.h"
7 #include "chrome/browser/chrome_notification_types.h" 12 #include "chrome/browser/chrome_notification_types.h"
13 #include "chrome/browser/history/history_notifications.h"
8 #include "chrome/browser/history/most_visited_tiles_experiment.h" 14 #include "chrome/browser/history/most_visited_tiles_experiment.h"
9 #include "chrome/browser/history/top_sites.h" 15 #include "chrome/browser/history/top_sites.h"
10 #include "chrome/browser/profiles/profile.h" 16 #include "chrome/browser/profiles/profile.h"
11 #include "chrome/browser/search/instant_io_context.h" 17 #include "chrome/browser/search/instant_io_context.h"
18 #include "chrome/browser/search/instant_service_factory.h"
12 #include "chrome/browser/search/instant_service_observer.h" 19 #include "chrome/browser/search/instant_service_observer.h"
13 #include "chrome/browser/search/local_ntp_source.h" 20 #include "chrome/browser/search/local_ntp_source.h"
14 #include "chrome/browser/search/most_visited_iframe_source.h" 21 #include "chrome/browser/search/most_visited_iframe_source.h"
15 #include "chrome/browser/search/search.h" 22 #include "chrome/browser/search/search.h"
16 #include "chrome/browser/search/suggestions/suggestions_source.h" 23 #include "chrome/browser/search/suggestions/suggestions_source.h"
17 #include "chrome/browser/search_engines/search_terms_data.h" 24 #include "chrome/browser/search_engines/template_url.h"
18 #include "chrome/browser/search_engines/template_url_service.h" 25 #include "chrome/browser/search_engines/template_url_service.h"
19 #include "chrome/browser/search_engines/template_url_service_factory.h" 26 #include "chrome/browser/search_engines/template_url_service_factory.h"
20 #include "chrome/browser/themes/theme_properties.h" 27 #include "chrome/browser/themes/theme_properties.h"
21 #include "chrome/browser/themes/theme_service.h" 28 #include "chrome/browser/themes/theme_service.h"
22 #include "chrome/browser/themes/theme_service_factory.h" 29 #include "chrome/browser/themes/theme_service_factory.h"
23 #include "chrome/browser/thumbnails/thumbnail_list_source.h" 30 #include "chrome/browser/thumbnails/thumbnail_list_source.h"
24 #include "chrome/browser/ui/search/instant_search_prerenderer.h"
25 #include "chrome/browser/ui/webui/favicon_source.h" 31 #include "chrome/browser/ui/webui/favicon_source.h"
26 #include "chrome/browser/ui/webui/ntp/thumbnail_source.h" 32 #include "chrome/browser/ui/webui/ntp/thumbnail_source.h"
27 #include "chrome/browser/ui/webui/theme_source.h" 33 #include "chrome/browser/ui/webui/theme_source.h"
34 #include "chrome/common/pref_names.h"
28 #include "chrome/common/render_messages.h" 35 #include "chrome/common/render_messages.h"
29 #include "content/public/browser/browser_thread.h" 36 #include "content/public/browser/browser_thread.h"
37 #include "content/public/browser/notification_details.h"
30 #include "content/public/browser/notification_service.h" 38 #include "content/public/browser/notification_service.h"
39 #include "content/public/browser/notification_source.h"
31 #include "content/public/browser/notification_types.h" 40 #include "content/public/browser/notification_types.h"
32 #include "content/public/browser/render_process_host.h" 41 #include "content/public/browser/render_process_host.h"
33 #include "content/public/browser/url_data_source.h" 42 #include "content/public/browser/url_data_source.h"
34 #include "grit/theme_resources.h" 43 #include "grit/theme_resources.h"
35 #include "third_party/skia/include/core/SkColor.h" 44 #include "net/base/net_util.h"
45 #include "net/url_request/url_request.h"
36 #include "ui/gfx/color_utils.h" 46 #include "ui/gfx/color_utils.h"
37 #include "ui/gfx/image/image_skia.h" 47 #include "ui/gfx/image/image_skia.h"
38 #include "ui/gfx/sys_color_change_listener.h" 48 #include "ui/gfx/sys_color_change_listener.h"
49 #include "url/gurl.h"
39 50
51 using content::BrowserThread;
40 52
41 namespace { 53 namespace {
42 54
43 const int kSectionBorderAlphaTransparency = 80; 55 const int kSectionBorderAlphaTransparency = 80;
44 56
45 // Converts SkColor to RGBAColor 57 // Converts SkColor to RGBAColor
46 RGBAColor SkColorToRGBAColor(const SkColor& sKColor) { 58 RGBAColor SkColorToRGBAColor(const SkColor& sKColor) {
47 RGBAColor color; 59 RGBAColor color;
48 color.r = SkColorGetR(sKColor); 60 color.r = SkColorGetR(sKColor);
49 color.g = SkColorGetG(sKColor); 61 color.g = SkColorGetG(sKColor);
50 color.b = SkColorGetB(sKColor); 62 color.b = SkColorGetB(sKColor);
51 color.a = SkColorGetA(sKColor); 63 color.a = SkColorGetA(sKColor);
52 return color; 64 return color;
53 } 65 }
54 66
55 } // namespace 67 } // namespace
56 68
57 InstantService::InstantService(Profile* profile) 69 InstantService::InstantService(Profile* profile)
58 : profile_(profile), 70 : profile_(profile),
59 template_url_service_(TemplateURLServiceFactory::GetForProfile(profile_)),
60 omnibox_start_margin_(chrome::kDisableStartMargin), 71 omnibox_start_margin_(chrome::kDisableStartMargin),
61 weak_ptr_factory_(this) { 72 weak_ptr_factory_(this) {
62 // The initialization below depends on a typical set of browser threads. Skip 73 // Stub for unit tests.
63 // it if we are running in a unit test without the full suite. 74 if (!BrowserThread::CurrentlyOn(BrowserThread::UI))
64 if (!content::BrowserThread::CurrentlyOn(content::BrowserThread::UI))
65 return; 75 return;
66 76
67 // This depends on the existence of the typical browser threads. Therefore it
68 // is only instantiated here (after the check for a UI thread above).
69 instant_io_context_ = new InstantIOContext();
70
71 previous_google_base_url_ =
72 GURL(UIThreadSearchTermsData(profile).GoogleBaseURLValue());
73
74 // TemplateURLService is NULL by default in tests.
75 if (template_url_service_) {
76 template_url_service_->AddObserver(this);
77 const TemplateURL* default_search_provider =
78 template_url_service_->GetDefaultSearchProvider();
79 if (default_search_provider) {
80 previous_default_search_provider_.reset(
81 new TemplateURLData(default_search_provider->data()));
82 }
83 }
84
85 ResetInstantSearchPrerenderer(); 77 ResetInstantSearchPrerenderer();
86 78
87 registrar_.Add(this, 79 registrar_.Add(this,
88 content::NOTIFICATION_RENDERER_PROCESS_CREATED, 80 content::NOTIFICATION_RENDERER_PROCESS_CREATED,
89 content::NotificationService::AllSources()); 81 content::NotificationService::AllSources());
90 registrar_.Add(this, 82 registrar_.Add(this,
91 content::NOTIFICATION_RENDERER_PROCESS_TERMINATED, 83 content::NOTIFICATION_RENDERER_PROCESS_TERMINATED,
92 content::NotificationService::AllSources()); 84 content::NotificationService::AllSources());
93 85
94 history::TopSites* top_sites = profile_->GetTopSites(); 86 history::TopSites* top_sites = profile_->GetTopSites();
95 if (top_sites) { 87 if (top_sites) {
96 registrar_.Add(this, 88 registrar_.Add(this,
97 chrome::NOTIFICATION_TOP_SITES_CHANGED, 89 chrome::NOTIFICATION_TOP_SITES_CHANGED,
98 content::Source<history::TopSites>(top_sites)); 90 content::Source<history::TopSites>(top_sites));
99 } 91 }
92 instant_io_context_ = new InstantIOContext();
100 93
101 if (profile_ && profile_->GetResourceContext()) { 94 if (profile_ && profile_->GetResourceContext()) {
102 content::BrowserThread::PostTask( 95 BrowserThread::PostTask(
103 content::BrowserThread::IO, FROM_HERE, 96 BrowserThread::IO, FROM_HERE,
104 base::Bind(&InstantIOContext::SetUserDataOnIO, 97 base::Bind(&InstantIOContext::SetUserDataOnIO,
105 profile->GetResourceContext(), instant_io_context_)); 98 profile->GetResourceContext(), instant_io_context_));
106 } 99 }
107 100
108 // Set up the data sources that Instant uses on the NTP. 101 // Set up the data sources that Instant uses on the NTP.
109 #if defined(ENABLE_THEMES) 102 #if defined(ENABLE_THEMES)
110 // Listen for theme installation. 103 // Listen for theme installation.
111 registrar_.Add(this, chrome::NOTIFICATION_BROWSER_THEME_CHANGED, 104 registrar_.Add(this, chrome::NOTIFICATION_BROWSER_THEME_CHANGED,
112 content::Source<ThemeService>( 105 content::Source<ThemeService>(
113 ThemeServiceFactory::GetForProfile(profile_))); 106 ThemeServiceFactory::GetForProfile(profile_)));
114 107
115 content::URLDataSource::Add(profile_, new ThemeSource(profile_)); 108 content::URLDataSource::Add(profile_, new ThemeSource(profile_));
116 #endif // defined(ENABLE_THEMES) 109 #endif // defined(ENABLE_THEMES)
117 110
118 // TODO(aurimas) remove this #if once instant_service.cc is no longer compiled 111 // TODO(aurimas) remove this #if once instant_service.cc is no longer compiled
119 // on Android. 112 // on Android.
120 #if !defined(OS_ANDROID) 113 #if !defined(OS_ANDROID)
121 content::URLDataSource::Add(profile_, new ThumbnailSource(profile_, false)); 114 content::URLDataSource::Add(profile_, new ThumbnailSource(profile_, false));
122 content::URLDataSource::Add(profile_, new ThumbnailSource(profile_, true)); 115 content::URLDataSource::Add(profile_, new ThumbnailSource(profile_, true));
123 content::URLDataSource::Add(profile_, new ThumbnailListSource(profile_)); 116 content::URLDataSource::Add(profile_, new ThumbnailListSource(profile_));
124 #endif // !defined(OS_ANDROID) 117 #endif // !defined(OS_ANDROID)
125 118
126 content::URLDataSource::Add( 119 content::URLDataSource::Add(
127 profile_, new FaviconSource(profile_, FaviconSource::FAVICON)); 120 profile_, new FaviconSource(profile_, FaviconSource::FAVICON));
128 content::URLDataSource::Add(profile_, new LocalNtpSource(profile_)); 121 content::URLDataSource::Add(profile_, new LocalNtpSource(profile_));
129 content::URLDataSource::Add(profile_, new MostVisitedIframeSource()); 122 content::URLDataSource::Add(profile_, new MostVisitedIframeSource());
130 content::URLDataSource::Add( 123 content::URLDataSource::Add(
131 profile_, new suggestions::SuggestionsSource(profile_)); 124 profile_, new suggestions::SuggestionsSource(profile_));
125
126 profile_pref_registrar_.Init(profile_->GetPrefs());
127 profile_pref_registrar_.Add(
128 prefs::kDefaultSearchProviderID,
129 base::Bind(&InstantService::OnDefaultSearchProviderChanged,
130 base::Unretained(this)));
131
132 registrar_.Add(this, chrome::NOTIFICATION_GOOGLE_URL_UPDATED,
133 content::Source<Profile>(profile_->GetOriginalProfile()));
132 } 134 }
133 135
134 InstantService::~InstantService() { 136 InstantService::~InstantService() {
135 if (template_url_service_)
136 template_url_service_->RemoveObserver(this);
137 } 137 }
138 138
139 void InstantService::AddInstantProcess(int process_id) { 139 void InstantService::AddInstantProcess(int process_id) {
140 process_ids_.insert(process_id); 140 process_ids_.insert(process_id);
141 141
142 if (instant_io_context_.get()) { 142 if (instant_io_context_.get()) {
143 content::BrowserThread::PostTask( 143 BrowserThread::PostTask(BrowserThread::IO,
144 content::BrowserThread::IO, FROM_HERE, 144 FROM_HERE,
145 base::Bind(&InstantIOContext::AddInstantProcessOnIO, 145 base::Bind(&InstantIOContext::AddInstantProcessOnIO,
146 instant_io_context_, process_id)); 146 instant_io_context_,
147 process_id));
147 } 148 }
148 } 149 }
149 150
150 bool InstantService::IsInstantProcess(int process_id) const { 151 bool InstantService::IsInstantProcess(int process_id) const {
151 return process_ids_.find(process_id) != process_ids_.end(); 152 return process_ids_.find(process_id) != process_ids_.end();
152 } 153 }
153 154
154 void InstantService::AddObserver(InstantServiceObserver* observer) { 155 void InstantService::AddObserver(InstantServiceObserver* observer) {
155 observers_.AddObserver(observer); 156 observers_.AddObserver(observer);
156 } 157 }
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after
193 } 194 }
194 195
195 void InstantService::UpdateMostVisitedItemsInfo() { 196 void InstantService::UpdateMostVisitedItemsInfo() {
196 NotifyAboutMostVisitedItems(); 197 NotifyAboutMostVisitedItems();
197 } 198 }
198 199
199 void InstantService::Shutdown() { 200 void InstantService::Shutdown() {
200 process_ids_.clear(); 201 process_ids_.clear();
201 202
202 if (instant_io_context_.get()) { 203 if (instant_io_context_.get()) {
203 content::BrowserThread::PostTask( 204 BrowserThread::PostTask(
204 content::BrowserThread::IO, FROM_HERE, 205 BrowserThread::IO,
206 FROM_HERE,
205 base::Bind(&InstantIOContext::ClearInstantProcessesOnIO, 207 base::Bind(&InstantIOContext::ClearInstantProcessesOnIO,
206 instant_io_context_)); 208 instant_io_context_));
207 } 209 }
208 instant_io_context_ = NULL; 210 instant_io_context_ = NULL;
209 } 211 }
210 212
211 void InstantService::Observe(int type, 213 void InstantService::Observe(int type,
212 const content::NotificationSource& source, 214 const content::NotificationSource& source,
213 const content::NotificationDetails& details) { 215 const content::NotificationDetails& details) {
214 switch (type) { 216 switch (type) {
(...skipping 13 matching lines...) Expand all
228 weak_ptr_factory_.GetWeakPtr()), false); 230 weak_ptr_factory_.GetWeakPtr()), false);
229 } 231 }
230 break; 232 break;
231 } 233 }
232 #if defined(ENABLE_THEMES) 234 #if defined(ENABLE_THEMES)
233 case chrome::NOTIFICATION_BROWSER_THEME_CHANGED: { 235 case chrome::NOTIFICATION_BROWSER_THEME_CHANGED: {
234 OnThemeChanged(content::Source<ThemeService>(source).ptr()); 236 OnThemeChanged(content::Source<ThemeService>(source).ptr());
235 break; 237 break;
236 } 238 }
237 #endif // defined(ENABLE_THEMES) 239 #endif // defined(ENABLE_THEMES)
240 case chrome::NOTIFICATION_GOOGLE_URL_UPDATED: {
241 OnGoogleURLUpdated(
242 content::Source<Profile>(source).ptr(),
243 content::Details<GoogleURLTracker::UpdatedDetails>(details).ptr());
244 break;
245 }
238 default: 246 default:
239 NOTREACHED() << "Unexpected notification type in InstantService."; 247 NOTREACHED() << "Unexpected notification type in InstantService.";
240 } 248 }
241 } 249 }
242 250
243 void InstantService::SendSearchURLsToRenderer(content::RenderProcessHost* rph) { 251 void InstantService::SendSearchURLsToRenderer(content::RenderProcessHost* rph) {
244 rph->Send(new ChromeViewMsg_SetSearchURLs( 252 rph->Send(new ChromeViewMsg_SetSearchURLs(
245 chrome::GetSearchURLs(profile_), chrome::GetNewTabPageURL(profile_))); 253 chrome::GetSearchURLs(profile_), chrome::GetNewTabPageURL(profile_)));
246 } 254 }
247 255
248 void InstantService::OnOmniboxStartMarginChanged(int start_margin) { 256 void InstantService::OnOmniboxStartMarginChanged(int start_margin) {
249 omnibox_start_margin_ = start_margin; 257 omnibox_start_margin_ = start_margin;
250 FOR_EACH_OBSERVER(InstantServiceObserver, observers_, 258 FOR_EACH_OBSERVER(InstantServiceObserver, observers_,
251 OmniboxStartMarginChanged(omnibox_start_margin_)); 259 OmniboxStartMarginChanged(omnibox_start_margin_));
252 } 260 }
253 261
254 void InstantService::OnRendererProcessTerminated(int process_id) { 262 void InstantService::OnRendererProcessTerminated(int process_id) {
255 process_ids_.erase(process_id); 263 process_ids_.erase(process_id);
256 264
257 if (instant_io_context_.get()) { 265 if (instant_io_context_.get()) {
258 content::BrowserThread::PostTask( 266 BrowserThread::PostTask(
259 content::BrowserThread::IO, FROM_HERE, 267 BrowserThread::IO,
268 FROM_HERE,
260 base::Bind(&InstantIOContext::RemoveInstantProcessOnIO, 269 base::Bind(&InstantIOContext::RemoveInstantProcessOnIO,
261 instant_io_context_, process_id)); 270 instant_io_context_,
271 process_id));
262 } 272 }
263 } 273 }
264 274
265 void InstantService::OnMostVisitedItemsReceived( 275 void InstantService::OnMostVisitedItemsReceived(
266 const history::MostVisitedURLList& data) { 276 const history::MostVisitedURLList& data) {
267 history::MostVisitedURLList reordered_data(data); 277 history::MostVisitedURLList reordered_data(data);
268 history::MostVisitedTilesExperiment::MaybeShuffle(&reordered_data); 278 history::MostVisitedTilesExperiment::MaybeShuffle(&reordered_data);
269 279
270 std::vector<InstantMostVisitedItem> new_most_visited_items; 280 std::vector<InstantMostVisitedItem> new_most_visited_items;
271 for (size_t i = 0; i < reordered_data.size(); i++) { 281 for (size_t i = 0; i < reordered_data.size(); i++) {
(...skipping 114 matching lines...) Expand 10 before | Expand all | Expand 10 after
386 theme_info_->image_height = image->height(); 396 theme_info_->image_height = image->height();
387 397
388 theme_info_->has_attribution = 398 theme_info_->has_attribution =
389 theme_service->HasCustomImage(IDR_THEME_NTP_ATTRIBUTION); 399 theme_service->HasCustomImage(IDR_THEME_NTP_ATTRIBUTION);
390 } 400 }
391 401
392 FOR_EACH_OBSERVER(InstantServiceObserver, observers_, 402 FOR_EACH_OBSERVER(InstantServiceObserver, observers_,
393 ThemeInfoChanged(*theme_info_)); 403 ThemeInfoChanged(*theme_info_));
394 } 404 }
395 405
396 void InstantService::OnTemplateURLServiceChanged() { 406 void InstantService::OnGoogleURLUpdated(
397 // Check whether the default search provider was changed. 407 Profile* profile,
398 const TemplateURL* template_url = 408 GoogleURLTracker::UpdatedDetails* details) {
399 template_url_service_->GetDefaultSearchProvider(); 409 GURL last_prompted_url(
400 bool default_search_provider_changed = !TemplateURL::MatchesData( 410 profile->GetPrefs()->GetString(prefs::kLastPromptedGoogleURL));
401 template_url, previous_default_search_provider_.get()); 411
402 if (default_search_provider_changed) { 412 // See GoogleURLTracker::OnURLFetchComplete().
403 previous_default_search_provider_.reset( 413 // last_prompted_url.is_empty() indicates very first run of Chrome. So there
404 template_url ? new TemplateURLData(template_url->data()) : NULL); 414 // is no need to notify, as there won't be any old state.
415 if (last_prompted_url.is_empty())
416 return;
417
418 ResetInstantSearchPrerenderer();
419
420 // Only the scheme changed. Ignore it since we do not prompt the user in this
421 // case.
422 if (net::StripWWWFromHost(details->first) ==
423 net::StripWWWFromHost(details->second))
424 return;
425
426 FOR_EACH_OBSERVER(InstantServiceObserver, observers_, GoogleURLUpdated());
427 }
428
429 void InstantService::OnDefaultSearchProviderChanged(
430 const std::string& pref_name) {
431 DCHECK_EQ(pref_name, std::string(prefs::kDefaultSearchProviderID));
432 const TemplateURL* template_url = TemplateURLServiceFactory::GetForProfile(
433 profile_)->GetDefaultSearchProvider();
434 if (!template_url) {
435 // A NULL |template_url| could mean either this notification is sent during
436 // the browser start up operation or the user now has no default search
437 // provider. There is no way for the user to reach this state using the
438 // Chrome settings. Only explicitly poking at the DB or bugs in the Sync
439 // could cause that, neither of which we support.
440 return;
405 } 441 }
406 442
407 // Note that, even if the TemplateURL for the Default Search Provider has not 443 ResetInstantSearchPrerenderer();
408 // changed, the effective URLs might change if they reference the Google base
409 // URL. The TemplateURLService will notify us when the effective URL changes
410 // in this way but it's up to us to do the work to check both.
411 GURL google_base_url(UIThreadSearchTermsData(profile_).GoogleBaseURLValue());
412 if (google_base_url != previous_google_base_url_) {
413 previous_google_base_url_ = google_base_url;
414 if (template_url && template_url->HasGoogleBaseURLs())
415 default_search_provider_changed = true;
416 }
417 444
418 if (default_search_provider_changed) { 445 FOR_EACH_OBSERVER(
419 ResetInstantSearchPrerenderer(); 446 InstantServiceObserver, observers_, DefaultSearchProviderChanged());
420 FOR_EACH_OBSERVER(InstantServiceObserver, observers_,
421 DefaultSearchProviderChanged());
422 }
423 } 447 }
424 448
425 void InstantService::ResetInstantSearchPrerenderer() { 449 void InstantService::ResetInstantSearchPrerenderer() {
426 if (!chrome::ShouldPrefetchSearchResults()) 450 if (!chrome::ShouldPrefetchSearchResults())
427 return; 451 return;
428 452
429 GURL url(chrome::GetSearchResultPrefetchBaseURL(profile_)); 453 GURL url(chrome::GetSearchResultPrefetchBaseURL(profile_));
430 instant_prerenderer_.reset( 454 if (url.is_valid())
431 url.is_valid() ? new InstantSearchPrerenderer(profile_, url) : NULL); 455 instant_prerenderer_.reset(new InstantSearchPrerenderer(profile_, url));
456 else
457 instant_prerenderer_.reset();
432 } 458 }
OLDNEW
« no previous file with comments | « chrome/browser/search/instant_service.h ('k') | chrome/browser/search/instant_service_observer.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698