Index: chrome/browser/search/local_ntp_source.cc |
diff --git a/chrome/browser/search/local_ntp_source.cc b/chrome/browser/search/local_ntp_source.cc |
index 7fecfbfbe1274de97ae4bccf153a81c60e229ef4..bbdff8db43802affab8076cb364460afb45bc7c7 100644 |
--- a/chrome/browser/search/local_ntp_source.cc |
+++ b/chrome/browser/search/local_ntp_source.cc |
@@ -26,6 +26,8 @@ |
#include "grit/browser_resources.h" |
#include "grit/theme_resources.h" |
#include "net/url_request/url_request.h" |
+#include "third_party/re2/re2/re2.h" |
+#include "third_party/re2/re2/stringpiece.h" |
#include "ui/base/l10n/l10n_util.h" |
#include "ui/base/resource/resource_bundle.h" |
#include "ui/base/ui_base_switches.h" |
@@ -41,6 +43,9 @@ const int kLocalResource = -1; |
const char kConfigDataFilename[] = "config.js"; |
+// Matches lines of form '<include src="foo">' and captures 'foo'. |
+const std::string kInlineResouceRegex = "<include.*?src\\=[\"'](.+?)[\"'].*?>"; |
+ |
const struct Resource{ |
const char* filename; |
int identifier; |
@@ -174,6 +179,48 @@ std::string LocalNtpSource::GetSource() const { |
return chrome::kChromeSearchLocalNtpHost; |
} |
+// Helper method invoked by both CheckLocalIncludes and FlattenLocalInclude. |
+// Checks for any <include> directives; if any are found, loads the associated |
+// file and calls FlattenLocalInclude with the result. Otherwise, processing |
+// is done, and so the original callback is invoked. |
+void LocalNtpSource::CheckLocalIncludesHelper( |
Mathieu
2015/10/19 19:59:01
these three functions don't really belong in this
|
+ const content::URLDataSource::GotDataCallback& callback, |
+ std::string resource) { |
+ std::string filename; |
+ re2::StringPiece resourceWrapper(resource); |
+ if (re2::RE2::FindAndConsume(&resourceWrapper, kInlineResouceRegex, |
+ &filename)) { |
+ content::URLDataSource::GotDataCallback wrapper = |
+ base::Bind(&FlattenLocalInclude, callback, resource); |
+ local_ntp::SendLocalFileResource(filename, wrapper); |
+ } else { |
+ callback.Run(base::RefCountedString::TakeString(&resource)); |
+ } |
+} |
+ |
+// Wrapper around the above helper function for use as a callback. Processes |
+// local files to inline any files indicated by an <include> directive. |
+void LocalNtpSource::CheckLocalIncludes( |
+ const content::URLDataSource::GotDataCallback& callback, |
+ base::RefCountedMemory* resource) { |
+ std::string resourceAsStr(resource->front_as<char>(), resource->size()); |
+ CheckLocalIncludesHelper(callback, resourceAsStr); |
+} |
+ |
+// Replaces the first <include> directive found with the given file contents. |
+// Afterwards, re-invokes CheckLocalIncludesHelper to handle any subsequent |
+// <include>s, including those which may have been added by the newly-inlined |
+// resource. |
+void LocalNtpSource::FlattenLocalInclude( |
+ const content::URLDataSource::GotDataCallback& callback, |
+ std::string topLevelResource, |
+ base::RefCountedMemory* inlineResource) { |
+ std::string inlineAsStr(inlineResource->front_as<char>(), |
+ inlineResource->size()); |
+ re2::RE2::Replace(&topLevelResource, kInlineResouceRegex, inlineAsStr); |
+ CheckLocalIncludesHelper(callback, topLevelResource); |
+} |
+ |
void LocalNtpSource::StartDataRequest( |
const std::string& path, |
int render_process_id, |
@@ -192,7 +239,9 @@ void LocalNtpSource::StartDataRequest( |
if (stripped_path == "local-ntp.html" || stripped_path == "local-ntp.js" || |
stripped_path == "local-ntp.css") { |
base::ReplaceChars(stripped_path, "-", "_", &stripped_path); |
- local_ntp::SendLocalFileResource(stripped_path, callback); |
+ content::URLDataSource::GotDataCallback wrapper = |
+ base::Bind(&CheckLocalIncludes, callback); |
+ local_ntp::SendLocalFileResource(stripped_path, wrapper); |
return; |
} |
} |