| 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/file.h" | 5 #include "net/disk_cache/blockfile/file.h" |
| 6 | 6 |
| 7 #include <stdint.h> |
| 8 |
| 9 #include <limits> |
| 10 |
| 7 #include "base/bind.h" | 11 #include "base/bind.h" |
| 8 #include "base/lazy_instance.h" | 12 #include "base/lazy_instance.h" |
| 9 #include "base/location.h" | 13 #include "base/location.h" |
| 10 #include "base/logging.h" | 14 #include "base/logging.h" |
| 11 #include "base/run_loop.h" | 15 #include "base/run_loop.h" |
| 12 #include "base/task_runner_util.h" | 16 #include "base/task_runner_util.h" |
| 13 #include "base/threading/sequenced_worker_pool.h" | 17 #include "base/threading/sequenced_worker_pool.h" |
| 14 #include "net/base/net_errors.h" | 18 #include "net/base/net_errors.h" |
| 15 #include "net/disk_cache/disk_cache.h" | 19 #include "net/disk_cache/disk_cache.h" |
| 16 | 20 |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 49 base_file_.Initialize(name, flags); | 53 base_file_.Initialize(name, flags); |
| 50 return base_file_.IsValid(); | 54 return base_file_.IsValid(); |
| 51 } | 55 } |
| 52 | 56 |
| 53 bool File::IsValid() const { | 57 bool File::IsValid() const { |
| 54 return base_file_.IsValid(); | 58 return base_file_.IsValid(); |
| 55 } | 59 } |
| 56 | 60 |
| 57 bool File::Read(void* buffer, size_t buffer_len, size_t offset) { | 61 bool File::Read(void* buffer, size_t buffer_len, size_t offset) { |
| 58 DCHECK(base_file_.IsValid()); | 62 DCHECK(base_file_.IsValid()); |
| 59 if (buffer_len > static_cast<size_t>(kint32max) || | 63 if (buffer_len > static_cast<size_t>(std::numeric_limits<int32_t>::max()) || |
| 60 offset > static_cast<size_t>(kint32max)) { | 64 offset > static_cast<size_t>(std::numeric_limits<int32_t>::max())) { |
| 61 return false; | 65 return false; |
| 62 } | 66 } |
| 63 | 67 |
| 64 int ret = base_file_.Read(offset, static_cast<char*>(buffer), buffer_len); | 68 int ret = base_file_.Read(offset, static_cast<char*>(buffer), buffer_len); |
| 65 return (static_cast<size_t>(ret) == buffer_len); | 69 return (static_cast<size_t>(ret) == buffer_len); |
| 66 } | 70 } |
| 67 | 71 |
| 68 bool File::Write(const void* buffer, size_t buffer_len, size_t offset) { | 72 bool File::Write(const void* buffer, size_t buffer_len, size_t offset) { |
| 69 DCHECK(base_file_.IsValid()); | 73 DCHECK(base_file_.IsValid()); |
| 70 if (buffer_len > static_cast<size_t>(kint32max) || | 74 if (buffer_len > static_cast<size_t>(std::numeric_limits<int32_t>::max()) || |
| 71 offset > static_cast<size_t>(kint32max)) { | 75 offset > static_cast<size_t>(std::numeric_limits<int32_t>::max())) { |
| 72 return false; | 76 return false; |
| 73 } | 77 } |
| 74 | 78 |
| 75 int ret = base_file_.Write(offset, static_cast<const char*>(buffer), | 79 int ret = base_file_.Write(offset, static_cast<const char*>(buffer), |
| 76 buffer_len); | 80 buffer_len); |
| 77 return (static_cast<size_t>(ret) == buffer_len); | 81 return (static_cast<size_t>(ret) == buffer_len); |
| 78 } | 82 } |
| 79 | 83 |
| 80 bool File::Read(void* buffer, size_t buffer_len, size_t offset, | 84 bool File::Read(void* buffer, size_t buffer_len, size_t offset, |
| 81 FileIOCallback* callback, bool* completed) { | 85 FileIOCallback* callback, bool* completed) { |
| 82 DCHECK(base_file_.IsValid()); | 86 DCHECK(base_file_.IsValid()); |
| 83 if (!callback) { | 87 if (!callback) { |
| 84 if (completed) | 88 if (completed) |
| 85 *completed = true; | 89 *completed = true; |
| 86 return Read(buffer, buffer_len, offset); | 90 return Read(buffer, buffer_len, offset); |
| 87 } | 91 } |
| 88 | 92 |
| 89 if (buffer_len > static_cast<size_t>(kint32max) || | 93 if (buffer_len > static_cast<size_t>(std::numeric_limits<int32_t>::max()) || |
| 90 offset > static_cast<size_t>(kint32max)) { | 94 offset > static_cast<size_t>(std::numeric_limits<int32_t>::max())) { |
| 91 return false; | 95 return false; |
| 92 } | 96 } |
| 93 | 97 |
| 94 base::PostTaskAndReplyWithResult( | 98 base::PostTaskAndReplyWithResult( |
| 95 s_worker_pool.Pointer(), FROM_HERE, | 99 s_worker_pool.Pointer(), FROM_HERE, |
| 96 base::Bind(&File::DoRead, this, buffer, buffer_len, offset), | 100 base::Bind(&File::DoRead, this, buffer, buffer_len, offset), |
| 97 base::Bind(&File::OnOperationComplete, this, callback)); | 101 base::Bind(&File::OnOperationComplete, this, callback)); |
| 98 | 102 |
| 99 *completed = false; | 103 *completed = false; |
| 100 return true; | 104 return true; |
| 101 } | 105 } |
| 102 | 106 |
| 103 bool File::Write(const void* buffer, size_t buffer_len, size_t offset, | 107 bool File::Write(const void* buffer, size_t buffer_len, size_t offset, |
| 104 FileIOCallback* callback, bool* completed) { | 108 FileIOCallback* callback, bool* completed) { |
| 105 DCHECK(base_file_.IsValid()); | 109 DCHECK(base_file_.IsValid()); |
| 106 if (!callback) { | 110 if (!callback) { |
| 107 if (completed) | 111 if (completed) |
| 108 *completed = true; | 112 *completed = true; |
| 109 return Write(buffer, buffer_len, offset); | 113 return Write(buffer, buffer_len, offset); |
| 110 } | 114 } |
| 111 | 115 |
| 112 if (buffer_len > static_cast<size_t>(kint32max) || | 116 if (buffer_len > static_cast<size_t>(std::numeric_limits<int32_t>::max()) || |
| 113 offset > static_cast<size_t>(kint32max)) { | 117 offset > static_cast<size_t>(std::numeric_limits<int32_t>::max())) { |
| 114 return false; | 118 return false; |
| 115 } | 119 } |
| 116 | 120 |
| 117 base::PostTaskAndReplyWithResult( | 121 base::PostTaskAndReplyWithResult( |
| 118 s_worker_pool.Pointer(), FROM_HERE, | 122 s_worker_pool.Pointer(), FROM_HERE, |
| 119 base::Bind(&File::DoWrite, this, buffer, buffer_len, offset), | 123 base::Bind(&File::DoWrite, this, buffer, buffer_len, offset), |
| 120 base::Bind(&File::OnOperationComplete, this, callback)); | 124 base::Bind(&File::OnOperationComplete, this, callback)); |
| 121 | 125 |
| 122 *completed = false; | 126 *completed = false; |
| 123 return true; | 127 return true; |
| 124 } | 128 } |
| 125 | 129 |
| 126 bool File::SetLength(size_t length) { | 130 bool File::SetLength(size_t length) { |
| 127 DCHECK(base_file_.IsValid()); | 131 DCHECK(base_file_.IsValid()); |
| 128 if (length > kuint32max) | 132 if (length > std::numeric_limits<uint32_t>::max()) |
| 129 return false; | 133 return false; |
| 130 | 134 |
| 131 return base_file_.SetLength(length); | 135 return base_file_.SetLength(length); |
| 132 } | 136 } |
| 133 | 137 |
| 134 size_t File::GetLength() { | 138 size_t File::GetLength() { |
| 135 DCHECK(base_file_.IsValid()); | 139 DCHECK(base_file_.IsValid()); |
| 136 int64 len = base_file_.GetLength(); | 140 int64_t len = base_file_.GetLength(); |
| 137 | 141 |
| 138 if (len > static_cast<int64>(kuint32max)) | 142 if (len > static_cast<int64_t>(std::numeric_limits<uint32_t>::max())) |
| 139 return kuint32max; | 143 return std::numeric_limits<uint32_t>::max(); |
| 140 | 144 |
| 141 return static_cast<size_t>(len); | 145 return static_cast<size_t>(len); |
| 142 } | 146 } |
| 143 | 147 |
| 144 // Static. | 148 // Static. |
| 145 void File::WaitForPendingIO(int* num_pending_io) { | 149 void File::WaitForPendingIO(int* num_pending_io) { |
| 146 // We are running unit tests so we should wait for all callbacks. Sadly, the | 150 // We are running unit tests so we should wait for all callbacks. Sadly, the |
| 147 // worker pool only waits for tasks on the worker pool, not the "Reply" tasks | 151 // worker pool only waits for tasks on the worker pool, not the "Reply" tasks |
| 148 // so we have to let the current message loop to run. | 152 // so we have to let the current message loop to run. |
| 149 s_worker_pool.Get().FlushForTesting(); | 153 s_worker_pool.Get().FlushForTesting(); |
| (...skipping 28 matching lines...) Expand all Loading... |
| 178 return net::ERR_CACHE_WRITE_FAILURE; | 182 return net::ERR_CACHE_WRITE_FAILURE; |
| 179 } | 183 } |
| 180 | 184 |
| 181 // This method actually makes sure that the last reference to the file doesn't | 185 // This method actually makes sure that the last reference to the file doesn't |
| 182 // go away on the worker pool. | 186 // go away on the worker pool. |
| 183 void File::OnOperationComplete(FileIOCallback* callback, int result) { | 187 void File::OnOperationComplete(FileIOCallback* callback, int result) { |
| 184 callback->OnFileIOComplete(result); | 188 callback->OnFileIOComplete(result); |
| 185 } | 189 } |
| 186 | 190 |
| 187 } // namespace disk_cache | 191 } // namespace disk_cache |
| OLD | NEW |