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 = 1024 * 1024 * 1024; // 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::StartBuildingBlob(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 RemoveBlob(url); | |
88 } | 102 } |
89 | 103 |
90 void BlobStorageController::RegisterBlobUrlFrom( | 104 void BlobStorageController::FinishBuildingBlob( |
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); | |
114 } | |
115 | |
116 void BlobStorageController::AddFinishedBlob(const GURL& url, | |
117 const BlobData* data) { | |
118 StartBuildingBlob(url); | |
119 for (std::vector<BlobData::Item>::const_iterator iter = | |
120 data->items().begin(); | |
121 iter != data->items().end(); ++iter) { | |
122 AppendBlobDataItem(url, *iter); | |
123 } | |
124 FinishBuildingBlob(url, data->content_type()); | |
125 } | |
126 | |
127 void BlobStorageController::CloneBlob( | |
91 const GURL& url, const GURL& src_url) { | 128 const GURL& url, const GURL& src_url) { |
92 DCHECK(url.SchemeIs("blob")); | 129 DCHECK(url.SchemeIs("blob")); |
93 DCHECK(!BlobUrlHasRef(url)); | 130 DCHECK(!BlobUrlHasRef(url)); |
94 | 131 |
95 BlobData* blob_data = GetBlobDataFromUrl(src_url); | 132 BlobData* blob_data = GetBlobDataFromUrl(src_url); |
96 DCHECK(blob_data); | 133 DCHECK(blob_data); |
97 if (!blob_data) | 134 if (!blob_data) |
98 return; | 135 return; |
99 | 136 |
100 blob_map_[url.spec()] = blob_data; | 137 blob_map_[url.spec()] = blob_data; |
138 IncrementBlobDataUsage(blob_data); | |
101 } | 139 } |
102 | 140 |
103 void BlobStorageController::UnregisterBlobUrl(const GURL& url) { | 141 void BlobStorageController::RemoveBlob(const GURL& url) { |
104 blob_map_.erase(url.spec()); | 142 DCHECK(url.SchemeIs("blob")); |
143 DCHECK(!BlobUrlHasRef(url)); | |
144 | |
145 if (!RemoveFromMapHelper(&unfinalized_blob_map_, url)) | |
146 RemoveFromMapHelper(&blob_map_, url); | |
105 } | 147 } |
106 | 148 |
149 bool BlobStorageController::RemoveFromMapHelper( | |
150 BlobMap* map, const GURL& url) { | |
151 BlobMap::iterator found = map->find(url.spec()); | |
152 if (found == map->end()) | |
153 return false; | |
154 if (DecrementBlobDataUsage(found->second)) | |
155 memory_usage_ -= found->second->GetMemoryUsage(); | |
156 map->erase(found); | |
157 return true; | |
158 } | |
159 | |
160 | |
107 BlobData* BlobStorageController::GetBlobDataFromUrl(const GURL& url) { | 161 BlobData* BlobStorageController::GetBlobDataFromUrl(const GURL& url) { |
108 BlobMap::iterator found = blob_map_.find( | 162 BlobMap::iterator found = blob_map_.find( |
109 BlobUrlHasRef(url) ? ClearBlobUrlRef(url).spec() : url.spec()); | 163 BlobUrlHasRef(url) ? ClearBlobUrlRef(url).spec() : url.spec()); |
110 return (found != blob_map_.end()) ? found->second : NULL; | 164 return (found != blob_map_.end()) ? found->second : NULL; |
111 } | 165 } |
112 | 166 |
113 void BlobStorageController::ResolveBlobReferencesInUploadData( | 167 void BlobStorageController::ResolveBlobReferencesInUploadData( |
114 net::UploadData* upload_data) { | 168 net::UploadData* upload_data) { |
115 DCHECK(upload_data); | 169 DCHECK(upload_data); |
116 | 170 |
117 std::vector<net::UploadData::Element>* uploads = upload_data->elements(); | 171 std::vector<net::UploadData::Element>* uploads = upload_data->elements(); |
118 std::vector<net::UploadData::Element>::iterator iter; | 172 std::vector<net::UploadData::Element>::iterator iter; |
119 for (iter = uploads->begin(); iter != uploads->end();) { | 173 for (iter = uploads->begin(); iter != uploads->end();) { |
120 if (iter->type() != net::UploadData::TYPE_BLOB) { | 174 if (iter->type() != net::UploadData::TYPE_BLOB) { |
121 iter++; | 175 iter++; |
122 continue; | 176 continue; |
123 } | 177 } |
124 | 178 |
125 // Find the referred blob data. | 179 // Find the referred blob data. |
126 webkit_blob::BlobData* blob_data = GetBlobDataFromUrl(iter->blob_url()); | 180 BlobData* blob_data = GetBlobDataFromUrl(iter->blob_url()); |
127 DCHECK(blob_data); | 181 DCHECK(blob_data); |
128 if (!blob_data) { | 182 if (!blob_data) { |
129 // TODO(jianli): We should probably fail uploading the data | 183 // TODO(jianli): We should probably fail uploading the data |
130 iter++; | 184 iter++; |
131 continue; | 185 continue; |
132 } | 186 } |
133 | 187 |
134 // Remove this element. | 188 // Remove this element. |
135 iter = uploads->erase(iter); | 189 iter = uploads->erase(iter); |
136 | 190 |
137 // If there is no element in the referred blob data, continue the loop. | 191 // 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 | 192 // Note that we should not increase iter since it already points to the one |
139 // after the removed element. | 193 // after the removed element. |
140 if (blob_data->items().empty()) | 194 if (blob_data->items().empty()) |
141 continue; | 195 continue; |
142 | 196 |
143 // Insert the elements in the referred blob data. | 197 // Insert the elements in the referred blob data. |
144 // Note that we traverse from the bottom so that the elements can be | 198 // Note that we traverse from the bottom so that the elements can be |
145 // inserted in the original order. | 199 // inserted in the original order. |
146 for (size_t i = blob_data->items().size(); i > 0; --i) { | 200 for (size_t i = blob_data->items().size(); i > 0; --i) { |
147 iter = uploads->insert(iter, net::UploadData::Element()); | 201 iter = uploads->insert(iter, net::UploadData::Element()); |
148 | 202 |
149 const webkit_blob::BlobData::Item& item = blob_data->items().at(i - 1); | 203 const BlobData::Item& item = blob_data->items().at(i - 1); |
150 switch (item.type()) { | 204 switch (item.type) { |
151 case webkit_blob::BlobData::TYPE_DATA: | 205 case BlobData::TYPE_DATA: |
152 // TODO(jianli): Figure out how to avoid copying the data. | 206 // TODO(jianli): Figure out how to avoid copying the data. |
153 iter->SetToBytes( | 207 iter->SetToBytes( |
154 &item.data().at(0) + static_cast<int>(item.offset()), | 208 &item.data.at(0) + static_cast<int>(item.offset), |
155 static_cast<int>(item.length())); | 209 static_cast<int>(item.length)); |
156 break; | 210 break; |
157 case webkit_blob::BlobData::TYPE_FILE: | 211 case BlobData::TYPE_FILE: |
158 // TODO(michaeln): Ensure that any temp files survive till the | 212 // TODO(michaeln): Ensure that any temp files survive till the |
159 // net::URLRequest is done with the upload. | 213 // net::URLRequest is done with the upload. |
160 iter->SetToFilePathRange( | 214 iter->SetToFilePathRange( |
161 item.file_path(), | 215 item.file_path, |
162 item.offset(), | 216 item.offset, |
163 item.length(), | 217 item.length, |
164 item.expected_modification_time()); | 218 item.expected_modification_time); |
165 break; | 219 break; |
166 default: | 220 default: |
167 NOTREACHED(); | 221 NOTREACHED(); |
168 break; | 222 break; |
169 } | 223 } |
170 } | 224 } |
171 } | 225 } |
172 } | 226 } |
173 | 227 |
174 void BlobStorageController::AppendStorageItems( | 228 void BlobStorageController::AppendStorageItems( |
175 BlobData* target_blob_data, BlobData* src_blob_data, | 229 BlobData* target_blob_data, BlobData* src_blob_data, |
176 uint64 offset, uint64 length) { | 230 uint64 offset, uint64 length) { |
177 DCHECK(target_blob_data && src_blob_data && | 231 DCHECK(target_blob_data && src_blob_data && |
178 length != static_cast<uint64>(-1)); | 232 length != static_cast<uint64>(-1)); |
179 | 233 |
180 std::vector<BlobData::Item>::const_iterator iter = | 234 std::vector<BlobData::Item>::const_iterator iter = |
181 src_blob_data->items().begin(); | 235 src_blob_data->items().begin(); |
182 if (offset) { | 236 if (offset) { |
183 for (; iter != src_blob_data->items().end(); ++iter) { | 237 for (; iter != src_blob_data->items().end(); ++iter) { |
184 if (offset >= iter->length()) | 238 if (offset >= iter->length) |
185 offset -= iter->length(); | 239 offset -= iter->length; |
186 else | 240 else |
187 break; | 241 break; |
188 } | 242 } |
189 } | 243 } |
190 | 244 |
191 for (; iter != src_blob_data->items().end() && length > 0; ++iter) { | 245 for (; iter != src_blob_data->items().end() && length > 0; ++iter) { |
192 uint64 current_length = iter->length() - offset; | 246 uint64 current_length = iter->length - offset; |
193 uint64 new_length = current_length > length ? length : current_length; | 247 uint64 new_length = current_length > length ? length : current_length; |
194 if (iter->type() == BlobData::TYPE_DATA) { | 248 if (iter->type == BlobData::TYPE_DATA) { |
195 target_blob_data->AppendData(iter->data(), | 249 target_blob_data->AppendData(iter->data, |
196 static_cast<uint32>(iter->offset() + offset), | 250 static_cast<uint32>(iter->offset + offset), |
197 static_cast<uint32>(new_length)); | 251 static_cast<uint32>(new_length)); |
198 } else { | 252 } else { |
199 DCHECK(iter->type() == BlobData::TYPE_FILE); | 253 DCHECK(iter->type == BlobData::TYPE_FILE); |
200 AppendFileItem(target_blob_data, | 254 AppendFileItem(target_blob_data, |
201 iter->file_path(), | 255 iter->file_path, |
202 iter->offset() + offset, | 256 iter->offset + offset, |
203 new_length, | 257 new_length, |
204 iter->expected_modification_time()); | 258 iter->expected_modification_time); |
205 } | 259 } |
206 length -= new_length; | 260 length -= new_length; |
207 offset = 0; | 261 offset = 0; |
208 } | 262 } |
209 } | 263 } |
210 | 264 |
211 void BlobStorageController::AppendFileItem( | 265 void BlobStorageController::AppendFileItem( |
212 BlobData* target_blob_data, | 266 BlobData* target_blob_data, |
213 const FilePath& file_path, uint64 offset, uint64 length, | 267 const FilePath& file_path, uint64 offset, uint64 length, |
214 const base::Time& expected_modification_time) { | 268 const base::Time& expected_modification_time) { |
215 target_blob_data->AppendFile(file_path, offset, length, | 269 target_blob_data->AppendFile(file_path, offset, length, |
216 expected_modification_time); | 270 expected_modification_time); |
217 | 271 |
218 // It may be a temporary file that should be deleted when no longer needed. | 272 // It may be a temporary file that should be deleted when no longer needed. |
219 scoped_refptr<DeletableFileReference> deletable_file = | 273 scoped_refptr<DeletableFileReference> deletable_file = |
220 DeletableFileReference::Get(file_path); | 274 DeletableFileReference::Get(file_path); |
221 if (deletable_file) | 275 if (deletable_file) |
222 target_blob_data->AttachDeletableFileReference(deletable_file); | 276 target_blob_data->AttachDeletableFileReference(deletable_file); |
223 } | 277 } |
224 | 278 |
279 void BlobStorageController::IncrementBlobDataUsage(BlobData* blob_data) { | |
280 blob_data_usage_count_[blob_data] += 1; | |
281 } | |
282 | |
283 bool BlobStorageController::DecrementBlobDataUsage(BlobData* blob_data) { | |
284 BlobDataUsageMap::iterator found = blob_data_usage_count_.find(blob_data); | |
285 DCHECK(found != blob_data_usage_count_.end()); | |
jianli
2011/09/30 00:59:34
More sanity check like:
DCHECK(found->second > 0
| |
286 if (--(found->second)) | |
287 return false; // Still in use | |
288 blob_data_usage_count_.erase(found); | |
289 return true; | |
290 } | |
291 | |
225 } // namespace webkit_blob | 292 } // namespace webkit_blob |
OLD | NEW |