| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #ifndef STORAGE_BROWSER_BLOB_BLOB_ASYNC_BUILDER_HOST_H_ | 5 #ifndef STORAGE_BROWSER_BLOB_BLOB_ASYNC_BUILDER_HOST_H_ |
| 6 #define STORAGE_BROWSER_BLOB_BLOB_ASYNC_BUILDER_HOST_H_ | 6 #define STORAGE_BROWSER_BLOB_BLOB_ASYNC_BUILDER_HOST_H_ |
| 7 | 7 |
| 8 #include <list> |
| 8 #include <map> | 9 #include <map> |
| 9 #include <string> | 10 #include <string> |
| 10 #include <vector> | 11 #include <vector> |
| 11 | 12 |
| 12 #include "base/callback.h" | 13 #include "base/callback.h" |
| 13 #include "base/macros.h" | 14 #include "base/macros.h" |
| 14 #include "base/memory/ref_counted.h" | 15 #include "base/memory/ref_counted.h" |
| 15 #include "base/memory/scoped_ptr.h" | 16 #include "base/memory/scoped_ptr.h" |
| 16 #include "base/memory/shared_memory_handle.h" | 17 #include "base/memory/shared_memory_handle.h" |
| 18 #include "base/memory/weak_ptr.h" |
| 17 #include "storage/browser/blob/blob_async_transport_strategy.h" | 19 #include "storage/browser/blob/blob_async_transport_strategy.h" |
| 18 #include "storage/browser/blob/blob_data_builder.h" | 20 #include "storage/browser/blob/blob_data_builder.h" |
| 21 #include "storage/browser/blob/blob_memory_controller.h" |
| 19 #include "storage/browser/storage_browser_export.h" | 22 #include "storage/browser/storage_browser_export.h" |
| 20 #include "storage/common/blob_storage/blob_item_bytes_request.h" | 23 #include "storage/common/blob_storage/blob_item_bytes_request.h" |
| 21 #include "storage/common/blob_storage/blob_item_bytes_response.h" | 24 #include "storage/common/blob_storage/blob_item_bytes_response.h" |
| 22 #include "storage/common/blob_storage/blob_storage_constants.h" | 25 #include "storage/common/blob_storage/blob_storage_constants.h" |
| 23 #include "storage/common/data_element.h" | 26 #include "storage/common/data_element.h" |
| 24 | 27 |
| 25 namespace base { | 28 namespace base { |
| 26 class SharedMemory; | 29 class SharedMemory; |
| 27 } | 30 } |
| 28 | 31 |
| 29 namespace storage { | 32 namespace storage { |
| 33 class BlobMemoryController; |
| 34 class ShareableFileReference; |
| 30 | 35 |
| 31 // This class holds all blobs that are currently being built asynchronously for | 36 // 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 | 37 // a child process. It sends memory request, cancel, and done messages through |
| 33 // the given callbacks. | 38 // the given callbacks. |
| 34 // This also includes handling 'shortcut' logic, where the host will use the | 39 // 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 | 40 // initial data in the description instead of requesting for data if we have |
| 36 // enough immediate space. | 41 // enough immediate space. |
| 37 class STORAGE_EXPORT BlobAsyncBuilderHost { | 42 class STORAGE_EXPORT BlobAsyncBuilderHost { |
| 38 public: | 43 public: |
| 39 BlobAsyncBuilderHost(); | 44 BlobAsyncBuilderHost(); |
| 40 virtual ~BlobAsyncBuilderHost(); | 45 virtual ~BlobAsyncBuilderHost(); |
| 41 | 46 |
| 42 // This method begins the construction of the blob given the descriptions. | 47 // This method begins the construction of the blob given the descriptions. |
| 43 // After this method is called, either of the following can happen. | 48 // After this method is called, either of the following can happen. |
| 44 // * The |done| callback is triggered immediately because we can shortcut the | 49 // * The |done| callback is triggered immediately because we can shortcut the |
| 45 // construction. | 50 // construction. |
| 46 // * The |request_memory| callback is called to request memory from the | 51 // * The |request_memory| callback is called to request memory from the |
| 47 // renderer. This class waits for calls to OnMemoryResponses to continue. | 52 // renderer. This class waits for calls to OnMemoryResponses to continue. |
| 48 // The last argument in the callback corresponds to file handle sizes. | 53 // The last argument in the callback corresponds to file handle sizes. |
| 49 // When all memory is recieved, the |done| callback is triggered. | 54 // When all memory is recieved, the |done| callback is triggered. |
| 50 // * The |cancel| callback is triggered if there is an error at any point. All | 55 // * The |cancel| callback is triggered if there is an error at any point. All |
| 51 // state for the cancelled blob is cleared before |cancel| is called. | 56 // state for the cancelled blob is cleared before |cancel| is called. |
| 52 // Returns if the arguments are valid and we have a good IPC message. | 57 // Returns if the arguments are valid and we have a good IPC message. |
| 53 bool StartBuildingBlob( | 58 bool StartBuildingBlob( |
| 54 const std::string& uuid, | 59 const std::string& uuid, |
| 55 const std::string& type, | 60 const std::string& type, |
| 56 const std::vector<DataElement>& descriptions, | 61 const std::vector<DataElement>& descriptions, |
| 57 size_t memory_available, | 62 BlobMemoryController* memory_controller, |
| 58 const base::Callback< | 63 const base::Callback< |
| 59 void(const std::vector<storage::BlobItemBytesRequest>&, | 64 void(const std::vector<storage::BlobItemBytesRequest>&, |
| 60 const std::vector<base::SharedMemoryHandle>&, | 65 const std::vector<base::SharedMemoryHandle>&, |
| 61 const std::vector<uint64_t>&)>& request_memory, | 66 const std::vector<uint64_t>&)>& request_memory, |
| 62 const base::Callback<void(const BlobDataBuilder&)>& done, | 67 const base::Callback<void(const BlobDataBuilder&)>& done, |
| 63 const base::Callback<void(IPCBlobCreationCancelCode)>& cancel); | 68 const base::Callback<void(IPCBlobCreationCancelCode)>& cancel); |
| 64 | 69 |
| 65 // This is called when we have responses from the Renderer to our calls to | 70 // This is called when we have responses from the Renderer to our calls to |
| 66 // the request_memory callback above. | 71 // the request_memory callback above. |
| 67 // Returns if the arguments are valid and we have a good IPC message. | 72 // Returns if the arguments are valid and we have a good IPC message. |
| 68 bool OnMemoryResponses(const std::string& uuid, | 73 bool OnMemoryResponses(const std::string& uuid, |
| 69 const std::vector<BlobItemBytesResponse>& responses); | 74 const std::vector<BlobItemBytesResponse>& responses); |
| 70 | 75 |
| 71 // This erases the blob building state. | 76 // This erases the blob building state. |
| 72 void StopBuildingBlob(const std::string& uuid); | 77 void StopBuildingBlob(const std::string& uuid); |
| 73 | 78 |
| 74 size_t blob_building_count() const { return async_blob_map_.size(); } | 79 size_t blob_building_count() const { return async_blob_map_.size(); } |
| 75 | 80 |
| 76 // For testing use only. Must be called before StartBuildingBlob. | 81 // For testing use only. Must be called before StartBuildingBlob. |
| 77 void SetMemoryConstantsForTesting(size_t max_ipc_memory_size, | 82 void SetMemoryConstantsForTesting(size_t max_ipc_memory_size, |
| 78 size_t max_shared_memory_size, | 83 size_t max_shared_memory_size, |
| 79 uint64_t max_file_size) { | 84 uint64_t max_file_size, |
| 85 size_t in_flight_space) { |
| 80 max_ipc_memory_size_ = max_ipc_memory_size; | 86 max_ipc_memory_size_ = max_ipc_memory_size; |
| 81 max_shared_memory_size_ = max_shared_memory_size; | 87 max_shared_memory_size_ = max_shared_memory_size; |
| 82 max_file_size_ = max_file_size; | 88 max_file_size_ = max_file_size; |
| 89 in_flight_space_ = in_flight_space; |
| 83 } | 90 } |
| 84 | 91 |
| 85 private: | 92 private: |
| 93 struct BlobBuildingState; |
| 86 struct BlobBuildingState { | 94 struct BlobBuildingState { |
| 87 BlobBuildingState(); | 95 explicit BlobBuildingState(const std::string& uuid); |
| 88 ~BlobBuildingState(); | 96 ~BlobBuildingState(); |
| 89 | 97 |
| 90 std::string type; | 98 std::string type; |
| 91 BlobAsyncTransportStrategy transport_strategy; | 99 BlobAsyncTransportStrategy transport_strategy; |
| 92 size_t next_request; | 100 BlobDataBuilder builder; |
| 93 size_t num_fulfilled_requests; | 101 std::vector<bool> request_received; |
| 102 size_t next_request = 0; |
| 103 size_t num_fulfilled_requests = 0; |
| 104 bool requests_started = false; |
| 105 |
| 106 base::Callback<void( |
| 107 const std::vector<storage::BlobItemBytesRequest>&, |
| 108 const std::vector<base::SharedMemoryHandle>&, |
| 109 const std::vector<scoped_refptr<ShareableFileReference>>&)> |
| 110 request_memory_callback; |
| 111 base::Callback<void(const BlobDataBuilder&)> done_callback; |
| 112 base::Callback<void(IPCBlobCreationCancelCode)> cancel_callback; |
| 113 |
| 114 // Shared memory bookkeeping. |
| 94 scoped_ptr<base::SharedMemory> shared_memory_block; | 115 scoped_ptr<base::SharedMemory> shared_memory_block; |
| 95 // This is the number of requests that have been sent to populate the above | 116 // This is the number of requests that have been sent to populate the above |
| 96 // shared data. We won't ask for more data in shared memory until all | 117 // shared data. We won't ask for more data in shared memory until all |
| 97 // requests have been responded to. | 118 // requests have been responded to. |
| 98 size_t num_shared_memory_requests; | 119 size_t num_shared_memory_requests = 0; |
| 99 // Only relevant if num_shared_memory_requests is > 0 | 120 // Only relevant if num_shared_memory_requests is > 0 |
| 100 size_t current_shared_memory_handle_index; | 121 size_t current_shared_memory_handle_index = 0; |
| 101 | 122 |
| 102 base::Callback<void(const std::vector<storage::BlobItemBytesRequest>&, | 123 // File bookkeeping. |
| 103 const std::vector<base::SharedMemoryHandle>&, | 124 std::vector<BlobMemoryController::FileCreationInfo> files_created; |
| 104 const std::vector<uint64_t>&)> request_memory_callback; | |
| 105 base::Callback<void(const BlobDataBuilder&)> done_callback; | |
| 106 base::Callback<void(IPCBlobCreationCancelCode)> cancel_callback; | |
| 107 }; | 125 }; |
| 108 | 126 |
| 109 typedef std::map<std::string, scoped_ptr<BlobBuildingState>> AsyncBlobMap; | 127 typedef std::map<std::string, scoped_ptr<BlobBuildingState>> AsyncBlobMap; |
| 110 | 128 |
| 111 // This is the 'main loop' of our memory requests to the renderer. | 129 // This is used as a callback to the memory manager. This starts the sending |
| 130 // of requests to the renderer. It also does all file creation for file |
| 131 // handles. |
| 132 void StartRequestingBlobAndCreateFileHandles( |
| 133 const std::string& uuid, |
| 134 base::WeakPtr<BlobMemoryController> memory_controller, |
| 135 bool can_request); |
| 136 |
| 137 // This is the 'main loop' for sending requests for the given blob. This |
| 138 // includes logic for handling IPC and shared memory requests. |
| 112 void ContinueBlobMemoryRequests(const std::string& uuid); | 139 void ContinueBlobMemoryRequests(const std::string& uuid); |
| 113 | 140 |
| 141 // This is called from the memory manager when it has created a file for us. |
| 142 void FileCreatedForBlob( |
| 143 const std::string& uuid, |
| 144 size_t handle_index, |
| 145 const BlobMemoryController::FileCreationInfo& creation_info); |
| 146 |
| 114 void CancelAndCleanup(const std::string& uuid, | 147 void CancelAndCleanup(const std::string& uuid, |
| 115 IPCBlobCreationCancelCode code); | 148 IPCBlobCreationCancelCode code); |
| 116 void DoneAndCleanup(const std::string& uuid); | 149 void DoneAndCleanup(const std::string& uuid); |
| 117 | 150 |
| 118 AsyncBlobMap async_blob_map_; | 151 AsyncBlobMap async_blob_map_; |
| 119 | 152 |
| 153 size_t requesting_memory_size_ = 0; |
| 154 |
| 120 // Here for testing. | 155 // Here for testing. |
| 121 size_t max_ipc_memory_size_ = kBlobStorageIPCThresholdBytes; | 156 size_t max_ipc_memory_size_ = kBlobStorageIPCThresholdBytes; |
| 122 size_t max_shared_memory_size_ = kBlobStorageMaxSharedMemoryBytes; | 157 size_t max_shared_memory_size_ = kBlobStorageMaxSharedMemoryBytes; |
| 123 uint64_t max_file_size_ = kBlobStorageMaxFileSizeBytes; | 158 uint64_t max_file_size_ = kBlobStorageMaxFileSizeBytes; |
| 159 size_t in_flight_space_ = kBlobStorageInFlightMemory; |
| 160 |
| 161 base::WeakPtrFactory<BlobAsyncBuilderHost> weak_ptr_factory_; |
| 124 | 162 |
| 125 DISALLOW_COPY_AND_ASSIGN(BlobAsyncBuilderHost); | 163 DISALLOW_COPY_AND_ASSIGN(BlobAsyncBuilderHost); |
| 126 }; | 164 }; |
| 127 | 165 |
| 128 } // namespace storage | 166 } // namespace storage |
| 129 #endif // STORAGE_BROWSER_BLOB_BLOB_ASYNC_BUILDER_HOST_H_ | 167 #endif // STORAGE_BROWSER_BLOB_BLOB_ASYNC_BUILDER_HOST_H_ |
| OLD | NEW |