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 "base/single_thread_task_runner.h" | |
21 #include "storage/browser/blob/blob_data_handle.h" | 22 #include "storage/browser/blob/blob_data_handle.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" | 25 #include "storage/browser/blob/internal_blob_data.h" |
24 #include "storage/browser/storage_browser_export.h" | 26 #include "storage/browser/storage_browser_export.h" |
25 #include "storage/common/blob_storage/blob_storage_constants.h" | 27 #include "storage/common/blob_storage/blob_storage_constants.h" |
26 #include "storage/common/data_element.h" | 28 #include "storage/common/data_element.h" |
27 | 29 |
28 class GURL; | 30 class GURL; |
29 | 31 |
30 namespace base { | 32 namespace base { |
31 class FilePath; | 33 class FilePath; |
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
68 const BlobDataBuilder& builder); | 70 const BlobDataBuilder& builder); |
69 | 71 |
70 // Deprecated, use const ref version above. | 72 // Deprecated, use const ref version above. |
71 std::unique_ptr<BlobDataHandle> AddFinishedBlob( | 73 std::unique_ptr<BlobDataHandle> AddFinishedBlob( |
72 const BlobDataBuilder* builder); | 74 const BlobDataBuilder* builder); |
73 | 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(); } |
michaeln
2016/07/07 20:05:21
terminology things...
finished vs complete vs br
| |
80 size_t memory_available() const { | 81 |
81 return kBlobStorageMaxMemoryUsage - memory_usage_; | 82 const BlobStorageRegistry& registry() { return registry_; } |
83 | |
84 void EnableDisk(const base::FilePath storage_directory, | |
michaeln
2016/07/07 20:05:21
const ref
| |
85 scoped_refptr<base::SingleThreadTaskRunner> file_runner); | |
86 | |
87 std::unique_ptr<BlobDataHandle> BuildBrokenBlob( | |
88 const std::string& uuid, | |
89 const std::string& content_type, | |
90 const std::string& content_disposition, | |
91 IPCBlobCreationCancelCode reason); | |
92 | |
93 | |
94 std::unique_ptr<BlobDataHandle> BuildCompleteBlob(const BlobDataBuilder& conte nt, | |
michaeln
2016/07/07 20:05:22
|builder| for consistency here and below. in pre-e
| |
95 bool content_pending); | |
96 | |
97 // We assume that any populated data can fit into our quota, where the called | |
98 // called BlobMemoryController::MaybeFitInMemoryNow and/or | |
99 // BlobMemoryController::NotifyWhenMemoryCanPopulated. | |
100 std::unique_ptr<BlobDataHandle> BuildBlob(const BlobDataBuilder& content, | |
101 bool content_pending); | |
102 | |
103 void BreakAndFinishBlob(const std::string& uuid, | |
104 IPCBlobCreationCancelCode code); | |
105 | |
106 // This notifies the construction system that the unpopulated data in the | |
107 // given blob has been populated. | |
108 void FinishedPopulatingBlob(const std::string& uuid); | |
michaeln
2016/07/07 20:05:21
terminology nits:
populating vs transporting
comp
| |
109 | |
110 protected: | |
111 friend class BlobSlice; | |
112 friend class BlobFlattener; | |
113 | |
114 friend class content::BlobDispatcherHostTest; | |
115 | |
116 // Not thread safe. | |
117 static uint64_t GetAndIncrementItemId(); | |
118 | |
119 BlobMemoryController* mutable_memory_controller() { | |
120 return &memory_controller_; | |
82 } | 121 } |
83 | 122 |
84 const BlobStorageRegistry& registry() { return registry_; } | 123 const BlobMemoryController& memory_controller() { return memory_controller_; } |
85 | 124 |
86 private: | 125 private: |
87 using BlobRegistryEntry = BlobStorageRegistry::Entry; | 126 using BlobRegistryEntry = BlobStorageRegistry::Entry; |
88 using BlobConstructedCallback = BlobStorageRegistry::BlobConstructedCallback; | 127 using BlobConstructedCallback = BlobStorageRegistry::BlobConstructedCallback; |
89 friend class content::BlobDispatcherHost; | 128 friend class content::BlobDispatcherHost; |
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 BlobReaderTest; |
95 FRIEND_TEST_ALL_PREFIXES(BlobReaderTest, HandleBeforeAsyncCancel); | 134 FRIEND_TEST_ALL_PREFIXES(BlobReaderTest, HandleBeforeAsyncCancel); |
96 FRIEND_TEST_ALL_PREFIXES(BlobReaderTest, ReadFromIncompleteBlob); | 135 FRIEND_TEST_ALL_PREFIXES(BlobReaderTest, ReadFromIncompleteBlob); |
97 friend class BlobStorageContextTest; | 136 friend class BlobStorageContextTest; |
98 FRIEND_TEST_ALL_PREFIXES(BlobStorageContextTest, IncrementDecrementRef); | 137 FRIEND_TEST_ALL_PREFIXES(BlobStorageContextTest, IncrementDecrementRef); |
99 FRIEND_TEST_ALL_PREFIXES(BlobStorageContextTest, OnCancelBuildingBlob); | 138 FRIEND_TEST_ALL_PREFIXES(BlobStorageContextTest, OnCancelBuildingBlob); |
100 FRIEND_TEST_ALL_PREFIXES(BlobStorageContextTest, PublicBlobUrls); | 139 FRIEND_TEST_ALL_PREFIXES(BlobStorageContextTest, PublicBlobUrls); |
101 FRIEND_TEST_ALL_PREFIXES(BlobStorageContextTest, | 140 FRIEND_TEST_ALL_PREFIXES(BlobStorageContextTest, |
102 TestUnknownBrokenAndBuildingBlobReference); | 141 TestUnknownBrokenAndBuildingBlobReference); |
103 friend class ViewBlobInternalsJob; | 142 friend class ViewBlobInternalsJob; |
104 | 143 |
105 // CompletePendingBlob or CancelPendingBlob should be called after this. | 144 std::unique_ptr<BlobDataHandle> CreateHandle(const std::string& uuid, |
106 void CreatePendingBlob(const std::string& uuid, | 145 BlobRegistryEntry* entry); |
107 const std::string& content_type, | |
108 const std::string& content_disposition); | |
109 | 146 |
110 // This includes resolving blob references in the builder. This will run the | 147 bool CanFinishBuilding(BlobRegistryEntry* entry); |
111 // callbacks given in RunOnConstructionComplete. | |
112 void CompletePendingBlob(const BlobDataBuilder& external_builder); | |
113 | 148 |
114 // This will run the callbacks given in RunOnConstructionComplete. | 149 void FinishBuilding(BlobRegistryEntry* entry); |
115 void CancelPendingBlob(const std::string& uuid, | 150 |
116 IPCBlobCreationCancelCode reason); | 151 void OnEnoughSizeForDependentCopies(const std::string& uuid, bool can_fit); |
152 | |
153 void OnDependentBlobFinished(const std::string& owning_blob_uuid, | |
154 bool construction_success, | |
155 IPCBlobCreationCancelCode reason); | |
156 | |
157 void ClearAndFreeMemory(const std::string& uuid, BlobRegistryEntry* entry); | |
117 | 158 |
118 void IncrementBlobRefCount(const std::string& uuid); | 159 void IncrementBlobRefCount(const std::string& uuid); |
119 void DecrementBlobRefCount(const std::string& uuid); | 160 void DecrementBlobRefCount(const std::string& uuid); |
120 | 161 |
121 // Methods called by BlobDataHandle: | 162 // Methods called by BlobDataHandle: |
122 // This will return an empty snapshot until the blob is complete. | 163 // This will return an empty snapshot until the blob is complete. |
123 // TODO(dmurph): After we make the snapshot method in BlobHandle private, then | 164 // TODO(dmurph): After we make the snapshot method in BlobHandle private, then |
124 // make this DCHECK on the blob not being complete. | 165 // make this DCHECK on the blob not being complete. |
125 std::unique_ptr<BlobDataSnapshot> CreateSnapshot(const std::string& uuid); | 166 std::unique_ptr<BlobDataSnapshot> CreateSnapshot(const std::string& uuid); |
126 bool IsBroken(const std::string& uuid) const; | 167 bool IsBroken(const std::string& uuid) const; |
127 bool IsBeingBuilt(const std::string& uuid) const; | 168 bool IsBeingBuilt(const std::string& uuid) const; |
128 // Runs |done| when construction completes, with true if it was successful, | 169 // 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 | 170 // and false if there was an error, which is reported in the second argument |
130 // of the callback. | 171 // of the callback. |
131 void RunOnConstructionComplete(const std::string& uuid, | 172 void RunOnConstructionComplete(const std::string& uuid, |
132 const BlobConstructedCallback& done); | 173 const BlobConstructedCallback& done); |
133 | 174 |
134 // Appends the given blob item to the blob builder. The new blob | 175 void UpdateItemsInRecents(BlobRegistryEntry* entry); |
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 | 176 |
144 // Allocates a shareable blob data item, with blob references resolved. If | 177 void PerformCopy(const BlobStorageRegistry::ItemCopyEntry& copy, |
145 // there isn't enough memory, then a nullptr is returned. | 178 InternalBlobData* source, |
146 scoped_refptr<ShareableBlobDataItem> AllocateShareableBlobDataItem( | 179 InternalBlobData* destination); |
147 const std::string& target_blob_uuid, | |
148 scoped_refptr<BlobDataItem> data_item); | |
149 | |
150 // Deconstructs the blob and appends it's contents to the target blob. Items | |
151 // are shared if possible, and copied if the given offset and length | |
152 // have to split an item. | |
153 bool AppendBlob(const std::string& target_blob_uuid, | |
154 const InternalBlobData& blob, | |
155 uint64_t offset, | |
156 uint64_t length, | |
157 InternalBlobData::Builder* target_blob_data); | |
158 | 180 |
159 BlobStorageRegistry registry_; | 181 BlobStorageRegistry registry_; |
160 | 182 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 | 183 |
166 DISALLOW_COPY_AND_ASSIGN(BlobStorageContext); | 184 DISALLOW_COPY_AND_ASSIGN(BlobStorageContext); |
167 }; | 185 }; |
168 | 186 |
169 } // namespace storage | 187 } // namespace storage |
170 | 188 |
171 #endif // STORAGE_BROWSER_BLOB_BLOB_STORAGE_CONTEXT_H_ | 189 #endif // STORAGE_BROWSER_BLOB_BLOB_STORAGE_CONTEXT_H_ |
OLD | NEW |