[Icons NTP] Adding images for new NTP tile close button.
[chromium-blink-merge.git] / chrome / browser / search / local_ntp_source.cc
blobb7780cb15ec7d64b62d795fb04d65922e23e5c0b
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/metrics/field_trial.h"
12 #include "base/strings/string_util.h"
13 #include "base/strings/stringprintf.h"
14 #include "base/strings/utf_string_conversions.h"
15 #include "base/values.h"
16 #include "chrome/browser/search/instant_io_context.h"
17 #include "chrome/browser/search/search.h"
18 #include "chrome/browser/search_engines/template_url_service_factory.h"
19 #include "chrome/common/url_constants.h"
20 #include "chrome/grit/generated_resources.h"
21 #include "components/search_engines/template_url_prepopulate_data.h"
22 #include "components/search_engines/template_url_service.h"
23 #include "components/variations/variations_associated_data.h"
24 #include "grit/browser_resources.h"
25 #include "grit/theme_resources.h"
26 #include "net/url_request/url_request.h"
27 #include "ui/base/l10n/l10n_util.h"
28 #include "ui/base/resource/resource_bundle.h"
29 #include "ui/base/webui/jstemplate_builder.h"
30 #include "ui/base/webui/web_ui_util.h"
31 #include "ui/resources/grit/ui_resources.h"
32 #include "url/gurl.h"
34 namespace {
36 // Signifies a locally constructed resource, i.e. not from grit/.
37 const int kLocalResource = -1;
39 const char kConfigDataFilename[] = "config.js";
41 const struct Resource{
42 const char* filename;
43 int identifier;
44 const char* mime_type;
45 } kResources[] = {
46 { "local-ntp.html", IDR_LOCAL_NTP_HTML, "text/html" },
47 { "local-ntp.js", IDR_LOCAL_NTP_JS, "application/javascript" },
48 { kConfigDataFilename, kLocalResource, "application/javascript" },
49 { "local-ntp.css", IDR_LOCAL_NTP_CSS, "text/css" },
50 { "images/close_2.png", IDR_CLOSE_2, "image/png" },
51 { "images/close_2_hover.png", IDR_CLOSE_2_H, "image/png" },
52 { "images/close_2_active.png", IDR_CLOSE_2_P, "image/png" },
53 { "images/close_2_white.png", IDR_CLOSE_2_MASK, "image/png" },
54 { "images/close_3_mask.png", IDR_CLOSE_3_MASK, "image/png" },
55 { "images/close_4_button.png", IDR_CLOSE_4_BUTTON, "image/png" },
56 { "images/google_logo.png", IDR_LOCAL_NTP_IMAGES_LOGO_PNG, "image/png" },
57 { "images/white_google_logo.png",
58 IDR_LOCAL_NTP_IMAGES_WHITE_LOGO_PNG, "image/png" },
59 { "images/ntp_default_favicon.png", IDR_NTP_DEFAULT_FAVICON, "image/png" },
62 // Strips any query parameters from the specified path.
63 std::string StripParameters(const std::string& path) {
64 return path.substr(0, path.find("?"));
67 bool DefaultSearchProviderIsGoogle(Profile* profile) {
68 if (!profile)
69 return false;
71 TemplateURLService* template_url_service =
72 TemplateURLServiceFactory::GetForProfile(profile);
73 if (!template_url_service)
74 return false;
76 const TemplateURL* default_provider =
77 template_url_service->GetDefaultSearchProvider();
78 return default_provider &&
79 (TemplateURLPrepopulateData::GetEngineType(
80 *default_provider, template_url_service->search_terms_data()) ==
81 SEARCH_ENGINE_GOOGLE);
84 // Returns whether icon NTP is enabled.
85 bool IsIconNTPEnabled() {
86 return variations::GetVariationParamValue("IconNTP", "state") == "enabled";
89 // Returns whether we are in the Fast NTP experiment or not.
90 bool IsLocalNTPFastEnabled() {
91 return StartsWithASCII(base::FieldTrialList::FindFullName("LocalNTPFast"),
92 "Enabled", true);
95 // Serves a particular resource.
96 // Used for bypassing the default resources when we are in an experiment.
97 void SendResource(int resource_id,
98 const content::URLDataSource::GotDataCallback& callback) {
99 scoped_refptr<base::RefCountedStaticMemory> response(
100 ResourceBundle::GetSharedInstance().LoadDataResourceBytes(resource_id));
101 callback.Run(response.get());
104 // Adds a localized string keyed by resource id to the dictionary.
105 void AddString(base::DictionaryValue* dictionary,
106 const std::string& key,
107 int resource_id) {
108 dictionary->SetString(key, l10n_util::GetStringUTF16(resource_id));
111 // Adds a localized string for the Google searchbox placeholder text.
112 void AddGoogleSearchboxPlaceholderString(base::DictionaryValue* dictionary) {
113 base::string16 placeholder = l10n_util::GetStringFUTF16(
114 IDS_OMNIBOX_EMPTY_HINT_WITH_DEFAULT_SEARCH_PROVIDER,
115 base::ASCIIToUTF16("Google"));
116 dictionary->SetString("searchboxPlaceholder", placeholder);
119 // Populates |translated_strings| dictionary for the local NTP. |is_google|
120 // indicates that this page is the Google Local NTP.
121 scoped_ptr<base::DictionaryValue> GetTranslatedStrings(bool is_google) {
122 scoped_ptr<base::DictionaryValue> translated_strings(
123 new base::DictionaryValue());
125 AddString(translated_strings.get(), "thumbnailRemovedNotification",
126 IDS_NEW_TAB_THUMBNAIL_REMOVED_NOTIFICATION);
127 AddString(translated_strings.get(), "removeThumbnailTooltip",
128 IDS_NEW_TAB_REMOVE_THUMBNAIL_TOOLTIP);
129 AddString(translated_strings.get(), "undoThumbnailRemove",
130 IDS_NEW_TAB_UNDO_THUMBNAIL_REMOVE);
131 AddString(translated_strings.get(), "restoreThumbnailsShort",
132 IDS_NEW_TAB_RESTORE_THUMBNAILS_SHORT_LINK);
133 AddString(translated_strings.get(), "attributionIntro",
134 IDS_NEW_TAB_ATTRIBUTION_INTRO);
135 AddString(translated_strings.get(), "title", IDS_NEW_TAB_TITLE);
136 if (is_google)
137 AddGoogleSearchboxPlaceholderString(translated_strings.get());
139 return translated_strings.Pass();
142 // Returns a JS dictionary of configuration data for the local NTP.
143 std::string GetConfigData(Profile* profile) {
144 base::DictionaryValue config_data;
145 bool is_google = DefaultSearchProviderIsGoogle(profile) &&
146 chrome::ShouldShowGoogleLocalNTP();
147 config_data.Set("translatedStrings",
148 GetTranslatedStrings(is_google).release());
149 config_data.SetBoolean("isGooglePage", is_google);
150 config_data.SetBoolean("useIcons", IsIconNTPEnabled());
152 // Serialize the dictionary.
153 std::string js_text;
154 JSONStringValueSerializer serializer(&js_text);
155 serializer.Serialize(config_data);
157 std::string config_data_js;
158 config_data_js.append("var configData = ");
159 config_data_js.append(js_text);
160 config_data_js.append(";");
161 return config_data_js;
164 std::string GetLocalNtpPath() {
165 return std::string(chrome::kChromeSearchScheme) + "://" +
166 std::string(chrome::kChromeSearchLocalNtpHost) + "/";
169 } // namespace
171 LocalNtpSource::LocalNtpSource(Profile* profile) : profile_(profile) {
174 LocalNtpSource::~LocalNtpSource() {
177 std::string LocalNtpSource::GetSource() const {
178 return chrome::kChromeSearchLocalNtpHost;
181 void LocalNtpSource::StartDataRequest(
182 const std::string& path,
183 int render_process_id,
184 int render_frame_id,
185 const content::URLDataSource::GotDataCallback& callback) {
186 const std::string stripped_path = StripParameters(path);
187 if (stripped_path == kConfigDataFilename) {
188 std::string config_data_js = GetConfigData(profile_);
189 callback.Run(base::RefCountedString::TakeString(&config_data_js));
190 return;
192 if (IsLocalNTPFastEnabled()) {
193 if (stripped_path == "local-ntp.html") {
194 return SendResource(IDR_LOCAL_NTP_FAST_HTML, callback);
195 } else if (stripped_path == "local-ntp.js") {
196 return SendResource(IDR_LOCAL_NTP_FAST_JS, callback);
197 } else if (stripped_path == "local-ntp.css") {
198 return SendResource(IDR_LOCAL_NTP_FAST_CSS, callback);
201 float scale = 1.0f;
202 std::string filename;
203 webui::ParsePathAndScale(
204 GURL(GetLocalNtpPath() + stripped_path), &filename, &scale);
205 ui::ScaleFactor scale_factor = ui::GetSupportedScaleFactor(scale);
206 for (size_t i = 0; i < arraysize(kResources); ++i) {
207 if (filename == kResources[i].filename) {
208 scoped_refptr<base::RefCountedStaticMemory> response(
209 ResourceBundle::GetSharedInstance().LoadDataResourceBytesForScale(
210 kResources[i].identifier, scale_factor));
211 callback.Run(response.get());
212 return;
215 callback.Run(NULL);
218 std::string LocalNtpSource::GetMimeType(
219 const std::string& path) const {
220 const std::string stripped_path = StripParameters(path);
221 for (size_t i = 0; i < arraysize(kResources); ++i) {
222 if (stripped_path == kResources[i].filename)
223 return kResources[i].mime_type;
225 return std::string();
228 bool LocalNtpSource::ShouldServiceRequest(
229 const net::URLRequest* request) const {
230 DCHECK(request->url().host() == chrome::kChromeSearchLocalNtpHost);
231 if (!InstantIOContext::ShouldServiceRequest(request))
232 return false;
234 if (request->url().SchemeIs(chrome::kChromeSearchScheme)) {
235 std::string filename;
236 webui::ParsePathAndScale(request->url(), &filename, NULL);
237 for (size_t i = 0; i < arraysize(kResources); ++i) {
238 if (filename == kResources[i].filename)
239 return true;
242 return false;
245 std::string LocalNtpSource::GetContentSecurityPolicyFrameSrc() const {
246 // Allow embedding of most visited iframes.
247 return base::StringPrintf("frame-src %s;",
248 chrome::kChromeSearchMostVisitedUrl);