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/bind.h" |
9 #include "base/logging.h" | 10 #include "base/logging.h" |
10 #include "base/prefs/pref_service.h" | 11 #include "base/prefs/pref_service.h" |
11 #include "base/strings/string_number_conversions.h" | 12 #include "base/strings/string_number_conversions.h" |
12 #include "chrome/browser/chrome_notification_types.h" | 13 #include "chrome/browser/chrome_notification_types.h" |
13 #include "chrome/browser/history/history_notifications.h" | 14 #include "chrome/browser/history/history_notifications.h" |
14 #include "chrome/browser/history/most_visited_tiles_experiment.h" | 15 #include "chrome/browser/history/most_visited_tiles_experiment.h" |
15 #include "chrome/browser/history/top_sites.h" | 16 #include "chrome/browser/history/top_sites.h" |
16 #include "chrome/browser/profiles/profile.h" | 17 #include "chrome/browser/profiles/profile.h" |
17 #include "chrome/browser/search/instant_io_context.h" | 18 #include "chrome/browser/search/instant_io_context.h" |
18 #include "chrome/browser/search/instant_service_factory.h" | 19 #include "chrome/browser/search/instant_service_factory.h" |
(...skipping 21 matching lines...) Expand all Loading... |
40 #include "content/public/browser/notification_source.h" | 41 #include "content/public/browser/notification_source.h" |
41 #include "content/public/browser/notification_types.h" | 42 #include "content/public/browser/notification_types.h" |
42 #include "content/public/browser/render_process_host.h" | 43 #include "content/public/browser/render_process_host.h" |
43 #include "content/public/browser/url_data_source.h" | 44 #include "content/public/browser/url_data_source.h" |
44 #include "grit/theme_resources.h" | 45 #include "grit/theme_resources.h" |
45 #include "net/base/net_util.h" | 46 #include "net/base/net_util.h" |
46 #include "net/url_request/url_request.h" | 47 #include "net/url_request/url_request.h" |
47 #include "ui/gfx/color_utils.h" | 48 #include "ui/gfx/color_utils.h" |
48 #include "ui/gfx/image/image_skia.h" | 49 #include "ui/gfx/image/image_skia.h" |
49 #include "ui/gfx/sys_color_change_listener.h" | 50 #include "ui/gfx/sys_color_change_listener.h" |
50 #include "url/gurl.h" | |
51 | 51 |
52 using content::BrowserThread; | 52 using content::BrowserThread; |
53 | 53 |
54 namespace { | 54 namespace { |
55 | 55 |
56 const int kSectionBorderAlphaTransparency = 80; | 56 const int kSectionBorderAlphaTransparency = 80; |
57 | 57 |
58 // Converts SkColor to RGBAColor | 58 // Converts SkColor to RGBAColor |
59 RGBAColor SkColorToRGBAColor(const SkColor& sKColor) { | 59 RGBAColor SkColorToRGBAColor(const SkColor& sKColor) { |
60 RGBAColor color; | 60 RGBAColor color; |
61 color.r = SkColorGetR(sKColor); | 61 color.r = SkColorGetR(sKColor); |
62 color.g = SkColorGetG(sKColor); | 62 color.g = SkColorGetG(sKColor); |
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_)) { |
74 // Stub for unit tests. | 76 // Stub for unit tests. |
75 if (!BrowserThread::CurrentlyOn(BrowserThread::UI)) | 77 if (!BrowserThread::CurrentlyOn(BrowserThread::UI)) |
76 return; | 78 return; |
77 | 79 |
| 80 TemplateURLService* template_url_service = |
| 81 TemplateURLServiceFactory::GetForProfile(profile_); |
| 82 if (template_url_service) |
| 83 template_url_service->AddObserver(this); |
| 84 |
78 ResetInstantSearchPrerenderer(); | 85 ResetInstantSearchPrerenderer(); |
79 | 86 |
80 registrar_.Add(this, | 87 registrar_.Add(this, |
81 content::NOTIFICATION_RENDERER_PROCESS_CREATED, | 88 content::NOTIFICATION_RENDERER_PROCESS_CREATED, |
82 content::NotificationService::AllSources()); | 89 content::NotificationService::AllSources()); |
83 registrar_.Add(this, | 90 registrar_.Add(this, |
84 content::NOTIFICATION_RENDERER_PROCESS_TERMINATED, | 91 content::NOTIFICATION_RENDERER_PROCESS_TERMINATED, |
85 content::NotificationService::AllSources()); | 92 content::NotificationService::AllSources()); |
86 | 93 |
87 history::TopSites* top_sites = profile_->GetTopSites(); | 94 history::TopSites* top_sites = profile_->GetTopSites(); |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
119 | 126 |
120 content::URLDataSource::Add( | 127 content::URLDataSource::Add( |
121 profile_, new FaviconSource(profile_, FaviconSource::FAVICON)); | 128 profile_, new FaviconSource(profile_, FaviconSource::FAVICON)); |
122 content::URLDataSource::Add(profile_, new LocalNtpSource(profile_)); | 129 content::URLDataSource::Add(profile_, new LocalNtpSource(profile_)); |
123 content::URLDataSource::Add(profile_, new MostVisitedIframeSource()); | 130 content::URLDataSource::Add(profile_, new MostVisitedIframeSource()); |
124 if (suggestions::SuggestionsService::IsEnabled()) { | 131 if (suggestions::SuggestionsService::IsEnabled()) { |
125 content::URLDataSource::Add( | 132 content::URLDataSource::Add( |
126 profile_, new suggestions::SuggestionsSource(profile_)); | 133 profile_, new suggestions::SuggestionsSource(profile_)); |
127 } | 134 } |
128 | 135 |
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 registrar_.Add(this, chrome::NOTIFICATION_GOOGLE_URL_UPDATED, |
136 content::Source<Profile>(profile_->GetOriginalProfile())); | 137 content::Source<Profile>(profile_->GetOriginalProfile())); |
137 } | 138 } |
138 | 139 |
139 InstantService::~InstantService() { | 140 InstantService::~InstantService() { |
140 } | 141 } |
141 | 142 |
142 void InstantService::AddInstantProcess(int process_id) { | 143 void InstantService::AddInstantProcess(int process_id) { |
143 process_ids_.insert(process_id); | 144 process_ids_.insert(process_id); |
144 | 145 |
(...skipping 277 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
422 | 423 |
423 // Only the scheme changed. Ignore it since we do not prompt the user in this | 424 // Only the scheme changed. Ignore it since we do not prompt the user in this |
424 // case. | 425 // case. |
425 if (net::StripWWWFromHost(details->first) == | 426 if (net::StripWWWFromHost(details->first) == |
426 net::StripWWWFromHost(details->second)) | 427 net::StripWWWFromHost(details->second)) |
427 return; | 428 return; |
428 | 429 |
429 FOR_EACH_OBSERVER(InstantServiceObserver, observers_, GoogleURLUpdated()); | 430 FOR_EACH_OBSERVER(InstantServiceObserver, observers_, GoogleURLUpdated()); |
430 } | 431 } |
431 | 432 |
432 void InstantService::OnDefaultSearchProviderChanged( | 433 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( | 434 const TemplateURL* template_url = TemplateURLServiceFactory::GetForProfile( |
436 profile_)->GetDefaultSearchProvider(); | 435 profile_)->GetDefaultSearchProvider(); |
437 if (!template_url) { | 436 if (!template_url) { |
438 // A NULL |template_url| could mean either this notification is sent during | 437 // 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 | 438 // 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 | 439 // 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 | 440 // Chrome settings. Only explicitly poking at the DB or bugs in the Sync |
442 // could cause that, neither of which we support. | 441 // could cause that, neither of which we support. |
443 return; | 442 return; |
444 } | 443 } |
| 444 GURL new_search_result_prefetch_base_url( |
| 445 chrome::GetSearchResultPrefetchBaseURL(profile_)); |
| 446 if (new_search_result_prefetch_base_url == |
| 447 current_search_result_prefetch_base_url_) |
| 448 return; |
| 449 current_search_result_prefetch_base_url_ = |
| 450 new_search_result_prefetch_base_url; |
445 | 451 |
446 ResetInstantSearchPrerenderer(); | 452 ResetInstantSearchPrerenderer(); |
447 | 453 |
448 FOR_EACH_OBSERVER( | 454 FOR_EACH_OBSERVER( |
449 InstantServiceObserver, observers_, DefaultSearchProviderChanged()); | 455 InstantServiceObserver, observers_, DefaultSearchProviderChanged()); |
450 } | 456 } |
451 | 457 |
452 void InstantService::ResetInstantSearchPrerenderer() { | 458 void InstantService::ResetInstantSearchPrerenderer() { |
453 if (!chrome::ShouldPrefetchSearchResults()) | 459 if (!chrome::ShouldPrefetchSearchResults()) |
454 return; | 460 return; |
455 | 461 |
456 GURL url(chrome::GetSearchResultPrefetchBaseURL(profile_)); | 462 if (current_search_result_prefetch_base_url_.is_valid()) { |
457 if (url.is_valid()) | 463 instant_prerenderer_.reset(new InstantSearchPrerenderer( |
458 instant_prerenderer_.reset(new InstantSearchPrerenderer(profile_, url)); | 464 profile_, current_search_result_prefetch_base_url_)); |
459 else | 465 } else { |
460 instant_prerenderer_.reset(); | 466 instant_prerenderer_.reset(); |
| 467 } |
461 } | 468 } |
OLD | NEW |