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/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 Loading... | |
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, chrome::NOTIFICATION_GOOGLE_URL_UPDATED, |
97 content::URLDataSource::Add(profile, new FaviconSource( | 105 content::Source<Profile>(profile_->GetOriginalProfile())); |
98 profile, FaviconSource::FAVICON)); | 106 |
99 content::URLDataSource::Add(profile, new LocalNtpSource(profile)); | |
100 content::URLDataSource::Add(profile, new MostVisitedIframeSource()); | |
101 registrar_.Add(this, chrome::NOTIFICATION_PROFILE_DESTROYED, | 107 registrar_.Add(this, chrome::NOTIFICATION_PROFILE_DESTROYED, |
102 content::Source<Profile>(profile_)); | 108 content::Source<Profile>(profile_)); |
109 | |
110 content::URLDataSource::Add(profile_, new ThumbnailSource(profile_)); | |
111 content::URLDataSource::Add( | |
112 profile_, new FaviconSource(profile_, FaviconSource::FAVICON)); | |
113 content::URLDataSource::Add(profile_, new LocalNtpSource(profile_)); | |
114 content::URLDataSource::Add(profile_, new MostVisitedIframeSource()); | |
115 | |
116 profile_pref_registrar_.Init(profile_->GetPrefs()); | |
117 profile_pref_registrar_.Add( | |
118 prefs::kDefaultSearchProviderID, | |
119 base::Bind(&InstantService::OnDefaultSearchProviderChanged, | |
120 base::Unretained(this))); | |
103 } | 121 } |
104 | 122 |
105 InstantService::~InstantService() { | 123 InstantService::~InstantService() { |
106 } | 124 } |
107 | 125 |
108 void InstantService::AddInstantProcess(int process_id) { | 126 void InstantService::AddInstantProcess(int process_id) { |
109 process_ids_.insert(process_id); | 127 process_ids_.insert(process_id); |
110 | 128 |
111 if (instant_io_context_.get()) { | 129 if (instant_io_context_.get()) { |
112 BrowserThread::PostTask(BrowserThread::IO, | 130 BrowserThread::PostTask(BrowserThread::IO, |
(...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
187 return ntp_prerenderer_.GetNTPContents(); | 205 return ntp_prerenderer_.GetNTPContents(); |
188 } | 206 } |
189 | 207 |
190 void InstantService::OnBrowserInstantControllerCreated() { | 208 void InstantService::OnBrowserInstantControllerCreated() { |
191 if (profile_->IsOffTheRecord()) | 209 if (profile_->IsOffTheRecord()) |
192 return; | 210 return; |
193 | 211 |
194 ++browser_instant_controller_object_count_; | 212 ++browser_instant_controller_object_count_; |
195 | 213 |
196 if (browser_instant_controller_object_count_ == 1) | 214 if (browser_instant_controller_object_count_ == 1) |
197 ntp_prerenderer_.PreloadInstantNTP(); | 215 ntp_prerenderer_.Init(); |
198 } | 216 } |
199 | 217 |
200 void InstantService::OnBrowserInstantControllerDestroyed() { | 218 void InstantService::OnBrowserInstantControllerDestroyed() { |
201 if (profile_->IsOffTheRecord()) | 219 if (profile_->IsOffTheRecord()) |
202 return; | 220 return; |
203 | 221 |
204 DCHECK_GT(browser_instant_controller_object_count_, 0U); | 222 DCHECK_GT(browser_instant_controller_object_count_, 0U); |
205 --browser_instant_controller_object_count_; | 223 --browser_instant_controller_object_count_; |
206 | 224 |
207 // All browser windows have closed, so release the InstantNTP resources to | 225 // All browser windows have closed, so release the InstantNTP resources to |
208 // work around http://crbug.com/180810. | 226 // work around http://crbug.com/180810. |
209 if (browser_instant_controller_object_count_ == 0) | 227 if (browser_instant_controller_object_count_ == 0) |
210 ntp_prerenderer_.DeleteNTPContents(); | 228 ntp_prerenderer_.Destroy(); |
samarth
2013/08/14 00:24:14
Init -> InitNTP or PreloadNTP
Destroy -> DestroyNT
Anuj
2013/08/14 06:38:57
Reverted those methods.
| |
211 } | 229 } |
212 | 230 |
213 void InstantService::Observe(int type, | 231 void InstantService::Observe(int type, |
214 const content::NotificationSource& source, | 232 const content::NotificationSource& source, |
215 const content::NotificationDetails& details) { | 233 const content::NotificationDetails& details) { |
216 switch (type) { | 234 switch (type) { |
217 case content::NOTIFICATION_RENDERER_PROCESS_TERMINATED: { | 235 case content::NOTIFICATION_RENDERER_PROCESS_TERMINATED: { |
218 int process_id = | 236 int process_id = |
219 content::Source<content::RenderProcessHost>(source)->GetID(); | 237 content::Source<content::RenderProcessHost>(source)->GetID(); |
220 process_ids_.erase(process_id); | 238 process_ids_.erase(process_id); |
(...skipping 26 matching lines...) Expand all Loading... | |
247 case chrome::NOTIFICATION_PROFILE_DESTROYED: { | 265 case chrome::NOTIFICATION_PROFILE_DESTROYED: { |
248 // Last chance to delete InstantNTP contents. We generally delete | 266 // Last chance to delete InstantNTP contents. We generally delete |
249 // preloaded InstantNTP when the last BrowserInstantController object is | 267 // preloaded InstantNTP when the last BrowserInstantController object is |
250 // destroyed. When the browser shutdown happens without closing browsers, | 268 // destroyed. When the browser shutdown happens without closing browsers, |
251 // there is a race condition between BrowserInstantController destruction | 269 // there is a race condition between BrowserInstantController destruction |
252 // and Profile destruction. | 270 // and Profile destruction. |
253 if (GetNTPContents()) | 271 if (GetNTPContents()) |
254 ntp_prerenderer_.DeleteNTPContents(); | 272 ntp_prerenderer_.DeleteNTPContents(); |
255 break; | 273 break; |
256 } | 274 } |
275 case chrome::NOTIFICATION_GOOGLE_URL_UPDATED: { | |
samarth
2013/08/14 00:24:14
Does this ever fire if google is not your default
Anuj
2013/08/14 06:38:57
It is guarded by presence of GogoleBaseURLs in def
| |
276 OnGoogleURLUpdated( | |
277 content::Source<Profile>(source).ptr(), | |
278 content::Details<GoogleURLTracker::UpdatedDetails>(details).ptr()); | |
279 break; | |
280 } | |
257 default: | 281 default: |
258 NOTREACHED() << "Unexpected notification type in InstantService."; | 282 NOTREACHED() << "Unexpected notification type in InstantService."; |
259 } | 283 } |
260 } | 284 } |
261 | 285 |
262 void InstantService::OnMostVisitedItemsReceived( | 286 void InstantService::OnMostVisitedItemsReceived( |
263 const history::MostVisitedURLList& data) { | 287 const history::MostVisitedURLList& data) { |
264 history::MostVisitedURLList reordered_data(data); | 288 history::MostVisitedURLList reordered_data(data); |
265 history::MostVisitedTilesExperiment::MaybeShuffle(&reordered_data); | 289 history::MostVisitedTilesExperiment::MaybeShuffle(&reordered_data); |
266 | 290 |
(...skipping 121 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
388 theme_info_->image_height = image->height(); | 412 theme_info_->image_height = image->height(); |
389 | 413 |
390 theme_info_->has_attribution = | 414 theme_info_->has_attribution = |
391 theme_service->HasCustomImage(IDR_THEME_NTP_ATTRIBUTION); | 415 theme_service->HasCustomImage(IDR_THEME_NTP_ATTRIBUTION); |
392 } | 416 } |
393 | 417 |
394 FOR_EACH_OBSERVER(InstantServiceObserver, observers_, | 418 FOR_EACH_OBSERVER(InstantServiceObserver, observers_, |
395 ThemeInfoChanged(*theme_info_)); | 419 ThemeInfoChanged(*theme_info_)); |
396 } | 420 } |
397 | 421 |
422 void InstantService::OnGoogleURLUpdated( | |
423 Profile* profile, | |
424 GoogleURLTracker::UpdatedDetails* details) { | |
425 GURL last_prompted_url( | |
426 profile->GetPrefs()->GetString(prefs::kLastPromptedGoogleURL)); | |
427 | |
428 // See GoogleURLTracker::OnURLFetchComplete. | |
429 // last_prompted_url.is_empty indicated very first run of Chrome. So there is | |
430 // no need to tamper with Instant resources. | |
431 if (last_prompted_url.is_empty()) | |
432 return; | |
433 | |
434 // Only the scheme changed. Ignore it since we do not prompt the user in this | |
435 // case. | |
436 if (net::StripWWWFromHost(details->first) == | |
437 net::StripWWWFromHost(details->second)) | |
438 return; | |
439 | |
440 FOR_EACH_OBSERVER(InstantServiceObserver, observers_, GoogleURLUpdated()); | |
441 } | |
442 | |
443 void InstantService::OnDefaultSearchProviderChanged( | |
444 const std::string& pref_name) { | |
445 DCHECK_EQ(pref_name, std::string(prefs::kDefaultSearchProviderID)); | |
446 const TemplateURL* template_url = TemplateURLServiceFactory::GetForProfile( | |
447 profile_)->GetDefaultSearchProvider(); | |
448 if (!template_url) { | |
449 // A NULL |template_url| could mean either this notification is sent during | |
450 // the browser start up operation or the user now has no default search | |
451 // provider. There is no way for the user to reach this state using the | |
452 // Chrome settings. Only explicitly poking at the DB or bugs in the Sync | |
453 // could cause that, neither of which we support. | |
454 return; | |
455 } | |
456 FOR_EACH_OBSERVER( | |
457 InstantServiceObserver, observers_, DefaultSearchProviderChanged()); | |
458 } | |
459 | |
398 InstantNTPPrerenderer* InstantService::ntp_prerenderer() { | 460 InstantNTPPrerenderer* InstantService::ntp_prerenderer() { |
399 return &ntp_prerenderer_; | 461 return &ntp_prerenderer_; |
400 } | 462 } |
OLD | NEW |