OLD | NEW |
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/prefs/pref_service.h" |
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
63 color.b = SkColorGetB(sKColor); | 63 color.b = SkColorGetB(sKColor); |
64 color.a = SkColorGetA(sKColor); | 64 color.a = SkColorGetA(sKColor); |
65 return color; | 65 return color; |
66 } | 66 } |
67 | 67 |
68 } // namespace | 68 } // namespace |
69 | 69 |
70 InstantService::InstantService(Profile* profile) | 70 InstantService::InstantService(Profile* profile) |
71 : profile_(profile), | 71 : profile_(profile), |
72 omnibox_start_margin_(chrome::kDisableStartMargin), | 72 omnibox_start_margin_(chrome::kDisableStartMargin), |
73 weak_ptr_factory_(this) { | 73 weak_ptr_factory_(this), |
| 74 current_search_result_prefetch_base_url_( |
| 75 chrome::GetSearchResultPrefetchBaseURL(profile_)), |
| 76 default_search_engine_watcher_(this) { |
74 // Stub for unit tests. | 77 // Stub for unit tests. |
75 if (!BrowserThread::CurrentlyOn(BrowserThread::UI)) | 78 if (!BrowserThread::CurrentlyOn(BrowserThread::UI)) |
76 return; | 79 return; |
77 | 80 |
| 81 TemplateURLService* template_url_service = |
| 82 TemplateURLServiceFactory::GetForProfile(profile_); |
| 83 if (template_url_service) |
| 84 template_url_service->AddObserver(&default_search_engine_watcher_); |
| 85 |
78 ResetInstantSearchPrerenderer(); | 86 ResetInstantSearchPrerenderer(); |
79 | 87 |
80 registrar_.Add(this, | 88 registrar_.Add(this, |
81 content::NOTIFICATION_RENDERER_PROCESS_CREATED, | 89 content::NOTIFICATION_RENDERER_PROCESS_CREATED, |
82 content::NotificationService::AllSources()); | 90 content::NotificationService::AllSources()); |
83 registrar_.Add(this, | 91 registrar_.Add(this, |
84 content::NOTIFICATION_RENDERER_PROCESS_TERMINATED, | 92 content::NOTIFICATION_RENDERER_PROCESS_TERMINATED, |
85 content::NotificationService::AllSources()); | 93 content::NotificationService::AllSources()); |
86 | 94 |
87 history::TopSites* top_sites = profile_->GetTopSites(); | 95 history::TopSites* top_sites = profile_->GetTopSites(); |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
119 | 127 |
120 content::URLDataSource::Add( | 128 content::URLDataSource::Add( |
121 profile_, new FaviconSource(profile_, FaviconSource::FAVICON)); | 129 profile_, new FaviconSource(profile_, FaviconSource::FAVICON)); |
122 content::URLDataSource::Add(profile_, new LocalNtpSource(profile_)); | 130 content::URLDataSource::Add(profile_, new LocalNtpSource(profile_)); |
123 content::URLDataSource::Add(profile_, new MostVisitedIframeSource()); | 131 content::URLDataSource::Add(profile_, new MostVisitedIframeSource()); |
124 if (suggestions::SuggestionsService::IsEnabled()) { | 132 if (suggestions::SuggestionsService::IsEnabled()) { |
125 content::URLDataSource::Add( | 133 content::URLDataSource::Add( |
126 profile_, new suggestions::SuggestionsSource(profile_)); | 134 profile_, new suggestions::SuggestionsSource(profile_)); |
127 } | 135 } |
128 | 136 |
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, | 137 registrar_.Add(this, chrome::NOTIFICATION_GOOGLE_URL_UPDATED, |
136 content::Source<Profile>(profile_->GetOriginalProfile())); | 138 content::Source<Profile>(profile_->GetOriginalProfile())); |
137 } | 139 } |
138 | 140 |
139 InstantService::~InstantService() { | 141 InstantService::~InstantService() { |
140 } | 142 } |
141 | 143 |
142 void InstantService::AddInstantProcess(int process_id) { | 144 void InstantService::AddInstantProcess(int process_id) { |
143 process_ids_.insert(process_id); | 145 process_ids_.insert(process_id); |
144 | 146 |
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
193 if (!theme_info_) | 195 if (!theme_info_) |
194 OnThemeChanged(ThemeServiceFactory::GetForProfile(profile_)); | 196 OnThemeChanged(ThemeServiceFactory::GetForProfile(profile_)); |
195 else | 197 else |
196 OnThemeChanged(NULL); | 198 OnThemeChanged(NULL); |
197 } | 199 } |
198 | 200 |
199 void InstantService::UpdateMostVisitedItemsInfo() { | 201 void InstantService::UpdateMostVisitedItemsInfo() { |
200 NotifyAboutMostVisitedItems(); | 202 NotifyAboutMostVisitedItems(); |
201 } | 203 } |
202 | 204 |
| 205 InstantService::DefaultSearchEngineWatcher::DefaultSearchEngineWatcher( |
| 206 InstantService* outer) |
| 207 : outer_(outer) { |
| 208 } |
| 209 |
| 210 void InstantService::DefaultSearchEngineWatcher::OnTemplateURLServiceChanged() { |
| 211 outer_->OnTemplateURLServiceChanged(); |
| 212 } |
| 213 |
203 void InstantService::Shutdown() { | 214 void InstantService::Shutdown() { |
204 process_ids_.clear(); | 215 process_ids_.clear(); |
205 | 216 |
206 if (instant_io_context_.get()) { | 217 if (instant_io_context_.get()) { |
207 BrowserThread::PostTask( | 218 BrowserThread::PostTask( |
208 BrowserThread::IO, | 219 BrowserThread::IO, |
209 FROM_HERE, | 220 FROM_HERE, |
210 base::Bind(&InstantIOContext::ClearInstantProcessesOnIO, | 221 base::Bind(&InstantIOContext::ClearInstantProcessesOnIO, |
211 instant_io_context_)); | 222 instant_io_context_)); |
212 } | 223 } |
(...skipping 209 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
422 | 433 |
423 // Only the scheme changed. Ignore it since we do not prompt the user in this | 434 // Only the scheme changed. Ignore it since we do not prompt the user in this |
424 // case. | 435 // case. |
425 if (net::StripWWWFromHost(details->first) == | 436 if (net::StripWWWFromHost(details->first) == |
426 net::StripWWWFromHost(details->second)) | 437 net::StripWWWFromHost(details->second)) |
427 return; | 438 return; |
428 | 439 |
429 FOR_EACH_OBSERVER(InstantServiceObserver, observers_, GoogleURLUpdated()); | 440 FOR_EACH_OBSERVER(InstantServiceObserver, observers_, GoogleURLUpdated()); |
430 } | 441 } |
431 | 442 |
432 void InstantService::OnDefaultSearchProviderChanged( | 443 void InstantService::OnTemplateURLServiceChanged() { |
433 const std::string& pref_name) { | |
434 DCHECK_EQ(pref_name, std::string(prefs::kDefaultSearchProviderID)); | |
435 const TemplateURL* template_url = TemplateURLServiceFactory::GetForProfile( | 444 const TemplateURL* template_url = TemplateURLServiceFactory::GetForProfile( |
436 profile_)->GetDefaultSearchProvider(); | 445 profile_)->GetDefaultSearchProvider(); |
437 if (!template_url) { | 446 if (!template_url) { |
438 // A NULL |template_url| could mean either this notification is sent during | 447 // 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 | 448 // 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 | 449 // 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 | 450 // Chrome settings. Only explicitly poking at the DB or bugs in the Sync |
442 // could cause that, neither of which we support. | 451 // could cause that, neither of which we support. |
443 return; | 452 return; |
444 } | 453 } |
| 454 GURL new_search_result_prefetch_base_url( |
| 455 chrome::GetSearchResultPrefetchBaseURL(profile_)); |
| 456 if (new_search_result_prefetch_base_url == |
| 457 current_search_result_prefetch_base_url_) |
| 458 return; |
| 459 current_search_result_prefetch_base_url_ = |
| 460 new_search_result_prefetch_base_url; |
445 | 461 |
446 ResetInstantSearchPrerenderer(); | 462 ResetInstantSearchPrerenderer(); |
447 | 463 |
448 FOR_EACH_OBSERVER( | 464 FOR_EACH_OBSERVER( |
449 InstantServiceObserver, observers_, DefaultSearchProviderChanged()); | 465 InstantServiceObserver, observers_, DefaultSearchProviderChanged()); |
450 } | 466 } |
451 | 467 |
452 void InstantService::ResetInstantSearchPrerenderer() { | 468 void InstantService::ResetInstantSearchPrerenderer() { |
453 if (!chrome::ShouldPrefetchSearchResults()) | 469 if (!chrome::ShouldPrefetchSearchResults()) |
454 return; | 470 return; |
455 | 471 |
456 GURL url(chrome::GetSearchResultPrefetchBaseURL(profile_)); | 472 if (current_search_result_prefetch_base_url_.is_valid()) { |
457 if (url.is_valid()) | 473 instant_prerenderer_.reset(new InstantSearchPrerenderer( |
458 instant_prerenderer_.reset(new InstantSearchPrerenderer(profile_, url)); | 474 profile_, current_search_result_prefetch_base_url_)); |
459 else | 475 } else { |
460 instant_prerenderer_.reset(); | 476 instant_prerenderer_.reset(); |
| 477 } |
461 } | 478 } |
OLD | NEW |