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 CONTENT_CHILD_BLOB_STORAGE_BLOB_TRANSPORT_CONTROLLER_H_ | 5 #ifndef CONTENT_CHILD_BLOB_STORAGE_BLOB_TRANSPORT_CONTROLLER_H_ |
6 #define CONTENT_CHILD_BLOB_STORAGE_BLOB_TRANSPORT_CONTROLLER_H_ | 6 #define CONTENT_CHILD_BLOB_STORAGE_BLOB_TRANSPORT_CONTROLLER_H_ |
7 | 7 |
8 #include <stddef.h> | 8 #include <stddef.h> |
9 | 9 |
10 #include <map> | 10 #include <map> |
11 #include <memory> | 11 #include <memory> |
12 #include <string> | 12 #include <string> |
13 #include <vector> | 13 #include <vector> |
14 | 14 |
15 #include "base/callback_forward.h" | |
15 #include "base/gtest_prod_util.h" | 16 #include "base/gtest_prod_util.h" |
16 #include "base/macros.h" | 17 #include "base/macros.h" |
17 #include "base/memory/ref_counted.h" | 18 #include "base/memory/ref_counted.h" |
18 #include "base/memory/shared_memory_handle.h" | 19 #include "base/memory/shared_memory_handle.h" |
19 #include "content/common/content_export.h" | 20 #include "content/common/content_export.h" |
20 #include "ipc/ipc_platform_file.h" | 21 #include "ipc/ipc_platform_file.h" |
21 #include "storage/common/blob_storage/blob_storage_constants.h" | 22 #include "storage/common/blob_storage/blob_storage_constants.h" |
22 | 23 |
23 namespace base { | 24 namespace base { |
24 template <typename T> | 25 template <typename T> |
25 struct DefaultLazyInstanceTraits; | 26 struct DefaultLazyInstanceTraits; |
26 class SingleThreadTaskRunner; | 27 class SingleThreadTaskRunner; |
28 class TaskRunner; | |
27 } | 29 } |
28 | 30 |
29 namespace storage { | 31 namespace storage { |
30 class DataElement; | 32 class DataElement; |
31 struct BlobItemBytesRequest; | 33 struct BlobItemBytesRequest; |
32 struct BlobItemBytesResponse; | 34 struct BlobItemBytesResponse; |
33 } | 35 } |
34 | 36 |
35 namespace IPC { | 37 namespace IPC { |
38 class Message; | |
36 class Sender; | 39 class Sender; |
37 } | 40 } |
38 | 41 |
39 namespace content { | 42 namespace content { |
40 | 43 |
41 class BlobConsolidation; | 44 class BlobConsolidation; |
42 class ThreadSafeSender; | 45 class ThreadSafeSender; |
43 | 46 |
44 // This class is used to manage all the asynchronous transporation of blobs from | 47 // This class is used to manage all the asynchronous transporation of blobs from |
45 // the Renderer to the Browser process, where it's handling the Renderer side. | 48 // the Renderer to the Browser process, where it's handling the Renderer side. |
46 // The function of this class is to: | 49 // The function of this class is to: |
47 // * Be a lazy singleton, | 50 // * Be a lazy singleton, |
48 // * hold all of the blob data that is being transported to the Browser process, | 51 // * hold all of the blob data that is being transported to the Browser process, |
49 // * create the blob item 'descriptions' for the browser, | 52 // * create the blob item 'descriptions' for the browser, |
50 // * include shortcut data in the descriptions, | 53 // * include shortcut data in the descriptions, |
51 // * generate responses to blob memory requests, and | 54 // * generate responses to blob memory requests, and |
52 // * send IPC responses. | 55 // * send IPC responses. |
53 // Must be used on the IO thread. | 56 // Must be used on the IO thread. |
54 class CONTENT_EXPORT BlobTransportController { | 57 class CONTENT_EXPORT BlobTransportController { |
55 public: | 58 public: |
59 using IPCSender = base::Callback<void(std::unique_ptr<IPC::Message>)>; | |
60 | |
56 static BlobTransportController* GetInstance(); | 61 static BlobTransportController* GetInstance(); |
57 | 62 |
58 // This kicks off a blob transfer to the browser thread, which involves | 63 // This kicks off a blob transfer to the browser thread, which involves |
59 // sending an IPC message and storing the blob consolidation object. Designed | 64 // sending an IPC message and storing the blob consolidation object. Designed |
60 // to be called by the main thread or a worker thread. | 65 // to be called by the main thread or a worker thread. |
61 // This also calls ChildProcess::AddRefProcess to keep our process around | 66 // This also calls ChildProcess::AddRefProcess to keep our process around |
62 // while we transfer. | 67 // while we transfer. |
63 static void InitiateBlobTransfer( | 68 static void InitiateBlobTransfer( |
64 const std::string& uuid, | 69 const std::string& uuid, |
65 const std::string& content_type, | 70 const std::string& content_type, |
66 std::unique_ptr<BlobConsolidation> consolidation, | 71 scoped_refptr<BlobConsolidation> consolidation, |
67 scoped_refptr<ThreadSafeSender> sender, | 72 scoped_refptr<ThreadSafeSender> sender, |
68 base::SingleThreadTaskRunner* io_runner, | 73 base::SingleThreadTaskRunner* io_runner, |
69 scoped_refptr<base::SingleThreadTaskRunner> main_runner); | 74 scoped_refptr<base::SingleThreadTaskRunner> main_runner); |
70 | 75 |
71 // This responds to the request using the sender. | 76 // This responds to the request using the |ipc_sender| callback. If we need |
77 // to save files then the call will happen asynchronously. | |
michaeln
2016/04/21 18:51:43
maybe also mention that a single call to OnMemoryR
| |
72 void OnMemoryRequest( | 78 void OnMemoryRequest( |
73 const std::string& uuid, | 79 const std::string& uuid, |
74 const std::vector<storage::BlobItemBytesRequest>& requests, | 80 const std::vector<storage::BlobItemBytesRequest>& requests, |
75 std::vector<base::SharedMemoryHandle>* memory_handles, | 81 std::vector<base::SharedMemoryHandle>* memory_handles, |
76 const std::vector<IPC::PlatformFileForTransit>& file_handles, | 82 const std::vector<IPC::PlatformFileForTransit>& file_handles, |
77 IPC::Sender* sender); | 83 scoped_refptr<base::TaskRunner> io_runner, |
michaeln
2016/04/21 01:58:41
this method can only be called on the io thread so
dmurph
2016/04/22 22:37:10
Done.
| |
84 base::TaskRunner* file_runner, | |
85 const IPCSender& ipc_sender); | |
michaeln
2016/04/21 01:58:41
we generally use the IPC::Sender interface for thi
dmurph
2016/04/22 22:37:10
Done.
| |
78 | 86 |
79 void OnCancel(const std::string& uuid, | 87 void OnCancel(const std::string& uuid, |
80 storage::IPCBlobCreationCancelCode code); | 88 storage::IPCBlobCreationCancelCode code); |
81 | 89 |
82 void OnDone(const std::string& uuid); | 90 void OnDone(const std::string& uuid); |
83 | 91 |
84 bool IsTransporting(const std::string& uuid) { | 92 bool IsTransporting(const std::string& uuid) { |
85 return blob_storage_.find(uuid) != blob_storage_.end(); | 93 return blob_storage_.find(uuid) != blob_storage_.end(); |
86 } | 94 } |
87 | 95 |
88 private: | 96 private: |
97 friend struct base::DefaultLazyInstanceTraits<BlobTransportController>; | |
89 friend class BlobTransportControllerTest; | 98 friend class BlobTransportControllerTest; |
90 FRIEND_TEST_ALL_PREFIXES(BlobTransportControllerTest, Descriptions); | 99 FRIEND_TEST_ALL_PREFIXES(BlobTransportControllerTest, Descriptions); |
91 FRIEND_TEST_ALL_PREFIXES(BlobTransportControllerTest, Responses); | 100 FRIEND_TEST_ALL_PREFIXES(BlobTransportControllerTest, Responses); |
92 FRIEND_TEST_ALL_PREFIXES(BlobTransportControllerTest, SharedMemory); | 101 FRIEND_TEST_ALL_PREFIXES(BlobTransportControllerTest, SharedMemory); |
102 FRIEND_TEST_ALL_PREFIXES(BlobTransportControllerTest, Disk); | |
93 FRIEND_TEST_ALL_PREFIXES(BlobTransportControllerTest, ResponsesErrors); | 103 FRIEND_TEST_ALL_PREFIXES(BlobTransportControllerTest, ResponsesErrors); |
94 | 104 |
105 using ResponseCallback = base::Callback<void( | |
106 const std::vector<storage::BlobItemBytesResponse>& /* responses */)>; | |
107 using CancelCallback = | |
108 base::Callback<void(storage::IPCBlobCreationCancelCode /* reason */)>; | |
109 | |
110 enum class ResponsesStatus { | |
111 BLOB_NOT_FOUND, | |
112 SHARED_MEMORY_MAP_FAILED, | |
113 PENDING_IO, | |
114 SUCCESS | |
115 }; | |
116 | |
95 static void GetDescriptions(BlobConsolidation* consolidation, | 117 static void GetDescriptions(BlobConsolidation* consolidation, |
96 size_t max_data_population, | 118 size_t max_data_population, |
97 std::vector<storage::DataElement>* out); | 119 std::vector<storage::DataElement>* out); |
98 | 120 |
99 BlobTransportController(); | 121 BlobTransportController(); |
100 ~BlobTransportController(); | 122 ~BlobTransportController(); |
101 | 123 |
102 // Clears all internal state. If our map wasn't previously empty, then we call | 124 // Clears all internal state. If our map wasn't previously empty, then we call |
103 // ChildProcess::ReleaseProcess to release our previous reference. | 125 // ChildProcess::ReleaseProcess to release our previous reference. |
104 void ClearForTesting(); | 126 void ClearForTesting(); |
105 | 127 |
106 enum class ResponsesStatus { | 128 void SendResponses( |
107 BLOB_NOT_FOUND, | 129 const IPCSender& ipc_sender, |
108 SHARED_MEMORY_MAP_FAILED, | 130 const std::string& uuid, |
109 SUCCESS | 131 const std::vector<storage::BlobItemBytesResponse>& responses); |
110 }; | 132 |
111 friend struct base::DefaultLazyInstanceTraits<BlobTransportController>; | 133 void SendCancelAndReleaseConsolidation( |
134 const IPCSender& ipc_sender, | |
135 const std::string& uuid, | |
136 storage::IPCBlobCreationCancelCode reason); | |
112 | 137 |
113 void StoreBlobDataForRequests( | 138 void StoreBlobDataForRequests( |
114 const std::string& uuid, | 139 const std::string& uuid, |
115 std::unique_ptr<BlobConsolidation> consolidation, | 140 scoped_refptr<BlobConsolidation> consolidation, |
116 scoped_refptr<base::SingleThreadTaskRunner> main_runner); | 141 scoped_refptr<base::SingleThreadTaskRunner> main_runner); |
117 | 142 |
118 ResponsesStatus GetResponses( | 143 ResponsesStatus GetResponses( |
119 const std::string& uuid, | 144 const std::string& uuid, |
120 const std::vector<storage::BlobItemBytesRequest>& requests, | 145 const std::vector<storage::BlobItemBytesRequest>& requests, |
121 std::vector<base::SharedMemoryHandle>* memory_handles, | 146 std::vector<base::SharedMemoryHandle>* memory_handles, |
122 const std::vector<IPC::PlatformFileForTransit>& file_handles, | 147 const std::vector<IPC::PlatformFileForTransit>& file_handles, |
123 std::vector<storage::BlobItemBytesResponse>* output); | 148 std::vector<storage::BlobItemBytesResponse>* output, |
149 scoped_refptr<base::TaskRunner> io_runner, | |
michaeln
2016/04/21 01:58:41
io_runner may not needed here for the same reason
dmurph
2016/04/22 22:37:10
Done.
| |
150 base::TaskRunner* file_runner, | |
151 const ResponseCallback& response_callback, | |
152 const CancelCallback& error_callback); | |
michaeln
2016/04/21 01:58:41
ditto IPC::Sender
Since IPC::Sender would be a ra
kinuko
2016/04/21 07:54:34
Btw-- why don't we use ThreadSafeSender here and e
dmurph
2016/04/22 22:37:10
I don't have a thread safe version, I just have th
| |
124 | 153 |
125 // Deletes the consolidation, and if we removed the last consolidation from | 154 // Deletes the consolidation, and if we removed the last consolidation from |
126 // our map, we call ChildProcess::ReleaseProcess to release our previous | 155 // our map, we call ChildProcess::ReleaseProcess to release our previous |
127 // reference. | 156 // reference. |
128 void ReleaseBlobConsolidation(const std::string& uuid); | 157 void ReleaseBlobConsolidation(const std::string& uuid); |
129 | 158 |
130 scoped_refptr<base::SingleThreadTaskRunner> main_thread_runner_; | 159 scoped_refptr<base::SingleThreadTaskRunner> main_thread_runner_; |
131 std::map<std::string, std::unique_ptr<BlobConsolidation>> blob_storage_; | 160 std::map<std::string, scoped_refptr<BlobConsolidation>> blob_storage_; |
132 | 161 |
133 DISALLOW_COPY_AND_ASSIGN(BlobTransportController); | 162 DISALLOW_COPY_AND_ASSIGN(BlobTransportController); |
134 }; | 163 }; |
135 | 164 |
136 } // namespace content | 165 } // namespace content |
137 #endif // CONTENT_CHILD_BLOB_STORAGE_BLOB_TRANSPORT_CONTROLLER_H_ | 166 #endif // CONTENT_CHILD_BLOB_STORAGE_BLOB_TRANSPORT_CONTROLLER_H_ |
OLD | NEW |