| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 58 BlobData* target_blob_data = found->second; | 58 BlobData* target_blob_data = found->second; |
| 59 DCHECK(target_blob_data); | 59 DCHECK(target_blob_data); |
| 60 | 60 |
| 61 memory_usage_ -= target_blob_data->GetMemoryUsage(); | 61 memory_usage_ -= target_blob_data->GetMemoryUsage(); |
| 62 | 62 |
| 63 // 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 |
| 64 // list of Data and File items. | 64 // list of Data and File items. |
| 65 // 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. |
| 66 // 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 |
| 67 // modification time. | 67 // modification time. |
| 68 // 3) The FileSystem File item is denoted by the FileSystem URL, the range |
| 69 // and the expected modification time. |
| 68 // All the Blob items in the passing blob data are resolved and expanded into | 70 // All the Blob items in the passing blob data are resolved and expanded into |
| 69 // a set of Data and File items. | 71 // a set of Data and File items. |
| 70 | 72 |
| 71 DCHECK(item.length > 0); | 73 DCHECK(item.length > 0); |
| 72 switch (item.type) { | 74 switch (item.type) { |
| 73 case BlobData::TYPE_DATA: | 75 case BlobData::TYPE_DATA: |
| 74 // WebBlobData does not allow partial data. | 76 // WebBlobData does not allow partial data. |
| 75 DCHECK(!(item.offset) && item.length == item.data.size()); | 77 DCHECK(!(item.offset) && item.length == item.data.size()); |
| 76 target_blob_data->AppendData(item.data.c_str(), item.data.size()); | 78 target_blob_data->AppendData(item.data.c_str(), item.data.size()); |
| 77 break; | 79 break; |
| 78 case BlobData::TYPE_DATA_EXTERNAL: | 80 case BlobData::TYPE_DATA_EXTERNAL: |
| 79 DCHECK(!item.offset); | 81 DCHECK(!item.offset); |
| 80 target_blob_data->AppendData(item.data_external, item.length); | 82 target_blob_data->AppendData(item.data_external, item.length); |
| 81 break; | 83 break; |
| 82 case BlobData::TYPE_FILE: | 84 case BlobData::TYPE_FILE: |
| 83 AppendFileItem(target_blob_data, | 85 AppendFileItem(target_blob_data, |
| 84 item.file_path, | 86 item.file_path, |
| 85 item.offset, | 87 item.offset, |
| 86 item.length, | 88 item.length, |
| 87 item.expected_modification_time); | 89 item.expected_modification_time); |
| 88 break; | 90 break; |
| 89 case BlobData::TYPE_BLOB: | 91 case BlobData::TYPE_BLOB: { |
| 90 BlobData* src_blob_data = GetBlobDataFromUrl(item.blob_url); | 92 BlobData* src_blob_data = GetBlobDataFromUrl(item.url); |
| 91 DCHECK(src_blob_data); | 93 DCHECK(src_blob_data); |
| 92 if (src_blob_data) | 94 if (src_blob_data) |
| 93 AppendStorageItems(target_blob_data, | 95 AppendStorageItems(target_blob_data, |
| 94 src_blob_data, | 96 src_blob_data, |
| 95 item.offset, | 97 item.offset, |
| 96 item.length); | 98 item.length); |
| 97 break; | 99 break; |
| 100 } |
| 101 case BlobData::TYPE_FILE_FILESYSTEM: |
| 102 AppendFileSystemFileItem(target_blob_data, |
| 103 item.url, |
| 104 item.offset, |
| 105 item.length, |
| 106 item.expected_modification_time); |
| 107 break; |
| 98 } | 108 } |
| 99 | 109 |
| 100 memory_usage_ += target_blob_data->GetMemoryUsage(); | 110 memory_usage_ += target_blob_data->GetMemoryUsage(); |
| 101 | 111 |
| 102 // If we're using too much memory, drop this blob. | 112 // If we're using too much memory, drop this blob. |
| 103 // TODO(michaeln): Blob memory storage does not yet spill over to disk, | 113 // TODO(michaeln): Blob memory storage does not yet spill over to disk, |
| 104 // until it does, we'll prevent memory usage over a max amount. | 114 // until it does, we'll prevent memory usage over a max amount. |
| 105 if (memory_usage_ > kMaxMemoryUsage) | 115 if (memory_usage_ > kMaxMemoryUsage) |
| 106 RemoveBlob(url); | 116 RemoveBlob(url); |
| 107 } | 117 } |
| (...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 176 std::vector<net::UploadData::Element>* uploads = | 186 std::vector<net::UploadData::Element>* uploads = |
| 177 upload_data->elements_mutable(); | 187 upload_data->elements_mutable(); |
| 178 std::vector<net::UploadData::Element>::iterator iter; | 188 std::vector<net::UploadData::Element>::iterator iter; |
| 179 for (iter = uploads->begin(); iter != uploads->end();) { | 189 for (iter = uploads->begin(); iter != uploads->end();) { |
| 180 if (iter->type() != net::UploadData::TYPE_BLOB) { | 190 if (iter->type() != net::UploadData::TYPE_BLOB) { |
| 181 iter++; | 191 iter++; |
| 182 continue; | 192 continue; |
| 183 } | 193 } |
| 184 | 194 |
| 185 // Find the referred blob data. | 195 // Find the referred blob data. |
| 186 BlobData* blob_data = GetBlobDataFromUrl(iter->blob_url()); | 196 BlobData* blob_data = GetBlobDataFromUrl(iter->url()); |
| 187 DCHECK(blob_data); | 197 DCHECK(blob_data); |
| 188 if (!blob_data) { | 198 if (!blob_data) { |
| 189 // TODO(jianli): We should probably fail uploading the data | 199 // TODO(jianli): We should probably fail uploading the data |
| 190 iter++; | 200 iter++; |
| 191 continue; | 201 continue; |
| 192 } | 202 } |
| 193 | 203 |
| 194 // Remove this element. | 204 // Remove this element. |
| 195 iter = uploads->erase(iter); | 205 iter = uploads->erase(iter); |
| 196 | 206 |
| (...skipping 24 matching lines...) Expand all Loading... |
| 221 &item.data.at(0) + static_cast<int>(item.offset), | 231 &item.data.at(0) + static_cast<int>(item.offset), |
| 222 static_cast<int>(item.length)); | 232 static_cast<int>(item.length)); |
| 223 break; | 233 break; |
| 224 case BlobData::TYPE_FILE: | 234 case BlobData::TYPE_FILE: |
| 225 iter->SetToFilePathRange( | 235 iter->SetToFilePathRange( |
| 226 item.file_path, | 236 item.file_path, |
| 227 item.offset, | 237 item.offset, |
| 228 item.length, | 238 item.length, |
| 229 item.expected_modification_time); | 239 item.expected_modification_time); |
| 230 break; | 240 break; |
| 241 case BlobData::TYPE_FILE_FILESYSTEM: |
| 242 iter->SetToFileSystemURLRange( |
| 243 item.url, |
| 244 item.offset, |
| 245 item.length, |
| 246 item.expected_modification_time); |
| 247 break; |
| 231 default: | 248 default: |
| 232 NOTREACHED(); | 249 NOTREACHED(); |
| 233 break; | 250 break; |
| 234 } | 251 } |
| 235 } | 252 } |
| 236 } | 253 } |
| 237 } | 254 } |
| 238 | 255 |
| 239 void BlobStorageController::AppendStorageItems( | 256 void BlobStorageController::AppendStorageItems( |
| 240 BlobData* target_blob_data, BlobData* src_blob_data, | 257 BlobData* target_blob_data, BlobData* src_blob_data, |
| (...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 280 target_blob_data->AppendFile(file_path, offset, length, | 297 target_blob_data->AppendFile(file_path, offset, length, |
| 281 expected_modification_time); | 298 expected_modification_time); |
| 282 | 299 |
| 283 // It may be a temporary file that should be deleted when no longer needed. | 300 // It may be a temporary file that should be deleted when no longer needed. |
| 284 scoped_refptr<ShareableFileReference> shareable_file = | 301 scoped_refptr<ShareableFileReference> shareable_file = |
| 285 ShareableFileReference::Get(file_path); | 302 ShareableFileReference::Get(file_path); |
| 286 if (shareable_file) | 303 if (shareable_file) |
| 287 target_blob_data->AttachShareableFileReference(shareable_file); | 304 target_blob_data->AttachShareableFileReference(shareable_file); |
| 288 } | 305 } |
| 289 | 306 |
| 307 void BlobStorageController::AppendFileSystemFileItem( |
| 308 BlobData* target_blob_data, |
| 309 const GURL& url, uint64 offset, uint64 length, |
| 310 const base::Time& expected_modification_time) { |
| 311 target_blob_data->AppendFileSystemFile(url, offset, length, |
| 312 expected_modification_time); |
| 313 } |
| 314 |
| 290 void BlobStorageController::IncrementBlobDataUsage(BlobData* blob_data) { | 315 void BlobStorageController::IncrementBlobDataUsage(BlobData* blob_data) { |
| 291 blob_data_usage_count_[blob_data] += 1; | 316 blob_data_usage_count_[blob_data] += 1; |
| 292 } | 317 } |
| 293 | 318 |
| 294 bool BlobStorageController::DecrementBlobDataUsage(BlobData* blob_data) { | 319 bool BlobStorageController::DecrementBlobDataUsage(BlobData* blob_data) { |
| 295 BlobDataUsageMap::iterator found = blob_data_usage_count_.find(blob_data); | 320 BlobDataUsageMap::iterator found = blob_data_usage_count_.find(blob_data); |
| 296 DCHECK(found != blob_data_usage_count_.end()); | 321 DCHECK(found != blob_data_usage_count_.end()); |
| 297 if (--(found->second)) | 322 if (--(found->second)) |
| 298 return false; // Still in use | 323 return false; // Still in use |
| 299 blob_data_usage_count_.erase(found); | 324 blob_data_usage_count_.erase(found); |
| 300 return true; | 325 return true; |
| 301 } | 326 } |
| 302 | 327 |
| 303 } // namespace webkit_blob | 328 } // namespace webkit_blob |
| OLD | NEW |