| 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 // This class facilitates moving memory from the renderer to the browser. |
| 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 { | 41 class STORAGE_EXPORT BlobAsyncBuilderHost { |
| 50 public: | 42 public: |
| 51 using RequestMemoryCallback = base::Callback<void( | 43 // One is expected to use std::move when calling this callback. |
| 52 std::unique_ptr<std::vector<storage::BlobItemBytesRequest>>, | 44 using RequestMemoryCallback = |
| 53 std::unique_ptr<std::vector<base::SharedMemoryHandle>>, | 45 base::Callback<void(std::vector<storage::BlobItemBytesRequest>, |
| 54 std::unique_ptr<std::vector<base::File>>)>; | 46 std::vector<base::SharedMemoryHandle>, |
| 47 std::vector<base::File>)>; |
| 48 |
| 55 BlobAsyncBuilderHost(); | 49 BlobAsyncBuilderHost(); |
| 56 ~BlobAsyncBuilderHost(); | 50 ~BlobAsyncBuilderHost(); |
| 57 | 51 |
| 58 // This registers the given blob internally and adds it to the storage. | 52 // This registers the given blob internally and adds it to the storage with a |
| 59 // Calling this method also guarentees that the referenced blobs are kept | 53 // refcount of 1. |completion_callback| is called synchronously or |
| 60 // alive for the duration of the construction of this blob. | 54 // asynchronously with: |
| 61 // We return | 55 // * INVALID_CONSTRUCTION_ARGUMENTS if we have invalid input arguments/data. |
| 62 // * BAD_IPC if we already have the blob registered or if we reference ourself | 56 // We treat this as a critical error, and don't bother registering the blob |
| 63 // in the referenced_blob_uuids. | 57 // in the BlobStorageContext. |
| 64 // * CANCEL_REFERENCED_BLOB_BROKEN if one of the referenced blobs is broken or | 58 // * 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 | 59 // doesn't exist. |
| 66 // REFERENCED_BLOB_BROKEN. | 60 // * DONE if we don't need any more data transported and we can clean up. |
| 67 // * DONE if we successfully registered the blob. | 61 void RegisterBlob(const std::string& uuid, |
| 68 BlobTransportResult RegisterBlobUUID( | 62 const std::string& content_type, |
| 69 const std::string& uuid, | 63 const std::string& content_disposition, |
| 70 const std::string& content_type, | 64 const std::vector<DataElement>& elements, |
| 71 const std::string& content_disposition, | 65 BlobStorageContext* context, |
| 72 const std::set<std::string>& referenced_blob_uuids, | 66 const RequestMemoryCallback& request_memory, |
| 73 BlobStorageContext* context); | 67 const BlobStatusCallback& completion_callback); |
| 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 | 68 |
| 94 // This is called when we have responses from the Renderer to our calls to | 69 // 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. | 70 // the request_memory callback above. See above for return value meaning. |
| 96 BlobTransportResult OnMemoryResponses( | 71 BlobStatus OnMemoryResponses( |
| 97 const std::string& uuid, | 72 const std::string& uuid, |
| 98 const std::vector<BlobItemBytesResponse>& responses, | 73 const std::vector<BlobItemBytesResponse>& responses, |
| 99 BlobStorageContext* context); | 74 BlobStorageContext* context); |
| 100 | 75 |
| 101 // This removes the BlobBuildingState from our map and flags the blob as | 76 // 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 | 77 // 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 | 78 // the blob, or from the DispatcherHost (Renderer). The blob MUST be being |
| 104 // built in this builder. | 79 // built in this builder. |
| 105 // Note: if the blob isn't in the context (renderer dereferenced it before we | 80 // 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. | 81 // finished constructing), then we don't bother touching the context. |
| 107 void CancelBuildingBlob(const std::string& uuid, | 82 void CancelBuildingBlob(const std::string& uuid, |
| 108 IPCBlobCreationCancelCode code, | 83 BlobStatus code, |
| 109 BlobStorageContext* context); | 84 BlobStorageContext* context); |
| 110 | 85 |
| 111 // This clears this object of pending construction. It also handles marking | 86 // 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 | 87 // 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 | 88 // 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. | 89 // by someone else if they still exist in the context. |
| 115 void CancelAll(BlobStorageContext* context); | 90 void CancelAll(BlobStorageContext* context); |
| 116 | 91 |
| 117 bool IsEmpty() const { return async_blob_map_.empty(); } | 92 bool IsEmpty() const { return async_blob_map_.empty(); } |
| 118 | 93 |
| 119 size_t blob_building_count() const { return async_blob_map_.size(); } | 94 size_t blob_building_count() const { return async_blob_map_.size(); } |
| 120 | 95 |
| 121 bool IsBeingBuilt(const std::string& key) const { | 96 bool IsBeingBuilt(const std::string& key) const { |
| 122 return async_blob_map_.find(key) != async_blob_map_.end(); | 97 return async_blob_map_.find(key) != async_blob_map_.end(); |
| 123 } | 98 } |
| 124 | 99 |
| 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: | 100 private: |
| 135 struct BlobBuildingState { | 101 struct BlobBuildingState { |
| 136 // |refernced_blob_handles| should be all handles generated from the set | 102 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(); | 103 ~BlobBuildingState(); |
| 143 | 104 |
| 105 IPCBlobItemRequestStrategy strategy = IPCBlobItemRequestStrategy::UNKNOWN; |
| 144 BlobAsyncTransportRequestBuilder request_builder; | 106 BlobAsyncTransportRequestBuilder request_builder; |
| 145 BlobDataBuilder data_builder; | 107 BlobDataBuilder data_builder; |
| 146 std::vector<bool> request_received; | 108 std::vector<bool> request_received; |
| 109 size_t num_fulfilled_requests = 0; |
| 110 |
| 111 RequestMemoryCallback request_memory_callback; |
| 112 BlobStatusCallback completion_callback; |
| 113 |
| 114 // Used by shared memory strategy. |
| 147 size_t next_request = 0; | 115 size_t next_request = 0; |
| 148 size_t num_fulfilled_requests = 0; | |
| 149 std::unique_ptr<base::SharedMemory> shared_memory_block; | 116 std::unique_ptr<base::SharedMemory> shared_memory_block; |
| 150 // This is the number of requests that have been sent to populate the above | 117 // 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 | 118 // shared data. We won't ask for more data in shared memory until all |
| 152 // requests have been responded to. | 119 // requests have been responded to. |
| 153 size_t num_shared_memory_requests = 0; | 120 size_t num_shared_memory_requests = 0; |
| 154 // Only relevant if num_shared_memory_requests is > 0 | 121 // Only relevant if num_shared_memory_requests is > 0 |
| 155 size_t current_shared_memory_handle_index = 0; | 122 size_t current_shared_memory_handle_index = 0; |
| 156 | 123 |
| 157 // We save these to double check that the RegisterBlob and StartBuildingBlob | 124 // Used by file strategy. |
| 158 // messages are in sync. | 125 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 }; | 126 }; |
| 172 | 127 |
| 173 typedef std::map<std::string, std::unique_ptr<BlobBuildingState>> | 128 typedef std::map<std::string, std::unique_ptr<BlobBuildingState>> |
| 174 AsyncBlobMap; | 129 AsyncBlobMap; |
| 175 | 130 |
| 131 BlobStatus StartRequests( |
| 132 const std::string& uuid, |
| 133 BlobBuildingState* state, |
| 134 BlobStorageContext* context, |
| 135 std::vector<BlobMemoryController::FileCreationInfo> file_infos); |
| 136 |
| 137 void OnCanStartBuildingBlob( |
| 138 const std::string& uuid, |
| 139 base::WeakPtr<BlobStorageContext> context, |
| 140 BlobStatus status, |
| 141 std::vector<BlobMemoryController::FileCreationInfo> file_infos); |
| 142 |
| 143 void SendIPCRequests(BlobBuildingState* state, BlobStorageContext* context); |
| 144 BlobStatus OnIPCResponses(const std::string& uuid, |
| 145 BlobBuildingState* state, |
| 146 const std::vector<BlobItemBytesResponse>& responses, |
| 147 BlobStorageContext* context); |
| 148 |
| 176 // This is the 'main loop' of our memory requests to the renderer. | 149 // This is the 'main loop' of our memory requests to the renderer. |
| 177 BlobTransportResult ContinueBlobMemoryRequests(const std::string& uuid, | 150 BlobStatus ContinueSharedMemoryRequests(const std::string& uuid, |
| 178 BlobStorageContext* context); | 151 BlobBuildingState* state, |
| 152 BlobStorageContext* context); |
| 179 | 153 |
| 180 // This is our callback for when we want to finish the blob and we're waiting | 154 BlobStatus OnSharedMemoryResponses( |
| 181 // for blobs we reference to be built. When the last callback occurs, we | 155 const std::string& uuid, |
| 182 // complete the blob and erase our internal state. | 156 BlobBuildingState* state, |
| 183 void ReferencedBlobFinished(const std::string& uuid, | 157 const std::vector<BlobItemBytesResponse>& responses, |
| 184 base::WeakPtr<BlobStorageContext> context, | 158 BlobStorageContext* context); |
| 185 bool construction_success, | 159 |
| 186 IPCBlobCreationCancelCode reason); | 160 void SendFileRequests( |
| 161 BlobBuildingState* state, |
| 162 BlobStorageContext* context, |
| 163 std::vector<BlobMemoryController::FileCreationInfo> files); |
| 164 |
| 165 BlobStatus OnFileResponses( |
| 166 const std::string& uuid, |
| 167 BlobBuildingState* state, |
| 168 const std::vector<BlobItemBytesResponse>& responses, |
| 169 BlobStorageContext* context); |
| 187 | 170 |
| 188 // This finishes creating the blob in the context, decrements blob references | 171 // This finishes creating the blob in the context, decrements blob references |
| 189 // that we were holding during construction, and erases our state. | 172 // that we were holding during construction, and erases our state. |
| 190 void FinishBuildingBlob(BlobBuildingState* state, | 173 void FinishBuildingBlob(BlobBuildingState* state, |
| 191 BlobStorageContext* context); | 174 BlobStorageContext* context); |
| 192 | 175 |
| 193 AsyncBlobMap async_blob_map_; | 176 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_; | 177 base::WeakPtrFactory<BlobAsyncBuilderHost> ptr_factory_; |
| 201 | 178 |
| 202 DISALLOW_COPY_AND_ASSIGN(BlobAsyncBuilderHost); | 179 DISALLOW_COPY_AND_ASSIGN(BlobAsyncBuilderHost); |
| 203 }; | 180 }; |
| 204 | 181 |
| 205 } // namespace storage | 182 } // namespace storage |
| 206 #endif // STORAGE_BROWSER_BLOB_BLOB_ASYNC_BUILDER_HOST_H_ | 183 #endif // STORAGE_BROWSER_BLOB_BLOB_ASYNC_BUILDER_HOST_H_ |
| OLD | NEW |