Refactor FacetManager into a separate file.
[chromium-blink-merge.git] / content / browser / fileapi / blob_storage_host.cc
blobfaf81010dd3f201dd48476a2a988ebeed51ca9b4
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"
10 #include "url/gurl.h"
12 using storage::BlobStorageContext;
14 namespace content {
16 BlobStorageHost::BlobStorageHost(BlobStorageContext* context)
17 : context_(context->AsWeakPtr()) {
20 BlobStorageHost::~BlobStorageHost() {
21 if (!context_.get())
22 return;
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))
36 return false;
37 context_->StartBuildingBlob(uuid);
38 blobs_inuse_map_[uuid] = 1;
39 return true;
42 bool BlobStorageHost::AppendBlobDataItem(
43 const std::string& uuid,
44 const storage::DataElement& data_item) {
45 if (!context_.get() || !IsBeingBuiltInHost(uuid))
46 return false;
47 context_->AppendBlobDataItem(uuid, data_item);
48 return true;
51 bool BlobStorageHost::CancelBuildingBlob(const std::string& uuid) {
52 if (!context_.get() || !IsBeingBuiltInHost(uuid))
53 return false;
54 blobs_inuse_map_.erase(uuid);
55 context_->CancelBuildingBlob(uuid);
56 return true;
59 bool BlobStorageHost::FinishBuildingBlob(
60 const std::string& uuid, const std::string& content_type) {
61 if (!context_.get() || !IsBeingBuiltInHost(uuid))
62 return false;
63 context_->FinishBuildingBlob(uuid, content_type);
64 return true;
67 bool BlobStorageHost::IncrementBlobRefCount(const std::string& uuid) {
68 if (!context_.get() || !context_->IsInUse(uuid) ||
69 context_->IsBeingBuilt(uuid))
70 return false;
71 context_->IncrementBlobRefCount(uuid);
72 blobs_inuse_map_[uuid] += 1;
73 return true;
76 bool BlobStorageHost::DecrementBlobRefCount(const std::string& uuid) {
77 if (!context_.get() || !IsInUseInHost(uuid))
78 return false;
79 context_->DecrementBlobRefCount(uuid);
80 blobs_inuse_map_[uuid] -= 1;
81 if (blobs_inuse_map_[uuid] == 0)
82 blobs_inuse_map_.erase(uuid);
83 return true;
86 bool BlobStorageHost::RegisterPublicBlobURL(
87 const GURL& blob_url, const std::string& uuid) {
88 if (!context_.get() || !IsInUseInHost(uuid) ||
89 context_->IsUrlRegistered(blob_url))
90 return false;
91 context_->RegisterPublicBlobURL(blob_url, uuid);
92 public_blob_urls_.insert(blob_url);
93 return true;
96 bool BlobStorageHost::RevokePublicBlobURL(const GURL& blob_url) {
97 if (!context_.get() || !IsUrlRegisteredInHost(blob_url))
98 return false;
99 context_->RevokePublicBlobURL(blob_url);
100 public_blob_urls_.erase(blob_url);
101 return true;
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