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

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

Issue 263823007: Handle TemplateURLService load failure better (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Remove initial default copying 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"
12 #include "chrome/browser/chrome_notification_types.h" 7 #include "chrome/browser/chrome_notification_types.h"
13 #include "chrome/browser/history/history_notifications.h"
14 #include "chrome/browser/history/most_visited_tiles_experiment.h" 8 #include "chrome/browser/history/most_visited_tiles_experiment.h"
15 #include "chrome/browser/history/top_sites.h" 9 #include "chrome/browser/history/top_sites.h"
16 #include "chrome/browser/profiles/profile.h" 10 #include "chrome/browser/profiles/profile.h"
17 #include "chrome/browser/search/instant_io_context.h" 11 #include "chrome/browser/search/instant_io_context.h"
18 #include "chrome/browser/search/instant_service_factory.h"
19 #include "chrome/browser/search/instant_service_observer.h" 12 #include "chrome/browser/search/instant_service_observer.h"
20 #include "chrome/browser/search/local_ntp_source.h" 13 #include "chrome/browser/search/local_ntp_source.h"
21 #include "chrome/browser/search/most_visited_iframe_source.h" 14 #include "chrome/browser/search/most_visited_iframe_source.h"
22 #include "chrome/browser/search/search.h" 15 #include "chrome/browser/search/search.h"
23 #include "chrome/browser/search/suggestions/suggestions_service.h" 16 #include "chrome/browser/search/suggestions/suggestions_service.h"
24 #include "chrome/browser/search/suggestions/suggestions_source.h" 17 #include "chrome/browser/search/suggestions/suggestions_source.h"
25 #include "chrome/browser/search_engines/template_url.h"
26 #include "chrome/browser/search_engines/template_url_service.h" 18 #include "chrome/browser/search_engines/template_url_service.h"
27 #include "chrome/browser/search_engines/template_url_service_factory.h" 19 #include "chrome/browser/search_engines/template_url_service_factory.h"
28 #include "chrome/browser/themes/theme_properties.h" 20 #include "chrome/browser/themes/theme_properties.h"
29 #include "chrome/browser/themes/theme_service.h" 21 #include "chrome/browser/themes/theme_service.h"
30 #include "chrome/browser/themes/theme_service_factory.h" 22 #include "chrome/browser/themes/theme_service_factory.h"
23 #include "chrome/browser/ui/search/instant_search_prerenderer.h"
31 #include "chrome/browser/ui/webui/favicon_source.h" 24 #include "chrome/browser/ui/webui/favicon_source.h"
32 #include "chrome/browser/ui/webui/ntp/thumbnail_list_source.h" 25 #include "chrome/browser/ui/webui/ntp/thumbnail_list_source.h"
33 #include "chrome/browser/ui/webui/ntp/thumbnail_source.h" 26 #include "chrome/browser/ui/webui/ntp/thumbnail_source.h"
34 #include "chrome/browser/ui/webui/theme_source.h" 27 #include "chrome/browser/ui/webui/theme_source.h"
35 #include "chrome/common/pref_names.h"
36 #include "chrome/common/render_messages.h" 28 #include "chrome/common/render_messages.h"
37 #include "content/public/browser/browser_thread.h" 29 #include "content/public/browser/browser_thread.h"
38 #include "content/public/browser/notification_details.h"
39 #include "content/public/browser/notification_service.h" 30 #include "content/public/browser/notification_service.h"
40 #include "content/public/browser/notification_source.h"
41 #include "content/public/browser/notification_types.h" 31 #include "content/public/browser/notification_types.h"
42 #include "content/public/browser/render_process_host.h" 32 #include "content/public/browser/render_process_host.h"
43 #include "content/public/browser/url_data_source.h" 33 #include "content/public/browser/url_data_source.h"
44 #include "grit/theme_resources.h" 34 #include "grit/theme_resources.h"
45 #include "net/base/net_util.h" 35 #include "third_party/skia/include/core/SkColor.h"
46 #include "net/url_request/url_request.h"
47 #include "ui/gfx/color_utils.h" 36 #include "ui/gfx/color_utils.h"
48 #include "ui/gfx/image/image_skia.h" 37 #include "ui/gfx/image/image_skia.h"
49 #include "ui/gfx/sys_color_change_listener.h" 38 #include "ui/gfx/sys_color_change_listener.h"
50 #include "url/gurl.h"
51 39
52 using content::BrowserThread; 40
41 // Helpers --------------------------------------------------------------------
53 42
54 namespace { 43 namespace {
55 44
56 const int kSectionBorderAlphaTransparency = 80; 45 const int kSectionBorderAlphaTransparency = 80;
57 46
58 // Converts SkColor to RGBAColor 47 // Converts SkColor to RGBAColor
59 RGBAColor SkColorToRGBAColor(const SkColor& sKColor) { 48 RGBAColor SkColorToRGBAColor(const SkColor& sKColor) {
60 RGBAColor color; 49 RGBAColor color;
61 color.r = SkColorGetR(sKColor); 50 color.r = SkColorGetR(sKColor);
62 color.g = SkColorGetG(sKColor); 51 color.g = SkColorGetG(sKColor);
63 color.b = SkColorGetB(sKColor); 52 color.b = SkColorGetB(sKColor);
64 color.a = SkColorGetA(sKColor); 53 color.a = SkColorGetA(sKColor);
65 return color; 54 return color;
66 } 55 }
67 56
68 } // namespace 57 } // namespace
69 58
59
60 // InstantService -------------------------------------------------------------
61
70 InstantService::InstantService(Profile* profile) 62 InstantService::InstantService(Profile* profile)
71 : profile_(profile), 63 : profile_(profile),
72 omnibox_start_margin_(chrome::kDisableStartMargin), 64 omnibox_start_margin_(chrome::kDisableStartMargin),
73 weak_ptr_factory_(this) { 65 weak_ptr_factory_(this),
66 current_search_result_prefetch_base_url_(
67 chrome::GetSearchResultPrefetchBaseURL(profile_)) {
74 // Stub for unit tests. 68 // Stub for unit tests.
75 if (!BrowserThread::CurrentlyOn(BrowserThread::UI)) 69 if (!content::BrowserThread::CurrentlyOn(content::BrowserThread::UI))
76 return; 70 return;
77 71
72 TemplateURLService* template_url_service =
73 TemplateURLServiceFactory::GetForProfile(profile_);
74 if (template_url_service)
75 template_url_service->AddObserver(this);
erikwright (departed) 2014/05/06 19:48:02 Should we do a RemoveObserver in the destructor?
Peter Kasting 2014/05/06 19:58:03 Yes. Good catch.
76
78 ResetInstantSearchPrerenderer(); 77 ResetInstantSearchPrerenderer();
79 78
80 registrar_.Add(this, 79 registrar_.Add(this,
81 content::NOTIFICATION_RENDERER_PROCESS_CREATED, 80 content::NOTIFICATION_RENDERER_PROCESS_CREATED,
82 content::NotificationService::AllSources()); 81 content::NotificationService::AllSources());
83 registrar_.Add(this, 82 registrar_.Add(this,
84 content::NOTIFICATION_RENDERER_PROCESS_TERMINATED, 83 content::NOTIFICATION_RENDERER_PROCESS_TERMINATED,
85 content::NotificationService::AllSources()); 84 content::NotificationService::AllSources());
86 85
87 history::TopSites* top_sites = profile_->GetTopSites(); 86 history::TopSites* top_sites = profile_->GetTopSites();
88 if (top_sites) { 87 if (top_sites) {
89 registrar_.Add(this, 88 registrar_.Add(this,
90 chrome::NOTIFICATION_TOP_SITES_CHANGED, 89 chrome::NOTIFICATION_TOP_SITES_CHANGED,
91 content::Source<history::TopSites>(top_sites)); 90 content::Source<history::TopSites>(top_sites));
92 } 91 }
93 instant_io_context_ = new InstantIOContext(); 92 instant_io_context_ = new InstantIOContext();
94 93
95 if (profile_ && profile_->GetResourceContext()) { 94 if (profile_ && profile_->GetResourceContext()) {
96 BrowserThread::PostTask( 95 content::BrowserThread::PostTask(
97 BrowserThread::IO, FROM_HERE, 96 content::BrowserThread::IO, FROM_HERE,
98 base::Bind(&InstantIOContext::SetUserDataOnIO, 97 base::Bind(&InstantIOContext::SetUserDataOnIO,
99 profile->GetResourceContext(), instant_io_context_)); 98 profile->GetResourceContext(), instant_io_context_));
100 } 99 }
101 100
102 // Set up the data sources that Instant uses on the NTP. 101 // Set up the data sources that Instant uses on the NTP.
103 #if defined(ENABLE_THEMES) 102 #if defined(ENABLE_THEMES)
104 // Listen for theme installation. 103 // Listen for theme installation.
105 registrar_.Add(this, chrome::NOTIFICATION_BROWSER_THEME_CHANGED, 104 registrar_.Add(this, chrome::NOTIFICATION_BROWSER_THEME_CHANGED,
106 content::Source<ThemeService>( 105 content::Source<ThemeService>(
107 ThemeServiceFactory::GetForProfile(profile_))); 106 ThemeServiceFactory::GetForProfile(profile_)));
(...skipping 10 matching lines...) Expand all
118 #endif // !defined(OS_ANDROID) 117 #endif // !defined(OS_ANDROID)
119 118
120 content::URLDataSource::Add( 119 content::URLDataSource::Add(
121 profile_, new FaviconSource(profile_, FaviconSource::FAVICON)); 120 profile_, new FaviconSource(profile_, FaviconSource::FAVICON));
122 content::URLDataSource::Add(profile_, new LocalNtpSource(profile_)); 121 content::URLDataSource::Add(profile_, new LocalNtpSource(profile_));
123 content::URLDataSource::Add(profile_, new MostVisitedIframeSource()); 122 content::URLDataSource::Add(profile_, new MostVisitedIframeSource());
124 if (suggestions::SuggestionsService::IsEnabled()) { 123 if (suggestions::SuggestionsService::IsEnabled()) {
125 content::URLDataSource::Add( 124 content::URLDataSource::Add(
126 profile_, new suggestions::SuggestionsSource(profile_)); 125 profile_, new suggestions::SuggestionsSource(profile_));
127 } 126 }
128
129 profile_pref_registrar_.Init(profile_->GetPrefs());
130 profile_pref_registrar_.Add(
131 prefs::kDefaultSearchProviderID,
132 base::Bind(&InstantService::OnDefaultSearchProviderChanged,
133 base::Unretained(this)));
134
135 registrar_.Add(this, chrome::NOTIFICATION_GOOGLE_URL_UPDATED,
136 content::Source<Profile>(profile_->GetOriginalProfile()));
137 } 127 }
138 128
139 InstantService::~InstantService() { 129 InstantService::~InstantService() {
140 } 130 }
141 131
142 void InstantService::AddInstantProcess(int process_id) { 132 void InstantService::AddInstantProcess(int process_id) {
143 process_ids_.insert(process_id); 133 process_ids_.insert(process_id);
144 134
145 if (instant_io_context_.get()) { 135 if (instant_io_context_.get()) {
146 BrowserThread::PostTask(BrowserThread::IO, 136 content::BrowserThread::PostTask(
147 FROM_HERE, 137 content::BrowserThread::IO, FROM_HERE,
148 base::Bind(&InstantIOContext::AddInstantProcessOnIO, 138 base::Bind(&InstantIOContext::AddInstantProcessOnIO,
149 instant_io_context_, 139 instant_io_context_, process_id));
150 process_id));
151 } 140 }
152 } 141 }
153 142
154 bool InstantService::IsInstantProcess(int process_id) const { 143 bool InstantService::IsInstantProcess(int process_id) const {
155 return process_ids_.find(process_id) != process_ids_.end(); 144 return process_ids_.find(process_id) != process_ids_.end();
156 } 145 }
157 146
158 void InstantService::AddObserver(InstantServiceObserver* observer) { 147 void InstantService::AddObserver(InstantServiceObserver* observer) {
159 observers_.AddObserver(observer); 148 observers_.AddObserver(observer);
160 } 149 }
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after
197 } 186 }
198 187
199 void InstantService::UpdateMostVisitedItemsInfo() { 188 void InstantService::UpdateMostVisitedItemsInfo() {
200 NotifyAboutMostVisitedItems(); 189 NotifyAboutMostVisitedItems();
201 } 190 }
202 191
203 void InstantService::Shutdown() { 192 void InstantService::Shutdown() {
204 process_ids_.clear(); 193 process_ids_.clear();
205 194
206 if (instant_io_context_.get()) { 195 if (instant_io_context_.get()) {
207 BrowserThread::PostTask( 196 content::BrowserThread::PostTask(
208 BrowserThread::IO, 197 content::BrowserThread::IO, FROM_HERE,
209 FROM_HERE,
210 base::Bind(&InstantIOContext::ClearInstantProcessesOnIO, 198 base::Bind(&InstantIOContext::ClearInstantProcessesOnIO,
211 instant_io_context_)); 199 instant_io_context_));
212 } 200 }
213 instant_io_context_ = NULL; 201 instant_io_context_ = NULL;
214 } 202 }
215 203
216 void InstantService::Observe(int type, 204 void InstantService::Observe(int type,
217 const content::NotificationSource& source, 205 const content::NotificationSource& source,
218 const content::NotificationDetails& details) { 206 const content::NotificationDetails& details) {
219 switch (type) { 207 switch (type) {
(...skipping 13 matching lines...) Expand all
233 weak_ptr_factory_.GetWeakPtr()), false); 221 weak_ptr_factory_.GetWeakPtr()), false);
234 } 222 }
235 break; 223 break;
236 } 224 }
237 #if defined(ENABLE_THEMES) 225 #if defined(ENABLE_THEMES)
238 case chrome::NOTIFICATION_BROWSER_THEME_CHANGED: { 226 case chrome::NOTIFICATION_BROWSER_THEME_CHANGED: {
239 OnThemeChanged(content::Source<ThemeService>(source).ptr()); 227 OnThemeChanged(content::Source<ThemeService>(source).ptr());
240 break; 228 break;
241 } 229 }
242 #endif // defined(ENABLE_THEMES) 230 #endif // defined(ENABLE_THEMES)
243 case chrome::NOTIFICATION_GOOGLE_URL_UPDATED: {
244 OnGoogleURLUpdated(
245 content::Source<Profile>(source).ptr(),
246 content::Details<GoogleURLTracker::UpdatedDetails>(details).ptr());
247 break;
248 }
249 default: 231 default:
250 NOTREACHED() << "Unexpected notification type in InstantService."; 232 NOTREACHED() << "Unexpected notification type in InstantService.";
251 } 233 }
252 } 234 }
253 235
254 void InstantService::SendSearchURLsToRenderer(content::RenderProcessHost* rph) { 236 void InstantService::SendSearchURLsToRenderer(content::RenderProcessHost* rph) {
255 rph->Send(new ChromeViewMsg_SetSearchURLs( 237 rph->Send(new ChromeViewMsg_SetSearchURLs(
256 chrome::GetSearchURLs(profile_), chrome::GetNewTabPageURL(profile_))); 238 chrome::GetSearchURLs(profile_), chrome::GetNewTabPageURL(profile_)));
257 } 239 }
258 240
259 void InstantService::OnOmniboxStartMarginChanged(int start_margin) { 241 void InstantService::OnOmniboxStartMarginChanged(int start_margin) {
260 omnibox_start_margin_ = start_margin; 242 omnibox_start_margin_ = start_margin;
261 FOR_EACH_OBSERVER(InstantServiceObserver, observers_, 243 FOR_EACH_OBSERVER(InstantServiceObserver, observers_,
262 OmniboxStartMarginChanged(omnibox_start_margin_)); 244 OmniboxStartMarginChanged(omnibox_start_margin_));
263 } 245 }
264 246
265 void InstantService::OnRendererProcessTerminated(int process_id) { 247 void InstantService::OnRendererProcessTerminated(int process_id) {
266 process_ids_.erase(process_id); 248 process_ids_.erase(process_id);
267 249
268 if (instant_io_context_.get()) { 250 if (instant_io_context_.get()) {
269 BrowserThread::PostTask( 251 content::BrowserThread::PostTask(
270 BrowserThread::IO, 252 content::BrowserThread::IO, FROM_HERE,
271 FROM_HERE,
272 base::Bind(&InstantIOContext::RemoveInstantProcessOnIO, 253 base::Bind(&InstantIOContext::RemoveInstantProcessOnIO,
273 instant_io_context_, 254 instant_io_context_, process_id));
274 process_id));
275 } 255 }
276 } 256 }
277 257
278 void InstantService::OnMostVisitedItemsReceived( 258 void InstantService::OnMostVisitedItemsReceived(
279 const history::MostVisitedURLList& data) { 259 const history::MostVisitedURLList& data) {
280 history::MostVisitedURLList reordered_data(data); 260 history::MostVisitedURLList reordered_data(data);
281 history::MostVisitedTilesExperiment::MaybeShuffle(&reordered_data); 261 history::MostVisitedTilesExperiment::MaybeShuffle(&reordered_data);
282 262
283 std::vector<InstantMostVisitedItem> new_most_visited_items; 263 std::vector<InstantMostVisitedItem> new_most_visited_items;
284 for (size_t i = 0; i < reordered_data.size(); i++) { 264 for (size_t i = 0; i < reordered_data.size(); i++) {
(...skipping 114 matching lines...) Expand 10 before | Expand all | Expand 10 after
399 theme_info_->image_height = image->height(); 379 theme_info_->image_height = image->height();
400 380
401 theme_info_->has_attribution = 381 theme_info_->has_attribution =
402 theme_service->HasCustomImage(IDR_THEME_NTP_ATTRIBUTION); 382 theme_service->HasCustomImage(IDR_THEME_NTP_ATTRIBUTION);
403 } 383 }
404 384
405 FOR_EACH_OBSERVER(InstantServiceObserver, observers_, 385 FOR_EACH_OBSERVER(InstantServiceObserver, observers_,
406 ThemeInfoChanged(*theme_info_)); 386 ThemeInfoChanged(*theme_info_));
407 } 387 }
408 388
409 void InstantService::OnGoogleURLUpdated( 389 void InstantService::OnTemplateURLServiceChanged() {
410 Profile* profile,
411 GoogleURLTracker::UpdatedDetails* details) {
412 GURL last_prompted_url(
413 profile->GetPrefs()->GetString(prefs::kLastPromptedGoogleURL));
414
415 // See GoogleURLTracker::OnURLFetchComplete().
416 // last_prompted_url.is_empty() indicates very first run of Chrome. So there
417 // is no need to notify, as there won't be any old state.
418 if (last_prompted_url.is_empty())
419 return;
420
421 ResetInstantSearchPrerenderer();
422
423 // Only the scheme changed. Ignore it since we do not prompt the user in this
424 // case.
425 if (net::StripWWWFromHost(details->first) ==
426 net::StripWWWFromHost(details->second))
427 return;
428
429 FOR_EACH_OBSERVER(InstantServiceObserver, observers_, GoogleURLUpdated());
430 }
431
432 void InstantService::OnDefaultSearchProviderChanged(
433 const std::string& pref_name) {
434 DCHECK_EQ(pref_name, std::string(prefs::kDefaultSearchProviderID));
435 const TemplateURL* template_url = TemplateURLServiceFactory::GetForProfile( 390 const TemplateURL* template_url = TemplateURLServiceFactory::GetForProfile(
436 profile_)->GetDefaultSearchProvider(); 391 profile_)->GetDefaultSearchProvider();
437 if (!template_url) { 392 if (!template_url) {
438 // A NULL |template_url| could mean either this notification is sent during 393 // A NULL |template_url| could mean either this notification is sent during
439 // the browser start up operation or the user now has no default search 394 // the browser start up operation or the user now has no default search
440 // provider. There is no way for the user to reach this state using the 395 // provider. There is no way for the user to reach this state using the
441 // Chrome settings. Only explicitly poking at the DB or bugs in the Sync 396 // Chrome settings. Only explicitly poking at the DB or bugs in the Sync
442 // could cause that, neither of which we support. 397 // could cause that, neither of which we support.
443 return; 398 return;
444 } 399 }
400 GURL new_search_result_prefetch_base_url(
401 chrome::GetSearchResultPrefetchBaseURL(profile_));
402 if (new_search_result_prefetch_base_url ==
403 current_search_result_prefetch_base_url_)
404 return;
405 current_search_result_prefetch_base_url_ =
406 new_search_result_prefetch_base_url;
445 407
446 ResetInstantSearchPrerenderer(); 408 ResetInstantSearchPrerenderer();
447 409
448 FOR_EACH_OBSERVER( 410 FOR_EACH_OBSERVER(
449 InstantServiceObserver, observers_, DefaultSearchProviderChanged()); 411 InstantServiceObserver, observers_, DefaultSearchProviderChanged());
450 } 412 }
451 413
452 void InstantService::ResetInstantSearchPrerenderer() { 414 void InstantService::ResetInstantSearchPrerenderer() {
453 if (!chrome::ShouldPrefetchSearchResults()) 415 if (!chrome::ShouldPrefetchSearchResults())
454 return; 416 return;
455 417
456 GURL url(chrome::GetSearchResultPrefetchBaseURL(profile_)); 418 if (current_search_result_prefetch_base_url_.is_valid()) {
457 if (url.is_valid()) 419 instant_prerenderer_.reset(new InstantSearchPrerenderer(
458 instant_prerenderer_.reset(new InstantSearchPrerenderer(profile_, url)); 420 profile_, current_search_result_prefetch_base_url_));
459 else 421 } else {
460 instant_prerenderer_.reset(); 422 instant_prerenderer_.reset();
423 }
461 } 424 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698