| Index: webkit/blob/blob_storage_context.cc
|
| ===================================================================
|
| --- webkit/blob/blob_storage_context.cc (revision 166276)
|
| +++ webkit/blob/blob_storage_context.cc (working copy)
|
| @@ -2,9 +2,12 @@
|
| // Use of this source code is governed by a BSD-style license that can be
|
| // found in the LICENSE file.
|
|
|
| -#include "webkit/blob/blob_storage_controller.h"
|
| +#include "webkit/blob/blob_storage_context.h"
|
|
|
| +#include "base/bind.h"
|
| +#include "base/location.h"
|
| #include "base/logging.h"
|
| +#include "base/message_loop_proxy.h"
|
| #include "googleurl/src/gurl.h"
|
| #include "webkit/blob/blob_data.h"
|
|
|
| @@ -32,29 +35,147 @@
|
|
|
| } // namespace
|
|
|
| -BlobStorageController::BlobStorageController()
|
| +//-----------------------------------------------------------------------
|
| +// BlobDataHandle
|
| +//-----------------------------------------------------------------------
|
| +
|
| +BlobDataHandle::BlobDataHandle(BlobData* blob_data, BlobStorageContext* context,
|
| + base::SequencedTaskRunner* task_runner)
|
| + : blob_data_(blob_data),
|
| + context_(context),
|
| + io_task_runner_(task_runner) {
|
| + // Ensures the uuid remains registered and the underlying data is not deleted.
|
| + context_->IncrementBlobRefCount(blob_data->uuid());
|
| +}
|
| +
|
| +BlobDataHandle::~BlobDataHandle() {
|
| + if (io_task_runner_->RunsTasksOnCurrentThread()) {
|
| + context_->DecrementBlobRefCount(blob_data_->uuid());
|
| + return;
|
| + }
|
| + io_task_runner_->PostTask(
|
| + FROM_HERE,
|
| + base::Bind(&BlobStorageContext::DecrementBlobRefCount,
|
| + base::Unretained(context_), blob_data_->uuid()));
|
| + io_task_runner_->ReleaseSoon(FROM_HERE, blob_data_.release());
|
| +}
|
| +
|
| +BlobData* BlobDataHandle::data() const {
|
| + DCHECK(io_task_runner_->RunsTasksOnCurrentThread());
|
| + return blob_data_.get();
|
| +}
|
| +
|
| +
|
| +//-----------------------------------------------------------------------
|
| +// BlobStorageConsumer
|
| +//-----------------------------------------------------------------------
|
| +
|
| +// TODO(michaeln): I need to tighten this up, some assertions and better
|
| +// map usage (aka use ->find more). Do something reasonable given bad inputs
|
| +// like addref'ing a id that does not exist.
|
| +
|
| +BlobStorageConsumer::BlobStorageConsumer(BlobStorageContext* context)
|
| + : context_(context) {
|
| +}
|
| +
|
| +BlobStorageConsumer::~BlobStorageConsumer() {
|
| + for (BlobReferenceMap::iterator iter = blobs_inuse_map_.begin();
|
| + iter != blobs_inuse_map_.end(); ++iter) {
|
| + for (int i = 0; i < iter->second; ++i)
|
| + context_->DecrementBlobRefCount(iter->first);
|
| + }
|
| +
|
| + for (std::set<GURL>::iterator iter = public_blob_urls_.begin();
|
| + iter != public_blob_urls_.end(); ++iter) {
|
| + context_->RevokePublicBlobURL(*iter);
|
| + }
|
| +}
|
| +
|
| +void BlobStorageConsumer::StartBuildingBlob(const std::string& uuid) {
|
| + blobs_inuse_map_[uuid] = 1;
|
| + context_->StartBuildingBlob(uuid);
|
| +}
|
| +
|
| +void BlobStorageConsumer::AppendBlobDataItem(
|
| + const std::string& uuid, const BlobData::Item& data_item) {
|
| + context_->AppendBlobDataItem(uuid, data_item);
|
| +}
|
| +
|
| +void BlobStorageConsumer::CancelBuildingBlob(const std::string& uuid) {
|
| + blobs_inuse_map_.erase(uuid);
|
| + context_->CancelBuildingBlob(uuid);
|
| +}
|
| +
|
| +void BlobStorageConsumer::FinishBuildingBlob(
|
| + const std::string& uuid, const std::string& content_type) {
|
| + context_->FinishBuildingBlob(uuid, content_type);
|
| +}
|
| +
|
| +void BlobStorageConsumer::IncrementBlobRefCount(const std::string& uuid) {
|
| + blobs_inuse_map_[uuid] += 1;
|
| + context_->IncrementBlobRefCount(uuid);
|
| +}
|
| +
|
| +void BlobStorageConsumer::DecrementBlobRefCount(const std::string& uuid) {
|
| + blobs_inuse_map_[uuid] -= 1;
|
| + if (blobs_inuse_map_[uuid] == 0)
|
| + blobs_inuse_map_.erase(uuid);
|
| + context_->DecrementBlobRefCount(uuid);
|
| +}
|
| +
|
| +void BlobStorageConsumer::RegisterPublicBlobURL(
|
| + const GURL& blob_url, const std::string& uuid) {
|
| + public_blob_urls_.insert(blob_url);
|
| + context_->RegisterPublicBlobURL(blob_url, uuid);
|
| +}
|
| +
|
| +void BlobStorageConsumer::RevokePublicBlobURL(const GURL& blob_url) {
|
| + public_blob_urls_.erase(blob_url);
|
| + context_->RevokePublicBlobURL(blob_url);
|
| +}
|
| +
|
| +//-----------------------------------------------------------------------
|
| +// BlobStorageContext
|
| +//-----------------------------------------------------------------------
|
| +
|
| +BlobStorageContext::BlobStorageContext()
|
| : memory_usage_(0) {
|
| }
|
|
|
| -BlobStorageController::~BlobStorageController() {
|
| +BlobStorageContext::~BlobStorageContext() {
|
| }
|
|
|
| -void BlobStorageController::StartBuildingBlob(const GURL& url) {
|
| - DCHECK(url.SchemeIs("blob"));
|
| - DCHECK(!BlobUrlHasRef(url));
|
| - BlobData* blob_data = new BlobData;
|
| - unfinalized_blob_map_[url.spec()] = blob_data;
|
| - IncrementBlobDataUsage(blob_data);
|
| +scoped_ptr<BlobDataHandle> BlobStorageContext::GetBlobDataFromUUID(
|
| + const std::string& uuid) {
|
| + scoped_ptr<BlobDataHandle> result;
|
| + BlobMap::iterator found = blob_map_.find(uuid);
|
| + if (found == blob_map_.end())
|
| + return result.Pass();
|
| + result.reset(new BlobDataHandle(found->second.second, this,
|
| + base::MessageLoopProxy::current()));
|
| + return result.Pass();
|
| }
|
|
|
| -void BlobStorageController::AppendBlobDataItem(
|
| - const GURL& url, const BlobData::Item& item) {
|
| - DCHECK(url.SchemeIs("blob"));
|
| - DCHECK(!BlobUrlHasRef(url));
|
| - BlobMap::iterator found = unfinalized_blob_map_.find(url.spec());
|
| +scoped_ptr<BlobDataHandle> BlobStorageContext::GetBlobDataFromPublicUrl(
|
| + const GURL& url) {
|
| + BlobURLMap::iterator found = public_blob_urls_.find(
|
| + BlobUrlHasRef(url) ? ClearBlobUrlRef(url) : url);
|
| + if (found == public_blob_urls_.end())
|
| + return scoped_ptr<BlobDataHandle>();
|
| + return GetBlobDataFromUUID(found->second);
|
| +}
|
| +
|
| +void BlobStorageContext::StartBuildingBlob(const std::string& uuid) {
|
| + DCHECK(unfinalized_blob_map_.find(uuid) == unfinalized_blob_map_.end());
|
| + unfinalized_blob_map_[uuid] = std::make_pair(1 , new BlobData(uuid));
|
| +}
|
| +
|
| +void BlobStorageContext::AppendBlobDataItem(
|
| + const std::string& uuid, const BlobData::Item& item) {
|
| + BlobMap::iterator found = unfinalized_blob_map_.find(uuid);
|
| if (found == unfinalized_blob_map_.end())
|
| return;
|
| - BlobData* target_blob_data = found->second;
|
| + BlobData* target_blob_data = found->second.second;
|
| DCHECK(target_blob_data);
|
|
|
| memory_usage_ -= target_blob_data->GetMemoryUsage();
|
| @@ -64,10 +185,8 @@
|
| // 1) The Data item is denoted by the raw data and the range.
|
| // 2) The File item is denoted by the file path, the range and the expected
|
| // modification time.
|
| - // 3) The FileSystem File item is denoted by the FileSystem URL, the range
|
| - // and the expected modification time.
|
| // All the Blob items in the passing blob data are resolved and expanded into
|
| - // a set of Data and File items.
|
| + // a set of Data, File, and FileSystemFile items.
|
|
|
| DCHECK(item.length() > 0);
|
| switch (item.type()) {
|
| @@ -84,17 +203,17 @@
|
| break;
|
| case BlobData::Item::TYPE_FILE_FILESYSTEM:
|
| AppendFileSystemFileItem(target_blob_data,
|
| - item.url(),
|
| + item.filesystem_url(),
|
| item.offset(),
|
| item.length(),
|
| item.expected_modification_time());
|
| break;
|
| case BlobData::Item::TYPE_BLOB: {
|
| - BlobData* src_blob_data = GetBlobDataFromUrl(item.url());
|
| - DCHECK(src_blob_data);
|
| - if (src_blob_data)
|
| + scoped_ptr<BlobDataHandle> src = GetBlobDataFromUUID(item.blob_uuid());
|
| + DCHECK(src.get());
|
| + if (src.get())
|
| AppendStorageItems(target_blob_data,
|
| - src_blob_data,
|
| + src->data(),
|
| item.offset(),
|
| item.length());
|
| break;
|
| @@ -109,74 +228,87 @@
|
| // If we're using too much memory, drop this blob.
|
| // TODO(michaeln): Blob memory storage does not yet spill over to disk,
|
| // until it does, we'll prevent memory usage over a max amount.
|
| - if (memory_usage_ > kMaxMemoryUsage)
|
| - RemoveBlob(url);
|
| + if (memory_usage_ > kMaxMemoryUsage) {
|
| + DCHECK_EQ(1, found->second.first);
|
| + memory_usage_ -= target_blob_data->GetMemoryUsage();
|
| + unfinalized_blob_map_.erase(found);
|
| + }
|
| }
|
|
|
| -void BlobStorageController::FinishBuildingBlob(
|
| - const GURL& url, const std::string& content_type) {
|
| - DCHECK(url.SchemeIs("blob"));
|
| - DCHECK(!BlobUrlHasRef(url));
|
| - BlobMap::iterator found = unfinalized_blob_map_.find(url.spec());
|
| - if (found == unfinalized_blob_map_.end())
|
| +void BlobStorageContext::FinishBuildingBlob(
|
| + const std::string& uuid, const std::string& content_type) {
|
| + BlobMap::iterator found = unfinalized_blob_map_.find(uuid);
|
| + if (found == unfinalized_blob_map_.end()) {
|
| + DCHECK(false);
|
| return;
|
| - found->second->set_content_type(content_type);
|
| - blob_map_[url.spec()] = found->second;
|
| + }
|
| + found->second.second->set_content_type(content_type);
|
| + blob_map_[uuid] = std::make_pair(1, found->second.second);
|
| unfinalized_blob_map_.erase(found);
|
| }
|
|
|
| -void BlobStorageController::AddFinishedBlob(const GURL& url,
|
| - const BlobData* data) {
|
| - StartBuildingBlob(url);
|
| +void BlobStorageContext::CancelBuildingBlob(const std::string& uuid) {
|
| + DCHECK(unfinalized_blob_map_.find(uuid) != unfinalized_blob_map_.end());
|
| + DecrementBlobRefCountHelper(&unfinalized_blob_map_, uuid);
|
| + DCHECK(unfinalized_blob_map_.find(uuid) == unfinalized_blob_map_.end());
|
| +}
|
| +
|
| +void BlobStorageContext::AddFinishedBlob(const BlobData* data) {
|
| + StartBuildingBlob(data->uuid());
|
| for (std::vector<BlobData::Item>::const_iterator iter =
|
| data->items().begin();
|
| iter != data->items().end(); ++iter) {
|
| - AppendBlobDataItem(url, *iter);
|
| + AppendBlobDataItem(data->uuid(), *iter);
|
| }
|
| - FinishBuildingBlob(url, data->content_type());
|
| + FinishBuildingBlob(data->uuid(), data->content_type());
|
| }
|
|
|
| -void BlobStorageController::CloneBlob(
|
| - const GURL& url, const GURL& src_url) {
|
| - DCHECK(url.SchemeIs("blob"));
|
| - DCHECK(!BlobUrlHasRef(url));
|
| -
|
| - BlobData* blob_data = GetBlobDataFromUrl(src_url);
|
| - DCHECK(blob_data);
|
| - if (!blob_data)
|
| +void BlobStorageContext::IncrementBlobRefCount(const std::string& uuid) {
|
| + BlobMap::iterator found = blob_map_.find(uuid);
|
| + if (found == blob_map_.end()) {
|
| + DCHECK(false);
|
| return;
|
| -
|
| - blob_map_[url.spec()] = blob_data;
|
| - IncrementBlobDataUsage(blob_data);
|
| + }
|
| + ++(found->second.first);
|
| }
|
|
|
| -void BlobStorageController::RemoveBlob(const GURL& url) {
|
| - DCHECK(url.SchemeIs("blob"));
|
| - DCHECK(!BlobUrlHasRef(url));
|
| -
|
| - if (!RemoveFromMapHelper(&unfinalized_blob_map_, url))
|
| - RemoveFromMapHelper(&blob_map_, url);
|
| +void BlobStorageContext::DecrementBlobRefCount(const std::string& uuid) {
|
| + if (!DecrementBlobRefCountHelper(&unfinalized_blob_map_, uuid))
|
| + DecrementBlobRefCountHelper(&blob_map_, uuid);
|
| }
|
|
|
| -bool BlobStorageController::RemoveFromMapHelper(
|
| - BlobMap* map, const GURL& url) {
|
| - BlobMap::iterator found = map->find(url.spec());
|
| +bool BlobStorageContext::DecrementBlobRefCountHelper(
|
| + BlobMap* map, const std::string& uuid) {
|
| + BlobMap::iterator found = map->find(uuid);
|
| if (found == map->end())
|
| return false;
|
| - if (DecrementBlobDataUsage(found->second))
|
| - memory_usage_ -= found->second->GetMemoryUsage();
|
| - map->erase(found);
|
| + if (--(found->second.first) == 0) {
|
| + if (found->second.second->uuid() == uuid) {
|
| + memory_usage_ -= found->second.second->GetMemoryUsage();
|
| + } else {
|
| + // TODO(michaeln): Remove this once HackCloneBlob is removed,
|
| + // barring that hack, uuid's should always match.
|
| + DecrementBlobRefCount(found->second.second->uuid());
|
| + }
|
| + map->erase(found);
|
| + }
|
| return true;
|
| }
|
|
|
| +void BlobStorageContext::RegisterPublicBlobURL(
|
| + const GURL& blob_url, const std::string& uuid) {
|
| + DCHECK(!BlobUrlHasRef(blob_url));
|
| + IncrementBlobRefCount(uuid);
|
| + public_blob_urls_[blob_url] = uuid;
|
| +}
|
|
|
| -BlobData* BlobStorageController::GetBlobDataFromUrl(const GURL& url) {
|
| - BlobMap::iterator found = blob_map_.find(
|
| - BlobUrlHasRef(url) ? ClearBlobUrlRef(url).spec() : url.spec());
|
| - return (found != blob_map_.end()) ? found->second : NULL;
|
| +void BlobStorageContext::RevokePublicBlobURL(const GURL& blob_url) {
|
| + DCHECK(!BlobUrlHasRef(blob_url));
|
| + DecrementBlobRefCount(public_blob_urls_[blob_url]);
|
| + public_blob_urls_.erase(blob_url);
|
| }
|
|
|
| -void BlobStorageController::AppendStorageItems(
|
| +void BlobStorageContext::AppendStorageItems(
|
| BlobData* target_blob_data, BlobData* src_blob_data,
|
| uint64 offset, uint64 length) {
|
| DCHECK(target_blob_data && src_blob_data &&
|
| @@ -213,7 +345,7 @@
|
| }
|
| }
|
|
|
| -void BlobStorageController::AppendFileItem(
|
| +void BlobStorageContext::AppendFileItem(
|
| BlobData* target_blob_data,
|
| const FilePath& file_path, uint64 offset, uint64 length,
|
| const base::Time& expected_modification_time) {
|
| @@ -227,25 +359,12 @@
|
| target_blob_data->AttachShareableFileReference(shareable_file);
|
| }
|
|
|
| -void BlobStorageController::AppendFileSystemFileItem(
|
| +void BlobStorageContext::AppendFileSystemFileItem(
|
| BlobData* target_blob_data,
|
| - const GURL& url, uint64 offset, uint64 length,
|
| + const GURL& fileSystemUrl, uint64 offset, uint64 length,
|
| const base::Time& expected_modification_time) {
|
| - target_blob_data->AppendFileSystemFile(url, offset, length,
|
| + target_blob_data->AppendFileSystemFile(fileSystemUrl, offset, length,
|
| expected_modification_time);
|
| }
|
|
|
| -void BlobStorageController::IncrementBlobDataUsage(BlobData* blob_data) {
|
| - blob_data_usage_count_[blob_data] += 1;
|
| -}
|
| -
|
| -bool BlobStorageController::DecrementBlobDataUsage(BlobData* blob_data) {
|
| - BlobDataUsageMap::iterator found = blob_data_usage_count_.find(blob_data);
|
| - DCHECK(found != blob_data_usage_count_.end());
|
| - if (--(found->second))
|
| - return false; // Still in use
|
| - blob_data_usage_count_.erase(found);
|
| - return true;
|
| -}
|
| -
|
| } // namespace webkit_blob
|
|
|