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 "base/memory/ptr_util.h" |
8 #include "services/shell/public/cpp/identity.h" | 8 #include "services/shell/public/cpp/identity.h" |
9 #include "services/shell/public/cpp/lib/connection_impl.h" | 9 #include "services/shell/public/cpp/lib/connection_impl.h" |
10 | 10 |
11 namespace shell { | 11 namespace shell { |
12 | 12 |
13 Connector::ConnectParams::ConnectParams(const Identity& target) | 13 Connector::ConnectParams::ConnectParams(const Identity& target) |
14 : target_(target) {} | 14 : target_(target) {} |
15 | 15 |
16 Connector::ConnectParams::ConnectParams(const std::string& name) | 16 Connector::ConnectParams::ConnectParams(const std::string& name) |
17 : target_(name, mojom::kInheritUserID) {} | 17 : target_(name, mojom::kInheritUserID) {} |
18 | 18 |
19 Connector::ConnectParams::~ConnectParams() {} | 19 Connector::ConnectParams::~ConnectParams() {} |
20 | 20 |
21 ConnectorImpl::ConnectorImpl(mojom::ConnectorPtrInfo unbound_state) | 21 ConnectorImpl::ConnectorImpl(mojom::ConnectorPtrInfo unbound_state) |
22 : unbound_state_(std::move(unbound_state)) { | 22 : unbound_state_(std::move(unbound_state)) {} |
23 thread_checker_.DetachFromThread(); | |
24 } | |
25 | 23 |
26 ConnectorImpl::ConnectorImpl(mojom::ConnectorPtr connector) | 24 ConnectorImpl::ConnectorImpl(mojom::ConnectorPtr connector) |
27 : connector_(std::move(connector)) { | 25 : connector_(std::move(connector)) { |
| 26 thread_checker_.reset(new base::ThreadChecker); |
28 connector_.set_connection_error_handler( | 27 connector_.set_connection_error_handler( |
29 base::Bind(&ConnectorImpl::OnConnectionError, base::Unretained(this))); | 28 base::Bind(&ConnectorImpl::OnConnectionError, base::Unretained(this))); |
30 } | 29 } |
31 | 30 |
32 ConnectorImpl::~ConnectorImpl() {} | 31 ConnectorImpl::~ConnectorImpl() {} |
33 | 32 |
34 void ConnectorImpl::OnConnectionError() { | 33 void ConnectorImpl::OnConnectionError() { |
35 DCHECK(thread_checker_.CalledOnValidThread()); | 34 DCHECK(thread_checker_->CalledOnValidThread()); |
36 connector_.reset(); | 35 connector_.reset(); |
37 } | 36 } |
38 | 37 |
39 std::unique_ptr<Connection> ConnectorImpl::Connect(const std::string& name) { | 38 std::unique_ptr<Connection> ConnectorImpl::Connect(const std::string& name) { |
40 ConnectParams params(name); | 39 ConnectParams params(name); |
41 return Connect(¶ms); | 40 return Connect(¶ms); |
42 } | 41 } |
43 | 42 |
44 std::unique_ptr<Connection> ConnectorImpl::Connect(ConnectParams* params) { | 43 std::unique_ptr<Connection> ConnectorImpl::Connect(ConnectParams* params) { |
45 if (!BindIfNecessary()) | 44 // Bind this object to the current thread the first time it is used to |
46 return nullptr; | 45 // connect. |
| 46 if (!connector_.is_bound()) { |
| 47 if (!unbound_state_.is_valid()) { |
| 48 // It's possible to get here when the link to the shell has been severed |
| 49 // (and so the connector pipe has been closed) but the app has chosen not |
| 50 // to quit. |
| 51 return nullptr; |
| 52 } |
| 53 connector_.Bind(std::move(unbound_state_)); |
| 54 connector_.set_connection_error_handler( |
| 55 base::Bind(&ConnectorImpl::OnConnectionError, base::Unretained(this))); |
| 56 thread_checker_.reset(new base::ThreadChecker); |
| 57 } |
| 58 DCHECK(thread_checker_->CalledOnValidThread()); |
47 | 59 |
48 DCHECK(thread_checker_.CalledOnValidThread()); | |
49 DCHECK(params); | 60 DCHECK(params); |
50 | |
51 // We allow all interfaces on outgoing connections since we are presumably in | 61 // We allow all interfaces on outgoing connections since we are presumably in |
52 // a position to know who we're talking to. | 62 // a position to know who we're talking to. |
53 CapabilityRequest request; | 63 CapabilityRequest request; |
54 request.interfaces.insert("*"); | 64 request.interfaces.insert("*"); |
55 mojom::InterfaceProviderPtr local_interfaces; | 65 mojom::InterfaceProviderPtr local_interfaces; |
56 mojom::InterfaceProviderRequest local_request = GetProxy(&local_interfaces); | 66 mojom::InterfaceProviderRequest local_request = GetProxy(&local_interfaces); |
57 mojom::InterfaceProviderPtr remote_interfaces; | 67 mojom::InterfaceProviderPtr remote_interfaces; |
58 mojom::InterfaceProviderRequest remote_request = GetProxy(&remote_interfaces); | 68 mojom::InterfaceProviderRequest remote_request = GetProxy(&remote_interfaces); |
59 std::unique_ptr<internal::ConnectionImpl> registry( | 69 std::unique_ptr<internal::ConnectionImpl> registry( |
60 new internal::ConnectionImpl( | 70 new internal::ConnectionImpl( |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
95 return std::move(registry); | 105 return std::move(registry); |
96 } | 106 } |
97 connector_->Connect(mojom::Identity::From(params->target()), | 107 connector_->Connect(mojom::Identity::From(params->target()), |
98 std::move(remote_request), std::move(local_interfaces), | 108 std::move(remote_request), std::move(local_interfaces), |
99 std::move(client_process_connection), | 109 std::move(client_process_connection), |
100 registry->GetConnectCallback()); | 110 registry->GetConnectCallback()); |
101 return std::move(registry); | 111 return std::move(registry); |
102 } | 112 } |
103 | 113 |
104 std::unique_ptr<Connector> ConnectorImpl::Clone() { | 114 std::unique_ptr<Connector> ConnectorImpl::Clone() { |
105 if (!BindIfNecessary()) | |
106 return nullptr; | |
107 | |
108 mojom::ConnectorPtr connector; | 115 mojom::ConnectorPtr connector; |
109 mojom::ConnectorRequest request = GetProxy(&connector); | 116 connector_->Clone(GetProxy(&connector)); |
110 connector_->Clone(std::move(request)); | 117 return base::WrapUnique(new ConnectorImpl(connector.PassInterface())); |
111 return base::MakeUnique<ConnectorImpl>(connector.PassInterface()); | |
112 } | |
113 | |
114 bool ConnectorImpl::BindIfNecessary() { | |
115 // Bind this object to the current thread the first time it is used to | |
116 // connect. | |
117 if (!connector_.is_bound()) { | |
118 if (!unbound_state_.is_valid()) { | |
119 // It's possible to get here when the link to the shell has been severed | |
120 // (and so the connector pipe has been closed) but the app has chosen not | |
121 // to quit. | |
122 return false; | |
123 } | |
124 | |
125 // Bind the ThreadChecker to this thread. | |
126 DCHECK(thread_checker_.CalledOnValidThread()); | |
127 | |
128 connector_.Bind(std::move(unbound_state_)); | |
129 connector_.set_connection_error_handler( | |
130 base::Bind(&ConnectorImpl::OnConnectionError, base::Unretained(this))); | |
131 } | |
132 | |
133 return true; | |
134 } | |
135 | |
136 std::unique_ptr<Connector> Connector::Create(mojom::ConnectorRequest* request) { | |
137 mojom::ConnectorPtr proxy; | |
138 *request = mojo::GetProxy(&proxy); | |
139 return base::MakeUnique<ConnectorImpl>(proxy.PassInterface()); | |
140 } | 118 } |
141 | 119 |
142 } // namespace shell | 120 } // namespace shell |
OLD | NEW |