Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(245)

Side by Side Diff: chrome/browser/search/local_files_ntp_source.cc

Issue 1450533002: Revert of Enabling included files when reloading local NTP with --local-ntp-reload. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 5 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 #if !defined(GOOGLE_CHROME_BUILD) && !defined(OS_IOS)
6
7 #include "chrome/browser/search/local_files_ntp_source.h" 5 #include "chrome/browser/search/local_files_ntp_source.h"
8 6
9 #include "base/bind.h" 7 #include "base/bind.h"
10 #include "base/files/file_path.h" 8 #include "base/files/file_path.h"
11 #include "base/files/file_util.h" 9 #include "base/files/file_util.h"
12 #include "base/location.h" 10 #include "base/location.h"
13 #include "base/memory/ref_counted_memory.h" 11 #include "base/memory/ref_counted_memory.h"
14 #include "base/memory/scoped_ptr.h" 12 #include "base/memory/scoped_ptr.h"
15 #include "base/path_service.h" 13 #include "base/path_service.h"
16 #include "base/strings/string_util.h" 14 #include "base/strings/string_util.h"
17 #include "base/threading/thread_restrictions.h" 15 #include "base/threading/thread_restrictions.h"
18 #include "chrome/common/url_constants.h" 16 #include "chrome/common/url_constants.h"
19 #include "content/public/browser/browser_thread.h" 17 #include "content/public/browser/browser_thread.h"
20 #include "content/public/browser/url_data_source.h" 18 #include "content/public/browser/url_data_source.h"
21 #include "third_party/re2/re2/re2.h"
22 #include "third_party/re2/re2/stringpiece.h"
23 19
24 namespace { 20 namespace {
25 21
26 const char kBasePath[] = "chrome/browser/resources/local_ntp"; 22 const char kBasePath[] = "chrome/browser/resources/local_ntp";
27 23
28 // Matches lines of form '<include src="foo">' and captures 'foo'.
29 const char kInlineResourceRegex[] = "<include.*?src\\=[\"'](.+?)[\"'].*?>";
30
31 void CallbackWithLoadedResource( 24 void CallbackWithLoadedResource(
32 const std::string& origin, 25 const std::string& origin,
33 const content::URLDataSource::GotDataCallback& callback, 26 const content::URLDataSource::GotDataCallback& callback,
34 const std::string& content) { 27 const std::string& content) {
35 std::string output = content; 28 std::string output = content;
36 if (!origin.empty()) 29 if (!origin.empty())
37 base::ReplaceFirstSubstringAfterOffset(&output, 0, "{{ORIGIN}}", origin); 30 base::ReplaceFirstSubstringAfterOffset(&output, 0, "{{ORIGIN}}", origin);
38 31
39 scoped_refptr<base::RefCountedString> response( 32 scoped_refptr<base::RefCountedString> response(
40 base::RefCountedString::TakeString(&output)); 33 base::RefCountedString::TakeString(&output));
41 callback.Run(response.get()); 34 callback.Run(response.get());
42 } 35 }
43 36
44 // Read a file to a string and return. 37 // Read a file to a string and return.
45 std::string ReadFileAndReturn(const base::FilePath& path) { 38 std::string ReadFileAndReturn(const base::FilePath& path) {
46 std::string data; 39 std::string data;
47 // This call can fail, but it doesn't matter for our purposes. If it fails, 40 // This call can fail, but it doesn't matter for our purposes. If it fails,
48 // we simply return an empty string. 41 // we simply return an empty string.
49 base::ReadFileToString(path, &data); 42 base::ReadFileToString(path, &data);
50 return data; 43 return data;
51 } 44 }
52 45
53 } // namespace 46 } // namespace
54 47
55 namespace local_ntp { 48 namespace local_ntp {
56 49
57 void FlattenLocalInclude(
58 const content::URLDataSource::GotDataCallback& callback,
59 std::string topLevelResource,
60 base::RefCountedMemory* inlineResource);
61
62 // Helper method invoked by both CheckLocalIncludes and FlattenLocalInclude.
63 // Checks for any <include> directives; if any are found, loads the associated
64 // file and calls FlattenLocalInclude with the result. Otherwise, processing
65 // is done, and so the original callback is invoked.
66 void CheckLocalIncludesHelper(
67 const content::URLDataSource::GotDataCallback& callback,
68 std::string& resource) {
69 std::string filename;
70 re2::StringPiece resourceWrapper(resource);
71 if (re2::RE2::FindAndConsume(&resourceWrapper, kInlineResourceRegex,
72 &filename)) {
73 content::URLDataSource::GotDataCallback wrapper =
74 base::Bind(&FlattenLocalInclude, callback, resource);
75 SendLocalFileResource(filename, wrapper);
76 } else {
77 callback.Run(base::RefCountedString::TakeString(&resource));
78 }
79 }
80
81 // Wrapper around the above helper function for use as a callback. Processes
82 // local files to inline any files indicated by an <include> directive.
83 void CheckLocalIncludes(
84 const content::URLDataSource::GotDataCallback& callback,
85 base::RefCountedMemory* resource) {
86 std::string resourceAsStr(resource->front_as<char>(), resource->size());
87 CheckLocalIncludesHelper(callback, resourceAsStr);
88 }
89
90 // Replaces the first <include> directive found with the given file contents.
91 // Afterwards, re-invokes CheckLocalIncludesHelper to handle any subsequent
92 // <include>s, including those which may have been added by the newly-inlined
93 // resource.
94 void FlattenLocalInclude(
95 const content::URLDataSource::GotDataCallback& callback,
96 std::string topLevelResource,
97 base::RefCountedMemory* inlineResource) {
98 std::string inlineAsStr(inlineResource->front_as<char>(),
99 inlineResource->size());
100 re2::RE2::Replace(&topLevelResource, kInlineResourceRegex, inlineAsStr);
101 CheckLocalIncludesHelper(callback, topLevelResource);
102 }
103
104 void SendLocalFileResource( 50 void SendLocalFileResource(
105 const std::string& path, 51 const std::string& path,
106 const content::URLDataSource::GotDataCallback& callback) { 52 const content::URLDataSource::GotDataCallback& callback) {
107 SendLocalFileResourceWithOrigin(path, std::string(), callback); 53 SendLocalFileResourceWithOrigin(path, std::string(), callback);
108 } 54 }
109 55
110 void SendLocalFileResourceWithOrigin( 56 void SendLocalFileResourceWithOrigin(
111 const std::string& path, 57 const std::string& path,
112 const std::string& origin, 58 const std::string& origin,
113 const content::URLDataSource::GotDataCallback& callback) { 59 const content::URLDataSource::GotDataCallback& callback) {
114 base::FilePath fullpath; 60 base::FilePath fullpath;
115 PathService::Get(base::DIR_SOURCE_ROOT, &fullpath); 61 PathService::Get(base::DIR_SOURCE_ROOT, &fullpath);
116 fullpath = fullpath.AppendASCII(kBasePath).AppendASCII(path); 62 fullpath = fullpath.AppendASCII(kBasePath).AppendASCII(path);
117 content::URLDataSource::GotDataCallback wrapper =
118 base::Bind(&CheckLocalIncludes, callback);
119 base::PostTaskAndReplyWithResult( 63 base::PostTaskAndReplyWithResult(
120 content::BrowserThread::GetBlockingPool(), FROM_HERE, 64 content::BrowserThread::GetBlockingPool(), FROM_HERE,
121 base::Bind(&ReadFileAndReturn, fullpath), 65 base::Bind(&ReadFileAndReturn, fullpath),
122 base::Bind(&CallbackWithLoadedResource, origin, wrapper)); 66 base::Bind(&CallbackWithLoadedResource, origin, callback));
123 } 67 }
124 68
125 } // namespace local_ntp 69 } // namespace local_ntp
126
127 #endif // !defined(GOOGLE_CHROME_BUILD) && !defined(OS_IOS)
OLDNEW
« no previous file with comments | « chrome/browser/search/local_files_ntp_source.h ('k') | chrome/browser/search/local_ntp_source.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698