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

Side by Side Diff: services/url_response_disk_cache/url_response_disk_cache_impl.cc

Issue 1088533003: Adding URLResponse Disk Cache to mojo. (Closed) Base URL: https://github.com/domokit/mojo.git@master
Patch Set: Follow review Created 5 years, 7 months 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 | « services/url_response_disk_cache/url_response_disk_cache_impl.h ('k') | shell/BUILD.gn » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 // Copyright 2015 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "services/url_response_disk_cache/url_response_disk_cache_impl.h"
6
7 #include "base/bind.h"
8 #include "base/files/file_util.h"
9 #include "base/files/important_file_writer.h"
10 #include "base/location.h"
11 #include "base/logging.h"
12 #include "base/strings/string_util.h"
13 #include "base/strings/stringprintf.h"
14 #include "mojo/common/data_pipe_utils.h"
15 #include "mojo/public/cpp/application/application_connection.h"
16 #include "mojo/public/cpp/bindings/lib/fixed_buffer.h"
17 #include "services/url_response_disk_cache/url_response_disk_cache_entry.mojom.h "
18 #include "third_party/zlib/google/zip_reader.h"
19 #include "url/gurl.h"
20
21 namespace mojo {
22
23 namespace {
24
25 const char kEtagHeader[] = "etag";
26
27 template <typename T>
28 void Serialize(T input, std::string* output) {
29 typedef typename mojo::internal::WrapperTraits<T>::DataType DataType;
30 size_t size = GetSerializedSize_(input);
31 mojo::internal::FixedBuffer buf(size);
32 DataType data_type;
33 Serialize_(input.Pass(), &buf, &data_type);
34 std::vector<Handle> handles;
35 data_type->EncodePointersAndHandles(&handles);
36 void* serialized_data = buf.Leak();
37 *output = std::string(static_cast<char*>(serialized_data), size);
38 free(serialized_data);
39 }
40
41 template <typename T>
42 void Deserialize(std::string input, T* output) {
43 typedef typename mojo::internal::WrapperTraits<T>::DataType DataType;
44 DataType data_type = reinterpret_cast<DataType>(&input[0]);
45 std::vector<Handle> handles;
46 data_type->DecodePointersAndHandles(&handles);
47 Deserialize_(data_type, output);
48 }
49
50 Array<uint8_t> PathToArray(const base::FilePath& path) {
51 if (path.empty())
52 return Array<uint8_t>();
53 const std::string& string = path.value();
54 Array<uint8_t> result(string.size());
55 memcpy(&result.front(), string.data(), string.size());
56 return result.Pass();
57 }
58
59 // Encode a string in ascii. This uses # as an escape character. It also escapes
60 // ':' because it is an usual path separator.
61 std::string EncodeString(const std::string& string) {
62 std::string result = "";
63 for (size_t i = 0; i < string.size(); ++i) {
64 unsigned char c = string[i];
65 if (c >= 32 && c < 128 && c != '#' && c != ':') {
66 result += c;
67 } else {
68 result += base::StringPrintf("#%02x", c);
69 }
70 }
71 return result;
72 }
73
74 // This service use a directory under HOME to store all of its data,
75 base::FilePath GetBaseDirectory() {
76 return base::FilePath(getenv("HOME")).Append(".mojo_url_response_disk_cache");
77 }
78
79 // Returns the directory used store cached data for the given |url|, under
80 // |base_directory|.
81 base::FilePath GetDirName(base::FilePath base_directory,
82 const std::string& url) {
83 // TODO(qsr): If the speed of directory traversal is problematic, this might
84 // need to change to use less directories.
85 return base_directory.Append(EncodeString(url));
86 }
87
88 // Returns the directory that the consumer can use to cache its own data.
89 base::FilePath GetConsumerCacheDirectory(const base::FilePath& main_cache) {
90 return main_cache.Append("consumer_cache");
91 }
92
93 // Returns the path of the sentinel used to keep track of a zipped response has
94 // already been extracted.
95 base::FilePath GetExtractedSentinel(const base::FilePath& main_cache) {
96 return main_cache.Append("extracted_sentinel");
97 }
98
99 // Runs the given callback. If |success| is false, call back with an error.
100 // Otherwise, store |entry| in |entry_path|, then call back with the given
101 // paths.
102 void RunCallbackWithSuccess(
103 const URLResponseDiskCacheImpl::FilePathPairCallback& callback,
104 const base::FilePath& content_path,
105 const base::FilePath& cache_dir,
106 const base::FilePath& entry_path,
107 CacheEntryPtr entry,
108 bool success) {
109 if (!success) {
110 callback.Run(base::FilePath(), base::FilePath());
111 return;
112 }
113 std::string serialized_entry;
114 Serialize(entry.Pass(), &serialized_entry);
115 // We can ignore write error, as it will just force to clear the cache on the
116 // next request.
117 base::ImportantFileWriter::WriteFileAtomically(entry_path, serialized_entry);
118 callback.Run(content_path, cache_dir);
119 }
120
121 // Run the given mojo callback with the given paths.
122 void RunMojoCallback(
123 const Callback<void(Array<uint8_t>, Array<uint8_t>)>& callback,
124 const base::FilePath& path1,
125 const base::FilePath& path2) {
126 callback.Run(PathToArray(path1), PathToArray(path2));
127 }
128
129 // Returns the list of values for the given |header_name| in the given list of
130 // headers.
131 std::vector<std::string> GetHeaderValues(const std::string& header_name,
132 const Array<String>& headers) {
133 std::vector<std::string> result;
134 for (std::string header : headers.storage()) {
135 if (StartsWithASCII(header, header_name, false)) {
136 auto begin = header.begin();
137 auto end = header.end();
138 begin += header_name.size();
139 // Extract the content of the header by finding the remaining string after
140 // the ':' and stripping all spaces.
141 while (begin < end && *begin != ':')
142 begin++;
143 if (begin < end) {
144 begin++;
145 while (begin < end && *begin == ' ')
146 begin++;
147 while (end > begin && *(end - 1) == ' ')
148 end--;
149 if (begin < end) {
150 result.push_back(std::string(begin, end));
151 }
152 }
153 }
154 }
155 return result;
156 }
157
158 // Returns whether the given directory |dir| constains a valid entry file for
159 // the given |response|. If this is the case and |output| is not |nullptr|, then
160 // the deserialized entry is returned in |*output|.
161 bool IsCacheEntryValid(const base::FilePath& dir,
162 URLResponse* response,
163 CacheEntryPtr* output) {
164 // Find the entry file, and deserialize it.
165 base::FilePath entry_path = dir.Append("entry");
166 if (!base::PathExists(entry_path))
167 return false;
168 std::string serialized_entry;
169 if (!ReadFileToString(entry_path, &serialized_entry))
170 return false;
171 CacheEntryPtr entry;
172 Deserialize(serialized_entry, &entry);
173
174 // If |entry| or |response| has not headers, it is not possible to check if
175 // the entry is valid, so returns |false|.
176 if (entry->headers.is_null() || response->headers.is_null())
177 return false;
178
179 // Only handle etag for the moment.
180 std::string etag_header_name = kEtagHeader;
181 std::vector<std::string> entry_etags =
182 GetHeaderValues(etag_header_name, entry->headers);
183 if (entry_etags.size() == 0)
184 return false;
185 std::vector<std::string> response_etags =
186 GetHeaderValues(etag_header_name, response->headers);
187 if (response_etags.size() == 0)
188 return false;
189
190 // Looking for the first etag header.
191 bool result = entry_etags[0] == response_etags[0];
192
193 // Returns |entry| if requested.
194 if (output)
195 *output = entry.Pass();
196
197 return result;
198 }
199
200 } // namespace
201
202 URLResponseDiskCacheImpl::URLResponseDiskCacheImpl(
203 scoped_refptr<base::SequencedWorkerPool> worker_pool,
204 const std::string& remote_application_url,
205 InterfaceRequest<URLResponseDiskCache> request)
206 : worker_pool_(worker_pool), binding_(this, request.Pass()) {
207 base_directory_ = GetBaseDirectory();
208 // The cached files are shared only for application of the same origin.
209 if (remote_application_url != "") {
210 base_directory_ = base_directory_.Append(
211 EncodeString(GURL(remote_application_url).GetOrigin().spec()));
212 }
213 }
214
215 URLResponseDiskCacheImpl::~URLResponseDiskCacheImpl() {
216 }
217
218 void URLResponseDiskCacheImpl::GetFile(URLResponsePtr response,
219 const GetFileCallback& callback) {
220 return GetFileInternal(response.Pass(),
221 base::Bind(&RunMojoCallback, callback));
222 }
223
224 void URLResponseDiskCacheImpl::GetExtractedContent(
225 URLResponsePtr response,
226 const GetExtractedContentCallback& callback) {
227 base::FilePath dir = GetDirName(base_directory_, response->url);
228 base::FilePath extracted_dir = dir.Append("extracted");
229 if (IsCacheEntryValid(dir, response.get(), nullptr) &&
230 PathExists(GetExtractedSentinel(dir))) {
231 callback.Run(PathToArray(extracted_dir), PathToArray(dir));
232 return;
233 }
234
235 GetFileInternal(
236 response.Pass(),
237 base::Bind(&URLResponseDiskCacheImpl::GetExtractedContentInternal,
238 base::Unretained(this), base::Bind(&RunMojoCallback, callback),
239 extracted_dir));
240 }
241
242 void URLResponseDiskCacheImpl::GetFileInternal(
243 URLResponsePtr response,
244 const FilePathPairCallback& callback) {
245 base::FilePath dir = GetDirName(base_directory_, response->url);
246
247 // Check if the response is cached and valid. If that's the case, returns the
248 // cached value.
249 CacheEntryPtr entry;
250 if (IsCacheEntryValid(dir, response.get(), &entry)) {
251 callback.Run(base::FilePath(entry->content_path),
252 GetConsumerCacheDirectory(dir));
253 return;
254 }
255
256 // As the response was either not cached or the cached value is not valid, if
257 // the cache directory for the response exists, it needs to be cleaned.
258 if (base::PathExists(dir)) {
259 base::FilePath to_delete;
260 CHECK(CreateTemporaryDirInDir(base_directory_, "to_delete", &to_delete));
261 CHECK(Move(dir, to_delete));
262 worker_pool_->PostTask(
263 FROM_HERE,
264 base::Bind(base::IgnoreResult(&base::DeleteFile), to_delete, true));
265 }
266
267 // If the response has not a valid body, and it is not possible to create
268 // either the cache directory or the consumer cache directory, returns an
269 // error.
270 if (!response->body.is_valid() ||
271 !base::CreateDirectoryAndGetError(dir, nullptr) ||
272 !base::CreateDirectoryAndGetError(GetConsumerCacheDirectory(dir),
273 nullptr)) {
274 callback.Run(base::FilePath(), base::FilePath());
275 return;
276 }
277
278 // Fill the entry values for the request.
279 base::FilePath entry_path = dir.Append("entry");
280 base::FilePath content;
281 CHECK(CreateTemporaryFileInDir(dir, &content));
282 entry = CacheEntry::New();
283 entry->url = response->url;
284 entry->content_path = content.value();
285 entry->headers = response->headers.Pass();
286 // Asynchronously copy the response body to the cached file. The entry is send
287 // to the callback so that it is saved on disk only if the copy of the body
288 // succeded.
289 common::CopyToFile(response->body.Pass(), content, worker_pool_.get(),
290 base::Bind(&RunCallbackWithSuccess, callback, content,
291 GetConsumerCacheDirectory(dir), entry_path,
292 base::Passed(entry.Pass())));
293 }
294
295 void URLResponseDiskCacheImpl::GetExtractedContentInternal(
296 const FilePathPairCallback& callback,
297 const base::FilePath& extracted_dir,
298 const base::FilePath& content,
299 const base::FilePath& dir) {
300 // If it is not possible to get the cached file, returns an error.
301 if (content.empty()) {
302 callback.Run(base::FilePath(), base::FilePath());
303 return;
304 }
305
306 // Unzip the content to the extracted directory. In case of any error, returns
307 // an error.
308 zip::ZipReader reader;
309 if (!reader.Open(content)) {
310 callback.Run(base::FilePath(), base::FilePath());
311 return;
312 }
313 while (reader.HasMore()) {
314 bool success = reader.OpenCurrentEntryInZip();
315 success = success && reader.ExtractCurrentEntryIntoDirectory(extracted_dir);
316 success = success && reader.AdvanceToNextEntry();
317 if (!success) {
318 callback.Run(base::FilePath(), base::FilePath());
319 return;
320 }
321 }
322 // We can ignore write error, as it will just force to clear the cache on the
323 // next request.
324 WriteFile(GetExtractedSentinel(dir), nullptr, 0);
325 callback.Run(extracted_dir, GetConsumerCacheDirectory(dir));
326 }
327
328 } // namespace mojo
OLDNEW
« no previous file with comments | « services/url_response_disk_cache/url_response_disk_cache_impl.h ('k') | shell/BUILD.gn » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698