ozone: evdev: Sync caps lock LED state to evdev
[chromium-blink-merge.git] / chrome / browser / search / instant_service.cc
blobf02d6561ea173138e6a4e1e400201f291b66d8ec
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/favicon_source.h"
23 #include "chrome/browser/ui/webui/ntp/thumbnail_source.h"
24 #include "chrome/browser/ui/webui/theme_source.h"
25 #include "chrome/common/render_messages.h"
26 #include "components/history/core/browser/top_sites.h"
27 #include "components/search_engines/template_url_service.h"
28 #include "content/public/browser/browser_thread.h"
29 #include "content/public/browser/notification_service.h"
30 #include "content/public/browser/notification_types.h"
31 #include "content/public/browser/render_process_host.h"
32 #include "content/public/browser/url_data_source.h"
33 #include "grit/theme_resources.h"
34 #include "third_party/skia/include/core/SkColor.h"
35 #include "ui/gfx/color_utils.h"
36 #include "ui/gfx/image/image_skia.h"
37 #include "ui/gfx/sys_color_change_listener.h"
39 #if !defined(OS_ANDROID)
40 #include "chrome/browser/search/local_ntp_source.h"
41 #endif
43 namespace {
45 const int kSectionBorderAlphaTransparency = 80;
47 // Converts SkColor to RGBAColor
48 RGBAColor SkColorToRGBAColor(const SkColor& sKColor) {
49 RGBAColor color;
50 color.r = SkColorGetR(sKColor);
51 color.g = SkColorGetG(sKColor);
52 color.b = SkColorGetB(sKColor);
53 color.a = SkColorGetA(sKColor);
54 return color;
57 } // namespace
59 InstantService::InstantService(Profile* profile)
60 : profile_(profile),
61 template_url_service_(TemplateURLServiceFactory::GetForProfile(profile_)),
62 omnibox_start_margin_(chrome::kDisableStartMargin),
63 weak_ptr_factory_(this) {
64 // The initialization below depends on a typical set of browser threads. Skip
65 // it if we are running in a unit test without the full suite.
66 if (!content::BrowserThread::CurrentlyOn(content::BrowserThread::UI))
67 return;
69 // This depends on the existence of the typical browser threads. Therefore it
70 // is only instantiated here (after the check for a UI thread above).
71 instant_io_context_ = new InstantIOContext();
73 previous_google_base_url_ =
74 GURL(UIThreadSearchTermsData(profile).GoogleBaseURLValue());
76 // TemplateURLService is NULL by default in tests.
77 if (template_url_service_) {
78 template_url_service_->AddObserver(this);
79 const TemplateURL* default_search_provider =
80 template_url_service_->GetDefaultSearchProvider();
81 if (default_search_provider) {
82 previous_default_search_provider_.reset(
83 new TemplateURLData(default_search_provider->data()));
87 ResetInstantSearchPrerenderer();
89 registrar_.Add(this,
90 content::NOTIFICATION_RENDERER_PROCESS_CREATED,
91 content::NotificationService::AllSources());
92 registrar_.Add(this,
93 content::NOTIFICATION_RENDERER_PROCESS_TERMINATED,
94 content::NotificationService::AllSources());
96 scoped_refptr<history::TopSites> top_sites =
97 TopSitesFactory::GetForProfile(profile_);
98 if (top_sites)
99 top_sites->AddObserver(this);
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
119 // on Android.
120 #if !defined(OS_ANDROID)
121 content::URLDataSource::Add(profile_, new LocalNtpSource(profile_));
122 content::URLDataSource::Add(profile_, new ThumbnailSource(profile_, false));
123 content::URLDataSource::Add(profile_, new ThumbnailSource(profile_, true));
124 content::URLDataSource::Add(profile_, new ThumbnailListSource(profile_));
125 #endif // !defined(OS_ANDROID)
127 content::URLDataSource::Add(
128 profile_, new FaviconSource(profile_, FaviconSource::FAVICON));
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 scoped_refptr<history::TopSites> top_sites =
164 TopSitesFactory::GetForProfile(profile_);
165 if (!top_sites)
166 return;
168 top_sites->AddBlacklistedURL(url);
171 void InstantService::UndoMostVisitedDeletion(const GURL& url) {
172 scoped_refptr<history::TopSites> top_sites =
173 TopSitesFactory::GetForProfile(profile_);
174 if (!top_sites)
175 return;
177 top_sites->RemoveBlacklistedURL(url);
180 void InstantService::UndoAllMostVisitedDeletions() {
181 scoped_refptr<history::TopSites> top_sites =
182 TopSitesFactory::GetForProfile(profile_);
183 if (!top_sites)
184 return;
186 top_sites->ClearBlacklistedURLs();
189 void InstantService::UpdateThemeInfo() {
190 // Update theme background info.
191 // Initialize |theme_info| if necessary.
192 if (!theme_info_)
193 OnThemeChanged(ThemeServiceFactory::GetForProfile(profile_));
194 else
195 OnThemeChanged(NULL);
198 void InstantService::UpdateMostVisitedItemsInfo() {
199 NotifyAboutMostVisitedItems();
202 void InstantService::Shutdown() {
203 process_ids_.clear();
205 if (instant_io_context_.get()) {
206 content::BrowserThread::PostTask(
207 content::BrowserThread::IO, FROM_HERE,
208 base::Bind(&InstantIOContext::ClearInstantProcessesOnIO,
209 instant_io_context_));
212 scoped_refptr<history::TopSites> top_sites =
213 TopSitesFactory::GetForProfile(profile_);
214 if (top_sites)
215 top_sites->RemoveObserver(this);
217 instant_io_context_ = NULL;
220 void InstantService::Observe(int type,
221 const content::NotificationSource& source,
222 const content::NotificationDetails& details) {
223 switch (type) {
224 case content::NOTIFICATION_RENDERER_PROCESS_CREATED:
225 SendSearchURLsToRenderer(
226 content::Source<content::RenderProcessHost>(source).ptr());
227 break;
228 case content::NOTIFICATION_RENDERER_PROCESS_TERMINATED:
229 OnRendererProcessTerminated(
230 content::Source<content::RenderProcessHost>(source)->GetID());
231 break;
232 #if defined(ENABLE_THEMES)
233 case chrome::NOTIFICATION_BROWSER_THEME_CHANGED: {
234 OnThemeChanged(content::Source<ThemeService>(source).ptr());
235 break;
237 #endif // defined(ENABLE_THEMES)
238 default:
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 std::vector<InstantMostVisitedItem> new_most_visited_items;
269 for (size_t i = 0; i < reordered_data.size(); i++) {
270 const history::MostVisitedURL& url = reordered_data[i];
271 InstantMostVisitedItem item;
272 item.url = url.url;
273 item.title = url.title;
274 new_most_visited_items.push_back(item);
277 most_visited_items_ = new_most_visited_items;
278 NotifyAboutMostVisitedItems();
281 void InstantService::NotifyAboutMostVisitedItems() {
282 FOR_EACH_OBSERVER(InstantServiceObserver, observers_,
283 MostVisitedItemsChanged(most_visited_items_));
286 void InstantService::OnThemeChanged(ThemeService* theme_service) {
287 if (!theme_service) {
288 DCHECK(theme_info_.get());
289 FOR_EACH_OBSERVER(InstantServiceObserver, observers_,
290 ThemeInfoChanged(*theme_info_));
291 return;
294 // Get theme information from theme service.
295 theme_info_.reset(new ThemeBackgroundInfo());
297 // Get if the current theme is the default theme.
298 theme_info_->using_default_theme = theme_service->UsingDefaultTheme();
300 // Get theme colors.
301 SkColor background_color =
302 theme_service->GetColor(ThemeProperties::COLOR_NTP_BACKGROUND);
303 SkColor text_color =
304 theme_service->GetColor(ThemeProperties::COLOR_NTP_TEXT);
305 SkColor link_color =
306 theme_service->GetColor(ThemeProperties::COLOR_NTP_LINK);
307 SkColor text_color_light =
308 theme_service->GetColor(ThemeProperties::COLOR_NTP_TEXT_LIGHT);
309 SkColor header_color =
310 theme_service->GetColor(ThemeProperties::COLOR_NTP_HEADER);
311 // Generate section border color from the header color.
312 SkColor section_border_color =
313 SkColorSetARGB(kSectionBorderAlphaTransparency,
314 SkColorGetR(header_color),
315 SkColorGetG(header_color),
316 SkColorGetB(header_color));
318 // Invert colors if needed.
319 if (gfx::IsInvertedColorScheme()) {
320 background_color = color_utils::InvertColor(background_color);
321 text_color = color_utils::InvertColor(text_color);
322 link_color = color_utils::InvertColor(link_color);
323 text_color_light = color_utils::InvertColor(text_color_light);
324 header_color = color_utils::InvertColor(header_color);
325 section_border_color = color_utils::InvertColor(section_border_color);
328 // Set colors.
329 theme_info_->background_color = SkColorToRGBAColor(background_color);
330 theme_info_->text_color = SkColorToRGBAColor(text_color);
331 theme_info_->link_color = SkColorToRGBAColor(link_color);
332 theme_info_->text_color_light = SkColorToRGBAColor(text_color_light);
333 theme_info_->header_color = SkColorToRGBAColor(header_color);
334 theme_info_->section_border_color = SkColorToRGBAColor(section_border_color);
336 int logo_alternate = theme_service->GetDisplayProperty(
337 ThemeProperties::NTP_LOGO_ALTERNATE);
338 theme_info_->logo_alternate = logo_alternate == 1;
340 if (theme_service->HasCustomImage(IDR_THEME_NTP_BACKGROUND)) {
341 // Set theme id for theme background image url.
342 theme_info_->theme_id = theme_service->GetThemeID();
344 // Set theme background image horizontal alignment.
345 int alignment = theme_service->GetDisplayProperty(
346 ThemeProperties::NTP_BACKGROUND_ALIGNMENT);
347 if (alignment & ThemeProperties::ALIGN_LEFT)
348 theme_info_->image_horizontal_alignment = THEME_BKGRND_IMAGE_ALIGN_LEFT;
349 else if (alignment & ThemeProperties::ALIGN_RIGHT)
350 theme_info_->image_horizontal_alignment = THEME_BKGRND_IMAGE_ALIGN_RIGHT;
351 else
352 theme_info_->image_horizontal_alignment = THEME_BKGRND_IMAGE_ALIGN_CENTER;
354 // Set theme background image vertical alignment.
355 if (alignment & ThemeProperties::ALIGN_TOP)
356 theme_info_->image_vertical_alignment = THEME_BKGRND_IMAGE_ALIGN_TOP;
357 else if (alignment & ThemeProperties::ALIGN_BOTTOM)
358 theme_info_->image_vertical_alignment = THEME_BKGRND_IMAGE_ALIGN_BOTTOM;
359 else
360 theme_info_->image_vertical_alignment = THEME_BKGRND_IMAGE_ALIGN_CENTER;
362 // Set theme backgorund image tiling.
363 int tiling = theme_service->GetDisplayProperty(
364 ThemeProperties::NTP_BACKGROUND_TILING);
365 switch (tiling) {
366 case ThemeProperties::NO_REPEAT:
367 theme_info_->image_tiling = THEME_BKGRND_IMAGE_NO_REPEAT;
368 break;
369 case ThemeProperties::REPEAT_X:
370 theme_info_->image_tiling = THEME_BKGRND_IMAGE_REPEAT_X;
371 break;
372 case ThemeProperties::REPEAT_Y:
373 theme_info_->image_tiling = THEME_BKGRND_IMAGE_REPEAT_Y;
374 break;
375 case ThemeProperties::REPEAT:
376 theme_info_->image_tiling = THEME_BKGRND_IMAGE_REPEAT;
377 break;
380 // Set theme background image height.
381 gfx::ImageSkia* image = theme_service->GetImageSkiaNamed(
382 IDR_THEME_NTP_BACKGROUND);
383 DCHECK(image);
384 theme_info_->image_height = image->height();
386 theme_info_->has_attribution =
387 theme_service->HasCustomImage(IDR_THEME_NTP_ATTRIBUTION);
390 FOR_EACH_OBSERVER(InstantServiceObserver, observers_,
391 ThemeInfoChanged(*theme_info_));
394 void InstantService::OnTemplateURLServiceChanged() {
395 // Check whether the default search provider was changed.
396 const TemplateURL* template_url =
397 template_url_service_->GetDefaultSearchProvider();
398 bool default_search_provider_changed = !TemplateURL::MatchesData(
399 template_url, previous_default_search_provider_.get(),
400 UIThreadSearchTermsData(profile_));
401 if (default_search_provider_changed) {
402 previous_default_search_provider_.reset(
403 template_url ? new TemplateURLData(template_url->data()) : NULL);
406 // Note that, even if the TemplateURL for the Default Search Provider has not
407 // changed, the effective URLs might change if they reference the Google base
408 // URL. The TemplateURLService will notify us when the effective URL changes
409 // in this way but it's up to us to do the work to check both.
410 bool google_base_url_domain_changed = false;
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 UIThreadSearchTermsData(profile_)))
416 google_base_url_domain_changed = true;
419 if (default_search_provider_changed || google_base_url_domain_changed) {
420 ResetInstantSearchPrerenderer();
421 FOR_EACH_OBSERVER(
422 InstantServiceObserver, observers_,
423 DefaultSearchProviderChanged(google_base_url_domain_changed));
427 void InstantService::TopSitesLoaded(history::TopSites* top_sites) {
430 void InstantService::TopSitesChanged(history::TopSites* top_sites) {
431 top_sites->GetMostVisitedURLs(
432 base::Bind(&InstantService::OnMostVisitedItemsReceived,
433 weak_ptr_factory_.GetWeakPtr()),
434 false);
437 void InstantService::ResetInstantSearchPrerenderer() {
438 if (!chrome::ShouldPrefetchSearchResults())
439 return;
441 GURL url(chrome::GetSearchResultPrefetchBaseURL(profile_));
442 instant_prerenderer_.reset(
443 url.is_valid() ? new InstantSearchPrerenderer(profile_, url) : NULL);