OLD | NEW |
| (Empty) |
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 | |
3 // found in the LICENSE file. | |
4 | |
5 #include "webkit/blob/blob_storage_host.h" | |
6 | |
7 #include "base/sequenced_task_runner.h" | |
8 #include "googleurl/src/gurl.h" | |
9 #include "webkit/blob/blob_data_handle.h" | |
10 #include "webkit/blob/blob_storage_context.h" | |
11 | |
12 namespace webkit_blob { | |
13 | |
14 BlobStorageHost::BlobStorageHost(BlobStorageContext* context) | |
15 : context_(context->AsWeakPtr()) { | |
16 } | |
17 | |
18 BlobStorageHost::~BlobStorageHost() { | |
19 if (!context_) | |
20 return; | |
21 for (std::set<GURL>::iterator iter = public_blob_urls_.begin(); | |
22 iter != public_blob_urls_.end(); ++iter) { | |
23 context_->RevokePublicBlobURL(*iter); | |
24 } | |
25 for (BlobReferenceMap::iterator iter = blobs_inuse_map_.begin(); | |
26 iter != blobs_inuse_map_.end(); ++iter) { | |
27 for (int i = 0; i < iter->second; ++i) | |
28 context_->DecrementBlobRefCount(iter->first); | |
29 } | |
30 } | |
31 | |
32 bool BlobStorageHost::StartBuildingBlob(const std::string& uuid) { | |
33 if (!context_ || uuid.empty() || context_->IsInUse(uuid)) | |
34 return false; | |
35 context_->StartBuildingBlob(uuid); | |
36 blobs_inuse_map_[uuid] = 1; | |
37 return true; | |
38 } | |
39 | |
40 bool BlobStorageHost::AppendBlobDataItem( | |
41 const std::string& uuid, const BlobData::Item& data_item) { | |
42 if (!context_ || !IsBeingBuiltInHost(uuid)) | |
43 return false; | |
44 context_->AppendBlobDataItem(uuid, data_item); | |
45 return true; | |
46 } | |
47 | |
48 bool BlobStorageHost::CancelBuildingBlob(const std::string& uuid) { | |
49 if (!context_ || !IsBeingBuiltInHost(uuid)) | |
50 return false; | |
51 blobs_inuse_map_.erase(uuid); | |
52 context_->CancelBuildingBlob(uuid); | |
53 return true; | |
54 } | |
55 | |
56 bool BlobStorageHost::FinishBuildingBlob( | |
57 const std::string& uuid, const std::string& content_type) { | |
58 if (!context_ || !IsBeingBuiltInHost(uuid)) | |
59 return false; | |
60 context_->FinishBuildingBlob(uuid, content_type); | |
61 return true; | |
62 } | |
63 | |
64 bool BlobStorageHost::IncrementBlobRefCount(const std::string& uuid) { | |
65 if (!context_ || !context_->IsInUse(uuid) || context_->IsBeingBuilt(uuid)) | |
66 return false; | |
67 context_->IncrementBlobRefCount(uuid); | |
68 blobs_inuse_map_[uuid] += 1; | |
69 return true; | |
70 } | |
71 | |
72 bool BlobStorageHost::DecrementBlobRefCount(const std::string& uuid) { | |
73 if (!context_ || !IsInUseInHost(uuid)) | |
74 return false; | |
75 context_->DecrementBlobRefCount(uuid); | |
76 blobs_inuse_map_[uuid] -= 1; | |
77 if (blobs_inuse_map_[uuid] == 0) | |
78 blobs_inuse_map_.erase(uuid); | |
79 return true; | |
80 } | |
81 | |
82 bool BlobStorageHost::RegisterPublicBlobURL( | |
83 const GURL& blob_url, const std::string& uuid) { | |
84 if (!context_ || !IsInUseInHost(uuid) || context_->IsUrlRegistered(blob_url)) | |
85 return false; | |
86 context_->RegisterPublicBlobURL(blob_url, uuid); | |
87 public_blob_urls_.insert(blob_url); | |
88 return true; | |
89 } | |
90 | |
91 bool BlobStorageHost::RevokePublicBlobURL(const GURL& blob_url) { | |
92 if (!context_ || !IsUrlRegisteredInHost(blob_url)) | |
93 return false; | |
94 context_->RevokePublicBlobURL(blob_url); | |
95 public_blob_urls_.erase(blob_url); | |
96 return true; | |
97 } | |
98 | |
99 bool BlobStorageHost::IsInUseInHost(const std::string& uuid) { | |
100 return blobs_inuse_map_.find(uuid) != blobs_inuse_map_.end(); | |
101 } | |
102 | |
103 bool BlobStorageHost::IsBeingBuiltInHost(const std::string& uuid) { | |
104 return IsInUseInHost(uuid) && context_->IsBeingBuilt(uuid); | |
105 } | |
106 | |
107 bool BlobStorageHost::IsUrlRegisteredInHost(const GURL& blob_url) { | |
108 return public_blob_urls_.find(blob_url) != public_blob_urls_.end(); | |
109 } | |
110 | |
111 } // namespace webkit_blob | |
OLD | NEW |