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_async_transport_request_builder.h" |
| 24 #include "storage/browser/blob/blob_data_builder.h" |
| 25 #include "storage/browser/blob/blob_memory_controller.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 // We treat this as a critical error, and don't bother registering the blob |
| 57 // in the BlobStorageContext. |
| 58 // * REFERENCED_BLOB_BROKEN if one of the referenced blobs is broken or |
| 59 // doesn't exist. |
| 60 // * DONE if we don't need any more data transported and we can clean up. |
| 61 void StartBuildingBlob(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. See above for return value meaning. |
| 71 BlobStatus OnMemoryResponses( |
| 72 const std::string& uuid, |
| 73 const std::vector<BlobItemBytesResponse>& responses, |
| 74 BlobStorageContext* context); |
| 75 |
| 76 // This removes the BlobBuildingState from our map and flags the blob as |
| 77 // broken in the context. This can be called both from our own logic to cancel |
| 78 // the blob, or from the DispatcherHost (Renderer). The blob MUST be being |
| 79 // built in this builder. |
| 80 // Note: if the blob isn't in the context (renderer dereferenced it before we |
| 81 // finished constructing), then we don't bother touching the context. |
| 82 void CancelBuildingBlob(const std::string& uuid, |
| 83 BlobStatus code, |
| 84 BlobStorageContext* context); |
| 85 |
| 86 // This clears this object of pending construction. It also handles marking |
| 87 // blobs that haven't been fully constructed as broken in the context if there |
| 88 // are any references being held by anyone. We know that they're being used |
| 89 // by someone else if they still exist in the context. |
| 90 void CancelAll(BlobStorageContext* context); |
| 91 |
| 92 bool IsEmpty() const { return async_blob_map_.empty(); } |
| 93 |
| 94 size_t blob_building_count() const { return async_blob_map_.size(); } |
| 95 |
| 96 bool IsBeingBuilt(const std::string& key) const { |
| 97 return async_blob_map_.find(key) != async_blob_map_.end(); |
| 98 } |
| 99 |
| 100 private: |
| 101 struct TransportState { |
| 102 explicit TransportState(const std::string& uuid, |
| 103 const std::string& content_type, |
| 104 const std::string& content_disposition, |
| 105 RequestMemoryCallback request_memory_callback, |
| 106 BlobStatusCallback completion_callback); |
| 107 ~TransportState(); |
| 108 |
| 109 IPCBlobItemRequestStrategy strategy = IPCBlobItemRequestStrategy::UNKNOWN; |
| 110 BlobAsyncTransportRequestBuilder request_builder; |
| 111 BlobDataBuilder data_builder; |
| 112 std::vector<bool> request_received; |
| 113 size_t num_fulfilled_requests = 0; |
| 114 |
| 115 const RequestMemoryCallback request_memory_callback; |
| 116 const BlobStatusCallback completion_callback; |
| 117 |
| 118 // Used by shared memory strategy. |
| 119 size_t next_request = 0; |
| 120 std::unique_ptr<base::SharedMemory> shared_memory_block; |
| 121 // This is the number of requests that have been sent to populate the above |
| 122 // shared data. We won't ask for more data in shared memory until all |
| 123 // requests have been responded to. |
| 124 size_t num_shared_memory_requests = 0; |
| 125 // Only relevant if num_shared_memory_requests is > 0 |
| 126 size_t current_shared_memory_handle_index = 0; |
| 127 |
| 128 // Used by file strategy. |
| 129 std::vector<scoped_refptr<ShareableFileReference>> files; |
| 130 }; |
| 131 |
| 132 typedef std::map<std::string, std::unique_ptr<TransportState>> |
| 133 AsyncBlobMap; |
| 134 |
| 135 BlobStatus StartRequests( |
| 136 const std::string& uuid, |
| 137 TransportState* state, |
| 138 BlobStorageContext* context, |
| 139 std::vector<BlobMemoryController::FileCreationInfo> file_infos); |
| 140 |
| 141 void OnPopulationAllowed( |
| 142 const std::string& uuid, |
| 143 base::WeakPtr<BlobStorageContext> context, |
| 144 BlobStatus status, |
| 145 std::vector<BlobMemoryController::FileCreationInfo> file_infos); |
| 146 |
| 147 void SendIPCRequests(TransportState* state, BlobStorageContext* context); |
| 148 BlobStatus OnIPCResponses(const std::string& uuid, |
| 149 TransportState* state, |
| 150 const std::vector<BlobItemBytesResponse>& responses, |
| 151 BlobStorageContext* context); |
| 152 |
| 153 // This is the 'main loop' of our memory requests to the renderer. |
| 154 BlobStatus ContinueSharedMemoryRequests(const std::string& uuid, |
| 155 TransportState* state, |
| 156 BlobStorageContext* context); |
| 157 |
| 158 BlobStatus OnSharedMemoryResponses( |
| 159 const std::string& uuid, |
| 160 TransportState* state, |
| 161 const std::vector<BlobItemBytesResponse>& responses, |
| 162 BlobStorageContext* context); |
| 163 |
| 164 void SendFileRequests( |
| 165 TransportState* state, |
| 166 BlobStorageContext* context, |
| 167 std::vector<BlobMemoryController::FileCreationInfo> files); |
| 168 |
| 169 BlobStatus OnFileResponses( |
| 170 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 FinishBuildingBlob(TransportState* state, |
| 178 BlobStorageContext* context); |
| 179 |
| 180 AsyncBlobMap async_blob_map_; |
| 181 base::WeakPtrFactory<BlobTransportHost> ptr_factory_; |
| 182 |
| 183 DISALLOW_COPY_AND_ASSIGN(BlobTransportHost); |
| 184 }; |
| 185 |
| 186 } // namespace storage |
| 187 #endif // STORAGE_BROWSER_BLOB_BLOB_TRANSPORT_HOST_H_ |
OLD | NEW |