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 const std::string kInlineResouceRegex = "<include.*?src\\=[\"'](.+?)[\"'].*?>"; | |
fserb
2015/10/06 15:53:17
add a comment saying what you want to match with t
| |
47 | |
44 const struct Resource{ | 48 const struct Resource{ |
45 const char* filename; | 49 const char* filename; |
46 int identifier; | 50 int identifier; |
47 const char* mime_type; | 51 const char* mime_type; |
48 } kResources[] = { | 52 } kResources[] = { |
49 { "local-ntp.html", IDR_LOCAL_NTP_HTML, "text/html" }, | 53 { "local-ntp.html", IDR_LOCAL_NTP_HTML, "text/html" }, |
50 { "local-ntp.js", IDR_LOCAL_NTP_JS, "application/javascript" }, | 54 { "local-ntp.js", IDR_LOCAL_NTP_JS, "application/javascript" }, |
51 { kConfigDataFilename, kLocalResource, "application/javascript" }, | 55 { kConfigDataFilename, kLocalResource, "application/javascript" }, |
52 { "local-ntp.css", IDR_LOCAL_NTP_CSS, "text/css" }, | 56 { "local-ntp.css", IDR_LOCAL_NTP_CSS, "text/css" }, |
53 { "images/close_2.png", IDR_CLOSE_2, "image/png" }, | 57 { "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) { | 171 LocalNtpSource::LocalNtpSource(Profile* profile) : profile_(profile) { |
168 } | 172 } |
169 | 173 |
170 LocalNtpSource::~LocalNtpSource() { | 174 LocalNtpSource::~LocalNtpSource() { |
171 } | 175 } |
172 | 176 |
173 std::string LocalNtpSource::GetSource() const { | 177 std::string LocalNtpSource::GetSource() const { |
174 return chrome::kChromeSearchLocalNtpHost; | 178 return chrome::kChromeSearchLocalNtpHost; |
175 } | 179 } |
176 | 180 |
181 void FlattenLocalInclude( | |
fserb
2015/10/06 15:53:17
can you add a comment here explaining the flow of
Mathieu
2015/10/09 15:10:38
should be a private method and you wouldn't need t
| |
182 const content::URLDataSource::GotDataCallback& callback, | |
183 std::string topLevelResource, | |
184 base::RefCountedMemory* inlineResource); | |
185 | |
186 void CheckLocalIncludesHelper( | |
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 // FlattenLocalInclude(callback, resource, | |
fserb
2015/10/06 15:53:17
remove those lines?
| |
197 // base::RefCountedString::TakeString(new std::string("foobar"))); | |
198 } else { | |
199 callback.Run(base::RefCountedString::TakeString(&resource)); | |
200 } | |
201 } | |
202 | |
203 void CheckLocalIncludes(const content::URLDataSource::GotDataCallback& callback, | |
fserb
2015/10/06 15:53:17
couldn't this and FlattenLocalInclude be the same
tmartino
2015/10/15 20:54:16
This doesn't really work, actually. We have two pl
| |
204 base::RefCountedMemory* resource) { | |
205 std::string resourceAsStr(resource->front_as<char>(), resource->size()); | |
206 CheckLocalIncludesHelper(callback, resourceAsStr); | |
207 } | |
208 | |
209 void FlattenLocalInclude( | |
210 const content::URLDataSource::GotDataCallback& callback, | |
211 std::string topLevelResource, | |
212 base::RefCountedMemory* inlineResource) { | |
213 std::string inlineAsStr(inlineResource->front_as<char>(), | |
214 inlineResource->size()); | |
215 re2::RE2::Replace(&topLevelResource, kInlineResouceRegex, inlineAsStr); | |
216 CheckLocalIncludesHelper(callback, topLevelResource); | |
217 } | |
218 | |
177 void LocalNtpSource::StartDataRequest( | 219 void LocalNtpSource::StartDataRequest( |
178 const std::string& path, | 220 const std::string& path, |
179 int render_process_id, | 221 int render_process_id, |
180 int render_frame_id, | 222 int render_frame_id, |
181 const content::URLDataSource::GotDataCallback& callback) { | 223 const content::URLDataSource::GotDataCallback& callback) { |
182 std::string stripped_path = StripParameters(path); | 224 std::string stripped_path = StripParameters(path); |
183 if (stripped_path == kConfigDataFilename) { | 225 if (stripped_path == kConfigDataFilename) { |
184 std::string config_data_js = GetConfigData(profile_); | 226 std::string config_data_js = GetConfigData(profile_); |
185 callback.Run(base::RefCountedString::TakeString(&config_data_js)); | 227 callback.Run(base::RefCountedString::TakeString(&config_data_js)); |
186 return; | 228 return; |
187 } | 229 } |
188 | 230 |
189 #if !defined(OFFICIAL_BUILD) | 231 #if !defined(OFFICIAL_BUILD) |
190 base::CommandLine* command_line = base::CommandLine::ForCurrentProcess(); | 232 base::CommandLine* command_line = base::CommandLine::ForCurrentProcess(); |
191 if (command_line->HasSwitch(switches::kLocalNtpReload)) { | 233 if (command_line->HasSwitch(switches::kLocalNtpReload)) { |
192 if (stripped_path == "local-ntp.html" || stripped_path == "local-ntp.js" || | 234 if (stripped_path == "local-ntp.html" || stripped_path == "local-ntp.js" || |
193 stripped_path == "local-ntp.css") { | 235 stripped_path == "local-ntp.css") { |
194 base::ReplaceChars(stripped_path, "-", "_", &stripped_path); | 236 base::ReplaceChars(stripped_path, "-", "_", &stripped_path); |
195 local_ntp::SendLocalFileResource(stripped_path, callback); | 237 content::URLDataSource::GotDataCallback wrapper = |
238 base::Bind(&CheckLocalIncludes, callback); | |
239 local_ntp::SendLocalFileResource(stripped_path, wrapper); | |
196 return; | 240 return; |
197 } | 241 } |
198 } | 242 } |
199 #endif | 243 #endif |
200 | 244 |
201 float scale = 1.0f; | 245 float scale = 1.0f; |
202 std::string filename; | 246 std::string filename; |
203 webui::ParsePathAndScale( | 247 webui::ParsePathAndScale( |
204 GURL(GetLocalNtpPath() + stripped_path), &filename, &scale); | 248 GURL(GetLocalNtpPath() + stripped_path), &filename, &scale); |
205 ui::ScaleFactor scale_factor = ui::GetSupportedScaleFactor(scale); | 249 ui::ScaleFactor scale_factor = ui::GetSupportedScaleFactor(scale); |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
241 } | 285 } |
242 } | 286 } |
243 return false; | 287 return false; |
244 } | 288 } |
245 | 289 |
246 std::string LocalNtpSource::GetContentSecurityPolicyFrameSrc() const { | 290 std::string LocalNtpSource::GetContentSecurityPolicyFrameSrc() const { |
247 // Allow embedding of most visited iframes. | 291 // Allow embedding of most visited iframes. |
248 return base::StringPrintf("frame-src %s;", | 292 return base::StringPrintf("frame-src %s;", |
249 chrome::kChromeSearchMostVisitedUrl); | 293 chrome::kChromeSearchMostVisitedUrl); |
250 } | 294 } |
OLD | NEW |