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 #include "content/browser/mojo/mojo_child_connection.h" | 5 #include "content/browser/mojo/mojo_child_connection.h" |
6 | 6 |
7 #include <stdint.h> | 7 #include <stdint.h> |
8 #include <utility> | 8 #include <utility> |
9 | 9 |
10 #include "base/macros.h" | |
11 #include "content/public/common/mojo_shell_connection.h" | 10 #include "content/public/common/mojo_shell_connection.h" |
12 #include "mojo/edk/embedder/embedder.h" | 11 #include "mojo/edk/embedder/embedder.h" |
13 #include "mojo/public/cpp/system/message_pipe.h" | 12 #include "mojo/public/cpp/system/message_pipe.h" |
14 #include "services/shell/public/cpp/connector.h" | 13 #include "services/shell/public/cpp/connector.h" |
15 #include "services/shell/public/cpp/identity.h" | 14 #include "services/shell/public/cpp/identity.h" |
16 #include "services/shell/public/interfaces/service.mojom.h" | 15 #include "services/shell/public/interfaces/service.mojom.h" |
17 | 16 |
18 namespace content { | 17 namespace content { |
19 | 18 |
20 namespace { | 19 MojoChildConnection::MojoChildConnection(const std::string& application_name, |
21 | 20 const std::string& instance_id, |
22 void CallBinderOnTaskRunner( | 21 const std::string& child_token, |
23 const shell::InterfaceRegistry::Binder& binder, | 22 shell::Connector* connector) |
24 scoped_refptr<base::SequencedTaskRunner> task_runner, | 23 : service_token_(mojo::edk::GenerateRandomToken()) { |
25 const mojo::String& interface_name, | |
26 mojo::ScopedMessagePipeHandle request_handle) { | |
27 task_runner->PostTask( | |
28 FROM_HERE, | |
29 base::Bind(binder, interface_name, base::Passed(&request_handle))); | |
30 } | |
31 | |
32 } // namespace | |
33 | |
34 class MojoChildConnection::IOThreadContext | |
35 : public base::RefCountedThreadSafe<IOThreadContext> { | |
36 public: | |
37 IOThreadContext() {} | |
38 | |
39 void Initialize(const std::string& application_name, | |
40 const std::string& instance_id, | |
41 shell::Connector* connector, | |
42 mojo::ScopedMessagePipeHandle service_pipe, | |
43 scoped_refptr<base::SequencedTaskRunner> io_task_runner, | |
44 const shell::InterfaceRegistry::Binder& default_binder) { | |
45 DCHECK(!io_task_runner_); | |
46 io_task_runner_ = io_task_runner; | |
47 std::unique_ptr<shell::Connector> io_thread_connector; | |
48 if (connector) | |
49 io_thread_connector = connector->Clone(); | |
50 io_task_runner_->PostTask( | |
51 FROM_HERE, | |
52 base::Bind(&IOThreadContext::InitializeOnIOThread, this, | |
53 application_name, instance_id, | |
54 base::Passed(&io_thread_connector), | |
55 base::Passed(&service_pipe), | |
56 base::Bind(&CallBinderOnTaskRunner, default_binder, | |
57 base::ThreadTaskRunnerHandle::Get()))); | |
58 } | |
59 | |
60 void ShutDown() { | |
61 if (!io_task_runner_) | |
62 return; | |
63 bool posted = io_task_runner_->PostTask( | |
64 FROM_HERE, | |
65 base::Bind(&IOThreadContext::ShutDownOnIOThread, this)); | |
66 DCHECK(posted); | |
67 } | |
68 | |
69 void GetRemoteInterfaceOnIOThread( | |
70 const mojo::String& interface_name, | |
71 mojo::ScopedMessagePipeHandle request_handle) { | |
72 if (connection_) { | |
73 connection_->GetRemoteInterfaces()->GetInterface( | |
74 interface_name, std::move(request_handle)); | |
75 } | |
76 } | |
77 | |
78 void SetProcessHandle(base::ProcessHandle handle) { | |
79 DCHECK(io_task_runner_); | |
80 io_task_runner_->PostTask( | |
81 FROM_HERE, | |
82 base::Bind(&IOThreadContext::SetProcessHandleOnIOThread, this, handle)); | |
83 } | |
84 | |
85 private: | |
86 friend class base::RefCountedThreadSafe<IOThreadContext>; | |
87 | |
88 virtual ~IOThreadContext() {} | |
89 | |
90 void InitializeOnIOThread( | |
91 const std::string& application_name, | |
92 const std::string& instance_id, | |
93 std::unique_ptr<shell::Connector> connector, | |
94 mojo::ScopedMessagePipeHandle service_pipe, | |
95 const shell::InterfaceRegistry::Binder& default_binder) { | |
96 shell::mojom::ServicePtr service; | |
97 service.Bind(mojo::InterfacePtrInfo<shell::mojom::Service>( | |
98 std::move(service_pipe), 0u)); | |
99 shell::mojom::PIDReceiverRequest pid_receiver_request = | |
100 mojo::GetProxy(&pid_receiver_); | |
101 | |
102 shell::Identity target(application_name, shell::mojom::kInheritUserID, | |
103 instance_id); | |
104 shell::Connector::ConnectParams params(target); | |
105 params.set_client_process_connection(std::move(service), | |
106 std::move(pid_receiver_request)); | |
107 | |
108 // In some unit testing scenarios a null connector is passed. | |
109 if (!connector) | |
110 return; | |
111 | |
112 connection_ = connector->Connect(¶ms); | |
113 connection_->GetInterfaceRegistry()->set_default_binder(default_binder); | |
114 } | |
115 | |
116 void ShutDownOnIOThread() { | |
117 connection_.reset(); | |
118 pid_receiver_.reset(); | |
119 } | |
120 | |
121 void SetProcessHandleOnIOThread(base::ProcessHandle handle) { | |
122 DCHECK(pid_receiver_.is_bound()); | |
123 pid_receiver_->SetPID(base::GetProcId(handle)); | |
124 pid_receiver_.reset(); | |
125 } | |
126 | |
127 scoped_refptr<base::SequencedTaskRunner> io_task_runner_; | |
128 std::unique_ptr<shell::Connection> connection_; | |
129 shell::mojom::PIDReceiverPtr pid_receiver_; | |
130 | |
131 DISALLOW_COPY_AND_ASSIGN(IOThreadContext); | |
132 }; | |
133 | |
134 MojoChildConnection::MojoChildConnection( | |
135 const std::string& application_name, | |
136 const std::string& instance_id, | |
137 const std::string& child_token, | |
138 shell::Connector* connector, | |
139 scoped_refptr<base::SequencedTaskRunner> io_task_runner) | |
140 : context_(new IOThreadContext), | |
141 service_token_(mojo::edk::GenerateRandomToken()), | |
142 interface_registry_(nullptr), | |
143 weak_factory_(this) { | |
144 mojo::ScopedMessagePipeHandle service_pipe = | 24 mojo::ScopedMessagePipeHandle service_pipe = |
145 mojo::edk::CreateParentMessagePipe(service_token_, child_token); | 25 mojo::edk::CreateParentMessagePipe(service_token_, child_token); |
146 | 26 |
147 context_ = new IOThreadContext; | 27 shell::mojom::ServicePtr service; |
148 context_->Initialize( | 28 service.Bind(mojo::InterfacePtrInfo<shell::mojom::Service>( |
149 application_name, instance_id, connector, std::move(service_pipe), | 29 std::move(service_pipe), 0u)); |
150 io_task_runner, | 30 shell::mojom::PIDReceiverRequest pid_receiver_request = |
151 base::Bind(&MojoChildConnection::GetInterface, | 31 GetProxy(&pid_receiver_); |
152 weak_factory_.GetWeakPtr())); | |
153 remote_interfaces_.Forward( | |
154 base::Bind(&CallBinderOnTaskRunner, | |
155 base::Bind(&IOThreadContext::GetRemoteInterfaceOnIOThread, | |
156 context_), io_task_runner)); | |
157 | 32 |
| 33 shell::Identity target(application_name, shell::mojom::kInheritUserID, |
| 34 instance_id); |
| 35 shell::Connector::ConnectParams params(target); |
| 36 params.set_client_process_connection(std::move(service), |
| 37 std::move(pid_receiver_request)); |
| 38 |
| 39 // In some unit testing scenarios a null connector is passed. |
| 40 if (!connector) |
| 41 return; |
| 42 |
| 43 connection_ = connector->Connect(¶ms); |
158 #if defined(OS_ANDROID) | 44 #if defined(OS_ANDROID) |
159 service_registry_android_ = | 45 service_registry_android_ = ServiceRegistryAndroid::Create( |
160 ServiceRegistryAndroid::Create(&interface_registry_, &remote_interfaces_); | 46 connection_->GetInterfaceRegistry(), connection_->GetRemoteInterfaces()); |
161 #endif | 47 #endif |
162 } | 48 } |
163 | 49 |
164 MojoChildConnection::~MojoChildConnection() { | 50 MojoChildConnection::~MojoChildConnection() {} |
165 context_->ShutDown(); | |
166 } | |
167 | 51 |
168 void MojoChildConnection::SetProcessHandle(base::ProcessHandle handle) { | 52 void MojoChildConnection::SetProcessHandle(base::ProcessHandle handle) { |
169 context_->SetProcessHandle(handle); | 53 DCHECK(pid_receiver_.is_bound()); |
170 } | 54 pid_receiver_->SetPID(base::GetProcId(handle)); |
171 | 55 pid_receiver_.reset(); |
172 void MojoChildConnection::GetInterface( | |
173 const mojo::String& interface_name, | |
174 mojo::ScopedMessagePipeHandle request_handle) { | |
175 static_cast<shell::mojom::InterfaceProvider*>(&interface_registry_) | |
176 ->GetInterface(interface_name, std::move(request_handle)); | |
177 } | 56 } |
178 | 57 |
179 } // namespace content | 58 } // namespace content |
OLD | NEW |