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