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/shell_connection.h" | 5 #include "services/shell/public/cpp/shell_connection.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/logging.h" | 8 #include "base/logging.h" |
9 #include "base/message_loop/message_loop.h" | 9 #include "base/message_loop/message_loop.h" |
10 #include "mojo/public/cpp/bindings/interface_ptr.h" | 10 #include "mojo/public/cpp/bindings/interface_ptr.h" |
11 #include "mojo/public/cpp/bindings/interface_request.h" | 11 #include "mojo/public/cpp/bindings/interface_request.h" |
12 #include "services/shell/public/cpp/capabilities.h" | 12 #include "services/shell/public/cpp/capabilities.h" |
13 #include "services/shell/public/cpp/connector.h" | 13 #include "services/shell/public/cpp/connector.h" |
14 #include "services/shell/public/cpp/lib/connection_impl.h" | 14 #include "services/shell/public/cpp/lib/connection_impl.h" |
15 #include "services/shell/public/cpp/lib/connector_impl.h" | 15 #include "services/shell/public/cpp/lib/connector_impl.h" |
16 #include "services/shell/public/cpp/shell_client.h" | 16 #include "services/shell/public/cpp/service.h" |
17 | 17 |
18 namespace shell { | 18 namespace shell { |
19 | 19 |
20 //////////////////////////////////////////////////////////////////////////////// | 20 //////////////////////////////////////////////////////////////////////////////// |
21 // ShellConnection, public: | 21 // ShellConnection, public: |
22 | 22 |
23 ShellConnection::ShellConnection(shell::ShellClient* client, | 23 ShellConnection::ShellConnection(shell::Service* client, |
24 mojom::ShellClientRequest request) | 24 mojom::ServiceRequest request) |
25 : client_(client), binding_(this) { | 25 : client_(client), binding_(this) { |
26 mojom::ConnectorPtr connector; | 26 mojom::ConnectorPtr connector; |
27 pending_connector_request_ = GetProxy(&connector); | 27 pending_connector_request_ = GetProxy(&connector); |
28 connector_.reset(new ConnectorImpl(std::move(connector))); | 28 connector_.reset(new ConnectorImpl(std::move(connector))); |
29 | 29 |
30 DCHECK(request.is_pending()); | 30 DCHECK(request.is_pending()); |
31 binding_.Bind(std::move(request)); | 31 binding_.Bind(std::move(request)); |
32 } | 32 } |
33 | 33 |
34 ShellConnection::~ShellConnection() {} | 34 ShellConnection::~ShellConnection() {} |
35 | 35 |
36 void ShellConnection::set_initialize_handler(const base::Closure& callback) { | 36 void ShellConnection::set_initialize_handler(const base::Closure& callback) { |
37 initialize_handler_ = callback; | 37 initialize_handler_ = callback; |
38 } | 38 } |
39 | 39 |
40 void ShellConnection::SetAppTestConnectorForTesting( | 40 void ShellConnection::SetAppTestConnectorForTesting( |
41 mojom::ConnectorPtr connector) { | 41 mojom::ConnectorPtr connector) { |
42 pending_connector_request_ = nullptr; | 42 pending_connector_request_ = nullptr; |
43 connector_.reset(new ConnectorImpl(std::move(connector))); | 43 connector_.reset(new ConnectorImpl(std::move(connector))); |
44 } | 44 } |
45 | 45 |
46 void ShellConnection::SetConnectionLostClosure(const base::Closure& closure) { | 46 void ShellConnection::SetConnectionLostClosure(const base::Closure& closure) { |
47 connection_lost_closure_ = closure; | 47 connection_lost_closure_ = closure; |
48 if (should_run_connection_lost_closure_ && | 48 if (should_run_connection_lost_closure_ && |
49 !connection_lost_closure_.is_null()) | 49 !connection_lost_closure_.is_null()) |
50 connection_lost_closure_.Run(); | 50 connection_lost_closure_.Run(); |
51 } | 51 } |
52 | 52 |
53 //////////////////////////////////////////////////////////////////////////////// | 53 //////////////////////////////////////////////////////////////////////////////// |
54 // ShellConnection, mojom::ShellClient implementation: | 54 // ShellConnection, mojom::Service implementation: |
55 | 55 |
56 void ShellConnection::Initialize(mojom::IdentityPtr identity, | 56 void ShellConnection::OnStart(mojom::IdentityPtr identity, |
57 uint32_t id, | 57 uint32_t id, |
58 const InitializeCallback& callback) { | 58 const OnStartCallback& callback) { |
59 identity_ = identity.To<Identity>(); | 59 identity_ = identity.To<Identity>(); |
60 if (!initialize_handler_.is_null()) | 60 if (!initialize_handler_.is_null()) |
61 initialize_handler_.Run(); | 61 initialize_handler_.Run(); |
62 | 62 |
63 callback.Run(std::move(pending_connector_request_)); | 63 callback.Run(std::move(pending_connector_request_)); |
64 | 64 |
65 DCHECK(binding_.is_bound()); | 65 DCHECK(binding_.is_bound()); |
66 binding_.set_connection_error_handler( | 66 binding_.set_connection_error_handler( |
67 base::Bind(&ShellConnection::OnConnectionError, base::Unretained(this))); | 67 base::Bind(&ShellConnection::OnConnectionError, base::Unretained(this))); |
68 | 68 |
69 client_->Initialize(connector_.get(), identity_, id); | 69 client_->OnStart(connector_.get(), identity_, id); |
70 } | 70 } |
71 | 71 |
72 void ShellConnection::AcceptConnection( | 72 void ShellConnection::OnConnect( |
73 mojom::IdentityPtr source, | 73 mojom::IdentityPtr source, |
74 uint32_t source_id, | 74 uint32_t source_id, |
75 mojom::InterfaceProviderRequest local_interfaces, | 75 mojom::InterfaceProviderRequest local_interfaces, |
76 mojom::InterfaceProviderPtr remote_interfaces, | 76 mojom::InterfaceProviderPtr remote_interfaces, |
77 mojom::CapabilityRequestPtr allowed_capabilities, | 77 mojom::CapabilityRequestPtr allowed_capabilities, |
78 const mojo::String& name) { | 78 const mojo::String& name) { |
79 std::unique_ptr<internal::ConnectionImpl> registry( | 79 std::unique_ptr<internal::ConnectionImpl> registry( |
80 new internal::ConnectionImpl(name, source.To<Identity>(), source_id, | 80 new internal::ConnectionImpl(name, source.To<Identity>(), source_id, |
81 allowed_capabilities.To<CapabilityRequest>(), | 81 allowed_capabilities.To<CapabilityRequest>(), |
82 Connection::State::CONNECTED)); | 82 Connection::State::CONNECTED)); |
(...skipping 13 matching lines...) Expand all Loading... |
96 client_->GetInterfaceProviderForConnection(); | 96 client_->GetInterfaceProviderForConnection(); |
97 if (remote_interface_provider) { | 97 if (remote_interface_provider) { |
98 remote_interface_provider->Bind(std::move(remote_interfaces)); | 98 remote_interface_provider->Bind(std::move(remote_interfaces)); |
99 registry->set_remote_interfaces(remote_interface_provider); | 99 registry->set_remote_interfaces(remote_interface_provider); |
100 } else { | 100 } else { |
101 std::unique_ptr<InterfaceProvider> interfaces(new InterfaceProvider); | 101 std::unique_ptr<InterfaceProvider> interfaces(new InterfaceProvider); |
102 interfaces->Bind(std::move(remote_interfaces)); | 102 interfaces->Bind(std::move(remote_interfaces)); |
103 registry->SetRemoteInterfaces(std::move(interfaces)); | 103 registry->SetRemoteInterfaces(std::move(interfaces)); |
104 } | 104 } |
105 | 105 |
106 if (!client_->AcceptConnection(registry.get())) | 106 if (!client_->OnConnect(registry.get())) |
107 return; | 107 return; |
108 | 108 |
109 // TODO(beng): it appears we never prune this list. We should, when the | 109 // TODO(beng): it appears we never prune this list. We should, when the |
110 // connection's remote service provider pipe breaks. | 110 // connection's remote service provider pipe breaks. |
111 incoming_connections_.push_back(std::move(registry)); | 111 incoming_connections_.push_back(std::move(registry)); |
112 } | 112 } |
113 | 113 |
114 //////////////////////////////////////////////////////////////////////////////// | 114 //////////////////////////////////////////////////////////////////////////////// |
115 // ShellConnection, private: | 115 // ShellConnection, private: |
116 | 116 |
117 void ShellConnection::OnConnectionError() { | 117 void ShellConnection::OnConnectionError() { |
118 // Note that the ShellClient doesn't technically have to quit now, it may live | 118 // Note that the Service doesn't technically have to quit now, it may live |
119 // on to service existing connections. All existing Connectors however are | 119 // on to service existing connections. All existing Connectors however are |
120 // invalid. | 120 // invalid. |
121 should_run_connection_lost_closure_ = client_->ShellConnectionLost(); | 121 should_run_connection_lost_closure_ = client_->OnStop(); |
122 if (should_run_connection_lost_closure_ && | 122 if (should_run_connection_lost_closure_ && |
123 !connection_lost_closure_.is_null()) | 123 !connection_lost_closure_.is_null()) |
124 connection_lost_closure_.Run(); | 124 connection_lost_closure_.Run(); |
125 // We don't reset the connector as clients may have taken a raw pointer to it. | 125 // We don't reset the connector as clients may have taken a raw pointer to it. |
126 // Connect() will return nullptr if they try to connect to anything. | 126 // Connect() will return nullptr if they try to connect to anything. |
127 } | 127 } |
128 | 128 |
129 } // namespace shell | 129 } // namespace shell |
OLD | NEW |