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 #if !defined(GOOGLE_CHROME_BUILD) && !defined(OS_IOS) |
| 8 |
7 #include "base/bind.h" | 9 #include "base/bind.h" |
8 #include "base/files/file_path.h" | 10 #include "base/files/file_path.h" |
9 #include "base/files/file_util.h" | 11 #include "base/files/file_util.h" |
10 #include "base/location.h" | 12 #include "base/location.h" |
11 #include "base/memory/ref_counted_memory.h" | 13 #include "base/memory/ref_counted_memory.h" |
12 #include "base/memory/scoped_ptr.h" | 14 #include "base/memory/scoped_ptr.h" |
13 #include "base/path_service.h" | 15 #include "base/path_service.h" |
14 #include "base/strings/string_util.h" | 16 #include "base/strings/string_util.h" |
15 #include "base/threading/thread_restrictions.h" | 17 #include "base/threading/thread_restrictions.h" |
16 #include "chrome/common/url_constants.h" | 18 #include "chrome/common/url_constants.h" |
17 #include "content/public/browser/browser_thread.h" | 19 #include "content/public/browser/browser_thread.h" |
18 #include "content/public/browser/url_data_source.h" | 20 #include "content/public/browser/url_data_source.h" |
| 21 #include "third_party/re2/re2/re2.h" |
| 22 #include "third_party/re2/re2/stringpiece.h" |
19 | 23 |
20 namespace { | 24 namespace { |
21 | 25 |
22 const char kBasePath[] = "chrome/browser/resources/local_ntp"; | 26 const char kBasePath[] = "chrome/browser/resources/local_ntp"; |
23 | 27 |
| 28 // Matches lines of form '<include src="foo">' and captures 'foo'. |
| 29 const char kInlineResourceRegex[] = "<include.*?src\\=[\"'](.+?)[\"'].*?>"; |
| 30 |
24 void CallbackWithLoadedResource( | 31 void CallbackWithLoadedResource( |
25 const std::string& origin, | 32 const std::string& origin, |
26 const content::URLDataSource::GotDataCallback& callback, | 33 const content::URLDataSource::GotDataCallback& callback, |
27 const std::string& content) { | 34 const std::string& content) { |
28 std::string output = content; | 35 std::string output = content; |
29 if (!origin.empty()) | 36 if (!origin.empty()) |
30 base::ReplaceFirstSubstringAfterOffset(&output, 0, "{{ORIGIN}}", origin); | 37 base::ReplaceFirstSubstringAfterOffset(&output, 0, "{{ORIGIN}}", origin); |
31 | 38 |
32 scoped_refptr<base::RefCountedString> response( | 39 scoped_refptr<base::RefCountedString> response( |
33 base::RefCountedString::TakeString(&output)); | 40 base::RefCountedString::TakeString(&output)); |
34 callback.Run(response.get()); | 41 callback.Run(response.get()); |
35 } | 42 } |
36 | 43 |
37 // Read a file to a string and return. | 44 // Read a file to a string and return. |
38 std::string ReadFileAndReturn(const base::FilePath& path) { | 45 std::string ReadFileAndReturn(const base::FilePath& path) { |
39 std::string data; | 46 std::string data; |
40 // This call can fail, but it doesn't matter for our purposes. If it fails, | 47 // This call can fail, but it doesn't matter for our purposes. If it fails, |
41 // we simply return an empty string. | 48 // we simply return an empty string. |
42 base::ReadFileToString(path, &data); | 49 base::ReadFileToString(path, &data); |
43 return data; | 50 return data; |
44 } | 51 } |
45 | 52 |
46 } // namespace | 53 } // namespace |
47 | 54 |
48 namespace local_ntp { | 55 namespace local_ntp { |
49 | 56 |
| 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 |
50 void SendLocalFileResource( | 104 void SendLocalFileResource( |
51 const std::string& path, | 105 const std::string& path, |
52 const content::URLDataSource::GotDataCallback& callback) { | 106 const content::URLDataSource::GotDataCallback& callback) { |
53 SendLocalFileResourceWithOrigin(path, std::string(), callback); | 107 SendLocalFileResourceWithOrigin(path, std::string(), callback); |
54 } | 108 } |
55 | 109 |
56 void SendLocalFileResourceWithOrigin( | 110 void SendLocalFileResourceWithOrigin( |
57 const std::string& path, | 111 const std::string& path, |
58 const std::string& origin, | 112 const std::string& origin, |
59 const content::URLDataSource::GotDataCallback& callback) { | 113 const content::URLDataSource::GotDataCallback& callback) { |
60 base::FilePath fullpath; | 114 base::FilePath fullpath; |
61 PathService::Get(base::DIR_SOURCE_ROOT, &fullpath); | 115 PathService::Get(base::DIR_SOURCE_ROOT, &fullpath); |
62 fullpath = fullpath.AppendASCII(kBasePath).AppendASCII(path); | 116 fullpath = fullpath.AppendASCII(kBasePath).AppendASCII(path); |
| 117 content::URLDataSource::GotDataCallback wrapper = |
| 118 base::Bind(&CheckLocalIncludes, callback); |
63 base::PostTaskAndReplyWithResult( | 119 base::PostTaskAndReplyWithResult( |
64 content::BrowserThread::GetBlockingPool(), FROM_HERE, | 120 content::BrowserThread::GetBlockingPool(), FROM_HERE, |
65 base::Bind(&ReadFileAndReturn, fullpath), | 121 base::Bind(&ReadFileAndReturn, fullpath), |
66 base::Bind(&CallbackWithLoadedResource, origin, callback)); | 122 base::Bind(&CallbackWithLoadedResource, origin, wrapper)); |
67 } | 123 } |
68 | 124 |
69 } // namespace local_ntp | 125 } // namespace local_ntp |
| 126 |
| 127 #endif // !defined(GOOGLE_CHROME_BUILD) && !defined(OS_IOS) |
OLD | NEW |