OLD | NEW |
---|---|
1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2013 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 "storage/browser/blob/blob_storage_context.h" | 5 #include "storage/browser/blob/blob_storage_context.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/location.h" | 8 #include "base/location.h" |
9 #include "base/logging.h" | 9 #include "base/logging.h" |
10 #include "base/message_loop/message_loop_proxy.h" | 10 #include "base/message_loop/message_loop_proxy.h" |
11 #include "base/metrics/histogram.h" | 11 #include "base/metrics/histogram.h" |
12 #include "storage/browser/blob/blob_data_handle.h" | 12 #include "base/stl_util.h" |
13 #include "storage/common/blob/blob_data.h" | 13 #include "storage/browser/blob/blob_data_builder.h" |
14 #include "url/gurl.h" | 14 #include "url/gurl.h" |
15 | 15 |
16 namespace storage { | 16 namespace storage { |
17 | 17 |
18 namespace { | 18 namespace { |
19 | 19 |
20 // We can't use GURL directly for these hash fragment manipulations | 20 // We can't use GURL directly for these hash fragment manipulations |
21 // since it doesn't have specific knowlege of the BlobURL format. GURL | 21 // since it doesn't have specific knowlege of the BlobURL format. GURL |
22 // treats BlobURLs as if they were PathURLs which don't support hash | 22 // treats BlobURLs as if they were PathURLs which don't support hash |
23 // fragments. | 23 // fragments. |
(...skipping 12 matching lines...) Expand all Loading... | |
36 // TODO(michaeln): use base::SysInfo::AmountOfPhysicalMemoryMB() in some | 36 // TODO(michaeln): use base::SysInfo::AmountOfPhysicalMemoryMB() in some |
37 // way to come up with a better limit. | 37 // way to come up with a better limit. |
38 static const int64 kMaxMemoryUsage = 500 * 1024 * 1024; // Half a gig. | 38 static const int64 kMaxMemoryUsage = 500 * 1024 * 1024; // Half a gig. |
39 | 39 |
40 } // namespace | 40 } // namespace |
41 | 41 |
42 BlobStorageContext::BlobMapEntry::BlobMapEntry() | 42 BlobStorageContext::BlobMapEntry::BlobMapEntry() |
43 : refcount(0), flags(0) { | 43 : refcount(0), flags(0) { |
44 } | 44 } |
45 | 45 |
46 BlobStorageContext::BlobMapEntry::BlobMapEntry( | 46 BlobStorageContext::BlobMapEntry::BlobMapEntry(int refcount, |
47 int refcount, int flags, BlobData* data) | 47 BlobDataBuilder* data) |
48 : refcount(refcount), flags(flags), data(data) { | 48 : refcount(refcount), flags(0), data_builder(data) { |
49 } | 49 } |
50 | 50 |
51 BlobStorageContext::BlobMapEntry::~BlobMapEntry() { | 51 BlobStorageContext::BlobMapEntry::~BlobMapEntry() { |
52 } | 52 } |
53 | 53 |
54 bool BlobStorageContext::BlobMapEntry::IsBeingBuilt() { | |
55 return data_builder; | |
56 } | |
57 | |
54 BlobStorageContext::BlobStorageContext() | 58 BlobStorageContext::BlobStorageContext() |
55 : memory_usage_(0) { | 59 : memory_usage_(0) { |
56 } | 60 } |
57 | 61 |
58 BlobStorageContext::~BlobStorageContext() { | 62 BlobStorageContext::~BlobStorageContext() { |
63 STLDeleteContainerPairSecondPointers(blob_map_.begin(), blob_map_.end()); | |
59 } | 64 } |
60 | 65 |
61 scoped_ptr<BlobDataHandle> BlobStorageContext::GetBlobDataFromUUID( | 66 scoped_ptr<BlobDataHandle> BlobStorageContext::GetBlobDataFromUUID( |
62 const std::string& uuid) { | 67 const std::string& uuid) { |
63 scoped_ptr<BlobDataHandle> result; | 68 scoped_ptr<BlobDataHandle> result; |
64 BlobMap::iterator found = blob_map_.find(uuid); | 69 BlobMap::iterator found = blob_map_.find(uuid); |
65 if (found == blob_map_.end()) | 70 if (found == blob_map_.end()) |
66 return result.Pass(); | 71 return result.Pass(); |
67 if (found->second.flags & EXCEEDED_MEMORY) | 72 auto* entry = found->second; |
73 if (entry->flags & EXCEEDED_MEMORY) | |
68 return result.Pass(); | 74 return result.Pass(); |
69 DCHECK(!(found->second.flags & BEING_BUILT)); | 75 DCHECK(!entry->IsBeingBuilt()); |
70 result.reset(new BlobDataHandle( | 76 result.reset( |
71 found->second.data.get(), this, base::MessageLoopProxy::current().get())); | 77 new BlobDataHandle(uuid, this, base::MessageLoopProxy::current().get())); |
72 return result.Pass(); | 78 return result.Pass(); |
73 } | 79 } |
74 | 80 |
75 scoped_ptr<BlobDataHandle> BlobStorageContext::GetBlobDataFromPublicURL( | 81 scoped_ptr<BlobDataHandle> BlobStorageContext::GetBlobDataFromPublicURL( |
76 const GURL& url) { | 82 const GURL& url) { |
77 BlobURLMap::iterator found = public_blob_urls_.find( | 83 BlobURLMap::iterator found = public_blob_urls_.find( |
78 BlobUrlHasRef(url) ? ClearBlobUrlRef(url) : url); | 84 BlobUrlHasRef(url) ? ClearBlobUrlRef(url) : url); |
79 if (found == public_blob_urls_.end()) | 85 if (found == public_blob_urls_.end()) |
80 return scoped_ptr<BlobDataHandle>(); | 86 return scoped_ptr<BlobDataHandle>(); |
81 return GetBlobDataFromUUID(found->second); | 87 return GetBlobDataFromUUID(found->second); |
82 } | 88 } |
83 | 89 |
84 scoped_ptr<BlobDataHandle> BlobStorageContext::AddFinishedBlob( | 90 scoped_ptr<BlobDataHandle> BlobStorageContext::AddFinishedBlob( |
85 const BlobData* data) { | 91 const BlobDataBuilder& data) { |
86 StartBuildingBlob(data->uuid()); | 92 StartBuildingBlob(data.uuid_); |
87 for (std::vector<BlobData::Item>::const_iterator iter = | 93 for (const auto& blob_item : data.items_) |
88 data->items().begin(); | 94 AppendBlobDataItem(data.uuid_, *(blob_item->item_)); |
89 iter != data->items().end(); ++iter) { | 95 FinishBuildingBlob(data.uuid_, data.content_type_); |
90 AppendBlobDataItem(data->uuid(), *iter); | 96 scoped_ptr<BlobDataHandle> handle = GetBlobDataFromUUID(data.uuid_); |
91 } | 97 DecrementBlobRefCount(data.uuid_); |
92 FinishBuildingBlob(data->uuid(), data->content_type()); | |
93 scoped_ptr<BlobDataHandle> handle = GetBlobDataFromUUID(data->uuid()); | |
94 DecrementBlobRefCount(data->uuid()); | |
95 return handle.Pass(); | 98 return handle.Pass(); |
96 } | 99 } |
97 | 100 |
98 bool BlobStorageContext::RegisterPublicBlobURL( | 101 bool BlobStorageContext::RegisterPublicBlobURL( |
99 const GURL& blob_url, const std::string& uuid) { | 102 const GURL& blob_url, const std::string& uuid) { |
100 DCHECK(!BlobUrlHasRef(blob_url)); | 103 DCHECK(!BlobUrlHasRef(blob_url)); |
101 DCHECK(IsInUse(uuid)); | 104 DCHECK(IsInUse(uuid)); |
102 DCHECK(!IsUrlRegistered(blob_url)); | 105 DCHECK(!IsUrlRegistered(blob_url)); |
103 if (!IsInUse(uuid) || IsUrlRegistered(blob_url)) | 106 if (!IsInUse(uuid) || IsUrlRegistered(blob_url)) |
104 return false; | 107 return false; |
105 IncrementBlobRefCount(uuid); | 108 IncrementBlobRefCount(uuid); |
106 public_blob_urls_[blob_url] = uuid; | 109 public_blob_urls_[blob_url] = uuid; |
107 return true; | 110 return true; |
108 } | 111 } |
109 | 112 |
110 void BlobStorageContext::RevokePublicBlobURL(const GURL& blob_url) { | 113 void BlobStorageContext::RevokePublicBlobURL(const GURL& blob_url) { |
111 DCHECK(!BlobUrlHasRef(blob_url)); | 114 DCHECK(!BlobUrlHasRef(blob_url)); |
112 if (!IsUrlRegistered(blob_url)) | 115 if (!IsUrlRegistered(blob_url)) |
113 return; | 116 return; |
114 DecrementBlobRefCount(public_blob_urls_[blob_url]); | 117 DecrementBlobRefCount(public_blob_urls_[blob_url]); |
115 public_blob_urls_.erase(blob_url); | 118 public_blob_urls_.erase(blob_url); |
116 } | 119 } |
117 | 120 |
121 scoped_ptr<BlobDataSnapshot> BlobStorageContext::CreateSnapshot( | |
122 const std::string& uuid) { | |
123 scoped_ptr<BlobDataSnapshot> result; | |
124 auto found = blob_map_.find(uuid); | |
125 DCHECK(found != blob_map_.end()) | |
126 << "Blob should be in map, as the handle is still around"; | |
127 BlobMapEntry* entry = found->second; | |
128 DCHECK(!entry->IsBeingBuilt()); | |
129 result.reset(new BlobDataSnapshot(*entry->data)); | |
130 return result.Pass(); | |
131 } | |
132 | |
118 void BlobStorageContext::StartBuildingBlob(const std::string& uuid) { | 133 void BlobStorageContext::StartBuildingBlob(const std::string& uuid) { |
119 DCHECK(!IsInUse(uuid) && !uuid.empty()); | 134 DCHECK(!IsInUse(uuid) && !uuid.empty()); |
120 blob_map_[uuid] = BlobMapEntry(1, BEING_BUILT, new BlobData(uuid)); | 135 blob_map_[uuid] = new BlobMapEntry(1, new BlobDataBuilder(uuid)); |
121 } | 136 } |
122 | 137 |
123 void BlobStorageContext::AppendBlobDataItem( | 138 void BlobStorageContext::AppendBlobDataItem(const std::string& uuid, |
124 const std::string& uuid, const BlobData::Item& item) { | 139 const DataElement& item) { |
125 DCHECK(IsBeingBuilt(uuid)); | 140 DCHECK(IsBeingBuilt(uuid)); |
126 BlobMap::iterator found = blob_map_.find(uuid); | 141 BlobMap::iterator found = blob_map_.find(uuid); |
127 if (found == blob_map_.end()) | 142 if (found == blob_map_.end()) |
128 return; | 143 return; |
129 if (found->second.flags & EXCEEDED_MEMORY) | 144 BlobMapEntry* entry = found->second; |
145 if (entry->flags & EXCEEDED_MEMORY) | |
130 return; | 146 return; |
131 BlobData* target_blob_data = found->second.data.get(); | 147 BlobDataBuilder* target_blob_data = entry->data_builder.get(); |
132 DCHECK(target_blob_data); | 148 DCHECK(target_blob_data); |
133 | 149 |
134 bool exceeded_memory = false; | 150 bool exceeded_memory = false; |
135 | 151 |
136 // The blob data is stored in the canonical way which only contains a | 152 // The blob data is stored in the canonical way which only contains a |
137 // list of Data, File, and FileSystem items. Aggregated TYPE_BLOB items | 153 // list of Data, File, and FileSystem items. Aggregated TYPE_BLOB items |
138 // are expanded into the primitive constituent types. | 154 // are expanded into the primitive constituent types. |
139 // 1) The Data item is denoted by the raw data and length. | 155 // 1) The Data item is denoted by the raw data and length. |
140 // 2) The File item is denoted by the file path, the range and the expected | 156 // 2) The File item is denoted by the file path, the range and the expected |
141 // modification time. | 157 // modification time. |
142 // 3) The FileSystem File item is denoted by the FileSystem URL, the range | 158 // 3) The FileSystem File item is denoted by the FileSystem URL, the range |
143 // and the expected modification time. | 159 // and the expected modification time. |
144 // 4) The Blob items are expanded. | 160 // 4) The Blob items are expanded. |
145 // TODO(michaeln): Would be nice to avoid copying Data items when expanding. | 161 // TODO(michaeln): Would be nice to avoid copying Data items when expanding. |
146 | 162 |
147 uint64 length = item.length(); | 163 uint64 length = item.length(); |
148 DCHECK_GT(length, 0u); | 164 DCHECK_GT(length, 0u); |
149 UMA_HISTOGRAM_COUNTS("Storage.Blob.StorageSizeBeforeAppend", | 165 UMA_HISTOGRAM_COUNTS("Storage.Blob.StorageSizeBeforeAppend", |
150 memory_usage_ / 1024); | 166 memory_usage_ / 1024); |
151 switch (item.type()) { | 167 switch (item.type()) { |
152 case BlobData::Item::TYPE_BYTES: | 168 case DataElement::TYPE_BYTES: |
153 UMA_HISTOGRAM_COUNTS("Storage.BlobItemSize.Bytes", length / 1024); | 169 UMA_HISTOGRAM_COUNTS("Storage.BlobItemSize.Bytes", length / 1024); |
154 DCHECK(!item.offset()); | 170 DCHECK(!item.offset()); |
155 exceeded_memory = !AppendBytesItem(target_blob_data, item.bytes(), | 171 exceeded_memory = !AppendBytesItem(target_blob_data, item.bytes(), |
156 static_cast<int64>(length)); | 172 static_cast<int64>(length)); |
157 break; | 173 break; |
158 case BlobData::Item::TYPE_FILE: | 174 case DataElement::TYPE_FILE: |
159 UMA_HISTOGRAM_COUNTS("Storage.BlobItemSize.File", length / 1024); | 175 UMA_HISTOGRAM_COUNTS("Storage.BlobItemSize.File", length / 1024); |
160 AppendFileItem(target_blob_data, item.path(), item.offset(), length, | 176 AppendFileItem(target_blob_data, item.path(), item.offset(), |
161 item.expected_modification_time()); | 177 item.length(), item.expected_modification_time()); |
162 break; | 178 break; |
163 case BlobData::Item::TYPE_FILE_FILESYSTEM: | 179 case DataElement::TYPE_FILE_FILESYSTEM: |
164 UMA_HISTOGRAM_COUNTS("Storage.BlobItemSize.FileSystem", length / 1024); | 180 UMA_HISTOGRAM_COUNTS("Storage.BlobItemSize.FileSystem", length / 1024); |
165 AppendFileSystemFileItem(target_blob_data, item.filesystem_url(), | 181 AppendFileSystemFileItem(target_blob_data, item.filesystem_url(), |
166 item.offset(), length, | 182 item.offset(), item.length(), |
167 item.expected_modification_time()); | 183 item.expected_modification_time()); |
168 break; | 184 break; |
169 case BlobData::Item::TYPE_BLOB: { | 185 case DataElement::TYPE_BLOB: { |
170 UMA_HISTOGRAM_COUNTS("Storage.BlobItemSize.Blob", length / 1024); | 186 UMA_HISTOGRAM_COUNTS("Storage.BlobItemSize.Blob", length / 1024); |
187 // We grab the handle to ensure it stays around while we copy it. | |
171 scoped_ptr<BlobDataHandle> src = GetBlobDataFromUUID(item.blob_uuid()); | 188 scoped_ptr<BlobDataHandle> src = GetBlobDataFromUUID(item.blob_uuid()); |
172 if (src) | 189 if (src) { |
173 exceeded_memory = !ExpandStorageItems(target_blob_data, | 190 BlobMapEntry* entry = blob_map_.find(item.blob_uuid())->second; |
brucedawson
2015/01/27 19:05:29
This line is confusing, and possibly a bug. The de
dmurph
2015/01/29 17:43:48
Thankfully this isn't a bug, I'll rename the inner
| |
174 src->data(), | 191 DCHECK(entry->data); |
175 item.offset(), | 192 exceeded_memory = !ExpandStorageItems(target_blob_data, *entry->data, |
176 item.length()); | 193 item.offset(), item.length()); |
194 } | |
177 break; | 195 break; |
178 } | 196 } |
179 default: | 197 default: |
180 NOTREACHED(); | 198 NOTREACHED(); |
181 break; | 199 break; |
182 } | 200 } |
183 UMA_HISTOGRAM_COUNTS("Storage.Blob.StorageSizeAfterAppend", | 201 UMA_HISTOGRAM_COUNTS("Storage.Blob.StorageSizeAfterAppend", |
184 memory_usage_ / 1024); | 202 memory_usage_ / 1024); |
185 | 203 |
186 // If we're using too much memory, drop this blob's data. | 204 // If we're using too much memory, drop this blob's data. |
187 // TODO(michaeln): Blob memory storage does not yet spill over to disk, | 205 // TODO(michaeln): Blob memory storage does not yet spill over to disk, |
188 // as a stop gap, we'll prevent memory usage over a max amount. | 206 // as a stop gap, we'll prevent memory usage over a max amount. |
189 if (exceeded_memory) { | 207 if (exceeded_memory) { |
190 memory_usage_ -= target_blob_data->GetMemoryUsage(); | 208 memory_usage_ -= target_blob_data->GetMemoryUsage(); |
191 found->second.flags |= EXCEEDED_MEMORY; | 209 entry->flags |= EXCEEDED_MEMORY; |
192 found->second.data = new BlobData(uuid); | 210 entry->data_builder.reset(new BlobDataBuilder(uuid)); |
193 return; | 211 return; |
194 } | 212 } |
195 } | 213 } |
196 | 214 |
197 void BlobStorageContext::FinishBuildingBlob( | 215 void BlobStorageContext::FinishBuildingBlob( |
198 const std::string& uuid, const std::string& content_type) { | 216 const std::string& uuid, const std::string& content_type) { |
199 DCHECK(IsBeingBuilt(uuid)); | 217 DCHECK(IsBeingBuilt(uuid)); |
200 BlobMap::iterator found = blob_map_.find(uuid); | 218 BlobMap::iterator found = blob_map_.find(uuid); |
201 if (found == blob_map_.end()) | 219 if (found == blob_map_.end()) |
202 return; | 220 return; |
203 found->second.data->set_content_type(content_type); | 221 BlobMapEntry* entry = found->second; |
204 found->second.flags &= ~BEING_BUILT; | 222 entry->data_builder->set_content_type(content_type); |
205 UMA_HISTOGRAM_COUNTS("Storage.Blob.ItemCount", | 223 entry->data = entry->data_builder->BuildSnapshot().Pass(); |
206 found->second.data->items().size()); | 224 entry->data_builder.reset(); |
207 UMA_HISTOGRAM_BOOLEAN( | 225 UMA_HISTOGRAM_COUNTS("Storage.Blob.ItemCount", entry->data->items().size()); |
208 "Storage.Blob.ExceededMemory", | 226 UMA_HISTOGRAM_BOOLEAN("Storage.Blob.ExceededMemory", |
209 (found->second.flags & EXCEEDED_MEMORY) == EXCEEDED_MEMORY); | 227 (entry->flags & EXCEEDED_MEMORY) == EXCEEDED_MEMORY); |
210 } | 228 } |
211 | 229 |
212 void BlobStorageContext::CancelBuildingBlob(const std::string& uuid) { | 230 void BlobStorageContext::CancelBuildingBlob(const std::string& uuid) { |
213 DCHECK(IsBeingBuilt(uuid)); | 231 DCHECK(IsBeingBuilt(uuid)); |
214 DecrementBlobRefCount(uuid); | 232 DecrementBlobRefCount(uuid); |
215 } | 233 } |
216 | 234 |
217 void BlobStorageContext::IncrementBlobRefCount(const std::string& uuid) { | 235 void BlobStorageContext::IncrementBlobRefCount(const std::string& uuid) { |
218 BlobMap::iterator found = blob_map_.find(uuid); | 236 BlobMap::iterator found = blob_map_.find(uuid); |
219 if (found == blob_map_.end()) { | 237 if (found == blob_map_.end()) { |
220 DCHECK(false); | 238 DCHECK(false); |
221 return; | 239 return; |
222 } | 240 } |
223 ++(found->second.refcount); | 241 ++(found->second->refcount); |
224 } | 242 } |
225 | 243 |
226 void BlobStorageContext::DecrementBlobRefCount(const std::string& uuid) { | 244 void BlobStorageContext::DecrementBlobRefCount(const std::string& uuid) { |
227 BlobMap::iterator found = blob_map_.find(uuid); | 245 BlobMap::iterator found = blob_map_.find(uuid); |
228 if (found == blob_map_.end()) | 246 if (found == blob_map_.end()) |
229 return; | 247 return; |
230 DCHECK_EQ(found->second.data->uuid(), uuid); | 248 auto* entry = found->second; |
231 if (--(found->second.refcount) == 0) { | 249 if (--(entry->refcount) == 0) { |
232 memory_usage_ -= found->second.data->GetMemoryUsage(); | 250 if (entry->IsBeingBuilt()) { |
251 memory_usage_ -= entry->data_builder->GetMemoryUsage(); | |
252 } else { | |
253 memory_usage_ -= entry->data->GetMemoryUsage(); | |
254 } | |
255 delete entry; | |
233 blob_map_.erase(found); | 256 blob_map_.erase(found); |
234 } | 257 } |
235 } | 258 } |
236 | 259 |
237 bool BlobStorageContext::ExpandStorageItems( | 260 bool BlobStorageContext::ExpandStorageItems( |
238 BlobData* target_blob_data, BlobData* src_blob_data, | 261 BlobDataBuilder* target_blob_data, |
239 uint64 offset, uint64 length) { | 262 const BlobDataSnapshot& src_blob_data, |
240 DCHECK(target_blob_data && src_blob_data && | 263 uint64 offset, |
241 length != static_cast<uint64>(-1)); | 264 uint64 length) { |
265 DCHECK(target_blob_data && length != static_cast<uint64>(-1)); | |
242 | 266 |
243 std::vector<BlobData::Item>::const_iterator iter = | 267 const std::vector<scoped_refptr<BlobDataItem>>& items = src_blob_data.items(); |
244 src_blob_data->items().begin(); | 268 auto iter = items.begin(); |
245 if (offset) { | 269 if (offset) { |
246 for (; iter != src_blob_data->items().end(); ++iter) { | 270 for (; iter != items.end(); ++iter) { |
247 if (offset >= iter->length()) | 271 const BlobDataItem& item = *(iter->get()); |
248 offset -= iter->length(); | 272 if (offset >= item.length()) |
273 offset -= item.length(); | |
249 else | 274 else |
250 break; | 275 break; |
251 } | 276 } |
252 } | 277 } |
253 | 278 |
254 for (; iter != src_blob_data->items().end() && length > 0; ++iter) { | 279 for (; iter != items.end() && length > 0; ++iter) { |
255 uint64 current_length = iter->length() - offset; | 280 const BlobDataItem& item = *(iter->get()); |
281 uint64 current_length = item.length() - offset; | |
256 uint64 new_length = current_length > length ? length : current_length; | 282 uint64 new_length = current_length > length ? length : current_length; |
257 if (iter->type() == BlobData::Item::TYPE_BYTES) { | 283 if (iter->get()->type() == DataElement::TYPE_BYTES) { |
258 if (!AppendBytesItem( | 284 if (!AppendBytesItem( |
259 target_blob_data, | 285 target_blob_data, |
260 iter->bytes() + static_cast<size_t>(iter->offset() + offset), | 286 item.bytes() + static_cast<size_t>(item.offset() + offset), |
261 static_cast<int64>(new_length))) { | 287 static_cast<int64>(new_length))) { |
262 return false; // exceeded memory | 288 return false; // exceeded memory |
263 } | 289 } |
264 } else if (iter->type() == BlobData::Item::TYPE_FILE) { | 290 } else if (item.type() == DataElement::TYPE_FILE) { |
265 AppendFileItem(target_blob_data, | 291 AppendFileItem(target_blob_data, item.path(), item.offset() + offset, |
266 iter->path(), | 292 new_length, item.expected_modification_time()); |
267 iter->offset() + offset, | |
268 new_length, | |
269 iter->expected_modification_time()); | |
270 } else { | 293 } else { |
271 DCHECK(iter->type() == BlobData::Item::TYPE_FILE_FILESYSTEM); | 294 DCHECK(item.type() == DataElement::TYPE_FILE_FILESYSTEM); |
272 AppendFileSystemFileItem(target_blob_data, | 295 AppendFileSystemFileItem(target_blob_data, item.filesystem_url(), |
273 iter->filesystem_url(), | 296 item.offset() + offset, new_length, |
274 iter->offset() + offset, | 297 item.expected_modification_time()); |
275 new_length, | |
276 iter->expected_modification_time()); | |
277 } | 298 } |
278 length -= new_length; | 299 length -= new_length; |
279 offset = 0; | 300 offset = 0; |
280 } | 301 } |
281 return true; | 302 return true; |
282 } | 303 } |
283 | 304 |
284 bool BlobStorageContext::AppendBytesItem( | 305 bool BlobStorageContext::AppendBytesItem(BlobDataBuilder* target_blob_data, |
285 BlobData* target_blob_data, const char* bytes, int64 length) { | 306 const char* bytes, |
307 int64 length) { | |
286 if (length < 0) { | 308 if (length < 0) { |
287 DCHECK(false); | 309 DCHECK(false); |
288 return false; | 310 return false; |
289 } | 311 } |
290 if (memory_usage_ + length > kMaxMemoryUsage) | 312 if (memory_usage_ + length > kMaxMemoryUsage) { |
291 return false; | 313 return false; |
314 } | |
292 target_blob_data->AppendData(bytes, static_cast<size_t>(length)); | 315 target_blob_data->AppendData(bytes, static_cast<size_t>(length)); |
293 memory_usage_ += length; | 316 memory_usage_ += length; |
294 return true; | 317 return true; |
295 } | 318 } |
296 | 319 |
297 void BlobStorageContext::AppendFileItem( | 320 void BlobStorageContext::AppendFileItem( |
298 BlobData* target_blob_data, | 321 BlobDataBuilder* target_blob_data, |
299 const base::FilePath& file_path, uint64 offset, uint64 length, | 322 const base::FilePath& file_path, |
323 uint64 offset, | |
324 uint64 length, | |
300 const base::Time& expected_modification_time) { | 325 const base::Time& expected_modification_time) { |
301 target_blob_data->AppendFile(file_path, offset, length, | |
302 expected_modification_time); | |
303 | |
304 // It may be a temporary file that should be deleted when no longer needed. | 326 // It may be a temporary file that should be deleted when no longer needed. |
305 scoped_refptr<ShareableFileReference> shareable_file = | 327 scoped_refptr<ShareableFileReference> shareable_file = |
306 ShareableFileReference::Get(file_path); | 328 ShareableFileReference::Get(file_path); |
307 if (shareable_file.get()) | 329 |
308 target_blob_data->AttachShareableFileReference(shareable_file.get()); | 330 target_blob_data->AppendFile(file_path, offset, length, |
331 expected_modification_time, shareable_file); | |
309 } | 332 } |
310 | 333 |
311 void BlobStorageContext::AppendFileSystemFileItem( | 334 void BlobStorageContext::AppendFileSystemFileItem( |
312 BlobData* target_blob_data, | 335 BlobDataBuilder* target_blob_data, |
313 const GURL& filesystem_url, uint64 offset, uint64 length, | 336 const GURL& filesystem_url, |
337 uint64 offset, | |
338 uint64 length, | |
314 const base::Time& expected_modification_time) { | 339 const base::Time& expected_modification_time) { |
315 target_blob_data->AppendFileSystemFile(filesystem_url, offset, length, | 340 target_blob_data->AppendFileSystemFile(filesystem_url, offset, length, |
316 expected_modification_time); | 341 expected_modification_time); |
317 } | 342 } |
318 | 343 |
319 bool BlobStorageContext::IsInUse(const std::string& uuid) { | 344 bool BlobStorageContext::IsInUse(const std::string& uuid) { |
320 return blob_map_.find(uuid) != blob_map_.end(); | 345 return blob_map_.find(uuid) != blob_map_.end(); |
321 } | 346 } |
322 | 347 |
323 bool BlobStorageContext::IsBeingBuilt(const std::string& uuid) { | 348 bool BlobStorageContext::IsBeingBuilt(const std::string& uuid) { |
324 BlobMap::iterator found = blob_map_.find(uuid); | 349 BlobMap::iterator found = blob_map_.find(uuid); |
325 if (found == blob_map_.end()) | 350 if (found == blob_map_.end()) |
326 return false; | 351 return false; |
327 return found->second.flags & BEING_BUILT; | 352 return found->second->IsBeingBuilt(); |
328 } | 353 } |
329 | 354 |
330 bool BlobStorageContext::IsUrlRegistered(const GURL& blob_url) { | 355 bool BlobStorageContext::IsUrlRegistered(const GURL& blob_url) { |
331 return public_blob_urls_.find(blob_url) != public_blob_urls_.end(); | 356 return public_blob_urls_.find(blob_url) != public_blob_urls_.end(); |
332 } | 357 } |
333 | 358 |
334 } // namespace storage | 359 } // namespace storage |
OLD | NEW |