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 <utility> |
13 #include <vector> | 14 #include <vector> |
14 | 15 |
| 16 #include "base/callback_forward.h" |
15 #include "base/gtest_prod_util.h" | 17 #include "base/gtest_prod_util.h" |
16 #include "base/macros.h" | 18 #include "base/macros.h" |
17 #include "base/memory/ref_counted.h" | 19 #include "base/memory/ref_counted.h" |
18 #include "base/memory/shared_memory_handle.h" | 20 #include "base/memory/shared_memory_handle.h" |
| 21 #include "base/memory/weak_ptr.h" |
19 #include "content/common/content_export.h" | 22 #include "content/common/content_export.h" |
20 #include "ipc/ipc_platform_file.h" | 23 #include "ipc/ipc_platform_file.h" |
21 #include "storage/common/blob_storage/blob_storage_constants.h" | 24 #include "storage/common/blob_storage/blob_storage_constants.h" |
22 | 25 |
23 namespace base { | 26 namespace base { |
24 template <typename T> | 27 template <typename T> |
25 struct DefaultLazyInstanceTraits; | 28 struct DefaultLazyInstanceTraits; |
26 class SingleThreadTaskRunner; | 29 class SingleThreadTaskRunner; |
| 30 class TaskRunner; |
27 } | 31 } |
28 | 32 |
29 namespace storage { | 33 namespace storage { |
30 class DataElement; | 34 class DataElement; |
31 struct BlobItemBytesRequest; | 35 struct BlobItemBytesRequest; |
32 struct BlobItemBytesResponse; | 36 struct BlobItemBytesResponse; |
33 } | 37 } |
34 | 38 |
35 namespace IPC { | 39 namespace IPC { |
| 40 class Message; |
36 class Sender; | 41 class Sender; |
37 } | 42 } |
38 | 43 |
39 namespace content { | 44 namespace content { |
40 | 45 |
41 class BlobConsolidation; | 46 class BlobConsolidation; |
42 class ThreadSafeSender; | 47 class ThreadSafeSender; |
43 | 48 |
44 // This class is used to manage all the asynchronous transporation of blobs from | 49 // 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. | 50 // the Renderer to the Browser process, where it's handling the Renderer side. |
46 // The function of this class is to: | 51 // The function of this class is to: |
47 // * Be a lazy singleton, | 52 // * Be a lazy singleton, |
48 // * hold all of the blob data that is being transported to the Browser process, | 53 // * hold all of the blob data that is being transported to the Browser process, |
49 // * create the blob item 'descriptions' for the browser, | 54 // * create the blob item 'descriptions' for the browser, |
50 // * include shortcut data in the descriptions, | 55 // * include shortcut data in the descriptions, |
51 // * generate responses to blob memory requests, and | 56 // * generate responses to blob memory requests, and |
52 // * send IPC responses. | 57 // * send IPC responses. |
| 58 // We try to keep the renderer alive during blob transportation by calling |
| 59 // ChildProcess::AddProcessRef and |
| 60 // blink::Platform::current()->suddenTerminationChanged. |
53 // Must be used on the IO thread. | 61 // Must be used on the IO thread. |
54 class CONTENT_EXPORT BlobTransportController { | 62 class CONTENT_EXPORT BlobTransportController { |
55 public: | 63 public: |
56 static BlobTransportController* GetInstance(); | 64 static BlobTransportController* GetInstance(); |
57 | 65 |
58 // This kicks off a blob transfer to the browser thread, which involves | 66 // This kicks off a blob transfer to the browser thread, which involves |
59 // sending an IPC message and storing the blob consolidation object. Designed | 67 // sending an IPC message and storing the blob consolidation object. Designed |
60 // to be called by the main thread or a worker thread. | 68 // to be called by the main thread or a worker thread. |
61 // This also calls ChildProcess::AddRefProcess to keep our process around | 69 // This also calls ChildProcess::AddRefProcess to keep our process around |
62 // while we transfer. | 70 // while we transfer. |
63 static void InitiateBlobTransfer( | 71 static void InitiateBlobTransfer( |
64 const std::string& uuid, | 72 const std::string& uuid, |
65 const std::string& content_type, | 73 const std::string& content_type, |
66 std::unique_ptr<BlobConsolidation> consolidation, | 74 scoped_refptr<BlobConsolidation> consolidation, |
67 scoped_refptr<ThreadSafeSender> sender, | 75 scoped_refptr<ThreadSafeSender> sender, |
68 base::SingleThreadTaskRunner* io_runner, | 76 base::SingleThreadTaskRunner* io_runner, |
69 scoped_refptr<base::SingleThreadTaskRunner> main_runner); | 77 scoped_refptr<base::SingleThreadTaskRunner> main_runner); |
70 | 78 |
71 // This responds to the request using the sender. | 79 // This responds to the request using the |sender|. If we need to save files |
| 80 // then we we hold onto the sender to send the (possibly multiple) reponses |
| 81 // asynchronously. Use CancelAllBlobTransfers to stop usage of the |sender|. |
| 82 // We close the file handles once we're done writing to them. |
72 void OnMemoryRequest( | 83 void OnMemoryRequest( |
73 const std::string& uuid, | 84 const std::string& uuid, |
74 const std::vector<storage::BlobItemBytesRequest>& requests, | 85 const std::vector<storage::BlobItemBytesRequest>& requests, |
75 std::vector<base::SharedMemoryHandle>* memory_handles, | 86 std::vector<base::SharedMemoryHandle>* memory_handles, |
76 const std::vector<IPC::PlatformFileForTransit>& file_handles, | 87 const std::vector<IPC::PlatformFileForTransit>& file_handles, |
| 88 base::TaskRunner* file_runner, |
77 IPC::Sender* sender); | 89 IPC::Sender* sender); |
78 | 90 |
79 void OnCancel(const std::string& uuid, | 91 void OnCancel(const std::string& uuid, |
80 storage::IPCBlobCreationCancelCode code); | 92 storage::IPCBlobCreationCancelCode code); |
81 | 93 |
82 void OnDone(const std::string& uuid); | 94 void OnDone(const std::string& uuid); |
83 | 95 |
84 bool IsTransporting(const std::string& uuid) { | 96 bool IsTransporting(const std::string& uuid) { |
85 return blob_storage_.find(uuid) != blob_storage_.end(); | 97 return blob_storage_.find(uuid) != blob_storage_.end(); |
86 } | 98 } |
87 | 99 |
| 100 // Invalidates all asynchronously running memory request handlers and clears |
| 101 // the internal state. If our map wasn't previously empty, then we call |
| 102 // ChildProcess::ReleaseProcess to release our previous reference. |
| 103 void CancelAllBlobTransfers(); |
| 104 |
88 private: | 105 private: |
| 106 friend struct base::DefaultLazyInstanceTraits<BlobTransportController>; |
89 friend class BlobTransportControllerTest; | 107 friend class BlobTransportControllerTest; |
90 FRIEND_TEST_ALL_PREFIXES(BlobTransportControllerTest, Descriptions); | 108 FRIEND_TEST_ALL_PREFIXES(BlobTransportControllerTest, Descriptions); |
91 FRIEND_TEST_ALL_PREFIXES(BlobTransportControllerTest, Responses); | 109 FRIEND_TEST_ALL_PREFIXES(BlobTransportControllerTest, Responses); |
92 FRIEND_TEST_ALL_PREFIXES(BlobTransportControllerTest, SharedMemory); | 110 FRIEND_TEST_ALL_PREFIXES(BlobTransportControllerTest, SharedMemory); |
| 111 FRIEND_TEST_ALL_PREFIXES(BlobTransportControllerTest, Disk); |
93 FRIEND_TEST_ALL_PREFIXES(BlobTransportControllerTest, ResponsesErrors); | 112 FRIEND_TEST_ALL_PREFIXES(BlobTransportControllerTest, ResponsesErrors); |
94 | 113 |
| 114 enum class ResponsesStatus { |
| 115 BLOB_NOT_FOUND, |
| 116 SHARED_MEMORY_MAP_FAILED, |
| 117 PENDING_IO, |
| 118 SUCCESS |
| 119 }; |
| 120 |
95 static void GetDescriptions(BlobConsolidation* consolidation, | 121 static void GetDescriptions(BlobConsolidation* consolidation, |
96 size_t max_data_population, | 122 size_t max_data_population, |
97 std::vector<storage::DataElement>* out); | 123 std::vector<storage::DataElement>* out); |
98 | 124 |
99 BlobTransportController(); | 125 BlobTransportController(); |
100 ~BlobTransportController(); | 126 ~BlobTransportController(); |
101 | 127 |
102 // Clears all internal state. If our map wasn't previously empty, then we call | 128 void OnFileWriteComplete( |
103 // ChildProcess::ReleaseProcess to release our previous reference. | 129 IPC::Sender* sender, |
104 void ClearForTesting(); | 130 const std::string& uuid, |
105 | 131 const std::pair<std::vector<storage::BlobItemBytesResponse>, |
106 enum class ResponsesStatus { | 132 storage::IPCBlobCreationCancelCode>& result); |
107 BLOB_NOT_FOUND, | |
108 SHARED_MEMORY_MAP_FAILED, | |
109 SUCCESS | |
110 }; | |
111 friend struct base::DefaultLazyInstanceTraits<BlobTransportController>; | |
112 | 133 |
113 void StoreBlobDataForRequests( | 134 void StoreBlobDataForRequests( |
114 const std::string& uuid, | 135 const std::string& uuid, |
115 std::unique_ptr<BlobConsolidation> consolidation, | 136 scoped_refptr<BlobConsolidation> consolidation, |
116 scoped_refptr<base::SingleThreadTaskRunner> main_runner); | 137 scoped_refptr<base::SingleThreadTaskRunner> main_runner); |
117 | 138 |
118 ResponsesStatus GetResponses( | 139 // Deletes the consolidation and calls ChildProcess::ReleaseProcess. |
119 const std::string& uuid, | |
120 const std::vector<storage::BlobItemBytesRequest>& requests, | |
121 std::vector<base::SharedMemoryHandle>* memory_handles, | |
122 const std::vector<IPC::PlatformFileForTransit>& file_handles, | |
123 std::vector<storage::BlobItemBytesResponse>* output); | |
124 | |
125 // Deletes the consolidation, and if we removed the last consolidation from | |
126 // our map, we call ChildProcess::ReleaseProcess to release our previous | |
127 // reference. | |
128 void ReleaseBlobConsolidation(const std::string& uuid); | 140 void ReleaseBlobConsolidation(const std::string& uuid); |
129 | 141 |
130 scoped_refptr<base::SingleThreadTaskRunner> main_thread_runner_; | 142 scoped_refptr<base::SingleThreadTaskRunner> main_thread_runner_; |
131 std::map<std::string, std::unique_ptr<BlobConsolidation>> blob_storage_; | 143 std::map<std::string, scoped_refptr<BlobConsolidation>> blob_storage_; |
| 144 base::WeakPtrFactory<BlobTransportController> weak_factory_; |
132 | 145 |
133 DISALLOW_COPY_AND_ASSIGN(BlobTransportController); | 146 DISALLOW_COPY_AND_ASSIGN(BlobTransportController); |
134 }; | 147 }; |
135 | 148 |
136 } // namespace content | 149 } // namespace content |
137 #endif // CONTENT_CHILD_BLOB_STORAGE_BLOB_TRANSPORT_CONTROLLER_H_ | 150 #endif // CONTENT_CHILD_BLOB_STORAGE_BLOB_TRANSPORT_CONTROLLER_H_ |
OLD | NEW |