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 #ifndef STORAGE_BROWSER_BLOB_BLOB_ENTRY_H_ |
| 6 #define STORAGE_BROWSER_BLOB_BLOB_ENTRY_H_ |
| 7 |
| 8 #include <stddef.h> |
| 9 |
| 10 #include <memory> |
| 11 #include <string> |
| 12 #include <vector> |
| 13 |
| 14 #include "base/callback_forward.h" |
| 15 #include "base/macros.h" |
| 16 #include "base/memory/ref_counted.h" |
| 17 #include "base/memory/weak_ptr.h" |
| 18 #include "base/optional.h" |
| 19 #include "storage/browser/blob/blob_memory_controller.h" |
| 20 #include "storage/browser/storage_browser_export.h" |
| 21 |
| 22 namespace storage { |
| 23 class BlobDataHandle; |
| 24 class ShareableBlobDataItem; |
| 25 class ViewBlobInternalsJob; |
| 26 |
| 27 // This class represents a blob in BlobStorageRegistry. We export this only for |
| 28 // unit tests. |
| 29 class STORAGE_EXPORT BlobEntry { |
| 30 public: |
| 31 using TransportAllowedCallback = |
| 32 base::Callback<void(BlobStatus, |
| 33 std::vector<BlobMemoryController::FileCreationInfo>)>; |
| 34 |
| 35 // This records a copy from a referenced blob. When we finish building our |
| 36 // blob we perform all of these copies. |
| 37 struct STORAGE_EXPORT ItemCopyEntry { |
| 38 ItemCopyEntry(scoped_refptr<ShareableBlobDataItem> source_item, |
| 39 size_t source_item_offset, |
| 40 scoped_refptr<ShareableBlobDataItem> dest_item); |
| 41 ~ItemCopyEntry(); |
| 42 ItemCopyEntry(ItemCopyEntry&& other); |
| 43 BlobEntry::ItemCopyEntry& operator=(BlobEntry::ItemCopyEntry&& rhs); |
| 44 |
| 45 scoped_refptr<ShareableBlobDataItem> source_item; |
| 46 size_t source_item_offset = 0; |
| 47 scoped_refptr<ShareableBlobDataItem> dest_item; |
| 48 |
| 49 private: |
| 50 DISALLOW_COPY_AND_ASSIGN(ItemCopyEntry); |
| 51 }; |
| 52 |
| 53 // This keeps track of our building state for our blob. While building, four |
| 54 // things can be happening mostly simultaneously: |
| 55 // 1. Waiting for quota to be reserved for memory needed (PENDING_QUOTA) |
| 56 // 2. Waiting for user population of data after quota (PENDING_TRANSPORT) |
| 57 // 3. Waiting for blobs we reference to complete (PENDING_INTERNALS) |
| 58 struct STORAGE_EXPORT BuildingState { |
| 59 // |transport_allowed_callback| is not null when data needs population. See |
| 60 // BlobStorageContext::BuildBlob for when the callback is called. |
| 61 BuildingState(bool transport_items_present, |
| 62 TransportAllowedCallback transport_allowed_callback, |
| 63 size_t num_building_dependent_blobs); |
| 64 ~BuildingState(); |
| 65 |
| 66 const bool transport_items_present; |
| 67 // We can have trasnport data that's either populated or unpopulated. If we |
| 68 // need population, this is populated. |
| 69 TransportAllowedCallback transport_allowed_callback; |
| 70 std::vector<ShareableBlobDataItem*> transport_items; |
| 71 |
| 72 // Stores all blobs that we're depending on for building. This keeps the |
| 73 // blobs alive while we build our blob. |
| 74 std::vector<std::unique_ptr<BlobDataHandle>> dependent_blobs; |
| 75 size_t num_building_dependent_blobs; |
| 76 |
| 77 base::WeakPtr<BlobMemoryController::QuotaAllocationTask> |
| 78 memory_quota_request; |
| 79 |
| 80 // These are copies from a referenced blob item to our blob items. Some of |
| 81 // these entries may have changed from bytes to files if they were paged. |
| 82 std::vector<ItemCopyEntry> copies; |
| 83 |
| 84 // When our blob finishes building these callbacks are called. |
| 85 std::vector<BlobStatusCallback> build_completion_callbacks; |
| 86 |
| 87 private: |
| 88 DISALLOW_COPY_AND_ASSIGN(BuildingState); |
| 89 }; |
| 90 |
| 91 BlobEntry(const std::string& content_type, |
| 92 const std::string& content_disposition); |
| 93 ~BlobEntry(); |
| 94 |
| 95 // Appends the given shared blob data item to this object. |
| 96 void AppendSharedBlobItem(scoped_refptr<ShareableBlobDataItem> item); |
| 97 |
| 98 // Returns if we're a pending blob that can finish building. |
| 99 bool CanFinishBuilding() const { |
| 100 return status_ == BlobStatus::PENDING_INTERNALS && |
| 101 building_state_->num_building_dependent_blobs == 0; |
| 102 } |
| 103 |
| 104 BlobStatus status() const { return status_; } |
| 105 |
| 106 size_t refcount() const { return refcount_; } |
| 107 |
| 108 const std::string& content_type() const { return content_type_; } |
| 109 |
| 110 const std::string& content_disposition() const { |
| 111 return content_disposition_; |
| 112 } |
| 113 |
| 114 // Total size of this blob in bytes. |
| 115 uint64_t total_size() const { return size_; }; |
| 116 |
| 117 // We record the cumulative offsets of each blob item (except for the first, |
| 118 // which would always be 0) to enable binary searching for an item at a |
| 119 // specific byte offset. |
| 120 const std::vector<uint64_t>& offsets() const { return offsets_; } |
| 121 |
| 122 const std::vector<scoped_refptr<ShareableBlobDataItem>>& items() const; |
| 123 |
| 124 protected: |
| 125 friend class BlobStorageContext; |
| 126 |
| 127 void IncrementRefCount() { ++refcount_; } |
| 128 void DecrementRefCount() { --refcount_; } |
| 129 |
| 130 void set_status(BlobStatus status) { status_ = status; } |
| 131 void set_size(uint64_t size) { size_ = size; } |
| 132 |
| 133 void ClearItems(); |
| 134 void ClearOffsets(); |
| 135 |
| 136 void set_building_state(std::unique_ptr<BuildingState> building_state) { |
| 137 building_state_ = std::move(building_state); |
| 138 } |
| 139 |
| 140 private: |
| 141 BlobStatus status_ = BlobStatus::PENDING_QUOTA; |
| 142 size_t refcount_ = 0; |
| 143 |
| 144 // Metadata. |
| 145 std::string content_type_; |
| 146 std::string content_disposition_; |
| 147 |
| 148 std::vector<scoped_refptr<ShareableBlobDataItem>> items_; |
| 149 |
| 150 // Size in bytes. IFF we're a single file then this can be uint64_max. |
| 151 uint64_t size_ = 0; |
| 152 |
| 153 // Only populated if len(items_) > 1. Used for binary search. |
| 154 // Since the offset of the first item is always 0, we exclude this. |
| 155 std::vector<uint64_t> offsets_; |
| 156 |
| 157 // Only populated if our status is PENDING_*. |
| 158 std::unique_ptr<BuildingState> building_state_; |
| 159 |
| 160 DISALLOW_COPY_AND_ASSIGN(BlobEntry); |
| 161 }; |
| 162 |
| 163 } // namespace storage |
| 164 #endif // STORAGE_BROWSER_BLOB_BLOB_ENTRY_H_ |
OLD | NEW |