• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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
3 // found in the LICENSE file.
4 
5 #include "net/disk_cache/file.h"
6 
7 #include "base/bind.h"
8 #include "base/lazy_instance.h"
9 #include "base/location.h"
10 #include "base/logging.h"
11 #include "base/run_loop.h"
12 #include "base/task_runner_util.h"
13 #include "base/threading/sequenced_worker_pool.h"
14 #include "net/base/net_errors.h"
15 #include "net/disk_cache/disk_cache.h"
16 
17 namespace {
18 
19 // The maximum number of threads for this pool.
20 const int kMaxThreads = 20;
21 
22 class FileWorkerPool : public base::SequencedWorkerPool {
23  public:
FileWorkerPool()24   FileWorkerPool() : base::SequencedWorkerPool(kMaxThreads, "CachePool") {}
25 
26  protected:
~FileWorkerPool()27   virtual ~FileWorkerPool() {}
28 };
29 
30 base::LazyInstance<FileWorkerPool>::Leaky s_worker_pool =
31     LAZY_INSTANCE_INITIALIZER;
32 
33 }  // namespace
34 
35 namespace disk_cache {
36 
File(base::PlatformFile file)37 File::File(base::PlatformFile file)
38     : init_(true),
39       mixed_(true),
40       platform_file_(file),
41       sync_platform_file_(base::kInvalidPlatformFileValue) {
42 }
43 
Init(const base::FilePath & name)44 bool File::Init(const base::FilePath& name) {
45   if (init_)
46     return false;
47 
48   int flags = base::PLATFORM_FILE_OPEN |
49               base::PLATFORM_FILE_READ |
50               base::PLATFORM_FILE_WRITE;
51   platform_file_ = base::CreatePlatformFile(name, flags, NULL, NULL);
52   if (platform_file_ < 0) {
53     platform_file_ = 0;
54     return false;
55   }
56 
57   init_ = true;
58   return true;
59 }
60 
platform_file() const61 base::PlatformFile File::platform_file() const {
62   return platform_file_;
63 }
64 
IsValid() const65 bool File::IsValid() const {
66   if (!init_)
67     return false;
68   return (base::kInvalidPlatformFileValue != platform_file_);
69 }
70 
Read(void * buffer,size_t buffer_len,size_t offset)71 bool File::Read(void* buffer, size_t buffer_len, size_t offset) {
72   DCHECK(init_);
73   if (buffer_len > static_cast<size_t>(kint32max) ||
74       offset > static_cast<size_t>(kint32max)) {
75     return false;
76   }
77 
78   int ret = base::ReadPlatformFile(platform_file_, offset,
79                                    static_cast<char*>(buffer), buffer_len);
80   return (static_cast<size_t>(ret) == buffer_len);
81 }
82 
Write(const void * buffer,size_t buffer_len,size_t offset)83 bool File::Write(const void* buffer, size_t buffer_len, size_t offset) {
84   DCHECK(init_);
85   if (buffer_len > static_cast<size_t>(kint32max) ||
86       offset > static_cast<size_t>(kint32max)) {
87     return false;
88   }
89 
90   int ret = base::WritePlatformFile(platform_file_, offset,
91                                     static_cast<const char*>(buffer),
92                                     buffer_len);
93   return (static_cast<size_t>(ret) == buffer_len);
94 }
95 
Read(void * buffer,size_t buffer_len,size_t offset,FileIOCallback * callback,bool * completed)96 bool File::Read(void* buffer, size_t buffer_len, size_t offset,
97                 FileIOCallback* callback, bool* completed) {
98   DCHECK(init_);
99   if (!callback) {
100     if (completed)
101       *completed = true;
102     return Read(buffer, buffer_len, offset);
103   }
104 
105   if (buffer_len > static_cast<size_t>(kint32max) ||
106       offset > static_cast<size_t>(kint32max)) {
107     return false;
108   }
109 
110   base::PostTaskAndReplyWithResult(
111       s_worker_pool.Pointer(), FROM_HERE,
112       base::Bind(&File::DoRead, this, buffer, buffer_len, offset),
113       base::Bind(&File::OnOperationComplete, this, callback));
114 
115   *completed = false;
116   return true;
117 }
118 
Write(const void * buffer,size_t buffer_len,size_t offset,FileIOCallback * callback,bool * completed)119 bool File::Write(const void* buffer, size_t buffer_len, size_t offset,
120                  FileIOCallback* callback, bool* completed) {
121   DCHECK(init_);
122   if (!callback) {
123     if (completed)
124       *completed = true;
125     return Write(buffer, buffer_len, offset);
126   }
127 
128   if (buffer_len > static_cast<size_t>(kint32max) ||
129       offset > static_cast<size_t>(kint32max)) {
130     return false;
131   }
132 
133   base::PostTaskAndReplyWithResult(
134       s_worker_pool.Pointer(), FROM_HERE,
135       base::Bind(&File::DoWrite, this, buffer, buffer_len, offset),
136       base::Bind(&File::OnOperationComplete, this, callback));
137 
138   *completed = false;
139   return true;
140 }
141 
SetLength(size_t length)142 bool File::SetLength(size_t length) {
143   DCHECK(init_);
144   if (length > kuint32max)
145     return false;
146 
147   return base::TruncatePlatformFile(platform_file_, length);
148 }
149 
GetLength()150 size_t File::GetLength() {
151   DCHECK(init_);
152   int64 len = base::SeekPlatformFile(platform_file_,
153                                      base::PLATFORM_FILE_FROM_END, 0);
154 
155   if (len > static_cast<int64>(kuint32max))
156     return kuint32max;
157 
158   return static_cast<size_t>(len);
159 }
160 
161 // Static.
WaitForPendingIO(int * num_pending_io)162 void File::WaitForPendingIO(int* num_pending_io) {
163   // We are running unit tests so we should wait for all callbacks. Sadly, the
164   // worker pool only waits for tasks on the worker pool, not the "Reply" tasks
165   // so we have to let the current message loop to run.
166   s_worker_pool.Get().FlushForTesting();
167   base::RunLoop().RunUntilIdle();
168 }
169 
170 // Static.
DropPendingIO()171 void File::DropPendingIO() {
172 }
173 
174 
~File()175 File::~File() {
176   if (IsValid())
177     base::ClosePlatformFile(platform_file_);
178 }
179 
180 // Runs on a worker thread.
DoRead(void * buffer,size_t buffer_len,size_t offset)181 int File::DoRead(void* buffer, size_t buffer_len, size_t offset) {
182   if (Read(const_cast<void*>(buffer), buffer_len, offset))
183     return static_cast<int>(buffer_len);
184 
185   return net::ERR_CACHE_READ_FAILURE;
186 }
187 
188 // Runs on a worker thread.
DoWrite(const void * buffer,size_t buffer_len,size_t offset)189 int File::DoWrite(const void* buffer, size_t buffer_len, size_t offset) {
190   if (Write(const_cast<void*>(buffer), buffer_len, offset))
191     return static_cast<int>(buffer_len);
192 
193   return net::ERR_CACHE_WRITE_FAILURE;
194 }
195 
196 // This method actually makes sure that the last reference to the file doesn't
197 // go away on the worker pool.
OnOperationComplete(FileIOCallback * callback,int result)198 void File::OnOperationComplete(FileIOCallback* callback, int result) {
199   callback->OnFileIOComplete(result);
200 }
201 
202 }  // namespace disk_cache
203