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

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: RemoveObserver 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 --------------------------------------------------------------------
Jered 2014/05/06 23:01:18 Please omit these section comments, they don't rea
Peter Kasting 2014/05/07 22:24:38 Just trying to stay consistent with the headers us
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);
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() {
130 TemplateURLService* template_url_service =
131 TemplateURLServiceFactory::GetForProfile(profile_);
132 if (template_url_service)
133 template_url_service->RemoveObserver(this);
140 } 134 }
141 135
142 void InstantService::AddInstantProcess(int process_id) { 136 void InstantService::AddInstantProcess(int process_id) {
143 process_ids_.insert(process_id); 137 process_ids_.insert(process_id);
144 138
145 if (instant_io_context_.get()) { 139 if (instant_io_context_.get()) {
146 BrowserThread::PostTask(BrowserThread::IO, 140 content::BrowserThread::PostTask(
147 FROM_HERE, 141 content::BrowserThread::IO, FROM_HERE,
148 base::Bind(&InstantIOContext::AddInstantProcessOnIO, 142 base::Bind(&InstantIOContext::AddInstantProcessOnIO,
149 instant_io_context_, 143 instant_io_context_, process_id));
150 process_id));
151 } 144 }
152 } 145 }
153 146
154 bool InstantService::IsInstantProcess(int process_id) const { 147 bool InstantService::IsInstantProcess(int process_id) const {
155 return process_ids_.find(process_id) != process_ids_.end(); 148 return process_ids_.find(process_id) != process_ids_.end();
156 } 149 }
157 150
158 void InstantService::AddObserver(InstantServiceObserver* observer) { 151 void InstantService::AddObserver(InstantServiceObserver* observer) {
159 observers_.AddObserver(observer); 152 observers_.AddObserver(observer);
160 } 153 }
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after
197 } 190 }
198 191
199 void InstantService::UpdateMostVisitedItemsInfo() { 192 void InstantService::UpdateMostVisitedItemsInfo() {
200 NotifyAboutMostVisitedItems(); 193 NotifyAboutMostVisitedItems();
201 } 194 }
202 195
203 void InstantService::Shutdown() { 196 void InstantService::Shutdown() {
204 process_ids_.clear(); 197 process_ids_.clear();
205 198
206 if (instant_io_context_.get()) { 199 if (instant_io_context_.get()) {
207 BrowserThread::PostTask( 200 content::BrowserThread::PostTask(
208 BrowserThread::IO, 201 content::BrowserThread::IO, FROM_HERE,
209 FROM_HERE,
210 base::Bind(&InstantIOContext::ClearInstantProcessesOnIO, 202 base::Bind(&InstantIOContext::ClearInstantProcessesOnIO,
211 instant_io_context_)); 203 instant_io_context_));
212 } 204 }
213 instant_io_context_ = NULL; 205 instant_io_context_ = NULL;
214 } 206 }
215 207
216 void InstantService::Observe(int type, 208 void InstantService::Observe(int type,
217 const content::NotificationSource& source, 209 const content::NotificationSource& source,
218 const content::NotificationDetails& details) { 210 const content::NotificationDetails& details) {
219 switch (type) { 211 switch (type) {
(...skipping 13 matching lines...) Expand all
233 weak_ptr_factory_.GetWeakPtr()), false); 225 weak_ptr_factory_.GetWeakPtr()), false);
234 } 226 }
235 break; 227 break;
236 } 228 }
237 #if defined(ENABLE_THEMES) 229 #if defined(ENABLE_THEMES)
238 case chrome::NOTIFICATION_BROWSER_THEME_CHANGED: { 230 case chrome::NOTIFICATION_BROWSER_THEME_CHANGED: {
239 OnThemeChanged(content::Source<ThemeService>(source).ptr()); 231 OnThemeChanged(content::Source<ThemeService>(source).ptr());
240 break; 232 break;
241 } 233 }
242 #endif // defined(ENABLE_THEMES) 234 #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: 235 default:
250 NOTREACHED() << "Unexpected notification type in InstantService."; 236 NOTREACHED() << "Unexpected notification type in InstantService.";
251 } 237 }
252 } 238 }
253 239
254 void InstantService::SendSearchURLsToRenderer(content::RenderProcessHost* rph) { 240 void InstantService::SendSearchURLsToRenderer(content::RenderProcessHost* rph) {
255 rph->Send(new ChromeViewMsg_SetSearchURLs( 241 rph->Send(new ChromeViewMsg_SetSearchURLs(
256 chrome::GetSearchURLs(profile_), chrome::GetNewTabPageURL(profile_))); 242 chrome::GetSearchURLs(profile_), chrome::GetNewTabPageURL(profile_)));
257 } 243 }
258 244
259 void InstantService::OnOmniboxStartMarginChanged(int start_margin) { 245 void InstantService::OnOmniboxStartMarginChanged(int start_margin) {
260 omnibox_start_margin_ = start_margin; 246 omnibox_start_margin_ = start_margin;
261 FOR_EACH_OBSERVER(InstantServiceObserver, observers_, 247 FOR_EACH_OBSERVER(InstantServiceObserver, observers_,
262 OmniboxStartMarginChanged(omnibox_start_margin_)); 248 OmniboxStartMarginChanged(omnibox_start_margin_));
263 } 249 }
264 250
265 void InstantService::OnRendererProcessTerminated(int process_id) { 251 void InstantService::OnRendererProcessTerminated(int process_id) {
266 process_ids_.erase(process_id); 252 process_ids_.erase(process_id);
267 253
268 if (instant_io_context_.get()) { 254 if (instant_io_context_.get()) {
269 BrowserThread::PostTask( 255 content::BrowserThread::PostTask(
270 BrowserThread::IO, 256 content::BrowserThread::IO, FROM_HERE,
271 FROM_HERE,
272 base::Bind(&InstantIOContext::RemoveInstantProcessOnIO, 257 base::Bind(&InstantIOContext::RemoveInstantProcessOnIO,
273 instant_io_context_, 258 instant_io_context_, process_id));
274 process_id));
275 } 259 }
276 } 260 }
277 261
278 void InstantService::OnMostVisitedItemsReceived( 262 void InstantService::OnMostVisitedItemsReceived(
279 const history::MostVisitedURLList& data) { 263 const history::MostVisitedURLList& data) {
280 history::MostVisitedURLList reordered_data(data); 264 history::MostVisitedURLList reordered_data(data);
281 history::MostVisitedTilesExperiment::MaybeShuffle(&reordered_data); 265 history::MostVisitedTilesExperiment::MaybeShuffle(&reordered_data);
282 266
283 std::vector<InstantMostVisitedItem> new_most_visited_items; 267 std::vector<InstantMostVisitedItem> new_most_visited_items;
284 for (size_t i = 0; i < reordered_data.size(); i++) { 268 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(); 383 theme_info_->image_height = image->height();
400 384
401 theme_info_->has_attribution = 385 theme_info_->has_attribution =
402 theme_service->HasCustomImage(IDR_THEME_NTP_ATTRIBUTION); 386 theme_service->HasCustomImage(IDR_THEME_NTP_ATTRIBUTION);
403 } 387 }
404 388
405 FOR_EACH_OBSERVER(InstantServiceObserver, observers_, 389 FOR_EACH_OBSERVER(InstantServiceObserver, observers_,
406 ThemeInfoChanged(*theme_info_)); 390 ThemeInfoChanged(*theme_info_));
407 } 391 }
408 392
409 void InstantService::OnGoogleURLUpdated( 393 void InstantService::OnTemplateURLServiceChanged() {
Jered 2014/05/06 23:01:18 Is this notification now totally unused? What abou
Peter Kasting 2014/05/06 23:17:23 Do you mean, will searchdomaincheck changes cause
Jered 2014/05/07 00:08:27 The unit test helper seems to not use GoogleURLTra
Peter Kasting 2014/05/07 00:20:18 Why not? It's firing the same notification. Ther
Jered 2014/05/07 00:47:04 Ok. I've convinced myself that TemplateURLService:
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( 394 const TemplateURL* template_url = TemplateURLServiceFactory::GetForProfile(
436 profile_)->GetDefaultSearchProvider(); 395 profile_)->GetDefaultSearchProvider();
437 if (!template_url) { 396 if (!template_url) {
438 // A NULL |template_url| could mean either this notification is sent during 397 // 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 398 // 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 399 // 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 400 // Chrome settings. Only explicitly poking at the DB or bugs in the Sync
442 // could cause that, neither of which we support. 401 // could cause that, neither of which we support.
443 return; 402 return;
444 } 403 }
404 GURL new_search_result_prefetch_base_url(
405 chrome::GetSearchResultPrefetchBaseURL(profile_));
406 if (new_search_result_prefetch_base_url ==
Jered 2014/05/06 23:01:18 This test prevents notifying observers about the c
Peter Kasting 2014/05/06 23:17:23 When isn't it correct? (We have to filter _some_
Jered 2014/05/07 00:08:27 BrowserInstantController::DefaultSearchProviderCha
Peter Kasting 2014/05/07 00:20:18 If observers care about all DSP changes, not just
Jered 2014/05/07 00:47:04 Observers care about any DSP changes that affect i
Peter Kasting 2014/05/07 00:50:27 The only observer of this is BrowserInstantControl
erikwright (departed) 2014/05/07 18:40:01 The BrowserInstantController calls InstantService:
Peter Kasting 2014/05/07 18:43:37 I think we can detect whether the changes in quest
407 current_search_result_prefetch_base_url_)
408 return;
409 current_search_result_prefetch_base_url_ =
410 new_search_result_prefetch_base_url;
445 411
446 ResetInstantSearchPrerenderer(); 412 ResetInstantSearchPrerenderer();
447 413
448 FOR_EACH_OBSERVER( 414 FOR_EACH_OBSERVER(
449 InstantServiceObserver, observers_, DefaultSearchProviderChanged()); 415 InstantServiceObserver, observers_, DefaultSearchProviderChanged());
450 } 416 }
451 417
452 void InstantService::ResetInstantSearchPrerenderer() { 418 void InstantService::ResetInstantSearchPrerenderer() {
453 if (!chrome::ShouldPrefetchSearchResults()) 419 if (!chrome::ShouldPrefetchSearchResults())
454 return; 420 return;
455 421
456 GURL url(chrome::GetSearchResultPrefetchBaseURL(profile_)); 422 if (current_search_result_prefetch_base_url_.is_valid()) {
457 if (url.is_valid()) 423 instant_prerenderer_.reset(new InstantSearchPrerenderer(
458 instant_prerenderer_.reset(new InstantSearchPrerenderer(profile_, url)); 424 profile_, current_search_result_prefetch_base_url_));
459 else 425 } else {
460 instant_prerenderer_.reset(); 426 instant_prerenderer_.reset();
427 }
461 } 428 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698