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 | |
| 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 unfinalized_blob_map_[url.spec()] = new BlobData(); | |
| 47 } | |
| 44 | 48 |
| 45 scoped_refptr<BlobData> target_blob_data(new BlobData()); | 49 void BlobStorageController::AppendBlobDataItem( |
| 46 target_blob_data->set_content_type(blob_data->content_type()); | 50 const GURL& url, const webkit_blob::BlobData::Item& item) { |
| 47 target_blob_data->set_content_disposition(blob_data->content_disposition()); | 51 DCHECK(url.SchemeIs("blob")); |
| 52 DCHECK(!BlobUrlHasRef(url)); | |
| 53 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
| |
| 54 if (found == unfinalized_blob_map_.end()) | |
| 55 return; | |
| 56 BlobData* target_blob_data = found->second; | |
| 57 DCHECK(target_blob_data); | |
| 58 | |
| 59 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
| |
| 48 | 60 |
| 49 // The blob data is stored in the "canonical" way. That is, it only contains a | 61 // The blob data is stored in the "canonical" way. That is, it only contains a |
| 50 // list of Data and File items. | 62 // list of Data and File items. |
| 51 // 1) The Data item is denoted by the raw data and the range. | 63 // 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 | 64 // 2) The File item is denoted by the file path, the range and the expected |
| 53 // modification time. | 65 // modification time. |
| 54 // All the Blob items in the passing blob data are resolved and expanded into | 66 // All the Blob items in the passing blob data are resolved and expanded into |
| 55 // a set of Data and File items. | 67 // a set of Data and File items. |
| 56 | 68 |
| 57 for (std::vector<BlobData::Item>::const_iterator iter = | 69 switch (item.type()) { |
| 58 blob_data->items().begin(); | 70 case BlobData::TYPE_DATA: |
| 59 iter != blob_data->items().end(); ++iter) { | 71 // WebBlobData does not allow partial data. |
| 60 switch (iter->type()) { | 72 DCHECK(!(item.offset()) && item.length() == item.data().size()); |
| 61 case BlobData::TYPE_DATA: { | 73 target_blob_data->AppendData(item.data()); |
| 62 // WebBlobData does not allow partial data. | 74 break; |
| 63 DCHECK(!(iter->offset()) && iter->length() == iter->data().size()); | 75 case BlobData::TYPE_FILE: |
| 64 target_blob_data->AppendData(iter->data()); | 76 AppendFileItem(target_blob_data, |
| 65 break; | 77 item.file_path(), |
| 66 } | 78 item.offset(), |
| 67 case BlobData::TYPE_FILE: | 79 item.length(), |
| 68 AppendFileItem(target_blob_data, | 80 item.expected_modification_time()); |
| 69 iter->file_path(), | 81 break; |
| 70 iter->offset(), | 82 case BlobData::TYPE_BLOB: |
| 71 iter->length(), | 83 BlobData* src_blob_data = GetBlobDataFromUrl(item.blob_url()); |
| 72 iter->expected_modification_time()); | 84 DCHECK(src_blob_data); |
| 73 break; | 85 if (src_blob_data) |
| 74 case BlobData::TYPE_BLOB: { | 86 AppendStorageItems(target_blob_data, |
| 75 BlobData* src_blob_data = GetBlobDataFromUrl(iter->blob_url()); | 87 src_blob_data, |
| 76 DCHECK(src_blob_data); | 88 item.offset(), |
| 77 if (src_blob_data) | 89 item.length()); |
| 78 AppendStorageItems(target_blob_data.get(), | 90 break; |
| 79 src_blob_data, | |
| 80 iter->offset(), | |
| 81 iter->length()); | |
| 82 break; | |
| 83 } | |
| 84 } | |
| 85 } | 91 } |
| 86 | 92 |
| 87 blob_map_[url.spec()] = target_blob_data; | 93 memory_usage_ += target_blob_data->GetMemoryUsage(); |
| 94 | |
| 95 // If we're using too much memory, drop this blob. | |
| 96 // TODO(michaeln): Blob memory storage does not yet spill over to disk, | |
| 97 // until it does, we'll prevent memory usage over a max amount. | |
| 98 if (memory_usage_ > kMaxMemoryUsage) | |
| 99 UnregisterBlobUrl(url); | |
| 100 } | |
| 101 | |
| 102 void BlobStorageController::FinalizeBlob( | |
| 103 const GURL& url, const std::string& content_type) { | |
| 104 DCHECK(url.SchemeIs("blob")); | |
| 105 DCHECK(!BlobUrlHasRef(url)); | |
| 106 BlobMap::iterator found = unfinalized_blob_map_.find(url.spec()); | |
| 107 if (found == unfinalized_blob_map_.end()) | |
| 108 return; | |
| 109 found->second->set_content_type(content_type); | |
| 110 blob_map_[url.spec()] = found->second; | |
| 111 unfinalized_blob_map_.erase(found); | |
| 88 } | 112 } |
| 89 | 113 |
| 90 void BlobStorageController::RegisterBlobUrlFrom( | 114 void BlobStorageController::RegisterBlobUrlFrom( |
| 91 const GURL& url, const GURL& src_url) { | 115 const GURL& url, const GURL& src_url) { |
| 92 DCHECK(url.SchemeIs("blob")); | 116 DCHECK(url.SchemeIs("blob")); |
| 93 DCHECK(!BlobUrlHasRef(url)); | 117 DCHECK(!BlobUrlHasRef(url)); |
| 94 | 118 |
| 95 BlobData* blob_data = GetBlobDataFromUrl(src_url); | 119 BlobData* blob_data = GetBlobDataFromUrl(src_url); |
| 96 DCHECK(blob_data); | 120 DCHECK(blob_data); |
| 97 if (!blob_data) | 121 if (!blob_data) |
| 98 return; | 122 return; |
| 99 | 123 |
| 100 blob_map_[url.spec()] = blob_data; | 124 blob_map_[url.spec()] = blob_data; |
|
michaeln
2011/09/24 01:15:24
Bah... this throws off the memory_usage_ tracking.
| |
| 101 } | 125 } |
| 102 | 126 |
| 103 void BlobStorageController::UnregisterBlobUrl(const GURL& url) { | 127 void BlobStorageController::UnregisterBlobUrl(const GURL& url) { |
| 104 blob_map_.erase(url.spec()); | 128 DCHECK(url.SchemeIs("blob")); |
| 129 DCHECK(!BlobUrlHasRef(url)); | |
| 130 | |
| 131 BlobMap::iterator found = unfinalized_blob_map_.find(url.spec()); | |
| 132 if (found != unfinalized_blob_map_.end()) { | |
| 133 memory_usage_ -= found->second->GetMemoryUsage(); | |
| 134 unfinalized_blob_map_.erase(found); | |
| 135 return; | |
| 136 } | |
| 137 | |
| 138 found = blob_map_.find(url.spec()); | |
| 139 if (found != blob_map_.end()) { | |
| 140 memory_usage_ -= found->second->GetMemoryUsage(); | |
| 141 blob_map_.erase(found); | |
| 142 } | |
| 105 } | 143 } |
| 106 | 144 |
| 107 BlobData* BlobStorageController::GetBlobDataFromUrl(const GURL& url) { | 145 BlobData* BlobStorageController::GetBlobDataFromUrl(const GURL& url) { |
| 108 BlobMap::iterator found = blob_map_.find( | 146 BlobMap::iterator found = blob_map_.find( |
| 109 BlobUrlHasRef(url) ? ClearBlobUrlRef(url).spec() : url.spec()); | 147 BlobUrlHasRef(url) ? ClearBlobUrlRef(url).spec() : url.spec()); |
| 110 return (found != blob_map_.end()) ? found->second : NULL; | 148 return (found != blob_map_.end()) ? found->second : NULL; |
| 111 } | 149 } |
| 112 | 150 |
| 113 void BlobStorageController::ResolveBlobReferencesInUploadData( | 151 void BlobStorageController::ResolveBlobReferencesInUploadData( |
| 114 net::UploadData* upload_data) { | 152 net::UploadData* upload_data) { |
| (...skipping 101 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 216 expected_modification_time); | 254 expected_modification_time); |
| 217 | 255 |
| 218 // It may be a temporary file that should be deleted when no longer needed. | 256 // It may be a temporary file that should be deleted when no longer needed. |
| 219 scoped_refptr<DeletableFileReference> deletable_file = | 257 scoped_refptr<DeletableFileReference> deletable_file = |
| 220 DeletableFileReference::Get(file_path); | 258 DeletableFileReference::Get(file_path); |
| 221 if (deletable_file) | 259 if (deletable_file) |
| 222 target_blob_data->AttachDeletableFileReference(deletable_file); | 260 target_blob_data->AttachDeletableFileReference(deletable_file); |
| 223 } | 261 } |
| 224 | 262 |
| 225 } // namespace webkit_blob | 263 } // namespace webkit_blob |
| OLD | NEW |