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" | 10 #include "base/macros.h" |
(...skipping 18 matching lines...) Expand all Loading... |
29 base::Bind(binder, interface_name, base::Passed(&request_handle))); | 29 base::Bind(binder, interface_name, base::Passed(&request_handle))); |
30 } | 30 } |
31 | 31 |
32 } // namespace | 32 } // namespace |
33 | 33 |
34 class MojoChildConnection::IOThreadContext | 34 class MojoChildConnection::IOThreadContext |
35 : public base::RefCountedThreadSafe<IOThreadContext> { | 35 : public base::RefCountedThreadSafe<IOThreadContext> { |
36 public: | 36 public: |
37 IOThreadContext() {} | 37 IOThreadContext() {} |
38 | 38 |
39 void Initialize(const std::string& application_name, | 39 void Initialize(const shell::Identity& child_identity, |
40 const std::string& instance_id, | |
41 shell::Connector* connector, | 40 shell::Connector* connector, |
42 mojo::ScopedMessagePipeHandle service_pipe, | 41 mojo::ScopedMessagePipeHandle service_pipe, |
43 scoped_refptr<base::SequencedTaskRunner> io_task_runner, | 42 scoped_refptr<base::SequencedTaskRunner> io_task_runner, |
44 const shell::InterfaceRegistry::Binder& default_binder) { | 43 const shell::InterfaceRegistry::Binder& default_binder) { |
45 DCHECK(!io_task_runner_); | 44 DCHECK(!io_task_runner_); |
46 io_task_runner_ = io_task_runner; | 45 io_task_runner_ = io_task_runner; |
47 std::unique_ptr<shell::Connector> io_thread_connector; | 46 std::unique_ptr<shell::Connector> io_thread_connector; |
48 if (connector) | 47 if (connector) |
49 io_thread_connector = connector->Clone(); | 48 io_thread_connector = connector->Clone(); |
50 io_task_runner_->PostTask( | 49 io_task_runner_->PostTask( |
51 FROM_HERE, | 50 FROM_HERE, |
52 base::Bind(&IOThreadContext::InitializeOnIOThread, this, | 51 base::Bind(&IOThreadContext::InitializeOnIOThread, this, |
53 application_name, instance_id, | 52 child_identity, |
54 base::Passed(&io_thread_connector), | 53 base::Passed(&io_thread_connector), |
55 base::Passed(&service_pipe), | 54 base::Passed(&service_pipe), |
56 base::Bind(&CallBinderOnTaskRunner, default_binder, | 55 base::Bind(&CallBinderOnTaskRunner, default_binder, |
57 base::ThreadTaskRunnerHandle::Get()))); | 56 base::ThreadTaskRunnerHandle::Get()))); |
58 } | 57 } |
59 | 58 |
60 void ShutDown() { | 59 void ShutDown() { |
61 if (!io_task_runner_) | 60 if (!io_task_runner_) |
62 return; | 61 return; |
63 bool posted = io_task_runner_->PostTask( | 62 bool posted = io_task_runner_->PostTask( |
(...skipping 17 matching lines...) Expand all Loading... |
81 FROM_HERE, | 80 FROM_HERE, |
82 base::Bind(&IOThreadContext::SetProcessHandleOnIOThread, this, handle)); | 81 base::Bind(&IOThreadContext::SetProcessHandleOnIOThread, this, handle)); |
83 } | 82 } |
84 | 83 |
85 private: | 84 private: |
86 friend class base::RefCountedThreadSafe<IOThreadContext>; | 85 friend class base::RefCountedThreadSafe<IOThreadContext>; |
87 | 86 |
88 virtual ~IOThreadContext() {} | 87 virtual ~IOThreadContext() {} |
89 | 88 |
90 void InitializeOnIOThread( | 89 void InitializeOnIOThread( |
91 const std::string& application_name, | 90 const shell::Identity& child_identity, |
92 const std::string& instance_id, | |
93 std::unique_ptr<shell::Connector> connector, | 91 std::unique_ptr<shell::Connector> connector, |
94 mojo::ScopedMessagePipeHandle service_pipe, | 92 mojo::ScopedMessagePipeHandle service_pipe, |
95 const shell::InterfaceRegistry::Binder& default_binder) { | 93 const shell::InterfaceRegistry::Binder& default_binder) { |
96 shell::mojom::ServicePtr service; | 94 shell::mojom::ServicePtr service; |
97 service.Bind(mojo::InterfacePtrInfo<shell::mojom::Service>( | 95 service.Bind(mojo::InterfacePtrInfo<shell::mojom::Service>( |
98 std::move(service_pipe), 0u)); | 96 std::move(service_pipe), 0u)); |
99 shell::mojom::PIDReceiverRequest pid_receiver_request = | 97 shell::mojom::PIDReceiverRequest pid_receiver_request = |
100 mojo::GetProxy(&pid_receiver_); | 98 mojo::GetProxy(&pid_receiver_); |
101 | 99 |
102 shell::Identity target(application_name, shell::mojom::kInheritUserID, | 100 shell::Connector::ConnectParams params(child_identity); |
103 instance_id); | |
104 shell::Connector::ConnectParams params(target); | |
105 params.set_client_process_connection(std::move(service), | 101 params.set_client_process_connection(std::move(service), |
106 std::move(pid_receiver_request)); | 102 std::move(pid_receiver_request)); |
107 | 103 |
108 // In some unit testing scenarios a null connector is passed. | 104 // In some unit testing scenarios a null connector is passed. |
109 if (!connector) | 105 if (!connector) |
110 return; | 106 return; |
111 | 107 |
112 connection_ = connector->Connect(¶ms); | 108 connection_ = connector->Connect(¶ms); |
113 connection_->GetInterfaceRegistry()->set_default_binder(default_binder); | 109 connection_->GetInterfaceRegistry()->set_default_binder(default_binder); |
114 } | 110 } |
(...skipping 10 matching lines...) Expand all Loading... |
125 } | 121 } |
126 | 122 |
127 scoped_refptr<base::SequencedTaskRunner> io_task_runner_; | 123 scoped_refptr<base::SequencedTaskRunner> io_task_runner_; |
128 std::unique_ptr<shell::Connection> connection_; | 124 std::unique_ptr<shell::Connection> connection_; |
129 shell::mojom::PIDReceiverPtr pid_receiver_; | 125 shell::mojom::PIDReceiverPtr pid_receiver_; |
130 | 126 |
131 DISALLOW_COPY_AND_ASSIGN(IOThreadContext); | 127 DISALLOW_COPY_AND_ASSIGN(IOThreadContext); |
132 }; | 128 }; |
133 | 129 |
134 MojoChildConnection::MojoChildConnection( | 130 MojoChildConnection::MojoChildConnection( |
135 const std::string& application_name, | 131 const std::string& service_name, |
136 const std::string& instance_id, | 132 const std::string& instance_id, |
137 const std::string& child_token, | 133 const std::string& child_token, |
138 shell::Connector* connector, | 134 shell::Connector* connector, |
139 scoped_refptr<base::SequencedTaskRunner> io_task_runner) | 135 scoped_refptr<base::SequencedTaskRunner> io_task_runner) |
140 : context_(new IOThreadContext), | 136 : context_(new IOThreadContext), |
| 137 child_identity_(service_name, shell::mojom::kInheritUserID, instance_id), |
141 service_token_(mojo::edk::GenerateRandomToken()), | 138 service_token_(mojo::edk::GenerateRandomToken()), |
142 interface_registry_(nullptr), | 139 interface_registry_(nullptr), |
143 weak_factory_(this) { | 140 weak_factory_(this) { |
144 mojo::ScopedMessagePipeHandle service_pipe = | 141 mojo::ScopedMessagePipeHandle service_pipe = |
145 mojo::edk::CreateParentMessagePipe(service_token_, child_token); | 142 mojo::edk::CreateParentMessagePipe(service_token_, child_token); |
146 | 143 |
147 context_ = new IOThreadContext; | 144 context_ = new IOThreadContext; |
148 context_->Initialize( | 145 context_->Initialize( |
149 application_name, instance_id, connector, std::move(service_pipe), | 146 child_identity_, connector, std::move(service_pipe), |
150 io_task_runner, | 147 io_task_runner, |
151 base::Bind(&MojoChildConnection::GetInterface, | 148 base::Bind(&MojoChildConnection::GetInterface, |
152 weak_factory_.GetWeakPtr())); | 149 weak_factory_.GetWeakPtr())); |
153 remote_interfaces_.Forward( | 150 remote_interfaces_.Forward( |
154 base::Bind(&CallBinderOnTaskRunner, | 151 base::Bind(&CallBinderOnTaskRunner, |
155 base::Bind(&IOThreadContext::GetRemoteInterfaceOnIOThread, | 152 base::Bind(&IOThreadContext::GetRemoteInterfaceOnIOThread, |
156 context_), io_task_runner)); | 153 context_), io_task_runner)); |
157 | |
158 #if defined(OS_ANDROID) | |
159 interface_registry_android_ = | |
160 InterfaceRegistryAndroid::Create(&interface_registry_); | |
161 #endif | |
162 } | 154 } |
163 | 155 |
164 MojoChildConnection::~MojoChildConnection() { | 156 MojoChildConnection::~MojoChildConnection() { |
165 context_->ShutDown(); | 157 context_->ShutDown(); |
166 } | 158 } |
167 | 159 |
168 void MojoChildConnection::SetProcessHandle(base::ProcessHandle handle) { | 160 void MojoChildConnection::SetProcessHandle(base::ProcessHandle handle) { |
169 context_->SetProcessHandle(handle); | 161 context_->SetProcessHandle(handle); |
170 } | 162 } |
171 | 163 |
172 void MojoChildConnection::GetInterface( | 164 void MojoChildConnection::GetInterface( |
173 const mojo::String& interface_name, | 165 const mojo::String& interface_name, |
174 mojo::ScopedMessagePipeHandle request_handle) { | 166 mojo::ScopedMessagePipeHandle request_handle) { |
175 static_cast<shell::mojom::InterfaceProvider*>(&interface_registry_) | 167 static_cast<shell::mojom::InterfaceProvider*>(&interface_registry_) |
176 ->GetInterface(interface_name, std::move(request_handle)); | 168 ->GetInterface(interface_name, std::move(request_handle)); |
177 } | 169 } |
178 | 170 |
179 } // namespace content | 171 } // namespace content |
OLD | NEW |