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