OLD | NEW |
---|---|
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/browser/search/local_ntp_source.h" | 5 #include "chrome/browser/search/local_ntp_source.h" |
6 | 6 |
7 #include "base/command_line.h" | 7 #include "base/command_line.h" |
8 #include "base/json/json_string_value_serializer.h" | 8 #include "base/json/json_string_value_serializer.h" |
9 #include "base/logging.h" | 9 #include "base/logging.h" |
10 #include "base/memory/ref_counted_memory.h" | 10 #include "base/memory/ref_counted_memory.h" |
11 #include "base/memory/scoped_ptr.h" | 11 #include "base/memory/scoped_ptr.h" |
12 #include "base/metrics/field_trial.h" | 12 #include "base/metrics/field_trial.h" |
13 #include "base/strings/string_util.h" | 13 #include "base/strings/string_util.h" |
14 #include "base/strings/stringprintf.h" | 14 #include "base/strings/stringprintf.h" |
15 #include "base/strings/utf_string_conversions.h" | 15 #include "base/strings/utf_string_conversions.h" |
16 #include "base/values.h" | 16 #include "base/values.h" |
17 #include "chrome/browser/search/instant_io_context.h" | 17 #include "chrome/browser/search/instant_io_context.h" |
18 #include "chrome/browser/search/local_files_ntp_source.h" | 18 #include "chrome/browser/search/local_files_ntp_source.h" |
19 #include "chrome/browser/search/search.h" | 19 #include "chrome/browser/search/search.h" |
20 #include "chrome/browser/search_engines/template_url_service_factory.h" | 20 #include "chrome/browser/search_engines/template_url_service_factory.h" |
21 #include "chrome/common/chrome_switches.h" | 21 #include "chrome/common/chrome_switches.h" |
22 #include "chrome/common/url_constants.h" | 22 #include "chrome/common/url_constants.h" |
23 #include "chrome/grit/generated_resources.h" | 23 #include "chrome/grit/generated_resources.h" |
24 #include "components/search_engines/template_url_prepopulate_data.h" | 24 #include "components/search_engines/template_url_prepopulate_data.h" |
25 #include "components/search_engines/template_url_service.h" | 25 #include "components/search_engines/template_url_service.h" |
26 #include "grit/browser_resources.h" | 26 #include "grit/browser_resources.h" |
27 #include "grit/theme_resources.h" | 27 #include "grit/theme_resources.h" |
28 #include "net/url_request/url_request.h" | 28 #include "net/url_request/url_request.h" |
29 #include "third_party/re2/re2/re2.h" | |
30 #include "third_party/re2/re2/stringpiece.h" | |
29 #include "ui/base/l10n/l10n_util.h" | 31 #include "ui/base/l10n/l10n_util.h" |
30 #include "ui/base/resource/resource_bundle.h" | 32 #include "ui/base/resource/resource_bundle.h" |
31 #include "ui/base/ui_base_switches.h" | 33 #include "ui/base/ui_base_switches.h" |
32 #include "ui/base/webui/jstemplate_builder.h" | 34 #include "ui/base/webui/jstemplate_builder.h" |
33 #include "ui/base/webui/web_ui_util.h" | 35 #include "ui/base/webui/web_ui_util.h" |
34 #include "ui/resources/grit/ui_resources.h" | 36 #include "ui/resources/grit/ui_resources.h" |
35 #include "url/gurl.h" | 37 #include "url/gurl.h" |
36 | 38 |
37 namespace { | 39 namespace { |
38 | 40 |
39 // Signifies a locally constructed resource, i.e. not from grit/. | 41 // Signifies a locally constructed resource, i.e. not from grit/. |
40 const int kLocalResource = -1; | 42 const int kLocalResource = -1; |
41 | 43 |
42 const char kConfigDataFilename[] = "config.js"; | 44 const char kConfigDataFilename[] = "config.js"; |
43 | 45 |
46 // Matches lines of form '<include src="foo">' and captures 'foo'. | |
47 const std::string kInlineResouceRegex = "<include.*?src\\=[\"'](.+?)[\"'].*?>"; | |
48 | |
44 const struct Resource{ | 49 const struct Resource{ |
45 const char* filename; | 50 const char* filename; |
46 int identifier; | 51 int identifier; |
47 const char* mime_type; | 52 const char* mime_type; |
48 } kResources[] = { | 53 } kResources[] = { |
49 { "local-ntp.html", IDR_LOCAL_NTP_HTML, "text/html" }, | 54 { "local-ntp.html", IDR_LOCAL_NTP_HTML, "text/html" }, |
50 { "local-ntp.js", IDR_LOCAL_NTP_JS, "application/javascript" }, | 55 { "local-ntp.js", IDR_LOCAL_NTP_JS, "application/javascript" }, |
51 { kConfigDataFilename, kLocalResource, "application/javascript" }, | 56 { kConfigDataFilename, kLocalResource, "application/javascript" }, |
52 { "local-ntp.css", IDR_LOCAL_NTP_CSS, "text/css" }, | 57 { "local-ntp.css", IDR_LOCAL_NTP_CSS, "text/css" }, |
53 { "images/close_2.png", IDR_CLOSE_2, "image/png" }, | 58 { "images/close_2.png", IDR_CLOSE_2, "image/png" }, |
(...skipping 113 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
167 LocalNtpSource::LocalNtpSource(Profile* profile) : profile_(profile) { | 172 LocalNtpSource::LocalNtpSource(Profile* profile) : profile_(profile) { |
168 } | 173 } |
169 | 174 |
170 LocalNtpSource::~LocalNtpSource() { | 175 LocalNtpSource::~LocalNtpSource() { |
171 } | 176 } |
172 | 177 |
173 std::string LocalNtpSource::GetSource() const { | 178 std::string LocalNtpSource::GetSource() const { |
174 return chrome::kChromeSearchLocalNtpHost; | 179 return chrome::kChromeSearchLocalNtpHost; |
175 } | 180 } |
176 | 181 |
182 // Helper method invoked by both CheckLocalIncludes and FlattenLocalInclude. | |
183 // Checks for any <include> directives; if any are found, loads the associated | |
184 // file and calls FlattenLocalInclude with the result. Otherwise, processing | |
185 // is done, and so the original callback is invoked. | |
186 void LocalNtpSource::CheckLocalIncludesHelper( | |
Mathieu
2015/10/19 19:59:01
these three functions don't really belong in this
| |
187 const content::URLDataSource::GotDataCallback& callback, | |
188 std::string resource) { | |
189 std::string filename; | |
190 re2::StringPiece resourceWrapper(resource); | |
191 if (re2::RE2::FindAndConsume(&resourceWrapper, kInlineResouceRegex, | |
192 &filename)) { | |
193 content::URLDataSource::GotDataCallback wrapper = | |
194 base::Bind(&FlattenLocalInclude, callback, resource); | |
195 local_ntp::SendLocalFileResource(filename, wrapper); | |
196 } else { | |
197 callback.Run(base::RefCountedString::TakeString(&resource)); | |
198 } | |
199 } | |
200 | |
201 // Wrapper around the above helper function for use as a callback. Processes | |
202 // local files to inline any files indicated by an <include> directive. | |
203 void LocalNtpSource::CheckLocalIncludes( | |
204 const content::URLDataSource::GotDataCallback& callback, | |
205 base::RefCountedMemory* resource) { | |
206 std::string resourceAsStr(resource->front_as<char>(), resource->size()); | |
207 CheckLocalIncludesHelper(callback, resourceAsStr); | |
208 } | |
209 | |
210 // Replaces the first <include> directive found with the given file contents. | |
211 // Afterwards, re-invokes CheckLocalIncludesHelper to handle any subsequent | |
212 // <include>s, including those which may have been added by the newly-inlined | |
213 // resource. | |
214 void LocalNtpSource::FlattenLocalInclude( | |
215 const content::URLDataSource::GotDataCallback& callback, | |
216 std::string topLevelResource, | |
217 base::RefCountedMemory* inlineResource) { | |
218 std::string inlineAsStr(inlineResource->front_as<char>(), | |
219 inlineResource->size()); | |
220 re2::RE2::Replace(&topLevelResource, kInlineResouceRegex, inlineAsStr); | |
221 CheckLocalIncludesHelper(callback, topLevelResource); | |
222 } | |
223 | |
177 void LocalNtpSource::StartDataRequest( | 224 void LocalNtpSource::StartDataRequest( |
178 const std::string& path, | 225 const std::string& path, |
179 int render_process_id, | 226 int render_process_id, |
180 int render_frame_id, | 227 int render_frame_id, |
181 const content::URLDataSource::GotDataCallback& callback) { | 228 const content::URLDataSource::GotDataCallback& callback) { |
182 std::string stripped_path = StripParameters(path); | 229 std::string stripped_path = StripParameters(path); |
183 if (stripped_path == kConfigDataFilename) { | 230 if (stripped_path == kConfigDataFilename) { |
184 std::string config_data_js = GetConfigData(profile_); | 231 std::string config_data_js = GetConfigData(profile_); |
185 callback.Run(base::RefCountedString::TakeString(&config_data_js)); | 232 callback.Run(base::RefCountedString::TakeString(&config_data_js)); |
186 return; | 233 return; |
187 } | 234 } |
188 | 235 |
189 #if !defined(OFFICIAL_BUILD) | 236 #if !defined(OFFICIAL_BUILD) |
190 base::CommandLine* command_line = base::CommandLine::ForCurrentProcess(); | 237 base::CommandLine* command_line = base::CommandLine::ForCurrentProcess(); |
191 if (command_line->HasSwitch(switches::kLocalNtpReload)) { | 238 if (command_line->HasSwitch(switches::kLocalNtpReload)) { |
192 if (stripped_path == "local-ntp.html" || stripped_path == "local-ntp.js" || | 239 if (stripped_path == "local-ntp.html" || stripped_path == "local-ntp.js" || |
193 stripped_path == "local-ntp.css") { | 240 stripped_path == "local-ntp.css") { |
194 base::ReplaceChars(stripped_path, "-", "_", &stripped_path); | 241 base::ReplaceChars(stripped_path, "-", "_", &stripped_path); |
195 local_ntp::SendLocalFileResource(stripped_path, callback); | 242 content::URLDataSource::GotDataCallback wrapper = |
243 base::Bind(&CheckLocalIncludes, callback); | |
244 local_ntp::SendLocalFileResource(stripped_path, wrapper); | |
196 return; | 245 return; |
197 } | 246 } |
198 } | 247 } |
199 #endif | 248 #endif |
200 | 249 |
201 float scale = 1.0f; | 250 float scale = 1.0f; |
202 std::string filename; | 251 std::string filename; |
203 webui::ParsePathAndScale( | 252 webui::ParsePathAndScale( |
204 GURL(GetLocalNtpPath() + stripped_path), &filename, &scale); | 253 GURL(GetLocalNtpPath() + stripped_path), &filename, &scale); |
205 ui::ScaleFactor scale_factor = ui::GetSupportedScaleFactor(scale); | 254 ui::ScaleFactor scale_factor = ui::GetSupportedScaleFactor(scale); |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
241 } | 290 } |
242 } | 291 } |
243 return false; | 292 return false; |
244 } | 293 } |
245 | 294 |
246 std::string LocalNtpSource::GetContentSecurityPolicyFrameSrc() const { | 295 std::string LocalNtpSource::GetContentSecurityPolicyFrameSrc() const { |
247 // Allow embedding of most visited iframes. | 296 // Allow embedding of most visited iframes. |
248 return base::StringPrintf("frame-src %s;", | 297 return base::StringPrintf("frame-src %s;", |
249 chrome::kChromeSearchMostVisitedUrl); | 298 chrome::kChromeSearchMostVisitedUrl); |
250 } | 299 } |
OLD | NEW |