OLD | NEW |
| (Empty) |
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 | |
3 // found in the LICENSE file. | |
4 | |
5 #ifndef STORAGE_BROWSER_BLOB_BLOB_ASYNC_BUILDER_HOST_H_ | |
6 #define STORAGE_BROWSER_BLOB_BLOB_ASYNC_BUILDER_HOST_H_ | |
7 | |
8 #include <stddef.h> | |
9 #include <stdint.h> | |
10 | |
11 #include <map> | |
12 #include <memory> | |
13 #include <set> | |
14 #include <string> | |
15 #include <vector> | |
16 | |
17 #include "base/callback.h" | |
18 #include "base/files/file.h" | |
19 #include "base/macros.h" | |
20 #include "base/memory/ref_counted.h" | |
21 #include "base/memory/shared_memory_handle.h" | |
22 #include "base/memory/weak_ptr.h" | |
23 #include "storage/browser/blob/blob_async_transport_request_builder.h" | |
24 #include "storage/browser/blob/blob_data_builder.h" | |
25 #include "storage/browser/blob/blob_transport_result.h" | |
26 #include "storage/browser/storage_browser_export.h" | |
27 #include "storage/common/blob_storage/blob_item_bytes_request.h" | |
28 #include "storage/common/blob_storage/blob_item_bytes_response.h" | |
29 #include "storage/common/blob_storage/blob_storage_constants.h" | |
30 #include "storage/common/data_element.h" | |
31 | |
32 namespace base { | |
33 class SharedMemory; | |
34 } | |
35 | |
36 namespace storage { | |
37 class BlobDataHandle; | |
38 class BlobStorageContext; | |
39 | |
40 // This class | |
41 // * holds all blobs that are currently being built asynchronously for a child | |
42 // process, | |
43 // * sends memory requests through the given callback in |StartBuildingBlob|, | |
44 // and uses the BlobTransportResult return value to signify other results, | |
45 // * includes all logic for deciding which async transport strategy to use, and | |
46 // * handles all blob construction communication with the BlobStorageContext. | |
47 // The method |CancelAll| must be called by the consumer, it is not called on | |
48 // destruction. | |
49 class STORAGE_EXPORT BlobAsyncBuilderHost { | |
50 public: | |
51 using RequestMemoryCallback = base::Callback<void( | |
52 std::unique_ptr<std::vector<storage::BlobItemBytesRequest>>, | |
53 std::unique_ptr<std::vector<base::SharedMemoryHandle>>, | |
54 std::unique_ptr<std::vector<base::File>>)>; | |
55 BlobAsyncBuilderHost(); | |
56 ~BlobAsyncBuilderHost(); | |
57 | |
58 // This registers the given blob internally and adds it to the storage. | |
59 // Calling this method also guarentees that the referenced blobs are kept | |
60 // alive for the duration of the construction of this blob. | |
61 // We return | |
62 // * BAD_IPC if we already have the blob registered or if we reference ourself | |
63 // in the referenced_blob_uuids. | |
64 // * CANCEL_REFERENCED_BLOB_BROKEN if one of the referenced blobs is broken or | |
65 // doesn't exist. We store the blob in the context as broken with code | |
66 // REFERENCED_BLOB_BROKEN. | |
67 // * DONE if we successfully registered the blob. | |
68 BlobTransportResult RegisterBlobUUID( | |
69 const std::string& uuid, | |
70 const std::string& content_type, | |
71 const std::string& content_disposition, | |
72 const std::set<std::string>& referenced_blob_uuids, | |
73 BlobStorageContext* context); | |
74 | |
75 // This method begins the construction of the blob given the descriptions. The | |
76 // blob uuid MUST be building in this object. | |
77 // When we return: | |
78 // * DONE: The blob is finished transfering right away, and is now | |
79 // successfully saved in the context. | |
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 | |
94 // 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. | |
96 BlobTransportResult OnMemoryResponses( | |
97 const std::string& uuid, | |
98 const std::vector<BlobItemBytesResponse>& responses, | |
99 BlobStorageContext* context); | |
100 | |
101 // 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 | |
103 // the blob, or from the DispatcherHost (Renderer). The blob MUST be being | |
104 // built in this builder. | |
105 // 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. | |
107 void CancelBuildingBlob(const std::string& uuid, | |
108 IPCBlobCreationCancelCode code, | |
109 BlobStorageContext* context); | |
110 | |
111 // 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 | |
113 // 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. | |
115 void CancelAll(BlobStorageContext* context); | |
116 | |
117 bool IsEmpty() const { return async_blob_map_.empty(); } | |
118 | |
119 size_t blob_building_count() const { return async_blob_map_.size(); } | |
120 | |
121 bool IsBeingBuilt(const std::string& key) const { | |
122 return async_blob_map_.find(key) != async_blob_map_.end(); | |
123 } | |
124 | |
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: | |
135 struct BlobBuildingState { | |
136 // |refernced_blob_handles| should be all handles generated from the set | |
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(); | |
143 | |
144 BlobAsyncTransportRequestBuilder request_builder; | |
145 BlobDataBuilder data_builder; | |
146 std::vector<bool> request_received; | |
147 size_t next_request = 0; | |
148 size_t num_fulfilled_requests = 0; | |
149 std::unique_ptr<base::SharedMemory> shared_memory_block; | |
150 // 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 | |
152 // requests have been responded to. | |
153 size_t num_shared_memory_requests = 0; | |
154 // Only relevant if num_shared_memory_requests is > 0 | |
155 size_t current_shared_memory_handle_index = 0; | |
156 | |
157 // We save these to double check that the RegisterBlob and StartBuildingBlob | |
158 // messages are in sync. | |
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 }; | |
172 | |
173 typedef std::map<std::string, std::unique_ptr<BlobBuildingState>> | |
174 AsyncBlobMap; | |
175 | |
176 // This is the 'main loop' of our memory requests to the renderer. | |
177 BlobTransportResult ContinueBlobMemoryRequests(const std::string& uuid, | |
178 BlobStorageContext* context); | |
179 | |
180 // This is our callback for when we want to finish the blob and we're waiting | |
181 // for blobs we reference to be built. When the last callback occurs, we | |
182 // complete the blob and erase our internal state. | |
183 void ReferencedBlobFinished(const std::string& uuid, | |
184 base::WeakPtr<BlobStorageContext> context, | |
185 bool construction_success, | |
186 IPCBlobCreationCancelCode reason); | |
187 | |
188 // This finishes creating the blob in the context, decrements blob references | |
189 // that we were holding during construction, and erases our state. | |
190 void FinishBuildingBlob(BlobBuildingState* state, | |
191 BlobStorageContext* context); | |
192 | |
193 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_; | |
201 | |
202 DISALLOW_COPY_AND_ASSIGN(BlobAsyncBuilderHost); | |
203 }; | |
204 | |
205 } // namespace storage | |
206 #endif // STORAGE_BROWSER_BLOB_BLOB_ASYNC_BUILDER_HOST_H_ | |
OLD | NEW |