| 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 "storage/browser/blob/blob_data_handle.h" | 5 #include "storage/browser/blob/blob_data_handle.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/location.h" | 8 #include "base/location.h" |
| 9 #include "base/logging.h" | 9 #include "base/logging.h" |
| 10 #include "base/sequenced_task_runner.h" | 10 #include "base/sequenced_task_runner.h" |
| 11 #include "base/task_runner.h" | |
| 12 #include "base/time/time.h" | |
| 13 #include "storage/browser/blob/blob_data_snapshot.h" | 11 #include "storage/browser/blob/blob_data_snapshot.h" |
| 14 #include "storage/browser/blob/blob_reader.h" | |
| 15 #include "storage/browser/blob/blob_storage_context.h" | 12 #include "storage/browser/blob/blob_storage_context.h" |
| 16 #include "storage/browser/fileapi/file_stream_reader.h" | |
| 17 #include "storage/browser/fileapi/file_system_context.h" | |
| 18 #include "storage/browser/fileapi/file_system_url.h" | |
| 19 #include "url/gurl.h" | |
| 20 | 13 |
| 21 namespace storage { | 14 namespace storage { |
| 22 | 15 |
| 23 namespace { | |
| 24 | |
| 25 class FileStreamReaderProviderImpl | |
| 26 : public BlobReader::FileStreamReaderProvider { | |
| 27 public: | |
| 28 FileStreamReaderProviderImpl(FileSystemContext* file_system_context) | |
| 29 : file_system_context_(file_system_context) {} | |
| 30 ~FileStreamReaderProviderImpl() override {} | |
| 31 | |
| 32 scoped_ptr<FileStreamReader> CreateForLocalFile( | |
| 33 base::TaskRunner* task_runner, | |
| 34 const base::FilePath& file_path, | |
| 35 int64_t initial_offset, | |
| 36 const base::Time& expected_modification_time) override { | |
| 37 return make_scoped_ptr(FileStreamReader::CreateForLocalFile( | |
| 38 task_runner, file_path, initial_offset, expected_modification_time)); | |
| 39 } | |
| 40 | |
| 41 scoped_ptr<FileStreamReader> CreateFileStreamReader( | |
| 42 const GURL& filesystem_url, | |
| 43 int64_t offset, | |
| 44 int64_t max_bytes_to_read, | |
| 45 const base::Time& expected_modification_time) override { | |
| 46 return file_system_context_->CreateFileStreamReader( | |
| 47 storage::FileSystemURL( | |
| 48 file_system_context_->CrackURL( | |
| 49 filesystem_url)), | |
| 50 offset, max_bytes_to_read, | |
| 51 expected_modification_time) | |
| 52 .Pass(); | |
| 53 } | |
| 54 | |
| 55 private: | |
| 56 scoped_refptr<FileSystemContext> file_system_context_; | |
| 57 DISALLOW_COPY_AND_ASSIGN(FileStreamReaderProviderImpl); | |
| 58 }; | |
| 59 | |
| 60 } // namespace | |
| 61 | |
| 62 BlobDataHandle::BlobDataHandleShared::BlobDataHandleShared( | 16 BlobDataHandle::BlobDataHandleShared::BlobDataHandleShared( |
| 63 const std::string& uuid, | 17 const std::string& uuid, |
| 64 const std::string& content_type, | 18 BlobStorageContext* context, |
| 65 const std::string& content_disposition, | 19 base::SequencedTaskRunner* task_runner) |
| 66 BlobStorageContext* context) | 20 : uuid_(uuid), context_(context->AsWeakPtr()) { |
| 67 : uuid_(uuid), | |
| 68 content_type_(content_type), | |
| 69 content_disposition_(content_disposition), | |
| 70 context_(context->AsWeakPtr()) { | |
| 71 context_->IncrementBlobRefCount(uuid); | 21 context_->IncrementBlobRefCount(uuid); |
| 72 } | 22 } |
| 73 | 23 |
| 74 scoped_ptr<BlobReader> BlobDataHandle::CreateReader( | |
| 75 FileSystemContext* file_system_context, | |
| 76 base::SequencedTaskRunner* file_task_runner) const { | |
| 77 return scoped_ptr<BlobReader>(new BlobReader( | |
| 78 this, scoped_ptr<BlobReader::FileStreamReaderProvider>( | |
| 79 new FileStreamReaderProviderImpl(file_system_context)), | |
| 80 file_task_runner)); | |
| 81 } | |
| 82 | |
| 83 scoped_ptr<BlobDataSnapshot> | 24 scoped_ptr<BlobDataSnapshot> |
| 84 BlobDataHandle::BlobDataHandleShared::CreateSnapshot() const { | 25 BlobDataHandle::BlobDataHandleShared::CreateSnapshot() const { |
| 85 return context_->CreateSnapshot(uuid_).Pass(); | 26 return context_->CreateSnapshot(uuid_).Pass(); |
| 86 } | 27 } |
| 87 | 28 |
| 29 const std::string& BlobDataHandle::BlobDataHandleShared::uuid() const { |
| 30 return uuid_; |
| 31 } |
| 32 |
| 88 BlobDataHandle::BlobDataHandleShared::~BlobDataHandleShared() { | 33 BlobDataHandle::BlobDataHandleShared::~BlobDataHandleShared() { |
| 89 if (context_.get()) | 34 if (context_.get()) |
| 90 context_->DecrementBlobRefCount(uuid_); | 35 context_->DecrementBlobRefCount(uuid_); |
| 91 } | 36 } |
| 92 | 37 |
| 93 BlobDataHandle::BlobDataHandle(const std::string& uuid, | 38 BlobDataHandle::BlobDataHandle(const std::string& uuid, |
| 94 const std::string& content_type, | |
| 95 const std::string& content_disposition, | |
| 96 BlobStorageContext* context, | 39 BlobStorageContext* context, |
| 97 base::SequencedTaskRunner* io_task_runner) | 40 base::SequencedTaskRunner* task_runner) |
| 98 : io_task_runner_(io_task_runner), | 41 : io_task_runner_(task_runner), |
| 99 shared_(new BlobDataHandleShared(uuid, | 42 shared_(new BlobDataHandleShared(uuid, context, task_runner)) { |
| 100 content_type, | |
| 101 content_disposition, | |
| 102 context)) { | |
| 103 DCHECK(io_task_runner_.get()); | 43 DCHECK(io_task_runner_.get()); |
| 104 DCHECK(io_task_runner_->RunsTasksOnCurrentThread()); | 44 DCHECK(io_task_runner_->RunsTasksOnCurrentThread()); |
| 105 } | 45 } |
| 106 | 46 |
| 107 BlobDataHandle::BlobDataHandle(const BlobDataHandle& other) { | 47 BlobDataHandle::BlobDataHandle(const BlobDataHandle& other) { |
| 108 io_task_runner_ = other.io_task_runner_; | 48 io_task_runner_ = other.io_task_runner_; |
| 109 shared_ = other.shared_; | 49 shared_ = other.shared_; |
| 110 } | 50 } |
| 111 | 51 |
| 112 BlobDataHandle::~BlobDataHandle() { | 52 BlobDataHandle::~BlobDataHandle() { |
| 113 BlobDataHandleShared* raw = shared_.get(); | 53 BlobDataHandleShared* raw = shared_.get(); |
| 114 raw->AddRef(); | 54 raw->AddRef(); |
| 115 shared_ = nullptr; | 55 shared_ = nullptr; |
| 116 io_task_runner_->ReleaseSoon(FROM_HERE, raw); | 56 io_task_runner_->ReleaseSoon(FROM_HERE, raw); |
| 117 } | 57 } |
| 118 | 58 |
| 119 scoped_ptr<BlobDataSnapshot> BlobDataHandle::CreateSnapshot() const { | 59 scoped_ptr<BlobDataSnapshot> BlobDataHandle::CreateSnapshot() const { |
| 120 DCHECK(io_task_runner_->RunsTasksOnCurrentThread()); | 60 DCHECK(io_task_runner_->RunsTasksOnCurrentThread()); |
| 121 return shared_->CreateSnapshot().Pass(); | 61 return shared_->CreateSnapshot().Pass(); |
| 122 } | 62 } |
| 123 | 63 |
| 124 const std::string& BlobDataHandle::uuid() const { | 64 const std::string& BlobDataHandle::uuid() const { |
| 125 return shared_->uuid_; | 65 return shared_->uuid(); |
| 126 } | |
| 127 | |
| 128 const std::string& BlobDataHandle::content_type() const { | |
| 129 return shared_->content_type_; | |
| 130 } | |
| 131 | |
| 132 const std::string& BlobDataHandle::content_disposition() const { | |
| 133 return shared_->content_disposition_; | |
| 134 } | 66 } |
| 135 | 67 |
| 136 } // namespace storage | 68 } // namespace storage |
| OLD | NEW |