| 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_ASYNC_BUILDER_HOST_H_ | |
| 6 #define STORAGE_BROWSER_BLOB_BLOB_ASYNC_BUILDER_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_async_transport_request_builder.h" | |
| 23 #include "storage/browser/blob/blob_data_builder.h" | |
| 24 #include "storage/browser/blob/blob_transport_result.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 | |
| 40 // * holds all blobs that are currently being built asynchronously for a child | |
| 41 // process, | |
| 42 // * sends memory requests through the given callback in |StartBuildingBlob|, | |
| 43 // and uses the BlobTransportResult return value to signify other results, | |
| 44 // * includes all logic for deciding which async transport strategy to use, and | |
| 45 // * handles all blob construction communication with the BlobStorageContext. | |
| 46 // The method |CancelAll| must be called by the consumer, it is not called on | |
| 47 // destruction. | |
| 48 class STORAGE_EXPORT BlobAsyncBuilderHost { | |
| 49 public: | |
| 50 using RequestMemoryCallback = base::Callback<void( | |
| 51 std::unique_ptr<std::vector<storage::BlobItemBytesRequest>>, | |
| 52 std::unique_ptr<std::vector<base::SharedMemoryHandle>>, | |
| 53 std::unique_ptr<std::vector<base::File>>)>; | |
| 54 BlobAsyncBuilderHost(); | |
| 55 ~BlobAsyncBuilderHost(); | |
| 56 | |
| 57 // This registers the given blob internally and adds it to the storage. | |
| 58 // Calling this method also guarentees that the referenced blobs are kept | |
| 59 // alive for the duration of the construction of this blob. | |
| 60 // We return | |
| 61 // * BAD_IPC if we already have the blob registered or if we reference ourself | |
| 62 // in the referenced_blob_uuids. | |
| 63 // * CANCEL_REFERENCED_BLOB_BROKEN if one of the referenced blobs is broken or | |
| 64 // doesn't exist. We store the blob in the context as broken with code | |
| 65 // REFERENCED_BLOB_BROKEN. | |
| 66 // * DONE if we successfully registered the blob. | |
| 67 BlobTransportResult RegisterBlobUUID( | |
| 68 const std::string& uuid, | |
| 69 const std::string& content_type, | |
| 70 const std::string& content_disposition, | |
| 71 const std::set<std::string>& referenced_blob_uuids, | |
| 72 BlobStorageContext* context); | |
| 73 | |
| 74 // This method begins the construction of the blob given the descriptions. The | |
| 75 // blob uuid MUST be building in this object. | |
| 76 // When we return: | |
| 77 // * DONE: The blob is finished transfering right away, and is now | |
| 78 // successfully saved in the context. | |
| 79 // * PENDING_RESPONSES: The async builder host is waiting for responses from | |
| 80 // the renderer. It has called |request_memory| for these responses. | |
| 81 // * CANCEL_*: We have to cancel the blob construction. This function clears | |
| 82 // the blob's internal state and marks the blob as broken in the context | |
| 83 // before returning. | |
| 84 // * BAD_IPC: The arguments were invalid/bad. This marks the blob as broken in | |
| 85 // the context before returning. | |
| 86 BlobTransportResult StartBuildingBlob( | |
| 87 const std::string& uuid, | |
| 88 const std::vector<DataElement>& elements, | |
| 89 size_t memory_available, | |
| 90 BlobStorageContext* context, | |
| 91 const RequestMemoryCallback& request_memory); | |
| 92 | |
| 93 // This is called when we have responses from the Renderer to our calls to | |
| 94 // the request_memory callback above. See above for return value meaning. | |
| 95 BlobTransportResult OnMemoryResponses( | |
| 96 const std::string& uuid, | |
| 97 const std::vector<BlobItemBytesResponse>& responses, | |
| 98 BlobStorageContext* context); | |
| 99 | |
| 100 // This removes the BlobBuildingState from our map and flags the blob as | |
| 101 // broken in the context. This can be called both from our own logic to cancel | |
| 102 // the blob, or from the DispatcherHost (Renderer). The blob MUST be being | |
| 103 // built in this builder. | |
| 104 // Note: if the blob isn't in the context (renderer dereferenced it before we | |
| 105 // finished constructing), then we don't bother touching the context. | |
| 106 void CancelBuildingBlob(const std::string& uuid, | |
| 107 IPCBlobCreationCancelCode code, | |
| 108 BlobStorageContext* context); | |
| 109 | |
| 110 // This clears this object of pending construction. It also handles marking | |
| 111 // blobs that haven't been fully constructed as broken in the context if there | |
| 112 // are any references being held by anyone. We know that they're being used | |
| 113 // by someone else if they still exist in the context. | |
| 114 void CancelAll(BlobStorageContext* context); | |
| 115 | |
| 116 bool IsEmpty() const { return async_blob_map_.empty(); } | |
| 117 | |
| 118 size_t blob_building_count() const { return async_blob_map_.size(); } | |
| 119 | |
| 120 bool IsBeingBuilt(const std::string& key) const { | |
| 121 return async_blob_map_.find(key) != async_blob_map_.end(); | |
| 122 } | |
| 123 | |
| 124 // For testing use only. Must be called before StartBuildingBlob. | |
| 125 void SetMemoryConstantsForTesting(size_t max_ipc_memory_size, | |
| 126 size_t max_shared_memory_size, | |
| 127 uint64_t max_file_size) { | |
| 128 max_ipc_memory_size_ = max_ipc_memory_size; | |
| 129 max_shared_memory_size_ = max_shared_memory_size; | |
| 130 max_file_size_ = max_file_size; | |
| 131 } | |
| 132 | |
| 133 private: | |
| 134 struct BlobBuildingState { | |
| 135 // |refernced_blob_handles| should be all handles generated from the set | |
| 136 // of |refernced_blob_uuids|. | |
| 137 BlobBuildingState( | |
| 138 const std::string& uuid, | |
| 139 std::set<std::string> referenced_blob_uuids, | |
| 140 std::vector<std::unique_ptr<BlobDataHandle>>* referenced_blob_handles); | |
| 141 ~BlobBuildingState(); | |
| 142 | |
| 143 BlobAsyncTransportRequestBuilder request_builder; | |
| 144 BlobDataBuilder data_builder; | |
| 145 std::vector<bool> request_received; | |
| 146 size_t next_request = 0; | |
| 147 size_t num_fulfilled_requests = 0; | |
| 148 std::unique_ptr<base::SharedMemory> shared_memory_block; | |
| 149 // This is the number of requests that have been sent to populate the above | |
| 150 // shared data. We won't ask for more data in shared memory until all | |
| 151 // requests have been responded to. | |
| 152 size_t num_shared_memory_requests = 0; | |
| 153 // Only relevant if num_shared_memory_requests is > 0 | |
| 154 size_t current_shared_memory_handle_index = 0; | |
| 155 | |
| 156 // We save these to double check that the RegisterBlob and StartBuildingBlob | |
| 157 // messages are in sync. | |
| 158 std::set<std::string> referenced_blob_uuids; | |
| 159 // These are the blobs that are referenced in the newly constructed blob. | |
| 160 // We use these to make sure they stay alive while we create the new blob, | |
| 161 // and to wait until any blobs that are not done building are fully | |
| 162 // constructed. | |
| 163 std::vector<std::unique_ptr<BlobDataHandle>> referenced_blob_handles; | |
| 164 | |
| 165 // These are the number of blobs we're waiting for before we can start | |
| 166 // building. | |
| 167 size_t num_referenced_blobs_building = 0; | |
| 168 | |
| 169 BlobAsyncBuilderHost::RequestMemoryCallback request_memory_callback; | |
| 170 }; | |
| 171 | |
| 172 typedef std::map<std::string, std::unique_ptr<BlobBuildingState>> | |
| 173 AsyncBlobMap; | |
| 174 | |
| 175 // This is the 'main loop' of our memory requests to the renderer. | |
| 176 BlobTransportResult ContinueBlobMemoryRequests(const std::string& uuid, | |
| 177 BlobStorageContext* context); | |
| 178 | |
| 179 // This is our callback for when we want to finish the blob and we're waiting | |
| 180 // for blobs we reference to be built. When the last callback occurs, we | |
| 181 // complete the blob and erase our internal state. | |
| 182 void ReferencedBlobFinished(const std::string& uuid, | |
| 183 base::WeakPtr<BlobStorageContext> context, | |
| 184 bool construction_success, | |
| 185 IPCBlobCreationCancelCode reason); | |
| 186 | |
| 187 // This finishes creating the blob in the context, decrements blob references | |
| 188 // that we were holding during construction, and erases our state. | |
| 189 void FinishBuildingBlob(BlobBuildingState* state, | |
| 190 BlobStorageContext* context); | |
| 191 | |
| 192 AsyncBlobMap async_blob_map_; | |
| 193 | |
| 194 // Here for testing. | |
| 195 size_t max_ipc_memory_size_ = kBlobStorageIPCThresholdBytes; | |
| 196 size_t max_shared_memory_size_ = kBlobStorageMaxSharedMemoryBytes; | |
| 197 uint64_t max_file_size_ = kBlobStorageMaxFileSizeBytes; | |
| 198 | |
| 199 base::WeakPtrFactory<BlobAsyncBuilderHost> ptr_factory_; | |
| 200 | |
| 201 DISALLOW_COPY_AND_ASSIGN(BlobAsyncBuilderHost); | |
| 202 }; | |
| 203 | |
| 204 } // namespace storage | |
| 205 #endif // STORAGE_BROWSER_BLOB_BLOB_ASYNC_BUILDER_HOST_H_ | |
| OLD | NEW |