| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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/internal_blob_data.h" | 5 #include "storage/browser/blob/internal_blob_data.h" |
| 6 | 6 |
| 7 #include <stddef.h> | |
| 8 | |
| 9 #include <memory> | |
| 10 #include <utility> | 7 #include <utility> |
| 11 | 8 |
| 9 #include "base/callback.h" |
| 12 #include "base/containers/hash_tables.h" | 10 #include "base/containers/hash_tables.h" |
| 13 #include "base/metrics/histogram.h" | 11 #include "base/metrics/histogram.h" |
| 12 #include "storage/browser/blob/blob_data_handle.h" |
| 14 #include "storage/browser/blob/blob_data_item.h" | 13 #include "storage/browser/blob/blob_data_item.h" |
| 14 #include "storage/browser/blob/shareable_blob_data_item.h" |
| 15 #include "storage/common/data_element.h" | 15 #include "storage/common/data_element.h" |
| 16 | 16 |
| 17 namespace storage { | 17 namespace storage { |
| 18 namespace { |
| 19 bool IsBytes(DataElement::Type type) { |
| 20 return type == DataElement::TYPE_BYTES || |
| 21 type == DataElement::TYPE_BYTES_DESCRIPTION; |
| 22 } |
| 23 } // namespace |
| 18 | 24 |
| 19 InternalBlobData::Builder::Builder() : data_(new InternalBlobData()) { | 25 InternalBlobData::ItemCopyEntry::ItemCopyEntry( |
| 20 } | 26 scoped_refptr<ShareableBlobDataItem> source_item, |
| 21 InternalBlobData::Builder::~Builder() { | 27 size_t source_item_offset, |
| 22 } | 28 scoped_refptr<ShareableBlobDataItem> dest_item) |
| 29 : source_item(std::move(source_item)), |
| 30 source_item_offset(source_item_offset), |
| 31 dest_item(std::move(dest_item)) {} |
| 32 InternalBlobData::ItemCopyEntry::ItemCopyEntry(const ItemCopyEntry&) = default; |
| 33 InternalBlobData::ItemCopyEntry::~ItemCopyEntry() {} |
| 23 | 34 |
| 24 void InternalBlobData::Builder::AppendSharedBlobItem( | 35 InternalBlobData::BuildingState::BuildingState( |
| 36 TransportState transport_state, |
| 37 PopulatationAllowedCallback user_data_population_callback, |
| 38 size_t num_building_dependent_blobs, |
| 39 bool memory_quota_needed, |
| 40 bool file_quota_needed) |
| 41 : transport_state(transport_state), |
| 42 user_data_population_callback(user_data_population_callback), |
| 43 dependent_building_blobs_present(num_building_dependent_blobs > 0), |
| 44 num_building_dependent_blobs(num_building_dependent_blobs), |
| 45 memory_quota_needed(memory_quota_needed), |
| 46 file_quota_needed(file_quota_needed) {} |
| 47 |
| 48 InternalBlobData::BuildingState::~BuildingState() {} |
| 49 |
| 50 InternalBlobData::InternalBlobData(const std::string& content_type, |
| 51 const std::string& content_disposition) |
| 52 : content_type_(content_type), content_disposition_(content_disposition) {} |
| 53 InternalBlobData::~InternalBlobData() {} |
| 54 |
| 55 void InternalBlobData::AppendSharedBlobItem( |
| 56 const std::string& my_uuid, |
| 25 scoped_refptr<ShareableBlobDataItem> item) { | 57 scoped_refptr<ShareableBlobDataItem> item) { |
| 26 DCHECK(item); | 58 if (!items_.empty()) { |
| 27 DCHECK(data_); | 59 offsets_.push_back(size_); |
| 28 data_->items_.push_back(item); | 60 } |
| 29 } | 61 size_ += item->item()->length(); |
| 30 | 62 item->referencing_blobs_mutable()->insert(my_uuid); |
| 31 void InternalBlobData::Builder::RemoveBlobFromShareableItems( | 63 items_.push_back(std::move(item)); |
| 32 const std::string& blob_uuid) { | |
| 33 DCHECK(data_); | |
| 34 data_->RemoveBlobFromShareableItems(blob_uuid); | |
| 35 } | |
| 36 | |
| 37 size_t InternalBlobData::Builder::GetNonsharedMemoryUsage() const { | |
| 38 DCHECK(data_); | |
| 39 return data_->GetUnsharedMemoryUsage(); | |
| 40 } | |
| 41 | |
| 42 std::unique_ptr<InternalBlobData> InternalBlobData::Builder::Build() { | |
| 43 DCHECK(data_); | |
| 44 return std::move(data_); | |
| 45 } | |
| 46 | |
| 47 InternalBlobData::InternalBlobData() { | |
| 48 } | |
| 49 | |
| 50 InternalBlobData::~InternalBlobData() { | |
| 51 } | 64 } |
| 52 | 65 |
| 53 const std::vector<scoped_refptr<ShareableBlobDataItem>>& | 66 const std::vector<scoped_refptr<ShareableBlobDataItem>>& |
| 54 InternalBlobData::items() const { | 67 InternalBlobData::items() const { |
| 55 return items_; | 68 return items_; |
| 56 } | 69 } |
| 57 | 70 |
| 58 void InternalBlobData::RemoveBlobFromShareableItems( | 71 void InternalBlobData::RemoveBlobFromShareableItems( |
| 59 const std::string& blob_uuid) { | 72 const std::string& blob_uuid) { |
| 60 for (auto& data_item : items_) { | 73 for (auto& data_item : items_) { |
| 61 data_item->referencing_blobs_mutable()->erase(blob_uuid); | 74 data_item->referencing_blobs_mutable()->erase(blob_uuid); |
| 62 } | 75 } |
| 63 } | 76 } |
| 64 | 77 |
| 65 size_t InternalBlobData::GetUnsharedMemoryUsage() const { | 78 size_t InternalBlobData::GetUnsharedMemoryUsage() const { |
| 66 size_t memory = 0; | 79 size_t memory = 0; |
| 67 base::hash_set<void*> seen_items; | 80 base::hash_set<void*> seen_items; |
| 68 for (const auto& data_item : items_) { | 81 for (const auto& data_item : items_) { |
| 69 if (data_item->item()->type() != DataElement::TYPE_BYTES || | 82 if (!IsBytes(data_item->item()->type()) || |
| 70 data_item->referencing_blobs().size() > 1 || | 83 data_item->referencing_blobs().size() > 1 || |
| 71 seen_items.find(data_item.get()) != seen_items.end()) { | 84 seen_items.find(data_item.get()) != seen_items.end()) { |
| 72 continue; | 85 continue; |
| 73 } | 86 } |
| 87 LOG(ERROR) << "no one else has item " << data_item->item_id(); |
| 74 memory += data_item->item()->length(); | 88 memory += data_item->item()->length(); |
| 75 seen_items.insert(data_item.get()); | 89 seen_items.insert(data_item.get()); |
| 76 } | 90 } |
| 77 return memory; | 91 return memory; |
| 78 } | 92 } |
| 79 | 93 |
| 80 void InternalBlobData::GetMemoryUsage(size_t* total_memory, | |
| 81 size_t* unshared_memory) { | |
| 82 *total_memory = 0; | |
| 83 *unshared_memory = 0; | |
| 84 base::hash_set<void*> seen_items; | |
| 85 for (const auto& data_item : items_) { | |
| 86 if (data_item->item()->type() == DataElement::TYPE_BYTES) { | |
| 87 *total_memory += data_item->item()->length(); | |
| 88 if (data_item->referencing_blobs().size() == 1 && | |
| 89 seen_items.find(data_item.get()) == seen_items.end()) { | |
| 90 *unshared_memory += data_item->item()->length(); | |
| 91 seen_items.insert(data_item.get()); | |
| 92 } | |
| 93 } | |
| 94 } | |
| 95 } | |
| 96 | |
| 97 } // namespace storage | 94 } // namespace storage |
| OLD | NEW |