OLD | NEW |
---|---|
1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2013 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 #ifndef STORAGE_BROWSER_BLOB_BLOB_STORAGE_CONTEXT_H_ | 5 #ifndef STORAGE_BROWSER_BLOB_BLOB_STORAGE_CONTEXT_H_ |
6 #define STORAGE_BROWSER_BLOB_BLOB_STORAGE_CONTEXT_H_ | 6 #define STORAGE_BROWSER_BLOB_BLOB_STORAGE_CONTEXT_H_ |
7 | 7 |
8 #include <stddef.h> | 8 #include <stddef.h> |
9 #include <stdint.h> | 9 #include <stdint.h> |
10 | 10 |
11 #include <map> | 11 #include <map> |
12 #include <memory> | 12 #include <memory> |
13 #include <string> | 13 #include <string> |
14 #include <vector> | 14 #include <vector> |
15 | 15 |
16 #include "base/callback_forward.h" | 16 #include "base/callback_forward.h" |
17 #include "base/gtest_prod_util.h" | 17 #include "base/gtest_prod_util.h" |
18 #include "base/macros.h" | 18 #include "base/macros.h" |
19 #include "base/memory/ref_counted.h" | 19 #include "base/memory/ref_counted.h" |
20 #include "base/memory/weak_ptr.h" | 20 #include "base/memory/weak_ptr.h" |
21 #include "storage/browser/blob/blob_data_handle.h" | 21 #include "storage/browser/blob/blob_data_handle.h" |
22 #include "storage/browser/blob/blob_entry.h" | |
23 #include "storage/browser/blob/blob_memory_controller.h" | |
22 #include "storage/browser/blob/blob_storage_registry.h" | 24 #include "storage/browser/blob/blob_storage_registry.h" |
23 #include "storage/browser/blob/internal_blob_data.h" | |
24 #include "storage/browser/storage_browser_export.h" | 25 #include "storage/browser/storage_browser_export.h" |
25 #include "storage/common/blob_storage/blob_storage_constants.h" | 26 #include "storage/common/blob_storage/blob_storage_constants.h" |
26 #include "storage/common/data_element.h" | |
27 | 27 |
28 class GURL; | 28 class GURL; |
29 | 29 |
30 namespace base { | 30 namespace base { |
31 class FilePath; | 31 class FilePath; |
32 class Time; | 32 class Time; |
33 class TaskRunner; | |
33 } | 34 } |
34 | 35 |
35 namespace content { | 36 namespace content { |
36 class BlobDispatcherHost; | 37 class BlobDispatcherHost; |
37 class BlobDispatcherHostTest; | 38 class BlobDispatcherHostTest; |
38 } | 39 } |
39 | 40 |
40 namespace storage { | 41 namespace storage { |
41 | |
42 class BlobDataBuilder; | 42 class BlobDataBuilder; |
43 class BlobDataHandle; | |
43 class BlobDataItem; | 44 class BlobDataItem; |
44 class BlobDataSnapshot; | 45 class BlobDataSnapshot; |
45 class ShareableBlobDataItem; | 46 class ShareableBlobDataItem; |
46 | 47 |
47 // This class handles the logistics of blob Storage within the browser process, | 48 // This class handles the logistics of blob storage within the browser process. |
48 // and maintains a mapping from blob uuid to the data. The class is single | 49 // We are single threaded and should only be used on the IO thread. In Chromium |
49 // threaded and should only be used on the IO thread. | 50 // there is one instance per profile. |
50 // In chromium, there is one instance per profile. | 51 class STORAGE_EXPORT BlobStorageContext { |
51 class STORAGE_EXPORT BlobStorageContext | |
52 : public base::SupportsWeakPtr<BlobStorageContext> { | |
53 public: | 52 public: |
53 using TransportAllowedCallback = BlobEntry::TransportAllowedCallback; | |
54 | |
55 // Initializes the context without disk support. | |
54 BlobStorageContext(); | 56 BlobStorageContext(); |
55 ~BlobStorageContext(); | 57 ~BlobStorageContext(); |
56 | 58 |
57 std::unique_ptr<BlobDataHandle> GetBlobDataFromUUID(const std::string& uuid); | 59 std::unique_ptr<BlobDataHandle> GetBlobDataFromUUID(const std::string& uuid); |
58 std::unique_ptr<BlobDataHandle> GetBlobDataFromPublicURL(const GURL& url); | 60 std::unique_ptr<BlobDataHandle> GetBlobDataFromPublicURL(const GURL& url); |
59 | 61 |
60 // Useful for coining blobs from within the browser process. If the | 62 // Always returns a handle to a blob. Use BlobStatus::GetBlobStatus() and |
61 // blob cannot be added due to memory consumption, returns NULL. | 63 // BlobStatus::RunOnConstructionComplete(callback) to determine construction |
62 // A builder should not be used twice to create blobs, as the internal | 64 // completion and possible errors. |
63 // resources are refcounted instead of copied for memory efficiency. | |
64 // To cleanly use a builder multiple times, please call Clone() on the | |
65 // builder, or even better for memory savings, clear the builder and append | |
66 // the previously constructed blob. | |
67 std::unique_ptr<BlobDataHandle> AddFinishedBlob( | 65 std::unique_ptr<BlobDataHandle> AddFinishedBlob( |
68 const BlobDataBuilder& builder); | 66 const BlobDataBuilder& builder); |
69 | 67 |
70 // Deprecated, use const ref version above. | 68 // Deprecated, use const ref version above or BuildBlob below. |
71 std::unique_ptr<BlobDataHandle> AddFinishedBlob( | 69 std::unique_ptr<BlobDataHandle> AddFinishedBlob( |
72 const BlobDataBuilder* builder); | 70 const BlobDataBuilder* builder); |
73 | 71 |
72 std::unique_ptr<BlobDataHandle> AddBrokenBlob( | |
73 const std::string& uuid, | |
74 const std::string& content_type, | |
75 const std::string& content_disposition, | |
76 BlobStatus reason); | |
77 | |
74 // Useful for coining blob urls from within the browser process. | 78 // Useful for coining blob urls from within the browser process. |
75 bool RegisterPublicBlobURL(const GURL& url, const std::string& uuid); | 79 bool RegisterPublicBlobURL(const GURL& url, const std::string& uuid); |
76 void RevokePublicBlobURL(const GURL& url); | 80 void RevokePublicBlobURL(const GURL& url); |
77 | 81 |
78 size_t memory_usage() const { return memory_usage_; } | |
79 size_t blob_count() const { return registry_.blob_count(); } | 82 size_t blob_count() const { return registry_.blob_count(); } |
80 size_t memory_available() const { | 83 |
81 return kBlobStorageMaxMemoryUsage - memory_usage_; | 84 const BlobStorageRegistry& registry() { return registry_; } |
85 | |
86 // This builds a blob with the given |input_builder| and returns a handle to | |
87 // the constructed Blob. Blob metadata and data should be accessed through | |
88 // this handle. | |
89 // If there is data present that needs further population then we will call | |
90 // |can_populate_memory| when we're ready for the user data to be populated | |
kinuko
2016/11/10 05:16:36
population_allowed_callback
dmurph
2016/11/10 19:53:19
Done.
| |
91 // with the PENDING_DATA_POPULATION status. This can happen synchronously or | |
92 // asynchronously. Otherwise |can_populate_memory| should be null. | |
kinuko
2016/11/10 05:16:37
population_allowed_callback
dmurph
2016/11/10 19:53:19
Done.
| |
93 // In the further population case, the caller must call either | |
94 // OnTransportComplete or CancelBuildingBlob after | |
kinuko
2016/11/10 05:16:36
OnTransportComplete -> NotifyTransportComplete ?
dmurph
2016/11/10 19:53:19
Done.
| |
95 // |population_allowed_callback| is called to signify the data is finished | |
96 // populating or an error occurred (respectively). | |
97 // If the returned handle is broken, then the possible error cases are: | |
98 // * OUT_OF_MEMORY if we don't have enough memory to store the blob, | |
99 // * REFERENCED_BLOB_BROKEN if a referenced blob is broken or we're | |
100 // referencing ourself. | |
101 std::unique_ptr<BlobDataHandle> BuildBlob( | |
102 const BlobDataBuilder& input_builder, | |
103 const TransportAllowedCallback& population_allowed_callback); | |
104 | |
105 // This breaks a blob that is currently being built by using the BuildBlob | |
106 // method above. Any callbacks waiting on this blob, including the | |
107 // |population_allowed_callback| callback given to BuildBlob, will be called | |
108 // with this status code. | |
109 void CancelBuildingBlob(const std::string& uuid, BlobStatus code); | |
110 | |
111 // After calling BuildBlob above, the user should call this to notify the | |
112 // construction system that the unpopulated data in the given blob has been | |
113 // populated. Caller must have all pending items populated in the original | |
114 // builder |content| given in BuildBlob or we'll check-fail. If there isn't | |
115 // any pending data in the |input_builder| for the BuildBlob call, then this | |
116 // doesn't need to be called. | |
117 void NotifyTransportComplete(const std::string& uuid); | |
118 | |
119 const BlobMemoryController& memory_controller() { return memory_controller_; } | |
120 | |
121 base::WeakPtr<BlobStorageContext> AsWeakPtr() { | |
122 return ptr_factory_.GetWeakPtr(); | |
82 } | 123 } |
83 | 124 |
84 const BlobStorageRegistry& registry() { return registry_; } | 125 protected: |
85 | |
86 private: | |
87 using BlobRegistryEntry = BlobStorageRegistry::Entry; | |
88 using BlobConstructedCallback = BlobStorageRegistry::BlobConstructedCallback; | |
89 friend class content::BlobDispatcherHost; | 126 friend class content::BlobDispatcherHost; |
90 friend class BlobAsyncBuilderHost; | 127 friend class content::BlobDispatcherHostTest; |
91 friend class BlobAsyncBuilderHostTest; | 128 friend class BlobTransportHost; |
129 friend class BlobTransportHostTest; | |
92 friend class BlobDataHandle; | 130 friend class BlobDataHandle; |
93 friend class BlobDataHandle::BlobDataHandleShared; | 131 friend class BlobDataHandle::BlobDataHandleShared; |
94 friend class BlobReaderTest; | 132 friend class BlobFlattenerTest; |
95 FRIEND_TEST_ALL_PREFIXES(BlobReaderTest, HandleBeforeAsyncCancel); | 133 friend class BlobSliceTest; |
96 FRIEND_TEST_ALL_PREFIXES(BlobReaderTest, ReadFromIncompleteBlob); | |
97 friend class BlobStorageContextTest; | 134 friend class BlobStorageContextTest; |
98 FRIEND_TEST_ALL_PREFIXES(BlobStorageContextTest, IncrementDecrementRef); | 135 |
99 FRIEND_TEST_ALL_PREFIXES(BlobStorageContextTest, OnCancelBuildingBlob); | 136 // Transforms a BlobDataBuilder into a BlobEntry with no blob references. |
100 FRIEND_TEST_ALL_PREFIXES(BlobStorageContextTest, PublicBlobUrls); | 137 // BlobSlice is used to flatten out these references. Records the total size |
101 FRIEND_TEST_ALL_PREFIXES(BlobStorageContextTest, | 138 // and items for memory and file quota requests. |
102 TestUnknownBrokenAndBuildingBlobReference); | 139 // Visible for testing. |
kinuko
2016/11/10 05:16:37
nit: 'Visible' -> 'Exposed in the header file' ?
dmurph
2016/11/10 19:53:19
Done.
| |
103 friend class ViewBlobInternalsJob; | 140 struct STORAGE_EXPORT BlobFlattener { |
104 | 141 BlobFlattener(const BlobDataBuilder& input_builder, |
105 // CompletePendingBlob or CancelPendingBlob should be called after this. | 142 BlobEntry* output_blob, |
106 void CreatePendingBlob(const std::string& uuid, | 143 BlobStorageRegistry* registry); |
107 const std::string& content_type, | 144 ~BlobFlattener(); |
108 const std::string& content_disposition); | 145 |
109 | 146 // This can be: |
110 // This includes resolving blob references in the builder. This will run the | 147 // * PENDING_QUOTA if we need memory quota, if we're populated and don't |
111 // callbacks given in RunOnConstructionComplete. | 148 // need quota. |
112 void CompletePendingBlob(const BlobDataBuilder& external_builder); | 149 // * PENDING_INTERNALS if we're waiting on dependent blobs or we're done. |
113 | 150 // * INVALID_CONSTRUCTION_ARGUMENTS if we have invalid input. |
114 // This will run the callbacks given in RunOnConstructionComplete. | 151 // * REFERENCED_BLOB_BROKEN if one of the referenced blobs is broken or we |
115 void CancelPendingBlob(const std::string& uuid, | 152 // reference ourself. |
116 IPCBlobCreationCancelCode reason); | 153 BlobStatus status = BlobStatus::ERR_INVALID_CONSTRUCTION_ARGUMENTS; |
154 | |
155 // This is the total size of the blob, including all memory, files, etc. | |
156 uint64_t total_size = 0; | |
157 | |
158 std::vector<std::pair<std::string, BlobEntry*>> dependent_blobs; | |
159 | |
160 uint64_t memory_quota_needed = 0; | |
161 std::vector<scoped_refptr<ShareableBlobDataItem>> pending_memory_items; | |
162 | |
163 std::vector<ShareableBlobDataItem*> user_items; | |
164 | |
165 // These record all future copies we'll need to do from referenced blobs. | |
166 // This | |
167 // happens when we do a partial slice from a pending data or file item. | |
168 std::vector<BlobEntry::ItemCopyEntry> copies; | |
169 | |
170 private: | |
171 DISALLOW_COPY_AND_ASSIGN(BlobFlattener); | |
172 }; | |
173 | |
174 // Used when a blob reference has a size and offset. Records the source items | |
175 // and memory we need to copy if either side of slice intersects an item. | |
176 // Visible for testing. | |
kinuko
2016/11/10 05:16:36
ditto
dmurph
2016/11/10 19:53:19
Done.
| |
177 struct STORAGE_EXPORT BlobSlice { | |
178 BlobSlice(const BlobEntry& source, | |
179 uint64_t slice_offset, | |
180 uint64_t slice_size); | |
181 ~BlobSlice(); | |
182 | |
183 // Size of memory copying from the source blob. | |
184 size_t copying_memory_size = 0; | |
185 | |
186 size_t first_item_slice_offset = 0; | |
187 // Populated if our first slice item is a temporary item that we'll copy to | |
188 // later from this |first_source_item|, at offset |first_item_slice_offset|. | |
189 scoped_refptr<ShareableBlobDataItem> first_source_item; | |
190 // Populated if our last slice item is a temporary item that we'll copy to | |
191 // later from this |last_source_item|. | |
192 scoped_refptr<ShareableBlobDataItem> last_source_item; | |
193 | |
194 std::vector<scoped_refptr<ShareableBlobDataItem>> dest_items; | |
195 | |
196 private: | |
197 DISALLOW_COPY_AND_ASSIGN(BlobSlice); | |
198 }; | |
117 | 199 |
118 void IncrementBlobRefCount(const std::string& uuid); | 200 void IncrementBlobRefCount(const std::string& uuid); |
119 void DecrementBlobRefCount(const std::string& uuid); | 201 void DecrementBlobRefCount(const std::string& uuid); |
120 | 202 |
121 // Methods called by BlobDataHandle: | |
122 // This will return an empty snapshot until the blob is complete. | 203 // This will return an empty snapshot until the blob is complete. |
123 // TODO(dmurph): After we make the snapshot method in BlobHandle private, then | 204 // TODO(dmurph): After we make the snapshot method in BlobHandle private, then |
124 // make this DCHECK on the blob not being complete. | 205 // make this DCHECK on the blob not being complete. |
125 std::unique_ptr<BlobDataSnapshot> CreateSnapshot(const std::string& uuid); | 206 std::unique_ptr<BlobDataSnapshot> CreateSnapshot(const std::string& uuid); |
126 bool IsBroken(const std::string& uuid) const; | 207 |
127 bool IsBeingBuilt(const std::string& uuid) const; | 208 BlobStatus GetBlobStatus(const std::string& uuid) const; |
128 // Runs |done| when construction completes, with true if it was successful, | 209 |
129 // and false if there was an error, which is reported in the second argument | 210 // Runs |done| when construction completes with the final status of the blob. |
130 // of the callback. | |
131 void RunOnConstructionComplete(const std::string& uuid, | 211 void RunOnConstructionComplete(const std::string& uuid, |
132 const BlobConstructedCallback& done); | 212 const BlobStatusCallback& done_callback); |
133 | 213 |
134 // Appends the given blob item to the blob builder. The new blob | 214 BlobStorageRegistry* mutable_registry() { return ®istry_; } |
135 // retains ownership of data_item if applicable, and returns false if there | 215 |
136 // was an error and pouplates the error_code. We can either have an error of: | 216 BlobMemoryController* mutable_memory_controller() { |
137 // OUT_OF_MEMORY: We are out of memory to store this blob. | 217 return &memory_controller_; |
138 // REFERENCED_BLOB_BROKEN: One of the referenced blobs is broken. | 218 } |
139 bool AppendAllocatedBlobItem(const std::string& target_blob_uuid, | 219 |
140 scoped_refptr<BlobDataItem> data_item, | 220 private: |
141 InternalBlobData::Builder* target_blob_data, | 221 std::unique_ptr<BlobDataHandle> CreateHandle(const std::string& uuid, |
142 IPCBlobCreationCancelCode* error_code); | 222 BlobEntry* entry); |
143 | 223 |
144 // Allocates a shareable blob data item, with blob references resolved. If | 224 void NotifyTransportComplete(BlobEntry* entry); |
145 // there isn't enough memory, then a nullptr is returned. | 225 |
146 scoped_refptr<ShareableBlobDataItem> AllocateShareableBlobDataItem( | 226 void FinishBuilding(BlobEntry* entry); |
147 const std::string& target_blob_uuid, | 227 |
148 scoped_refptr<BlobDataItem> data_item); | 228 void RequestTransport( |
149 | 229 BlobEntry* entry, |
150 // Deconstructs the blob and appends it's contents to the target blob. Items | 230 std::vector<BlobMemoryController::FileCreationInfo> files); |
151 // are shared if possible, and copied if the given offset and length | 231 |
152 // have to split an item. | 232 void OnEnoughSizeForMemory(const std::string& uuid, bool can_fit); |
153 bool AppendBlob(const std::string& target_blob_uuid, | 233 |
154 const InternalBlobData& blob, | 234 void OnDependentBlobFinished(const std::string& owning_blob_uuid, |
155 uint64_t offset, | 235 BlobStatus reason); |
156 uint64_t length, | 236 |
157 InternalBlobData::Builder* target_blob_data); | 237 void ClearAndFreeMemory(const std::string& uuid, BlobEntry* entry); |
238 | |
239 // Shortcut method to set the status of the given items POPULATED_WITH_QUOTA. | |
240 // We expect the previous state to be QUOTA_GRANTED. | |
241 void SetItemStateToPopulated(std::vector<ShareableBlobDataItem*>* items); | |
158 | 242 |
159 BlobStorageRegistry registry_; | 243 BlobStorageRegistry registry_; |
160 | 244 BlobMemoryController memory_controller_; |
161 // Used to keep track of how much memory is being utilized for blob data, | 245 base::WeakPtrFactory<BlobStorageContext> ptr_factory_; |
162 // we count only the items of TYPE_DATA which are held in memory and not | |
163 // items of TYPE_FILE. | |
164 size_t memory_usage_; | |
165 | 246 |
166 DISALLOW_COPY_AND_ASSIGN(BlobStorageContext); | 247 DISALLOW_COPY_AND_ASSIGN(BlobStorageContext); |
167 }; | 248 }; |
168 | 249 |
169 } // namespace storage | 250 } // namespace storage |
170 | 251 |
171 #endif // STORAGE_BROWSER_BLOB_BLOB_STORAGE_CONTEXT_H_ | 252 #endif // STORAGE_BROWSER_BLOB_BLOB_STORAGE_CONTEXT_H_ |
OLD | NEW |