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/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; | |
|
michaeln
2016/11/16 00:55:36
not sure we hashmap is worth it the extra space, m
dmurph
2016/11/16 02:15:28
I'd argue that would make it OK to use the unorder
| |
| 135 | |
| 136 enum CancelType { SIGNAL_COMPLETION_CALLBACK, IGNORE_COMPLETION_CALLBACK }; | |
|
michaeln
2016/11/16 00:55:36
is this used anywhere?
dmurph
2016/11/16 02:15:28
Nope, removed.
| |
| 137 | |
| 138 void StartRequests( | |
| 139 const std::string& uuid, | |
| 140 TransportState* state, | |
| 141 BlobStorageContext* context, | |
| 142 std::vector<BlobMemoryController::FileCreationInfo> file_infos); | |
| 143 | |
| 144 void OnReadyForTransport( | |
| 145 const std::string& uuid, | |
| 146 base::WeakPtr<BlobStorageContext> context, | |
| 147 BlobStatus status, | |
| 148 std::vector<BlobMemoryController::FileCreationInfo> file_infos); | |
| 149 | |
| 150 void SendIPCRequests(TransportState* state, BlobStorageContext* context); | |
| 151 void OnIPCResponses(const std::string& uuid, | |
| 152 TransportState* state, | |
| 153 const std::vector<BlobItemBytesResponse>& responses, | |
| 154 BlobStorageContext* context); | |
| 155 | |
| 156 // This is the 'main loop' of our memory requests to the renderer. | |
| 157 void ContinueSharedMemoryRequests(const std::string& uuid, | |
| 158 TransportState* state, | |
| 159 BlobStorageContext* context); | |
| 160 | |
| 161 void OnSharedMemoryResponses( | |
| 162 const std::string& uuid, | |
| 163 TransportState* state, | |
| 164 const std::vector<BlobItemBytesResponse>& responses, | |
| 165 BlobStorageContext* context); | |
| 166 | |
| 167 void SendFileRequests( | |
| 168 TransportState* state, | |
| 169 BlobStorageContext* context, | |
| 170 std::vector<BlobMemoryController::FileCreationInfo> files); | |
| 171 | |
| 172 void OnFileResponses(const std::string& uuid, | |
| 173 TransportState* state, | |
| 174 const std::vector<BlobItemBytesResponse>& responses, | |
| 175 BlobStorageContext* context); | |
| 176 | |
| 177 // This finishes creating the blob in the context, decrements blob references | |
| 178 // that we were holding during construction, and erases our state. | |
| 179 void CompleteTransport(TransportState* state, BlobStorageContext* context); | |
| 180 | |
| 181 AsyncBlobMap async_blob_map_; | |
| 182 base::WeakPtrFactory<BlobTransportHost> ptr_factory_; | |
| 183 | |
| 184 DISALLOW_COPY_AND_ASSIGN(BlobTransportHost); | |
| 185 }; | |
| 186 | |
| 187 } // namespace storage | |
| 188 #endif // STORAGE_BROWSER_BLOB_BLOB_TRANSPORT_HOST_H_ | |
| OLD | NEW |