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"
9 #include "storage/browser/blob/blob_storage_context.h"
12 using storage::BlobStorageContext
;
16 BlobStorageHost::BlobStorageHost(BlobStorageContext
* context
)
17 : context_(context
->AsWeakPtr()) {
20 BlobStorageHost::~BlobStorageHost() {
23 for (std::set
<GURL
>::iterator iter
= public_blob_urls_
.begin();
24 iter
!= public_blob_urls_
.end(); ++iter
) {
25 context_
->RevokePublicBlobURL(*iter
);
27 for (BlobReferenceMap::iterator iter
= blobs_inuse_map_
.begin();
28 iter
!= blobs_inuse_map_
.end(); ++iter
) {
29 for (int i
= 0; i
< iter
->second
; ++i
)
30 context_
->DecrementBlobRefCount(iter
->first
);
34 bool BlobStorageHost::StartBuildingBlob(const std::string
& uuid
) {
35 if (!context_
.get() || uuid
.empty() || context_
->IsInUse(uuid
))
37 context_
->StartBuildingBlob(uuid
);
38 blobs_inuse_map_
[uuid
] = 1;
42 bool BlobStorageHost::AppendBlobDataItem(
43 const std::string
& uuid
,
44 const storage::DataElement
& data_item
) {
45 if (!context_
.get() || !IsBeingBuiltInHost(uuid
))
47 context_
->AppendBlobDataItem(uuid
, data_item
);
51 bool BlobStorageHost::CancelBuildingBlob(const std::string
& uuid
) {
52 if (!context_
.get() || !IsBeingBuiltInHost(uuid
))
54 blobs_inuse_map_
.erase(uuid
);
55 context_
->CancelBuildingBlob(uuid
);
59 bool BlobStorageHost::FinishBuildingBlob(
60 const std::string
& uuid
, const std::string
& content_type
) {
61 if (!context_
.get() || !IsBeingBuiltInHost(uuid
))
63 context_
->FinishBuildingBlob(uuid
, content_type
);
67 bool BlobStorageHost::IncrementBlobRefCount(const std::string
& uuid
) {
68 if (!context_
.get() || !context_
->IsInUse(uuid
) ||
69 context_
->IsBeingBuilt(uuid
))
71 context_
->IncrementBlobRefCount(uuid
);
72 blobs_inuse_map_
[uuid
] += 1;
76 bool BlobStorageHost::DecrementBlobRefCount(const std::string
& uuid
) {
77 if (!context_
.get() || !IsInUseInHost(uuid
))
79 context_
->DecrementBlobRefCount(uuid
);
80 blobs_inuse_map_
[uuid
] -= 1;
81 if (blobs_inuse_map_
[uuid
] == 0)
82 blobs_inuse_map_
.erase(uuid
);
86 bool BlobStorageHost::RegisterPublicBlobURL(
87 const GURL
& blob_url
, const std::string
& uuid
) {
88 if (!context_
.get() || !IsInUseInHost(uuid
) ||
89 context_
->IsUrlRegistered(blob_url
))
91 context_
->RegisterPublicBlobURL(blob_url
, uuid
);
92 public_blob_urls_
.insert(blob_url
);
96 bool BlobStorageHost::RevokePublicBlobURL(const GURL
& blob_url
) {
97 if (!context_
.get() || !IsUrlRegisteredInHost(blob_url
))
99 context_
->RevokePublicBlobURL(blob_url
);
100 public_blob_urls_
.erase(blob_url
);
104 bool BlobStorageHost::IsInUseInHost(const std::string
& uuid
) {
105 return blobs_inuse_map_
.find(uuid
) != blobs_inuse_map_
.end();
108 bool BlobStorageHost::IsBeingBuiltInHost(const std::string
& uuid
) {
109 return IsInUseInHost(uuid
) && context_
->IsBeingBuilt(uuid
);
112 bool BlobStorageHost::IsUrlRegisteredInHost(const GURL
& blob_url
) {
113 return public_blob_urls_
.find(blob_url
) != public_blob_urls_
.end();
116 } // namespace content