OLD | NEW |
---|---|
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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_ASYNC_BUILDER_HOST_H_ | 5 #ifndef STORAGE_BROWSER_BLOB_BLOB_ASYNC_BUILDER_HOST_H_ |
6 #define STORAGE_BROWSER_BLOB_BLOB_ASYNC_BUILDER_HOST_H_ | 6 #define STORAGE_BROWSER_BLOB_BLOB_ASYNC_BUILDER_HOST_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 <set> | 13 #include <set> |
14 #include <string> | 14 #include <string> |
15 #include <vector> | 15 #include <vector> |
16 | 16 |
17 #include "base/callback.h" | 17 #include "base/callback.h" |
18 #include "base/files/file.h" | 18 #include "base/files/file.h" |
19 #include "base/macros.h" | 19 #include "base/macros.h" |
20 #include "base/memory/ref_counted.h" | 20 #include "base/memory/ref_counted.h" |
21 #include "base/memory/shared_memory_handle.h" | 21 #include "base/memory/shared_memory_handle.h" |
22 #include "base/memory/weak_ptr.h" | 22 #include "base/memory/weak_ptr.h" |
23 #include "storage/browser/blob/blob_async_transport_request_builder.h" | 23 #include "storage/browser/blob/blob_async_transport_request_builder.h" |
24 #include "storage/browser/blob/blob_data_builder.h" | 24 #include "storage/browser/blob/blob_data_builder.h" |
25 #include "storage/browser/blob/blob_transport_result.h" | 25 #include "storage/browser/blob/blob_memory_controller.h" |
26 #include "storage/browser/storage_browser_export.h" | 26 #include "storage/browser/storage_browser_export.h" |
27 #include "storage/common/blob_storage/blob_item_bytes_request.h" | 27 #include "storage/common/blob_storage/blob_item_bytes_request.h" |
28 #include "storage/common/blob_storage/blob_item_bytes_response.h" | 28 #include "storage/common/blob_storage/blob_item_bytes_response.h" |
29 #include "storage/common/blob_storage/blob_storage_constants.h" | 29 #include "storage/common/blob_storage/blob_storage_constants.h" |
30 #include "storage/common/data_element.h" | 30 #include "storage/common/data_element.h" |
31 | 31 |
32 namespace base { | 32 namespace base { |
33 class SharedMemory; | 33 class SharedMemory; |
34 } | 34 } |
35 | 35 |
36 namespace storage { | 36 namespace storage { |
37 class BlobDataHandle; | 37 class BlobDataHandle; |
38 class BlobStorageContext; | 38 class BlobStorageContext; |
39 | 39 |
40 // This class | 40 // The responsibility of this class is to handle all transportation of data |
41 // * holds all blobs that are currently being built asynchronously for a child | 41 // between the renderer and the browser process, and correctly construct the |
42 // resulting blob using the BlobStorageContext. We: | |
43 // * hold all blobs that are currently being built asynchronously for a child | |
michaeln
2016/08/15 22:44:43
I'm not sure the detailed bullets add value. They
dmurph
2016/08/19 00:18:32
Done.
| |
42 // process, | 44 // process, |
43 // * sends memory requests through the given callback in |StartBuildingBlob|, | 45 // * sends memory requests through the given callback in |RegisterBlob|, |
44 // and uses the BlobTransportResult return value to signify other results, | 46 // and uses the BlobStatus return value to signify other results, |
michaeln
2016/08/15 22:44:43
i don't understand the comment on this line?
dmurph
2016/08/19 00:18:32
Done.
| |
45 // * includes all logic for deciding which async transport strategy to use, and | 47 // * includes all logic for deciding which async transport strategy to use, and |
46 // * handles all blob construction communication with the BlobStorageContext. | 48 // * handles all blob construction communication with the BlobStorageContext. |
47 // The method |CancelAll| must be called by the consumer, it is not called on | 49 // The method |CancelAll| must be called by the consumer, it is not called on |
48 // destruction. | 50 // destruction. |
49 class STORAGE_EXPORT BlobAsyncBuilderHost { | 51 class STORAGE_EXPORT BlobAsyncBuilderHost { |
michaeln
2016/08/15 22:44:43
Food for thought, this talks to the BlobTransportC
dmurph
2016/08/19 00:18:32
Hmmmmmmmmmm
| |
50 public: | 52 public: |
51 using RequestMemoryCallback = base::Callback<void( | 53 // One is expected to use std::move when calling this callback. |
52 std::unique_ptr<std::vector<storage::BlobItemBytesRequest>>, | 54 using RequestMemoryCallback = |
53 std::unique_ptr<std::vector<base::SharedMemoryHandle>>, | 55 base::Callback<void(std::vector<storage::BlobItemBytesRequest>, |
54 std::unique_ptr<std::vector<base::File>>)>; | 56 std::vector<base::SharedMemoryHandle>, |
57 std::vector<base::File>)>; | |
58 | |
55 BlobAsyncBuilderHost(); | 59 BlobAsyncBuilderHost(); |
56 ~BlobAsyncBuilderHost(); | 60 ~BlobAsyncBuilderHost(); |
57 | 61 |
58 // This registers the given blob internally and adds it to the storage. | 62 // This registers the given blob internally and adds it to the storage. The |
59 // Calling this method also guarentees that the referenced blobs are kept | 63 // |status_callback| is used when we return PENDING, and reports an |
60 // alive for the duration of the construction of this blob. | 64 // asynchronous error of if we're done transporting data. This function |
61 // We return | 65 // creates the blob with a default reference of 1. |
62 // * BAD_IPC if we already have the blob registered or if we reference ourself | 66 // This function returns: |
63 // in the referenced_blob_uuids. | 67 // * INVALID_CONSTRUCTION_ARGUMENTS if we have invalid input arguments/data. |
64 // * CANCEL_REFERENCED_BLOB_BROKEN if one of the referenced blobs is broken or | 68 // We treat this as a critical error, and don't bother registering the blob |
65 // doesn't exist. We store the blob in the context as broken with code | 69 // in the BlobStorageContext. |
66 // REFERENCED_BLOB_BROKEN. | 70 // * REFERENCED_BLOB_BROKEN if one of the referenced blobs is broken or |
67 // * DONE if we successfully registered the blob. | 71 // doesn't exist. |
68 BlobTransportResult RegisterBlobUUID( | 72 // * PENDING_MEMORY_QUOTA if we're waiting for memory quota, |
Marijn Kruisselbrink
2016/08/05 23:23:54
there's only one PENDING status now, right?
dmurph
2016/08/19 00:18:32
Done.
| |
69 const std::string& uuid, | 73 // * PENDING_DATA_POPULATION if we are waiting for responses from the |
70 const std::string& content_type, | 74 // renderer, and |
71 const std::string& content_disposition, | 75 // * DONE if the BlobStorageContext doesn't need any data transported and we |
72 const std::set<std::string>& referenced_blob_uuids, | 76 // can clean up. |
73 BlobStorageContext* context); | 77 BlobStatus RegisterBlob(const std::string& uuid, |
74 | 78 const std::string& content_type, |
75 // This method begins the construction of the blob given the descriptions. The | 79 const std::string& content_disposition, |
76 // blob uuid MUST be building in this object. | 80 const std::vector<DataElement>& elements, |
77 // When we return: | 81 BlobStorageContext* context, |
78 // * DONE: The blob is finished transfering right away, and is now | 82 const RequestMemoryCallback& request_memory, |
79 // successfully saved in the context. | 83 const BlobStatusCallback& status_callback); |
80 // * PENDING_RESPONSES: The async builder host is waiting for responses from | |
81 // the renderer. It has called |request_memory| for these responses. | |
82 // * CANCEL_*: We have to cancel the blob construction. This function clears | |
83 // the blob's internal state and marks the blob as broken in the context | |
84 // before returning. | |
85 // * BAD_IPC: The arguments were invalid/bad. This marks the blob as broken in | |
86 // the context before returning. | |
87 BlobTransportResult StartBuildingBlob( | |
88 const std::string& uuid, | |
89 const std::vector<DataElement>& elements, | |
90 size_t memory_available, | |
91 BlobStorageContext* context, | |
92 const RequestMemoryCallback& request_memory); | |
93 | 84 |
94 // This is called when we have responses from the Renderer to our calls to | 85 // This is called when we have responses from the Renderer to our calls to |
95 // the request_memory callback above. See above for return value meaning. | 86 // the request_memory callback above. See above for return value meaning. |
96 BlobTransportResult OnMemoryResponses( | 87 BlobStatus OnMemoryResponses( |
97 const std::string& uuid, | 88 const std::string& uuid, |
98 const std::vector<BlobItemBytesResponse>& responses, | 89 const std::vector<BlobItemBytesResponse>& responses, |
99 BlobStorageContext* context); | 90 BlobStorageContext* context); |
100 | 91 |
101 // This removes the BlobBuildingState from our map and flags the blob as | 92 // This removes the BlobBuildingState from our map and flags the blob as |
102 // broken in the context. This can be called both from our own logic to cancel | 93 // broken in the context. This can be called both from our own logic to cancel |
103 // the blob, or from the DispatcherHost (Renderer). The blob MUST be being | 94 // the blob, or from the DispatcherHost (Renderer). The blob MUST be being |
104 // built in this builder. | 95 // built in this builder. |
105 // Note: if the blob isn't in the context (renderer dereferenced it before we | 96 // Note: if the blob isn't in the context (renderer dereferenced it before we |
106 // finished constructing), then we don't bother touching the context. | 97 // finished constructing), then we don't bother touching the context. |
107 void CancelBuildingBlob(const std::string& uuid, | 98 void CancelBuildingBlob(const std::string& uuid, |
108 IPCBlobCreationCancelCode code, | 99 BlobStatus code, |
109 BlobStorageContext* context); | 100 BlobStorageContext* context); |
110 | 101 |
111 // This clears this object of pending construction. It also handles marking | 102 // This clears this object of pending construction. It also handles marking |
112 // blobs that haven't been fully constructed as broken in the context if there | 103 // blobs that haven't been fully constructed as broken in the context if there |
113 // are any references being held by anyone. We know that they're being used | 104 // are any references being held by anyone. We know that they're being used |
114 // by someone else if they still exist in the context. | 105 // by someone else if they still exist in the context. |
115 void CancelAll(BlobStorageContext* context); | 106 void CancelAll(BlobStorageContext* context); |
116 | 107 |
117 bool IsEmpty() const { return async_blob_map_.empty(); } | 108 bool IsEmpty() const { return async_blob_map_.empty(); } |
118 | 109 |
119 size_t blob_building_count() const { return async_blob_map_.size(); } | 110 size_t blob_building_count() const { return async_blob_map_.size(); } |
120 | 111 |
121 bool IsBeingBuilt(const std::string& key) const { | 112 bool IsBeingBuilt(const std::string& key) const { |
122 return async_blob_map_.find(key) != async_blob_map_.end(); | 113 return async_blob_map_.find(key) != async_blob_map_.end(); |
123 } | 114 } |
124 | 115 |
125 // For testing use only. Must be called before StartBuildingBlob. | |
126 void SetMemoryConstantsForTesting(size_t max_ipc_memory_size, | |
127 size_t max_shared_memory_size, | |
128 uint64_t max_file_size) { | |
129 max_ipc_memory_size_ = max_ipc_memory_size; | |
130 max_shared_memory_size_ = max_shared_memory_size; | |
131 max_file_size_ = max_file_size; | |
132 } | |
133 | |
134 private: | 116 private: |
135 struct BlobBuildingState { | 117 struct BlobBuildingState { |
136 // |refernced_blob_handles| should be all handles generated from the set | 118 explicit BlobBuildingState(const std::string& uuid); |
137 // of |refernced_blob_uuids|. | |
138 BlobBuildingState( | |
139 const std::string& uuid, | |
140 std::set<std::string> referenced_blob_uuids, | |
141 std::vector<std::unique_ptr<BlobDataHandle>>* referenced_blob_handles); | |
142 ~BlobBuildingState(); | 119 ~BlobBuildingState(); |
143 | 120 |
121 IPCBlobItemRequestStrategy strategy = IPCBlobItemRequestStrategy::UNKNOWN; | |
144 BlobAsyncTransportRequestBuilder request_builder; | 122 BlobAsyncTransportRequestBuilder request_builder; |
145 BlobDataBuilder data_builder; | 123 BlobDataBuilder data_builder; |
146 std::vector<bool> request_received; | 124 std::vector<bool> request_received; |
125 size_t num_fulfilled_requests = 0; | |
126 | |
127 RequestMemoryCallback request_memory_callback; | |
128 BlobStatusCallback status_callback; | |
129 | |
130 // Used by shared memory strategy. | |
147 size_t next_request = 0; | 131 size_t next_request = 0; |
148 size_t num_fulfilled_requests = 0; | |
149 std::unique_ptr<base::SharedMemory> shared_memory_block; | 132 std::unique_ptr<base::SharedMemory> shared_memory_block; |
150 // This is the number of requests that have been sent to populate the above | 133 // This is the number of requests that have been sent to populate the above |
151 // shared data. We won't ask for more data in shared memory until all | 134 // shared data. We won't ask for more data in shared memory until all |
152 // requests have been responded to. | 135 // requests have been responded to. |
153 size_t num_shared_memory_requests = 0; | 136 size_t num_shared_memory_requests = 0; |
154 // Only relevant if num_shared_memory_requests is > 0 | 137 // Only relevant if num_shared_memory_requests is > 0 |
155 size_t current_shared_memory_handle_index = 0; | 138 size_t current_shared_memory_handle_index = 0; |
156 | 139 |
157 // We save these to double check that the RegisterBlob and StartBuildingBlob | 140 // Used by file strategy. |
158 // messages are in sync. | 141 std::vector<scoped_refptr<ShareableFileReference>> files; |
159 std::set<std::string> referenced_blob_uuids; | |
160 // These are the blobs that are referenced in the newly constructed blob. | |
161 // We use these to make sure they stay alive while we create the new blob, | |
162 // and to wait until any blobs that are not done building are fully | |
163 // constructed. | |
164 std::vector<std::unique_ptr<BlobDataHandle>> referenced_blob_handles; | |
165 | |
166 // These are the number of blobs we're waiting for before we can start | |
167 // building. | |
168 size_t num_referenced_blobs_building = 0; | |
169 | |
170 BlobAsyncBuilderHost::RequestMemoryCallback request_memory_callback; | |
171 }; | 142 }; |
172 | 143 |
173 typedef std::map<std::string, std::unique_ptr<BlobBuildingState>> | 144 typedef std::map<std::string, std::unique_ptr<BlobBuildingState>> |
174 AsyncBlobMap; | 145 AsyncBlobMap; |
175 | 146 |
147 BlobStatus StartRequests( | |
148 const std::string& uuid, | |
149 BlobBuildingState* state, | |
150 BlobStorageContext* context, | |
151 std::vector<BlobMemoryController::FileCreationInfo> file_infos); | |
152 | |
153 void OnCanStartBuildingBlob( | |
154 const std::string& uuid, | |
155 base::WeakPtr<BlobStorageContext> context, | |
156 BlobStatus status, | |
157 std::vector<BlobMemoryController::FileCreationInfo> file_infos); | |
158 | |
159 void SendIPCRequests(BlobBuildingState* state, BlobStorageContext* context); | |
160 BlobStatus OnIPCResponses(const std::string& uuid, | |
161 BlobBuildingState* state, | |
162 const std::vector<BlobItemBytesResponse>& responses, | |
163 BlobStorageContext* context); | |
164 | |
176 // This is the 'main loop' of our memory requests to the renderer. | 165 // This is the 'main loop' of our memory requests to the renderer. |
177 BlobTransportResult ContinueBlobMemoryRequests(const std::string& uuid, | 166 BlobStatus ContinueSharedMemoryRequests(const std::string& uuid, |
178 BlobStorageContext* context); | 167 BlobBuildingState* state, |
168 BlobStorageContext* context); | |
179 | 169 |
180 // This is our callback for when we want to finish the blob and we're waiting | 170 BlobStatus OnSharedMemoryResponses( |
181 // for blobs we reference to be built. When the last callback occurs, we | 171 const std::string& uuid, |
182 // complete the blob and erase our internal state. | 172 BlobBuildingState* state, |
183 void ReferencedBlobFinished(const std::string& uuid, | 173 const std::vector<BlobItemBytesResponse>& responses, |
184 base::WeakPtr<BlobStorageContext> context, | 174 BlobStorageContext* context); |
185 bool construction_success, | 175 |
186 IPCBlobCreationCancelCode reason); | 176 void SendFileRequests( |
177 BlobBuildingState* state, | |
178 BlobStorageContext* context, | |
179 std::vector<BlobMemoryController::FileCreationInfo> files); | |
180 | |
181 BlobStatus OnFileResponses( | |
182 const std::string& uuid, | |
183 BlobBuildingState* state, | |
184 const std::vector<BlobItemBytesResponse>& responses, | |
185 BlobStorageContext* context); | |
187 | 186 |
188 // This finishes creating the blob in the context, decrements blob references | 187 // This finishes creating the blob in the context, decrements blob references |
189 // that we were holding during construction, and erases our state. | 188 // that we were holding during construction, and erases our state. |
190 void FinishBuildingBlob(BlobBuildingState* state, | 189 void FinishBuildingBlob(BlobBuildingState* state, |
191 BlobStorageContext* context); | 190 BlobStorageContext* context); |
192 | 191 |
193 AsyncBlobMap async_blob_map_; | 192 AsyncBlobMap async_blob_map_; |
194 | |
195 // Here for testing. | |
196 size_t max_ipc_memory_size_ = kBlobStorageIPCThresholdBytes; | |
197 size_t max_shared_memory_size_ = kBlobStorageMaxSharedMemoryBytes; | |
198 uint64_t max_file_size_ = kBlobStorageMaxFileSizeBytes; | |
199 | |
200 base::WeakPtrFactory<BlobAsyncBuilderHost> ptr_factory_; | 193 base::WeakPtrFactory<BlobAsyncBuilderHost> ptr_factory_; |
201 | 194 |
202 DISALLOW_COPY_AND_ASSIGN(BlobAsyncBuilderHost); | 195 DISALLOW_COPY_AND_ASSIGN(BlobAsyncBuilderHost); |
203 }; | 196 }; |
204 | 197 |
205 } // namespace storage | 198 } // namespace storage |
206 #endif // STORAGE_BROWSER_BLOB_BLOB_ASYNC_BUILDER_HOST_H_ | 199 #endif // STORAGE_BROWSER_BLOB_BLOB_ASYNC_BUILDER_HOST_H_ |
OLD | NEW |