OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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/blockfile/sparse_control.h" | 5 #include "net/disk_cache/blockfile/sparse_control.h" |
6 | 6 |
7 #include <stdint.h> | 7 #include <stdint.h> |
8 | 8 |
9 #include "base/bind.h" | 9 #include "base/bind.h" |
10 #include "base/format_macros.h" | 10 #include "base/format_macros.h" |
11 #include "base/location.h" | 11 #include "base/location.h" |
12 #include "base/logging.h" | 12 #include "base/logging.h" |
| 13 #include "base/macros.h" |
13 #include "base/single_thread_task_runner.h" | 14 #include "base/single_thread_task_runner.h" |
14 #include "base/strings/string_util.h" | 15 #include "base/strings/string_util.h" |
15 #include "base/strings/stringprintf.h" | 16 #include "base/strings/stringprintf.h" |
16 #include "base/thread_task_runner_handle.h" | 17 #include "base/thread_task_runner_handle.h" |
17 #include "base/time/time.h" | 18 #include "base/time/time.h" |
18 #include "net/base/io_buffer.h" | 19 #include "net/base/io_buffer.h" |
19 #include "net/base/net_errors.h" | 20 #include "net/base/net_errors.h" |
20 #include "net/disk_cache/blockfile/backend_impl.h" | 21 #include "net/disk_cache/blockfile/backend_impl.h" |
21 #include "net/disk_cache/blockfile/entry_impl.h" | 22 #include "net/disk_cache/blockfile/entry_impl.h" |
22 #include "net/disk_cache/blockfile/file.h" | 23 #include "net/disk_cache/blockfile/file.h" |
(...skipping 16 matching lines...) Expand all Loading... |
39 const int kMaxEntrySize = 0x100000; | 40 const int kMaxEntrySize = 0x100000; |
40 | 41 |
41 // The size of each data block (tracked by the child allocation bitmap). | 42 // The size of each data block (tracked by the child allocation bitmap). |
42 const int kBlockSize = 1024; | 43 const int kBlockSize = 1024; |
43 | 44 |
44 // Returns the name of a child entry given the base_name and signature of the | 45 // Returns the name of a child entry given the base_name and signature of the |
45 // parent and the child_id. | 46 // parent and the child_id. |
46 // If the entry is called entry_name, child entries will be named something | 47 // If the entry is called entry_name, child entries will be named something |
47 // like Range_entry_name:XXX:YYY where XXX is the entry signature and YYY is the | 48 // like Range_entry_name:XXX:YYY where XXX is the entry signature and YYY is the |
48 // number of the particular child. | 49 // number of the particular child. |
49 std::string GenerateChildName(const std::string& base_name, int64 signature, | 50 std::string GenerateChildName(const std::string& base_name, |
50 int64 child_id) { | 51 int64_t signature, |
| 52 int64_t child_id) { |
51 return base::StringPrintf("Range_%s:%" PRIx64 ":%" PRIx64, base_name.c_str(), | 53 return base::StringPrintf("Range_%s:%" PRIx64 ":%" PRIx64, base_name.c_str(), |
52 signature, child_id); | 54 signature, child_id); |
53 } | 55 } |
54 | 56 |
55 // This class deletes the children of a sparse entry. | 57 // This class deletes the children of a sparse entry. |
56 class ChildrenDeleter | 58 class ChildrenDeleter |
57 : public base::RefCounted<ChildrenDeleter>, | 59 : public base::RefCounted<ChildrenDeleter>, |
58 public disk_cache::FileIOCallback { | 60 public disk_cache::FileIOCallback { |
59 public: | 61 public: |
60 ChildrenDeleter(disk_cache::BackendImpl* backend, const std::string& name) | 62 ChildrenDeleter(disk_cache::BackendImpl* backend, const std::string& name) |
61 : backend_(backend->GetWeakPtr()), name_(name), signature_(0) {} | 63 : backend_(backend->GetWeakPtr()), name_(name), signature_(0) {} |
62 | 64 |
63 void OnFileIOComplete(int bytes_copied) override; | 65 void OnFileIOComplete(int bytes_copied) override; |
64 | 66 |
65 // Two ways of deleting the children: if we have the children map, use Start() | 67 // Two ways of deleting the children: if we have the children map, use Start() |
66 // directly, otherwise pass the data address to ReadData(). | 68 // directly, otherwise pass the data address to ReadData(). |
67 void Start(char* buffer, int len); | 69 void Start(char* buffer, int len); |
68 void ReadData(disk_cache::Addr address, int len); | 70 void ReadData(disk_cache::Addr address, int len); |
69 | 71 |
70 private: | 72 private: |
71 friend class base::RefCounted<ChildrenDeleter>; | 73 friend class base::RefCounted<ChildrenDeleter>; |
72 ~ChildrenDeleter() override {} | 74 ~ChildrenDeleter() override {} |
73 | 75 |
74 void DeleteChildren(); | 76 void DeleteChildren(); |
75 | 77 |
76 base::WeakPtr<disk_cache::BackendImpl> backend_; | 78 base::WeakPtr<disk_cache::BackendImpl> backend_; |
77 std::string name_; | 79 std::string name_; |
78 disk_cache::Bitmap children_map_; | 80 disk_cache::Bitmap children_map_; |
79 int64 signature_; | 81 int64_t signature_; |
80 scoped_ptr<char[]> buffer_; | 82 scoped_ptr<char[]> buffer_; |
81 DISALLOW_COPY_AND_ASSIGN(ChildrenDeleter); | 83 DISALLOW_COPY_AND_ASSIGN(ChildrenDeleter); |
82 }; | 84 }; |
83 | 85 |
84 // This is the callback of the file operation. | 86 // This is the callback of the file operation. |
85 void ChildrenDeleter::OnFileIOComplete(int bytes_copied) { | 87 void ChildrenDeleter::OnFileIOComplete(int bytes_copied) { |
86 char* buffer = buffer_.release(); | 88 char* buffer = buffer_.release(); |
87 Start(buffer, bytes_copied); | 89 Start(buffer, bytes_copied); |
88 } | 90 } |
89 | 91 |
(...skipping 148 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
238 bool SparseControl::CouldBeSparse() const { | 240 bool SparseControl::CouldBeSparse() const { |
239 DCHECK(!init_); | 241 DCHECK(!init_); |
240 | 242 |
241 if (entry_->GetDataSize(kSparseData)) | 243 if (entry_->GetDataSize(kSparseData)) |
242 return false; | 244 return false; |
243 | 245 |
244 // We don't verify the data, just see if it could be there. | 246 // We don't verify the data, just see if it could be there. |
245 return (entry_->GetDataSize(kSparseIndex) != 0); | 247 return (entry_->GetDataSize(kSparseIndex) != 0); |
246 } | 248 } |
247 | 249 |
248 int SparseControl::StartIO(SparseOperation op, int64 offset, net::IOBuffer* buf, | 250 int SparseControl::StartIO(SparseOperation op, |
249 int buf_len, const CompletionCallback& callback) { | 251 int64_t offset, |
| 252 net::IOBuffer* buf, |
| 253 int buf_len, |
| 254 const CompletionCallback& callback) { |
250 DCHECK(init_); | 255 DCHECK(init_); |
251 // We don't support simultaneous IO for sparse data. | 256 // We don't support simultaneous IO for sparse data. |
252 if (operation_ != kNoOperation) | 257 if (operation_ != kNoOperation) |
253 return net::ERR_CACHE_OPERATION_NOT_SUPPORTED; | 258 return net::ERR_CACHE_OPERATION_NOT_SUPPORTED; |
254 | 259 |
255 if (offset < 0 || buf_len < 0) | 260 if (offset < 0 || buf_len < 0) |
256 return net::ERR_INVALID_ARGUMENT; | 261 return net::ERR_INVALID_ARGUMENT; |
257 | 262 |
258 // We only support up to 64 GB. | 263 // We only support up to 64 GB. |
259 if (static_cast<uint64>(offset) + static_cast<unsigned int>(buf_len) >= | 264 if (static_cast<uint64_t>(offset) + static_cast<unsigned int>(buf_len) >= |
260 UINT64_C(0x1000000000)) { | 265 UINT64_C(0x1000000000)) { |
261 return net::ERR_CACHE_OPERATION_NOT_SUPPORTED; | 266 return net::ERR_CACHE_OPERATION_NOT_SUPPORTED; |
262 } | 267 } |
263 | 268 |
264 DCHECK(!user_buf_.get()); | 269 DCHECK(!user_buf_.get()); |
265 DCHECK(user_callback_.is_null()); | 270 DCHECK(user_callback_.is_null()); |
266 | 271 |
267 if (!buf && (op == kReadOperation || op == kWriteOperation)) | 272 if (!buf && (op == kReadOperation || op == kWriteOperation)) |
268 return 0; | 273 return 0; |
269 | 274 |
(...skipping 20 matching lines...) Expand all Loading... |
290 // Everything was done synchronously. | 295 // Everything was done synchronously. |
291 operation_ = kNoOperation; | 296 operation_ = kNoOperation; |
292 user_buf_ = NULL; | 297 user_buf_ = NULL; |
293 user_callback_.Reset(); | 298 user_callback_.Reset(); |
294 return result_; | 299 return result_; |
295 } | 300 } |
296 | 301 |
297 return net::ERR_IO_PENDING; | 302 return net::ERR_IO_PENDING; |
298 } | 303 } |
299 | 304 |
300 int SparseControl::GetAvailableRange(int64 offset, int len, int64* start) { | 305 int SparseControl::GetAvailableRange(int64_t offset, int len, int64_t* start) { |
301 DCHECK(init_); | 306 DCHECK(init_); |
302 // We don't support simultaneous IO for sparse data. | 307 // We don't support simultaneous IO for sparse data. |
303 if (operation_ != kNoOperation) | 308 if (operation_ != kNoOperation) |
304 return net::ERR_CACHE_OPERATION_NOT_SUPPORTED; | 309 return net::ERR_CACHE_OPERATION_NOT_SUPPORTED; |
305 | 310 |
306 DCHECK(start); | 311 DCHECK(start); |
307 | 312 |
308 range_found_ = false; | 313 range_found_ = false; |
309 int result = StartIO( | 314 int result = StartIO( |
310 kGetRangeOperation, offset, NULL, len, CompletionCallback()); | 315 kGetRangeOperation, offset, NULL, len, CompletionCallback()); |
(...skipping 123 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
434 | 439 |
435 // Read the actual bitmap. | 440 // Read the actual bitmap. |
436 buf = new net::IOBuffer(map_len); | 441 buf = new net::IOBuffer(map_len); |
437 rv = entry_->ReadData(kSparseIndex, sizeof(sparse_header_), buf.get(), | 442 rv = entry_->ReadData(kSparseIndex, sizeof(sparse_header_), buf.get(), |
438 map_len, CompletionCallback()); | 443 map_len, CompletionCallback()); |
439 if (rv != map_len) | 444 if (rv != map_len) |
440 return net::ERR_CACHE_READ_FAILURE; | 445 return net::ERR_CACHE_READ_FAILURE; |
441 | 446 |
442 // Grow the bitmap to the current size and copy the bits. | 447 // Grow the bitmap to the current size and copy the bits. |
443 children_map_.Resize(map_len * 8, false); | 448 children_map_.Resize(map_len * 8, false); |
444 children_map_.SetMap(reinterpret_cast<uint32*>(buf->data()), map_len); | 449 children_map_.SetMap(reinterpret_cast<uint32_t*>(buf->data()), map_len); |
445 return net::OK; | 450 return net::OK; |
446 } | 451 } |
447 | 452 |
448 bool SparseControl::OpenChild() { | 453 bool SparseControl::OpenChild() { |
449 DCHECK_GE(result_, 0); | 454 DCHECK_GE(result_, 0); |
450 | 455 |
451 std::string key = GenerateChildKey(); | 456 std::string key = GenerateChildKey(); |
452 if (child_) { | 457 if (child_) { |
453 // Keep using the same child or open another one?. | 458 // Keep using the same child or open another one?. |
454 if (key == child_->GetKey()) | 459 if (key == child_->GetKey()) |
(...skipping 441 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
896 CompletionCallback cb = abort_callbacks_[i]; | 901 CompletionCallback cb = abort_callbacks_[i]; |
897 if (i == abort_callbacks_.size() - 1) | 902 if (i == abort_callbacks_.size() - 1) |
898 abort_callbacks_.clear(); | 903 abort_callbacks_.clear(); |
899 | 904 |
900 entry_->Release(); // Don't touch object after this line. | 905 entry_->Release(); // Don't touch object after this line. |
901 cb.Run(net::OK); | 906 cb.Run(net::OK); |
902 } | 907 } |
903 } | 908 } |
904 | 909 |
905 } // namespace disk_cache | 910 } // namespace disk_cache |
OLD | NEW |