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