OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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/connection_impl.h" | 5 #include "services/shell/public/cpp/lib/connection_impl.h" |
6 | 6 |
7 #include <stdint.h> | 7 #include <stdint.h> |
8 | 8 |
9 #include <utility> | 9 #include <utility> |
10 | 10 |
11 #include "base/bind.h" | 11 #include "base/bind.h" |
12 #include "base/logging.h" | 12 #include "base/logging.h" |
13 #include "services/shell/public/cpp/connection.h" | 13 #include "services/shell/public/cpp/connection.h" |
14 #include "services/shell/public/cpp/interface_binder.h" | 14 #include "services/shell/public/cpp/interface_binder.h" |
15 | 15 |
16 namespace shell { | 16 namespace shell { |
17 namespace internal { | 17 namespace internal { |
18 | 18 |
19 //////////////////////////////////////////////////////////////////////////////// | 19 //////////////////////////////////////////////////////////////////////////////// |
20 // ConnectionImpl, public: | 20 // ConnectionImpl, public: |
21 | 21 |
22 ConnectionImpl::ConnectionImpl() | 22 ConnectionImpl::ConnectionImpl() |
23 : allow_all_interfaces_(true), | 23 : weak_factory_(this) {} |
24 weak_factory_(this) {} | |
25 | 24 |
26 ConnectionImpl::ConnectionImpl( | 25 ConnectionImpl::ConnectionImpl(const Identity& remote, State initial_state) |
27 const Identity& remote, | |
28 const CapabilityRequest& capability_request, | |
29 State initial_state) | |
30 : remote_(remote), | 26 : remote_(remote), |
31 state_(initial_state), | 27 state_(initial_state), |
32 capability_request_(capability_request), | |
33 allow_all_interfaces_(capability_request.interfaces.size() == 1 && | |
34 capability_request.interfaces.count("*") == 1), | |
35 weak_factory_(this) { | 28 weak_factory_(this) { |
36 } | 29 } |
37 | 30 |
38 ConnectionImpl::~ConnectionImpl() {} | 31 ConnectionImpl::~ConnectionImpl() {} |
39 | 32 |
40 void ConnectionImpl::SetExposedInterfaces( | |
41 std::unique_ptr<InterfaceRegistry> exposed_interfaces) { | |
42 exposed_interfaces_owner_ = std::move(exposed_interfaces); | |
43 set_exposed_interfaces(exposed_interfaces_owner_.get()); | |
44 } | |
45 | |
46 void ConnectionImpl::SetRemoteInterfaces( | 33 void ConnectionImpl::SetRemoteInterfaces( |
47 std::unique_ptr<InterfaceProvider> remote_interfaces) { | 34 std::unique_ptr<InterfaceProvider> remote_interfaces) { |
48 remote_interfaces_owner_ = std::move(remote_interfaces); | 35 remote_interfaces_owner_ = std::move(remote_interfaces); |
49 set_remote_interfaces(remote_interfaces_owner_.get()); | 36 set_remote_interfaces(remote_interfaces_owner_.get()); |
50 } | 37 } |
51 | 38 |
52 shell::mojom::Connector::ConnectCallback ConnectionImpl::GetConnectCallback() { | 39 shell::mojom::Connector::ConnectCallback ConnectionImpl::GetConnectCallback() { |
53 return base::Bind(&ConnectionImpl::OnConnectionCompleted, | 40 return base::Bind(&ConnectionImpl::OnConnectionCompleted, |
54 weak_factory_.GetWeakPtr()); | 41 weak_factory_.GetWeakPtr()); |
55 } | 42 } |
56 | 43 |
57 //////////////////////////////////////////////////////////////////////////////// | 44 //////////////////////////////////////////////////////////////////////////////// |
58 // ConnectionImpl, Connection implementation: | 45 // ConnectionImpl, Connection implementation: |
59 | 46 |
60 bool ConnectionImpl::HasCapabilityClass(const std::string& class_name) const { | |
61 return capability_request_.classes.count(class_name) > 0; | |
62 } | |
63 | |
64 const Identity& ConnectionImpl::GetRemoteIdentity() const { | 47 const Identity& ConnectionImpl::GetRemoteIdentity() const { |
65 return remote_; | 48 return remote_; |
66 } | 49 } |
67 | 50 |
68 void ConnectionImpl::SetConnectionLostClosure(const base::Closure& handler) { | 51 void ConnectionImpl::SetConnectionLostClosure(const base::Closure& handler) { |
69 if (remote_interfaces_) | 52 remote_interfaces_->SetConnectionLostClosure(handler); |
70 remote_interfaces_->SetConnectionLostClosure(handler); | |
71 else | |
72 exposed_interfaces_->SetConnectionLostClosure(handler); | |
73 } | 53 } |
74 | 54 |
75 shell::mojom::ConnectResult ConnectionImpl::GetResult() const { | 55 shell::mojom::ConnectResult ConnectionImpl::GetResult() const { |
76 return result_; | 56 return result_; |
77 } | 57 } |
78 | 58 |
79 bool ConnectionImpl::IsPending() const { | 59 bool ConnectionImpl::IsPending() const { |
80 return state_ == State::PENDING; | 60 return state_ == State::PENDING; |
81 } | 61 } |
82 | 62 |
83 void ConnectionImpl::AddConnectionCompletedClosure( | 63 void ConnectionImpl::AddConnectionCompletedClosure( |
84 const base::Closure& callback) { | 64 const base::Closure& callback) { |
85 if (IsPending()) | 65 if (IsPending()) |
86 connection_completed_callbacks_.push_back(callback); | 66 connection_completed_callbacks_.push_back(callback); |
87 else | 67 else |
88 callback.Run(); | 68 callback.Run(); |
89 } | 69 } |
90 | 70 |
91 bool ConnectionImpl::AllowsInterface(const std::string& interface_name) const { | |
92 return allow_all_interfaces_ || | |
93 capability_request_.interfaces.count(interface_name); | |
94 } | |
95 | |
96 InterfaceRegistry* ConnectionImpl::GetInterfaceRegistry() { | |
97 return exposed_interfaces_; | |
98 } | |
99 | |
100 InterfaceProvider* ConnectionImpl::GetRemoteInterfaces() { | 71 InterfaceProvider* ConnectionImpl::GetRemoteInterfaces() { |
101 return remote_interfaces_; | 72 return remote_interfaces_; |
102 } | 73 } |
103 | 74 |
104 base::WeakPtr<Connection> ConnectionImpl::GetWeakPtr() { | 75 base::WeakPtr<Connection> ConnectionImpl::GetWeakPtr() { |
105 return weak_factory_.GetWeakPtr(); | 76 return weak_factory_.GetWeakPtr(); |
106 } | 77 } |
107 | 78 |
108 //////////////////////////////////////////////////////////////////////////////// | 79 //////////////////////////////////////////////////////////////////////////////// |
109 // ConnectionImpl, private: | 80 // ConnectionImpl, private: |
110 | 81 |
111 void ConnectionImpl::OnConnectionCompleted(shell::mojom::ConnectResult result, | 82 void ConnectionImpl::OnConnectionCompleted(shell::mojom::ConnectResult result, |
112 mojo::String target_user_id) { | 83 mojo::String target_user_id) { |
113 DCHECK(State::PENDING == state_); | 84 DCHECK(State::PENDING == state_); |
114 | 85 |
115 result_ = result; | 86 result_ = result; |
116 state_ = result_ == shell::mojom::ConnectResult::SUCCEEDED ? | 87 state_ = result_ == shell::mojom::ConnectResult::SUCCEEDED ? |
117 State::CONNECTED : State::DISCONNECTED; | 88 State::CONNECTED : State::DISCONNECTED; |
118 remote_.set_user_id(target_user_id); | 89 remote_.set_user_id(target_user_id); |
119 std::vector<base::Closure> callbacks; | 90 std::vector<base::Closure> callbacks; |
120 callbacks.swap(connection_completed_callbacks_); | 91 callbacks.swap(connection_completed_callbacks_); |
121 for (auto callback : callbacks) | 92 for (auto callback : callbacks) |
122 callback.Run(); | 93 callback.Run(); |
123 } | 94 } |
124 | 95 |
125 } // namespace internal | 96 } // namespace internal |
126 } // namespace shell | 97 } // namespace shell |
OLD | NEW |