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/command_line.h"
8 #include "base/json/json_string_value_serializer.h"
9 #include "base/logging.h"
10 #include "base/memory/ref_counted_memory.h"
11 #include "base/memory/scoped_ptr.h"
12 #include "base/metrics/field_trial.h"
13 #include "base/strings/string_util.h"
14 #include "base/strings/stringprintf.h"
15 #include "base/strings/utf_string_conversions.h"
16 #include "base/values.h"
17 #include "chrome/browser/search/instant_io_context.h"
18 #include "chrome/browser/search/local_files_ntp_source.h"
19 #include "chrome/browser/search/search.h"
20 #include "chrome/browser/search_engines/template_url_service_factory.h"
21 #include "chrome/common/chrome_switches.h"
22 #include "chrome/common/url_constants.h"
23 #include "chrome/grit/generated_resources.h"
24 #include "components/search_engines/template_url_prepopulate_data.h"
25 #include "components/search_engines/template_url_service.h"
26 #include "grit/browser_resources.h"
27 #include "grit/theme_resources.h"
28 #include "net/url_request/url_request.h"
29 #include "ui/base/l10n/l10n_util.h"
30 #include "ui/base/resource/resource_bundle.h"
31 #include "ui/base/ui_base_switches.h"
32 #include "ui/base/webui/jstemplate_builder.h"
33 #include "ui/base/webui/web_ui_util.h"
34 #include "ui/resources/grit/ui_resources.h"
39 // Signifies a locally constructed resource, i.e. not from grit/.
40 const int kLocalResource
= -1;
42 const char kConfigDataFilename
[] = "config.js";
44 const struct Resource
{
47 const char* mime_type
;
49 { "local-ntp.html", IDR_LOCAL_NTP_HTML
, "text/html" },
50 { "local-ntp.js", IDR_LOCAL_NTP_JS
, "application/javascript" },
51 { kConfigDataFilename
, kLocalResource
, "application/javascript" },
52 { "local-ntp.css", IDR_LOCAL_NTP_CSS
, "text/css" },
53 { "images/close_2.png", IDR_CLOSE_2
, "image/png" },
54 { "images/close_2_hover.png", IDR_CLOSE_2_H
, "image/png" },
55 { "images/close_2_active.png", IDR_CLOSE_2_P
, "image/png" },
56 { "images/close_2_white.png", IDR_CLOSE_2_MASK
, "image/png" },
57 { "images/close_3_mask.png", IDR_CLOSE_3_MASK
, "image/png" },
58 { "images/close_4_button.png", IDR_CLOSE_4_BUTTON
, "image/png" },
59 { "images/google_logo.png", IDR_LOCAL_NTP_IMAGES_LOGO_PNG
, "image/png" },
60 { "images/ntp_default_favicon.png", IDR_NTP_DEFAULT_FAVICON
, "image/png" },
63 // Strips any query parameters from the specified path.
64 std::string
StripParameters(const std::string
& path
) {
65 return path
.substr(0, path
.find("?"));
68 bool DefaultSearchProviderIsGoogle(Profile
* profile
) {
72 TemplateURLService
* template_url_service
=
73 TemplateURLServiceFactory::GetForProfile(profile
);
74 if (!template_url_service
)
77 const TemplateURL
* default_provider
=
78 template_url_service
->GetDefaultSearchProvider();
79 return default_provider
&&
80 (TemplateURLPrepopulateData::GetEngineType(
81 *default_provider
, template_url_service
->search_terms_data()) ==
82 SEARCH_ENGINE_GOOGLE
);
85 // Returns whether icon NTP is enabled by experiment.
86 // TODO(huangs): Remove all 3 copies of this routine once Icon NTP launches.
87 bool IsIconNTPEnabled() {
88 // Note: It's important to query the field trial state first, to ensure that
89 // UMA reports the correct group.
90 const std::string group_name
= base::FieldTrialList::FindFullName("IconNTP");
91 using base::CommandLine
;
92 if (CommandLine::ForCurrentProcess()->HasSwitch(switches::kDisableIconNtp
))
94 if (CommandLine::ForCurrentProcess()->HasSwitch(switches::kEnableIconNtp
))
97 return base::StartsWith(group_name
, "Enabled", base::CompareCase::SENSITIVE
);
100 // Adds a localized string keyed by resource id to the dictionary.
101 void AddString(base::DictionaryValue
* dictionary
,
102 const std::string
& key
,
104 dictionary
->SetString(key
, l10n_util::GetStringUTF16(resource_id
));
107 // Adds a localized string for the Google searchbox placeholder text.
108 void AddGoogleSearchboxPlaceholderString(base::DictionaryValue
* dictionary
) {
109 base::string16 placeholder
= l10n_util::GetStringFUTF16(
110 IDS_SEARCH_BOX_EMPTY_HINT
,
111 base::ASCIIToUTF16("Google"));
112 dictionary
->SetString("searchboxPlaceholder", placeholder
);
115 // Populates |translated_strings| dictionary for the local NTP. |is_google|
116 // indicates that this page is the Google Local NTP.
117 scoped_ptr
<base::DictionaryValue
> GetTranslatedStrings(bool is_google
) {
118 scoped_ptr
<base::DictionaryValue
> translated_strings(
119 new base::DictionaryValue());
121 AddString(translated_strings
.get(), "thumbnailRemovedNotification",
122 IDS_NEW_TAB_THUMBNAIL_REMOVED_NOTIFICATION
);
123 AddString(translated_strings
.get(), "removeThumbnailTooltip",
124 IDS_NEW_TAB_REMOVE_THUMBNAIL_TOOLTIP
);
125 AddString(translated_strings
.get(), "undoThumbnailRemove",
126 IDS_NEW_TAB_UNDO_THUMBNAIL_REMOVE
);
127 AddString(translated_strings
.get(), "restoreThumbnailsShort",
128 IDS_NEW_TAB_RESTORE_THUMBNAILS_SHORT_LINK
);
129 AddString(translated_strings
.get(), "attributionIntro",
130 IDS_NEW_TAB_ATTRIBUTION_INTRO
);
131 AddString(translated_strings
.get(), "title", IDS_NEW_TAB_TITLE
);
133 AddGoogleSearchboxPlaceholderString(translated_strings
.get());
135 return translated_strings
.Pass();
138 // Returns a JS dictionary of configuration data for the local NTP.
139 std::string
GetConfigData(Profile
* profile
) {
140 base::DictionaryValue config_data
;
141 bool is_google
= DefaultSearchProviderIsGoogle(profile
) &&
142 search::ShouldShowGoogleLocalNTP();
143 config_data
.Set("translatedStrings",
144 GetTranslatedStrings(is_google
).release());
145 config_data
.SetBoolean("isGooglePage", is_google
);
146 config_data
.SetBoolean("useIcons", IsIconNTPEnabled());
148 // Serialize the dictionary.
150 JSONStringValueSerializer
serializer(&js_text
);
151 serializer
.Serialize(config_data
);
153 std::string config_data_js
;
154 config_data_js
.append("var configData = ");
155 config_data_js
.append(js_text
);
156 config_data_js
.append(";");
157 return config_data_js
;
160 std::string
GetLocalNtpPath() {
161 return std::string(chrome::kChromeSearchScheme
) + "://" +
162 std::string(chrome::kChromeSearchLocalNtpHost
) + "/";
167 LocalNtpSource::LocalNtpSource(Profile
* profile
) : profile_(profile
) {
170 LocalNtpSource::~LocalNtpSource() {
173 std::string
LocalNtpSource::GetSource() const {
174 return chrome::kChromeSearchLocalNtpHost
;
177 void LocalNtpSource::StartDataRequest(
178 const std::string
& path
,
179 int render_process_id
,
181 const content::URLDataSource::GotDataCallback
& callback
) {
182 std::string stripped_path
= StripParameters(path
);
183 if (stripped_path
== kConfigDataFilename
) {
184 std::string config_data_js
= GetConfigData(profile_
);
185 callback
.Run(base::RefCountedString::TakeString(&config_data_js
));
189 #if !defined(OFFICIAL_BUILD)
190 base::CommandLine
* command_line
= base::CommandLine::ForCurrentProcess();
191 if (command_line
->HasSwitch(switches::kLocalNtpReload
)) {
192 if (stripped_path
== "local-ntp.html" || stripped_path
== "local-ntp.js" ||
193 stripped_path
== "local-ntp.css") {
194 base::ReplaceChars(stripped_path
, "-", "_", &stripped_path
);
195 local_ntp::SendLocalFileResource(stripped_path
, callback
);
202 std::string filename
;
203 webui::ParsePathAndScale(
204 GURL(GetLocalNtpPath() + stripped_path
), &filename
, &scale
);
205 ui::ScaleFactor scale_factor
= ui::GetSupportedScaleFactor(scale
);
207 for (size_t i
= 0; i
< arraysize(kResources
); ++i
) {
208 if (filename
== kResources
[i
].filename
) {
209 scoped_refptr
<base::RefCountedStaticMemory
> response(
210 ResourceBundle::GetSharedInstance().LoadDataResourceBytesForScale(
211 kResources
[i
].identifier
, scale_factor
));
212 callback
.Run(response
.get());
219 std::string
LocalNtpSource::GetMimeType(
220 const std::string
& path
) const {
221 const std::string stripped_path
= StripParameters(path
);
222 for (size_t i
= 0; i
< arraysize(kResources
); ++i
) {
223 if (stripped_path
== kResources
[i
].filename
)
224 return kResources
[i
].mime_type
;
226 return std::string();
229 bool LocalNtpSource::ShouldServiceRequest(
230 const net::URLRequest
* request
) const {
231 DCHECK(request
->url().host() == chrome::kChromeSearchLocalNtpHost
);
232 if (!InstantIOContext::ShouldServiceRequest(request
))
235 if (request
->url().SchemeIs(chrome::kChromeSearchScheme
)) {
236 std::string filename
;
237 webui::ParsePathAndScale(request
->url(), &filename
, NULL
);
238 for (size_t i
= 0; i
< arraysize(kResources
); ++i
) {
239 if (filename
== kResources
[i
].filename
)
246 std::string
LocalNtpSource::GetContentSecurityPolicyFrameSrc() const {
247 // Allow embedding of most visited iframes.
248 return base::StringPrintf("frame-src %s;",
249 chrome::kChromeSearchMostVisitedUrl
);