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/common/service_manager/child_connection.h" | 5 #include "content/common/service_manager/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" | 10 #include "base/macros.h" |
| 11 #include "content/common/child.mojom.h" |
11 #include "content/public/common/service_manager_connection.h" | 12 #include "content/public/common/service_manager_connection.h" |
12 #include "mojo/edk/embedder/embedder.h" | 13 #include "mojo/edk/embedder/embedder.h" |
13 #include "mojo/public/cpp/system/message_pipe.h" | 14 #include "mojo/public/cpp/system/message_pipe.h" |
14 #include "services/service_manager/public/cpp/connector.h" | 15 #include "services/service_manager/public/cpp/connector.h" |
15 #include "services/service_manager/public/cpp/identity.h" | 16 #include "services/service_manager/public/cpp/identity.h" |
16 #include "services/service_manager/public/cpp/interface_registry.h" | 17 #include "services/service_manager/public/cpp/interface_registry.h" |
17 #include "services/service_manager/public/interfaces/service.mojom.h" | 18 #include "services/service_manager/public/interfaces/service.mojom.h" |
18 | 19 |
19 namespace content { | 20 namespace content { |
20 | 21 |
21 namespace { | |
22 | |
23 void CallBinderOnTaskRunner( | |
24 const service_manager::InterfaceRegistry::Binder& binder, | |
25 scoped_refptr<base::SequencedTaskRunner> task_runner, | |
26 const std::string& interface_name, | |
27 mojo::ScopedMessagePipeHandle request_handle) { | |
28 task_runner->PostTask( | |
29 FROM_HERE, | |
30 base::Bind(binder, interface_name, base::Passed(&request_handle))); | |
31 } | |
32 | |
33 } // namespace | |
34 | |
35 class ChildConnection::IOThreadContext | 22 class ChildConnection::IOThreadContext |
36 : public base::RefCountedThreadSafe<IOThreadContext> { | 23 : public base::RefCountedThreadSafe<IOThreadContext> { |
37 public: | 24 public: |
38 IOThreadContext() {} | 25 IOThreadContext() {} |
39 | 26 |
40 void Initialize(const service_manager::Identity& child_identity, | 27 void Initialize(const service_manager::Identity& child_identity, |
41 service_manager::Connector* connector, | 28 service_manager::Connector* connector, |
42 mojo::ScopedMessagePipeHandle service_pipe, | 29 mojo::ScopedMessagePipeHandle service_pipe, |
43 scoped_refptr<base::SequencedTaskRunner> io_task_runner) { | 30 scoped_refptr<base::SequencedTaskRunner> io_task_runner) { |
44 DCHECK(!io_task_runner_); | 31 DCHECK(!io_task_runner_); |
45 io_task_runner_ = io_task_runner; | 32 io_task_runner_ = io_task_runner; |
46 std::unique_ptr<service_manager::Connector> io_thread_connector; | 33 std::unique_ptr<service_manager::Connector> io_thread_connector; |
47 if (connector) | 34 if (connector) |
48 io_thread_connector = connector->Clone(); | 35 connector_ = connector->Clone(); |
| 36 child_identity_ = child_identity; |
49 io_task_runner_->PostTask( | 37 io_task_runner_->PostTask( |
50 FROM_HERE, | 38 FROM_HERE, |
51 base::Bind(&IOThreadContext::InitializeOnIOThread, this, | 39 base::Bind(&IOThreadContext::InitializeOnIOThread, this, |
52 child_identity, | 40 child_identity, |
53 base::Passed(&io_thread_connector), | |
54 base::Passed(&service_pipe))); | 41 base::Passed(&service_pipe))); |
55 } | 42 } |
56 | 43 |
| 44 void BindInterface(const std::string& interface_name, |
| 45 mojo::ScopedMessagePipeHandle interface_pipe) { |
| 46 io_task_runner_->PostTask( |
| 47 FROM_HERE, base::Bind(&IOThreadContext::BindInterfaceOnIOThread, this, |
| 48 interface_name, base::Passed(&interface_pipe))); |
| 49 } |
| 50 |
57 void ShutDown() { | 51 void ShutDown() { |
58 if (!io_task_runner_) | 52 if (!io_task_runner_) |
59 return; | 53 return; |
60 bool posted = io_task_runner_->PostTask( | 54 bool posted = io_task_runner_->PostTask( |
61 FROM_HERE, | 55 FROM_HERE, |
62 base::Bind(&IOThreadContext::ShutDownOnIOThread, this)); | 56 base::Bind(&IOThreadContext::ShutDownOnIOThread, this)); |
63 DCHECK(posted); | 57 DCHECK(posted); |
64 } | 58 } |
65 | 59 |
66 void GetRemoteInterfaceOnIOThread( | 60 void BindInterfaceOnIOThread(const std::string& interface_name, |
67 const std::string& interface_name, | 61 mojo::ScopedMessagePipeHandle request_handle) { |
68 mojo::ScopedMessagePipeHandle request_handle) { | 62 if (connector_) { |
69 if (connection_) { | 63 connector_->BindInterface(child_identity_, interface_name, |
70 connection_->GetRemoteInterfaces()->GetInterface( | 64 std::move(request_handle)); |
71 interface_name, std::move(request_handle)); | |
72 } | 65 } |
73 } | 66 } |
74 | 67 |
75 void SetProcessHandle(base::ProcessHandle handle) { | 68 void SetProcessHandle(base::ProcessHandle handle) { |
76 DCHECK(io_task_runner_); | 69 DCHECK(io_task_runner_); |
77 io_task_runner_->PostTask( | 70 io_task_runner_->PostTask( |
78 FROM_HERE, | 71 FROM_HERE, |
79 base::Bind(&IOThreadContext::SetProcessHandleOnIOThread, this, handle)); | 72 base::Bind(&IOThreadContext::SetProcessHandleOnIOThread, this, handle)); |
80 } | 73 } |
81 | 74 |
82 private: | 75 private: |
83 friend class base::RefCountedThreadSafe<IOThreadContext>; | 76 friend class base::RefCountedThreadSafe<IOThreadContext>; |
84 | 77 |
85 virtual ~IOThreadContext() {} | 78 virtual ~IOThreadContext() {} |
86 | 79 |
87 void InitializeOnIOThread( | 80 void InitializeOnIOThread( |
88 const service_manager::Identity& child_identity, | 81 const service_manager::Identity& child_identity, |
89 std::unique_ptr<service_manager::Connector> connector, | |
90 mojo::ScopedMessagePipeHandle service_pipe) { | 82 mojo::ScopedMessagePipeHandle service_pipe) { |
91 service_manager::mojom::ServicePtr service; | 83 service_manager::mojom::ServicePtr service; |
92 service.Bind(mojo::InterfacePtrInfo<service_manager::mojom::Service>( | 84 service.Bind(mojo::InterfacePtrInfo<service_manager::mojom::Service>( |
93 std::move(service_pipe), 0u)); | 85 std::move(service_pipe), 0u)); |
94 service_manager::mojom::PIDReceiverRequest pid_receiver_request( | 86 service_manager::mojom::PIDReceiverRequest pid_receiver_request( |
95 &pid_receiver_); | 87 &pid_receiver_); |
96 | 88 |
97 if (connector) { | 89 if (connector_) { |
98 connector->StartService(child_identity, | 90 connector_->StartService(child_identity, std::move(service), |
99 std::move(service), | 91 std::move(pid_receiver_request)); |
100 std::move(pid_receiver_request)); | 92 connector_->BindInterface(child_identity, &child_); |
101 connection_ = connector->Connect(child_identity); | |
102 } | 93 } |
103 } | 94 } |
104 | 95 |
105 void ShutDownOnIOThread() { | 96 void ShutDownOnIOThread() { |
106 connection_.reset(); | 97 connector_.reset(); |
107 pid_receiver_.reset(); | 98 pid_receiver_.reset(); |
108 } | 99 } |
109 | 100 |
110 void SetProcessHandleOnIOThread(base::ProcessHandle handle) { | 101 void SetProcessHandleOnIOThread(base::ProcessHandle handle) { |
111 DCHECK(pid_receiver_.is_bound()); | 102 DCHECK(pid_receiver_.is_bound()); |
112 pid_receiver_->SetPID(base::GetProcId(handle)); | 103 pid_receiver_->SetPID(base::GetProcId(handle)); |
113 pid_receiver_.reset(); | 104 pid_receiver_.reset(); |
114 } | 105 } |
115 | 106 |
116 scoped_refptr<base::SequencedTaskRunner> io_task_runner_; | 107 scoped_refptr<base::SequencedTaskRunner> io_task_runner_; |
117 std::unique_ptr<service_manager::Connection> connection_; | 108 // Usable from the IO thread only. |
| 109 std::unique_ptr<service_manager::Connector> connector_; |
| 110 service_manager::Identity child_identity_; |
| 111 // ServiceManagerConnection in the child monitors the lifetime of this pipe. |
| 112 mojom::ChildPtr child_; |
118 service_manager::mojom::PIDReceiverPtr pid_receiver_; | 113 service_manager::mojom::PIDReceiverPtr pid_receiver_; |
119 | 114 |
120 DISALLOW_COPY_AND_ASSIGN(IOThreadContext); | 115 DISALLOW_COPY_AND_ASSIGN(IOThreadContext); |
121 }; | 116 }; |
122 | 117 |
123 ChildConnection::ChildConnection( | 118 ChildConnection::ChildConnection( |
124 const std::string& service_name, | 119 const std::string& service_name, |
125 const std::string& instance_id, | 120 const std::string& instance_id, |
126 mojo::edk::PendingProcessConnection* process_connection, | 121 mojo::edk::PendingProcessConnection* process_connection, |
127 service_manager::Connector* connector, | 122 service_manager::Connector* connector, |
128 scoped_refptr<base::SequencedTaskRunner> io_task_runner) | 123 scoped_refptr<base::SequencedTaskRunner> io_task_runner) |
129 : context_(new IOThreadContext), | 124 : context_(new IOThreadContext), |
130 child_identity_(service_name, | 125 child_identity_(service_name, |
131 service_manager::mojom::kInheritUserID, | 126 service_manager::mojom::kInheritUserID, |
132 instance_id), | 127 instance_id), |
133 weak_factory_(this) { | 128 weak_factory_(this) { |
134 context_->Initialize(child_identity_, connector, | 129 context_->Initialize(child_identity_, connector, |
135 process_connection->CreateMessagePipe(&service_token_), | 130 process_connection->CreateMessagePipe(&service_token_), |
136 io_task_runner); | 131 io_task_runner); |
137 remote_interfaces_.Forward( | |
138 base::Bind(&CallBinderOnTaskRunner, | |
139 base::Bind(&IOThreadContext::GetRemoteInterfaceOnIOThread, | |
140 context_), io_task_runner)); | |
141 } | 132 } |
142 | 133 |
143 ChildConnection::~ChildConnection() { | 134 ChildConnection::~ChildConnection() { |
144 context_->ShutDown(); | 135 context_->ShutDown(); |
145 } | 136 } |
146 | 137 |
| 138 void ChildConnection::BindInterface( |
| 139 const std::string& interface_name, |
| 140 mojo::ScopedMessagePipeHandle interface_pipe) { |
| 141 context_->BindInterface(interface_name, std::move(interface_pipe)); |
| 142 } |
| 143 |
147 void ChildConnection::SetProcessHandle(base::ProcessHandle handle) { | 144 void ChildConnection::SetProcessHandle(base::ProcessHandle handle) { |
148 process_handle_ = handle; | 145 process_handle_ = handle; |
149 context_->SetProcessHandle(handle); | 146 context_->SetProcessHandle(handle); |
150 } | 147 } |
151 | 148 |
152 } // namespace content | 149 } // namespace content |
OLD | NEW |