| 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 "webkit/browser/blob/blob_data_handle.h" | 5 #include "webkit/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 "webkit/browser/blob/blob_storage_context.h" | 11 #include "webkit/browser/blob/blob_storage_context.h" |
| 12 #include "webkit/common/blob/blob_data.h" | 12 #include "webkit/common/blob/blob_data.h" |
| 13 | 13 |
| 14 namespace webkit_blob { | 14 namespace webkit_blob { |
| 15 | 15 |
| 16 BlobDataHandle::BlobDataHandleShared::BlobDataHandleShared( | 16 BlobDataHandle::BlobDataHandle(BlobData* blob_data, BlobStorageContext* context, |
| 17 BlobData* blob_data, | 17 base::SequencedTaskRunner* task_runner) |
| 18 BlobStorageContext* context, | |
| 19 base::SequencedTaskRunner* task_runner) | |
| 20 : blob_data_(blob_data), | 18 : blob_data_(blob_data), |
| 21 context_(context->AsWeakPtr()) { | 19 context_(context->AsWeakPtr()), |
| 20 io_task_runner_(task_runner) { |
| 21 // Ensures the uuid remains registered and the underlying data is not deleted. |
| 22 DCHECK(io_task_runner_->RunsTasksOnCurrentThread()); |
| 22 context_->IncrementBlobRefCount(blob_data->uuid()); | 23 context_->IncrementBlobRefCount(blob_data->uuid()); |
| 23 } | 24 blob_data_->AddRef(); |
| 24 | |
| 25 BlobData* BlobDataHandle::BlobDataHandleShared::data() const { | |
| 26 return blob_data_; | |
| 27 } | |
| 28 | |
| 29 const std::string& BlobDataHandle::BlobDataHandleShared::uuid() const { | |
| 30 return blob_data_->uuid(); | |
| 31 } | |
| 32 | |
| 33 BlobDataHandle::BlobDataHandleShared::~BlobDataHandleShared() { | |
| 34 if (context_.get()) | |
| 35 context_->DecrementBlobRefCount(blob_data_->uuid()); | |
| 36 } | |
| 37 | |
| 38 BlobDataHandle::BlobDataHandle(BlobData* blob_data, | |
| 39 BlobStorageContext* context, | |
| 40 base::SequencedTaskRunner* task_runner) | |
| 41 : io_task_runner_(task_runner), | |
| 42 shared_(new BlobDataHandleShared(blob_data, context, task_runner)) { | |
| 43 DCHECK(io_task_runner_); | |
| 44 DCHECK(io_task_runner_->RunsTasksOnCurrentThread()); | |
| 45 } | |
| 46 | |
| 47 BlobDataHandle::BlobDataHandle(const BlobDataHandle& other) { | |
| 48 io_task_runner_ = other.io_task_runner_; | |
| 49 shared_ = other.shared_; | |
| 50 } | 25 } |
| 51 | 26 |
| 52 BlobDataHandle::~BlobDataHandle() { | 27 BlobDataHandle::~BlobDataHandle() { |
| 53 BlobDataHandleShared* raw = shared_.get(); | 28 if (io_task_runner_->RunsTasksOnCurrentThread()) { |
| 54 raw->AddRef(); | 29 // Note: Do not test context_ or alter the blob_data_ refcount |
| 55 shared_ = 0; | 30 // on the wrong thread. |
| 56 io_task_runner_->ReleaseSoon(FROM_HERE, raw); | 31 if (context_.get()) |
| 32 context_->DecrementBlobRefCount(blob_data_->uuid()); |
| 33 blob_data_->Release(); |
| 34 return; |
| 35 } |
| 36 |
| 37 io_task_runner_->PostTask( |
| 38 FROM_HERE, |
| 39 base::Bind(&DeleteHelper, context_, base::Unretained(blob_data_))); |
| 57 } | 40 } |
| 58 | 41 |
| 59 BlobData* BlobDataHandle::data() const { | 42 BlobData* BlobDataHandle::data() const { |
| 60 DCHECK(io_task_runner_->RunsTasksOnCurrentThread()); | 43 DCHECK(io_task_runner_->RunsTasksOnCurrentThread()); |
| 61 return shared_->data(); | 44 return blob_data_; |
| 62 } | 45 } |
| 63 | 46 |
| 64 std::string BlobDataHandle::uuid() const { | 47 // static |
| 65 return shared_->uuid(); | 48 void BlobDataHandle::DeleteHelper( |
| 49 base::WeakPtr<BlobStorageContext> context, |
| 50 BlobData* blob_data) { |
| 51 if (context.get()) |
| 52 context->DecrementBlobRefCount(blob_data->uuid()); |
| 53 blob_data->Release(); |
| 66 } | 54 } |
| 67 | 55 |
| 68 } // namespace webkit_blob | 56 } // namespace webkit_blob |
| OLD | NEW |