ozone: evdev: Sync caps lock LED state to evdev
[chromium-blink-merge.git] / chrome / browser / search / local_ntp_source.cc
blobb4b4d7db944f5f21fe759cdeb8f350148e2e2164
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/local_ntp_source.h"
7 #include "base/json/json_string_value_serializer.h"
8 #include "base/logging.h"
9 #include "base/memory/ref_counted_memory.h"
10 #include "base/memory/scoped_ptr.h"
11 #include "base/strings/string_util.h"
12 #include "base/strings/stringprintf.h"
13 #include "base/strings/utf_string_conversions.h"
14 #include "base/values.h"
15 #include "chrome/browser/search/instant_io_context.h"
16 #include "chrome/browser/search/search.h"
17 #include "chrome/browser/search_engines/template_url_service_factory.h"
18 #include "chrome/common/url_constants.h"
19 #include "chrome/grit/generated_resources.h"
20 #include "components/search_engines/template_url_prepopulate_data.h"
21 #include "components/search_engines/template_url_service.h"
22 #include "grit/browser_resources.h"
23 #include "grit/theme_resources.h"
24 #include "net/url_request/url_request.h"
25 #include "ui/base/l10n/l10n_util.h"
26 #include "ui/base/resource/resource_bundle.h"
27 #include "ui/base/webui/jstemplate_builder.h"
28 #include "ui/base/webui/web_ui_util.h"
29 #include "ui/resources/grit/ui_resources.h"
30 #include "url/gurl.h"
32 namespace {
34 // Signifies a locally constructed resource, i.e. not from grit/.
35 const int kLocalResource = -1;
37 const char kConfigDataFilename[] = "config.js";
39 const struct Resource{
40 const char* filename;
41 int identifier;
42 const char* mime_type;
43 } kResources[] = {
44 { "local-ntp.html", IDR_LOCAL_NTP_HTML, "text/html" },
45 { "local-ntp.js", IDR_LOCAL_NTP_JS, "application/javascript" },
46 { kConfigDataFilename, kLocalResource, "application/javascript" },
47 { "local-ntp.css", IDR_LOCAL_NTP_CSS, "text/css" },
48 { "images/close_2.png", IDR_CLOSE_2, "image/png" },
49 { "images/close_2_hover.png", IDR_CLOSE_2_H, "image/png" },
50 { "images/close_2_active.png", IDR_CLOSE_2_P, "image/png" },
51 { "images/close_2_white.png", IDR_CLOSE_2_MASK, "image/png" },
52 { "images/close_3_mask.png", IDR_CLOSE_3_MASK, "image/png" },
53 { "images/google_logo.png", IDR_LOCAL_NTP_IMAGES_LOGO_PNG, "image/png" },
54 { "images/white_google_logo.png",
55 IDR_LOCAL_NTP_IMAGES_WHITE_LOGO_PNG, "image/png" },
56 { "images/ntp_default_favicon.png", IDR_NTP_DEFAULT_FAVICON, "image/png" },
59 // Strips any query parameters from the specified path.
60 std::string StripParameters(const std::string& path) {
61 return path.substr(0, path.find("?"));
64 bool DefaultSearchProviderIsGoogle(Profile* profile) {
65 if (!profile)
66 return false;
68 TemplateURLService* template_url_service =
69 TemplateURLServiceFactory::GetForProfile(profile);
70 if (!template_url_service)
71 return false;
73 const TemplateURL* default_provider =
74 template_url_service->GetDefaultSearchProvider();
75 return default_provider &&
76 (TemplateURLPrepopulateData::GetEngineType(
77 *default_provider, template_url_service->search_terms_data()) ==
78 SEARCH_ENGINE_GOOGLE);
81 // Adds a localized string keyed by resource id to the dictionary.
82 void AddString(base::DictionaryValue* dictionary,
83 const std::string& key,
84 int resource_id) {
85 dictionary->SetString(key, l10n_util::GetStringUTF16(resource_id));
88 // Adds a localized string for the Google searchbox placeholder text.
89 void AddGoogleSearchboxPlaceholderString(base::DictionaryValue* dictionary) {
90 base::string16 placeholder = l10n_util::GetStringFUTF16(
91 IDS_OMNIBOX_EMPTY_HINT_WITH_DEFAULT_SEARCH_PROVIDER,
92 base::ASCIIToUTF16("Google"));
93 dictionary->SetString("searchboxPlaceholder", placeholder);
96 // Populates |translated_strings| dictionary for the local NTP. |is_google|
97 // indicates that this page is the Google Local NTP.
98 scoped_ptr<base::DictionaryValue> GetTranslatedStrings(bool is_google) {
99 scoped_ptr<base::DictionaryValue> translated_strings(
100 new base::DictionaryValue());
102 AddString(translated_strings.get(), "thumbnailRemovedNotification",
103 IDS_NEW_TAB_THUMBNAIL_REMOVED_NOTIFICATION);
104 AddString(translated_strings.get(), "removeThumbnailTooltip",
105 IDS_NEW_TAB_REMOVE_THUMBNAIL_TOOLTIP);
106 AddString(translated_strings.get(), "undoThumbnailRemove",
107 IDS_NEW_TAB_UNDO_THUMBNAIL_REMOVE);
108 AddString(translated_strings.get(), "restoreThumbnailsShort",
109 IDS_NEW_TAB_RESTORE_THUMBNAILS_SHORT_LINK);
110 AddString(translated_strings.get(), "attributionIntro",
111 IDS_NEW_TAB_ATTRIBUTION_INTRO);
112 AddString(translated_strings.get(), "title", IDS_NEW_TAB_TITLE);
113 if (is_google)
114 AddGoogleSearchboxPlaceholderString(translated_strings.get());
116 return translated_strings.Pass();
119 // Returns a JS dictionary of configuration data for the local NTP.
120 std::string GetConfigData(Profile* profile) {
121 base::DictionaryValue config_data;
122 bool is_google = DefaultSearchProviderIsGoogle(profile) &&
123 chrome::ShouldShowGoogleLocalNTP();
124 config_data.Set("translatedStrings",
125 GetTranslatedStrings(is_google).release());
126 config_data.SetBoolean("isGooglePage", is_google);
128 // Serialize the dictionary.
129 std::string js_text;
130 JSONStringValueSerializer serializer(&js_text);
131 serializer.Serialize(config_data);
133 std::string config_data_js;
134 config_data_js.append("var configData = ");
135 config_data_js.append(js_text);
136 config_data_js.append(";");
137 return config_data_js;
140 std::string GetLocalNtpPath() {
141 return std::string(chrome::kChromeSearchScheme) + "://" +
142 std::string(chrome::kChromeSearchLocalNtpHost) + "/";
145 } // namespace
147 LocalNtpSource::LocalNtpSource(Profile* profile) : profile_(profile) {
150 LocalNtpSource::~LocalNtpSource() {
153 std::string LocalNtpSource::GetSource() const {
154 return chrome::kChromeSearchLocalNtpHost;
157 void LocalNtpSource::StartDataRequest(
158 const std::string& path,
159 int render_process_id,
160 int render_frame_id,
161 const content::URLDataSource::GotDataCallback& callback) {
162 const std::string stripped_path = StripParameters(path);
163 if (stripped_path == kConfigDataFilename) {
164 std::string config_data_js = GetConfigData(profile_);
165 callback.Run(base::RefCountedString::TakeString(&config_data_js));
166 return;
168 float scale = 1.0f;
169 std::string filename;
170 webui::ParsePathAndScale(
171 GURL(GetLocalNtpPath() + stripped_path), &filename, &scale);
172 ui::ScaleFactor scale_factor = ui::GetSupportedScaleFactor(scale);
173 for (size_t i = 0; i < arraysize(kResources); ++i) {
174 if (filename == kResources[i].filename) {
175 scoped_refptr<base::RefCountedStaticMemory> response(
176 ResourceBundle::GetSharedInstance().LoadDataResourceBytesForScale(
177 kResources[i].identifier, scale_factor));
178 callback.Run(response.get());
179 return;
182 callback.Run(NULL);
185 std::string LocalNtpSource::GetMimeType(
186 const std::string& path) const {
187 const std::string stripped_path = StripParameters(path);
188 for (size_t i = 0; i < arraysize(kResources); ++i) {
189 if (stripped_path == kResources[i].filename)
190 return kResources[i].mime_type;
192 return std::string();
195 bool LocalNtpSource::ShouldServiceRequest(
196 const net::URLRequest* request) const {
197 DCHECK(request->url().host() == chrome::kChromeSearchLocalNtpHost);
198 if (!InstantIOContext::ShouldServiceRequest(request))
199 return false;
201 if (request->url().SchemeIs(chrome::kChromeSearchScheme)) {
202 std::string filename;
203 webui::ParsePathAndScale(request->url(), &filename, NULL);
204 for (size_t i = 0; i < arraysize(kResources); ++i) {
205 if (filename == kResources[i].filename)
206 return true;
209 return false;
212 std::string LocalNtpSource::GetContentSecurityPolicyFrameSrc() const {
213 // Allow embedding of most visited iframes.
214 return base::StringPrintf("frame-src %s;",
215 chrome::kChromeSearchMostVisitedUrl);