1 // Copyright 2014 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.
5 #include "content/browser/fileapi/blob_storage_host.h"
7 #include "base/sequenced_task_runner.h"
8 #include "base/strings/string_util.h"
10 #include "webkit/browser/blob/blob_data_handle.h"
11 #include "webkit/browser/blob/blob_storage_context.h"
13 using storage::BlobStorageContext
;
14 using storage::BlobData
;
18 BlobStorageHost::BlobStorageHost(BlobStorageContext
* context
)
19 : context_(context
->AsWeakPtr()) {
22 BlobStorageHost::~BlobStorageHost() {
25 for (std::set
<GURL
>::iterator iter
= public_blob_urls_
.begin();
26 iter
!= public_blob_urls_
.end(); ++iter
) {
27 context_
->RevokePublicBlobURL(*iter
);
29 for (BlobReferenceMap::iterator iter
= blobs_inuse_map_
.begin();
30 iter
!= blobs_inuse_map_
.end(); ++iter
) {
31 for (int i
= 0; i
< iter
->second
; ++i
)
32 context_
->DecrementBlobRefCount(iter
->first
);
36 bool BlobStorageHost::StartBuildingBlob(const std::string
& uuid
) {
37 if (!context_
.get() || uuid
.empty() || context_
->IsInUse(uuid
))
39 context_
->StartBuildingBlob(uuid
);
40 blobs_inuse_map_
[uuid
] = 1;
44 bool BlobStorageHost::AppendBlobDataItem(
45 const std::string
& uuid
, const BlobData::Item
& data_item
) {
46 if (!context_
.get() || !IsBeingBuiltInHost(uuid
))
48 context_
->AppendBlobDataItem(uuid
, data_item
);
52 bool BlobStorageHost::CancelBuildingBlob(const std::string
& uuid
) {
53 if (!context_
.get() || !IsBeingBuiltInHost(uuid
))
55 blobs_inuse_map_
.erase(uuid
);
56 context_
->CancelBuildingBlob(uuid
);
60 bool BlobStorageHost::FinishBuildingBlob(
61 const std::string
& uuid
, const std::string
& content_type
) {
62 if (!context_
.get() || !IsBeingBuiltInHost(uuid
))
64 context_
->FinishBuildingBlob(uuid
, content_type
);
68 bool BlobStorageHost::IncrementBlobRefCount(const std::string
& uuid
) {
69 if (!context_
.get() || !context_
->IsInUse(uuid
) ||
70 context_
->IsBeingBuilt(uuid
))
72 context_
->IncrementBlobRefCount(uuid
);
73 blobs_inuse_map_
[uuid
] += 1;
77 bool BlobStorageHost::DecrementBlobRefCount(const std::string
& uuid
) {
78 if (!context_
.get() || !IsInUseInHost(uuid
))
80 context_
->DecrementBlobRefCount(uuid
);
81 blobs_inuse_map_
[uuid
] -= 1;
82 if (blobs_inuse_map_
[uuid
] == 0)
83 blobs_inuse_map_
.erase(uuid
);
87 bool BlobStorageHost::RegisterPublicBlobURL(
88 const GURL
& blob_url
, const std::string
& uuid
) {
89 if (!context_
.get() || !IsInUseInHost(uuid
) ||
90 context_
->IsUrlRegistered(blob_url
))
92 context_
->RegisterPublicBlobURL(blob_url
, uuid
);
93 public_blob_urls_
.insert(blob_url
);
97 bool BlobStorageHost::RevokePublicBlobURL(const GURL
& blob_url
) {
98 if (!context_
.get() || !IsUrlRegisteredInHost(blob_url
))
100 context_
->RevokePublicBlobURL(blob_url
);
101 public_blob_urls_
.erase(blob_url
);
105 bool BlobStorageHost::IsInUseInHost(const std::string
& uuid
) {
106 return blobs_inuse_map_
.find(uuid
) != blobs_inuse_map_
.end();
109 bool BlobStorageHost::IsBeingBuiltInHost(const std::string
& uuid
) {
110 return IsInUseInHost(uuid
) && context_
->IsBeingBuilt(uuid
);
113 bool BlobStorageHost::IsUrlRegisteredInHost(const GURL
& blob_url
) {
114 return public_blob_urls_
.find(blob_url
) != public_blob_urls_
.end();
117 } // namespace content