Chromium Code Reviews| Index: webkit/blob/blob_storage_controller.cc |
| =================================================================== |
| --- webkit/blob/blob_storage_controller.cc (revision 102191) |
| +++ webkit/blob/blob_storage_controller.cc (working copy) |
| @@ -29,23 +29,35 @@ |
| return GURL(url.spec().substr(0, hash_pos)); |
| } |
| +static const int64 kMaxMemoryUsage = 1073741824; // 1G |
| + |
| } // namespace |
| -BlobStorageController::BlobStorageController() { |
| +BlobStorageController::BlobStorageController() |
| + : memory_usage_(0) { |
| } |
| BlobStorageController::~BlobStorageController() { |
| } |
| -void BlobStorageController::RegisterBlobUrl( |
| - const GURL& url, const BlobData* blob_data) { |
| +void BlobStorageController::RegisterUnfinalizedBlobUrl(const GURL& url) { |
| DCHECK(url.SchemeIs("blob")); |
| DCHECK(!BlobUrlHasRef(url)); |
| + unfinalized_blob_map_[url.spec()] = new BlobData(); |
| +} |
| - scoped_refptr<BlobData> target_blob_data(new BlobData()); |
| - target_blob_data->set_content_type(blob_data->content_type()); |
| - target_blob_data->set_content_disposition(blob_data->content_disposition()); |
| +void BlobStorageController::AppendBlobDataItem( |
| + const GURL& url, const webkit_blob::BlobData::Item& item) { |
| + DCHECK(url.SchemeIs("blob")); |
| + DCHECK(!BlobUrlHasRef(url)); |
| + BlobMap::iterator found = unfinalized_blob_map_.find(url.spec()); |
|
jianli
2011/09/23 23:00:42
Can we introduce a helper function like GetUnfinal
michaeln
2011/09/24 01:15:24
In most cases, we actually want the iter more than
|
| + if (found == unfinalized_blob_map_.end()) |
| + return; |
| + BlobData* target_blob_data = found->second; |
| + DCHECK(target_blob_data); |
| + memory_usage_ -= target_blob_data->GetMemoryUsage(); |
|
jianli
2011/09/23 23:00:42
Can we avoid re-enumerating all the items to count
michaeln
2011/09/24 01:15:24
The value could be cached in BlobData, but i don't
|
| + |
| // 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. |
| @@ -54,39 +66,51 @@ |
| // All the Blob items in the passing blob data are resolved and expanded into |
| // a set of Data and File items. |
| - for (std::vector<BlobData::Item>::const_iterator iter = |
| - blob_data->items().begin(); |
| - iter != blob_data->items().end(); ++iter) { |
| - switch (iter->type()) { |
| - case BlobData::TYPE_DATA: { |
| - // WebBlobData does not allow partial data. |
| - DCHECK(!(iter->offset()) && iter->length() == iter->data().size()); |
| - target_blob_data->AppendData(iter->data()); |
| - break; |
| - } |
| - case BlobData::TYPE_FILE: |
| - AppendFileItem(target_blob_data, |
| - iter->file_path(), |
| - iter->offset(), |
| - iter->length(), |
| - iter->expected_modification_time()); |
| - break; |
| - case BlobData::TYPE_BLOB: { |
| - BlobData* src_blob_data = GetBlobDataFromUrl(iter->blob_url()); |
| - DCHECK(src_blob_data); |
| - if (src_blob_data) |
| - AppendStorageItems(target_blob_data.get(), |
| - src_blob_data, |
| - iter->offset(), |
| - iter->length()); |
| - break; |
| - } |
| - } |
| + switch (item.type()) { |
| + case BlobData::TYPE_DATA: |
| + // WebBlobData does not allow partial data. |
| + DCHECK(!(item.offset()) && item.length() == item.data().size()); |
| + target_blob_data->AppendData(item.data()); |
| + break; |
| + case BlobData::TYPE_FILE: |
| + AppendFileItem(target_blob_data, |
| + item.file_path(), |
| + item.offset(), |
| + item.length(), |
| + item.expected_modification_time()); |
| + break; |
| + case BlobData::TYPE_BLOB: |
| + BlobData* src_blob_data = GetBlobDataFromUrl(item.blob_url()); |
| + DCHECK(src_blob_data); |
| + if (src_blob_data) |
| + AppendStorageItems(target_blob_data, |
| + src_blob_data, |
| + item.offset(), |
| + item.length()); |
| + break; |
| } |
| - blob_map_[url.spec()] = target_blob_data; |
| + memory_usage_ += target_blob_data->GetMemoryUsage(); |
| + |
| + // 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) |
| + UnregisterBlobUrl(url); |
| } |
| +void BlobStorageController::FinalizeBlob( |
| + 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()) |
| + return; |
| + found->second->set_content_type(content_type); |
| + blob_map_[url.spec()] = found->second; |
| + unfinalized_blob_map_.erase(found); |
| +} |
| + |
| void BlobStorageController::RegisterBlobUrlFrom( |
| const GURL& url, const GURL& src_url) { |
| DCHECK(url.SchemeIs("blob")); |
| @@ -101,7 +125,21 @@ |
| } |
| void BlobStorageController::UnregisterBlobUrl(const GURL& url) { |
| - blob_map_.erase(url.spec()); |
| + DCHECK(url.SchemeIs("blob")); |
| + DCHECK(!BlobUrlHasRef(url)); |
| + |
| + BlobMap::iterator found = unfinalized_blob_map_.find(url.spec()); |
| + if (found != unfinalized_blob_map_.end()) { |
| + memory_usage_ -= found->second->GetMemoryUsage(); |
| + unfinalized_blob_map_.erase(found); |
| + return; |
| + } |
| + |
| + found = blob_map_.find(url.spec()); |
| + if (found != blob_map_.end()) { |
| + memory_usage_ -= found->second->GetMemoryUsage(); |
| + blob_map_.erase(found); |
| + } |
| } |
| BlobData* BlobStorageController::GetBlobDataFromUrl(const GURL& url) { |