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
3 // found in the LICENSE file.
5 #include "chrome/browser/search/instant_service.h"
7 #include "chrome/browser/chrome_notification_types.h"
8 #include "chrome/browser/history/top_sites_factory.h"
9 #include "chrome/browser/profiles/profile.h"
10 #include "chrome/browser/search/instant_io_context.h"
11 #include "chrome/browser/search/instant_service_observer.h"
12 #include "chrome/browser/search/most_visited_iframe_source.h"
13 #include "chrome/browser/search/search.h"
14 #include "chrome/browser/search/suggestions/suggestions_source.h"
15 #include "chrome/browser/search_engines/template_url_service_factory.h"
16 #include "chrome/browser/search_engines/ui_thread_search_terms_data.h"
17 #include "chrome/browser/themes/theme_properties.h"
18 #include "chrome/browser/themes/theme_service.h"
19 #include "chrome/browser/themes/theme_service_factory.h"
20 #include "chrome/browser/thumbnails/thumbnail_list_source.h"
21 #include "chrome/browser/ui/search/instant_search_prerenderer.h"
22 #include "chrome/browser/ui/webui/fallback_icon_source.h"
23 #include "chrome/browser/ui/webui/favicon_source.h"
24 #include "chrome/browser/ui/webui/large_icon_source.h"
25 #include "chrome/browser/ui/webui/ntp/thumbnail_source.h"
26 #include "chrome/browser/ui/webui/theme_source.h"
27 #include "chrome/common/render_messages.h"
28 #include "components/history/core/browser/top_sites.h"
29 #include "components/search_engines/template_url_service.h"
30 #include "content/public/browser/browser_thread.h"
31 #include "content/public/browser/notification_service.h"
32 #include "content/public/browser/notification_types.h"
33 #include "content/public/browser/render_process_host.h"
34 #include "content/public/browser/url_data_source.h"
35 #include "grit/theme_resources.h"
36 #include "third_party/skia/include/core/SkColor.h"
37 #include "ui/gfx/color_utils.h"
38 #include "ui/gfx/image/image_skia.h"
39 #include "ui/gfx/sys_color_change_listener.h"
41 #if !defined(OS_ANDROID)
42 #include "chrome/browser/search/local_ntp_source.h"
47 const int kSectionBorderAlphaTransparency
= 80;
49 // Converts SkColor to RGBAColor
50 RGBAColor
SkColorToRGBAColor(const SkColor
& sKColor
) {
52 color
.r
= SkColorGetR(sKColor
);
53 color
.g
= SkColorGetG(sKColor
);
54 color
.b
= SkColorGetB(sKColor
);
55 color
.a
= SkColorGetA(sKColor
);
61 InstantService::InstantService(Profile
* profile
)
63 template_url_service_(TemplateURLServiceFactory::GetForProfile(profile_
)),
64 omnibox_start_margin_(chrome::kDisableStartMargin
),
65 weak_ptr_factory_(this) {
66 // The initialization below depends on a typical set of browser threads. Skip
67 // it if we are running in a unit test without the full suite.
68 if (!content::BrowserThread::CurrentlyOn(content::BrowserThread::UI
))
71 // This depends on the existence of the typical browser threads. Therefore it
72 // is only instantiated here (after the check for a UI thread above).
73 instant_io_context_
= new InstantIOContext();
75 previous_google_base_url_
=
76 GURL(UIThreadSearchTermsData(profile
).GoogleBaseURLValue());
78 // TemplateURLService is NULL by default in tests.
79 if (template_url_service_
) {
80 template_url_service_
->AddObserver(this);
81 const TemplateURL
* default_search_provider
=
82 template_url_service_
->GetDefaultSearchProvider();
83 if (default_search_provider
) {
84 previous_default_search_provider_
.reset(
85 new TemplateURLData(default_search_provider
->data()));
89 ResetInstantSearchPrerenderer();
92 content::NOTIFICATION_RENDERER_PROCESS_CREATED
,
93 content::NotificationService::AllSources());
95 content::NOTIFICATION_RENDERER_PROCESS_TERMINATED
,
96 content::NotificationService::AllSources());
98 scoped_refptr
<history::TopSites
> top_sites
=
99 TopSitesFactory::GetForProfile(profile_
);
101 top_sites
->AddObserver(this);
103 if (profile_
&& profile_
->GetResourceContext()) {
104 content::BrowserThread::PostTask(
105 content::BrowserThread::IO
, FROM_HERE
,
106 base::Bind(&InstantIOContext::SetUserDataOnIO
,
107 profile
->GetResourceContext(), instant_io_context_
));
110 // Set up the data sources that Instant uses on the NTP.
111 #if defined(ENABLE_THEMES)
112 // Listen for theme installation.
113 registrar_
.Add(this, chrome::NOTIFICATION_BROWSER_THEME_CHANGED
,
114 content::Source
<ThemeService
>(
115 ThemeServiceFactory::GetForProfile(profile_
)));
117 content::URLDataSource::Add(profile_
, new ThemeSource(profile_
));
118 #endif // defined(ENABLE_THEMES)
120 // TODO(aurimas) remove this #if once instant_service.cc is no longer compiled
122 #if !defined(OS_ANDROID)
123 content::URLDataSource::Add(profile_
, new LocalNtpSource(profile_
));
124 content::URLDataSource::Add(profile_
, new ThumbnailSource(profile_
, false));
125 content::URLDataSource::Add(profile_
, new ThumbnailSource(profile_
, true));
126 content::URLDataSource::Add(profile_
, new ThumbnailListSource(profile_
));
127 #endif // !defined(OS_ANDROID)
129 content::URLDataSource::Add(profile_
, new LargeIconSource(profile
));
130 content::URLDataSource::Add(profile_
, new FallbackIconSource());
131 content::URLDataSource::Add(
132 profile_
, new FaviconSource(profile_
, FaviconSource::FAVICON
));
133 content::URLDataSource::Add(profile_
, new MostVisitedIframeSource());
134 content::URLDataSource::Add(
135 profile_
, new suggestions::SuggestionsSource(profile_
));
138 InstantService::~InstantService() {
139 if (template_url_service_
)
140 template_url_service_
->RemoveObserver(this);
143 void InstantService::AddInstantProcess(int process_id
) {
144 process_ids_
.insert(process_id
);
146 if (instant_io_context_
.get()) {
147 content::BrowserThread::PostTask(
148 content::BrowserThread::IO
, FROM_HERE
,
149 base::Bind(&InstantIOContext::AddInstantProcessOnIO
,
150 instant_io_context_
, process_id
));
154 bool InstantService::IsInstantProcess(int process_id
) const {
155 return process_ids_
.find(process_id
) != process_ids_
.end();
158 void InstantService::AddObserver(InstantServiceObserver
* observer
) {
159 observers_
.AddObserver(observer
);
162 void InstantService::RemoveObserver(InstantServiceObserver
* observer
) {
163 observers_
.RemoveObserver(observer
);
166 void InstantService::DeleteMostVisitedItem(const GURL
& url
) {
167 scoped_refptr
<history::TopSites
> top_sites
=
168 TopSitesFactory::GetForProfile(profile_
);
172 top_sites
->AddBlacklistedURL(url
);
175 void InstantService::UndoMostVisitedDeletion(const GURL
& url
) {
176 scoped_refptr
<history::TopSites
> top_sites
=
177 TopSitesFactory::GetForProfile(profile_
);
181 top_sites
->RemoveBlacklistedURL(url
);
184 void InstantService::UndoAllMostVisitedDeletions() {
185 scoped_refptr
<history::TopSites
> top_sites
=
186 TopSitesFactory::GetForProfile(profile_
);
190 top_sites
->ClearBlacklistedURLs();
193 void InstantService::UpdateThemeInfo() {
194 // Update theme background info.
195 // Initialize |theme_info| if necessary.
197 OnThemeChanged(ThemeServiceFactory::GetForProfile(profile_
));
199 OnThemeChanged(NULL
);
202 void InstantService::UpdateMostVisitedItemsInfo() {
203 NotifyAboutMostVisitedItems();
206 void InstantService::Shutdown() {
207 process_ids_
.clear();
209 if (instant_io_context_
.get()) {
210 content::BrowserThread::PostTask(
211 content::BrowserThread::IO
, FROM_HERE
,
212 base::Bind(&InstantIOContext::ClearInstantProcessesOnIO
,
213 instant_io_context_
));
216 scoped_refptr
<history::TopSites
> top_sites
=
217 TopSitesFactory::GetForProfile(profile_
);
219 top_sites
->RemoveObserver(this);
221 instant_io_context_
= NULL
;
224 void InstantService::Observe(int type
,
225 const content::NotificationSource
& source
,
226 const content::NotificationDetails
& details
) {
228 case content::NOTIFICATION_RENDERER_PROCESS_CREATED
:
229 SendSearchURLsToRenderer(
230 content::Source
<content::RenderProcessHost
>(source
).ptr());
232 case content::NOTIFICATION_RENDERER_PROCESS_TERMINATED
:
233 OnRendererProcessTerminated(
234 content::Source
<content::RenderProcessHost
>(source
)->GetID());
236 #if defined(ENABLE_THEMES)
237 case chrome::NOTIFICATION_BROWSER_THEME_CHANGED
: {
238 OnThemeChanged(content::Source
<ThemeService
>(source
).ptr());
241 #endif // defined(ENABLE_THEMES)
243 NOTREACHED() << "Unexpected notification type in InstantService.";
247 void InstantService::SendSearchURLsToRenderer(content::RenderProcessHost
* rph
) {
248 rph
->Send(new ChromeViewMsg_SetSearchURLs(
249 chrome::GetSearchURLs(profile_
), chrome::GetNewTabPageURL(profile_
)));
252 void InstantService::OnOmniboxStartMarginChanged(int start_margin
) {
253 omnibox_start_margin_
= start_margin
;
254 FOR_EACH_OBSERVER(InstantServiceObserver
, observers_
,
255 OmniboxStartMarginChanged(omnibox_start_margin_
));
258 void InstantService::OnRendererProcessTerminated(int process_id
) {
259 process_ids_
.erase(process_id
);
261 if (instant_io_context_
.get()) {
262 content::BrowserThread::PostTask(
263 content::BrowserThread::IO
, FROM_HERE
,
264 base::Bind(&InstantIOContext::RemoveInstantProcessOnIO
,
265 instant_io_context_
, process_id
));
269 void InstantService::OnMostVisitedItemsReceived(
270 const history::MostVisitedURLList
& data
) {
271 history::MostVisitedURLList
reordered_data(data
);
272 std::vector
<InstantMostVisitedItem
> new_most_visited_items
;
273 for (size_t i
= 0; i
< reordered_data
.size(); i
++) {
274 const history::MostVisitedURL
& url
= reordered_data
[i
];
275 InstantMostVisitedItem item
;
277 item
.title
= url
.title
;
278 new_most_visited_items
.push_back(item
);
281 most_visited_items_
= new_most_visited_items
;
282 NotifyAboutMostVisitedItems();
285 void InstantService::NotifyAboutMostVisitedItems() {
286 FOR_EACH_OBSERVER(InstantServiceObserver
, observers_
,
287 MostVisitedItemsChanged(most_visited_items_
));
290 void InstantService::OnThemeChanged(ThemeService
* theme_service
) {
291 if (!theme_service
) {
292 DCHECK(theme_info_
.get());
293 FOR_EACH_OBSERVER(InstantServiceObserver
, observers_
,
294 ThemeInfoChanged(*theme_info_
));
298 // Get theme information from theme service.
299 theme_info_
.reset(new ThemeBackgroundInfo());
301 // Get if the current theme is the default theme.
302 theme_info_
->using_default_theme
= theme_service
->UsingDefaultTheme();
305 SkColor background_color
=
306 theme_service
->GetColor(ThemeProperties::COLOR_NTP_BACKGROUND
);
308 theme_service
->GetColor(ThemeProperties::COLOR_NTP_TEXT
);
310 theme_service
->GetColor(ThemeProperties::COLOR_NTP_LINK
);
311 SkColor text_color_light
=
312 theme_service
->GetColor(ThemeProperties::COLOR_NTP_TEXT_LIGHT
);
313 SkColor header_color
=
314 theme_service
->GetColor(ThemeProperties::COLOR_NTP_HEADER
);
315 // Generate section border color from the header color.
316 SkColor section_border_color
=
317 SkColorSetARGB(kSectionBorderAlphaTransparency
,
318 SkColorGetR(header_color
),
319 SkColorGetG(header_color
),
320 SkColorGetB(header_color
));
322 // Invert colors if needed.
323 if (gfx::IsInvertedColorScheme()) {
324 background_color
= color_utils::InvertColor(background_color
);
325 text_color
= color_utils::InvertColor(text_color
);
326 link_color
= color_utils::InvertColor(link_color
);
327 text_color_light
= color_utils::InvertColor(text_color_light
);
328 header_color
= color_utils::InvertColor(header_color
);
329 section_border_color
= color_utils::InvertColor(section_border_color
);
333 theme_info_
->background_color
= SkColorToRGBAColor(background_color
);
334 theme_info_
->text_color
= SkColorToRGBAColor(text_color
);
335 theme_info_
->link_color
= SkColorToRGBAColor(link_color
);
336 theme_info_
->text_color_light
= SkColorToRGBAColor(text_color_light
);
337 theme_info_
->header_color
= SkColorToRGBAColor(header_color
);
338 theme_info_
->section_border_color
= SkColorToRGBAColor(section_border_color
);
340 int logo_alternate
= theme_service
->GetDisplayProperty(
341 ThemeProperties::NTP_LOGO_ALTERNATE
);
342 theme_info_
->logo_alternate
= logo_alternate
== 1;
344 if (theme_service
->HasCustomImage(IDR_THEME_NTP_BACKGROUND
)) {
345 // Set theme id for theme background image url.
346 theme_info_
->theme_id
= theme_service
->GetThemeID();
348 // Set theme background image horizontal alignment.
349 int alignment
= theme_service
->GetDisplayProperty(
350 ThemeProperties::NTP_BACKGROUND_ALIGNMENT
);
351 if (alignment
& ThemeProperties::ALIGN_LEFT
)
352 theme_info_
->image_horizontal_alignment
= THEME_BKGRND_IMAGE_ALIGN_LEFT
;
353 else if (alignment
& ThemeProperties::ALIGN_RIGHT
)
354 theme_info_
->image_horizontal_alignment
= THEME_BKGRND_IMAGE_ALIGN_RIGHT
;
356 theme_info_
->image_horizontal_alignment
= THEME_BKGRND_IMAGE_ALIGN_CENTER
;
358 // Set theme background image vertical alignment.
359 if (alignment
& ThemeProperties::ALIGN_TOP
)
360 theme_info_
->image_vertical_alignment
= THEME_BKGRND_IMAGE_ALIGN_TOP
;
361 else if (alignment
& ThemeProperties::ALIGN_BOTTOM
)
362 theme_info_
->image_vertical_alignment
= THEME_BKGRND_IMAGE_ALIGN_BOTTOM
;
364 theme_info_
->image_vertical_alignment
= THEME_BKGRND_IMAGE_ALIGN_CENTER
;
366 // Set theme backgorund image tiling.
367 int tiling
= theme_service
->GetDisplayProperty(
368 ThemeProperties::NTP_BACKGROUND_TILING
);
370 case ThemeProperties::NO_REPEAT
:
371 theme_info_
->image_tiling
= THEME_BKGRND_IMAGE_NO_REPEAT
;
373 case ThemeProperties::REPEAT_X
:
374 theme_info_
->image_tiling
= THEME_BKGRND_IMAGE_REPEAT_X
;
376 case ThemeProperties::REPEAT_Y
:
377 theme_info_
->image_tiling
= THEME_BKGRND_IMAGE_REPEAT_Y
;
379 case ThemeProperties::REPEAT
:
380 theme_info_
->image_tiling
= THEME_BKGRND_IMAGE_REPEAT
;
384 // Set theme background image height.
385 gfx::ImageSkia
* image
= theme_service
->GetImageSkiaNamed(
386 IDR_THEME_NTP_BACKGROUND
);
388 theme_info_
->image_height
= image
->height();
390 theme_info_
->has_attribution
=
391 theme_service
->HasCustomImage(IDR_THEME_NTP_ATTRIBUTION
);
394 FOR_EACH_OBSERVER(InstantServiceObserver
, observers_
,
395 ThemeInfoChanged(*theme_info_
));
398 void InstantService::OnTemplateURLServiceChanged() {
399 // Check whether the default search provider was changed.
400 const TemplateURL
* template_url
=
401 template_url_service_
->GetDefaultSearchProvider();
402 bool default_search_provider_changed
= !TemplateURL::MatchesData(
403 template_url
, previous_default_search_provider_
.get(),
404 UIThreadSearchTermsData(profile_
));
405 if (default_search_provider_changed
) {
406 previous_default_search_provider_
.reset(
407 template_url
? new TemplateURLData(template_url
->data()) : NULL
);
410 // Note that, even if the TemplateURL for the Default Search Provider has not
411 // changed, the effective URLs might change if they reference the Google base
412 // URL. The TemplateURLService will notify us when the effective URL changes
413 // in this way but it's up to us to do the work to check both.
414 bool google_base_url_domain_changed
= false;
415 GURL
google_base_url(UIThreadSearchTermsData(profile_
).GoogleBaseURLValue());
416 if (google_base_url
!= previous_google_base_url_
) {
417 previous_google_base_url_
= google_base_url
;
418 if (template_url
&& template_url
->HasGoogleBaseURLs(
419 UIThreadSearchTermsData(profile_
)))
420 google_base_url_domain_changed
= true;
423 if (default_search_provider_changed
|| google_base_url_domain_changed
) {
424 ResetInstantSearchPrerenderer();
426 InstantServiceObserver
, observers_
,
427 DefaultSearchProviderChanged(google_base_url_domain_changed
));
431 void InstantService::TopSitesLoaded(history::TopSites
* top_sites
) {
434 void InstantService::TopSitesChanged(history::TopSites
* top_sites
) {
435 top_sites
->GetMostVisitedURLs(
436 base::Bind(&InstantService::OnMostVisitedItemsReceived
,
437 weak_ptr_factory_
.GetWeakPtr()),
441 void InstantService::ResetInstantSearchPrerenderer() {
442 if (!chrome::ShouldPrefetchSearchResults())
445 GURL
url(chrome::GetSearchResultPrefetchBaseURL(profile_
));
446 instant_prerenderer_
.reset(
447 url
.is_valid() ? new InstantSearchPrerenderer(profile_
, url
) : NULL
);