OLD | NEW |
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 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 "services/shell/public/cpp/lib/connector_impl.h" | 5 #include "services/shell/public/cpp/lib/connector_impl.h" |
6 | 6 |
| 7 #include "base/memory/ptr_util.h" |
7 #include "services/shell/public/cpp/identity.h" | 8 #include "services/shell/public/cpp/identity.h" |
8 #include "services/shell/public/cpp/lib/connection_impl.h" | 9 #include "services/shell/public/cpp/lib/connection_impl.h" |
9 | 10 |
10 namespace mojo { | 11 namespace shell { |
11 | 12 |
12 Connector::ConnectParams::ConnectParams(const Identity& target) | 13 Connector::ConnectParams::ConnectParams(const Identity& target) |
13 : target_(target) {} | 14 : target_(target) {} |
| 15 |
14 Connector::ConnectParams::ConnectParams(const std::string& name) | 16 Connector::ConnectParams::ConnectParams(const std::string& name) |
15 : target_(name, shell::mojom::kInheritUserID) {} | 17 : target_(name, mojom::kInheritUserID) {} |
| 18 |
16 Connector::ConnectParams::~ConnectParams() {} | 19 Connector::ConnectParams::~ConnectParams() {} |
17 | 20 |
18 ConnectorImpl::ConnectorImpl(shell::mojom::ConnectorPtrInfo unbound_state) | 21 ConnectorImpl::ConnectorImpl(mojom::ConnectorPtrInfo unbound_state) |
19 : unbound_state_(std::move(unbound_state)) {} | 22 : unbound_state_(std::move(unbound_state)) {} |
20 ConnectorImpl::ConnectorImpl(shell::mojom::ConnectorPtr connector) | 23 |
| 24 ConnectorImpl::ConnectorImpl(mojom::ConnectorPtr connector) |
21 : connector_(std::move(connector)) { | 25 : connector_(std::move(connector)) { |
22 thread_checker_.reset(new base::ThreadChecker); | 26 thread_checker_.reset(new base::ThreadChecker); |
23 } | 27 } |
| 28 |
24 ConnectorImpl::~ConnectorImpl() {} | 29 ConnectorImpl::~ConnectorImpl() {} |
25 | 30 |
26 scoped_ptr<Connection> ConnectorImpl::Connect(const std::string& name) { | 31 std::unique_ptr<Connection> ConnectorImpl::Connect(const std::string& name) { |
27 ConnectParams params(name); | 32 ConnectParams params(name); |
28 return Connect(¶ms); | 33 return Connect(¶ms); |
29 } | 34 } |
30 | 35 |
31 scoped_ptr<Connection> ConnectorImpl::Connect(ConnectParams* params) { | 36 std::unique_ptr<Connection> ConnectorImpl::Connect(ConnectParams* params) { |
32 // Bind this object to the current thread the first time it is used to | 37 // Bind this object to the current thread the first time it is used to |
33 // connect. | 38 // connect. |
34 if (!connector_.is_bound()) { | 39 if (!connector_.is_bound()) { |
35 if (!unbound_state_.is_valid()) { | 40 if (!unbound_state_.is_valid()) { |
36 // It's possible to get here when the link to the shell has been severed | 41 // It's possible to get here when the link to the shell has been severed |
37 // (and so the connector pipe has been closed) but the app has chosen not | 42 // (and so the connector pipe has been closed) but the app has chosen not |
38 // to quit. | 43 // to quit. |
39 return nullptr; | 44 return nullptr; |
40 } | 45 } |
41 connector_.Bind(std::move(unbound_state_)); | 46 connector_.Bind(std::move(unbound_state_)); |
42 thread_checker_.reset(new base::ThreadChecker); | 47 thread_checker_.reset(new base::ThreadChecker); |
43 } | 48 } |
44 DCHECK(thread_checker_->CalledOnValidThread()); | 49 DCHECK(thread_checker_->CalledOnValidThread()); |
45 | 50 |
46 DCHECK(params); | 51 DCHECK(params); |
47 // We allow all interfaces on outgoing connections since we are presumably in | 52 // We allow all interfaces on outgoing connections since we are presumably in |
48 // a position to know who we're talking to. | 53 // a position to know who we're talking to. |
49 CapabilityRequest request; | 54 CapabilityRequest request; |
50 request.interfaces.insert("*"); | 55 request.interfaces.insert("*"); |
51 shell::mojom::InterfaceProviderPtr local_interfaces; | 56 mojom::InterfaceProviderPtr local_interfaces; |
52 shell::mojom::InterfaceProviderRequest local_request = | 57 mojom::InterfaceProviderRequest local_request = GetProxy(&local_interfaces); |
53 GetProxy(&local_interfaces); | 58 mojom::InterfaceProviderPtr remote_interfaces; |
54 shell::mojom::InterfaceProviderPtr remote_interfaces; | 59 mojom::InterfaceProviderRequest remote_request = GetProxy(&remote_interfaces); |
55 shell::mojom::InterfaceProviderRequest remote_request = | 60 std::unique_ptr<internal::ConnectionImpl> registry( |
56 GetProxy(&remote_interfaces); | 61 new internal::ConnectionImpl( |
57 scoped_ptr<internal::ConnectionImpl> registry(new internal::ConnectionImpl( | 62 params->target().name(), params->target(), mojom::kInvalidInstanceID, |
58 params->target().name(), params->target(), | 63 std::move(remote_interfaces), std::move(local_request), request, |
59 shell::mojom::kInvalidInstanceID, std::move(remote_interfaces), | 64 Connection::State::PENDING)); |
60 std::move(local_request), request, Connection::State::PENDING)); | |
61 | 65 |
62 shell::mojom::ShellClientPtr shell_client; | 66 mojom::ShellClientPtr shell_client; |
63 shell::mojom::PIDReceiverRequest pid_receiver_request; | 67 mojom::PIDReceiverRequest pid_receiver_request; |
64 params->TakeClientProcessConnection(&shell_client, &pid_receiver_request); | 68 params->TakeClientProcessConnection(&shell_client, &pid_receiver_request); |
65 shell::mojom::ClientProcessConnectionPtr client_process_connection; | 69 mojom::ClientProcessConnectionPtr client_process_connection; |
66 if (shell_client.is_bound() && pid_receiver_request.is_pending()) { | 70 if (shell_client.is_bound() && pid_receiver_request.is_pending()) { |
67 client_process_connection = shell::mojom::ClientProcessConnection::New(); | 71 client_process_connection = mojom::ClientProcessConnection::New(); |
68 client_process_connection->shell_client = | 72 client_process_connection->shell_client = |
69 shell_client.PassInterface().PassHandle(); | 73 shell_client.PassInterface().PassHandle(); |
70 client_process_connection->pid_receiver_request = | 74 client_process_connection->pid_receiver_request = |
71 pid_receiver_request.PassMessagePipe(); | 75 pid_receiver_request.PassMessagePipe(); |
72 } else if (shell_client.is_bound() || pid_receiver_request.is_pending()) { | 76 } else if (shell_client.is_bound() || pid_receiver_request.is_pending()) { |
73 NOTREACHED() << "If one of shell_client or pid_receiver_request is valid, " | 77 NOTREACHED() << "If one of shell_client or pid_receiver_request is valid, " |
74 << "both must be valid."; | 78 << "both must be valid."; |
75 return std::move(registry); | 79 return std::move(registry); |
76 } | 80 } |
77 connector_->Connect( | 81 connector_->Connect(mojom::Identity::From(params->target()), |
78 shell::mojom::Identity::From(params->target()), | 82 std::move(remote_request), std::move(local_interfaces), |
79 std::move(remote_request), std::move(local_interfaces), | 83 std::move(client_process_connection), |
80 std::move(client_process_connection), registry->GetConnectCallback()); | 84 registry->GetConnectCallback()); |
81 return std::move(registry); | 85 return std::move(registry); |
82 } | 86 } |
83 | 87 |
84 scoped_ptr<Connector> ConnectorImpl::Clone() { | 88 std::unique_ptr<Connector> ConnectorImpl::Clone() { |
85 shell::mojom::ConnectorPtr connector; | 89 mojom::ConnectorPtr connector; |
86 connector_->Clone(GetProxy(&connector)); | 90 connector_->Clone(GetProxy(&connector)); |
87 return make_scoped_ptr( | 91 return base::WrapUnique(new ConnectorImpl(connector.PassInterface())); |
88 new ConnectorImpl(connector.PassInterface())); | |
89 } | 92 } |
90 | 93 |
91 } // namespace mojo | 94 } // namespace shell |
OLD | NEW |