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

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: Combined BlobSlice & BlobFlattener files, more comments, a little cleanup. Created 4 years, 4 months 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_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/storage_browser_export.h" 24 #include "storage/browser/storage_browser_export.h"
25 #include "storage/common/blob_storage/blob_storage_constants.h" 25 #include "storage/common/blob_storage/blob_storage_constants.h"
26 #include "storage/common/data_element.h"
27 26
28 class GURL; 27 class GURL;
29 28
30 namespace base { 29 namespace base {
31 class FilePath; 30 class FilePath;
32 class Time; 31 class Time;
32 class TaskRunner;
33 } 33 }
34 34
35 namespace content { 35 namespace content {
36 class BlobDispatcherHost; 36 class BlobDispatcherHost;
37 class BlobDispatcherHostTest; 37 class BlobDispatcherHostTest;
38 } 38 }
39 39
40 namespace storage { 40 namespace storage {
41 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> { 52 : public base::SupportsWeakPtr<BlobStorageContext> {
53 public: 53 public:
54 using PopulatationAllowedCallback =
55 base::Callback<void(BlobStatus,
56 std::vector<BlobMemoryController::FileCreationInfo>)>;
57
54 BlobStorageContext(); 58 BlobStorageContext();
55 ~BlobStorageContext(); 59 ~BlobStorageContext();
56 60
57 std::unique_ptr<BlobDataHandle> GetBlobDataFromUUID(const std::string& uuid); 61 std::unique_ptr<BlobDataHandle> GetBlobDataFromUUID(const std::string& uuid);
58 std::unique_ptr<BlobDataHandle> GetBlobDataFromPublicURL(const GURL& url); 62 std::unique_ptr<BlobDataHandle> GetBlobDataFromPublicURL(const GURL& url);
59 63
60 // Useful for coining blobs from within the browser process. If the 64 // Useful for coining blobs from within the browser process. Use the
61 // blob cannot be added due to memory consumption, returns NULL. 65 // BlobDataHandle::GetBlobStatus call to check if there was an error. If the
62 // A builder should not be used twice to create blobs, as the internal 66 // builder contains items added by AppendFutureData or AppendFutureFile, then
63 // resources are refcounted instead of copied for memory efficiency. 67 // you must use the BuildBlob method below.
michaeln 2016/08/15 22:44:44 Might not need the comments about a finished blob
dmurph 2016/08/19 00:18:33 Done.
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( 68 std::unique_ptr<BlobDataHandle> AddFinishedBlob(
68 const BlobDataBuilder& builder); 69 const BlobDataBuilder& builder);
69 70
70 // Deprecated, use const ref version above. 71 // Deprecated, use const ref version above or BuildBlob below.
71 std::unique_ptr<BlobDataHandle> AddFinishedBlob( 72 std::unique_ptr<BlobDataHandle> AddFinishedBlob(
72 const BlobDataBuilder* builder); 73 const BlobDataBuilder* builder);
73 74
75 std::unique_ptr<BlobDataHandle> AddBrokenBlob(
76 const std::string& uuid,
77 const std::string& content_type,
78 const std::string& content_disposition,
79 BlobStatus reason);
80
74 // Useful for coining blob urls from within the browser process. 81 // Useful for coining blob urls from within the browser process.
75 bool RegisterPublicBlobURL(const GURL& url, const std::string& uuid); 82 bool RegisterPublicBlobURL(const GURL& url, const std::string& uuid);
76 void RevokePublicBlobURL(const GURL& url); 83 void RevokePublicBlobURL(const GURL& url);
77 84
78 size_t memory_usage() const { return memory_usage_; }
79 size_t blob_count() const { return registry_.blob_count(); } 85 size_t blob_count() const { return registry_.blob_count(); }
80 size_t memory_available() const {
81 return kBlobStorageMaxMemoryUsage - memory_usage_;
82 }
83 86
84 const BlobStorageRegistry& registry() { return registry_; } 87 const BlobStorageRegistry& registry() { return registry_; }
85 88
86 private: 89 void EnableDisk(const base::FilePath& storage_directory,
87 using BlobRegistryEntry = BlobStorageRegistry::Entry; 90 scoped_refptr<base::TaskRunner> file_runner);
88 using BlobConstructedCallback = BlobStorageRegistry::BlobConstructedCallback; 91
92 // This builds a blob with the given |input_builder| and returns a handle to
93 // the constructed Blob. Blob metadata and data should be accessed through
94 // this handle.
95 // If there is data present that needs further population then we will call
96 // |can_populate_memory| when we're ready for the user data to be populated
97 // with the PENDING_DATA_POPULATION status. This can happen synchronously or
98 // asynchronously. Otherwise |can_populate_memory| should be null.
99 // In the further population case, the caller must call either
100 // FinishedPopulatingPendingBlob or BreakAndFinishPendingBlob after
101 // |can_populate_memory| is called to signify the data is finished populating
102 // or an error occurred (respectively).
103 // If the returned handle is broken, then the possible error cases are:
104 // * OUT_OF_MEMORY if we don't have enough memory to store the blob,
105 // * REFERENCED_BLOB_BROKEN if a referenced blob is broken or we're
106 // referencing ourself.
michaeln 2016/08/15 22:44:44 I'd vote to rename the following three methods (in
107 std::unique_ptr<BlobDataHandle> BuildBlob(
108 const BlobDataBuilder& input_builder,
109 const PopulatationAllowedCallback& population_allowed_callback);
110
111 // This breaks a blob that is currently being build by using the BuildBlob
112 // method above. Any callbacks waiting on this blob, including the
113 // |population_allowed_callback| callback given to BuildBlob, will be called
114 // with this status code.
115 void BreakAndFinishPendingBlob(const std::string& uuid, BlobStatus code);
116
117 // After calling BuildBlob above, the user should call this to notify the
118 // construction system that the unpopulated data in the given blob has been
119 // populated. Caller must have all pending items populated in the original
120 // builder |content| given in BuildBlob or we'll check-fail. If there isn't
121 // any pending data in the |input_builder| for the BuildBlob call, then this
122 // doesn't need to be called.
123 void FinishedPopulatingPendingBlob(const std::string& uuid);
124
125 const BlobMemoryController& memory_controller() { return memory_controller_; }
126
127 protected:
89 friend class content::BlobDispatcherHost; 128 friend class content::BlobDispatcherHost;
129 friend class content::BlobDispatcherHostTest;
90 friend class BlobAsyncBuilderHost; 130 friend class BlobAsyncBuilderHost;
91 friend class BlobAsyncBuilderHostTest; 131 friend class BlobAsyncBuilderHostTest;
92 friend class BlobDataHandle; 132 friend class BlobDataHandle;
93 friend class BlobDataHandle::BlobDataHandleShared; 133 friend class BlobDataHandle::BlobDataHandleShared;
94 friend class BlobReaderTest; 134 friend class BlobFlattenerTest;
95 FRIEND_TEST_ALL_PREFIXES(BlobReaderTest, HandleBeforeAsyncCancel);
96 FRIEND_TEST_ALL_PREFIXES(BlobReaderTest, ReadFromIncompleteBlob);
97 friend class BlobStorageContextTest; 135 friend class BlobStorageContextTest;
98 FRIEND_TEST_ALL_PREFIXES(BlobStorageContextTest, IncrementDecrementRef);
99 FRIEND_TEST_ALL_PREFIXES(BlobStorageContextTest, OnCancelBuildingBlob);
100 FRIEND_TEST_ALL_PREFIXES(BlobStorageContextTest, PublicBlobUrls);
101 FRIEND_TEST_ALL_PREFIXES(BlobStorageContextTest,
102 TestUnknownBrokenAndBuildingBlobReference);
103 friend class ViewBlobInternalsJob;
104
105 // CompletePendingBlob or CancelPendingBlob should be called after this.
106 void CreatePendingBlob(const std::string& uuid,
107 const std::string& content_type,
108 const std::string& content_disposition);
109
110 // This includes resolving blob references in the builder. This will run the
111 // callbacks given in RunOnConstructionComplete.
112 void CompletePendingBlob(const BlobDataBuilder& external_builder);
113
114 // This will run the callbacks given in RunOnConstructionComplete.
115 void CancelPendingBlob(const std::string& uuid,
116 IPCBlobCreationCancelCode reason);
117 136
118 void IncrementBlobRefCount(const std::string& uuid); 137 void IncrementBlobRefCount(const std::string& uuid);
119 void DecrementBlobRefCount(const std::string& uuid); 138 void DecrementBlobRefCount(const std::string& uuid);
120 139
121 // Methods called by BlobDataHandle:
122 // This will return an empty snapshot until the blob is complete. 140 // This will return an empty snapshot until the blob is complete.
123 // TODO(dmurph): After we make the snapshot method in BlobHandle private, then 141 // TODO(dmurph): After we make the snapshot method in BlobHandle private, then
124 // make this DCHECK on the blob not being complete. 142 // make this DCHECK on the blob not being complete.
125 std::unique_ptr<BlobDataSnapshot> CreateSnapshot(const std::string& uuid); 143 std::unique_ptr<BlobDataSnapshot> CreateSnapshot(const std::string& uuid);
126 bool IsBroken(const std::string& uuid) const; 144
127 bool IsBeingBuilt(const std::string& uuid) const; 145 BlobStatus GetBlobStatus(const std::string& uuid) const;
128 // Runs |done| when construction completes, with true if it was successful, 146
129 // and false if there was an error, which is reported in the second argument 147 // Runs |done| when construction completes with the final status of the blob.
130 // of the callback.
131 void RunOnConstructionComplete(const std::string& uuid, 148 void RunOnConstructionComplete(const std::string& uuid,
132 const BlobConstructedCallback& done); 149 const BlobStatusCallback& done_callback);
133 150
134 // Appends the given blob item to the blob builder. The new blob 151 BlobStorageRegistry* mutable_registry() { return &registry_; }
135 // retains ownership of data_item if applicable, and returns false if there
136 // was an error and pouplates the error_code. We can either have an error of:
137 // OUT_OF_MEMORY: We are out of memory to store this blob.
138 // REFERENCED_BLOB_BROKEN: One of the referenced blobs is broken.
139 bool AppendAllocatedBlobItem(const std::string& target_blob_uuid,
140 scoped_refptr<BlobDataItem> data_item,
141 InternalBlobData::Builder* target_blob_data,
142 IPCBlobCreationCancelCode* error_code);
143 152
144 // Allocates a shareable blob data item, with blob references resolved. If 153 BlobMemoryController* mutable_memory_controller() {
145 // there isn't enough memory, then a nullptr is returned. 154 return &memory_controller_;
146 scoped_refptr<ShareableBlobDataItem> AllocateShareableBlobDataItem( 155 }
147 const std::string& target_blob_uuid,
148 scoped_refptr<BlobDataItem> data_item);
149 156
150 // Deconstructs the blob and appends it's contents to the target blob. Items 157 private:
151 // are shared if possible, and copied if the given offset and length 158 std::unique_ptr<BlobDataHandle> CreateHandle(const std::string& uuid,
152 // have to split an item. 159 InternalBlobData* entry);
153 bool AppendBlob(const std::string& target_blob_uuid, 160
154 const InternalBlobData& blob, 161 void FinishBuilding(InternalBlobData* entry);
155 uint64_t offset, 162
156 uint64_t length, 163 // Callback given for our quota request for the copies data.
157 InternalBlobData::Builder* target_blob_data); 164 void OnEnoughSizeForCopies(
165 const std::string& uuid,
166 bool can_fit,
167 std::vector<BlobMemoryController::FileCreationInfo> files);
168
169 // Callback given for our quota request for user pending data.
170 void OnEnoughSizeForUserData(
171 const std::string& uuid,
172 bool can_fit,
173 std::vector<BlobMemoryController::FileCreationInfo> files);
174
175 void OnDependentBlobFinished(const std::string& owning_blob_uuid,
176 BlobStatus reason);
177
178 void ClearAndFreeMemory(const std::string& uuid, InternalBlobData* entry);
179
180 // Shortcut method to set the status of the given items POPULATED_WITH_QUOTA.
181 // We expect the previous state to be QUOTA_GRANTED.
182 void SetItemStateToPopulated(std::vector<ShareableBlobDataItem*>* items);
158 183
159 BlobStorageRegistry registry_; 184 BlobStorageRegistry registry_;
160 185 BlobMemoryController memory_controller_;
161 // Used to keep track of how much memory is being utilized for blob data,
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 186
166 DISALLOW_COPY_AND_ASSIGN(BlobStorageContext); 187 DISALLOW_COPY_AND_ASSIGN(BlobStorageContext);
167 }; 188 };
168 189
169 } // namespace storage 190 } // namespace storage
170 191
171 #endif // STORAGE_BROWSER_BLOB_BLOB_STORAGE_CONTEXT_H_ 192 #endif // STORAGE_BROWSER_BLOB_BLOB_STORAGE_CONTEXT_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698