Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(435)

Side by Side Diff: storage/browser/blob/blob_storage_context.h

Issue 2448353002: [BlobAsync] Moving async handling into BlobStorageContext & quota out. (Closed)
Patch Set: removed unused code Created 4 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 gigiven |input_builder| and returns a handle to
kinuko 2016/11/17 07:53:02 wrong edit? gigiven -> given?
dmurph 2016/11/17 18:05:23 Done.
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 // |transport_allowed_callback| when we're ready for the user data to be
91 // populated with the PENDING_DATA_POPULATION status. This can happen
92 // synchronously or asynchronously. Otherwise |transport_allowed_callback|
93 // should be null. In the further population case, the caller must call either
94 // NotifyTransportComplete or CancelBuildingBlob after
95 // |transport_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& transport_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 // |transport_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 caller should call this method to
112 // notify the construction system that the unpopulated data in the given blob
113 // has been. populated. Caller must have all pending items populated in the
114 // original builder |input_builder| given in BuildBlob or we'll check-fail.
115 // If there is no pending data in the |input_builder| for the BuildBlob call,
116 // then this method 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 // Exposed in the header file for testing.
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 // Total memory size of the blob (not including files, etc).
158 uint64_t total_memory_size = 0;
159
160 std::vector<std::pair<std::string, BlobEntry*>> dependent_blobs;
161
162 uint64_t memory_quota_needed = 0;
163 std::vector<scoped_refptr<ShareableBlobDataItem>> pending_memory_items;
164
165 std::vector<ShareableBlobDataItem*> transport_items;
166
167 // These record all future copies we'll need to do from referenced blobs.
168 // This
169 // happens when we do a partial slice from a pending data or file item.
170 std::vector<BlobEntry::ItemCopyEntry> copies;
171
172 private:
173 DISALLOW_COPY_AND_ASSIGN(BlobFlattener);
174 };
175
176 // Used when a blob reference has a size and offset. Records the source items
177 // and memory we need to copy if either side of slice intersects an item.
178 // Exposed in the header file for testing.
179 struct STORAGE_EXPORT BlobSlice {
180 BlobSlice(const BlobEntry& source,
181 uint64_t slice_offset,
182 uint64_t slice_size);
183 ~BlobSlice();
184
185 // Size of memory copying from the source blob.
186 base::CheckedNumeric<size_t> copying_memory_size = 0;
187 // Size of all memory for UMA stats.
188 base::CheckedNumeric<size_t> total_memory_size = 0;
189
190 size_t first_item_slice_offset = 0;
191 // Populated if our first slice item is a temporary item that we'll copy to
192 // later from this |first_source_item|, at offset |first_item_slice_offset|.
193 scoped_refptr<ShareableBlobDataItem> first_source_item;
194 // Populated if our last slice item is a temporary item that we'll copy to
195 // later from this |last_source_item|.
196 scoped_refptr<ShareableBlobDataItem> last_source_item;
197
198 std::vector<scoped_refptr<ShareableBlobDataItem>> dest_items;
199
200 private:
201 DISALLOW_COPY_AND_ASSIGN(BlobSlice);
202 };
117 203
118 void IncrementBlobRefCount(const std::string& uuid); 204 void IncrementBlobRefCount(const std::string& uuid);
119 void DecrementBlobRefCount(const std::string& uuid); 205 void DecrementBlobRefCount(const std::string& uuid);
120 206
121 // Methods called by BlobDataHandle:
122 // This will return an empty snapshot until the blob is complete. 207 // This will return an empty snapshot until the blob is complete.
123 // TODO(dmurph): After we make the snapshot method in BlobHandle private, then 208 // TODO(dmurph): After we make the snapshot method in BlobHandle private, then
124 // make this DCHECK on the blob not being complete. 209 // make this DCHECK on the blob not being complete.
125 std::unique_ptr<BlobDataSnapshot> CreateSnapshot(const std::string& uuid); 210 std::unique_ptr<BlobDataSnapshot> CreateSnapshot(const std::string& uuid);
126 bool IsBroken(const std::string& uuid) const; 211
127 bool IsBeingBuilt(const std::string& uuid) const; 212 BlobStatus GetBlobStatus(const std::string& uuid) const;
128 // Runs |done| when construction completes, with true if it was successful, 213
129 // and false if there was an error, which is reported in the second argument 214 // Runs |done| when construction completes with the final status of the blob.
130 // of the callback.
131 void RunOnConstructionComplete(const std::string& uuid, 215 void RunOnConstructionComplete(const std::string& uuid,
132 const BlobConstructedCallback& done); 216 const BlobStatusCallback& done_callback);
133 217
134 // Appends the given blob item to the blob builder. The new blob 218 BlobStorageRegistry* mutable_registry() { return &registry_; }
135 // retains ownership of data_item if applicable, and returns false if there 219
136 // was an error and pouplates the error_code. We can either have an error of: 220 BlobMemoryController* mutable_memory_controller() {
137 // OUT_OF_MEMORY: We are out of memory to store this blob. 221 return &memory_controller_;
138 // REFERENCED_BLOB_BROKEN: One of the referenced blobs is broken. 222 }
139 bool AppendAllocatedBlobItem(const std::string& target_blob_uuid, 223
140 scoped_refptr<BlobDataItem> data_item, 224 private:
141 InternalBlobData::Builder* target_blob_data, 225 std::unique_ptr<BlobDataHandle> CreateHandle(const std::string& uuid,
142 IPCBlobCreationCancelCode* error_code); 226 BlobEntry* entry);
143 227
144 // Allocates a shareable blob data item, with blob references resolved. If 228 void NotifyTransportCompleteInternal(BlobEntry* entry);
145 // there isn't enough memory, then a nullptr is returned. 229
146 scoped_refptr<ShareableBlobDataItem> AllocateShareableBlobDataItem( 230 void CancelBuildingBlobInternal(BlobEntry* entry, BlobStatus reason);
147 const std::string& target_blob_uuid, 231
148 scoped_refptr<BlobDataItem> data_item); 232 void FinishBuilding(BlobEntry* entry);
149 233
150 // Deconstructs the blob and appends it's contents to the target blob. Items 234 void RequestTransport(
151 // are shared if possible, and copied if the given offset and length 235 BlobEntry* entry,
152 // have to split an item. 236 std::vector<BlobMemoryController::FileCreationInfo> files);
153 bool AppendBlob(const std::string& target_blob_uuid, 237
154 const InternalBlobData& blob, 238 void OnEnoughSizeForMemory(const std::string& uuid, bool can_fit);
155 uint64_t offset, 239
156 uint64_t length, 240 void OnDependentBlobFinished(const std::string& owning_blob_uuid,
157 InternalBlobData::Builder* target_blob_data); 241 BlobStatus reason);
242
243 void ClearAndFreeMemory(BlobEntry* entry);
158 244
159 BlobStorageRegistry registry_; 245 BlobStorageRegistry registry_;
160 246 BlobMemoryController memory_controller_;
161 // Used to keep track of how much memory is being utilized for blob data, 247 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 248
166 DISALLOW_COPY_AND_ASSIGN(BlobStorageContext); 249 DISALLOW_COPY_AND_ASSIGN(BlobStorageContext);
167 }; 250 };
168 251
169 } // namespace storage 252 } // namespace storage
170 253
171 #endif // STORAGE_BROWSER_BLOB_BLOB_STORAGE_CONTEXT_H_ 254 #endif // STORAGE_BROWSER_BLOB_BLOB_STORAGE_CONTEXT_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698