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