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