Chromium Code Reviews| 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/shared_memory_handle.h" | |
| 21 #include "base/memory/weak_ptr.h" | |
| 22 #include "storage/browser/blob/blob_data_builder.h" | |
| 23 #include "storage/browser/blob/blob_memory_controller.h" | |
| 24 #include "storage/browser/blob/blob_transport_request_builder.h" | |
| 25 #include "storage/browser/storage_browser_export.h" | |
| 26 #include "storage/common/blob_storage/blob_item_bytes_request.h" | |
| 27 #include "storage/common/blob_storage/blob_item_bytes_response.h" | |
| 28 #include "storage/common/blob_storage/blob_storage_constants.h" | |
| 29 #include "storage/common/data_element.h" | |
| 30 | |
| 31 namespace base { | |
| 32 class SharedMemory; | |
| 33 } | |
| 34 | |
| 35 namespace storage { | |
| 36 class BlobDataHandle; | |
| 37 class BlobStorageContext; | |
| 38 | |
| 39 // This class facilitates moving memory from the renderer to the browser. | |
| 40 class STORAGE_EXPORT BlobTransportHost { | |
| 41 public: | |
| 42 // One is expected to use std::move when calling this callback. | |
| 43 using RequestMemoryCallback = | |
| 44 base::Callback<void(std::vector<storage::BlobItemBytesRequest>, | |
| 45 std::vector<base::SharedMemoryHandle>, | |
| 46 std::vector<base::File>)>; | |
| 47 | |
| 48 BlobTransportHost(); | |
| 49 ~BlobTransportHost(); | |
| 50 | |
| 51 // This registers the given blob internally and adds it to the storage with a | |
| 52 // refcount of 1. |completion_callback| is called synchronously or | |
| 53 // asynchronously with: | |
| 54 // * INVALID_CONSTRUCTION_ARGUMENTS if we have invalid input arguments/data. | |
| 55 // We treat this as a critical error, and don't bother registering the blob | |
| 56 // in the BlobStorageContext. | |
| 57 // * REFERENCED_BLOB_BROKEN if one of the referenced blobs is broken or | |
| 58 // doesn't exist. | |
| 59 // * DONE if we don't need any more data transported and we can clean up. | |
| 60 void StartBuildingBlob(const std::string& uuid, | |
| 61 const std::string& content_type, | |
| 62 const std::string& content_disposition, | |
| 63 const std::vector<DataElement>& elements, | |
| 64 BlobStorageContext* context, | |
| 65 const RequestMemoryCallback& request_memory, | |
| 66 const BlobStatusCallback& completion_callback); | |
| 67 | |
| 68 // This is called when we have responses from the Renderer to our calls to | |
| 69 // the request_memory callback above. See above for return value meaning. | |
| 70 BlobStatus OnMemoryResponses( | |
| 71 const std::string& uuid, | |
| 72 const std::vector<BlobItemBytesResponse>& responses, | |
| 73 BlobStorageContext* context); | |
| 74 | |
| 75 // This removes the BlobBuildingState from our map and flags the blob as | |
| 76 // broken in the context. This can be called both from our own logic to cancel | |
| 77 // the blob, or from the DispatcherHost (Renderer). The blob MUST be being | |
| 78 // built in this builder. | |
| 79 // Note: if the blob isn't in the context (renderer dereferenced it before we | |
| 80 // finished constructing), then we don't bother touching the context. | |
| 81 void CancelBuildingBlob(const std::string& uuid, | |
| 82 BlobStatus code, | |
| 83 BlobStorageContext* context); | |
| 84 | |
| 85 // This clears this object of pending construction. It also handles marking | |
| 86 // blobs that haven't been fully constructed as broken in the context if there | |
| 87 // are any references being held by anyone. We know that they're being used | |
| 88 // by someone else if they still exist in the context. | |
| 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 explicit TransportState(const std::string& uuid, | |
|
kinuko
2016/11/09 16:45:07
nit: no need of explicit
dmurph
2016/11/09 19:13:27
Done.
| |
| 102 const std::string& content_type, | |
| 103 const std::string& content_disposition, | |
| 104 RequestMemoryCallback request_memory_callback, | |
| 105 BlobStatusCallback completion_callback); | |
| 106 ~TransportState(); | |
| 107 | |
| 108 IPCBlobItemRequestStrategy strategy = IPCBlobItemRequestStrategy::UNKNOWN; | |
| 109 BlobTransportRequestBuilder request_builder; | |
| 110 BlobDataBuilder data_builder; | |
| 111 std::vector<bool> request_received; | |
| 112 size_t num_fulfilled_requests = 0; | |
| 113 | |
| 114 const RequestMemoryCallback request_memory_callback; | |
| 115 const BlobStatusCallback completion_callback; | |
| 116 | |
| 117 // Used by shared memory strategy. | |
| 118 size_t next_request = 0; | |
| 119 std::unique_ptr<base::SharedMemory> shared_memory_block; | |
| 120 // This is the number of requests that have been sent to populate the above | |
| 121 // shared data. We won't ask for more data in shared memory until all | |
| 122 // requests have been responded to. | |
| 123 size_t num_shared_memory_requests = 0; | |
| 124 // Only relevant if num_shared_memory_requests is > 0 | |
| 125 size_t current_shared_memory_handle_index = 0; | |
| 126 | |
| 127 // Used by file strategy. | |
| 128 std::vector<scoped_refptr<ShareableFileReference>> files; | |
| 129 }; | |
| 130 | |
| 131 typedef std::map<std::string, std::unique_ptr<TransportState>> AsyncBlobMap; | |
| 132 | |
| 133 BlobStatus StartRequests( | |
| 134 const std::string& uuid, | |
| 135 TransportState* state, | |
| 136 BlobStorageContext* context, | |
| 137 std::vector<BlobMemoryController::FileCreationInfo> file_infos); | |
| 138 | |
| 139 void OnReadyForTransport( | |
| 140 const std::string& uuid, | |
| 141 base::WeakPtr<BlobStorageContext> context, | |
| 142 BlobStatus status, | |
| 143 std::vector<BlobMemoryController::FileCreationInfo> file_infos); | |
| 144 | |
| 145 void SendIPCRequests(TransportState* state, BlobStorageContext* context); | |
| 146 BlobStatus OnIPCResponses(const std::string& uuid, | |
| 147 TransportState* state, | |
| 148 const std::vector<BlobItemBytesResponse>& responses, | |
| 149 BlobStorageContext* context); | |
| 150 | |
| 151 // This is the 'main loop' of our memory requests to the renderer. | |
| 152 BlobStatus ContinueSharedMemoryRequests(const std::string& uuid, | |
| 153 TransportState* state, | |
| 154 BlobStorageContext* context); | |
| 155 | |
| 156 BlobStatus OnSharedMemoryResponses( | |
| 157 const std::string& uuid, | |
| 158 TransportState* state, | |
| 159 const std::vector<BlobItemBytesResponse>& responses, | |
| 160 BlobStorageContext* context); | |
| 161 | |
| 162 void SendFileRequests( | |
| 163 TransportState* state, | |
| 164 BlobStorageContext* context, | |
| 165 std::vector<BlobMemoryController::FileCreationInfo> files); | |
| 166 | |
| 167 BlobStatus OnFileResponses( | |
| 168 const std::string& uuid, | |
| 169 TransportState* state, | |
| 170 const std::vector<BlobItemBytesResponse>& responses, | |
| 171 BlobStorageContext* context); | |
| 172 | |
| 173 // This finishes creating the blob in the context, decrements blob references | |
| 174 // that we were holding during construction, and erases our state. | |
| 175 void CompleteTransport(TransportState* state, BlobStorageContext* context); | |
| 176 | |
| 177 AsyncBlobMap async_blob_map_; | |
| 178 base::WeakPtrFactory<BlobTransportHost> ptr_factory_; | |
| 179 | |
| 180 DISALLOW_COPY_AND_ASSIGN(BlobTransportHost); | |
| 181 }; | |
| 182 | |
| 183 } // namespace storage | |
| 184 #endif // STORAGE_BROWSER_BLOB_BLOB_TRANSPORT_HOST_H_ | |
| OLD | NEW |