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_ASYNC_BUILDER_HOST_H_ | |
| 6 #define STORAGE_BROWSER_BLOB_BLOB_ASYNC_BUILDER_HOST_H_ | |
| 7 | |
| 8 #include <map> | |
| 9 #include <string> | |
| 10 #include <vector> | |
| 11 | |
| 12 #include "base/callback.h" | |
| 13 #include "base/macros.h" | |
| 14 #include "base/memory/ref_counted.h" | |
| 15 #include "base/memory/scoped_ptr.h" | |
| 16 #include "base/memory/shared_memory_handle.h" | |
| 17 #include "storage/browser/blob/blob_async_transport_strategy.h" | |
| 18 #include "storage/browser/blob/blob_data_builder.h" | |
| 19 #include "storage/browser/storage_browser_export.h" | |
| 20 #include "storage/common/blob_storage/blob_item_bytes_request.h" | |
| 21 #include "storage/common/blob_storage/blob_item_bytes_response.h" | |
| 22 #include "storage/common/blob_storage/blob_storage_constants.h" | |
| 23 #include "storage/common/data_element.h" | |
| 24 | |
| 25 namespace base { | |
| 26 class SharedMemory; | |
| 27 } | |
| 28 | |
| 29 namespace storage { | |
| 30 | |
| 31 // This class holds all blobs that are currently being built asynchronously for | |
| 32 // a child process. It sends memory request, cancel, and done messages through | |
| 33 // the given callbacks. | |
| 34 // This also includes handling 'shortcut' logic, where the host will use the | |
| 35 // initial data in the description instead of requesting for data if we have | |
| 36 // enough immediate space. | |
| 37 class STORAGE_EXPORT BlobAsyncBuilderHost { | |
| 38 public: | |
| 39 BlobAsyncBuilderHost(); | |
| 40 virtual ~BlobAsyncBuilderHost(); | |
| 41 | |
| 42 // This method begins the construction of the blob given the descriptions. | |
| 43 // After this method is called, the following can happen: | |
|
kinuko
2015/11/25 16:08:17
nit: "either one of the followings can happen" ?
dmurph
2015/11/25 21:16:30
Clarified.
| |
| 44 // * The |done| callback is triggered immediately because we can shortcut the | |
| 45 // construction. | |
| 46 // * The |request_memory| callback is called to request memory from the | |
| 47 // renderer. This class waits for calls to OnMemoryResponses to continue. | |
| 48 // The last argument in the callback corresponds to file handle sizes. | |
| 49 // * The cancel callback is triggered if there is an error. | |
| 50 // Returns if the arguments are valid and we have a good IPC message. | |
| 51 bool StartBuildingBlob( | |
| 52 const std::string& uuid, | |
| 53 const std::string& type, | |
| 54 const std::vector<DataElement>& descriptions, | |
| 55 size_t memory_available, | |
| 56 const base::Callback< | |
| 57 void(const std::vector<storage::BlobItemBytesRequest>&, | |
| 58 const std::vector<base::SharedMemoryHandle>&, | |
| 59 const std::vector<uint64_t>&)>& request_memory, | |
| 60 const base::Callback<void(const BlobDataBuilder&)>& done, | |
| 61 const base::Callback<void(IPCBlobCreationCancelCode)>& cancel); | |
| 62 | |
| 63 // This is called when we have responses from the Renderer to our calls to | |
| 64 // the request_memory callback above. | |
| 65 // Returns if the arguments are valid and we have a good IPC message. | |
| 66 bool OnMemoryResponses(const std::string& uuid, | |
| 67 const std::vector<BlobItemBytesResponse>& responses); | |
| 68 | |
| 69 // This erases the blob building state. | |
| 70 void StopBuildingBlob(const std::string& uuid); | |
| 71 | |
| 72 size_t blob_building_count() const { return async_blob_map_.size(); } | |
| 73 | |
| 74 // For testing use only. Must be called before StartBuildingBlob. | |
| 75 void SetMemoryConstantsForTesting(size_t max_ipc_memory_size, | |
| 76 size_t max_shared_memory_size, | |
| 77 uint64_t max_file_size) { | |
| 78 max_ipc_memory_size_ = max_ipc_memory_size; | |
| 79 max_shared_memory_size_ = max_shared_memory_size; | |
| 80 max_file_size_ = max_file_size; | |
| 81 } | |
| 82 | |
| 83 private: | |
| 84 struct BlobBuildingState { | |
| 85 BlobBuildingState(); | |
| 86 ~BlobBuildingState(); | |
| 87 | |
| 88 std::string type; | |
| 89 BlobAsyncTransportStrategy transport_strategy; | |
| 90 size_t next_request; | |
| 91 size_t num_fulfilled_requests; | |
| 92 scoped_ptr<base::SharedMemory> shared_memory_block; | |
| 93 // This is the number of requests that have been sent to populate the above | |
| 94 // shared data. We won't ask for more data in shared memory until all | |
| 95 // requests have been responded to. | |
| 96 size_t num_shared_memory_requests; | |
| 97 // Only relevant if num_shared_memory_requests is > 0 | |
| 98 size_t current_shared_memory_handle_index; | |
| 99 | |
| 100 base::Callback<void(const std::vector<storage::BlobItemBytesRequest>&, | |
| 101 const std::vector<base::SharedMemoryHandle>&, | |
| 102 const std::vector<uint64_t>&)> request_memory_callback; | |
| 103 base::Callback<void(const BlobDataBuilder&)> done_callback; | |
| 104 base::Callback<void(IPCBlobCreationCancelCode)> cancel_callback; | |
| 105 }; | |
| 106 | |
| 107 typedef std::map<std::string, scoped_ptr<BlobBuildingState>> AsyncBlobMap; | |
| 108 | |
| 109 // This is the 'main loop' of our memory requests to the renderer. | |
| 110 void ContinueBlobMemoryRequests(const std::string& uuid); | |
| 111 | |
| 112 void CancelAndCleanup(const std::string& uuid, | |
| 113 IPCBlobCreationCancelCode code); | |
| 114 void DoneAndCleanup(const std::string& uuid); | |
| 115 | |
| 116 AsyncBlobMap async_blob_map_; | |
| 117 | |
| 118 // Here for testing. | |
| 119 size_t max_ipc_memory_size_ = kBlobStorageIPCThresholdBytes; | |
| 120 size_t max_shared_memory_size_ = kBlobStorageMaxSharedMemoryBytes; | |
| 121 uint64_t max_file_size_ = kBlobStorageMaxFileSizeBytes; | |
| 122 | |
| 123 DISALLOW_COPY_AND_ASSIGN(BlobAsyncBuilderHost); | |
| 124 }; | |
| 125 | |
| 126 } // namespace storage | |
| 127 #endif // STORAGE_BROWSER_BLOB_BLOB_ASYNC_BUILDER_HOST_H_ | |
| OLD | NEW |