Index: webkit/blob/blob_storage_context.cc |
=================================================================== |
--- webkit/blob/blob_storage_context.cc (revision 189105) |
+++ webkit/blob/blob_storage_context.cc (working copy) |
@@ -1,12 +1,16 @@ |
-// Copyright (c) 2012 The Chromium Authors. All rights reserved. |
+// Copyright (c) 2013 The Chromium Authors. All rights reserved. |
// 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" |
+#include "webkit/blob/blob_data_handle.h" |
namespace webkit_blob { |
@@ -28,52 +32,104 @@ |
return GURL(url.spec().substr(0, hash_pos)); |
} |
-static const int64 kMaxMemoryUsage = 1024 * 1024 * 1024; // 1G |
+// TODO(michaeln): use base::SysInfo::AmountOfPhysicalMemoryMB() in some |
+// way to come up with a better limit. |
+static const int64 kMaxMemoryUsage = 500 * 1024 * 1024; // Half a gig. |
} // namespace |
-BlobStorageController::BlobStorageController() |
+BlobStorageContext::BlobMapEntry::BlobMapEntry() |
+ : refcount(0), flags(0) { |
+} |
+ |
+BlobStorageContext::BlobMapEntry::BlobMapEntry( |
+ int refcount, int flags, BlobData* data) |
+ : refcount(refcount), flags(flags), data(data) { |
+} |
+ |
+BlobStorageContext::BlobMapEntry::~BlobMapEntry() { |
+} |
+ |
+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(); |
+ if (found->second.flags & EXCEEDED_MEMORY) |
+ return result.Pass(); |
+ DCHECK(!(found->second.flags & BEING_BUILT)); |
+ result.reset(new BlobDataHandle(found->second.data, 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()); |
- if (found == unfinalized_blob_map_.end()) |
+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); |
+} |
+ |
+scoped_ptr<BlobDataHandle> 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(data->uuid(), *iter); |
+ } |
+ FinishBuildingBlob(data->uuid(), data->content_type()); |
+ scoped_ptr<BlobDataHandle> handle = GetBlobDataFromUUID(data->uuid()); |
+ DecrementBlobRefCount(data->uuid()); |
+ return handle.Pass(); |
+} |
+ |
+void BlobStorageContext::StartBuildingBlob(const std::string& uuid) { |
+ DCHECK(!IsInUse(uuid) && !uuid.empty()); |
+ blob_map_[uuid] = BlobMapEntry(1, BEING_BUILT, new BlobData(uuid)); |
+} |
+ |
+void BlobStorageContext::AppendBlobDataItem( |
+ const std::string& uuid, const BlobData::Item& item) { |
+ DCHECK(IsBeingBuilt(uuid)); |
+ BlobMap::iterator found = blob_map_.find(uuid); |
+ if (found == blob_map_.end()) |
return; |
- BlobData* target_blob_data = found->second; |
+ if (found->second.flags & EXCEEDED_MEMORY) |
+ return; |
+ BlobData* target_blob_data = found->second.data; |
DCHECK(target_blob_data); |
- memory_usage_ -= target_blob_data->GetMemoryUsage(); |
+ bool exceeded_memory = false; |
- // The blob data is stored in the "canonical" way. That is, it only contains a |
- // list of Data and File items. |
- // 1) The Data item is denoted by the raw data and the range. |
+ // The blob data is stored in the canonical way which only contains a |
+ // list of Data, File, and FileSystem items. Aggregated TYPE_BLOB items |
+ // are expanded into the primitive constituent types. |
+ // 1) The Data item is denoted by the raw data and length. |
// 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. |
+ // 4) The Blob items are expanded. |
+ // TODO(michaeln): Would be nice to avoid copying Data items when expanding. |
DCHECK(item.length() > 0); |
switch (item.type()) { |
case BlobData::Item::TYPE_BYTES: |
DCHECK(!item.offset()); |
- target_blob_data->AppendData(item.bytes(), item.length()); |
+ exceeded_memory = !AppendBytesItem(target_blob_data, |
+ item.bytes(), |
+ static_cast<int64>(item.length())); |
break; |
case BlobData::Item::TYPE_FILE: |
AppendFileItem(target_blob_data, |
@@ -84,19 +140,18 @@ |
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) |
- AppendStorageItems(target_blob_data, |
- src_blob_data, |
- item.offset(), |
- item.length()); |
+ scoped_ptr<BlobDataHandle> src = GetBlobDataFromUUID(item.blob_uuid()); |
+ if (src.get()) |
+ exceeded_memory = !ExpandStorageItems(target_blob_data, |
+ src->data(), |
+ item.offset(), |
+ item.length()); |
break; |
} |
default: |
@@ -104,79 +159,70 @@ |
break; |
} |
- memory_usage_ += target_blob_data->GetMemoryUsage(); |
- |
- // If we're using too much memory, drop this blob. |
+ // If we're using too much memory, drop this blob's data. |
// 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); |
+ // as a stop gap, we'll prevent memory usage over a max amount. |
+ if (exceeded_memory) { |
+ memory_usage_ -= target_blob_data->GetMemoryUsage(); |
+ found->second.flags |= EXCEEDED_MEMORY; |
+ found->second.data = new BlobData(uuid); |
+ return; |
+ } |
} |
-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) { |
+ DCHECK(IsBeingBuilt(uuid)); |
+ BlobMap::iterator found = blob_map_.find(uuid); |
+ if (found == blob_map_.end()) |
return; |
- found->second->set_content_type(content_type); |
- blob_map_[url.spec()] = found->second; |
- unfinalized_blob_map_.erase(found); |
+ found->second.data->set_content_type(content_type); |
+ found->second.flags &= ~BEING_BUILT; |
} |
-void BlobStorageController::AddFinishedBlob(const GURL& url, |
- const BlobData* data) { |
- StartBuildingBlob(url); |
- for (std::vector<BlobData::Item>::const_iterator iter = |
- data->items().begin(); |
- iter != data->items().end(); ++iter) { |
- AppendBlobDataItem(url, *iter); |
- } |
- FinishBuildingBlob(url, data->content_type()); |
+void BlobStorageContext::CancelBuildingBlob(const std::string& uuid) { |
+ DCHECK(IsBeingBuilt(uuid)); |
+ DecrementBlobRefCount(uuid); |
} |
-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.refcount); |
} |
-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) { |
+ BlobMap::iterator found = blob_map_.find(uuid); |
+ if (found == blob_map_.end()) |
+ return; |
+ DCHECK_EQ(found->second.data->uuid(), uuid); |
+ if (--(found->second.refcount) == 0) { |
+ memory_usage_ -= found->second.data->GetMemoryUsage(); |
+ blob_map_.erase(found); |
+ } |
} |
-bool BlobStorageController::RemoveFromMapHelper( |
- BlobMap* map, const GURL& url) { |
- BlobMap::iterator found = map->find(url.spec()); |
- if (found == map->end()) |
- return false; |
- if (DecrementBlobDataUsage(found->second)) |
- memory_usage_ -= found->second->GetMemoryUsage(); |
- map->erase(found); |
- return true; |
+void BlobStorageContext::RegisterPublicBlobURL( |
+ const GURL& blob_url, const std::string& uuid) { |
+ DCHECK(!BlobUrlHasRef(blob_url)); |
+ DCHECK(IsInUse(uuid)); |
+ DCHECK(!IsUrlRegistered(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)); |
+ if (!IsUrlRegistered(blob_url)) |
+ return; |
+ DecrementBlobRefCount(public_blob_urls_[blob_url]); |
+ public_blob_urls_.erase(blob_url); |
} |
-void BlobStorageController::AppendStorageItems( |
+bool BlobStorageContext::ExpandStorageItems( |
BlobData* target_blob_data, BlobData* src_blob_data, |
uint64 offset, uint64 length) { |
DCHECK(target_blob_data && src_blob_data && |
@@ -197,9 +243,12 @@ |
uint64 current_length = iter->length() - offset; |
uint64 new_length = current_length > length ? length : current_length; |
if (iter->type() == BlobData::Item::TYPE_BYTES) { |
- target_blob_data->AppendData( |
- iter->bytes() + static_cast<size_t>(iter->offset() + offset), |
- static_cast<uint32>(new_length)); |
+ if (!AppendBytesItem( |
+ target_blob_data, |
+ iter->bytes() + static_cast<size_t>(iter->offset() + offset), |
+ static_cast<int64>(new_length))) { |
+ return false; // exceeded memory |
+ } |
} else if (iter->type() == BlobData::Item::TYPE_FILE) { |
AppendFileItem(target_blob_data, |
iter->path(), |
@@ -209,7 +258,7 @@ |
} else { |
DCHECK(iter->type() == BlobData::Item::TYPE_FILE_FILESYSTEM); |
AppendFileSystemFileItem(target_blob_data, |
- iter->url(), |
+ iter->filesystem_url(), |
iter->offset() + offset, |
new_length, |
iter->expected_modification_time()); |
@@ -217,9 +266,23 @@ |
length -= new_length; |
offset = 0; |
} |
+ return true; |
} |
-void BlobStorageController::AppendFileItem( |
+bool BlobStorageContext::AppendBytesItem( |
+ BlobData* target_blob_data, const char* bytes, int64 length) { |
+ if (length < 0) { |
+ DCHECK(false); |
+ return false; |
+ } |
+ if (memory_usage_ + length > kMaxMemoryUsage) |
+ return false; |
+ target_blob_data->AppendData(bytes, static_cast<size_t>(length)); |
+ memory_usage_ += length; |
+ return true; |
+} |
+ |
+void BlobStorageContext::AppendFileItem( |
BlobData* target_blob_data, |
const base::FilePath& file_path, uint64 offset, uint64 length, |
const base::Time& expected_modification_time) { |
@@ -233,25 +296,27 @@ |
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& filesystem_url, uint64 offset, uint64 length, |
const base::Time& expected_modification_time) { |
- target_blob_data->AppendFileSystemFile(url, offset, length, |
+ target_blob_data->AppendFileSystemFile(filesystem_url, offset, length, |
expected_modification_time); |
} |
-void BlobStorageController::IncrementBlobDataUsage(BlobData* blob_data) { |
- blob_data_usage_count_[blob_data] += 1; |
+bool BlobStorageContext::IsInUse(const std::string& uuid) { |
+ return blob_map_.find(uuid) != blob_map_.end(); |
} |
-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; |
+bool BlobStorageContext::IsBeingBuilt(const std::string& uuid) { |
+ BlobMap::iterator found = blob_map_.find(uuid); |
+ if (found == blob_map_.end()) |
+ return false; |
+ return found->second.flags & BEING_BUILT; |
} |
+bool BlobStorageContext::IsUrlRegistered(const GURL& blob_url) { |
+ return public_blob_urls_.find(blob_url) != public_blob_urls_.end(); |
+} |
+ |
} // namespace webkit_blob |