android_webview : Class member variable WeakPtrFactory in proper order.
[chromium-blink-merge.git] / content / browser / fileapi / blob_storage_host.cc
blob87e675ce5d1a30bed86ba6c36328382d5fca2fb4
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_data_handle.h"
10 #include "storage/browser/blob/blob_storage_context.h"
11 #include "url/gurl.h"
13 using storage::BlobStorageContext;
14 using storage::BlobData;
16 namespace content {
18 BlobStorageHost::BlobStorageHost(BlobStorageContext* context)
19 : context_(context->AsWeakPtr()) {
22 BlobStorageHost::~BlobStorageHost() {
23 if (!context_.get())
24 return;
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))
38 return false;
39 context_->StartBuildingBlob(uuid);
40 blobs_inuse_map_[uuid] = 1;
41 return true;
44 bool BlobStorageHost::AppendBlobDataItem(
45 const std::string& uuid, const BlobData::Item& data_item) {
46 if (!context_.get() || !IsBeingBuiltInHost(uuid))
47 return false;
48 context_->AppendBlobDataItem(uuid, data_item);
49 return true;
52 bool BlobStorageHost::CancelBuildingBlob(const std::string& uuid) {
53 if (!context_.get() || !IsBeingBuiltInHost(uuid))
54 return false;
55 blobs_inuse_map_.erase(uuid);
56 context_->CancelBuildingBlob(uuid);
57 return true;
60 bool BlobStorageHost::FinishBuildingBlob(
61 const std::string& uuid, const std::string& content_type) {
62 if (!context_.get() || !IsBeingBuiltInHost(uuid))
63 return false;
64 context_->FinishBuildingBlob(uuid, content_type);
65 return true;
68 bool BlobStorageHost::IncrementBlobRefCount(const std::string& uuid) {
69 if (!context_.get() || !context_->IsInUse(uuid) ||
70 context_->IsBeingBuilt(uuid))
71 return false;
72 context_->IncrementBlobRefCount(uuid);
73 blobs_inuse_map_[uuid] += 1;
74 return true;
77 bool BlobStorageHost::DecrementBlobRefCount(const std::string& uuid) {
78 if (!context_.get() || !IsInUseInHost(uuid))
79 return false;
80 context_->DecrementBlobRefCount(uuid);
81 blobs_inuse_map_[uuid] -= 1;
82 if (blobs_inuse_map_[uuid] == 0)
83 blobs_inuse_map_.erase(uuid);
84 return true;
87 bool BlobStorageHost::RegisterPublicBlobURL(
88 const GURL& blob_url, const std::string& uuid) {
89 if (!context_.get() || !IsInUseInHost(uuid) ||
90 context_->IsUrlRegistered(blob_url))
91 return false;
92 context_->RegisterPublicBlobURL(blob_url, uuid);
93 public_blob_urls_.insert(blob_url);
94 return true;
97 bool BlobStorageHost::RevokePublicBlobURL(const GURL& blob_url) {
98 if (!context_.get() || !IsUrlRegisteredInHost(blob_url))
99 return false;
100 context_->RevokePublicBlobURL(blob_url);
101 public_blob_urls_.erase(blob_url);
102 return true;
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