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

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

Issue 1276073004: Offline By Default (Closed) Base URL: https://github.com/domokit/mojo.git@master
Patch Set: Use memcpy to solve alignment issue. Created 5 years, 3 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
OLDNEW
1 // Copyright 2015 The Chromium Authors. All rights reserved. 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 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 "services/url_response_disk_cache/url_response_disk_cache_impl.h" 5 #include "services/url_response_disk_cache/url_response_disk_cache_impl.h"
6 6
7 #include <dirent.h> 7 #include <dirent.h>
8 #include <fcntl.h> 8 #include <fcntl.h>
9 #include <sys/types.h> 9 #include <sys/types.h>
10 10
11 #include <type_traits> 11 #include <type_traits>
12 12
13 #include "base/bind.h" 13 #include "base/bind.h"
14 #include "base/callback_helpers.h"
14 #include "base/files/file.h" 15 #include "base/files/file.h"
15 #include "base/files/file_util.h" 16 #include "base/files/file_util.h"
16 #include "base/location.h" 17 #include "base/location.h"
17 #include "base/logging.h" 18 #include "base/logging.h"
19 #include "base/strings/string_number_conversions.h"
18 #include "base/strings/string_util.h" 20 #include "base/strings/string_util.h"
19 #include "base/strings/stringprintf.h" 21 #include "base/strings/stringprintf.h"
20 #include "base/trace_event/trace_event.h" 22 #include "base/trace_event/trace_event.h"
23 #include "crypto/random.h"
21 #include "mojo/data_pipe_utils/data_pipe_utils.h" 24 #include "mojo/data_pipe_utils/data_pipe_utils.h"
22 #include "mojo/public/cpp/application/application_connection.h" 25 #include "mojo/public/cpp/application/application_connection.h"
23 #include "mojo/public/cpp/bindings/lib/fixed_buffer.h" 26 #include "mojo/public/cpp/bindings/lib/fixed_buffer.h"
27 #include "mojo/public/interfaces/network/http_header.mojom.h"
24 #include "services/url_response_disk_cache/url_response_disk_cache_entry.mojom.h " 28 #include "services/url_response_disk_cache/url_response_disk_cache_entry.mojom.h "
25 #include "third_party/zlib/google/zip_reader.h" 29 #include "third_party/zlib/google/zip_reader.h"
26 #include "url/gurl.h" 30 #include "url/gurl.h"
27 31
28 namespace mojo { 32 namespace mojo {
29 33
30 namespace { 34 namespace {
31 35
32 // The current version of the cache. This should only be incremented. When this 36 // The current version of the cache. This should only be incremented. When this
33 // is incremented, all current cache entries will be invalidated. 37 // is incremented, all current cache entries will be invalidated.
34 const uint32_t kCurrentVersion = 2; 38 const uint32_t kCurrentVersion = 0;
39
40 // The delay to wait before starting deleting data. This is delayed to not
41 // interfere with the shell startup.
42 const uint32_t kTrashDelayInSeconds = 60;
35 43
36 const char kEtagHeader[] = "etag"; 44 const char kEtagHeader[] = "etag";
37 45
38 const char kEntryName[] = "entry"; 46 std::string GetNewIdentifier() {
39 const char kEntryTmpName[] = "entry.tmp"; 47 char bytes[32];
40 48 crypto::RandBytes(bytes, arraysize(bytes));
41 // TODO(darin): These Serialize / Deserialize methods should not live here. 49 return base::HexEncode(bytes, arraysize(bytes));
42 // They use private details of the bindings system. Instead, we should provide
43 // these as helper functions under mojo/public/cpp/bindings/.
44
45 template <typename T>
46 void Serialize(T input, std::string* output) {
47 typedef typename mojo::internal::WrapperTraits<T>::DataType DataType;
48 size_t size = GetSerializedSize_(input);
49
50 output->clear();
51 output->resize(size);
52
53 mojo::internal::FixedBuffer buf;
54 buf.Initialize(&output->at(0), size);
55
56 DataType data_type;
57 Serialize_(input.Pass(), &buf, &data_type);
58 std::vector<Handle> handles;
59 data_type->EncodePointersAndHandles(&handles);
60 } 50 }
61 51
62 template <typename T> 52 void DoNothing(const base::FilePath& fp1, const base::FilePath& fp2) {}
63 bool Deserialize(std::string input, T* output) { 53
64 typedef typename mojo::internal::WrapperTraits<T>::DataType DataType; 54 void MovePathInDir(const base::FilePath& source,
ppi 2015/09/08 13:45:45 MovePathIntoDir?
qsr 2015/09/08 13:53:40 Done.
65 mojo::internal::BoundsChecker bounds_checker(&input[0], input.size(), 0); 55 const base::FilePath& destination) {
66 if (!std::remove_pointer<DataType>::type::Validate(&input[0], 56 if (!PathExists(source))
67 &bounds_checker)) { 57 return;
68 return false; 58
59 base::FilePath tmp_dir;
60 base::CreateTemporaryDirInDir(destination, "", &tmp_dir);
61 base::File::Error error;
62 if (!base::ReplaceFile(source, tmp_dir, &error)) {
63 LOG(ERROR) << "Failed to clear dir content: " << error;
69 } 64 }
70 DataType data_type = reinterpret_cast<DataType>(&input[0]);
71 std::vector<Handle> handles;
72 data_type->DecodePointersAndHandles(&handles);
73 Deserialize_(data_type, output);
74 return true;
75 } 65 }
76 66
77 void SaveEntry(CacheEntryPtr entry, base::ScopedFD dir) { 67 void ClearTrashDir(scoped_refptr<base::TaskRunner> task_runner,
78 TRACE_EVENT0("url_response_disk_cache", "SaveEntry"); 68 const base::FilePath& trash_dir) {
79 69 // Delete the trash directory.
80 std::string serialized_entry; 70 task_runner->PostDelayedTask(
81 Serialize(entry.Pass(), &serialized_entry); 71 FROM_HERE,
82 DCHECK_LT(serialized_entry.size(), 72 base::Bind(base::IgnoreResult(&base::DeleteFile), trash_dir, true),
83 static_cast<size_t>(std::numeric_limits<int>::max())); 73 base::TimeDelta::FromSeconds(kTrashDelayInSeconds));
84
85 int file_fd =
86 HANDLE_EINTR(openat(dir.get(), kEntryTmpName, O_WRONLY | O_CREAT, 0600));
87 if (file_fd < 0)
88 return;
89 base::File file(file_fd);
90 if (file.WriteAtCurrentPos(serialized_entry.data(),
91 serialized_entry.size()) !=
92 static_cast<int>(serialized_entry.size()))
93 return;
94 // The file must be closed before the file is moved.
95 file.Close();
96 renameat(dir.get(), kEntryTmpName, dir.get(), kEntryName);
97 } 74 }
98 75
99 Array<uint8_t> PathToArray(const base::FilePath& path) { 76 Array<uint8_t> PathToArray(const base::FilePath& path) {
100 if (path.empty()) 77 if (path.empty())
101 return Array<uint8_t>(); 78 return Array<uint8_t>();
102 const std::string& string = path.value(); 79 const std::string& string = path.value();
103 Array<uint8_t> result(string.size()); 80 Array<uint8_t> result(string.size());
104 memcpy(&result.front(), string.data(), string.size()); 81 memcpy(&result.front(), string.data(), string.size());
105 return result.Pass(); 82 return result.Pass();
106 } 83 }
107 84
108 // This method remove the query string of an url if one is present. It does 85 // This method remove the query string of an url if one is present. It does
109 // match the behavior of the application manager, which connects to the same app 86 // match the behavior of the application manager, which connects to the same app
110 // if requested twice with different query parameters. 87 // if requested twice with different query parameters.
111 std::string CanonicalizeURL(const std::string& url) { 88 std::string CanonicalizeURL(const std::string& url) {
112 GURL gurl(url); 89 GURL gurl(url);
113 90
114 if (!gurl.has_query()) { 91 if (!gurl.has_query()) {
115 return gurl.spec(); 92 return gurl.spec();
116 } 93 }
117 94
118 GURL::Replacements repl; 95 GURL::Replacements repl;
119 repl.SetQueryStr(""); 96 repl.SetQueryStr("");
120 std::string result = gurl.ReplaceComponents(repl).spec(); 97 std::string result = gurl.ReplaceComponents(repl).spec();
121 // Remove the dangling '?' because it's ugly. 98 // Remove the dangling '?' because it's ugly.
122 base::ReplaceChars(result, "?", "", &result); 99 base::ReplaceChars(result, "?", "", &result);
123 return result; 100 return result;
124 } 101 }
125 102
126 // Encode a string in ascii. This uses _ as an escape character. It also escapes
127 // ':' because it is an usual path separator, and '#' because dart refuses it in
128 // URLs.
129 std::string EncodeString(const std::string& string) {
130 std::string result = "";
131 for (size_t i = 0; i < string.size(); ++i) {
132 unsigned char c = string[i];
133 if (c >= 32 && c < 128 && c != '_' && c != ':' && c != '#') {
134 result += c;
135 } else {
136 result += base::StringPrintf("_%02x", c);
137 }
138 }
139 return result;
140 }
141
142 // This service use a directory under HOME to store all of its data, 103 // This service use a directory under HOME to store all of its data,
143 base::FilePath GetBaseDirectory() { 104 base::FilePath GetBaseDirectory() {
144 return base::FilePath(getenv("HOME")).Append(".mojo_url_response_disk_cache"); 105 return base::FilePath(getenv("HOME")).Append(".mojo_url_response_disk_cache");
145 } 106 }
146 107
108 // Returns the directory containing live data for the cache.
147 base::FilePath GetCacheDirectory() { 109 base::FilePath GetCacheDirectory() {
148 return GetBaseDirectory().Append("cache"); 110 return GetBaseDirectory().Append("cache");
149 } 111 }
150 112
113 // Returns a temporary that will be deleted after startup. This is used to have
114 // a consistent directory for outdated files in case the trash process doesn't
115 // finish.
151 base::FilePath GetTrashDirectory() { 116 base::FilePath GetTrashDirectory() {
152 return GetBaseDirectory().Append("trash"); 117 return GetBaseDirectory().Append("trash");
153 } 118 }
154 119
155 // Returns the directory used store cached data for the given |url|, under 120 // Returns a staging directory to save file before an entry can be saved in the
156 // |base_directory|. 121 // database. This directory is deleted when the service is started. This is used
157 base::FilePath GetDirName(base::FilePath base_directory, 122 // to prevent leaking files if there is an interuption while downloading a
158 const std::string& url) { 123 // response.
159 // TODO(qsr): If the speed of directory traversal is problematic, this might 124 base::FilePath GetStagingDirectory() {
160 // need to change to use less directories. 125 return GetBaseDirectory().Append("staging");
161 return base_directory.Append(EncodeString(CanonicalizeURL(url)));
162 } 126 }
163 127
164 // Returns the directory that the consumer can use to cache its own data. 128 // Returns path of the directory that the consumer can use to cache its own
165 base::FilePath GetConsumerCacheDirectory(const base::FilePath& main_cache) { 129 // data.
166 return main_cache.Append("consumer_cache"); 130 base::FilePath GetConsumerCacheDirectory(
131 const base::FilePath& cache_directory) {
132 return cache_directory.Append("consumer_cache");
167 } 133 }
168 134
169 // Returns the path of the sentinel used to keep track of a zipped response has 135 // Returns the path of the sentinel used to keep track of a zipped response has
170 // already been extracted. 136 // already been extracted.
171 base::FilePath GetExtractedSentinel(const base::FilePath& main_cache) { 137 base::FilePath GetExtractedSentinel(const base::FilePath& cache_directory) {
172 return main_cache.Append("extracted_sentinel"); 138 return cache_directory.Append("extracted_sentinel");
139 }
140
141 // Returns the path of the directory when a zipped content is extracted.
142 base::FilePath GetExtractedDirectory(const base::FilePath& cache_directory) {
143 return cache_directory.Append("extracted");
173 } 144 }
174 145
175 // Runs the given callback. If |success| is false, call back with an error. 146 // Runs the given callback. If |success| is false, call back with an error.
176 // Otherwise, store |entry| in |entry_path|, then call back with the given 147 // Otherwise, store a new entry in the databse, then call back with the content
177 // paths. 148 // path and the consumer cache path.
178 void RunCallbackWithSuccess( 149 void RunCallbackWithSuccess(
179 const URLResponseDiskCacheImpl::FilePathPairCallback& callback, 150 const URLResponseDiskCacheImpl::FilePathPairCallback& callback,
180 const base::FilePath& content_path, 151 const std::string& identifier,
181 const base::FilePath& cache_dir, 152 const std::string& request_origin,
182 const base::FilePath& entry_path, 153 const std::string& url,
183 CacheEntryPtr entry, 154 URLResponsePtr response,
184 base::TaskRunner* task_runner, 155 scoped_refptr<URLResponseDiskCacheDB> db,
156 scoped_refptr<base::TaskRunner> task_runner,
185 bool success) { 157 bool success) {
186 TRACE_EVENT1("url_response_disk_cache", "RunCallbackWithSuccess", 158 TRACE_EVENT2("url_response_disk_cache", "RunCallbackWithSuccess", "url", url,
187 "content_path", content_path.value()); 159 "identifier", identifier);
188 if (!success) { 160 if (!success) {
189 callback.Run(base::FilePath(), base::FilePath()); 161 callback.Run(base::FilePath(), base::FilePath());
190 return; 162 return;
191 } 163 }
192 164
193 // Save the entry in a background thread. The entry directory is opened and 165 base::FilePath staging_content = GetStagingDirectory().Append(identifier);
194 // passed because if this service wants to replace the content later on, it 166 base::FilePath cache_directory = GetCacheDirectory().Append(identifier);
195 // will start by moving the current directory. 167 base::FilePath content_path = cache_directory.Append(identifier);
196 base::ScopedFD dir(HANDLE_EINTR( 168 base::FilePath consumer_cache_directory =
197 open(entry_path.DirName().value().c_str(), O_RDONLY | O_DIRECTORY))); 169 GetConsumerCacheDirectory(cache_directory);
198 task_runner->PostTask(FROM_HERE,
199 base::Bind(&SaveEntry, base::Passed(entry.Pass()),
200 base::Passed(dir.Pass())));
201 170
202 callback.Run(content_path, cache_dir); 171 CacheEntryPtr entry = CacheEntry::New();
172 entry->response = response.Pass();
173 entry->cache_directory = cache_directory.value();
174 entry->content_path = content_path.value();
175
176 db->Put(request_origin, url, entry.Pass());
177
178 if (!base::CreateDirectoryAndGetError(cache_directory, nullptr) ||
179 !base::CreateDirectoryAndGetError(consumer_cache_directory, nullptr) ||
180 !base::ReplaceFile(staging_content, content_path, nullptr)) {
181 MovePathInDir(cache_directory, GetStagingDirectory());
182 callback.Run(base::FilePath(), base::FilePath());
183 return;
184 }
185
186 callback.Run(content_path, consumer_cache_directory);
203 } 187 }
204 188
205 // Run the given mojo callback with the given paths. 189 // Run the given mojo callback with the given paths.
206 void RunMojoCallback( 190 void RunMojoCallback(
207 const Callback<void(Array<uint8_t>, Array<uint8_t>)>& callback, 191 const Callback<void(Array<uint8_t>, Array<uint8_t>)>& callback,
208 const base::FilePath& path1, 192 const base::FilePath& path1,
209 const base::FilePath& path2) { 193 const base::FilePath& path2) {
210 callback.Run(PathToArray(path1), PathToArray(path2)); 194 callback.Run(PathToArray(path1), PathToArray(path2));
211 } 195 }
212 196
213 // Returns the list of values for the given |header_name| in the given list of 197 // Returns the list of values for the given |header_name| in the given list of
214 // headers. 198 // headers.
215 template <typename HeaderType>
216 std::vector<std::string> GetHeaderValues(const std::string& header_name, 199 std::vector<std::string> GetHeaderValues(const std::string& header_name,
217 const Array<HeaderType>& headers) { 200 const Array<HttpHeaderPtr>& headers) {
218 std::vector<std::string> result; 201 std::vector<std::string> result;
219 for (size_t i = 0u; i < headers.size(); ++i) { 202 for (size_t i = 0u; i < headers.size(); ++i) {
220 std::string name = headers[i]->name; 203 const std::string& name = headers[i]->name.get();
221 if (base::LowerCaseEqualsASCII(name, header_name.c_str())) 204 if (base::LowerCaseEqualsASCII(name, header_name.c_str()))
222 result.push_back(headers[i]->value); 205 result.push_back(headers[i]->value);
223 } 206 }
224 return result; 207 return result;
225 } 208 }
226 209
227 // Returns whether the given directory |dir| constains a valid entry file for 210 // Returns whether the given |entry| is valid.
228 // the given |response|. If this is the case and |output| is not |nullptr|, then 211 bool IsCacheEntryValid(const CacheEntryPtr& entry) {
229 // the deserialized entry is returned in |*output|. 212 return entry && PathExists(base::FilePath(entry->content_path));
230 bool IsCacheEntryValid(const base::FilePath& dir, 213 }
231 URLResponse* response,
232 CacheEntryPtr* output) {
233 // Find the entry file, and deserialize it.
234 base::FilePath entry_path = dir.Append(kEntryName);
235 if (!base::PathExists(entry_path))
236 return false;
237 std::string serialized_entry;
238 if (!ReadFileToString(entry_path, &serialized_entry))
239 return false;
240 CacheEntryPtr entry;
241 if (!Deserialize(serialized_entry, &entry))
242 return false;
243 214
244 // Obsolete entries are invalidated. 215 // Returns whether the given directory |entry| is valid and its content can be
245 if (entry->version != kCurrentVersion) 216 // used for the given |response|.
217 bool IsCacheEntryFresh(const URLResponsePtr& response,
218 const CacheEntryPtr& entry) {
219 DCHECK(response);
220 if (!IsCacheEntryValid(entry))
246 return false; 221 return false;
247 222
248 // If |entry| or |response| has not headers, it is not possible to check if 223 // If |entry| or |response| has not headers, it is not possible to check if
249 // the entry is valid, so returns |false|. 224 // the entry is valid, so returns |false|.
250 if (entry->headers.is_null() || response->headers.is_null()) 225 if (entry->response->headers.is_null() || response->headers.is_null())
251 return false; 226 return false;
252 227
253 // Only handle etag for the moment. 228 // Only handle etag for the moment.
254 std::string etag_header_name = kEtagHeader; 229 std::string etag_header_name = kEtagHeader;
255 std::vector<std::string> entry_etags = 230 std::vector<std::string> entry_etags =
256 GetHeaderValues(etag_header_name, entry->headers); 231 GetHeaderValues(etag_header_name, entry->response->headers);
257 if (entry_etags.size() == 0) 232 if (entry_etags.size() == 0)
258 return false; 233 return false;
259 std::vector<std::string> response_etags = 234 std::vector<std::string> response_etags =
260 GetHeaderValues(etag_header_name, response->headers); 235 GetHeaderValues(etag_header_name, response->headers);
261 if (response_etags.size() == 0) 236 if (response_etags.size() == 0)
262 return false; 237 return false;
263 238
264 // Looking for the first etag header. 239 // Looking for the first etag header.
265 bool result = entry_etags[0] == response_etags[0]; 240 return entry_etags[0] == response_etags[0];
241 }
266 242
267 // Returns |entry| if requested. 243 void PruneCache(scoped_refptr<URLResponseDiskCacheDB> db,
268 if (output) 244 const scoped_ptr<DBReader>& snapshot) {
269 *output = entry.Pass(); 245 LevelDBKeyPtr last_key;
270 246 LevelDBKeyPtr key;
271 return result; 247 CacheEntryPtr entry;
248 scoped_ptr<DBReader::Iterator> it = snapshot->Iterate();
249 while (it->HasNext()) {
250 it->GetNext(&key, &entry);
251 if (last_key && last_key->request_origin == key->request_origin &&
252 last_key->url == key->url) {
253 base::FilePath cache_directory = base::FilePath(entry->cache_directory);
254 if (base::DeleteFile(cache_directory, true))
255 db->Delete(key.Clone());
256 }
257 last_key = key.Pass();
258 }
272 } 259 }
273 260
274 } // namespace 261 } // namespace
275 262
276 // static 263 // static
277 void URLResponseDiskCacheImpl::ClearCache(base::TaskRunner* task_runner) { 264 scoped_refptr<URLResponseDiskCacheDB> URLResponseDiskCacheImpl::CreateDB(
278 // Create a unique subdirectory in trash. 265 scoped_refptr<base::TaskRunner> task_runner,
266 bool force_clean) {
267 // Create the trash directory if needed.
279 base::FilePath trash_dir = GetTrashDirectory(); 268 base::FilePath trash_dir = GetTrashDirectory();
280 base::CreateDirectory(trash_dir); 269 base::CreateDirectory(trash_dir);
281 base::FilePath dest_dir;
282 base::CreateTemporaryDirInDir(trash_dir, "", &dest_dir);
283 270
284 // Move the current cache directory, if present, into trash. 271 // Clean the trash directory when exiting this method.
285 base::FilePath cache_dir = GetCacheDirectory(); 272 base::ScopedClosureRunner trash_cleanup(
286 if (PathExists(cache_dir)) { 273 base::Bind(&ClearTrashDir, task_runner, trash_dir));
287 base::File::Error error; 274
288 if (!base::ReplaceFile(cache_dir, dest_dir, &error)) { 275 // Move the staging directory to trash.
289 LOG(ERROR) << "Failed to clear cache content: " << error; 276 MovePathInDir(GetStagingDirectory(), trash_dir);
277 // And recreate it.
278 base::CreateDirectory(GetStagingDirectory());
279
280 base::FilePath db_path = GetBaseDirectory().Append("db");
281
282 if (!force_clean && PathExists(db_path)) {
283 scoped_refptr<URLResponseDiskCacheDB> db =
284 new URLResponseDiskCacheDB(db_path);
285 if (db->GetVersion() == kCurrentVersion) {
286 task_runner->PostDelayedTask(
287 FROM_HERE,
288 base::Bind(&PruneCache, db, base::Passed(db->GetSnapshot())),
289 base::TimeDelta::FromSeconds(kTrashDelayInSeconds));
290 return db;
290 } 291 }
292 db = nullptr;
291 } 293 }
292 294
293 // Delete the trash directory. 295 // Move the database to trash.
294 task_runner->PostTask( 296 MovePathInDir(db_path, trash_dir);
295 FROM_HERE, 297 // Move the current cache content to trash.
296 base::Bind(base::IgnoreResult(&base::DeleteFile), trash_dir, true)); 298 MovePathInDir(GetCacheDirectory(), trash_dir);
299
300 scoped_refptr<URLResponseDiskCacheDB> result =
301 new URLResponseDiskCacheDB(db_path);
302 result->SetVersion(kCurrentVersion);
303 return result;
297 } 304 }
298 305
299 URLResponseDiskCacheImpl::URLResponseDiskCacheImpl( 306 URLResponseDiskCacheImpl::URLResponseDiskCacheImpl(
300 base::TaskRunner* task_runner, 307 scoped_refptr<base::TaskRunner> task_runner,
308 scoped_refptr<URLResponseDiskCacheDB> db,
301 const std::string& remote_application_url, 309 const std::string& remote_application_url,
302 InterfaceRequest<URLResponseDiskCache> request) 310 InterfaceRequest<URLResponseDiskCache> request)
303 : task_runner_(task_runner), binding_(this, request.Pass()) { 311 : task_runner_(task_runner), db_(db), binding_(this, request.Pass()) {
304 base_directory_ = GetCacheDirectory(); 312 request_origin_ = GURL(remote_application_url).GetOrigin().spec();
305 // The cached files are shared only for application of the same origin. 313 cache_directory_ = GetCacheDirectory();
306 if (remote_application_url != "") {
307 base_directory_ = base_directory_.Append(
308 EncodeString(GURL(remote_application_url).GetOrigin().spec()));
309 }
310 } 314 }
311 315
312 URLResponseDiskCacheImpl::~URLResponseDiskCacheImpl() { 316 URLResponseDiskCacheImpl::~URLResponseDiskCacheImpl() {
313 } 317 }
314 318
315 void URLResponseDiskCacheImpl::GetFile(URLResponsePtr response, 319 void URLResponseDiskCacheImpl::GetFile(URLResponsePtr response,
316 const GetFileCallback& callback) { 320 const GetFileCallback& callback) {
317 return GetFileInternal(response.Pass(), 321 return GetFileInternal(response.Pass(),
318 base::Bind(&RunMojoCallback, callback)); 322 base::Bind(&RunMojoCallback, callback));
319 } 323 }
320 324
325 void URLResponseDiskCacheImpl::GetCachedResponse(
326 const String& url,
327 const GetCachedResponseCallback& callback) {
328 CacheEntryPtr entry = db_->Get(request_origin_, CanonicalizeURL(url));
329 if (!IsCacheEntryValid(entry)) {
330 callback.Run(URLResponsePtr(), Array<uint8_t>(), Array<uint8_t>());
331 return;
332 }
333 callback.Run(
334 entry->response.Pass(), PathToArray(base::FilePath(entry->content_path)),
335 PathToArray(
336 GetConsumerCacheDirectory(base::FilePath(entry->cache_directory))));
337 }
338
339 void URLResponseDiskCacheImpl::UpdateCache(URLResponsePtr response) {
340 GetFileInternal(response.Pass(), base::Bind(&DoNothing));
341 }
342
321 void URLResponseDiskCacheImpl::GetExtractedContent( 343 void URLResponseDiskCacheImpl::GetExtractedContent(
322 URLResponsePtr response, 344 URLResponsePtr response,
323 const GetExtractedContentCallback& callback) { 345 const GetExtractedContentCallback& callback) {
324 base::FilePath dir = GetDirName(base_directory_, response->url); 346 if (response->error ||
325 base::FilePath extracted_dir = dir.Append("extracted"); 347 (response->status_code >= 400 && response->status_code < 600)) {
326 if (IsCacheEntryValid(dir, response.get(), nullptr) && 348 callback.Run(Array<uint8_t>(), Array<uint8_t>());
327 PathExists(GetExtractedSentinel(dir))) {
328 callback.Run(PathToArray(extracted_dir),
329 PathToArray(GetConsumerCacheDirectory(dir)));
330 return; 349 return;
331 } 350 }
332 351
333 GetFileInternal( 352 std::string url = CanonicalizeURL(response->url);
334 response.Pass(), 353
335 base::Bind(&URLResponseDiskCacheImpl::GetExtractedContentInternal, 354 // Check if the response is cached and valid. If that's the case, returns the
336 base::Unretained(this), base::Bind(&RunMojoCallback, callback), 355 // cached value.
337 dir, extracted_dir)); 356 CacheEntryPtr entry = db_->Get(request_origin_, url);
357
358 if (!IsCacheEntryFresh(response, entry)) {
359 GetFileInternal(
360 response.Pass(),
361 base::Bind(&URLResponseDiskCacheImpl::GetExtractedContentInternal,
362 base::Unretained(this),
363 base::Bind(&RunMojoCallback, callback)));
364 return;
365 }
366
367 base::FilePath cache_directory = base::FilePath(entry->cache_directory);
368 base::FilePath extracted_directory = GetExtractedDirectory(cache_directory);
369 if (PathExists(GetExtractedSentinel(cache_directory))) {
370 callback.Run(PathToArray(extracted_directory),
371 PathToArray(GetConsumerCacheDirectory(cache_directory)));
372 return;
373 }
374
375 GetExtractedContentInternal(base::Bind(&RunMojoCallback, callback),
376 base::FilePath(entry->content_path),
377 GetConsumerCacheDirectory(cache_directory));
338 } 378 }
339 379
340 void URLResponseDiskCacheImpl::GetFileInternal( 380 void URLResponseDiskCacheImpl::GetFileInternal(
341 URLResponsePtr response, 381 URLResponsePtr response,
342 const FilePathPairCallback& callback) { 382 const FilePathPairCallback& callback) {
343 base::FilePath dir = GetDirName(base_directory_, response->url); 383 if (response->error ||
344 384 (response->status_code >= 400 && response->status_code < 600)) {
345 // Check if the response is cached and valid. If that's the case, returns the
346 // cached value.
347 CacheEntryPtr entry;
348 if (IsCacheEntryValid(dir, response.get(), &entry)) {
349 callback.Run(base::FilePath(entry->content_path),
350 GetConsumerCacheDirectory(dir));
351 return;
352 }
353
354 // As the response was either not cached or the cached value is not valid, if
355 // the cache directory for the response exists, it needs to be cleaned.
356 if (base::PathExists(dir)) {
357 base::FilePath to_delete;
358 CHECK(CreateTemporaryDirInDir(base_directory_, "to_delete", &to_delete));
359 CHECK(Move(dir, to_delete));
360 task_runner_->PostTask(
361 FROM_HERE,
362 base::Bind(base::IgnoreResult(&base::DeleteFile), to_delete, true));
363 }
364
365 // If the response has not a valid body, and it is not possible to create
366 // either the cache directory or the consumer cache directory, returns an
367 // error.
368 if (!response->body.is_valid() ||
369 !base::CreateDirectoryAndGetError(dir, nullptr) ||
370 !base::CreateDirectoryAndGetError(GetConsumerCacheDirectory(dir),
371 nullptr)) {
372 callback.Run(base::FilePath(), base::FilePath()); 385 callback.Run(base::FilePath(), base::FilePath());
373 return; 386 return;
374 } 387 }
375 388
376 // Fill the entry values for the request. 389 std::string url = CanonicalizeURL(response->url);
377 base::FilePath entry_path = dir.Append(kEntryName); 390
378 base::FilePath content; 391 // Check if the response is cached and valid. If that's the case, returns
379 CHECK(CreateTemporaryFileInDir(dir, &content)); 392 // the cached value.
380 entry = CacheEntry::New(); 393 CacheEntryPtr entry = db_->Get(request_origin_, url);
381 entry->version = kCurrentVersion; 394 if (IsCacheEntryFresh(response, entry)) {
382 entry->url = response->url; 395 callback.Run(
383 entry->content_path = content.value(); 396 base::FilePath(entry->content_path),
384 for (size_t i = 0u; i < response->headers.size(); ++i) { 397 GetConsumerCacheDirectory(base::FilePath(entry->cache_directory)));
385 auto cache_header = CacheHeaders::New(); 398 return;
386 cache_header->name = response->headers[i]->name;
387 cache_header->value = response->headers[i]->value;
388 entry->headers.push_back(cache_header.Pass());
389 } 399 }
390 // Asynchronously copy the response body to the cached file. The entry is send 400
391 // to the callback so that it is saved on disk only if the copy of the body 401 if (!response->body.is_valid()) {
392 // succeded. 402 callback.Run(base::FilePath(), base::FilePath());
403 return;
404 }
405
406 std::string identifier = GetNewIdentifier();
407 // The content is copied to the staging directory so that files are not leaked
408 // if the shell terminates before an entry is saved to the database.
409 base::FilePath staging_content = GetStagingDirectory().Append(identifier);
410
411 ScopedDataPipeConsumerHandle body = response->body.Pass();
412
413 // Asynchronously copy the response body to the staging directory. The
414 // callback will move it to the cache directory and save an entry in the
415 // database only if the copy of the body succeded.
393 common::CopyToFile( 416 common::CopyToFile(
394 response->body.Pass(), content, task_runner_, 417 body.Pass(), staging_content, task_runner_.get(),
395 base::Bind(&RunCallbackWithSuccess, callback, content, 418 base::Bind(&RunCallbackWithSuccess, callback, identifier, request_origin_,
396 GetConsumerCacheDirectory(dir), entry_path, 419 url, base::Passed(response.Pass()), db_, task_runner_));
397 base::Passed(entry.Pass()), base::Unretained(task_runner_)));
398 } 420 }
399 421
400 void URLResponseDiskCacheImpl::GetExtractedContentInternal( 422 void URLResponseDiskCacheImpl::GetExtractedContentInternal(
401 const FilePathPairCallback& callback, 423 const FilePathPairCallback& callback,
402 const base::FilePath& base_dir, 424 const base::FilePath& content_path,
403 const base::FilePath& extracted_dir, 425 const base::FilePath& consumer_cache_directory) {
404 const base::FilePath& content,
405 const base::FilePath& cache_dir) {
406 TRACE_EVENT1("url_response_disk_cache", "GetExtractedContentInternal", 426 TRACE_EVENT1("url_response_disk_cache", "GetExtractedContentInternal",
407 "extracted_dir", extracted_dir.value()); 427 "content_path", content_path.value());
408 // If it is not possible to get the cached file, returns an error. 428 // If it is not possible to get the cached file, returns an error.
409 if (content.empty()) { 429 if (content_path.empty()) {
410 callback.Run(base::FilePath(), base::FilePath()); 430 callback.Run(base::FilePath(), base::FilePath());
411 return; 431 return;
412 } 432 }
413 433
434 base::FilePath cache_directory = consumer_cache_directory.DirName();
435 base::FilePath extracted_directory = GetExtractedDirectory(cache_directory);
436 base::FilePath extracted_sentinel = GetExtractedSentinel(cache_directory);
437
438 if (PathExists(extracted_sentinel)) {
439 callback.Run(extracted_directory, consumer_cache_directory);
440 return;
441 }
442
443 if (PathExists(extracted_directory)) {
444 if (!base::DeleteFile(extracted_directory, true)) {
445 callback.Run(base::FilePath(), base::FilePath());
446 return;
447 }
448 }
449
414 // Unzip the content to the extracted directory. In case of any error, returns 450 // Unzip the content to the extracted directory. In case of any error, returns
415 // an error. 451 // an error.
416 zip::ZipReader reader; 452 zip::ZipReader reader;
417 if (!reader.Open(content)) { 453 if (!reader.Open(content_path)) {
418 callback.Run(base::FilePath(), base::FilePath()); 454 callback.Run(base::FilePath(), base::FilePath());
419 return; 455 return;
420 } 456 }
421 while (reader.HasMore()) { 457 while (reader.HasMore()) {
422 bool success = reader.OpenCurrentEntryInZip(); 458 bool success = reader.OpenCurrentEntryInZip();
423 success = success && reader.ExtractCurrentEntryIntoDirectory(extracted_dir); 459 success =
460 success && reader.ExtractCurrentEntryIntoDirectory(extracted_directory);
424 success = success && reader.AdvanceToNextEntry(); 461 success = success && reader.AdvanceToNextEntry();
425 if (!success) { 462 if (!success) {
426 callback.Run(base::FilePath(), base::FilePath()); 463 callback.Run(base::FilePath(), base::FilePath());
427 return; 464 return;
428 } 465 }
429 } 466 }
430 // We can ignore write error, as it will just force to clear the cache on the 467 // We can ignore write error, as it will just force to clear the cache on the
431 // next request. 468 // next request.
432 WriteFile(GetExtractedSentinel(base_dir), nullptr, 0); 469 WriteFile(GetExtractedSentinel(cache_directory), nullptr, 0);
433 callback.Run(extracted_dir, cache_dir); 470 callback.Run(extracted_directory, consumer_cache_directory);
434 } 471 }
435 472
436 } // namespace mojo 473 } // namespace mojo
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698