Chromium Code Reviews| 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 | |
|
jianli
2011/09/29 01:13:33
How about sth like: 1024 x 1024 x 1024 to make it
michaeln
2011/09/29 18:59:37
Done.
| |
| 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 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->AppendItem(item); |
| 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)) | |
|
jianli
2011/09/29 01:13:33
Just want to check whether it is possible to rely
michaeln
2011/09/29 18:59:37
It's an option, but that would mean more tightly c
| |
| 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 |
| 117 std::vector<net::UploadData::Element>* uploads = upload_data->elements(); | 160 std::vector<net::UploadData::Element>* uploads = upload_data->elements(); |
| 118 std::vector<net::UploadData::Element>::iterator iter; | 161 std::vector<net::UploadData::Element>::iterator iter; |
| 119 for (iter = uploads->begin(); iter != uploads->end();) { | 162 for (iter = uploads->begin(); iter != uploads->end();) { |
| 120 if (iter->type() != net::UploadData::TYPE_BLOB) { | 163 if (iter->type() != net::UploadData::TYPE_BLOB) { |
| 121 iter++; | 164 iter++; |
| 122 continue; | 165 continue; |
| 123 } | 166 } |
| 124 | 167 |
| 125 // Find the referred blob data. | 168 // Find the referred blob data. |
| 126 webkit_blob::BlobData* blob_data = GetBlobDataFromUrl(iter->blob_url()); | 169 BlobData* blob_data = GetBlobDataFromUrl(iter->blob_url()); |
| 127 DCHECK(blob_data); | 170 DCHECK(blob_data); |
| 128 if (!blob_data) { | 171 if (!blob_data) { |
| 129 // TODO(jianli): We should probably fail uploading the data | 172 // TODO(jianli): We should probably fail uploading the data |
| 130 iter++; | 173 iter++; |
| 131 continue; | 174 continue; |
| 132 } | 175 } |
| 133 | 176 |
| 134 // Remove this element. | 177 // Remove this element. |
| 135 iter = uploads->erase(iter); | 178 iter = uploads->erase(iter); |
| 136 | 179 |
| 137 // If there is no element in the referred blob data, continue the loop. | 180 // If there is no element in the referred blob data, continue the loop. |
| 138 // Note that we should not increase iter since it already points to the one | 181 // Note that we should not increase iter since it already points to the one |
| 139 // after the removed element. | 182 // after the removed element. |
| 140 if (blob_data->items().empty()) | 183 if (blob_data->items().empty()) |
| 141 continue; | 184 continue; |
| 142 | 185 |
| 143 // Insert the elements in the referred blob data. | 186 // Insert the elements in the referred blob data. |
| 144 // Note that we traverse from the bottom so that the elements can be | 187 // Note that we traverse from the bottom so that the elements can be |
| 145 // inserted in the original order. | 188 // inserted in the original order. |
| 146 for (size_t i = blob_data->items().size(); i > 0; --i) { | 189 for (size_t i = blob_data->items().size(); i > 0; --i) { |
| 147 iter = uploads->insert(iter, net::UploadData::Element()); | 190 iter = uploads->insert(iter, net::UploadData::Element()); |
| 148 | 191 |
| 149 const webkit_blob::BlobData::Item& item = blob_data->items().at(i - 1); | 192 const BlobData::Item& item = blob_data->items().at(i - 1); |
| 150 switch (item.type()) { | 193 switch (item.type) { |
| 151 case webkit_blob::BlobData::TYPE_DATA: | 194 case BlobData::TYPE_DATA: |
| 152 // TODO(jianli): Figure out how to avoid copying the data. | 195 // TODO(jianli): Figure out how to avoid copying the data. |
| 153 iter->SetToBytes( | 196 iter->SetToBytes( |
| 154 &item.data().at(0) + static_cast<int>(item.offset()), | 197 &item.data.at(0) + static_cast<int>(item.offset), |
| 155 static_cast<int>(item.length())); | 198 static_cast<int>(item.length)); |
| 156 break; | 199 break; |
| 157 case webkit_blob::BlobData::TYPE_FILE: | 200 case BlobData::TYPE_FILE: |
| 158 // TODO(michaeln): Ensure that any temp files survive till the | 201 // TODO(michaeln): Ensure that any temp files survive till the |
| 159 // net::URLRequest is done with the upload. | 202 // net::URLRequest is done with the upload. |
| 160 iter->SetToFilePathRange( | 203 iter->SetToFilePathRange( |
| 161 item.file_path(), | 204 item.file_path, |
| 162 item.offset(), | 205 item.offset, |
| 163 item.length(), | 206 item.length, |
| 164 item.expected_modification_time()); | 207 item.expected_modification_time); |
| 165 break; | 208 break; |
| 166 default: | 209 default: |
| 167 NOTREACHED(); | 210 NOTREACHED(); |
| 168 break; | 211 break; |
| 169 } | 212 } |
| 170 } | 213 } |
| 171 } | 214 } |
| 172 } | 215 } |
| 173 | 216 |
| 174 void BlobStorageController::AppendStorageItems( | 217 void BlobStorageController::AppendStorageItems( |
| 175 BlobData* target_blob_data, BlobData* src_blob_data, | 218 BlobData* target_blob_data, BlobData* src_blob_data, |
| 176 uint64 offset, uint64 length) { | 219 uint64 offset, uint64 length) { |
| 177 DCHECK(target_blob_data && src_blob_data && | 220 DCHECK(target_blob_data && src_blob_data && |
| 178 length != static_cast<uint64>(-1)); | 221 length != static_cast<uint64>(-1)); |
| 179 | 222 |
| 180 std::vector<BlobData::Item>::const_iterator iter = | 223 std::vector<BlobData::Item>::const_iterator iter = |
| 181 src_blob_data->items().begin(); | 224 src_blob_data->items().begin(); |
| 182 if (offset) { | 225 if (offset) { |
| 183 for (; iter != src_blob_data->items().end(); ++iter) { | 226 for (; iter != src_blob_data->items().end(); ++iter) { |
| 184 if (offset >= iter->length()) | 227 if (offset >= iter->length) |
| 185 offset -= iter->length(); | 228 offset -= iter->length; |
| 186 else | 229 else |
| 187 break; | 230 break; |
| 188 } | 231 } |
| 189 } | 232 } |
| 190 | 233 |
| 191 for (; iter != src_blob_data->items().end() && length > 0; ++iter) { | 234 for (; iter != src_blob_data->items().end() && length > 0; ++iter) { |
| 192 uint64 current_length = iter->length() - offset; | 235 uint64 current_length = iter->length - offset; |
| 193 uint64 new_length = current_length > length ? length : current_length; | 236 uint64 new_length = current_length > length ? length : current_length; |
| 194 if (iter->type() == BlobData::TYPE_DATA) { | 237 if (iter->type == BlobData::TYPE_DATA) { |
| 195 target_blob_data->AppendData(iter->data(), | 238 target_blob_data->AppendData(iter->data, |
| 196 static_cast<uint32>(iter->offset() + offset), | 239 static_cast<uint32>(iter->offset + offset), |
| 197 static_cast<uint32>(new_length)); | 240 static_cast<uint32>(new_length)); |
| 198 } else { | 241 } else { |
| 199 DCHECK(iter->type() == BlobData::TYPE_FILE); | 242 DCHECK(iter->type == BlobData::TYPE_FILE); |
| 200 AppendFileItem(target_blob_data, | 243 AppendFileItem(target_blob_data, |
| 201 iter->file_path(), | 244 iter->file_path, |
| 202 iter->offset() + offset, | 245 iter->offset + offset, |
| 203 new_length, | 246 new_length, |
| 204 iter->expected_modification_time()); | 247 iter->expected_modification_time); |
| 205 } | 248 } |
| 206 length -= new_length; | 249 length -= new_length; |
| 207 offset = 0; | 250 offset = 0; |
| 208 } | 251 } |
| 209 } | 252 } |
| 210 | 253 |
| 211 void BlobStorageController::AppendFileItem( | 254 void BlobStorageController::AppendFileItem( |
| 212 BlobData* target_blob_data, | 255 BlobData* target_blob_data, |
| 213 const FilePath& file_path, uint64 offset, uint64 length, | 256 const FilePath& file_path, uint64 offset, uint64 length, |
| 214 const base::Time& expected_modification_time) { | 257 const base::Time& expected_modification_time) { |
| 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 |