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 "mojo/shell/public/cpp/lib/connector_impl.h" | |
6 | |
7 #include "mojo/shell/public/cpp/identity.h" | |
8 #include "mojo/shell/public/cpp/lib/connection_impl.h" | |
9 | |
10 namespace mojo { | |
11 | |
12 Connector::ConnectParams::ConnectParams(const Identity& target) | |
13 : target_(target) {} | |
14 Connector::ConnectParams::ConnectParams(const std::string& name) | |
15 : target_(name, shell::mojom::kInheritUserID) {} | |
16 Connector::ConnectParams::~ConnectParams() {} | |
17 | |
18 ConnectorImpl::ConnectorImpl(shell::mojom::ConnectorPtrInfo unbound_state) | |
19 : unbound_state_(std::move(unbound_state)) {} | |
20 ConnectorImpl::ConnectorImpl(shell::mojom::ConnectorPtr connector) | |
21 : connector_(std::move(connector)) { | |
22 thread_checker_.reset(new base::ThreadChecker); | |
23 } | |
24 ConnectorImpl::~ConnectorImpl() {} | |
25 | |
26 scoped_ptr<Connection> ConnectorImpl::Connect(const std::string& name) { | |
27 ConnectParams params(name); | |
28 return Connect(¶ms); | |
29 } | |
30 | |
31 scoped_ptr<Connection> ConnectorImpl::Connect(ConnectParams* params) { | |
32 // Bind this object to the current thread the first time it is used to | |
33 // connect. | |
34 if (!connector_.is_bound()) { | |
35 if (!unbound_state_.is_valid()) { | |
36 // It's possible to get here when the link to the shell has been severed | |
37 // (and so the connector pipe has been closed) but the app has chosen not | |
38 // to quit. | |
39 return nullptr; | |
40 } | |
41 connector_.Bind(std::move(unbound_state_)); | |
42 thread_checker_.reset(new base::ThreadChecker); | |
43 } | |
44 DCHECK(thread_checker_->CalledOnValidThread()); | |
45 | |
46 DCHECK(params); | |
47 // We allow all interfaces on outgoing connections since we are presumably in | |
48 // a position to know who we're talking to. | |
49 CapabilityRequest request; | |
50 request.interfaces.insert("*"); | |
51 shell::mojom::InterfaceProviderPtr local_interfaces; | |
52 shell::mojom::InterfaceProviderRequest local_request = | |
53 GetProxy(&local_interfaces); | |
54 shell::mojom::InterfaceProviderPtr remote_interfaces; | |
55 shell::mojom::InterfaceProviderRequest remote_request = | |
56 GetProxy(&remote_interfaces); | |
57 scoped_ptr<internal::ConnectionImpl> registry(new internal::ConnectionImpl( | |
58 params->target().name(), params->target(), | |
59 shell::mojom::kInvalidInstanceID, std::move(remote_interfaces), | |
60 std::move(local_request), request, Connection::State::PENDING)); | |
61 | |
62 shell::mojom::ShellClientPtr shell_client; | |
63 shell::mojom::PIDReceiverRequest pid_receiver_request; | |
64 params->TakeClientProcessConnection(&shell_client, &pid_receiver_request); | |
65 shell::mojom::ClientProcessConnectionPtr client_process_connection; | |
66 if (shell_client.is_bound() && pid_receiver_request.is_pending()) { | |
67 client_process_connection = shell::mojom::ClientProcessConnection::New(); | |
68 client_process_connection->shell_client = | |
69 shell_client.PassInterface().PassHandle(); | |
70 client_process_connection->pid_receiver_request = | |
71 pid_receiver_request.PassMessagePipe(); | |
72 } else if (shell_client.is_bound() || pid_receiver_request.is_pending()) { | |
73 NOTREACHED() << "If one of shell_client or pid_receiver_request is valid, " | |
74 << "both must be valid."; | |
75 return std::move(registry); | |
76 } | |
77 connector_->Connect( | |
78 shell::mojom::Identity::From(params->target()), | |
79 std::move(remote_request), std::move(local_interfaces), | |
80 std::move(client_process_connection), registry->GetConnectCallback()); | |
81 return std::move(registry); | |
82 } | |
83 | |
84 scoped_ptr<Connector> ConnectorImpl::Clone() { | |
85 shell::mojom::ConnectorPtr connector; | |
86 connector_->Clone(GetProxy(&connector)); | |
87 return make_scoped_ptr( | |
88 new ConnectorImpl(connector.PassInterface())); | |
89 } | |
90 | |
91 } // namespace mojo | |
OLD | NEW |