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 "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/format_macros.h" | 8 #include "base/format_macros.h" |
9 #include "base/logging.h" | 9 #include "base/logging.h" |
| 10 #include "base/macros.h" |
10 #include "base/message_loop/message_loop.h" | 11 #include "base/message_loop/message_loop.h" |
11 #include "base/strings/string_util.h" | 12 #include "base/strings/string_util.h" |
12 #include "base/strings/stringprintf.h" | 13 #include "base/strings/stringprintf.h" |
13 #include "base/time/time.h" | 14 #include "base/time/time.h" |
14 #include "net/base/io_buffer.h" | 15 #include "net/base/io_buffer.h" |
15 #include "net/base/net_errors.h" | 16 #include "net/base/net_errors.h" |
16 #include "net/disk_cache/blockfile/backend_impl.h" | 17 #include "net/disk_cache/blockfile/backend_impl.h" |
17 #include "net/disk_cache/blockfile/entry_impl.h" | 18 #include "net/disk_cache/blockfile/entry_impl.h" |
18 #include "net/disk_cache/blockfile/file.h" | 19 #include "net/disk_cache/blockfile/file.h" |
19 #include "net/disk_cache/net_log_parameters.h" | 20 #include "net/disk_cache/net_log_parameters.h" |
(...skipping 15 matching lines...) Expand all Loading... |
35 const int kMaxEntrySize = 0x100000; | 36 const int kMaxEntrySize = 0x100000; |
36 | 37 |
37 // The size of each data block (tracked by the child allocation bitmap). | 38 // The size of each data block (tracked by the child allocation bitmap). |
38 const int kBlockSize = 1024; | 39 const int kBlockSize = 1024; |
39 | 40 |
40 // Returns the name of a child entry given the base_name and signature of the | 41 // Returns the name of a child entry given the base_name and signature of the |
41 // parent and the child_id. | 42 // parent and the child_id. |
42 // If the entry is called entry_name, child entries will be named something | 43 // If the entry is called entry_name, child entries will be named something |
43 // like Range_entry_name:XXX:YYY where XXX is the entry signature and YYY is the | 44 // like Range_entry_name:XXX:YYY where XXX is the entry signature and YYY is the |
44 // number of the particular child. | 45 // number of the particular child. |
45 std::string GenerateChildName(const std::string& base_name, int64 signature, | 46 std::string GenerateChildName(const std::string& base_name, |
46 int64 child_id) { | 47 int64_t signature, |
| 48 int64_t child_id) { |
47 return base::StringPrintf("Range_%s:%" PRIx64 ":%" PRIx64, base_name.c_str(), | 49 return base::StringPrintf("Range_%s:%" PRIx64 ":%" PRIx64, base_name.c_str(), |
48 signature, child_id); | 50 signature, child_id); |
49 } | 51 } |
50 | 52 |
51 // This class deletes the children of a sparse entry. | 53 // This class deletes the children of a sparse entry. |
52 class ChildrenDeleter | 54 class ChildrenDeleter |
53 : public base::RefCounted<ChildrenDeleter>, | 55 : public base::RefCounted<ChildrenDeleter>, |
54 public disk_cache::FileIOCallback { | 56 public disk_cache::FileIOCallback { |
55 public: | 57 public: |
56 ChildrenDeleter(disk_cache::BackendImpl* backend, const std::string& name) | 58 ChildrenDeleter(disk_cache::BackendImpl* backend, const std::string& name) |
57 : backend_(backend->GetWeakPtr()), name_(name), signature_(0) {} | 59 : backend_(backend->GetWeakPtr()), name_(name), signature_(0) {} |
58 | 60 |
59 void OnFileIOComplete(int bytes_copied) override; | 61 void OnFileIOComplete(int bytes_copied) override; |
60 | 62 |
61 // Two ways of deleting the children: if we have the children map, use Start() | 63 // Two ways of deleting the children: if we have the children map, use Start() |
62 // directly, otherwise pass the data address to ReadData(). | 64 // directly, otherwise pass the data address to ReadData(). |
63 void Start(char* buffer, int len); | 65 void Start(char* buffer, int len); |
64 void ReadData(disk_cache::Addr address, int len); | 66 void ReadData(disk_cache::Addr address, int len); |
65 | 67 |
66 private: | 68 private: |
67 friend class base::RefCounted<ChildrenDeleter>; | 69 friend class base::RefCounted<ChildrenDeleter>; |
68 ~ChildrenDeleter() override {} | 70 ~ChildrenDeleter() override {} |
69 | 71 |
70 void DeleteChildren(); | 72 void DeleteChildren(); |
71 | 73 |
72 base::WeakPtr<disk_cache::BackendImpl> backend_; | 74 base::WeakPtr<disk_cache::BackendImpl> backend_; |
73 std::string name_; | 75 std::string name_; |
74 disk_cache::Bitmap children_map_; | 76 disk_cache::Bitmap children_map_; |
75 int64 signature_; | 77 int64_t signature_; |
76 scoped_ptr<char[]> buffer_; | 78 scoped_ptr<char[]> buffer_; |
77 DISALLOW_COPY_AND_ASSIGN(ChildrenDeleter); | 79 DISALLOW_COPY_AND_ASSIGN(ChildrenDeleter); |
78 }; | 80 }; |
79 | 81 |
80 // This is the callback of the file operation. | 82 // This is the callback of the file operation. |
81 void ChildrenDeleter::OnFileIOComplete(int bytes_copied) { | 83 void ChildrenDeleter::OnFileIOComplete(int bytes_copied) { |
82 char* buffer = buffer_.release(); | 84 char* buffer = buffer_.release(); |
83 Start(buffer, bytes_copied); | 85 Start(buffer, bytes_copied); |
84 } | 86 } |
85 | 87 |
(...skipping 129 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
215 bool SparseControl::CouldBeSparse() const { | 217 bool SparseControl::CouldBeSparse() const { |
216 DCHECK(!init_); | 218 DCHECK(!init_); |
217 | 219 |
218 if (entry_->GetDataSize(kSparseData)) | 220 if (entry_->GetDataSize(kSparseData)) |
219 return false; | 221 return false; |
220 | 222 |
221 // We don't verify the data, just see if it could be there. | 223 // We don't verify the data, just see if it could be there. |
222 return (entry_->GetDataSize(kSparseIndex) != 0); | 224 return (entry_->GetDataSize(kSparseIndex) != 0); |
223 } | 225 } |
224 | 226 |
225 int SparseControl::StartIO(SparseOperation op, int64 offset, net::IOBuffer* buf, | 227 int SparseControl::StartIO(SparseOperation op, |
226 int buf_len, const CompletionCallback& callback) { | 228 int64_t offset, |
| 229 net::IOBuffer* buf, |
| 230 int buf_len, |
| 231 const CompletionCallback& callback) { |
227 DCHECK(init_); | 232 DCHECK(init_); |
228 // We don't support simultaneous IO for sparse data. | 233 // We don't support simultaneous IO for sparse data. |
229 if (operation_ != kNoOperation) | 234 if (operation_ != kNoOperation) |
230 return net::ERR_CACHE_OPERATION_NOT_SUPPORTED; | 235 return net::ERR_CACHE_OPERATION_NOT_SUPPORTED; |
231 | 236 |
232 if (offset < 0 || buf_len < 0) | 237 if (offset < 0 || buf_len < 0) |
233 return net::ERR_INVALID_ARGUMENT; | 238 return net::ERR_INVALID_ARGUMENT; |
234 | 239 |
235 // We only support up to 64 GB. | 240 // We only support up to 64 GB. |
236 if (offset + buf_len >= 0x1000000000LL || offset + buf_len < 0) | 241 if (offset + buf_len >= 0x1000000000LL || offset + buf_len < 0) |
(...skipping 28 matching lines...) Expand all Loading... |
265 // Everything was done synchronously. | 270 // Everything was done synchronously. |
266 operation_ = kNoOperation; | 271 operation_ = kNoOperation; |
267 user_buf_ = NULL; | 272 user_buf_ = NULL; |
268 user_callback_.Reset(); | 273 user_callback_.Reset(); |
269 return result_; | 274 return result_; |
270 } | 275 } |
271 | 276 |
272 return net::ERR_IO_PENDING; | 277 return net::ERR_IO_PENDING; |
273 } | 278 } |
274 | 279 |
275 int SparseControl::GetAvailableRange(int64 offset, int len, int64* start) { | 280 int SparseControl::GetAvailableRange(int64_t offset, int len, int64_t* start) { |
276 DCHECK(init_); | 281 DCHECK(init_); |
277 // We don't support simultaneous IO for sparse data. | 282 // We don't support simultaneous IO for sparse data. |
278 if (operation_ != kNoOperation) | 283 if (operation_ != kNoOperation) |
279 return net::ERR_CACHE_OPERATION_NOT_SUPPORTED; | 284 return net::ERR_CACHE_OPERATION_NOT_SUPPORTED; |
280 | 285 |
281 DCHECK(start); | 286 DCHECK(start); |
282 | 287 |
283 range_found_ = false; | 288 range_found_ = false; |
284 int result = StartIO( | 289 int result = StartIO( |
285 kGetRangeOperation, offset, NULL, len, CompletionCallback()); | 290 kGetRangeOperation, offset, NULL, len, CompletionCallback()); |
(...skipping 146 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
432 | 437 |
433 // Read the actual bitmap. | 438 // Read the actual bitmap. |
434 buf = new net::IOBuffer(map_len); | 439 buf = new net::IOBuffer(map_len); |
435 rv = entry_->ReadData(kSparseIndex, sizeof(sparse_header_), buf.get(), | 440 rv = entry_->ReadData(kSparseIndex, sizeof(sparse_header_), buf.get(), |
436 map_len, CompletionCallback()); | 441 map_len, CompletionCallback()); |
437 if (rv != map_len) | 442 if (rv != map_len) |
438 return net::ERR_CACHE_READ_FAILURE; | 443 return net::ERR_CACHE_READ_FAILURE; |
439 | 444 |
440 // Grow the bitmap to the current size and copy the bits. | 445 // Grow the bitmap to the current size and copy the bits. |
441 children_map_.Resize(map_len * 8, false); | 446 children_map_.Resize(map_len * 8, false); |
442 children_map_.SetMap(reinterpret_cast<uint32*>(buf->data()), map_len); | 447 children_map_.SetMap(reinterpret_cast<uint32_t*>(buf->data()), map_len); |
443 return net::OK; | 448 return net::OK; |
444 } | 449 } |
445 | 450 |
446 bool SparseControl::OpenChild() { | 451 bool SparseControl::OpenChild() { |
447 DCHECK_GE(result_, 0); | 452 DCHECK_GE(result_, 0); |
448 | 453 |
449 std::string key = GenerateChildKey(); | 454 std::string key = GenerateChildKey(); |
450 if (child_) { | 455 if (child_) { |
451 // Keep using the same child or open another one?. | 456 // Keep using the same child or open another one?. |
452 if (key == child_->GetKey()) | 457 if (key == child_->GetKey()) |
(...skipping 406 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
859 DoAbortCallbacks(); | 864 DoAbortCallbacks(); |
860 return; | 865 return; |
861 } | 866 } |
862 | 867 |
863 // We are running a callback from the message loop. It's time to restart what | 868 // We are running a callback from the message loop. It's time to restart what |
864 // we were doing before. | 869 // we were doing before. |
865 DoChildrenIO(); | 870 DoChildrenIO(); |
866 } | 871 } |
867 | 872 |
868 } // namespace disk_cache | 873 } // namespace disk_cache |
OLD | NEW |