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 "base/bind.h" | |
6 #include "base/logging.h" | |
7 #include "base/message_loop/message_loop.h" | |
8 #include "mojo/public/cpp/bindings/interface_ptr.h" | |
9 #include "mojo/public/cpp/bindings/interface_request.h" | |
10 #include "mojo/shell/public/cpp/capabilities.h" | |
11 #include "mojo/shell/public/cpp/connector.h" | |
12 #include "mojo/shell/public/cpp/lib/connection_impl.h" | |
13 #include "mojo/shell/public/cpp/lib/connector_impl.h" | |
14 #include "mojo/shell/public/cpp/shell_client.h" | |
15 #include "mojo/shell/public/cpp/shell_connection.h" | |
16 | |
17 namespace mojo { | |
18 | |
19 //////////////////////////////////////////////////////////////////////////////// | |
20 // ShellConnection, public: | |
21 | |
22 ShellConnection::ShellConnection(mojo::ShellClient* client, | |
23 shell::mojom::ShellClientRequest request) | |
24 : client_(client), binding_(this) { | |
25 shell::mojom::ConnectorPtr connector; | |
26 pending_connector_request_ = GetProxy(&connector); | |
27 connector_.reset(new ConnectorImpl(std::move(connector))); | |
28 | |
29 DCHECK(request.is_pending()); | |
30 binding_.Bind(std::move(request)); | |
31 } | |
32 | |
33 ShellConnection::~ShellConnection() {} | |
34 | |
35 void ShellConnection::set_initialize_handler(const base::Closure& callback) { | |
36 initialize_handler_ = callback; | |
37 } | |
38 | |
39 void ShellConnection::SetAppTestConnectorForTesting( | |
40 shell::mojom::ConnectorPtr connector) { | |
41 pending_connector_request_ = nullptr; | |
42 connector_.reset(new ConnectorImpl(std::move(connector))); | |
43 } | |
44 | |
45 //////////////////////////////////////////////////////////////////////////////// | |
46 // ShellConnection, shell::mojom::ShellClient implementation: | |
47 | |
48 void ShellConnection::Initialize(shell::mojom::IdentityPtr identity, | |
49 uint32_t id, | |
50 const InitializeCallback& callback) { | |
51 if (!initialize_handler_.is_null()) | |
52 initialize_handler_.Run(); | |
53 | |
54 callback.Run(std::move(pending_connector_request_)); | |
55 | |
56 DCHECK(binding_.is_bound()); | |
57 binding_.set_connection_error_handler([this] { OnConnectionError(); }); | |
58 | |
59 client_->Initialize(connector_.get(), identity.To<Identity>(), id); | |
60 } | |
61 | |
62 void ShellConnection::AcceptConnection( | |
63 shell::mojom::IdentityPtr source, | |
64 uint32_t source_id, | |
65 shell::mojom::InterfaceProviderRequest local_interfaces, | |
66 shell::mojom::InterfaceProviderPtr remote_interfaces, | |
67 shell::mojom::CapabilityRequestPtr allowed_capabilities, | |
68 const String& name) { | |
69 scoped_ptr<Connection> registry(new internal::ConnectionImpl( | |
70 name, source.To<Identity>(), source_id, std::move(remote_interfaces), | |
71 std::move(local_interfaces), | |
72 allowed_capabilities.To<CapabilityRequest>(), | |
73 Connection::State::CONNECTED)); | |
74 if (!client_->AcceptConnection(registry.get())) | |
75 return; | |
76 | |
77 // TODO(beng): it appears we never prune this list. We should, when the | |
78 // connection's remote service provider pipe breaks. | |
79 incoming_connections_.push_back(std::move(registry)); | |
80 } | |
81 | |
82 //////////////////////////////////////////////////////////////////////////////// | |
83 // ShellConnection, private: | |
84 | |
85 void ShellConnection::OnConnectionError() { | |
86 // Note that the ShellClient doesn't technically have to quit now, it may live | |
87 // on to service existing connections. All existing Connectors however are | |
88 // invalid. | |
89 if (client_->ShellConnectionLost() && !connection_lost_closure_.is_null()) | |
90 connection_lost_closure_.Run(); | |
91 // We don't reset the connector as clients may have taken a raw pointer to it. | |
92 // Connect() will return nullptr if they try to connect to anything. | |
93 } | |
94 | |
95 } // namespace mojo | |
OLD | NEW |