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_TRANSPORT_HOST_H_ |
| 6 #define STORAGE_BROWSER_BLOB_BLOB_TRANSPORT_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_data_builder.h" |
| 24 #include "storage/browser/blob/blob_memory_controller.h" |
| 25 #include "storage/browser/blob/blob_transport_request_builder.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 facilitates moving memory from the renderer to the browser. |
| 41 class STORAGE_EXPORT BlobTransportHost { |
| 42 public: |
| 43 // One is expected to use std::move when calling this callback. |
| 44 using RequestMemoryCallback = |
| 45 base::Callback<void(std::vector<storage::BlobItemBytesRequest>, |
| 46 std::vector<base::SharedMemoryHandle>, |
| 47 std::vector<base::File>)>; |
| 48 |
| 49 BlobTransportHost(); |
| 50 ~BlobTransportHost(); |
| 51 |
| 52 // This registers the given blob internally and adds it to the storage with a |
| 53 // refcount of 1. |completion_callback| is called synchronously or |
| 54 // asynchronously with: |
| 55 // * INVALID_CONSTRUCTION_ARGUMENTS if we have invalid input arguments/data. |
| 56 // * REFERENCED_BLOB_BROKEN if one of the referenced blobs is broken or |
| 57 // doesn't exist. |
| 58 // * DONE if we don't need any more data transported and we can clean up. |
| 59 // Returns a blob handle that is never null. |
| 60 std::unique_ptr<BlobDataHandle> StartBuildingBlob( |
| 61 const std::string& uuid, |
| 62 const std::string& content_type, |
| 63 const std::string& content_disposition, |
| 64 const std::vector<DataElement>& elements, |
| 65 BlobStorageContext* context, |
| 66 const RequestMemoryCallback& request_memory, |
| 67 const BlobStatusCallback& completion_callback); |
| 68 |
| 69 // This is called when we have responses from the Renderer to our calls to |
| 70 // the request_memory callback above. The callbacks given in StartBuildingBlob |
| 71 // will be used to request for more memory or signal completion. |
| 72 // Note: The uuid must be being built in this host (IsBeingBuilt). |
| 73 void OnMemoryResponses(const std::string& uuid, |
| 74 const std::vector<BlobItemBytesResponse>& responses, |
| 75 BlobStorageContext* context); |
| 76 |
| 77 // This removes the TransportState from our map and flags the blob as broken |
| 78 // in the context. This can be called both from our own logic to cancel the |
| 79 // blob, or from the DispatcherHost (Renderer). The blob MUST be being built |
| 80 // in this builder. This also calls the |completion_callback|. |
| 81 // Note: if the blob isn't in the context (renderer dereferenced it before we |
| 82 // finished constructing), then we don't bother touching the context. |
| 83 void CancelBuildingBlob(const std::string& uuid, |
| 84 BlobStatus code, |
| 85 BlobStorageContext* context); |
| 86 |
| 87 // This clears this object of pending construction and does NOT call transport |
| 88 // complete callbacks. |
| 89 void CancelAll(BlobStorageContext* context); |
| 90 |
| 91 bool IsEmpty() const { return async_blob_map_.empty(); } |
| 92 |
| 93 size_t blob_building_count() const { return async_blob_map_.size(); } |
| 94 |
| 95 bool IsBeingBuilt(const std::string& key) const { |
| 96 return async_blob_map_.find(key) != async_blob_map_.end(); |
| 97 } |
| 98 |
| 99 private: |
| 100 struct TransportState { |
| 101 TransportState(const std::string& uuid, |
| 102 const std::string& content_type, |
| 103 const std::string& content_disposition, |
| 104 RequestMemoryCallback request_memory_callback, |
| 105 BlobStatusCallback completion_callback); |
| 106 TransportState(TransportState&&); |
| 107 TransportState& operator=(TransportState&&); |
| 108 DISALLOW_COPY_AND_ASSIGN(TransportState); |
| 109 ~TransportState(); |
| 110 |
| 111 IPCBlobItemRequestStrategy strategy = IPCBlobItemRequestStrategy::UNKNOWN; |
| 112 BlobTransportRequestBuilder request_builder; |
| 113 BlobDataBuilder data_builder; |
| 114 std::vector<bool> request_received; |
| 115 size_t num_fulfilled_requests = 0; |
| 116 |
| 117 RequestMemoryCallback request_memory_callback; |
| 118 BlobStatusCallback completion_callback; |
| 119 |
| 120 // Used by shared memory strategy. |
| 121 size_t next_request = 0; |
| 122 std::unique_ptr<base::SharedMemory> shared_memory_block; |
| 123 // This is the number of requests that have been sent to populate the above |
| 124 // shared data. We won't ask for more data in shared memory until all |
| 125 // requests have been responded to. |
| 126 size_t num_shared_memory_requests = 0; |
| 127 // Only relevant if num_shared_memory_requests is > 0 |
| 128 size_t current_shared_memory_handle_index = 0; |
| 129 |
| 130 // Used by file strategy. |
| 131 std::vector<scoped_refptr<ShareableFileReference>> files; |
| 132 }; |
| 133 |
| 134 typedef std::unordered_map<std::string, TransportState> AsyncBlobMap; |
| 135 |
| 136 void StartRequests( |
| 137 const std::string& uuid, |
| 138 TransportState* state, |
| 139 BlobStorageContext* context, |
| 140 std::vector<BlobMemoryController::FileCreationInfo> file_infos); |
| 141 |
| 142 void OnReadyForTransport( |
| 143 const std::string& uuid, |
| 144 base::WeakPtr<BlobStorageContext> context, |
| 145 BlobStatus status, |
| 146 std::vector<BlobMemoryController::FileCreationInfo> file_infos); |
| 147 |
| 148 void SendIPCRequests(TransportState* state, BlobStorageContext* context); |
| 149 void OnIPCResponses(const std::string& uuid, |
| 150 TransportState* state, |
| 151 const std::vector<BlobItemBytesResponse>& responses, |
| 152 BlobStorageContext* context); |
| 153 |
| 154 // This is the 'main loop' of our memory requests to the renderer. |
| 155 void ContinueSharedMemoryRequests(const std::string& uuid, |
| 156 TransportState* state, |
| 157 BlobStorageContext* context); |
| 158 |
| 159 void OnSharedMemoryResponses( |
| 160 const std::string& uuid, |
| 161 TransportState* state, |
| 162 const std::vector<BlobItemBytesResponse>& responses, |
| 163 BlobStorageContext* context); |
| 164 |
| 165 void SendFileRequests( |
| 166 TransportState* state, |
| 167 BlobStorageContext* context, |
| 168 std::vector<BlobMemoryController::FileCreationInfo> files); |
| 169 |
| 170 void OnFileResponses(const std::string& uuid, |
| 171 TransportState* state, |
| 172 const std::vector<BlobItemBytesResponse>& responses, |
| 173 BlobStorageContext* context); |
| 174 |
| 175 // This finishes creating the blob in the context, decrements blob references |
| 176 // that we were holding during construction, and erases our state. |
| 177 void CompleteTransport(TransportState* state, BlobStorageContext* context); |
| 178 |
| 179 AsyncBlobMap async_blob_map_; |
| 180 base::WeakPtrFactory<BlobTransportHost> ptr_factory_; |
| 181 |
| 182 DISALLOW_COPY_AND_ASSIGN(BlobTransportHost); |
| 183 }; |
| 184 |
| 185 } // namespace storage |
| 186 #endif // STORAGE_BROWSER_BLOB_BLOB_TRANSPORT_HOST_H_ |
OLD | NEW |