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/most_visited_tiles_experiment.h"
9 #include "chrome/browser/history/top_sites.h"
10 #include "chrome/browser/profiles/profile.h"
11 #include "chrome/browser/search/instant_io_context.h"
12 #include "chrome/browser/search/instant_service_observer.h"
13 #include "chrome/browser/search/local_ntp_source.h"
14 #include "chrome/browser/search/most_visited_iframe_source.h"
15 #include "chrome/browser/search/search.h"
16 #include "chrome/browser/search/suggestions/suggestions_source.h"
17 #include "chrome/browser/search_engines/search_terms_data.h"
18 #include "chrome/browser/search_engines/template_url_service.h"
19 #include "chrome/browser/search_engines/template_url_service_factory.h"
20 #include "chrome/browser/themes/theme_properties.h"
21 #include "chrome/browser/themes/theme_service.h"
22 #include "chrome/browser/themes/theme_service_factory.h"
23 #include "chrome/browser/thumbnails/thumbnail_list_source.h"
24 #include "chrome/browser/ui/search/instant_search_prerenderer.h"
25 #include "chrome/browser/ui/webui/favicon_source.h"
26 #include "chrome/browser/ui/webui/ntp/thumbnail_source.h"
27 #include "chrome/browser/ui/webui/theme_source.h"
28 #include "chrome/common/render_messages.h"
29 #include "content/public/browser/browser_thread.h"
30 #include "content/public/browser/notification_service.h"
31 #include "content/public/browser/notification_types.h"
32 #include "content/public/browser/render_process_host.h"
33 #include "content/public/browser/url_data_source.h"
34 #include "grit/theme_resources.h"
35 #include "third_party/skia/include/core/SkColor.h"
36 #include "ui/gfx/color_utils.h"
37 #include "ui/gfx/image/image_skia.h"
38 #include "ui/gfx/sys_color_change_listener.h"
43 const int kSectionBorderAlphaTransparency = 80;
45 // Converts SkColor to RGBAColor
46 RGBAColor SkColorToRGBAColor(const SkColor& sKColor) {
48 color.r = SkColorGetR(sKColor);
49 color.g = SkColorGetG(sKColor);
50 color.b = SkColorGetB(sKColor);
51 color.a = SkColorGetA(sKColor);
57 InstantService::InstantService(Profile* profile)
59 template_url_service_(TemplateURLServiceFactory::GetForProfile(profile_)),
60 omnibox_start_margin_(chrome::kDisableStartMargin),
61 weak_ptr_factory_(this) {
62 // The initialization below depends on a typical set of browser threads. Skip
63 // it if we are running in a unit test without the full suite.
64 if (!content::BrowserThread::CurrentlyOn(content::BrowserThread::UI))
67 // This depends on the existence of the typical browser threads. Therefore it
68 // is only instantiated here (after the check for a UI thread above).
69 instant_io_context_ = new InstantIOContext();
71 previous_google_base_url_ =
72 GURL(UIThreadSearchTermsData(profile).GoogleBaseURLValue());
74 // TemplateURLService is NULL by default in tests.
75 if (template_url_service_) {
76 template_url_service_->AddObserver(this);
77 const TemplateURL* default_search_provider =
78 template_url_service_->GetDefaultSearchProvider();
79 if (default_search_provider) {
80 previous_default_search_provider_.reset(
81 new TemplateURLData(default_search_provider->data()));
85 ResetInstantSearchPrerenderer();
88 content::NOTIFICATION_RENDERER_PROCESS_CREATED,
89 content::NotificationService::AllSources());
91 content::NOTIFICATION_RENDERER_PROCESS_TERMINATED,
92 content::NotificationService::AllSources());
94 history::TopSites* top_sites = profile_->GetTopSites();
97 chrome::NOTIFICATION_TOP_SITES_CHANGED,
98 content::Source<history::TopSites>(top_sites));
101 if (profile_ && profile_->GetResourceContext()) {
102 content::BrowserThread::PostTask(
103 content::BrowserThread::IO, FROM_HERE,
104 base::Bind(&InstantIOContext::SetUserDataOnIO,
105 profile->GetResourceContext(), instant_io_context_));
108 // Set up the data sources that Instant uses on the NTP.
109 #if defined(ENABLE_THEMES)
110 // Listen for theme installation.
111 registrar_.Add(this, chrome::NOTIFICATION_BROWSER_THEME_CHANGED,
112 content::Source<ThemeService>(
113 ThemeServiceFactory::GetForProfile(profile_)));
115 content::URLDataSource::Add(profile_, new ThemeSource(profile_));
116 #endif // defined(ENABLE_THEMES)
118 // TODO(aurimas) remove this #if once instant_service.cc is no longer compiled
120 #if !defined(OS_ANDROID)
121 content::URLDataSource::Add(profile_, new ThumbnailSource(profile_, false));
122 content::URLDataSource::Add(profile_, new ThumbnailSource(profile_, true));
123 content::URLDataSource::Add(profile_, new ThumbnailListSource(profile_));
124 #endif // !defined(OS_ANDROID)
126 content::URLDataSource::Add(
127 profile_, new FaviconSource(profile_, FaviconSource::FAVICON));
128 content::URLDataSource::Add(profile_, new LocalNtpSource(profile_));
129 content::URLDataSource::Add(profile_, new MostVisitedIframeSource());
130 content::URLDataSource::Add(
131 profile_, new suggestions::SuggestionsSource(profile_));
134 InstantService::~InstantService() {
135 if (template_url_service_)
136 template_url_service_->RemoveObserver(this);
139 void InstantService::AddInstantProcess(int process_id) {
140 process_ids_.insert(process_id);
142 if (instant_io_context_.get()) {
143 content::BrowserThread::PostTask(
144 content::BrowserThread::IO, FROM_HERE,
145 base::Bind(&InstantIOContext::AddInstantProcessOnIO,
146 instant_io_context_, process_id));
150 bool InstantService::IsInstantProcess(int process_id) const {
151 return process_ids_.find(process_id) != process_ids_.end();
154 void InstantService::AddObserver(InstantServiceObserver* observer) {
155 observers_.AddObserver(observer);
158 void InstantService::RemoveObserver(InstantServiceObserver* observer) {
159 observers_.RemoveObserver(observer);
162 void InstantService::DeleteMostVisitedItem(const GURL& url) {
163 history::TopSites* top_sites = profile_->GetTopSites();
167 top_sites->AddBlacklistedURL(url);
170 void InstantService::UndoMostVisitedDeletion(const GURL& url) {
171 history::TopSites* top_sites = profile_->GetTopSites();
175 top_sites->RemoveBlacklistedURL(url);
178 void InstantService::UndoAllMostVisitedDeletions() {
179 history::TopSites* top_sites = profile_->GetTopSites();
183 top_sites->ClearBlacklistedURLs();
186 void InstantService::UpdateThemeInfo() {
187 // Update theme background info.
188 // Initialize |theme_info| if necessary.
190 OnThemeChanged(ThemeServiceFactory::GetForProfile(profile_));
192 OnThemeChanged(NULL);
195 void InstantService::UpdateMostVisitedItemsInfo() {
196 NotifyAboutMostVisitedItems();
199 void InstantService::Shutdown() {
200 process_ids_.clear();
202 if (instant_io_context_.get()) {
203 content::BrowserThread::PostTask(
204 content::BrowserThread::IO, FROM_HERE,
205 base::Bind(&InstantIOContext::ClearInstantProcessesOnIO,
206 instant_io_context_));
208 instant_io_context_ = NULL;
211 void InstantService::Observe(int type,
212 const content::NotificationSource& source,
213 const content::NotificationDetails& details) {
215 case content::NOTIFICATION_RENDERER_PROCESS_CREATED:
216 SendSearchURLsToRenderer(
217 content::Source<content::RenderProcessHost>(source).ptr());
219 case content::NOTIFICATION_RENDERER_PROCESS_TERMINATED:
220 OnRendererProcessTerminated(
221 content::Source<content::RenderProcessHost>(source)->GetID());
223 case chrome::NOTIFICATION_TOP_SITES_CHANGED: {
224 history::TopSites* top_sites = profile_->GetTopSites();
226 top_sites->GetMostVisitedURLs(
227 base::Bind(&InstantService::OnMostVisitedItemsReceived,
228 weak_ptr_factory_.GetWeakPtr()), false);
232 #if defined(ENABLE_THEMES)
233 case chrome::NOTIFICATION_BROWSER_THEME_CHANGED: {
234 OnThemeChanged(content::Source<ThemeService>(source).ptr());
237 #endif // defined(ENABLE_THEMES)
239 NOTREACHED() << "Unexpected notification type in InstantService.";
243 void InstantService::SendSearchURLsToRenderer(content::RenderProcessHost* rph) {
244 rph->Send(new ChromeViewMsg_SetSearchURLs(
245 chrome::GetSearchURLs(profile_), chrome::GetNewTabPageURL(profile_)));
248 void InstantService::OnOmniboxStartMarginChanged(int start_margin) {
249 omnibox_start_margin_ = start_margin;
250 FOR_EACH_OBSERVER(InstantServiceObserver, observers_,
251 OmniboxStartMarginChanged(omnibox_start_margin_));
254 void InstantService::OnRendererProcessTerminated(int process_id) {
255 process_ids_.erase(process_id);
257 if (instant_io_context_.get()) {
258 content::BrowserThread::PostTask(
259 content::BrowserThread::IO, FROM_HERE,
260 base::Bind(&InstantIOContext::RemoveInstantProcessOnIO,
261 instant_io_context_, process_id));
265 void InstantService::OnMostVisitedItemsReceived(
266 const history::MostVisitedURLList& data) {
267 history::MostVisitedURLList reordered_data(data);
268 history::MostVisitedTilesExperiment::MaybeShuffle(&reordered_data);
270 std::vector<InstantMostVisitedItem> new_most_visited_items;
271 for (size_t i = 0; i < reordered_data.size(); i++) {
272 const history::MostVisitedURL& url = reordered_data[i];
273 InstantMostVisitedItem item;
275 item.title = url.title;
276 new_most_visited_items.push_back(item);
279 most_visited_items_ = new_most_visited_items;
280 NotifyAboutMostVisitedItems();
283 void InstantService::NotifyAboutMostVisitedItems() {
284 FOR_EACH_OBSERVER(InstantServiceObserver, observers_,
285 MostVisitedItemsChanged(most_visited_items_));
288 void InstantService::OnThemeChanged(ThemeService* theme_service) {
289 if (!theme_service) {
290 DCHECK(theme_info_.get());
291 FOR_EACH_OBSERVER(InstantServiceObserver, observers_,
292 ThemeInfoChanged(*theme_info_));
296 // Get theme information from theme service.
297 theme_info_.reset(new ThemeBackgroundInfo());
299 // Get if the current theme is the default theme.
300 theme_info_->using_default_theme = theme_service->UsingDefaultTheme();
303 SkColor background_color =
304 theme_service->GetColor(ThemeProperties::COLOR_NTP_BACKGROUND);
306 theme_service->GetColor(ThemeProperties::COLOR_NTP_TEXT);
308 theme_service->GetColor(ThemeProperties::COLOR_NTP_LINK);
309 SkColor text_color_light =
310 theme_service->GetColor(ThemeProperties::COLOR_NTP_TEXT_LIGHT);
311 SkColor header_color =
312 theme_service->GetColor(ThemeProperties::COLOR_NTP_HEADER);
313 // Generate section border color from the header color.
314 SkColor section_border_color =
315 SkColorSetARGB(kSectionBorderAlphaTransparency,
316 SkColorGetR(header_color),
317 SkColorGetG(header_color),
318 SkColorGetB(header_color));
320 // Invert colors if needed.
321 if (gfx::IsInvertedColorScheme()) {
322 background_color = color_utils::InvertColor(background_color);
323 text_color = color_utils::InvertColor(text_color);
324 link_color = color_utils::InvertColor(link_color);
325 text_color_light = color_utils::InvertColor(text_color_light);
326 header_color = color_utils::InvertColor(header_color);
327 section_border_color = color_utils::InvertColor(section_border_color);
331 theme_info_->background_color = SkColorToRGBAColor(background_color);
332 theme_info_->text_color = SkColorToRGBAColor(text_color);
333 theme_info_->link_color = SkColorToRGBAColor(link_color);
334 theme_info_->text_color_light = SkColorToRGBAColor(text_color_light);
335 theme_info_->header_color = SkColorToRGBAColor(header_color);
336 theme_info_->section_border_color = SkColorToRGBAColor(section_border_color);
338 int logo_alternate = theme_service->GetDisplayProperty(
339 ThemeProperties::NTP_LOGO_ALTERNATE);
340 theme_info_->logo_alternate = logo_alternate == 1;
342 if (theme_service->HasCustomImage(IDR_THEME_NTP_BACKGROUND)) {
343 // Set theme id for theme background image url.
344 theme_info_->theme_id = theme_service->GetThemeID();
346 // Set theme background image horizontal alignment.
347 int alignment = theme_service->GetDisplayProperty(
348 ThemeProperties::NTP_BACKGROUND_ALIGNMENT);
349 if (alignment & ThemeProperties::ALIGN_LEFT)
350 theme_info_->image_horizontal_alignment = THEME_BKGRND_IMAGE_ALIGN_LEFT;
351 else if (alignment & ThemeProperties::ALIGN_RIGHT)
352 theme_info_->image_horizontal_alignment = THEME_BKGRND_IMAGE_ALIGN_RIGHT;
354 theme_info_->image_horizontal_alignment = THEME_BKGRND_IMAGE_ALIGN_CENTER;
356 // Set theme background image vertical alignment.
357 if (alignment & ThemeProperties::ALIGN_TOP)
358 theme_info_->image_vertical_alignment = THEME_BKGRND_IMAGE_ALIGN_TOP;
359 else if (alignment & ThemeProperties::ALIGN_BOTTOM)
360 theme_info_->image_vertical_alignment = THEME_BKGRND_IMAGE_ALIGN_BOTTOM;
362 theme_info_->image_vertical_alignment = THEME_BKGRND_IMAGE_ALIGN_CENTER;
364 // Set theme backgorund image tiling.
365 int tiling = theme_service->GetDisplayProperty(
366 ThemeProperties::NTP_BACKGROUND_TILING);
368 case ThemeProperties::NO_REPEAT:
369 theme_info_->image_tiling = THEME_BKGRND_IMAGE_NO_REPEAT;
371 case ThemeProperties::REPEAT_X:
372 theme_info_->image_tiling = THEME_BKGRND_IMAGE_REPEAT_X;
374 case ThemeProperties::REPEAT_Y:
375 theme_info_->image_tiling = THEME_BKGRND_IMAGE_REPEAT_Y;
377 case ThemeProperties::REPEAT:
378 theme_info_->image_tiling = THEME_BKGRND_IMAGE_REPEAT;
382 // Set theme background image height.
383 gfx::ImageSkia* image = theme_service->GetImageSkiaNamed(
384 IDR_THEME_NTP_BACKGROUND);
386 theme_info_->image_height = image->height();
388 theme_info_->has_attribution =
389 theme_service->HasCustomImage(IDR_THEME_NTP_ATTRIBUTION);
392 FOR_EACH_OBSERVER(InstantServiceObserver, observers_,
393 ThemeInfoChanged(*theme_info_));
396 void InstantService::OnTemplateURLServiceChanged() {
397 // Check whether the default search provider was changed.
398 const TemplateURL* template_url =
399 template_url_service_->GetDefaultSearchProvider();
400 bool default_search_provider_changed = !TemplateURL::MatchesData(
401 template_url, previous_default_search_provider_.get());
402 if (default_search_provider_changed) {
403 previous_default_search_provider_.reset(
404 template_url ? new TemplateURLData(template_url->data()) : NULL);
407 // Note that, even if the TemplateURL for the Default Search Provider has not
408 // changed, the effective URLs might change if they reference the Google base
409 // URL. The TemplateURLService will notify us when the effective URL changes
410 // in this way but it's up to us to do the work to check both.
411 GURL google_base_url(UIThreadSearchTermsData(profile_).GoogleBaseURLValue());
412 if (google_base_url != previous_google_base_url_) {
413 previous_google_base_url_ = google_base_url;
414 if (template_url && template_url->HasGoogleBaseURLs())
415 default_search_provider_changed = true;
418 if (default_search_provider_changed) {
419 ResetInstantSearchPrerenderer();
420 FOR_EACH_OBSERVER(InstantServiceObserver, observers_,
421 DefaultSearchProviderChanged());
425 void InstantService::ResetInstantSearchPrerenderer() {
426 if (!chrome::ShouldPrefetchSearchResults())
429 GURL url(chrome::GetSearchResultPrefetchBaseURL(profile_));
430 instant_prerenderer_.reset(
431 url.is_valid() ? new InstantSearchPrerenderer(profile_, url) : NULL);