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

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

Issue 2055053003: [BlobAsync] Disk support for blob storage (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: rebase 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
« no previous file with comments | « storage/browser/blob/blob_reader.cc ('k') | storage/browser/blob/blob_storage_context.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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_memory_controller.h"
22 #include "storage/browser/blob/blob_storage_registry.h" 23 #include "storage/browser/blob/blob_storage_registry.h"
23 #include "storage/browser/blob/internal_blob_data.h" 24 #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 PopulatationAllowedCallback =
54 InternalBlobData::PopulatationAllowedCallback;
55
56 // Initializes the context without disk support.
54 BlobStorageContext(); 57 BlobStorageContext();
58 // Initialized the context with disk support if the file_runner is populated.
59 BlobStorageContext(const base::FilePath& storage_directory,
60 scoped_refptr<base::TaskRunner> file_runner);
55 ~BlobStorageContext(); 61 ~BlobStorageContext();
56 62
57 std::unique_ptr<BlobDataHandle> GetBlobDataFromUUID(const std::string& uuid); 63 std::unique_ptr<BlobDataHandle> GetBlobDataFromUUID(const std::string& uuid);
58 std::unique_ptr<BlobDataHandle> GetBlobDataFromPublicURL(const GURL& url); 64 std::unique_ptr<BlobDataHandle> GetBlobDataFromPublicURL(const GURL& url);
59 65
60 // Useful for coining blobs from within the browser process. If the
61 // blob cannot be added due to memory consumption, returns NULL.
62 // A builder should not be used twice to create blobs, as the internal
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( 66 std::unique_ptr<BlobDataHandle> AddFinishedBlob(
68 const BlobDataBuilder& builder); 67 const BlobDataBuilder& builder);
69 68
70 // Deprecated, use const ref version above. 69 // Deprecated, use const ref version above or BuildBlob below.
71 std::unique_ptr<BlobDataHandle> AddFinishedBlob( 70 std::unique_ptr<BlobDataHandle> AddFinishedBlob(
72 const BlobDataBuilder* builder); 71 const BlobDataBuilder* builder);
73 72
73 std::unique_ptr<BlobDataHandle> AddBrokenBlob(
74 const std::string& uuid,
75 const std::string& content_type,
76 const std::string& content_disposition,
77 BlobStatus reason);
78
74 // Useful for coining blob urls from within the browser process. 79 // Useful for coining blob urls from within the browser process.
75 bool RegisterPublicBlobURL(const GURL& url, const std::string& uuid); 80 bool RegisterPublicBlobURL(const GURL& url, const std::string& uuid);
76 void RevokePublicBlobURL(const GURL& url); 81 void RevokePublicBlobURL(const GURL& url);
77 82
78 size_t memory_usage() const { return memory_usage_; }
79 size_t blob_count() const { return registry_.blob_count(); } 83 size_t blob_count() const { return registry_.blob_count(); }
80 size_t memory_available() const { 84
81 return kBlobStorageMaxMemoryUsage - memory_usage_; 85 const BlobStorageRegistry& registry() { return registry_; }
86
87 // This builds a blob with the given |input_builder| and returns a handle to
88 // the constructed Blob. Blob metadata and data should be accessed through
89 // this handle.
90 // If there is data present that needs further population then we will call
91 // |can_populate_memory| when we're ready for the user data to be populated
92 // with the PENDING_DATA_POPULATION status. This can happen synchronously or
93 // asynchronously. Otherwise |can_populate_memory| should be null.
94 // In the further population case, the caller must call either
95 // FinishedPopulatingPendingBlob or BreakAndFinishPendingBlob after
96 // |can_populate_memory| is called to signify the data is finished populating
97 // or an error occurred (respectively).
98 // If the returned handle is broken, then the possible error cases are:
99 // * OUT_OF_MEMORY if we don't have enough memory to store the blob,
100 // * REFERENCED_BLOB_BROKEN if a referenced blob is broken or we're
101 // referencing ourself.
102 std::unique_ptr<BlobDataHandle> BuildBlob(
103 const BlobDataBuilder& input_builder,
104 const PopulatationAllowedCallback& population_allowed_callback);
105
106 // This breaks a blob that is currently being build by using the BuildBlob
107 // method above. Any callbacks waiting on this blob, including the
108 // |population_allowed_callback| callback given to BuildBlob, will be called
109 // with this status code.
110 void BreakAndFinishPendingBlob(const std::string& uuid, BlobStatus code);
111
112 // After calling BuildBlob above, the user should call this to notify the
113 // construction system that the unpopulated data in the given blob has been
114 // populated. Caller must have all pending items populated in the original
115 // builder |content| given in BuildBlob or we'll check-fail. If there isn't
116 // any pending data in the |input_builder| for the BuildBlob call, then this
117 // doesn't need to be called.
118 void FinishedPopulatingPendingBlob(const std::string& uuid);
119
120 const BlobMemoryController& memory_controller() { return memory_controller_; }
121
122 base::WeakPtr<BlobStorageContext> AsWeakPtr() {
123 return ptr_factory_.GetWeakPtr();
82 } 124 }
83 125
84 const BlobStorageRegistry& registry() { return registry_; } 126 protected:
85
86 private:
87 using BlobRegistryEntry = BlobStorageRegistry::Entry;
88 using BlobConstructedCallback = BlobStorageRegistry::BlobConstructedCallback;
89 friend class content::BlobDispatcherHost; 127 friend class content::BlobDispatcherHost;
128 friend class content::BlobDispatcherHostTest;
90 friend class BlobAsyncBuilderHost; 129 friend class BlobAsyncBuilderHost;
91 friend class BlobAsyncBuilderHostTest; 130 friend class BlobAsyncBuilderHostTest;
92 friend class BlobDataHandle; 131 friend class BlobDataHandle;
93 friend class BlobDataHandle::BlobDataHandleShared; 132 friend class BlobDataHandle::BlobDataHandleShared;
94 friend class BlobReaderTest; 133 friend class BlobFlattenerTest;
95 FRIEND_TEST_ALL_PREFIXES(BlobReaderTest, HandleBeforeAsyncCancel); 134 friend class BlobSliceTest;
96 FRIEND_TEST_ALL_PREFIXES(BlobReaderTest, ReadFromIncompleteBlob);
97 friend class BlobStorageContextTest; 135 friend class BlobStorageContextTest;
98 FRIEND_TEST_ALL_PREFIXES(BlobStorageContextTest, IncrementDecrementRef); 136
99 FRIEND_TEST_ALL_PREFIXES(BlobStorageContextTest, OnCancelBuildingBlob); 137 // Transforms a BlobDataBuilder into a InternalBlobData with no blob
100 FRIEND_TEST_ALL_PREFIXES(BlobStorageContextTest, PublicBlobUrls); 138 // references. We use BlobSlice to flatten out these references. We record
101 FRIEND_TEST_ALL_PREFIXES(BlobStorageContextTest, 139 // the total size and items for memory and file quota requests.
102 TestUnknownBrokenAndBuildingBlobReference); 140 // Visible for testing.
103 friend class ViewBlobInternalsJob; 141 struct STORAGE_EXPORT BlobFlattener {
104 142 BlobFlattener(const BlobDataBuilder& input_builder,
105 // CompletePendingBlob or CancelPendingBlob should be called after this. 143 InternalBlobData* output_blob,
106 void CreatePendingBlob(const std::string& uuid, 144 BlobStorageRegistry* registry);
107 const std::string& content_type, 145 ~BlobFlattener();
108 const std::string& content_disposition); 146
109 147 // This can be:
110 // This includes resolving blob references in the builder. This will run the 148 // * PENDING_INTERNALS if we're populated and don't need quota.
111 // callbacks given in RunOnConstructionComplete. 149 // * PENDING if there's pending data that the user needs to
112 void CompletePendingBlob(const BlobDataBuilder& external_builder); 150 // populate. This also means we need to request quota.
113 151 // * INVALID_CONSTRUCTION_ARGUMENTS if we have invalid input.
114 // This will run the callbacks given in RunOnConstructionComplete. 152 // * REFERENCED_BLOB_BROKEN if one of the referenced blobs is broken or we
115 void CancelPendingBlob(const std::string& uuid, 153 // reference ourself.
116 IPCBlobCreationCancelCode reason); 154 BlobStatus status = BlobStatus::ERR_INVALID_CONSTRUCTION_ARGUMENTS;
155
156 // This is the total size of the blob, including all memory, files, etc.
157 uint64_t total_size = 0;
158
159 std::vector<std::pair<std::string, InternalBlobData*>> dependent_blobs;
160
161 uint64_t memory_quota_needed = 0;
162 std::vector<scoped_refptr<ShareableBlobDataItem>> pending_memory_items;
163
164 uint64_t file_quota_needed = 0;
165 std::vector<scoped_refptr<ShareableBlobDataItem>> pending_file_items;
166
167 std::vector<ShareableBlobDataItem*> user_items;
168
169 // These record all future copies we'll need to do from referenced blobs.
170 // This
171 // happens when we do a partial slice from a pending data or file item.
172 std::vector<InternalBlobData::ItemCopyEntry> copies;
173 };
174
175 // Used when a blob reference has a size and offset. Records the source items
176 // and memory we need to copy if either side of slice intersects an item.
177 // Visible for testing.
178 struct STORAGE_EXPORT BlobSlice {
179 BlobSlice(const InternalBlobData& source,
180 uint64_t slice_offset,
181 uint64_t slice_size);
182 ~BlobSlice();
183
184 // Size of memory copying from the source blob.
185 size_t copying_memory_size = 0;
186
187 size_t first_item_slice_offset = 0;
188 // Populated if our first slice item is a temporary item that we'll copy to
189 // later from this |first_source_item|, at offset |first_item_slice_offset|.
190 scoped_refptr<ShareableBlobDataItem> first_source_item;
191 // Populated if our last slice item is a temporary item that we'll copy to
192 // later from this |last_source_item|.
193 scoped_refptr<ShareableBlobDataItem> last_source_item;
194
195 std::vector<scoped_refptr<ShareableBlobDataItem>> dest_items;
196 };
117 197
118 void IncrementBlobRefCount(const std::string& uuid); 198 void IncrementBlobRefCount(const std::string& uuid);
119 void DecrementBlobRefCount(const std::string& uuid); 199 void DecrementBlobRefCount(const std::string& uuid);
120 200
121 // Methods called by BlobDataHandle:
122 // This will return an empty snapshot until the blob is complete. 201 // This will return an empty snapshot until the blob is complete.
123 // TODO(dmurph): After we make the snapshot method in BlobHandle private, then 202 // TODO(dmurph): After we make the snapshot method in BlobHandle private, then
124 // make this DCHECK on the blob not being complete. 203 // make this DCHECK on the blob not being complete.
125 std::unique_ptr<BlobDataSnapshot> CreateSnapshot(const std::string& uuid); 204 std::unique_ptr<BlobDataSnapshot> CreateSnapshot(const std::string& uuid);
126 bool IsBroken(const std::string& uuid) const; 205
127 bool IsBeingBuilt(const std::string& uuid) const; 206 BlobStatus GetBlobStatus(const std::string& uuid) const;
128 // Runs |done| when construction completes, with true if it was successful, 207
129 // and false if there was an error, which is reported in the second argument 208 // Runs |done| when construction completes with the final status of the blob.
130 // of the callback.
131 void RunOnConstructionComplete(const std::string& uuid, 209 void RunOnConstructionComplete(const std::string& uuid,
132 const BlobConstructedCallback& done); 210 const BlobStatusCallback& done_callback);
133 211
134 // Appends the given blob item to the blob builder. The new blob 212 BlobStorageRegistry* mutable_registry() { return &registry_; }
135 // retains ownership of data_item if applicable, and returns false if there 213
136 // was an error and pouplates the error_code. We can either have an error of: 214 BlobMemoryController* mutable_memory_controller() {
137 // OUT_OF_MEMORY: We are out of memory to store this blob. 215 return &memory_controller_;
138 // REFERENCED_BLOB_BROKEN: One of the referenced blobs is broken. 216 }
139 bool AppendAllocatedBlobItem(const std::string& target_blob_uuid, 217
140 scoped_refptr<BlobDataItem> data_item, 218 private:
141 InternalBlobData::Builder* target_blob_data, 219 std::unique_ptr<BlobDataHandle> CreateHandle(const std::string& uuid,
142 IPCBlobCreationCancelCode* error_code); 220 InternalBlobData* entry);
143 221
144 // Allocates a shareable blob data item, with blob references resolved. If 222 void FinishedPopulatingPendingBlob(InternalBlobData* entry);
145 // there isn't enough memory, then a nullptr is returned. 223
146 scoped_refptr<ShareableBlobDataItem> AllocateShareableBlobDataItem( 224 void FinishBuilding(InternalBlobData* entry);
147 const std::string& target_blob_uuid, 225
148 scoped_refptr<BlobDataItem> data_item); 226 void RequestUserPopulation(
149 227 InternalBlobData* entry,
150 // Deconstructs the blob and appends it's contents to the target blob. Items 228 std::vector<BlobMemoryController::FileCreationInfo> files);
151 // are shared if possible, and copied if the given offset and length 229
152 // have to split an item. 230 void OnEnoughSizeForMemory(const std::string& uuid, bool can_fit);
153 bool AppendBlob(const std::string& target_blob_uuid, 231
154 const InternalBlobData& blob, 232 void OnEnoughSizeForTransportFiles(
155 uint64_t offset, 233 const std::string& uuid,
156 uint64_t length, 234 bool can_fit,
157 InternalBlobData::Builder* target_blob_data); 235 std::vector<BlobMemoryController::FileCreationInfo> files);
236
237 void OnDependentBlobFinished(const std::string& owning_blob_uuid,
238 BlobStatus reason);
239
240 void ClearAndFreeMemory(const std::string& uuid, InternalBlobData* entry);
241
242 // Shortcut method to set the status of the given items POPULATED_WITH_QUOTA.
243 // We expect the previous state to be QUOTA_GRANTED.
244 void SetItemStateToPopulated(std::vector<ShareableBlobDataItem*>* items);
158 245
159 BlobStorageRegistry registry_; 246 BlobStorageRegistry registry_;
160 247 BlobMemoryController memory_controller_;
161 // Used to keep track of how much memory is being utilized for blob data, 248 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 249
166 DISALLOW_COPY_AND_ASSIGN(BlobStorageContext); 250 DISALLOW_COPY_AND_ASSIGN(BlobStorageContext);
167 }; 251 };
168 252
169 } // namespace storage 253 } // namespace storage
170 254
171 #endif // STORAGE_BROWSER_BLOB_BLOB_STORAGE_CONTEXT_H_ 255 #endif // STORAGE_BROWSER_BLOB_BLOB_STORAGE_CONTEXT_H_
OLDNEW
« no previous file with comments | « storage/browser/blob/blob_reader.cc ('k') | storage/browser/blob/blob_storage_context.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698