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