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. We add |my_uuid| | |
96 // to the shareable item's uuid set. | |
kinuko
2016/11/17 07:53:01
Comment looks stale
dmurph
2016/11/17 18:05:23
Done.
| |
97 void AppendSharedBlobItem(scoped_refptr<ShareableBlobDataItem> item); | |
98 | |
99 // Returns if we're a pending blob that can finish building. | |
100 bool CanFinishBuilding() const { | |
101 return status_ == BlobStatus::PENDING_INTERNALS && | |
102 building_state_->num_building_dependent_blobs == 0; | |
103 } | |
104 | |
105 BlobStatus status() const { return status_; } | |
106 | |
107 size_t refcount() const { return refcount_; } | |
108 | |
109 const std::string& content_type() const { return content_type_; } | |
110 | |
111 const std::string& content_disposition() const { | |
112 return content_disposition_; | |
113 } | |
114 | |
115 // Total size of this blob in bytes. | |
116 uint64_t total_size() const { return size_; }; | |
117 | |
118 // We record the cumulative offsets of each blob item (except for the first, | |
119 // which would always be 0) to enable binary searching for an item at a | |
120 // specific byte offset. | |
121 const std::vector<uint64_t>& offsets() const { return offsets_; } | |
122 | |
123 const std::vector<scoped_refptr<ShareableBlobDataItem>>& items() const; | |
124 | |
125 protected: | |
126 friend class BlobStorageContext; | |
127 | |
128 void IncrementRefCount() { ++refcount_; } | |
129 void DecrementRefCount() { --refcount_; } | |
130 | |
131 void set_status(BlobStatus status) { status_ = status; } | |
132 void set_size(uint64_t size) { size_ = size; } | |
133 | |
134 void ClearItems(); | |
135 void ClearOffsets(); | |
136 | |
137 void set_building_state(std::unique_ptr<BuildingState> building_state) { | |
138 building_state_ = std::move(building_state); | |
139 } | |
140 | |
141 private: | |
142 BlobStatus status_ = BlobStatus::PENDING_QUOTA; | |
143 size_t refcount_ = 0; | |
144 | |
145 // Metadata. | |
146 std::string content_type_; | |
147 std::string content_disposition_; | |
148 | |
149 std::vector<scoped_refptr<ShareableBlobDataItem>> items_; | |
150 | |
151 // Size in bytes. IFF we're a single file then this can be uint64_max. | |
152 uint64_t size_ = 0; | |
153 | |
154 // Only populated if len(items_) > 1. Used for binary search. | |
155 // Since the offset of the first item is always 0, we exclude this. | |
156 std::vector<uint64_t> offsets_; | |
157 | |
158 // Only populated if our status is PENDING_*. | |
159 std::unique_ptr<BuildingState> building_state_; | |
160 | |
161 DISALLOW_COPY_AND_ASSIGN(BlobEntry); | |
162 }; | |
163 | |
164 } // namespace storage | |
165 #endif // STORAGE_BROWSER_BLOB_BLOB_ENTRY_H_ | |
OLD | NEW |