| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "webkit/blob/blob_storage_controller.h" | 5 #include "webkit/blob/blob_storage_controller.h" |
| 6 | 6 |
| 7 #include "base/logging.h" | 7 #include "base/logging.h" |
| 8 #include "googleurl/src/gurl.h" | 8 #include "googleurl/src/gurl.h" |
| 9 #include "net/base/upload_data.h" | 9 #include "net/base/upload_data.h" |
| 10 #include "webkit/blob/blob_data.h" | 10 #include "webkit/blob/blob_data.h" |
| (...skipping 11 matching lines...) Expand all Loading... |
| 22 return url.spec().find('#') != std::string::npos; | 22 return url.spec().find('#') != std::string::npos; |
| 23 } | 23 } |
| 24 | 24 |
| 25 GURL ClearBlobUrlRef(const GURL& url) { | 25 GURL ClearBlobUrlRef(const GURL& url) { |
| 26 size_t hash_pos = url.spec().find('#'); | 26 size_t hash_pos = url.spec().find('#'); |
| 27 if (hash_pos == std::string::npos) | 27 if (hash_pos == std::string::npos) |
| 28 return url; | 28 return url; |
| 29 return GURL(url.spec().substr(0, hash_pos)); | 29 return GURL(url.spec().substr(0, hash_pos)); |
| 30 } | 30 } |
| 31 | 31 |
| 32 static const int64 kMaxMemoryUsage = 1073741824; // 1G |
| 33 |
| 32 } // namespace | 34 } // namespace |
| 33 | 35 |
| 34 BlobStorageController::BlobStorageController() { | 36 BlobStorageController::BlobStorageController() |
| 37 : memory_usage_(0) { |
| 35 } | 38 } |
| 36 | 39 |
| 37 BlobStorageController::~BlobStorageController() { | 40 BlobStorageController::~BlobStorageController() { |
| 38 } | 41 } |
| 39 | 42 |
| 40 void BlobStorageController::RegisterBlobUrl( | 43 void BlobStorageController::RegisterUnfinalizedBlobUrl(const GURL& url) { |
| 41 const GURL& url, const BlobData* blob_data) { | |
| 42 DCHECK(url.SchemeIs("blob")); | 44 DCHECK(url.SchemeIs("blob")); |
| 43 DCHECK(!BlobUrlHasRef(url)); | 45 DCHECK(!BlobUrlHasRef(url)); |
| 46 BlobData* blob_data = new BlobData; |
| 47 unfinalized_blob_map_[url.spec()] = blob_data; |
| 48 IncrementBlobDataUsage(blob_data); |
| 49 } |
| 44 | 50 |
| 45 scoped_refptr<BlobData> target_blob_data(new BlobData()); | 51 void BlobStorageController::AppendBlobDataItem( |
| 46 target_blob_data->set_content_type(blob_data->content_type()); | 52 const GURL& url, const webkit_blob::BlobData::Item& item) { |
| 47 target_blob_data->set_content_disposition(blob_data->content_disposition()); | 53 DCHECK(url.SchemeIs("blob")); |
| 54 DCHECK(!BlobUrlHasRef(url)); |
| 55 BlobMap::iterator found = unfinalized_blob_map_.find(url.spec()); |
| 56 if (found == unfinalized_blob_map_.end()) |
| 57 return; |
| 58 BlobData* target_blob_data = found->second; |
| 59 DCHECK(target_blob_data); |
| 60 |
| 61 memory_usage_ -= target_blob_data->GetMemoryUsage(); |
| 48 | 62 |
| 49 // The blob data is stored in the "canonical" way. That is, it only contains a | 63 // The blob data is stored in the "canonical" way. That is, it only contains a |
| 50 // list of Data and File items. | 64 // list of Data and File items. |
| 51 // 1) The Data item is denoted by the raw data and the range. | 65 // 1) The Data item is denoted by the raw data and the range. |
| 52 // 2) The File item is denoted by the file path, the range and the expected | 66 // 2) The File item is denoted by the file path, the range and the expected |
| 53 // modification time. | 67 // modification time. |
| 54 // All the Blob items in the passing blob data are resolved and expanded into | 68 // All the Blob items in the passing blob data are resolved and expanded into |
| 55 // a set of Data and File items. | 69 // a set of Data and File items. |
| 56 | 70 |
| 57 for (std::vector<BlobData::Item>::const_iterator iter = | 71 switch (item.type()) { |
| 58 blob_data->items().begin(); | 72 case BlobData::TYPE_DATA: |
| 59 iter != blob_data->items().end(); ++iter) { | 73 // WebBlobData does not allow partial data. |
| 60 switch (iter->type()) { | 74 DCHECK(!(item.offset()) && item.length() == item.data().size()); |
| 61 case BlobData::TYPE_DATA: { | 75 target_blob_data->AppendData(item.data()); |
| 62 // WebBlobData does not allow partial data. | 76 break; |
| 63 DCHECK(!(iter->offset()) && iter->length() == iter->data().size()); | 77 case BlobData::TYPE_FILE: |
| 64 target_blob_data->AppendData(iter->data()); | 78 AppendFileItem(target_blob_data, |
| 65 break; | 79 item.file_path(), |
| 66 } | 80 item.offset(), |
| 67 case BlobData::TYPE_FILE: | 81 item.length(), |
| 68 AppendFileItem(target_blob_data, | 82 item.expected_modification_time()); |
| 69 iter->file_path(), | 83 break; |
| 70 iter->offset(), | 84 case BlobData::TYPE_BLOB: |
| 71 iter->length(), | 85 BlobData* src_blob_data = GetBlobDataFromUrl(item.blob_url()); |
| 72 iter->expected_modification_time()); | 86 DCHECK(src_blob_data); |
| 73 break; | 87 if (src_blob_data) |
| 74 case BlobData::TYPE_BLOB: { | 88 AppendStorageItems(target_blob_data, |
| 75 BlobData* src_blob_data = GetBlobDataFromUrl(iter->blob_url()); | 89 src_blob_data, |
| 76 DCHECK(src_blob_data); | 90 item.offset(), |
| 77 if (src_blob_data) | 91 item.length()); |
| 78 AppendStorageItems(target_blob_data.get(), | 92 break; |
| 79 src_blob_data, | |
| 80 iter->offset(), | |
| 81 iter->length()); | |
| 82 break; | |
| 83 } | |
| 84 } | |
| 85 } | 93 } |
| 86 | 94 |
| 87 blob_map_[url.spec()] = target_blob_data; | 95 memory_usage_ += target_blob_data->GetMemoryUsage(); |
| 96 |
| 97 // If we're using too much memory, drop this blob. |
| 98 // TODO(michaeln): Blob memory storage does not yet spill over to disk, |
| 99 // until it does, we'll prevent memory usage over a max amount. |
| 100 if (memory_usage_ > kMaxMemoryUsage) |
| 101 UnregisterBlobUrl(url); |
| 102 } |
| 103 |
| 104 void BlobStorageController::FinalizeBlob( |
| 105 const GURL& url, const std::string& content_type) { |
| 106 DCHECK(url.SchemeIs("blob")); |
| 107 DCHECK(!BlobUrlHasRef(url)); |
| 108 BlobMap::iterator found = unfinalized_blob_map_.find(url.spec()); |
| 109 if (found == unfinalized_blob_map_.end()) |
| 110 return; |
| 111 found->second->set_content_type(content_type); |
| 112 blob_map_[url.spec()] = found->second; |
| 113 unfinalized_blob_map_.erase(found); |
| 88 } | 114 } |
| 89 | 115 |
| 90 void BlobStorageController::RegisterBlobUrlFrom( | 116 void BlobStorageController::RegisterBlobUrlFrom( |
| 91 const GURL& url, const GURL& src_url) { | 117 const GURL& url, const GURL& src_url) { |
| 92 DCHECK(url.SchemeIs("blob")); | 118 DCHECK(url.SchemeIs("blob")); |
| 93 DCHECK(!BlobUrlHasRef(url)); | 119 DCHECK(!BlobUrlHasRef(url)); |
| 94 | 120 |
| 95 BlobData* blob_data = GetBlobDataFromUrl(src_url); | 121 BlobData* blob_data = GetBlobDataFromUrl(src_url); |
| 96 DCHECK(blob_data); | 122 DCHECK(blob_data); |
| 97 if (!blob_data) | 123 if (!blob_data) |
| 98 return; | 124 return; |
| 99 | 125 |
| 100 blob_map_[url.spec()] = blob_data; | 126 blob_map_[url.spec()] = blob_data; |
| 127 IncrementBlobDataUsage(blob_data); |
| 101 } | 128 } |
| 102 | 129 |
| 103 void BlobStorageController::UnregisterBlobUrl(const GURL& url) { | 130 void BlobStorageController::UnregisterBlobUrl(const GURL& url) { |
| 104 blob_map_.erase(url.spec()); | 131 DCHECK(url.SchemeIs("blob")); |
| 132 DCHECK(!BlobUrlHasRef(url)); |
| 133 |
| 134 if (!RemoveFromMapHelper(&unfinalized_blob_map_, url)) |
| 135 RemoveFromMapHelper(&blob_map_, url); |
| 105 } | 136 } |
| 106 | 137 |
| 138 bool BlobStorageController::RemoveFromMapHelper( |
| 139 BlobMap* map, const GURL& url) { |
| 140 BlobMap::iterator found = map->find(url.spec()); |
| 141 if (found == map->end()) |
| 142 return false; |
| 143 if (DecrementBlobDataUsage(found->second)) |
| 144 memory_usage_ -= found->second->GetMemoryUsage(); |
| 145 map->erase(found); |
| 146 return true; |
| 147 } |
| 148 |
| 149 |
| 107 BlobData* BlobStorageController::GetBlobDataFromUrl(const GURL& url) { | 150 BlobData* BlobStorageController::GetBlobDataFromUrl(const GURL& url) { |
| 108 BlobMap::iterator found = blob_map_.find( | 151 BlobMap::iterator found = blob_map_.find( |
| 109 BlobUrlHasRef(url) ? ClearBlobUrlRef(url).spec() : url.spec()); | 152 BlobUrlHasRef(url) ? ClearBlobUrlRef(url).spec() : url.spec()); |
| 110 return (found != blob_map_.end()) ? found->second : NULL; | 153 return (found != blob_map_.end()) ? found->second : NULL; |
| 111 } | 154 } |
| 112 | 155 |
| 113 void BlobStorageController::ResolveBlobReferencesInUploadData( | 156 void BlobStorageController::ResolveBlobReferencesInUploadData( |
| 114 net::UploadData* upload_data) { | 157 net::UploadData* upload_data) { |
| 115 DCHECK(upload_data); | 158 DCHECK(upload_data); |
| 116 | 159 |
| (...skipping 98 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 215 target_blob_data->AppendFile(file_path, offset, length, | 258 target_blob_data->AppendFile(file_path, offset, length, |
| 216 expected_modification_time); | 259 expected_modification_time); |
| 217 | 260 |
| 218 // It may be a temporary file that should be deleted when no longer needed. | 261 // It may be a temporary file that should be deleted when no longer needed. |
| 219 scoped_refptr<DeletableFileReference> deletable_file = | 262 scoped_refptr<DeletableFileReference> deletable_file = |
| 220 DeletableFileReference::Get(file_path); | 263 DeletableFileReference::Get(file_path); |
| 221 if (deletable_file) | 264 if (deletable_file) |
| 222 target_blob_data->AttachDeletableFileReference(deletable_file); | 265 target_blob_data->AttachDeletableFileReference(deletable_file); |
| 223 } | 266 } |
| 224 | 267 |
| 268 void BlobStorageController::IncrementBlobDataUsage(BlobData* blob_data) { |
| 269 blob_data_usage_count_[blob_data] += 1; |
| 270 } |
| 271 |
| 272 bool BlobStorageController::DecrementBlobDataUsage(BlobData* blob_data) { |
| 273 BlobDataUsageMap::iterator found = blob_data_usage_count_.find(blob_data); |
| 274 DCHECK(found != blob_data_usage_count_.end()); |
| 275 if (--(found->second)) |
| 276 return false; // Still in use |
| 277 blob_data_usage_count_.erase(found); |
| 278 return true; |
| 279 } |
| 280 |
| 225 } // namespace webkit_blob | 281 } // namespace webkit_blob |
| OLD | NEW |