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::BlobDataHandle(BlobData* blob_data, BlobStorageContext* context, | 16 BlobDataHandle::BlobDataHandleShared::BlobDataHandleShared( |
17 base::SequencedTaskRunner* task_runner) | 17 BlobData* blob_data, |
18 BlobStorageContext* context, | |
19 base::SequencedTaskRunner* task_runner) | |
18 : blob_data_(blob_data), | 20 : blob_data_(blob_data), |
19 context_(context->AsWeakPtr()), | 21 context_(context->AsWeakPtr()), |
20 io_task_runner_(task_runner) { | 22 io_task_runner_(task_runner) { |
21 // Ensures the uuid remains registered and the underlying data is not deleted. | 23 DCHECK(io_task_runner_); |
22 DCHECK(io_task_runner_->RunsTasksOnCurrentThread()); | 24 DCHECK(io_task_runner_->RunsTasksOnCurrentThread()); |
23 context_->IncrementBlobRefCount(blob_data->uuid()); | 25 context_->IncrementBlobRefCount(blob_data->uuid()); |
24 blob_data_->AddRef(); | |
25 } | 26 } |
26 | 27 |
27 BlobDataHandle::~BlobDataHandle() { | 28 BlobData* BlobDataHandle::BlobDataHandleShared::data() const { |
28 if (io_task_runner_->RunsTasksOnCurrentThread()) { | |
29 // Note: Do not test context_ or alter the blob_data_ refcount | |
30 // on the wrong thread. | |
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_))); | |
40 } | |
41 | |
42 BlobData* BlobDataHandle::data() const { | |
43 DCHECK(io_task_runner_->RunsTasksOnCurrentThread()); | 29 DCHECK(io_task_runner_->RunsTasksOnCurrentThread()); |
44 return blob_data_; | 30 return blob_data_; |
45 } | 31 } |
46 | 32 |
47 // static | 33 const std::string& BlobDataHandle::BlobDataHandleShared::uuid() const { |
48 void BlobDataHandle::DeleteHelper( | 34 return blob_data_->uuid(); |
michaeln
2014/04/28 18:25:22
nice that this is no longer needed since the Share
| |
49 base::WeakPtr<BlobStorageContext> context, | 35 } |
50 BlobData* blob_data) { | 36 |
51 if (context.get()) | 37 BlobDataHandle::BlobDataHandleShared::~BlobDataHandleShared() { |
52 context->DecrementBlobRefCount(blob_data->uuid()); | 38 if (context_.get()) |
53 blob_data->Release(); | 39 context_->DecrementBlobRefCount(blob_data_->uuid()); |
40 } | |
41 | |
42 BlobDataHandle::BlobDataHandle(BlobData* blob_data, | |
43 BlobStorageContext* context, | |
44 base::SequencedTaskRunner* task_runner) | |
45 : io_task_runner_(task_runner), | |
46 shared_(new BlobDataHandleShared(blob_data, context, task_runner)) { | |
47 DCHECK(io_task_runner_); | |
48 } | |
49 | |
50 BlobDataHandle::BlobDataHandle(const BlobDataHandle& other) { | |
51 io_task_runner_ = other.io_task_runner_; | |
52 shared_ = other.shared_; | |
53 } | |
54 | |
55 BlobDataHandle::~BlobDataHandle() { | |
56 BlobDataHandleShared* raw = shared_.get(); | |
57 raw->AddRef(); | |
58 shared_ = 0; | |
59 io_task_runner_->ReleaseSoon(FROM_HERE, raw); | |
60 } | |
61 | |
62 BlobData* BlobDataHandle::data() const { | |
michaeln
2014/04/28 18:25:22
if the DCHECK(io_task_runner_->RunsTasksOnCurrentT
ericu
2014/04/28 18:43:01
Good point; done.
| |
63 return shared_->data(); | |
64 } | |
65 | |
66 std::string BlobDataHandle::uuid() const { | |
67 return shared_->uuid(); | |
54 } | 68 } |
55 | 69 |
56 } // namespace webkit_blob | 70 } // namespace webkit_blob |
OLD | NEW |