OLD | NEW |
| (Empty) |
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 | |
3 // found in the LICENSE file. | |
4 | |
5 #include "services/shell/public/cpp/lib/connector_impl.h" | |
6 | |
7 #include "base/memory/ptr_util.h" | |
8 #include "services/shell/public/cpp/identity.h" | |
9 #include "services/shell/public/cpp/lib/connection_impl.h" | |
10 | |
11 namespace shell { | |
12 | |
13 Connector::ConnectParams::ConnectParams(const Identity& target) | |
14 : target_(target) {} | |
15 | |
16 Connector::ConnectParams::ConnectParams(const std::string& name) | |
17 : target_(name, mojom::kInheritUserID) {} | |
18 | |
19 Connector::ConnectParams::~ConnectParams() {} | |
20 | |
21 ConnectorImpl::ConnectorImpl(mojom::ConnectorPtrInfo unbound_state) | |
22 : unbound_state_(std::move(unbound_state)) { | |
23 thread_checker_.DetachFromThread(); | |
24 } | |
25 | |
26 ConnectorImpl::ConnectorImpl(mojom::ConnectorPtr connector) | |
27 : connector_(std::move(connector)) { | |
28 connector_.set_connection_error_handler( | |
29 base::Bind(&ConnectorImpl::OnConnectionError, base::Unretained(this))); | |
30 } | |
31 | |
32 ConnectorImpl::~ConnectorImpl() {} | |
33 | |
34 void ConnectorImpl::OnConnectionError() { | |
35 DCHECK(thread_checker_.CalledOnValidThread()); | |
36 connector_.reset(); | |
37 } | |
38 | |
39 std::unique_ptr<Connection> ConnectorImpl::Connect(const std::string& name) { | |
40 ConnectParams params(name); | |
41 return Connect(¶ms); | |
42 } | |
43 | |
44 std::unique_ptr<Connection> ConnectorImpl::Connect(ConnectParams* params) { | |
45 if (!BindIfNecessary()) | |
46 return nullptr; | |
47 | |
48 DCHECK(thread_checker_.CalledOnValidThread()); | |
49 DCHECK(params); | |
50 | |
51 mojom::InterfaceProviderPtr remote_interfaces; | |
52 mojom::InterfaceProviderRequest remote_request = GetProxy(&remote_interfaces); | |
53 std::unique_ptr<internal::ConnectionImpl> connection( | |
54 new internal::ConnectionImpl(params->target(), | |
55 Connection::State::PENDING)); | |
56 if (params->remote_interfaces()) { | |
57 params->remote_interfaces()->Bind(std::move(remote_interfaces)); | |
58 connection->set_remote_interfaces(params->remote_interfaces()); | |
59 } else { | |
60 std::unique_ptr<InterfaceProvider> remote_interface_provider( | |
61 new InterfaceProvider); | |
62 remote_interface_provider->Bind(std::move(remote_interfaces)); | |
63 connection->SetRemoteInterfaces(std::move(remote_interface_provider)); | |
64 } | |
65 | |
66 mojom::ServicePtr service; | |
67 mojom::PIDReceiverRequest pid_receiver_request; | |
68 params->TakeClientProcessConnection(&service, &pid_receiver_request); | |
69 mojom::ClientProcessConnectionPtr client_process_connection; | |
70 if (service.is_bound() && pid_receiver_request.is_pending()) { | |
71 client_process_connection = mojom::ClientProcessConnection::New(); | |
72 client_process_connection->service = | |
73 service.PassInterface().PassHandle(); | |
74 client_process_connection->pid_receiver_request = | |
75 pid_receiver_request.PassMessagePipe(); | |
76 } else if (service.is_bound() || pid_receiver_request.is_pending()) { | |
77 NOTREACHED() << "If one of service or pid_receiver_request is valid, " | |
78 << "both must be valid."; | |
79 return std::move(connection); | |
80 } | |
81 connector_->Connect(params->target(), std::move(remote_request), | |
82 std::move(client_process_connection), | |
83 connection->GetConnectCallback()); | |
84 return std::move(connection); | |
85 } | |
86 | |
87 std::unique_ptr<Connector> ConnectorImpl::Clone() { | |
88 if (!BindIfNecessary()) | |
89 return nullptr; | |
90 | |
91 mojom::ConnectorPtr connector; | |
92 mojom::ConnectorRequest request = GetProxy(&connector); | |
93 connector_->Clone(std::move(request)); | |
94 return base::MakeUnique<ConnectorImpl>(connector.PassInterface()); | |
95 } | |
96 | |
97 bool ConnectorImpl::BindIfNecessary() { | |
98 // Bind this object to the current thread the first time it is used to | |
99 // connect. | |
100 if (!connector_.is_bound()) { | |
101 if (!unbound_state_.is_valid()) { | |
102 // It's possible to get here when the link to the shell has been severed | |
103 // (and so the connector pipe has been closed) but the app has chosen not | |
104 // to quit. | |
105 return false; | |
106 } | |
107 | |
108 // Bind the ThreadChecker to this thread. | |
109 DCHECK(thread_checker_.CalledOnValidThread()); | |
110 | |
111 connector_.Bind(std::move(unbound_state_)); | |
112 connector_.set_connection_error_handler( | |
113 base::Bind(&ConnectorImpl::OnConnectionError, base::Unretained(this))); | |
114 } | |
115 | |
116 return true; | |
117 } | |
118 | |
119 std::unique_ptr<Connector> Connector::Create(mojom::ConnectorRequest* request) { | |
120 mojom::ConnectorPtr proxy; | |
121 *request = mojo::GetProxy(&proxy); | |
122 return base::MakeUnique<ConnectorImpl>(proxy.PassInterface()); | |
123 } | |
124 | |
125 } // namespace shell | |
OLD | NEW |