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

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

Issue 1442793002: 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
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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 #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"
21 19
22 namespace { 20 namespace {
23 21
24 const char kBasePath[] = "chrome/browser/resources/local_ntp"; 22 const char kBasePath[] = "chrome/browser/resources/local_ntp";
25 23
26 // Matches lines of form '<include src="foo">' and captures 'foo'.
27 const char kInlineResourceRegex[] = "<include.*?src\\=[\"'](.+?)[\"'].*?>";
28
29 void CallbackWithLoadedResource( 24 void CallbackWithLoadedResource(
30 const std::string& origin, 25 const std::string& origin,
31 const content::URLDataSource::GotDataCallback& callback, 26 const content::URLDataSource::GotDataCallback& callback,
32 const std::string& content) { 27 const std::string& content) {
33 std::string output = content; 28 std::string output = content;
34 if (!origin.empty()) 29 if (!origin.empty())
35 base::ReplaceFirstSubstringAfterOffset(&output, 0, "{{ORIGIN}}", origin); 30 base::ReplaceFirstSubstringAfterOffset(&output, 0, "{{ORIGIN}}", origin);
36 31
37 scoped_refptr<base::RefCountedString> response( 32 scoped_refptr<base::RefCountedString> response(
38 base::RefCountedString::TakeString(&output)); 33 base::RefCountedString::TakeString(&output));
39 callback.Run(response.get()); 34 callback.Run(response.get());
40 } 35 }
41 36
42 // Read a file to a string and return. 37 // Read a file to a string and return.
43 std::string ReadFileAndReturn(const base::FilePath& path) { 38 std::string ReadFileAndReturn(const base::FilePath& path) {
44 std::string data; 39 std::string data;
45 // 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,
46 // we simply return an empty string. 41 // we simply return an empty string.
47 base::ReadFileToString(path, &data); 42 base::ReadFileToString(path, &data);
48 return data; 43 return data;
49 } 44 }
50 45
51 } // namespace 46 } // namespace
52 47
53 namespace local_ntp { 48 namespace local_ntp {
54 49
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) {
67 std::string filename;
68 re2::StringPiece resourceWrapper(resource);
69 if (re2::RE2::FindAndConsume(&resourceWrapper, kInlineResourceRegex,
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,
95 base::RefCountedMemory* inlineResource) {
96 std::string inlineAsStr(inlineResource->front_as<char>(),
97 inlineResource->size());
98 re2::RE2::Replace(&topLevelResource, kInlineResourceRegex, inlineAsStr);
99 CheckLocalIncludesHelper(callback, topLevelResource);
100 }
101
102 void SendLocalFileResource( 50 void SendLocalFileResource(
103 const std::string& path, 51 const std::string& path,
104 const content::URLDataSource::GotDataCallback& callback) { 52 const content::URLDataSource::GotDataCallback& callback) {
105 SendLocalFileResourceWithOrigin(path, std::string(), callback); 53 SendLocalFileResourceWithOrigin(path, std::string(), callback);
106 } 54 }
107 55
108 void SendLocalFileResourceWithOrigin( 56 void SendLocalFileResourceWithOrigin(
109 const std::string& path, 57 const std::string& path,
110 const std::string& origin, 58 const std::string& origin,
111 const content::URLDataSource::GotDataCallback& callback) { 59 const content::URLDataSource::GotDataCallback& callback) {
112 base::FilePath fullpath; 60 base::FilePath fullpath;
113 PathService::Get(base::DIR_SOURCE_ROOT, &fullpath); 61 PathService::Get(base::DIR_SOURCE_ROOT, &fullpath);
114 fullpath = fullpath.AppendASCII(kBasePath).AppendASCII(path); 62 fullpath = fullpath.AppendASCII(kBasePath).AppendASCII(path);
115 content::URLDataSource::GotDataCallback wrapper =
116 base::Bind(&CheckLocalIncludes, callback);
117 base::PostTaskAndReplyWithResult( 63 base::PostTaskAndReplyWithResult(
118 content::BrowserThread::GetBlockingPool(), FROM_HERE, 64 content::BrowserThread::GetBlockingPool(), FROM_HERE,
119 base::Bind(&ReadFileAndReturn, fullpath), 65 base::Bind(&ReadFileAndReturn, fullpath),
120 base::Bind(&CallbackWithLoadedResource, origin, wrapper)); 66 base::Bind(&CallbackWithLoadedResource, origin, callback));
121 } 67 }
122 68
123 } // namespace local_ntp 69 } // namespace local_ntp
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698