[Cronet] Delay StartNetLog and StopNetLog until native request context is initialized
[chromium-blink-merge.git] / chrome / browser / search / local_ntp_source.cc
blob9e2fdef8a3f276861f62f04996d5f376252f0860
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/google_logo.png", IDR_LOCAL_NTP_IMAGES_LOGO_PNG, "image/png" },
56 { "images/white_google_logo.png",
57 IDR_LOCAL_NTP_IMAGES_WHITE_LOGO_PNG, "image/png" },
58 { "images/ntp_default_favicon.png", IDR_NTP_DEFAULT_FAVICON, "image/png" },
61 // Strips any query parameters from the specified path.
62 std::string StripParameters(const std::string& path) {
63 return path.substr(0, path.find("?"));
66 bool DefaultSearchProviderIsGoogle(Profile* profile) {
67 if (!profile)
68 return false;
70 TemplateURLService* template_url_service =
71 TemplateURLServiceFactory::GetForProfile(profile);
72 if (!template_url_service)
73 return false;
75 const TemplateURL* default_provider =
76 template_url_service->GetDefaultSearchProvider();
77 return default_provider &&
78 (TemplateURLPrepopulateData::GetEngineType(
79 *default_provider, template_url_service->search_terms_data()) ==
80 SEARCH_ENGINE_GOOGLE);
83 // Returns whether icon NTP is enabled.
84 bool IsIconNTPEnabled() {
85 return variations::GetVariationParamValue("IconNTP", "state") == "enabled";
88 // Returns whether we are in the Fast NTP experiment or not.
89 bool IsLocalNTPFastEnabled() {
90 return StartsWithASCII(base::FieldTrialList::FindFullName("LocalNTPFast"),
91 "Enabled", true);
94 // Serves a particular resource.
95 // Used for bypassing the default resources when we are in an experiment.
96 void SendResource(int resource_id,
97 const content::URLDataSource::GotDataCallback& callback) {
98 scoped_refptr<base::RefCountedStaticMemory> response(
99 ResourceBundle::GetSharedInstance().LoadDataResourceBytes(resource_id));
100 callback.Run(response.get());
103 // Adds a localized string keyed by resource id to the dictionary.
104 void AddString(base::DictionaryValue* dictionary,
105 const std::string& key,
106 int resource_id) {
107 dictionary->SetString(key, l10n_util::GetStringUTF16(resource_id));
110 // Adds a localized string for the Google searchbox placeholder text.
111 void AddGoogleSearchboxPlaceholderString(base::DictionaryValue* dictionary) {
112 base::string16 placeholder = l10n_util::GetStringFUTF16(
113 IDS_OMNIBOX_EMPTY_HINT_WITH_DEFAULT_SEARCH_PROVIDER,
114 base::ASCIIToUTF16("Google"));
115 dictionary->SetString("searchboxPlaceholder", placeholder);
118 // Populates |translated_strings| dictionary for the local NTP. |is_google|
119 // indicates that this page is the Google Local NTP.
120 scoped_ptr<base::DictionaryValue> GetTranslatedStrings(bool is_google) {
121 scoped_ptr<base::DictionaryValue> translated_strings(
122 new base::DictionaryValue());
124 AddString(translated_strings.get(), "thumbnailRemovedNotification",
125 IDS_NEW_TAB_THUMBNAIL_REMOVED_NOTIFICATION);
126 AddString(translated_strings.get(), "removeThumbnailTooltip",
127 IDS_NEW_TAB_REMOVE_THUMBNAIL_TOOLTIP);
128 AddString(translated_strings.get(), "undoThumbnailRemove",
129 IDS_NEW_TAB_UNDO_THUMBNAIL_REMOVE);
130 AddString(translated_strings.get(), "restoreThumbnailsShort",
131 IDS_NEW_TAB_RESTORE_THUMBNAILS_SHORT_LINK);
132 AddString(translated_strings.get(), "attributionIntro",
133 IDS_NEW_TAB_ATTRIBUTION_INTRO);
134 AddString(translated_strings.get(), "title", IDS_NEW_TAB_TITLE);
135 if (is_google)
136 AddGoogleSearchboxPlaceholderString(translated_strings.get());
138 return translated_strings.Pass();
141 // Returns a JS dictionary of configuration data for the local NTP.
142 std::string GetConfigData(Profile* profile) {
143 base::DictionaryValue config_data;
144 bool is_google = DefaultSearchProviderIsGoogle(profile) &&
145 chrome::ShouldShowGoogleLocalNTP();
146 config_data.Set("translatedStrings",
147 GetTranslatedStrings(is_google).release());
148 config_data.SetBoolean("isGooglePage", is_google);
149 config_data.SetBoolean("useIcons", IsIconNTPEnabled());
151 // Serialize the dictionary.
152 std::string js_text;
153 JSONStringValueSerializer serializer(&js_text);
154 serializer.Serialize(config_data);
156 std::string config_data_js;
157 config_data_js.append("var configData = ");
158 config_data_js.append(js_text);
159 config_data_js.append(";");
160 return config_data_js;
163 std::string GetLocalNtpPath() {
164 return std::string(chrome::kChromeSearchScheme) + "://" +
165 std::string(chrome::kChromeSearchLocalNtpHost) + "/";
168 } // namespace
170 LocalNtpSource::LocalNtpSource(Profile* profile) : profile_(profile) {
173 LocalNtpSource::~LocalNtpSource() {
176 std::string LocalNtpSource::GetSource() const {
177 return chrome::kChromeSearchLocalNtpHost;
180 void LocalNtpSource::StartDataRequest(
181 const std::string& path,
182 int render_process_id,
183 int render_frame_id,
184 const content::URLDataSource::GotDataCallback& callback) {
185 const std::string stripped_path = StripParameters(path);
186 if (stripped_path == kConfigDataFilename) {
187 std::string config_data_js = GetConfigData(profile_);
188 callback.Run(base::RefCountedString::TakeString(&config_data_js));
189 return;
191 if (IsLocalNTPFastEnabled()) {
192 if (stripped_path == "local-ntp.html") {
193 return SendResource(IDR_LOCAL_NTP_FAST_HTML, callback);
194 } else if (stripped_path == "local-ntp.js") {
195 return SendResource(IDR_LOCAL_NTP_FAST_JS, callback);
196 } else if (stripped_path == "local-ntp.css") {
197 return SendResource(IDR_LOCAL_NTP_FAST_CSS, callback);
200 float scale = 1.0f;
201 std::string filename;
202 webui::ParsePathAndScale(
203 GURL(GetLocalNtpPath() + stripped_path), &filename, &scale);
204 ui::ScaleFactor scale_factor = ui::GetSupportedScaleFactor(scale);
205 for (size_t i = 0; i < arraysize(kResources); ++i) {
206 if (filename == kResources[i].filename) {
207 scoped_refptr<base::RefCountedStaticMemory> response(
208 ResourceBundle::GetSharedInstance().LoadDataResourceBytesForScale(
209 kResources[i].identifier, scale_factor));
210 callback.Run(response.get());
211 return;
214 callback.Run(NULL);
217 std::string LocalNtpSource::GetMimeType(
218 const std::string& path) const {
219 const std::string stripped_path = StripParameters(path);
220 for (size_t i = 0; i < arraysize(kResources); ++i) {
221 if (stripped_path == kResources[i].filename)
222 return kResources[i].mime_type;
224 return std::string();
227 bool LocalNtpSource::ShouldServiceRequest(
228 const net::URLRequest* request) const {
229 DCHECK(request->url().host() == chrome::kChromeSearchLocalNtpHost);
230 if (!InstantIOContext::ShouldServiceRequest(request))
231 return false;
233 if (request->url().SchemeIs(chrome::kChromeSearchScheme)) {
234 std::string filename;
235 webui::ParsePathAndScale(request->url(), &filename, NULL);
236 for (size_t i = 0; i < arraysize(kResources); ++i) {
237 if (filename == kResources[i].filename)
238 return true;
241 return false;
244 std::string LocalNtpSource::GetContentSecurityPolicyFrameSrc() const {
245 // Allow embedding of most visited iframes.
246 return base::StringPrintf("frame-src %s;",
247 chrome::kChromeSearchMostVisitedUrl);