| OLD | NEW |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 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 CONTENT_BROWSER_BLOB_STORAGE_BLOB_DISPATCHER_HOST_H_ | 5 #ifndef CONTENT_BROWSER_BLOB_STORAGE_BLOB_DISPATCHER_HOST_H_ |
| 6 #define CONTENT_BROWSER_BLOB_STORAGE_BLOB_DISPATCHER_HOST_H_ | 6 #define CONTENT_BROWSER_BLOB_STORAGE_BLOB_DISPATCHER_HOST_H_ |
| 7 | 7 |
| 8 #include <memory> |
| 8 #include <set> | 9 #include <set> |
| 9 #include <string> | 10 #include <string> |
| 10 #include <vector> | 11 #include <vector> |
| 11 | 12 |
| 12 #include "base/files/file.h" | 13 #include "base/files/file.h" |
| 13 #include "base/gtest_prod_util.h" | 14 #include "base/gtest_prod_util.h" |
| 14 #include "base/macros.h" | 15 #include "base/macros.h" |
| 15 #include "base/memory/ref_counted.h" | 16 #include "base/memory/ref_counted.h" |
| 16 #include "base/memory/shared_memory_handle.h" | 17 #include "base/memory/shared_memory_handle.h" |
| 17 #include "content/common/content_export.h" | 18 #include "content/common/content_export.h" |
| 18 #include "content/public/browser/browser_message_filter.h" | 19 #include "content/public/browser/browser_message_filter.h" |
| 19 #include "storage/browser/blob/blob_async_builder_host.h" | 20 #include "storage/browser/blob/blob_transport_host.h" |
| 20 #include "storage/browser/blob/blob_transport_result.h" | |
| 21 #include "storage/common/blob_storage/blob_storage_constants.h" | 21 #include "storage/common/blob_storage/blob_storage_constants.h" |
| 22 | 22 |
| 23 class GURL; | 23 class GURL; |
| 24 | 24 |
| 25 namespace storage { | 25 namespace storage { |
| 26 class DataElement; | 26 class DataElement; |
| 27 class BlobDataHandle; |
| 27 struct BlobItemBytesRequest; | 28 struct BlobItemBytesRequest; |
| 28 struct BlobItemBytesResponse; | 29 struct BlobItemBytesResponse; |
| 29 class BlobStorageContext; | 30 class BlobStorageContext; |
| 30 class FileSystemContext; | 31 class FileSystemContext; |
| 31 } | 32 } |
| 32 | 33 |
| 33 namespace content { | 34 namespace content { |
| 34 class ChromeBlobStorageContext; | 35 class ChromeBlobStorageContext; |
| 35 | 36 |
| 36 // This class's responsibility is to listen for and dispatch blob storage | 37 // This class's responsibility is to listen for and dispatch blob storage |
| 37 // messages and handle logistics of blob storage for a single child process. | 38 // messages and handle logistics of blob storage for a single child process. |
| 38 // When the child process terminates all blob references attributable to | 39 // When the child process terminates all blob references attributable to |
| 39 // that process go away upon destruction of the instance. | 40 // that process go away upon destruction of the instance. |
| 40 // This lives in the browser process, is single threaded (IO thread), and there | 41 // This lives in the browser process, is single threaded (IO thread), and there |
| 41 // is one per child process. | 42 // is one per child process. |
| 42 class CONTENT_EXPORT BlobDispatcherHost : public BrowserMessageFilter { | 43 class CONTENT_EXPORT BlobDispatcherHost : public BrowserMessageFilter { |
| 43 public: | 44 public: |
| 44 BlobDispatcherHost( | 45 BlobDispatcherHost( |
| 45 int process_id, | 46 int process_id, |
| 46 scoped_refptr<ChromeBlobStorageContext> blob_storage_context, | 47 scoped_refptr<ChromeBlobStorageContext> blob_storage_context, |
| 47 scoped_refptr<storage::FileSystemContext> file_system_context); | 48 scoped_refptr<storage::FileSystemContext> file_system_context); |
| 48 | 49 |
| 49 // BrowserMessageFilter implementation. | 50 // BrowserMessageFilter implementation. |
| 50 void OnChannelClosing() override; | 51 void OnChannelClosing() override; |
| 51 bool OnMessageReceived(const IPC::Message& message) override; | 52 bool OnMessageReceived(const IPC::Message& message) override; |
| 52 | 53 |
| 53 protected: | 54 protected: |
| 54 ~BlobDispatcherHost() override; | 55 ~BlobDispatcherHost() override; |
| 55 | 56 |
| 56 // For testing use only. | |
| 57 void SetMemoryConstantsForTesting(size_t max_ipc_memory_size, | |
| 58 size_t max_shared_memory_size, | |
| 59 uint64_t max_file_size) { | |
| 60 async_builder_.SetMemoryConstantsForTesting( | |
| 61 max_ipc_memory_size, max_shared_memory_size, max_file_size); | |
| 62 } | |
| 63 | |
| 64 private: | 57 private: |
| 65 friend class base::RefCountedThreadSafe<BlobDispatcherHost>; | 58 friend class base::RefCountedThreadSafe<BlobDispatcherHost>; |
| 66 friend class BlobDispatcherHostTest; | 59 friend class BlobDispatcherHostTest; |
| 67 FRIEND_TEST_ALL_PREFIXES(BlobDispatcherHostTest, EmptyUUIDs); | 60 FRIEND_TEST_ALL_PREFIXES(BlobDispatcherHostTest, EmptyUUIDs); |
| 68 FRIEND_TEST_ALL_PREFIXES(BlobDispatcherHostTest, MultipleTransfers); | 61 FRIEND_TEST_ALL_PREFIXES(BlobDispatcherHostTest, MultipleTransfers); |
| 69 FRIEND_TEST_ALL_PREFIXES(BlobDispatcherHostTest, SharedMemoryTransfer); | 62 FRIEND_TEST_ALL_PREFIXES(BlobDispatcherHostTest, SharedMemoryTransfer); |
| 70 FRIEND_TEST_ALL_PREFIXES(BlobDispatcherHostTest, OnCancelBuildingBlob); | 63 FRIEND_TEST_ALL_PREFIXES(BlobDispatcherHostTest, OnCancelBuildingBlob); |
| 71 FRIEND_TEST_ALL_PREFIXES(BlobDispatcherHostTest, | 64 FRIEND_TEST_ALL_PREFIXES(BlobDispatcherHostTest, |
| 72 BlobReferenceWhileConstructing); | 65 BlobReferenceWhileConstructing); |
| 73 FRIEND_TEST_ALL_PREFIXES(BlobDispatcherHostTest, | 66 FRIEND_TEST_ALL_PREFIXES(BlobDispatcherHostTest, |
| (...skipping 12 matching lines...) Expand all Loading... |
| 86 FRIEND_TEST_ALL_PREFIXES(BlobDispatcherHostTest, | 79 FRIEND_TEST_ALL_PREFIXES(BlobDispatcherHostTest, |
| 87 CreateBlobWithBrokenReference); | 80 CreateBlobWithBrokenReference); |
| 88 FRIEND_TEST_ALL_PREFIXES(BlobDispatcherHostTest, | 81 FRIEND_TEST_ALL_PREFIXES(BlobDispatcherHostTest, |
| 89 DeferenceBlobOnDifferentHost); | 82 DeferenceBlobOnDifferentHost); |
| 90 FRIEND_TEST_ALL_PREFIXES(BlobDispatcherHostTest, BuildingReferenceChain); | 83 FRIEND_TEST_ALL_PREFIXES(BlobDispatcherHostTest, BuildingReferenceChain); |
| 91 FRIEND_TEST_ALL_PREFIXES(BlobDispatcherHostTest, | 84 FRIEND_TEST_ALL_PREFIXES(BlobDispatcherHostTest, |
| 92 BuildingReferenceChainWithCancel); | 85 BuildingReferenceChainWithCancel); |
| 93 FRIEND_TEST_ALL_PREFIXES(BlobDispatcherHostTest, | 86 FRIEND_TEST_ALL_PREFIXES(BlobDispatcherHostTest, |
| 94 BuildingReferenceChainWithSourceDeath); | 87 BuildingReferenceChainWithSourceDeath); |
| 95 | 88 |
| 96 typedef std::map<std::string, int> BlobReferenceMap; | 89 void OnRegisterBlob(const std::string& uuid, |
| 97 | 90 const std::string& content_type, |
| 98 void OnRegisterBlobUUID(const std::string& uuid, | 91 const std::string& content_disposition, |
| 99 const std::string& content_type, | 92 const std::vector<storage::DataElement>& descriptions); |
| 100 const std::string& content_disposition, | |
| 101 const std::set<std::string>& referenced_blob_uuids); | |
| 102 void OnStartBuildingBlob( | |
| 103 const std::string& uuid, | |
| 104 const std::vector<storage::DataElement>& descriptions); | |
| 105 void OnMemoryItemResponse( | 93 void OnMemoryItemResponse( |
| 106 const std::string& uuid, | 94 const std::string& uuid, |
| 107 const std::vector<storage::BlobItemBytesResponse>& response); | 95 const std::vector<storage::BlobItemBytesResponse>& response); |
| 108 void OnCancelBuildingBlob(const std::string& uuid, | 96 void OnCancelBuildingBlob(const std::string& uuid, |
| 109 const storage::IPCBlobCreationCancelCode code); | 97 const storage::BlobStatus code); |
| 110 | 98 |
| 111 void OnIncrementBlobRefCount(const std::string& uuid); | 99 void OnIncrementBlobRefCount(const std::string& uuid); |
| 112 void OnDecrementBlobRefCount(const std::string& uuid); | 100 void OnDecrementBlobRefCount(const std::string& uuid); |
| 113 void OnRegisterPublicBlobURL(const GURL& public_url, const std::string& uuid); | 101 void OnRegisterPublicBlobURL(const GURL& public_url, const std::string& uuid); |
| 114 void OnRevokePublicBlobURL(const GURL& public_url); | 102 void OnRevokePublicBlobURL(const GURL& public_url); |
| 115 | 103 |
| 116 storage::BlobStorageContext* context(); | 104 storage::BlobStorageContext* context(); |
| 117 | 105 |
| 118 void SendMemoryRequest( | 106 void SendMemoryRequest(const std::string& uuid, |
| 119 const std::string& uuid, | 107 std::vector<storage::BlobItemBytesRequest> requests, |
| 120 std::unique_ptr<std::vector<storage::BlobItemBytesRequest>> requests, | 108 std::vector<base::SharedMemoryHandle> memory_handles, |
| 121 std::unique_ptr<std::vector<base::SharedMemoryHandle>> memory_handles, | 109 std::vector<base::File> files); |
| 122 std::unique_ptr<std::vector<base::File>> files); | |
| 123 | 110 |
| 124 // Send the appropriate IPC response to the renderer for the given result. | 111 // The status should never be a pending status (see BlobStatusIsPending), and |
| 125 void SendIPCResponse(const std::string& uuid, | 112 // we ignore calls for |uuid|s that aren't in use in this host. |
| 126 storage::BlobTransportResult result); | 113 void SendFinalBlobStatus(const std::string& uuid, storage::BlobStatus status); |
| 127 | 114 |
| 128 bool IsInUseInHost(const std::string& uuid); | 115 bool IsInUseInHost(const std::string& uuid); |
| 129 bool IsUrlRegisteredInHost(const GURL& blob_url); | 116 bool IsUrlRegisteredInHost(const GURL& blob_url); |
| 130 | 117 |
| 131 // Unregisters all blobs and urls that were registered in this host. | 118 // Unregisters all blobs and urls that were registered in this host. |
| 132 void ClearHostFromBlobStorageContext(); | 119 void ClearHostFromBlobStorageContext(); |
| 133 | 120 |
| 121 struct HostedBlobState { |
| 122 explicit HostedBlobState(std::unique_ptr<storage::BlobDataHandle> handle); |
| 123 ~HostedBlobState(); |
| 124 HostedBlobState(HostedBlobState&&); |
| 125 HostedBlobState& operator=(HostedBlobState&&); |
| 126 DISALLOW_COPY_AND_ASSIGN(HostedBlobState); |
| 127 |
| 128 int refcount = 1; |
| 129 std::unique_ptr<storage::BlobDataHandle> handle; |
| 130 }; |
| 131 |
| 134 const int process_id_; | 132 const int process_id_; |
| 135 scoped_refptr<storage::FileSystemContext> file_system_context_; | 133 scoped_refptr<storage::FileSystemContext> file_system_context_; |
| 136 | 134 |
| 137 // Collection of blob ids and a count of how many usages | 135 // Collection of blob ids and a count of how many usages |
| 138 // of that id are attributable to this consumer. | 136 // of that id are attributable to this consumer. |
| 139 BlobReferenceMap blobs_inuse_map_; | 137 std::unordered_map<std::string, HostedBlobState> blobs_inuse_map_; |
| 140 | 138 |
| 141 // The set of public blob urls coined by this consumer. | 139 // The set of public blob urls coined by this consumer. |
| 142 std::set<GURL> public_blob_urls_; | 140 std::set<GURL> public_blob_urls_; |
| 143 | 141 |
| 144 scoped_refptr<ChromeBlobStorageContext> blob_storage_context_; | 142 scoped_refptr<ChromeBlobStorageContext> blob_storage_context_; |
| 145 storage::BlobAsyncBuilderHost async_builder_; | 143 storage::BlobTransportHost transport_host_; |
| 146 | 144 |
| 147 DISALLOW_COPY_AND_ASSIGN(BlobDispatcherHost); | 145 DISALLOW_COPY_AND_ASSIGN(BlobDispatcherHost); |
| 148 }; | 146 }; |
| 149 } // namespace content | 147 } // namespace content |
| 150 #endif // CONTENT_BROWSER_BLOB_STORAGE_BLOB_DISPATCHER_HOST_H_ | 148 #endif // CONTENT_BROWSER_BLOB_STORAGE_BLOB_DISPATCHER_HOST_H_ |
| OLD | NEW |