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 } |
23 | 25 |
24 ConnectorImpl::ConnectorImpl(mojom::ConnectorPtr connector) | 26 ConnectorImpl::ConnectorImpl(mojom::ConnectorPtr connector) |
25 : connector_(std::move(connector)) { | 27 : connector_(std::move(connector)) { |
26 thread_checker_.reset(new base::ThreadChecker); | |
27 connector_.set_connection_error_handler( | 28 connector_.set_connection_error_handler( |
28 base::Bind(&ConnectorImpl::OnConnectionError, base::Unretained(this))); | 29 base::Bind(&ConnectorImpl::OnConnectionError, base::Unretained(this))); |
29 } | 30 } |
30 | 31 |
31 ConnectorImpl::~ConnectorImpl() {} | 32 ConnectorImpl::~ConnectorImpl() {} |
32 | 33 |
33 void ConnectorImpl::OnConnectionError() { | 34 void ConnectorImpl::OnConnectionError() { |
34 DCHECK(thread_checker_->CalledOnValidThread()); | 35 DCHECK(thread_checker_.CalledOnValidThread()); |
35 connector_.reset(); | 36 connector_.reset(); |
36 } | 37 } |
37 | 38 |
38 std::unique_ptr<Connection> ConnectorImpl::Connect(const std::string& name) { | 39 std::unique_ptr<Connection> ConnectorImpl::Connect(const std::string& name) { |
39 ConnectParams params(name); | 40 ConnectParams params(name); |
40 return Connect(¶ms); | 41 return Connect(¶ms); |
41 } | 42 } |
42 | 43 |
43 std::unique_ptr<Connection> ConnectorImpl::Connect(ConnectParams* params) { | 44 std::unique_ptr<Connection> ConnectorImpl::Connect(ConnectParams* params) { |
44 // Bind this object to the current thread the first time it is used to | 45 if (!BindIfNecessary()) |
45 // connect. | 46 return nullptr; |
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()); | |
59 | 47 |
| 48 DCHECK(thread_checker_.CalledOnValidThread()); |
60 DCHECK(params); | 49 DCHECK(params); |
| 50 |
61 // We allow all interfaces on outgoing connections since we are presumably in | 51 // We allow all interfaces on outgoing connections since we are presumably in |
62 // a position to know who we're talking to. | 52 // a position to know who we're talking to. |
63 CapabilityRequest request; | 53 CapabilityRequest request; |
64 request.interfaces.insert("*"); | 54 request.interfaces.insert("*"); |
65 mojom::InterfaceProviderPtr local_interfaces; | 55 mojom::InterfaceProviderPtr local_interfaces; |
66 mojom::InterfaceProviderRequest local_request = GetProxy(&local_interfaces); | 56 mojom::InterfaceProviderRequest local_request = GetProxy(&local_interfaces); |
67 mojom::InterfaceProviderPtr remote_interfaces; | 57 mojom::InterfaceProviderPtr remote_interfaces; |
68 mojom::InterfaceProviderRequest remote_request = GetProxy(&remote_interfaces); | 58 mojom::InterfaceProviderRequest remote_request = GetProxy(&remote_interfaces); |
69 std::unique_ptr<internal::ConnectionImpl> registry( | 59 std::unique_ptr<internal::ConnectionImpl> registry( |
70 new internal::ConnectionImpl( | 60 new internal::ConnectionImpl( |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
105 return std::move(registry); | 95 return std::move(registry); |
106 } | 96 } |
107 connector_->Connect(mojom::Identity::From(params->target()), | 97 connector_->Connect(mojom::Identity::From(params->target()), |
108 std::move(remote_request), std::move(local_interfaces), | 98 std::move(remote_request), std::move(local_interfaces), |
109 std::move(client_process_connection), | 99 std::move(client_process_connection), |
110 registry->GetConnectCallback()); | 100 registry->GetConnectCallback()); |
111 return std::move(registry); | 101 return std::move(registry); |
112 } | 102 } |
113 | 103 |
114 std::unique_ptr<Connector> ConnectorImpl::Clone() { | 104 std::unique_ptr<Connector> ConnectorImpl::Clone() { |
| 105 if (!BindIfNecessary()) |
| 106 return nullptr; |
| 107 |
115 mojom::ConnectorPtr connector; | 108 mojom::ConnectorPtr connector; |
116 connector_->Clone(GetProxy(&connector)); | 109 mojom::ConnectorRequest request = GetProxy(&connector); |
117 return base::WrapUnique(new ConnectorImpl(connector.PassInterface())); | 110 connector_->Clone(std::move(request)); |
| 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()); |
118 } | 140 } |
119 | 141 |
120 } // namespace shell | 142 } // namespace shell |
OLD | NEW |