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, either of the following can happen. |
| 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 // 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 |
| 51 // 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. |
| 53 bool StartBuildingBlob( |
| 54 const std::string& uuid, |
| 55 const std::string& type, |
| 56 const std::vector<DataElement>& descriptions, |
| 57 size_t memory_available, |
| 58 const base::Callback< |
| 59 void(const std::vector<storage::BlobItemBytesRequest>&, |
| 60 const std::vector<base::SharedMemoryHandle>&, |
| 61 const std::vector<uint64_t>&)>& request_memory, |
| 62 const base::Callback<void(const BlobDataBuilder&)>& done, |
| 63 const base::Callback<void(IPCBlobCreationCancelCode)>& cancel); |
| 64 |
| 65 // This is called when we have responses from the Renderer to our calls to |
| 66 // the request_memory callback above. |
| 67 // Returns if the arguments are valid and we have a good IPC message. |
| 68 bool OnMemoryResponses(const std::string& uuid, |
| 69 const std::vector<BlobItemBytesResponse>& responses); |
| 70 |
| 71 // This erases the blob building state. |
| 72 void StopBuildingBlob(const std::string& uuid); |
| 73 |
| 74 size_t blob_building_count() const { return async_blob_map_.size(); } |
| 75 |
| 76 // For testing use only. Must be called before StartBuildingBlob. |
| 77 void SetMemoryConstantsForTesting(size_t max_ipc_memory_size, |
| 78 size_t max_shared_memory_size, |
| 79 uint64_t max_file_size) { |
| 80 max_ipc_memory_size_ = max_ipc_memory_size; |
| 81 max_shared_memory_size_ = max_shared_memory_size; |
| 82 max_file_size_ = max_file_size; |
| 83 } |
| 84 |
| 85 private: |
| 86 struct BlobBuildingState { |
| 87 BlobBuildingState(); |
| 88 ~BlobBuildingState(); |
| 89 |
| 90 std::string type; |
| 91 BlobAsyncTransportStrategy transport_strategy; |
| 92 size_t next_request; |
| 93 size_t num_fulfilled_requests; |
| 94 scoped_ptr<base::SharedMemory> shared_memory_block; |
| 95 // 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 |
| 97 // requests have been responded to. |
| 98 size_t num_shared_memory_requests; |
| 99 // Only relevant if num_shared_memory_requests is > 0 |
| 100 size_t current_shared_memory_handle_index; |
| 101 |
| 102 base::Callback<void(const std::vector<storage::BlobItemBytesRequest>&, |
| 103 const std::vector<base::SharedMemoryHandle>&, |
| 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 }; |
| 108 |
| 109 typedef std::map<std::string, scoped_ptr<BlobBuildingState>> AsyncBlobMap; |
| 110 |
| 111 // This is the 'main loop' of our memory requests to the renderer. |
| 112 void ContinueBlobMemoryRequests(const std::string& uuid); |
| 113 |
| 114 void CancelAndCleanup(const std::string& uuid, |
| 115 IPCBlobCreationCancelCode code); |
| 116 void DoneAndCleanup(const std::string& uuid); |
| 117 |
| 118 AsyncBlobMap async_blob_map_; |
| 119 |
| 120 // Here for testing. |
| 121 size_t max_ipc_memory_size_ = kBlobStorageIPCThresholdBytes; |
| 122 size_t max_shared_memory_size_ = kBlobStorageMaxSharedMemoryBytes; |
| 123 uint64_t max_file_size_ = kBlobStorageMaxFileSizeBytes; |
| 124 |
| 125 DISALLOW_COPY_AND_ASSIGN(BlobAsyncBuilderHost); |
| 126 }; |
| 127 |
| 128 } // namespace storage |
| 129 #endif // STORAGE_BROWSER_BLOB_BLOB_ASYNC_BUILDER_HOST_H_ |
OLD | NEW |