| 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" |
| 11 #include "storage/browser/blob/blob_data_snapshot.h" | 13 #include "storage/browser/blob/blob_data_snapshot.h" |
| 14 #include "storage/browser/blob/blob_reader.h" |
| 12 #include "storage/browser/blob/blob_storage_context.h" | 15 #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" |
| 13 | 20 |
| 14 namespace storage { | 21 namespace storage { |
| 15 | 22 |
| 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 |
| 16 BlobDataHandle::BlobDataHandleShared::BlobDataHandleShared( | 62 BlobDataHandle::BlobDataHandleShared::BlobDataHandleShared( |
| 17 const std::string& uuid, | 63 const std::string& uuid, |
| 18 BlobStorageContext* context, | 64 const std::string& content_type, |
| 19 base::SequencedTaskRunner* task_runner) | 65 const std::string& content_disposition, |
| 20 : uuid_(uuid), context_(context->AsWeakPtr()) { | 66 BlobStorageContext* context) |
| 67 : uuid_(uuid), |
| 68 content_type_(content_type), |
| 69 content_disposition_(content_disposition), |
| 70 context_(context->AsWeakPtr()) { |
| 21 context_->IncrementBlobRefCount(uuid); | 71 context_->IncrementBlobRefCount(uuid); |
| 22 } | 72 } |
| 23 | 73 |
| 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 |
| 24 scoped_ptr<BlobDataSnapshot> | 83 scoped_ptr<BlobDataSnapshot> |
| 25 BlobDataHandle::BlobDataHandleShared::CreateSnapshot() const { | 84 BlobDataHandle::BlobDataHandleShared::CreateSnapshot() const { |
| 26 return context_->CreateSnapshot(uuid_).Pass(); | 85 return context_->CreateSnapshot(uuid_).Pass(); |
| 27 } | 86 } |
| 28 | 87 |
| 29 const std::string& BlobDataHandle::BlobDataHandleShared::uuid() const { | |
| 30 return uuid_; | |
| 31 } | |
| 32 | |
| 33 BlobDataHandle::BlobDataHandleShared::~BlobDataHandleShared() { | 88 BlobDataHandle::BlobDataHandleShared::~BlobDataHandleShared() { |
| 34 if (context_.get()) | 89 if (context_.get()) |
| 35 context_->DecrementBlobRefCount(uuid_); | 90 context_->DecrementBlobRefCount(uuid_); |
| 36 } | 91 } |
| 37 | 92 |
| 38 BlobDataHandle::BlobDataHandle(const std::string& uuid, | 93 BlobDataHandle::BlobDataHandle(const std::string& uuid, |
| 94 const std::string& content_type, |
| 95 const std::string& content_disposition, |
| 39 BlobStorageContext* context, | 96 BlobStorageContext* context, |
| 40 base::SequencedTaskRunner* task_runner) | 97 base::SequencedTaskRunner* io_task_runner) |
| 41 : io_task_runner_(task_runner), | 98 : io_task_runner_(io_task_runner), |
| 42 shared_(new BlobDataHandleShared(uuid, context, task_runner)) { | 99 shared_(new BlobDataHandleShared(uuid, |
| 100 content_type, |
| 101 content_disposition, |
| 102 context)) { |
| 43 DCHECK(io_task_runner_.get()); | 103 DCHECK(io_task_runner_.get()); |
| 44 DCHECK(io_task_runner_->RunsTasksOnCurrentThread()); | 104 DCHECK(io_task_runner_->RunsTasksOnCurrentThread()); |
| 45 } | 105 } |
| 46 | 106 |
| 47 BlobDataHandle::BlobDataHandle(const BlobDataHandle& other) { | 107 BlobDataHandle::BlobDataHandle(const BlobDataHandle& other) { |
| 48 io_task_runner_ = other.io_task_runner_; | 108 io_task_runner_ = other.io_task_runner_; |
| 49 shared_ = other.shared_; | 109 shared_ = other.shared_; |
| 50 } | 110 } |
| 51 | 111 |
| 52 BlobDataHandle::~BlobDataHandle() { | 112 BlobDataHandle::~BlobDataHandle() { |
| 53 BlobDataHandleShared* raw = shared_.get(); | 113 BlobDataHandleShared* raw = shared_.get(); |
| 54 raw->AddRef(); | 114 raw->AddRef(); |
| 55 shared_ = nullptr; | 115 shared_ = nullptr; |
| 56 io_task_runner_->ReleaseSoon(FROM_HERE, raw); | 116 io_task_runner_->ReleaseSoon(FROM_HERE, raw); |
| 57 } | 117 } |
| 58 | 118 |
| 59 scoped_ptr<BlobDataSnapshot> BlobDataHandle::CreateSnapshot() const { | 119 scoped_ptr<BlobDataSnapshot> BlobDataHandle::CreateSnapshot() const { |
| 60 DCHECK(io_task_runner_->RunsTasksOnCurrentThread()); | 120 DCHECK(io_task_runner_->RunsTasksOnCurrentThread()); |
| 61 return shared_->CreateSnapshot().Pass(); | 121 return shared_->CreateSnapshot().Pass(); |
| 62 } | 122 } |
| 63 | 123 |
| 64 const std::string& BlobDataHandle::uuid() const { | 124 const std::string& BlobDataHandle::uuid() const { |
| 65 return shared_->uuid(); | 125 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_; |
| 66 } | 134 } |
| 67 | 135 |
| 68 } // namespace storage | 136 } // namespace storage |
| OLD | NEW |