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