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