| 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 <algorithm> |
| 8 #include <limits> |
| 9 |
| 7 #include "base/bind.h" | 10 #include "base/bind.h" |
| 8 #include "base/location.h" | 11 #include "base/location.h" |
| 9 #include "base/logging.h" | 12 #include "base/logging.h" |
| 13 #include "base/memory/scoped_ptr.h" |
| 10 #include "base/message_loop/message_loop_proxy.h" | 14 #include "base/message_loop/message_loop_proxy.h" |
| 11 #include "base/metrics/histogram.h" | 15 #include "base/metrics/histogram.h" |
| 12 #include "base/stl_util.h" | 16 #include "base/stl_util.h" |
| 13 #include "storage/browser/blob/blob_data_builder.h" | 17 #include "storage/browser/blob/blob_data_builder.h" |
| 18 #include "storage/browser/blob/blob_data_handle.h" |
| 14 #include "url/gurl.h" | 19 #include "url/gurl.h" |
| 15 | 20 |
| 16 namespace storage { | 21 namespace storage { |
| 17 | 22 |
| 18 namespace { | 23 namespace { |
| 19 | 24 |
| 20 // We can't use GURL directly for these hash fragment manipulations | 25 // We can't use GURL directly for these hash fragment manipulations |
| 21 // since it doesn't have specific knowlege of the BlobURL format. GURL | 26 // 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 | 27 // treats BlobURLs as if they were PathURLs which don't support hash |
| 23 // fragments. | 28 // fragments. |
| 24 | 29 |
| 25 bool BlobUrlHasRef(const GURL& url) { | 30 bool BlobUrlHasRef(const GURL& url) { |
| 26 return url.spec().find('#') != std::string::npos; | 31 return url.spec().find('#') != std::string::npos; |
| 27 } | 32 } |
| 28 | 33 |
| 29 GURL ClearBlobUrlRef(const GURL& url) { | 34 GURL ClearBlobUrlRef(const GURL& url) { |
| 30 size_t hash_pos = url.spec().find('#'); | 35 size_t hash_pos = url.spec().find('#'); |
| 31 if (hash_pos == std::string::npos) | 36 if (hash_pos == std::string::npos) |
| 32 return url; | 37 return url; |
| 33 return GURL(url.spec().substr(0, hash_pos)); | 38 return GURL(url.spec().substr(0, hash_pos)); |
| 34 } | 39 } |
| 35 | 40 |
| 36 // TODO(michaeln): use base::SysInfo::AmountOfPhysicalMemoryMB() in some | 41 // TODO(michaeln): use base::SysInfo::AmountOfPhysicalMemoryMB() in some |
| 37 // way to come up with a better limit. | 42 // way to come up with a better limit. |
| 38 static const int64 kMaxMemoryUsage = 500 * 1024 * 1024; // Half a gig. | 43 static const int64 kMaxMemoryUsage = 500 * 1024 * 1024; // Half a gig. |
| 39 | 44 |
| 40 } // namespace | 45 } // namespace |
| 41 | 46 |
| 42 BlobStorageContext::BlobMapEntry::BlobMapEntry() | 47 BlobStorageContext::BlobMapEntry::BlobMapEntry() : refcount(0), flags(0) { |
| 43 : refcount(0), flags(0) { | |
| 44 } | 48 } |
| 45 | 49 |
| 46 BlobStorageContext::BlobMapEntry::BlobMapEntry(int refcount, | 50 BlobStorageContext::BlobMapEntry::BlobMapEntry(int refcount, |
| 47 BlobDataBuilder* data) | 51 InternalBlobData::Builder* data) |
| 48 : refcount(refcount), flags(0), data_builder(data) { | 52 : refcount(refcount), flags(0), data_builder(data) { |
| 49 } | 53 } |
| 50 | 54 |
| 51 BlobStorageContext::BlobMapEntry::~BlobMapEntry() { | 55 BlobStorageContext::BlobMapEntry::~BlobMapEntry() { |
| 52 } | 56 } |
| 53 | 57 |
| 54 bool BlobStorageContext::BlobMapEntry::IsBeingBuilt() { | 58 bool BlobStorageContext::BlobMapEntry::IsBeingBuilt() { |
| 55 return data_builder; | 59 return data_builder; |
| 56 } | 60 } |
| 57 | 61 |
| 58 BlobStorageContext::BlobStorageContext() | 62 BlobStorageContext::BlobStorageContext() : memory_usage_(0) { |
| 59 : memory_usage_(0) { | |
| 60 } | 63 } |
| 61 | 64 |
| 62 BlobStorageContext::~BlobStorageContext() { | 65 BlobStorageContext::~BlobStorageContext() { |
| 63 STLDeleteContainerPairSecondPointers(blob_map_.begin(), blob_map_.end()); | 66 STLDeleteContainerPairSecondPointers(blob_map_.begin(), blob_map_.end()); |
| 64 } | 67 } |
| 65 | 68 |
| 66 scoped_ptr<BlobDataHandle> BlobStorageContext::GetBlobDataFromUUID( | 69 scoped_ptr<BlobDataHandle> BlobStorageContext::GetBlobDataFromUUID( |
| 67 const std::string& uuid) { | 70 const std::string& uuid) { |
| 68 scoped_ptr<BlobDataHandle> result; | 71 scoped_ptr<BlobDataHandle> result; |
| 69 BlobMap::iterator found = blob_map_.find(uuid); | 72 BlobMap::iterator found = blob_map_.find(uuid); |
| 70 if (found == blob_map_.end()) | 73 if (found == blob_map_.end()) |
| 71 return result.Pass(); | 74 return result.Pass(); |
| 72 auto* entry = found->second; | 75 auto* entry = found->second; |
| 73 if (entry->flags & EXCEEDED_MEMORY) | 76 if (entry->flags & EXCEEDED_MEMORY) |
| 74 return result.Pass(); | 77 return result.Pass(); |
| 75 DCHECK(!entry->IsBeingBuilt()); | 78 DCHECK(!entry->IsBeingBuilt()); |
| 76 result.reset( | 79 result.reset( |
| 77 new BlobDataHandle(uuid, this, base::MessageLoopProxy::current().get())); | 80 new BlobDataHandle(uuid, this, base::MessageLoopProxy::current().get())); |
| 78 return result.Pass(); | 81 return result.Pass(); |
| 79 } | 82 } |
| 80 | 83 |
| 81 scoped_ptr<BlobDataHandle> BlobStorageContext::GetBlobDataFromPublicURL( | 84 scoped_ptr<BlobDataHandle> BlobStorageContext::GetBlobDataFromPublicURL( |
| 82 const GURL& url) { | 85 const GURL& url) { |
| 83 BlobURLMap::iterator found = public_blob_urls_.find( | 86 BlobURLMap::iterator found = |
| 84 BlobUrlHasRef(url) ? ClearBlobUrlRef(url) : url); | 87 public_blob_urls_.find(BlobUrlHasRef(url) ? ClearBlobUrlRef(url) : url); |
| 85 if (found == public_blob_urls_.end()) | 88 if (found == public_blob_urls_.end()) |
| 86 return scoped_ptr<BlobDataHandle>(); | 89 return scoped_ptr<BlobDataHandle>(); |
| 87 return GetBlobDataFromUUID(found->second); | 90 return GetBlobDataFromUUID(found->second); |
| 88 } | 91 } |
| 89 | 92 |
| 90 scoped_ptr<BlobDataHandle> BlobStorageContext::AddFinishedBlob( | 93 scoped_ptr<BlobDataHandle> BlobStorageContext::AddFinishedBlob( |
| 91 const BlobDataBuilder& data) { | 94 BlobDataBuilder* external_builder) { |
| 92 StartBuildingBlob(data.uuid_); | 95 StartBuildingBlob(external_builder->uuid_); |
| 93 for (const auto& blob_item : data.items_) | 96 BlobMap::iterator found = blob_map_.find(external_builder->uuid_); |
| 94 AppendBlobDataItem(data.uuid_, *(blob_item->item_)); | 97 DCHECK(found != blob_map_.end()); |
| 95 FinishBuildingBlob(data.uuid_, data.content_type_); | 98 BlobMapEntry* entry = found->second; |
| 96 scoped_ptr<BlobDataHandle> handle = GetBlobDataFromUUID(data.uuid_); | 99 InternalBlobData::Builder* target_blob_builder = entry->data_builder.get(); |
| 97 DecrementBlobRefCount(data.uuid_); | 100 DCHECK(target_blob_builder); |
| 101 |
| 102 target_blob_builder->set_content_disposition( |
| 103 external_builder->content_disposition_); |
| 104 for (const auto& blob_item : external_builder->items_) { |
| 105 if (!AppendAllocatedBlobItem(external_builder->uuid_, blob_item, |
| 106 target_blob_builder)) { |
| 107 BlobEntryExceededMemory(entry); |
| 108 break; |
| 109 } |
| 110 } |
| 111 |
| 112 FinishBuildingBlob(external_builder->uuid_, external_builder->content_type_); |
| 113 scoped_ptr<BlobDataHandle> handle = |
| 114 GetBlobDataFromUUID(external_builder->uuid_); |
| 115 DecrementBlobRefCount(external_builder->uuid_); |
| 98 return handle.Pass(); | 116 return handle.Pass(); |
| 99 } | 117 } |
| 100 | 118 |
| 101 bool BlobStorageContext::RegisterPublicBlobURL( | 119 bool BlobStorageContext::RegisterPublicBlobURL(const GURL& blob_url, |
| 102 const GURL& blob_url, const std::string& uuid) { | 120 const std::string& uuid) { |
| 103 DCHECK(!BlobUrlHasRef(blob_url)); | 121 DCHECK(!BlobUrlHasRef(blob_url)); |
| 104 DCHECK(IsInUse(uuid)); | 122 DCHECK(IsInUse(uuid)); |
| 105 DCHECK(!IsUrlRegistered(blob_url)); | 123 DCHECK(!IsUrlRegistered(blob_url)); |
| 106 if (!IsInUse(uuid) || IsUrlRegistered(blob_url)) | 124 if (!IsInUse(uuid) || IsUrlRegistered(blob_url)) |
| 107 return false; | 125 return false; |
| 108 IncrementBlobRefCount(uuid); | 126 IncrementBlobRefCount(uuid); |
| 109 public_blob_urls_[blob_url] = uuid; | 127 public_blob_urls_[blob_url] = uuid; |
| 110 return true; | 128 return true; |
| 111 } | 129 } |
| 112 | 130 |
| 113 void BlobStorageContext::RevokePublicBlobURL(const GURL& blob_url) { | 131 void BlobStorageContext::RevokePublicBlobURL(const GURL& blob_url) { |
| 114 DCHECK(!BlobUrlHasRef(blob_url)); | 132 DCHECK(!BlobUrlHasRef(blob_url)); |
| 115 if (!IsUrlRegistered(blob_url)) | 133 if (!IsUrlRegistered(blob_url)) |
| 116 return; | 134 return; |
| 117 DecrementBlobRefCount(public_blob_urls_[blob_url]); | 135 DecrementBlobRefCount(public_blob_urls_[blob_url]); |
| 118 public_blob_urls_.erase(blob_url); | 136 public_blob_urls_.erase(blob_url); |
| 119 } | 137 } |
| 120 | 138 |
| 121 scoped_ptr<BlobDataSnapshot> BlobStorageContext::CreateSnapshot( | 139 scoped_ptr<BlobDataSnapshot> BlobStorageContext::CreateSnapshot( |
| 122 const std::string& uuid) { | 140 const std::string& uuid) { |
| 123 scoped_ptr<BlobDataSnapshot> result; | 141 scoped_ptr<BlobDataSnapshot> result; |
| 124 auto found = blob_map_.find(uuid); | 142 auto found = blob_map_.find(uuid); |
| 125 DCHECK(found != blob_map_.end()) | 143 DCHECK(found != blob_map_.end()) |
| 126 << "Blob should be in map, as the handle is still around"; | 144 << "Blob " << uuid << " should be in map, as the handle is still around"; |
| 127 BlobMapEntry* entry = found->second; | 145 BlobMapEntry* entry = found->second; |
| 128 DCHECK(!entry->IsBeingBuilt()); | 146 DCHECK(!entry->IsBeingBuilt()); |
| 129 result.reset(new BlobDataSnapshot(*entry->data)); | 147 const InternalBlobData& data = *entry->data; |
| 130 return result.Pass(); | 148 |
| 149 scoped_ptr<BlobDataSnapshot> snapshot(new BlobDataSnapshot( |
| 150 uuid, data.content_type(), data.content_disposition())); |
| 151 snapshot->items_.reserve(data.items().size()); |
| 152 for (const auto& shareable_item : data.items()) { |
| 153 snapshot->items_.push_back(shareable_item->item()); |
| 154 } |
| 155 return snapshot; |
| 131 } | 156 } |
| 132 | 157 |
| 133 void BlobStorageContext::StartBuildingBlob(const std::string& uuid) { | 158 void BlobStorageContext::StartBuildingBlob(const std::string& uuid) { |
| 134 DCHECK(!IsInUse(uuid) && !uuid.empty()); | 159 DCHECK(!IsInUse(uuid) && !uuid.empty()); |
| 135 blob_map_[uuid] = new BlobMapEntry(1, new BlobDataBuilder(uuid)); | 160 blob_map_[uuid] = new BlobMapEntry(1, new InternalBlobData::Builder()); |
| 136 } | 161 } |
| 137 | 162 |
| 138 void BlobStorageContext::AppendBlobDataItem(const std::string& uuid, | 163 void BlobStorageContext::AppendBlobDataItem( |
| 139 const DataElement& item) { | 164 const std::string& uuid, |
| 165 const storage::DataElement& ipc_data_element) { |
| 140 DCHECK(IsBeingBuilt(uuid)); | 166 DCHECK(IsBeingBuilt(uuid)); |
| 141 BlobMap::iterator found = blob_map_.find(uuid); | 167 BlobMap::iterator found = blob_map_.find(uuid); |
| 142 if (found == blob_map_.end()) | 168 if (found == blob_map_.end()) |
| 143 return; | 169 return; |
| 144 BlobMapEntry* entry = found->second; | 170 BlobMapEntry* entry = found->second; |
| 145 if (entry->flags & EXCEEDED_MEMORY) | 171 if (entry->flags & EXCEEDED_MEMORY) |
| 146 return; | 172 return; |
| 147 BlobDataBuilder* target_blob_data = entry->data_builder.get(); | 173 InternalBlobData::Builder* target_blob_builder = entry->data_builder.get(); |
| 148 DCHECK(target_blob_data); | 174 DCHECK(target_blob_builder); |
| 149 | 175 |
| 150 bool exceeded_memory = false; | 176 if (ipc_data_element.type() == DataElement::TYPE_BYTES && |
| 151 | 177 memory_usage_ + ipc_data_element.length() > kMaxMemoryUsage) { |
| 152 // The blob data is stored in the canonical way which only contains a | 178 BlobEntryExceededMemory(entry); |
| 153 // list of Data, File, and FileSystem items. Aggregated TYPE_BLOB items | 179 return; |
| 154 // are expanded into the primitive constituent types. | |
| 155 // 1) The Data item is denoted by the raw data and length. | |
| 156 // 2) The File item is denoted by the file path, the range and the expected | |
| 157 // modification time. | |
| 158 // 3) The FileSystem File item is denoted by the FileSystem URL, the range | |
| 159 // and the expected modification time. | |
| 160 // 4) The Blob items are expanded. | |
| 161 // TODO(michaeln): Would be nice to avoid copying Data items when expanding. | |
| 162 | |
| 163 uint64 length = item.length(); | |
| 164 DCHECK_GT(length, 0u); | |
| 165 UMA_HISTOGRAM_COUNTS("Storage.Blob.StorageSizeBeforeAppend", | |
| 166 memory_usage_ / 1024); | |
| 167 switch (item.type()) { | |
| 168 case DataElement::TYPE_BYTES: | |
| 169 UMA_HISTOGRAM_COUNTS("Storage.BlobItemSize.Bytes", length / 1024); | |
| 170 DCHECK(!item.offset()); | |
| 171 exceeded_memory = !AppendBytesItem(target_blob_data, item.bytes(), | |
| 172 static_cast<int64>(length)); | |
| 173 break; | |
| 174 case DataElement::TYPE_FILE: | |
| 175 UMA_HISTOGRAM_COUNTS("Storage.BlobItemSize.File", length / 1024); | |
| 176 AppendFileItem(target_blob_data, item.path(), item.offset(), | |
| 177 item.length(), item.expected_modification_time()); | |
| 178 break; | |
| 179 case DataElement::TYPE_FILE_FILESYSTEM: | |
| 180 UMA_HISTOGRAM_COUNTS("Storage.BlobItemSize.FileSystem", length / 1024); | |
| 181 AppendFileSystemFileItem(target_blob_data, item.filesystem_url(), | |
| 182 item.offset(), item.length(), | |
| 183 item.expected_modification_time()); | |
| 184 break; | |
| 185 case DataElement::TYPE_BLOB: { | |
| 186 UMA_HISTOGRAM_COUNTS("Storage.BlobItemSize.Blob", length / 1024); | |
| 187 // We grab the handle to ensure it stays around while we copy it. | |
| 188 scoped_ptr<BlobDataHandle> src = GetBlobDataFromUUID(item.blob_uuid()); | |
| 189 if (src) { | |
| 190 BlobMapEntry* entry = blob_map_.find(item.blob_uuid())->second; | |
| 191 DCHECK(entry->data); | |
| 192 exceeded_memory = !ExpandStorageItems(target_blob_data, *entry->data, | |
| 193 item.offset(), item.length()); | |
| 194 } | |
| 195 break; | |
| 196 } | |
| 197 default: | |
| 198 NOTREACHED(); | |
| 199 break; | |
| 200 } | 180 } |
| 201 UMA_HISTOGRAM_COUNTS("Storage.Blob.StorageSizeAfterAppend", | 181 if (!AppendAllocatedBlobItem(uuid, AllocateBlobItem(uuid, ipc_data_element), |
| 202 memory_usage_ / 1024); | 182 target_blob_builder)) { |
| 203 | 183 BlobEntryExceededMemory(entry); |
| 204 // If we're using too much memory, drop this blob's data. | |
| 205 // TODO(michaeln): Blob memory storage does not yet spill over to disk, | |
| 206 // as a stop gap, we'll prevent memory usage over a max amount. | |
| 207 if (exceeded_memory) { | |
| 208 memory_usage_ -= target_blob_data->GetMemoryUsage(); | |
| 209 entry->flags |= EXCEEDED_MEMORY; | |
| 210 entry->data_builder.reset(new BlobDataBuilder(uuid)); | |
| 211 return; | |
| 212 } | 184 } |
| 213 } | 185 } |
| 214 | 186 |
| 215 void BlobStorageContext::FinishBuildingBlob( | 187 void BlobStorageContext::FinishBuildingBlob(const std::string& uuid, |
| 216 const std::string& uuid, const std::string& content_type) { | 188 const std::string& content_type) { |
| 217 DCHECK(IsBeingBuilt(uuid)); | 189 DCHECK(IsBeingBuilt(uuid)); |
| 218 BlobMap::iterator found = blob_map_.find(uuid); | 190 BlobMap::iterator found = blob_map_.find(uuid); |
| 219 if (found == blob_map_.end()) | 191 if (found == blob_map_.end()) |
| 220 return; | 192 return; |
| 221 BlobMapEntry* entry = found->second; | 193 BlobMapEntry* entry = found->second; |
| 222 entry->data_builder->set_content_type(content_type); | 194 entry->data_builder->set_content_type(content_type); |
| 223 entry->data = entry->data_builder->BuildSnapshot().Pass(); | 195 entry->data = entry->data_builder->Build(); |
| 224 entry->data_builder.reset(); | 196 entry->data_builder.reset(); |
| 225 UMA_HISTOGRAM_COUNTS("Storage.Blob.ItemCount", entry->data->items().size()); | 197 UMA_HISTOGRAM_COUNTS("Storage.Blob.ItemCount", entry->data->items().size()); |
| 226 UMA_HISTOGRAM_BOOLEAN("Storage.Blob.ExceededMemory", | 198 UMA_HISTOGRAM_BOOLEAN("Storage.Blob.ExceededMemory", |
| 227 (entry->flags & EXCEEDED_MEMORY) == EXCEEDED_MEMORY); | 199 (entry->flags & EXCEEDED_MEMORY) == EXCEEDED_MEMORY); |
| 200 size_t total_memory = 0, nonshared_memory = 0; |
| 201 entry->data->GetMemoryUsage(&total_memory, &nonshared_memory); |
| 202 UMA_HISTOGRAM_COUNTS("Storage.Blob.TotalSize", total_memory / 1024); |
| 203 UMA_HISTOGRAM_COUNTS("Storage.Blob.TotalUnsharedSize", |
| 204 nonshared_memory / 1024); |
| 228 } | 205 } |
| 229 | 206 |
| 230 void BlobStorageContext::CancelBuildingBlob(const std::string& uuid) { | 207 void BlobStorageContext::CancelBuildingBlob(const std::string& uuid) { |
| 231 DCHECK(IsBeingBuilt(uuid)); | 208 DCHECK(IsBeingBuilt(uuid)); |
| 232 DecrementBlobRefCount(uuid); | 209 DecrementBlobRefCount(uuid); |
| 233 } | 210 } |
| 234 | 211 |
| 235 void BlobStorageContext::IncrementBlobRefCount(const std::string& uuid) { | 212 void BlobStorageContext::IncrementBlobRefCount(const std::string& uuid) { |
| 236 BlobMap::iterator found = blob_map_.find(uuid); | 213 BlobMap::iterator found = blob_map_.find(uuid); |
| 237 if (found == blob_map_.end()) { | 214 if (found == blob_map_.end()) { |
| 238 DCHECK(false); | 215 DCHECK(false); |
| 239 return; | 216 return; |
| 240 } | 217 } |
| 241 ++(found->second->refcount); | 218 ++(found->second->refcount); |
| 242 } | 219 } |
| 243 | 220 |
| 244 void BlobStorageContext::DecrementBlobRefCount(const std::string& uuid) { | 221 void BlobStorageContext::DecrementBlobRefCount(const std::string& uuid) { |
| 245 BlobMap::iterator found = blob_map_.find(uuid); | 222 BlobMap::iterator found = blob_map_.find(uuid); |
| 246 if (found == blob_map_.end()) | 223 if (found == blob_map_.end()) |
| 247 return; | 224 return; |
| 248 auto* entry = found->second; | 225 auto* entry = found->second; |
| 249 if (--(entry->refcount) == 0) { | 226 if (--(entry->refcount) == 0) { |
| 227 size_t memory_freeing = 0; |
| 250 if (entry->IsBeingBuilt()) { | 228 if (entry->IsBeingBuilt()) { |
| 251 memory_usage_ -= entry->data_builder->GetMemoryUsage(); | 229 memory_freeing = entry->data_builder->GetNonsharedMemoryUsage(); |
| 230 entry->data_builder->RemoveBlobFromShareableItems(uuid); |
| 252 } else { | 231 } else { |
| 253 memory_usage_ -= entry->data->GetMemoryUsage(); | 232 memory_freeing = entry->data->GetUnsharedMemoryUsage(); |
| 233 entry->data->RemoveBlobFromShareableItems(uuid); |
| 254 } | 234 } |
| 235 DCHECK_LE(memory_freeing, memory_usage_); |
| 236 memory_usage_ -= memory_freeing; |
| 255 delete entry; | 237 delete entry; |
| 256 blob_map_.erase(found); | 238 blob_map_.erase(found); |
| 257 } | 239 } |
| 258 } | 240 } |
| 259 | 241 |
| 260 bool BlobStorageContext::ExpandStorageItems( | 242 void BlobStorageContext::BlobEntryExceededMemory(BlobMapEntry* entry) { |
| 261 BlobDataBuilder* target_blob_data, | 243 // If we're using too much memory, drop this blob's data. |
| 262 const BlobDataSnapshot& src_blob_data, | 244 // TODO(michaeln): Blob memory storage does not yet spill over to disk, |
| 263 uint64 offset, | 245 // as a stop gap, we'll prevent memory usage over a max amount. |
| 264 uint64 length) { | 246 memory_usage_ -= entry->data_builder->GetNonsharedMemoryUsage(); |
| 265 DCHECK(target_blob_data && length != static_cast<uint64>(-1)); | 247 entry->flags |= EXCEEDED_MEMORY; |
| 248 entry->data_builder.reset(new InternalBlobData::Builder()); |
| 249 } |
| 266 | 250 |
| 267 const std::vector<scoped_refptr<BlobDataItem>>& items = src_blob_data.items(); | 251 scoped_refptr<BlobDataItem> BlobStorageContext::AllocateBlobItem( |
| 252 const std::string& uuid, |
| 253 const DataElement& ipc_data) { |
| 254 scoped_refptr<BlobDataItem> blob_item; |
| 255 |
| 256 uint64 length = ipc_data.length(); |
| 257 scoped_ptr<DataElement> element(new DataElement()); |
| 258 switch (ipc_data.type()) { |
| 259 case DataElement::TYPE_BYTES: |
| 260 DCHECK(!ipc_data.offset()); |
| 261 element->SetToBytes(ipc_data.bytes(), length); |
| 262 blob_item = new BlobDataItem(element.Pass()); |
| 263 break; |
| 264 case DataElement::TYPE_FILE: |
| 265 element->SetToFilePathRange(ipc_data.path(), ipc_data.offset(), length, |
| 266 ipc_data.expected_modification_time()); |
| 267 blob_item = new BlobDataItem( |
| 268 element.Pass(), ShareableFileReference::Get(ipc_data.path())); |
| 269 break; |
| 270 case DataElement::TYPE_FILE_FILESYSTEM: |
| 271 element->SetToFileSystemUrlRange(ipc_data.filesystem_url(), |
| 272 ipc_data.offset(), length, |
| 273 ipc_data.expected_modification_time()); |
| 274 blob_item = new BlobDataItem(element.Pass()); |
| 275 break; |
| 276 case DataElement::TYPE_BLOB: |
| 277 // This is a temporary item that will be deconstructed later. |
| 278 element->SetToBlobRange(ipc_data.blob_uuid(), ipc_data.offset(), |
| 279 ipc_data.length()); |
| 280 blob_item = new BlobDataItem(element.Pass()); |
| 281 break; |
| 282 default: |
| 283 NOTREACHED(); |
| 284 break; |
| 285 } |
| 286 |
| 287 return blob_item; |
| 288 } |
| 289 |
| 290 bool BlobStorageContext::AppendAllocatedBlobItem( |
| 291 const std::string& target_blob_uuid, |
| 292 scoped_refptr<BlobDataItem> blob_item, |
| 293 InternalBlobData::Builder* target_blob_builder) { |
| 294 bool exceeded_memory = false; |
| 295 |
| 296 // The blob data is stored in the canonical way which only contains a |
| 297 // list of Data, File, and FileSystem items. Aggregated TYPE_BLOB items |
| 298 // are expanded into the primitive constituent types and reused if possible. |
| 299 // 1) The Data item is denoted by the raw data and length. |
| 300 // 2) The File item is denoted by the file path, the range and the expected |
| 301 // modification time. |
| 302 // 3) The FileSystem File item is denoted by the FileSystem URL, the range |
| 303 // and the expected modification time. |
| 304 // 4) The Blob item is denoted by the source blob and an offset and size. |
| 305 // Internal items that are fully used by the new blob (not cut by the |
| 306 // offset or size) are shared between the blobs. Otherwise, the relevant |
| 307 // portion of the item is copied. |
| 308 |
| 309 const DataElement& data_element = blob_item->data_element(); |
| 310 uint64 length = data_element.length(); |
| 311 uint64 offset = data_element.offset(); |
| 312 DCHECK_GT(length, 0u); |
| 313 UMA_HISTOGRAM_COUNTS("Storage.Blob.StorageSizeBeforeAppend", |
| 314 memory_usage_ / 1024); |
| 315 switch (data_element.type()) { |
| 316 case DataElement::TYPE_BYTES: |
| 317 UMA_HISTOGRAM_COUNTS("Storage.BlobItemSize.Bytes", length / 1024); |
| 318 DCHECK(!offset); |
| 319 if (memory_usage_ + length > kMaxMemoryUsage) { |
| 320 exceeded_memory = true; |
| 321 break; |
| 322 } |
| 323 memory_usage_ += length; |
| 324 target_blob_builder->AppendSharedBlobItem( |
| 325 new ShareableBlobDataItem(target_blob_uuid, blob_item)); |
| 326 break; |
| 327 case DataElement::TYPE_FILE: { |
| 328 bool full_file = (length == std::numeric_limits<uint64>::max()); |
| 329 UMA_HISTOGRAM_BOOLEAN("Storage.BlobItemSize.File.Unknown", full_file); |
| 330 if (!full_file) { |
| 331 UMA_HISTOGRAM_COUNTS("Storage.BlobItemSize.File", |
| 332 (length - offset) / 1024); |
| 333 } |
| 334 target_blob_builder->AppendSharedBlobItem( |
| 335 new ShareableBlobDataItem(target_blob_uuid, blob_item)); |
| 336 break; |
| 337 } |
| 338 case DataElement::TYPE_FILE_FILESYSTEM: { |
| 339 bool full_file = (length == std::numeric_limits<uint64>::max()); |
| 340 UMA_HISTOGRAM_BOOLEAN("Storage.BlobItemSize.FileSystem.Unknown", |
| 341 full_file); |
| 342 if (!full_file) { |
| 343 UMA_HISTOGRAM_COUNTS("Storage.BlobItemSize.FileSystem", |
| 344 (length - offset) / 1024); |
| 345 } |
| 346 target_blob_builder->AppendSharedBlobItem( |
| 347 new ShareableBlobDataItem(target_blob_uuid, blob_item)); |
| 348 break; |
| 349 } |
| 350 case DataElement::TYPE_BLOB: { |
| 351 UMA_HISTOGRAM_COUNTS("Storage.BlobItemSize.Blob", |
| 352 (length - offset) / 1024); |
| 353 // We grab the handle to ensure it stays around while we copy it. |
| 354 scoped_ptr<BlobDataHandle> src = |
| 355 GetBlobDataFromUUID(data_element.blob_uuid()); |
| 356 if (src) { |
| 357 BlobMapEntry* other_entry = |
| 358 blob_map_.find(data_element.blob_uuid())->second; |
| 359 DCHECK(other_entry->data); |
| 360 exceeded_memory = !AppendBlob(target_blob_uuid, *other_entry->data, |
| 361 offset, length, target_blob_builder); |
| 362 } |
| 363 break; |
| 364 } |
| 365 default: |
| 366 NOTREACHED(); |
| 367 break; |
| 368 } |
| 369 UMA_HISTOGRAM_COUNTS("Storage.Blob.StorageSizeAfterAppend", |
| 370 memory_usage_ / 1024); |
| 371 |
| 372 return !exceeded_memory; |
| 373 } |
| 374 |
| 375 bool BlobStorageContext::AppendBlob( |
| 376 const std::string& target_blob_uuid, |
| 377 const InternalBlobData& blob, |
| 378 size_t offset, |
| 379 size_t length, |
| 380 InternalBlobData::Builder* target_blob_builder) { |
| 381 DCHECK(length > 0); |
| 382 |
| 383 const std::vector<scoped_refptr<ShareableBlobDataItem>>& items = blob.items(); |
| 268 auto iter = items.begin(); | 384 auto iter = items.begin(); |
| 269 if (offset) { | 385 if (offset) { |
| 270 for (; iter != items.end(); ++iter) { | 386 for (; iter != items.end(); ++iter) { |
| 271 const BlobDataItem& item = *(iter->get()); | 387 const BlobDataItem& item = *(iter->get()->item()); |
| 272 if (offset >= item.length()) | 388 if (offset >= item.length()) |
| 273 offset -= item.length(); | 389 offset -= item.length(); |
| 274 else | 390 else |
| 275 break; | 391 break; |
| 276 } | 392 } |
| 277 } | 393 } |
| 278 | 394 |
| 279 for (; iter != items.end() && length > 0; ++iter) { | 395 for (; iter != items.end() && length > 0; ++iter) { |
| 280 const BlobDataItem& item = *(iter->get()); | 396 scoped_refptr<ShareableBlobDataItem> shareable_item = iter->get(); |
| 281 uint64 current_length = item.length() - offset; | 397 const BlobDataItem& item = *(shareable_item->item()); |
| 282 uint64 new_length = current_length > length ? length : current_length; | 398 size_t item_length = item.length(); |
| 283 if (iter->get()->type() == DataElement::TYPE_BYTES) { | 399 DCHECK_GT(item_length, offset); |
| 284 if (!AppendBytesItem( | 400 size_t current_length = item_length - offset; |
| 285 target_blob_data, | 401 size_t new_length = current_length > length ? length : current_length; |
| 286 item.bytes() + static_cast<size_t>(item.offset() + offset), | 402 |
| 287 static_cast<int64>(new_length))) { | 403 bool reusing_blob_item = offset == 0 && new_length == item.length(); |
| 288 return false; // exceeded memory | 404 UMA_HISTOGRAM_BOOLEAN("Storage.Blob.ReusedItem", reusing_blob_item); |
| 289 } | 405 if (reusing_blob_item) { |
| 290 } else if (item.type() == DataElement::TYPE_FILE) { | 406 shareable_item->referencing_blobs().insert(target_blob_uuid); |
| 291 AppendFileItem(target_blob_data, item.path(), item.offset() + offset, | 407 target_blob_builder->AppendSharedBlobItem(shareable_item); |
| 292 new_length, item.expected_modification_time()); | 408 length -= new_length; |
| 293 } else { | 409 continue; |
| 294 DCHECK(item.type() == DataElement::TYPE_FILE_FILESYSTEM); | 410 } |
| 295 AppendFileSystemFileItem(target_blob_data, item.filesystem_url(), | 411 |
| 296 item.offset() + offset, new_length, | 412 // We need to do copying of the items when we have a different offset or |
| 297 item.expected_modification_time()); | 413 // length |
| 414 switch (item.type()) { |
| 415 case DataElement::TYPE_BYTES: { |
| 416 UMA_HISTOGRAM_COUNTS("Storage.BlobItemSize.BlobSlice.Bytes", |
| 417 new_length / 1024); |
| 418 if (memory_usage_ + new_length > kMaxMemoryUsage) { |
| 419 return false; |
| 420 } |
| 421 DCHECK(!item.offset()); |
| 422 scoped_ptr<DataElement> element(new DataElement()); |
| 423 element->SetToBytes(item.bytes() + offset, |
| 424 static_cast<int64>(new_length)); |
| 425 memory_usage_ += new_length; |
| 426 target_blob_builder->AppendSharedBlobItem(new ShareableBlobDataItem( |
| 427 target_blob_uuid, new BlobDataItem(element.Pass()))); |
| 428 } break; |
| 429 case DataElement::TYPE_FILE: { |
| 430 DCHECK_NE(item.length(), std::numeric_limits<uint64>::max()) |
| 431 << "We cannot use a section of a file with an unknown length"; |
| 432 UMA_HISTOGRAM_COUNTS("Storage.BlobItemSize.BlobSlice.File", |
| 433 new_length / 1024); |
| 434 scoped_ptr<DataElement> element(new DataElement()); |
| 435 element->SetToFilePathRange(item.path(), item.offset() + offset, |
| 436 new_length, |
| 437 item.expected_modification_time()); |
| 438 target_blob_builder->AppendSharedBlobItem(new ShareableBlobDataItem( |
| 439 target_blob_uuid, |
| 440 new BlobDataItem(element.Pass(), item.file_handle_))); |
| 441 } break; |
| 442 case DataElement::TYPE_FILE_FILESYSTEM: { |
| 443 UMA_HISTOGRAM_COUNTS("Storage.BlobItemSize.BlobSlice.FileSystem", |
| 444 new_length / 1024); |
| 445 scoped_ptr<DataElement> element(new DataElement()); |
| 446 element->SetToFileSystemUrlRange(item.filesystem_url(), |
| 447 item.offset() + offset, new_length, |
| 448 item.expected_modification_time()); |
| 449 target_blob_builder->AppendSharedBlobItem(new ShareableBlobDataItem( |
| 450 target_blob_uuid, new BlobDataItem(element.Pass()))); |
| 451 } break; |
| 452 default: |
| 453 CHECK(false) << "Illegal blob item type: " << item.type(); |
| 298 } | 454 } |
| 299 length -= new_length; | 455 length -= new_length; |
| 300 offset = 0; | 456 offset = 0; |
| 301 } | 457 } |
| 302 return true; | 458 return true; |
| 303 } | 459 } |
| 304 | 460 |
| 305 bool BlobStorageContext::AppendBytesItem(BlobDataBuilder* target_blob_data, | |
| 306 const char* bytes, | |
| 307 int64 length) { | |
| 308 if (length < 0) { | |
| 309 DCHECK(false); | |
| 310 return false; | |
| 311 } | |
| 312 if (memory_usage_ + length > kMaxMemoryUsage) { | |
| 313 return false; | |
| 314 } | |
| 315 target_blob_data->AppendData(bytes, static_cast<size_t>(length)); | |
| 316 memory_usage_ += length; | |
| 317 return true; | |
| 318 } | |
| 319 | |
| 320 void BlobStorageContext::AppendFileItem( | |
| 321 BlobDataBuilder* target_blob_data, | |
| 322 const base::FilePath& file_path, | |
| 323 uint64 offset, | |
| 324 uint64 length, | |
| 325 const base::Time& expected_modification_time) { | |
| 326 // It may be a temporary file that should be deleted when no longer needed. | |
| 327 scoped_refptr<ShareableFileReference> shareable_file = | |
| 328 ShareableFileReference::Get(file_path); | |
| 329 | |
| 330 target_blob_data->AppendFile(file_path, offset, length, | |
| 331 expected_modification_time, shareable_file); | |
| 332 } | |
| 333 | |
| 334 void BlobStorageContext::AppendFileSystemFileItem( | |
| 335 BlobDataBuilder* target_blob_data, | |
| 336 const GURL& filesystem_url, | |
| 337 uint64 offset, | |
| 338 uint64 length, | |
| 339 const base::Time& expected_modification_time) { | |
| 340 target_blob_data->AppendFileSystemFile(filesystem_url, offset, length, | |
| 341 expected_modification_time); | |
| 342 } | |
| 343 | |
| 344 bool BlobStorageContext::IsInUse(const std::string& uuid) { | 461 bool BlobStorageContext::IsInUse(const std::string& uuid) { |
| 345 return blob_map_.find(uuid) != blob_map_.end(); | 462 return blob_map_.find(uuid) != blob_map_.end(); |
| 346 } | 463 } |
| 347 | 464 |
| 348 bool BlobStorageContext::IsBeingBuilt(const std::string& uuid) { | 465 bool BlobStorageContext::IsBeingBuilt(const std::string& uuid) { |
| 349 BlobMap::iterator found = blob_map_.find(uuid); | 466 BlobMap::iterator found = blob_map_.find(uuid); |
| 350 if (found == blob_map_.end()) | 467 if (found == blob_map_.end()) |
| 351 return false; | 468 return false; |
| 352 return found->second->IsBeingBuilt(); | 469 return found->second->IsBeingBuilt(); |
| 353 } | 470 } |
| 354 | 471 |
| 355 bool BlobStorageContext::IsUrlRegistered(const GURL& blob_url) { | 472 bool BlobStorageContext::IsUrlRegistered(const GURL& blob_url) { |
| 356 return public_blob_urls_.find(blob_url) != public_blob_urls_.end(); | 473 return public_blob_urls_.find(blob_url) != public_blob_urls_.end(); |
| 357 } | 474 } |
| 358 | 475 |
| 359 } // namespace storage | 476 } // namespace storage |
| OLD | NEW |