Chromium Code Reviews| OLD | NEW |
|---|---|
| 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 "content/browser/indexed_db/indexed_db_backing_store.h" | 5 #include "content/browser/indexed_db/indexed_db_backing_store.h" |
| 6 | 6 |
| 7 #include "base/file_util.h" | 7 #include "base/file_util.h" |
| 8 #include "base/files/file_path.h" | 8 #include "base/files/file_path.h" |
| 9 #include "base/format_macros.h" | 9 #include "base/format_macros.h" |
| 10 #include "base/json/json_reader.h" | 10 #include "base/json/json_reader.h" |
| (...skipping 2235 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 2246 int64 database_id_; | 2246 int64 database_id_; |
| 2247 IndexedDBBackingStore* backing_store_; | 2247 IndexedDBBackingStore* backing_store_; |
| 2248 scoped_refptr<IndexedDBBackingStore::BlobWriteCallback> callback_; | 2248 scoped_refptr<IndexedDBBackingStore::BlobWriteCallback> callback_; |
| 2249 scoped_ptr<FileWriterDelegate> delegate_; | 2249 scoped_ptr<FileWriterDelegate> delegate_; |
| 2250 bool aborted_; | 2250 bool aborted_; |
| 2251 | 2251 |
| 2252 DISALLOW_COPY_AND_ASSIGN(ChainedBlobWriterImpl); | 2252 DISALLOW_COPY_AND_ASSIGN(ChainedBlobWriterImpl); |
| 2253 }; | 2253 }; |
| 2254 | 2254 |
| 2255 class LocalWriteClosure : public FileWriterDelegate::DelegateWriteCallback, | 2255 class LocalWriteClosure : public FileWriterDelegate::DelegateWriteCallback, |
| 2256 public base::RefCounted<LocalWriteClosure> { | 2256 public base::RefCountedThreadSafe<LocalWriteClosure> { |
| 2257 public: | 2257 public: |
| 2258 LocalWriteClosure(IndexedDBBackingStore::Transaction::ChainedBlobWriter* | 2258 LocalWriteClosure(IndexedDBBackingStore::Transaction::ChainedBlobWriter* |
| 2259 chained_blob_writer, | 2259 chained_blob_writer, |
| 2260 base::TaskRunner* task_runner) | 2260 base::TaskRunner* task_runner) |
| 2261 : chained_blob_writer_(chained_blob_writer), | 2261 : chained_blob_writer_(chained_blob_writer), |
| 2262 task_runner_(task_runner), | 2262 task_runner_(task_runner), |
| 2263 bytes_written_(0) {} | 2263 bytes_written_(0) {} |
| 2264 | 2264 |
| 2265 void Run(base::File::Error rv, | 2265 void Run(base::File::Error rv, |
| 2266 int64 bytes, | 2266 int64 bytes, |
| 2267 FileWriterDelegate::WriteProgressStatus write_status) { | 2267 FileWriterDelegate::WriteProgressStatus write_status) { |
| 2268 DCHECK_GE(bytes, 0); | 2268 DCHECK_GE(bytes, 0); |
| 2269 bytes_written_ += bytes; | 2269 bytes_written_ += bytes; |
| 2270 if (write_status == FileWriterDelegate::SUCCESS_IO_PENDING) | 2270 if (write_status == FileWriterDelegate::SUCCESS_IO_PENDING) |
| 2271 return; // We don't care about progress events. | 2271 return; // We don't care about progress events. |
| 2272 if (rv == base::File::FILE_OK) { | 2272 if (rv == base::File::FILE_OK) { |
| 2273 DCHECK_EQ(write_status, FileWriterDelegate::SUCCESS_COMPLETED); | 2273 DCHECK_EQ(write_status, FileWriterDelegate::SUCCESS_COMPLETED); |
| 2274 } else { | 2274 } else { |
| 2275 DCHECK(write_status == FileWriterDelegate::ERROR_WRITE_STARTED || | 2275 DCHECK(write_status == FileWriterDelegate::ERROR_WRITE_STARTED || |
| 2276 write_status == FileWriterDelegate::ERROR_WRITE_NOT_STARTED); | 2276 write_status == FileWriterDelegate::ERROR_WRITE_NOT_STARTED); |
| 2277 } | 2277 } |
| 2278 task_runner_->PostTask( | 2278 task_runner_->PostTask( |
| 2279 FROM_HERE, | 2279 FROM_HERE, |
| 2280 base::Bind(&LocalWriteClosure::callBlobCallbackOnIDBTaskRunner, | 2280 base::Bind(&LocalWriteClosure::callBlobCallbackOnIDBTaskRunner, |
| 2281 this, | 2281 this, |
| 2282 write_status == FileWriterDelegate::SUCCESS_COMPLETED)); | 2282 write_status == FileWriterDelegate::SUCCESS_COMPLETED)); |
| 2283 } | 2283 } |
| 2284 | 2284 |
| 2285 static void ReleaseWriterOnIDBThread( | |
| 2286 scoped_refptr<IndexedDBBackingStore::Transaction::ChainedBlobWriter> | |
| 2287 chained_blob_writer) { | |
| 2288 // Don't actually release the writer (the closure will do that) Merely | |
|
jsbell
2014/07/23 17:41:15
Nit: Needs a period at the end of the sentence.
cmumford
2014/07/23 17:46:09
Done.
| |
| 2289 // posting the ref counted pointer over to this thread ensures that it is | |
| 2290 // deleted on the correct thread. | |
| 2291 } | |
| 2292 | |
| 2285 void writeBlobToFileOnIOThread(const FilePath& file_path, | 2293 void writeBlobToFileOnIOThread(const FilePath& file_path, |
| 2286 const GURL& blob_url, | 2294 const GURL& blob_url, |
| 2287 net::URLRequestContext* request_context) { | 2295 net::URLRequestContext* request_context) { |
| 2288 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO)); | 2296 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO)); |
| 2289 scoped_ptr<fileapi::FileStreamWriter> writer( | 2297 scoped_ptr<fileapi::FileStreamWriter> writer( |
| 2290 fileapi::FileStreamWriter::CreateForLocalFile( | 2298 fileapi::FileStreamWriter::CreateForLocalFile( |
| 2291 task_runner_, file_path, 0, | 2299 task_runner_, file_path, 0, |
| 2292 fileapi::FileStreamWriter::CREATE_NEW_FILE)); | 2300 fileapi::FileStreamWriter::CREATE_NEW_FILE)); |
| 2293 scoped_ptr<FileWriterDelegate> delegate( | 2301 scoped_ptr<FileWriterDelegate> delegate( |
| 2294 new FileWriterDelegate(writer.Pass(), | 2302 new FileWriterDelegate(writer.Pass(), |
| 2295 FileWriterDelegate::FLUSH_ON_COMPLETION)); | 2303 FileWriterDelegate::FLUSH_ON_COMPLETION)); |
| 2296 | 2304 |
| 2297 DCHECK(blob_url.is_valid()); | 2305 DCHECK(blob_url.is_valid()); |
| 2298 scoped_ptr<net::URLRequest> blob_request(request_context->CreateRequest( | 2306 scoped_ptr<net::URLRequest> blob_request(request_context->CreateRequest( |
| 2299 blob_url, net::DEFAULT_PRIORITY, delegate.get(), NULL)); | 2307 blob_url, net::DEFAULT_PRIORITY, delegate.get(), NULL)); |
| 2300 | 2308 |
| 2301 delegate->Start(blob_request.Pass(), | 2309 delegate->Start(blob_request.Pass(), |
| 2302 base::Bind(&LocalWriteClosure::Run, this)); | 2310 base::Bind(&LocalWriteClosure::Run, this)); |
| 2303 chained_blob_writer_->set_delegate(delegate.Pass()); | 2311 chained_blob_writer_->set_delegate(delegate.Pass()); |
| 2304 } | 2312 } |
| 2305 | 2313 |
| 2306 private: | 2314 private: |
| 2307 virtual ~LocalWriteClosure() {} | 2315 virtual ~LocalWriteClosure() { |
| 2308 friend class base::RefCounted<LocalWriteClosure>; | 2316 // Make sure the ChainedBlobWriter is derefed (and deleted) on the IDB |
| 2317 // thread since it owns a transaction which has thread affinity. | |
| 2318 task_runner_->PostTask( | |
| 2319 FROM_HERE, | |
| 2320 base::Bind(&LocalWriteClosure::ReleaseWriterOnIDBThread, | |
| 2321 chained_blob_writer_)); | |
| 2322 } | |
| 2323 friend class base::RefCountedThreadSafe<LocalWriteClosure>; | |
| 2309 | 2324 |
| 2310 void callBlobCallbackOnIDBTaskRunner(bool succeeded) { | 2325 void callBlobCallbackOnIDBTaskRunner(bool succeeded) { |
| 2311 DCHECK(task_runner_->RunsTasksOnCurrentThread()); | 2326 DCHECK(task_runner_->RunsTasksOnCurrentThread()); |
| 2312 chained_blob_writer_->ReportWriteCompletion(succeeded, bytes_written_); | 2327 chained_blob_writer_->ReportWriteCompletion(succeeded, bytes_written_); |
| 2313 } | 2328 } |
| 2314 | 2329 |
| 2315 IndexedDBBackingStore::Transaction::ChainedBlobWriter* chained_blob_writer_; | 2330 scoped_refptr<IndexedDBBackingStore::Transaction::ChainedBlobWriter> |
| 2331 chained_blob_writer_; | |
| 2316 base::TaskRunner* task_runner_; | 2332 base::TaskRunner* task_runner_; |
| 2317 int64 bytes_written_; | 2333 int64 bytes_written_; |
| 2318 | 2334 |
| 2319 DISALLOW_COPY_AND_ASSIGN(LocalWriteClosure); | 2335 DISALLOW_COPY_AND_ASSIGN(LocalWriteClosure); |
| 2320 }; | 2336 }; |
| 2321 | 2337 |
| 2322 bool IndexedDBBackingStore::WriteBlobFile( | 2338 bool IndexedDBBackingStore::WriteBlobFile( |
| 2323 int64 database_id, | 2339 int64 database_id, |
| 2324 const Transaction::WriteDescriptor& descriptor, | 2340 const Transaction::WriteDescriptor& descriptor, |
| 2325 Transaction::ChainedBlobWriter* chained_blob_writer) { | 2341 Transaction::ChainedBlobWriter* chained_blob_writer) { |
| (...skipping 1933 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 4259 int64_t size, | 4275 int64_t size, |
| 4260 base::Time last_modified) | 4276 base::Time last_modified) |
| 4261 : is_file_(true), | 4277 : is_file_(true), |
| 4262 file_path_(file_path), | 4278 file_path_(file_path), |
| 4263 key_(key), | 4279 key_(key), |
| 4264 size_(size), | 4280 size_(size), |
| 4265 last_modified_(last_modified) { | 4281 last_modified_(last_modified) { |
| 4266 } | 4282 } |
| 4267 | 4283 |
| 4268 } // namespace content | 4284 } // namespace content |
| OLD | NEW |