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