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