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