OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "storage/browser/blob/internal_blob_data.h" |
| 6 |
| 7 #include "base/containers/hash_tables.h" |
| 8 #include "base/metrics/histogram.h" |
| 9 #include "storage/browser/blob/blob_data_item.h" |
| 10 #include "storage/common/data_element.h" |
| 11 |
| 12 namespace storage { |
| 13 |
| 14 InternalBlobData::Builder::Builder() : data_(new InternalBlobData()) { |
| 15 } |
| 16 InternalBlobData::Builder::~Builder() { |
| 17 } |
| 18 |
| 19 void InternalBlobData::Builder::AppendSharedBlobItem( |
| 20 scoped_refptr<ShareableBlobDataItem> item) { |
| 21 DCHECK(item); |
| 22 DCHECK(data_); |
| 23 data_->items_.push_back(item); |
| 24 } |
| 25 |
| 26 void InternalBlobData::Builder::RemoveBlobFromShareableItems( |
| 27 const std::string& blob_uuid) { |
| 28 DCHECK(data_); |
| 29 data_->RemoveBlobFromShareableItems(blob_uuid); |
| 30 } |
| 31 |
| 32 void InternalBlobData::Builder::set_content_type( |
| 33 const std::string& content_type) { |
| 34 DCHECK(data_); |
| 35 data_->content_type_ = content_type; |
| 36 } |
| 37 |
| 38 void InternalBlobData::Builder::set_content_disposition( |
| 39 const std::string& content_disposition) { |
| 40 DCHECK(data_); |
| 41 data_->content_disposition_ = content_disposition; |
| 42 } |
| 43 |
| 44 size_t InternalBlobData::Builder::GetNonsharedMemoryUsage() const { |
| 45 DCHECK(data_); |
| 46 return data_->GetUnsharedMemoryUsage(); |
| 47 } |
| 48 |
| 49 scoped_ptr<InternalBlobData> InternalBlobData::Builder::Build() { |
| 50 DCHECK(data_); |
| 51 return data_.Pass(); |
| 52 } |
| 53 |
| 54 InternalBlobData::InternalBlobData() { |
| 55 } |
| 56 |
| 57 InternalBlobData::~InternalBlobData() { |
| 58 } |
| 59 |
| 60 const std::vector<scoped_refptr<ShareableBlobDataItem>>& |
| 61 InternalBlobData::items() const { |
| 62 return items_; |
| 63 } |
| 64 const std::string& InternalBlobData::content_type() const { |
| 65 return content_type_; |
| 66 } |
| 67 const std::string& InternalBlobData::content_disposition() const { |
| 68 return content_disposition_; |
| 69 } |
| 70 |
| 71 void InternalBlobData::RemoveBlobFromShareableItems( |
| 72 const std::string& blob_uuid) { |
| 73 for (auto& data_item : items_) { |
| 74 data_item->referencing_blobs().erase(blob_uuid); |
| 75 } |
| 76 } |
| 77 |
| 78 size_t InternalBlobData::GetUnsharedMemoryUsage() const { |
| 79 size_t memory = 0; |
| 80 base::hash_set<void*> seen_items; |
| 81 for (const auto& data_item : items_) { |
| 82 if (data_item->item()->type() != DataElement::TYPE_BYTES || |
| 83 data_item->referencing_blobs().size() > 1 || |
| 84 seen_items.find(data_item.get()) != seen_items.end()) { |
| 85 continue; |
| 86 } |
| 87 memory += data_item->item()->length(); |
| 88 seen_items.insert(data_item.get()); |
| 89 } |
| 90 return memory; |
| 91 } |
| 92 |
| 93 void InternalBlobData::GetMemoryUsage(size_t* total_memory, |
| 94 size_t* unshared_memory) { |
| 95 *total_memory = 0; |
| 96 *unshared_memory = 0; |
| 97 base::hash_set<void*> seen_items; |
| 98 for (const auto& data_item : items_) { |
| 99 if (data_item->item()->type() == DataElement::TYPE_BYTES) { |
| 100 *total_memory += data_item->item()->length(); |
| 101 if (data_item->referencing_blobs().size() == 1 || |
| 102 seen_items.find(data_item.get()) == seen_items.end()) { |
| 103 *unshared_memory += data_item->item()->length(); |
| 104 seen_items.insert(data_item.get()); |
| 105 } |
| 106 } |
| 107 } |
| 108 } |
| 109 |
| 110 } // namespace storage |
OLD | NEW |