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

Side by Side Diff: net/disk_cache/simple/simple_synchronous_entry.cc

Issue 12226095: Make synchronous methods on disk_cache::SimpleEntryImpl() reentrant. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: on top of trunk, ready to land Created 7 years, 9 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 | Annotate | Revision Log
« no previous file with comments | « net/disk_cache/simple/simple_synchronous_entry.h ('k') | 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 (c) 2013 The Chromium Authors. All rights reserved. 1 // Copyright (c) 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 "net/disk_cache/simple/simple_synchronous_entry.h" 5 #include "net/disk_cache/simple/simple_synchronous_entry.h"
6 6
7 #include <algorithm> 7 #include <algorithm>
8 #include <cstring> 8 #include <cstring>
9 #include <limits> 9 #include <limits>
10 10
11 #include "base/basictypes.h" 11 #include "base/basictypes.h"
12 #include "base/compiler_specific.h" 12 #include "base/compiler_specific.h"
13 #include "base/file_util.h" 13 #include "base/file_util.h"
14 #include "base/hash.h" 14 #include "base/hash.h"
15 #include "base/location.h" 15 #include "base/location.h"
16 #include "base/memory/scoped_ptr.h" 16 #include "base/memory/scoped_ptr.h"
17 #include "base/message_loop_proxy.h" 17 #include "base/message_loop_proxy.h"
18 #include "base/sha1.h" 18 #include "base/sha1.h"
19 #include "base/stringprintf.h" 19 #include "base/stringprintf.h"
20 #include "base/task_runner.h" 20 #include "base/task_runner.h"
21 #include "net/base/io_buffer.h" 21 #include "net/base/io_buffer.h"
22 #include "net/base/net_errors.h" 22 #include "net/base/net_errors.h"
23 #include "net/disk_cache/simple/simple_disk_format.h"
24 23
25 using base::ClosePlatformFile; 24 using base::ClosePlatformFile;
26 using base::FilePath; 25 using base::FilePath;
27 using base::GetPlatformFileInfo; 26 using base::GetPlatformFileInfo;
28 using base::PlatformFileError; 27 using base::PlatformFileError;
29 using base::PlatformFileInfo; 28 using base::PlatformFileInfo;
30 using base::PLATFORM_FILE_CREATE; 29 using base::PLATFORM_FILE_CREATE;
31 using base::PLATFORM_FILE_OK; 30 using base::PLATFORM_FILE_OK;
32 using base::PLATFORM_FILE_OPEN; 31 using base::PLATFORM_FILE_OPEN;
33 using base::PLATFORM_FILE_READ; 32 using base::PLATFORM_FILE_READ;
(...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after
97 } 96 }
98 callback_runner->PostTask(FROM_HERE, base::Bind(callback, sync_entry)); 97 callback_runner->PostTask(FROM_HERE, base::Bind(callback, sync_entry));
99 } 98 }
100 99
101 // static 100 // static
102 void SimpleSynchronousEntry::DoomEntry( 101 void SimpleSynchronousEntry::DoomEntry(
103 const FilePath& path, 102 const FilePath& path,
104 const std::string& key, 103 const std::string& key,
105 scoped_refptr<TaskRunner> callback_runner, 104 scoped_refptr<TaskRunner> callback_runner,
106 const net::CompletionCallback& callback) { 105 const net::CompletionCallback& callback) {
107 for (int i = 0; i < kIndexCount; ++i) { 106 for (int i = 0; i < kSimpleEntryFileCount; ++i) {
108 FilePath to_delete = path.AppendASCII(GetFilenameForKeyAndIndex(key, i)); 107 FilePath to_delete = path.AppendASCII(GetFilenameForKeyAndIndex(key, i));
109 bool ALLOW_UNUSED result = file_util::Delete(to_delete, false); 108 bool ALLOW_UNUSED result = file_util::Delete(to_delete, false);
110 DLOG_IF(ERROR, !result) << "Could not delete " << to_delete.MaybeAsASCII(); 109 DLOG_IF(ERROR, !result) << "Could not delete " << to_delete.MaybeAsASCII();
111 } 110 }
112 if (!callback.is_null()) 111 if (!callback.is_null())
113 callback_runner->PostTask(FROM_HERE, base::Bind(callback, net::OK)); 112 callback_runner->PostTask(FROM_HERE, base::Bind(callback, net::OK));
114 } 113 }
115 114
116 void SimpleSynchronousEntry::DoomAndClose() { 115 void SimpleSynchronousEntry::DoomAndClose() {
117 scoped_refptr<TaskRunner> callback_runner = callback_runner_; 116 scoped_refptr<TaskRunner> callback_runner = callback_runner_;
118 FilePath path = path_; 117 FilePath path = path_;
119 std::string key = key_; 118 std::string key = key_;
120 119
121 Close(); 120 Close();
122 // |this| is now deleted. 121 // |this| is now deleted.
123 122
124 DoomEntry(path, key, callback_runner, net::CompletionCallback()); 123 DoomEntry(path, key, callback_runner, net::CompletionCallback());
125 } 124 }
126 125
127 void SimpleSynchronousEntry::Close() { 126 void SimpleSynchronousEntry::Close() {
128 for (int i = 0; i < kIndexCount; ++i) { 127 for (int i = 0; i < kSimpleEntryFileCount; ++i) {
129 bool ALLOW_UNUSED result = ClosePlatformFile(files_[i]); 128 bool ALLOW_UNUSED result = ClosePlatformFile(files_[i]);
130 DLOG_IF(INFO, !result) << "Could not Close() file."; 129 DLOG_IF(INFO, !result) << "Could not Close() file.";
131 } 130 }
132 delete this; 131 delete this;
133 } 132 }
134 133
135 void SimpleSynchronousEntry::ReadData( 134 void SimpleSynchronousEntry::ReadData(
136 int index, 135 int index,
137 int offset, 136 int offset,
138 net::IOBuffer* buf, 137 net::IOBuffer* buf,
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after
187 : callback_runner_(callback_runner), 186 : callback_runner_(callback_runner),
188 path_(path), 187 path_(path),
189 key_(key), 188 key_(key),
190 initialized_(false) { 189 initialized_(false) {
191 } 190 }
192 191
193 SimpleSynchronousEntry::~SimpleSynchronousEntry() { 192 SimpleSynchronousEntry::~SimpleSynchronousEntry() {
194 } 193 }
195 194
196 bool SimpleSynchronousEntry::OpenOrCreateFiles(bool create) { 195 bool SimpleSynchronousEntry::OpenOrCreateFiles(bool create) {
197 for (int i = 0; i < kIndexCount; ++i) { 196 for (int i = 0; i < kSimpleEntryFileCount; ++i) {
198 FilePath filename = path_.AppendASCII(GetFilenameForKeyAndIndex(key_, i)); 197 FilePath filename = path_.AppendASCII(GetFilenameForKeyAndIndex(key_, i));
199 int flags = PLATFORM_FILE_READ | PLATFORM_FILE_WRITE; 198 int flags = PLATFORM_FILE_READ | PLATFORM_FILE_WRITE;
200 if (create) 199 if (create)
201 flags |= PLATFORM_FILE_CREATE; 200 flags |= PLATFORM_FILE_CREATE;
202 else 201 else
203 flags |= PLATFORM_FILE_OPEN; 202 flags |= PLATFORM_FILE_OPEN;
204 PlatformFileError error; 203 PlatformFileError error;
205 files_[i] = CreatePlatformFile(filename, flags, NULL, &error); 204 files_[i] = CreatePlatformFile(filename, flags, NULL, &error);
206 if (error != PLATFORM_FILE_OK) { 205 if (error != PLATFORM_FILE_OK) {
207 DVLOG(8) << "CreatePlatformFile error " << error << " while " 206 DVLOG(8) << "CreatePlatformFile error " << error << " while "
208 << (create ? "creating " : "opening ") 207 << (create ? "creating " : "opening ")
209 << filename.MaybeAsASCII(); 208 << filename.MaybeAsASCII();
210 while (--i >= 0) { 209 while (--i >= 0) {
211 bool ALLOW_UNUSED did_close = ClosePlatformFile(files_[i]); 210 bool ALLOW_UNUSED did_close = ClosePlatformFile(files_[i]);
212 DLOG_IF(INFO, !did_close) << "Could not close file " 211 DLOG_IF(INFO, !did_close) << "Could not close file "
213 << filename.MaybeAsASCII(); 212 << filename.MaybeAsASCII();
214 } 213 }
215 return false; 214 return false;
216 } 215 }
217 } 216 }
218 217
219 for (int i = 0; i < kIndexCount; ++i) { 218 for (int i = 0; i < kSimpleEntryFileCount; ++i) {
220 PlatformFileInfo file_info; 219 PlatformFileInfo file_info;
221 bool success = GetPlatformFileInfo(files_[i], &file_info); 220 bool success = GetPlatformFileInfo(files_[i], &file_info);
222 if (!success) { 221 if (!success) {
223 DLOG(WARNING) << "Could not get platform file info."; 222 DLOG(WARNING) << "Could not get platform file info.";
224 continue; 223 continue;
225 } 224 }
226 last_used_ = std::max(last_used_, file_info.last_accessed); 225 last_used_ = std::max(last_used_, file_info.last_accessed);
227 last_modified_ = std::max(last_modified_, file_info.last_modified); 226 last_modified_ = std::max(last_modified_, file_info.last_modified);
228 data_size_[i] = DataSizeFromKeyAndFileSize(key_.size(), file_info.size); 227 data_size_[i] = DataSizeFromKeyAndFileSize(key_.size(), file_info.size);
229 } 228 }
230 229
231 return true; 230 return true;
232 } 231 }
233 232
234 bool SimpleSynchronousEntry::InitializeForOpen() { 233 bool SimpleSynchronousEntry::InitializeForOpen() {
235 DCHECK(!initialized_); 234 DCHECK(!initialized_);
236 if (!OpenOrCreateFiles(false)) 235 if (!OpenOrCreateFiles(false))
237 return false; 236 return false;
238 237
239 for (int i = 0; i < kIndexCount; ++i) { 238 for (int i = 0; i < kSimpleEntryFileCount; ++i) {
240 SimpleFileHeader header; 239 SimpleFileHeader header;
241 int header_read_result = 240 int header_read_result =
242 ReadPlatformFile(files_[i], 0, reinterpret_cast<char*>(&header), 241 ReadPlatformFile(files_[i], 0, reinterpret_cast<char*>(&header),
243 sizeof(header)); 242 sizeof(header));
244 if (header_read_result != sizeof(header)) { 243 if (header_read_result != sizeof(header)) {
245 DLOG(WARNING) << "Cannot read header from entry."; 244 DLOG(WARNING) << "Cannot read header from entry.";
246 return false; 245 return false;
247 } 246 }
248 247
249 if (header.initial_magic_number != kSimpleInitialMagicNumber) { 248 if (header.initial_magic_number != kSimpleInitialMagicNumber) {
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after
285 return true; 284 return true;
286 } 285 }
287 286
288 bool SimpleSynchronousEntry::InitializeForCreate() { 287 bool SimpleSynchronousEntry::InitializeForCreate() {
289 DCHECK(!initialized_); 288 DCHECK(!initialized_);
290 if (!OpenOrCreateFiles(true)) { 289 if (!OpenOrCreateFiles(true)) {
291 DLOG(WARNING) << "Could not create platform files."; 290 DLOG(WARNING) << "Could not create platform files.";
292 return false; 291 return false;
293 } 292 }
294 293
295 for (int i = 0; i < kIndexCount; ++i) { 294 for (int i = 0; i < kSimpleEntryFileCount; ++i) {
296 SimpleFileHeader header; 295 SimpleFileHeader header;
297 header.initial_magic_number = kSimpleInitialMagicNumber; 296 header.initial_magic_number = kSimpleInitialMagicNumber;
298 header.version = kSimpleVersion; 297 header.version = kSimpleVersion;
299 298
300 header.key_length = key_.size(); 299 header.key_length = key_.size();
301 header.key_hash = base::Hash(key_); 300 header.key_hash = base::Hash(key_);
302 301
303 if (WritePlatformFile(files_[i], 0, reinterpret_cast<char*>(&header), 302 if (WritePlatformFile(files_[i], 0, reinterpret_cast<char*>(&header),
304 sizeof(header)) != sizeof(header)) { 303 sizeof(header)) != sizeof(header)) {
305 // TODO(gavinp): Clean up created files. 304 // TODO(gavinp): Clean up created files.
306 DLOG(WARNING) << "Could not write headers to new cache entry."; 305 DLOG(WARNING) << "Could not write headers to new cache entry.";
307 return false; 306 return false;
308 } 307 }
309 308
310 if (WritePlatformFile(files_[i], sizeof(header), key_.data(), 309 if (WritePlatformFile(files_[i], sizeof(header), key_.data(),
311 key_.size()) != implicit_cast<int>(key_.size())) { 310 key_.size()) != implicit_cast<int>(key_.size())) {
312 // TODO(gavinp): Clean up created files. 311 // TODO(gavinp): Clean up created files.
313 DLOG(WARNING) << "Could not write keys to new cache entry."; 312 DLOG(WARNING) << "Could not write keys to new cache entry.";
314 return false; 313 return false;
315 } 314 }
316 } 315 }
317 316
318 initialized_ = true; 317 initialized_ = true;
319 return true; 318 return true;
320 } 319 }
321 320
322 } // namespace disk_cache 321 } // namespace disk_cache
OLDNEW
« no previous file with comments | « net/disk_cache/simple/simple_synchronous_entry.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698