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

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: Fixed layout tests, cleaned up test visibility Created 4 years, 5 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 // and maintains a mapping from blob uuid to the data. The class is single
49 // threaded and should only be used on the IO thread. 50 // threaded and should only be used on the IO thread.
50 // In chromium, there is one instance per profile. 51 // In chromium, there is one instance per profile.
51 class STORAGE_EXPORT BlobStorageContext 52 class STORAGE_EXPORT BlobStorageContext
52 : public base::SupportsWeakPtr<BlobStorageContext> { 53 : public base::SupportsWeakPtr<BlobStorageContext> {
53 public: 54 public:
54 BlobStorageContext(); 55 BlobStorageContext();
55 ~BlobStorageContext(); 56 ~BlobStorageContext();
56 57
57 std::unique_ptr<BlobDataHandle> GetBlobDataFromUUID(const std::string& uuid); 58 std::unique_ptr<BlobDataHandle> GetBlobDataFromUUID(const std::string& uuid);
58 std::unique_ptr<BlobDataHandle> GetBlobDataFromPublicURL(const GURL& url); 59 std::unique_ptr<BlobDataHandle> GetBlobDataFromPublicURL(const GURL& url);
59 60
60 // Useful for coining blobs from within the browser process. If the 61 // Useful for coining blobs from within the browser process. Use the
61 // blob cannot be added due to memory consumption, returns NULL. 62 // 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 63 // builder contains items added by AppendFutureData or AppendFutureFile, then
63 // resources are refcounted instead of copied for memory efficiency. 64 // you must use the BuildBlob method below.
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 {
81 return kBlobStorageMaxMemoryUsage - memory_usage_;
82 }
83 83
84 const BlobStorageRegistry& registry() { return registry_; } 84 const BlobStorageRegistry& registry() { return registry_; }
85 85
86 private: 86 void EnableDisk(const base::FilePath& storage_directory,
87 using BlobRegistryEntry = BlobStorageRegistry::Entry; 87 scoped_refptr<base::TaskRunner> file_runner);
88 using BlobConstructedCallback = BlobStorageRegistry::BlobConstructedCallback; 88
89 // This builds a blob with the given |input_builder| and returns a handle to
90 // the constructed Blob. Blob metadata and data should be accessed through
91 // this handle.
92 // If there is data present that needs further population then we will call
93 // |can_populate_memory| when we're ready for the user data to be populated
94 // with the PENDING_DATA_POPULATION status. This can happen synchronously or
95 // asynchronously. Otherwise |can_populate_memory| should be null.
96 // In the further population case, the caller must call either
97 // FinishedPopulatingPendingBlob or BreakAndFinishPendingBlob after
98 // |can_populate_memory| is called to signify the data is finished populating
99 // or an error occurred (respectively).
100 // If the returned handle is broken, then the possible error cases are:
101 // * OUT_OF_MEMORY if we don't have enough memory to store the blob,
102 // * REFERENCED_BLOB_BROKEN if a referenced blob is broken or we're
103 // referencing ourself.
104 std::unique_ptr<BlobDataHandle> BuildBlob(
105 const BlobDataBuilder& input_builder,
106 const BlobStatusCallback& can_populate_memory);
107
108 // Note: Any callbacks waiting on this blob, including the
109 // |can_populate_memory| callback given to BuildBlob, will be called with this
110 // status code.
111 void BreakAndFinishPendingBlob(const std::string& uuid, BlobStatus code);
112
113 // This notifies the construction system that the unpopulated data in the
114 // given blob has been populated. Caller must have all pending items populated
115 // in the original builder |content| given in BuildBlob or we'll check-fail.
116 void FinishedPopulatingPendingBlob(const std::string& uuid);
117
118 const BlobMemoryController& memory_controller() { return memory_controller_; }
119
120 protected:
89 friend class content::BlobDispatcherHost; 121 friend class content::BlobDispatcherHost;
122 friend class content::BlobDispatcherHostTest;
90 friend class BlobAsyncBuilderHost; 123 friend class BlobAsyncBuilderHost;
91 friend class BlobAsyncBuilderHostTest; 124 friend class BlobAsyncBuilderHostTest;
92 friend class BlobDataHandle; 125 friend class BlobDataHandle;
93 friend class BlobDataHandle::BlobDataHandleShared; 126 friend class BlobDataHandle::BlobDataHandleShared;
94 friend class BlobReaderTest; 127 friend class BlobFlattenerTest;
95 FRIEND_TEST_ALL_PREFIXES(BlobReaderTest, HandleBeforeAsyncCancel);
96 FRIEND_TEST_ALL_PREFIXES(BlobReaderTest, ReadFromIncompleteBlob);
97 friend class BlobStorageContextTest; 128 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 129
118 void IncrementBlobRefCount(const std::string& uuid); 130 void IncrementBlobRefCount(const std::string& uuid);
119 void DecrementBlobRefCount(const std::string& uuid); 131 void DecrementBlobRefCount(const std::string& uuid);
120 132
121 // Methods called by BlobDataHandle: 133 // Methods called by BlobDataHandle:
122 // This will return an empty snapshot until the blob is complete. 134 // This will return an empty snapshot until the blob is complete.
123 // TODO(dmurph): After we make the snapshot method in BlobHandle private, then 135 // TODO(dmurph): After we make the snapshot method in BlobHandle private, then
124 // make this DCHECK on the blob not being complete. 136 // make this DCHECK on the blob not being complete.
125 std::unique_ptr<BlobDataSnapshot> CreateSnapshot(const std::string& uuid); 137 std::unique_ptr<BlobDataSnapshot> CreateSnapshot(const std::string& uuid);
126 bool IsBroken(const std::string& uuid) const; 138 BlobStatus GetBlobStatus(const std::string& uuid) const;
127 bool IsBeingBuilt(const std::string& uuid) const;
128 // Runs |done| when construction completes, with true if it was successful, 139 // Runs |done| when construction completes, with true if it was successful,
129 // and false if there was an error, which is reported in the second argument 140 // and false if there was an error, which is reported in the second argument
130 // of the callback. 141 // of the callback.
131 void RunOnConstructionComplete(const std::string& uuid, 142 void RunOnConstructionComplete(const std::string& uuid,
132 const BlobConstructedCallback& done); 143 const BlobStatusCallback& done);
133 144
134 // Appends the given blob item to the blob builder. The new blob 145 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 146
144 // Allocates a shareable blob data item, with blob references resolved. If 147 BlobMemoryController* mutable_memory_controller() {
145 // there isn't enough memory, then a nullptr is returned. 148 return &memory_controller_;
146 scoped_refptr<ShareableBlobDataItem> AllocateShareableBlobDataItem( 149 }
147 const std::string& target_blob_uuid,
148 scoped_refptr<BlobDataItem> data_item);
149 150
150 // Deconstructs the blob and appends it's contents to the target blob. Items 151 private:
151 // are shared if possible, and copied if the given offset and length 152 std::unique_ptr<BlobDataHandle> CreateHandle(const std::string& uuid,
152 // have to split an item. 153 InternalBlobData* entry);
153 bool AppendBlob(const std::string& target_blob_uuid, 154
154 const InternalBlobData& blob, 155 bool CanFinishBuilding(InternalBlobData* entry);
155 uint64_t offset, 156
156 uint64_t length, 157 void FinishBuilding(InternalBlobData* entry);
157 InternalBlobData::Builder* target_blob_data); 158
159 void OnEnoughSizeForBlobData(const std::string& uuid, bool can_fit);
160
161 void OnDependentBlobFinished(const std::string& owning_blob_uuid,
162 BlobStatus reason);
163
164 void ClearAndFreeMemory(const std::string& uuid, InternalBlobData* entry);
158 165
159 BlobStorageRegistry registry_; 166 BlobStorageRegistry registry_;
160 167 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 168
166 DISALLOW_COPY_AND_ASSIGN(BlobStorageContext); 169 DISALLOW_COPY_AND_ASSIGN(BlobStorageContext);
167 }; 170 };
168 171
169 } // namespace storage 172 } // namespace storage
170 173
171 #endif // STORAGE_BROWSER_BLOB_BLOB_STORAGE_CONTEXT_H_ 174 #endif // STORAGE_BROWSER_BLOB_BLOB_STORAGE_CONTEXT_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698