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 |
44 const int kSectionBorderAlphaTransparency = 80; | 52 const int kSectionBorderAlphaTransparency = 80; |
45 | 53 |
46 // Converts SkColor to RGBAColor | 54 // Converts SkColor to RGBAColor |
47 RGBAColor SkColorToRGBAColor(const SkColor& sKColor) { | 55 RGBAColor SkColorToRGBAColor(const SkColor& sKColor) { |
48 RGBAColor color; | 56 RGBAColor color; |
49 color.r = SkColorGetR(sKColor); | 57 color.r = SkColorGetR(sKColor); |
50 color.g = SkColorGetG(sKColor); | 58 color.g = SkColorGetG(sKColor); |
51 color.b = SkColorGetB(sKColor); | 59 color.b = SkColorGetB(sKColor); |
52 color.a = SkColorGetA(sKColor); | 60 color.a = SkColorGetA(sKColor); |
53 return color; | 61 return color; |
54 } | 62 } |
55 | 63 |
56 } // namespace | 64 } // namespace |
57 | 65 |
58 InstantService::InstantService(Profile* profile) | 66 InstantService::InstantService(Profile* profile) |
59 : profile_(profile), | 67 : profile_(profile), |
60 ntp_prerenderer_(profile, profile->GetPrefs()), | 68 ntp_prerenderer_(profile, this, profile->GetPrefs()), |
61 browser_instant_controller_object_count_(0), | 69 browser_instant_controller_object_count_(0), |
62 weak_ptr_factory_(this) { | 70 weak_ptr_factory_(this) { |
63 // Stub for unit tests. | 71 // Stub for unit tests. |
64 if (!BrowserThread::CurrentlyOn(BrowserThread::UI)) | 72 if (!BrowserThread::CurrentlyOn(BrowserThread::UI)) |
65 return; | 73 return; |
66 | 74 |
67 registrar_.Add(this, | 75 registrar_.Add(this, |
68 content::NOTIFICATION_RENDERER_PROCESS_TERMINATED, | 76 content::NOTIFICATION_RENDERER_PROCESS_TERMINATED, |
69 content::NotificationService::AllSources()); | 77 content::NotificationService::AllSources()); |
70 | 78 |
(...skipping 12 matching lines...) Expand all 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, false)); | 104 |
97 content::URLDataSource::Add(profile, new ThumbnailSource(profile, true)); | 105 content::URLDataSource::Add(profile_, new ThumbnailSource(profile_, false)); |
98 content::URLDataSource::Add(profile, new FaviconSource( | 106 content::URLDataSource::Add(profile_, new ThumbnailSource(profile_, true)); |
99 profile, FaviconSource::FAVICON)); | 107 content::URLDataSource::Add( |
100 content::URLDataSource::Add(profile, new LocalNtpSource(profile)); | 108 profile_, new FaviconSource(profile_, FaviconSource::FAVICON)); |
101 content::URLDataSource::Add(profile, new MostVisitedIframeSource()); | 109 content::URLDataSource::Add(profile_, new LocalNtpSource(profile_)); |
| 110 content::URLDataSource::Add(profile_, new MostVisitedIframeSource()); |
| 111 |
| 112 profile_pref_registrar_.Init(profile_->GetPrefs()); |
| 113 profile_pref_registrar_.Add( |
| 114 prefs::kDefaultSearchProviderID, |
| 115 base::Bind(&InstantService::OnDefaultSearchProviderChanged, |
| 116 base::Unretained(this))); |
| 117 |
| 118 registrar_.Add(this, chrome::NOTIFICATION_GOOGLE_URL_UPDATED, |
| 119 content::Source<Profile>(profile_->GetOriginalProfile())); |
102 registrar_.Add(this, chrome::NOTIFICATION_PROFILE_DESTROYED, | 120 registrar_.Add(this, chrome::NOTIFICATION_PROFILE_DESTROYED, |
103 content::Source<Profile>(profile_)); | 121 content::Source<Profile>(profile_)); |
104 } | 122 } |
105 | 123 |
106 InstantService::~InstantService() { | 124 InstantService::~InstantService() { |
107 } | 125 } |
108 | 126 |
109 void InstantService::AddInstantProcess(int process_id) { | 127 void InstantService::AddInstantProcess(int process_id) { |
110 process_ids_.insert(process_id); | 128 process_ids_.insert(process_id); |
111 | 129 |
(...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
188 return ntp_prerenderer_.GetNTPContents(); | 206 return ntp_prerenderer_.GetNTPContents(); |
189 } | 207 } |
190 | 208 |
191 void InstantService::OnBrowserInstantControllerCreated() { | 209 void InstantService::OnBrowserInstantControllerCreated() { |
192 if (profile_->IsOffTheRecord()) | 210 if (profile_->IsOffTheRecord()) |
193 return; | 211 return; |
194 | 212 |
195 ++browser_instant_controller_object_count_; | 213 ++browser_instant_controller_object_count_; |
196 | 214 |
197 if (browser_instant_controller_object_count_ == 1) | 215 if (browser_instant_controller_object_count_ == 1) |
198 ntp_prerenderer_.PreloadInstantNTP(); | 216 ntp_prerenderer_.ReloadInstantNTP(); |
199 } | 217 } |
200 | 218 |
201 void InstantService::OnBrowserInstantControllerDestroyed() { | 219 void InstantService::OnBrowserInstantControllerDestroyed() { |
202 if (profile_->IsOffTheRecord()) | 220 if (profile_->IsOffTheRecord()) |
203 return; | 221 return; |
204 | 222 |
205 DCHECK_GT(browser_instant_controller_object_count_, 0U); | 223 DCHECK_GT(browser_instant_controller_object_count_, 0U); |
206 --browser_instant_controller_object_count_; | 224 --browser_instant_controller_object_count_; |
207 | 225 |
208 // All browser windows have closed, so release the InstantNTP resources to | 226 // All browser windows have closed, so release the InstantNTP resources to |
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
248 case chrome::NOTIFICATION_PROFILE_DESTROYED: { | 266 case chrome::NOTIFICATION_PROFILE_DESTROYED: { |
249 // Last chance to delete InstantNTP contents. We generally delete | 267 // Last chance to delete InstantNTP contents. We generally delete |
250 // preloaded InstantNTP when the last BrowserInstantController object is | 268 // preloaded InstantNTP when the last BrowserInstantController object is |
251 // destroyed. When the browser shutdown happens without closing browsers, | 269 // destroyed. When the browser shutdown happens without closing browsers, |
252 // there is a race condition between BrowserInstantController destruction | 270 // there is a race condition between BrowserInstantController destruction |
253 // and Profile destruction. | 271 // and Profile destruction. |
254 if (GetNTPContents()) | 272 if (GetNTPContents()) |
255 ntp_prerenderer_.DeleteNTPContents(); | 273 ntp_prerenderer_.DeleteNTPContents(); |
256 break; | 274 break; |
257 } | 275 } |
| 276 case chrome::NOTIFICATION_GOOGLE_URL_UPDATED: { |
| 277 OnGoogleURLUpdated( |
| 278 content::Source<Profile>(source).ptr(), |
| 279 content::Details<GoogleURLTracker::UpdatedDetails>(details).ptr()); |
| 280 break; |
| 281 } |
258 default: | 282 default: |
259 NOTREACHED() << "Unexpected notification type in InstantService."; | 283 NOTREACHED() << "Unexpected notification type in InstantService."; |
260 } | 284 } |
261 } | 285 } |
262 | 286 |
263 void InstantService::OnMostVisitedItemsReceived( | 287 void InstantService::OnMostVisitedItemsReceived( |
264 const history::MostVisitedURLList& data) { | 288 const history::MostVisitedURLList& data) { |
265 history::MostVisitedURLList reordered_data(data); | 289 history::MostVisitedURLList reordered_data(data); |
266 history::MostVisitedTilesExperiment::MaybeShuffle(&reordered_data); | 290 history::MostVisitedTilesExperiment::MaybeShuffle(&reordered_data); |
267 | 291 |
(...skipping 116 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
384 theme_info_->image_height = image->height(); | 408 theme_info_->image_height = image->height(); |
385 | 409 |
386 theme_info_->has_attribution = | 410 theme_info_->has_attribution = |
387 theme_service->HasCustomImage(IDR_THEME_NTP_ATTRIBUTION); | 411 theme_service->HasCustomImage(IDR_THEME_NTP_ATTRIBUTION); |
388 } | 412 } |
389 | 413 |
390 FOR_EACH_OBSERVER(InstantServiceObserver, observers_, | 414 FOR_EACH_OBSERVER(InstantServiceObserver, observers_, |
391 ThemeInfoChanged(*theme_info_)); | 415 ThemeInfoChanged(*theme_info_)); |
392 } | 416 } |
393 | 417 |
| 418 void InstantService::OnGoogleURLUpdated( |
| 419 Profile* profile, |
| 420 GoogleURLTracker::UpdatedDetails* details) { |
| 421 GURL last_prompted_url( |
| 422 profile->GetPrefs()->GetString(prefs::kLastPromptedGoogleURL)); |
| 423 |
| 424 // See GoogleURLTracker::OnURLFetchComplete(). |
| 425 // last_prompted_url.is_empty() indicates very first run of Chrome. So there |
| 426 // is no need to notify, as there won't be any old state. |
| 427 if (last_prompted_url.is_empty()) |
| 428 return; |
| 429 |
| 430 // Only the scheme changed. Ignore it since we do not prompt the user in this |
| 431 // case. |
| 432 if (net::StripWWWFromHost(details->first) == |
| 433 net::StripWWWFromHost(details->second)) |
| 434 return; |
| 435 |
| 436 FOR_EACH_OBSERVER(InstantServiceObserver, observers_, GoogleURLUpdated()); |
| 437 } |
| 438 |
| 439 void InstantService::OnDefaultSearchProviderChanged( |
| 440 const std::string& pref_name) { |
| 441 DCHECK_EQ(pref_name, std::string(prefs::kDefaultSearchProviderID)); |
| 442 const TemplateURL* template_url = TemplateURLServiceFactory::GetForProfile( |
| 443 profile_)->GetDefaultSearchProvider(); |
| 444 if (!template_url) { |
| 445 // A NULL |template_url| could mean either this notification is sent during |
| 446 // the browser start up operation or the user now has no default search |
| 447 // provider. There is no way for the user to reach this state using the |
| 448 // Chrome settings. Only explicitly poking at the DB or bugs in the Sync |
| 449 // could cause that, neither of which we support. |
| 450 return; |
| 451 } |
| 452 FOR_EACH_OBSERVER( |
| 453 InstantServiceObserver, observers_, DefaultSearchProviderChanged()); |
| 454 } |
| 455 |
394 InstantNTPPrerenderer* InstantService::ntp_prerenderer() { | 456 InstantNTPPrerenderer* InstantService::ntp_prerenderer() { |
395 return &ntp_prerenderer_; | 457 return &ntp_prerenderer_; |
396 } | 458 } |
OLD | NEW |