| 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 "services/shell/public/cpp/shell_connection.h" | |
| 6 | |
| 7 #include "base/bind.h" | |
| 8 #include "base/logging.h" | |
| 9 #include "base/message_loop/message_loop.h" | |
| 10 #include "mojo/public/cpp/bindings/interface_ptr.h" | |
| 11 #include "mojo/public/cpp/bindings/interface_request.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" | |
| 15 #include "services/shell/public/cpp/lib/connector_impl.h" | |
| 16 #include "services/shell/public/cpp/service.h" | |
| 17 | |
| 18 namespace shell { | |
| 19 | |
| 20 //////////////////////////////////////////////////////////////////////////////// | |
| 21 // ShellConnection, public: | |
| 22 | |
| 23 ShellConnection::ShellConnection(shell::Service* client, | |
| 24 mojom::ServiceRequest request) | |
| 25 : client_(client), binding_(this) { | |
| 26 mojom::ConnectorPtr connector; | |
| 27 pending_connector_request_ = GetProxy(&connector); | |
| 28 connector_.reset(new ConnectorImpl(std::move(connector))); | |
| 29 | |
| 30 DCHECK(request.is_pending()); | |
| 31 binding_.Bind(std::move(request)); | |
| 32 } | |
| 33 | |
| 34 ShellConnection::~ShellConnection() {} | |
| 35 | |
| 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) { | |
| 47 connection_lost_closure_ = closure; | |
| 48 if (should_run_connection_lost_closure_ && | |
| 49 !connection_lost_closure_.is_null()) | |
| 50 connection_lost_closure_.Run(); | |
| 51 } | |
| 52 | |
| 53 //////////////////////////////////////////////////////////////////////////////// | |
| 54 // ShellConnection, mojom::Service implementation: | |
| 55 | |
| 56 void ShellConnection::OnStart(mojom::IdentityPtr identity, | |
| 57 uint32_t id, | |
| 58 const OnStartCallback& callback) { | |
| 59 identity_ = identity.To<Identity>(); | |
| 60 if (!initialize_handler_.is_null()) | |
| 61 initialize_handler_.Run(); | |
| 62 | |
| 63 callback.Run(std::move(pending_connector_request_)); | |
| 64 | |
| 65 DCHECK(binding_.is_bound()); | |
| 66 binding_.set_connection_error_handler( | |
| 67 base::Bind(&ShellConnection::OnConnectionError, base::Unretained(this))); | |
| 68 | |
| 69 client_->OnStart(connector_.get(), identity_, id); | |
| 70 } | |
| 71 | |
| 72 void ShellConnection::OnConnect( | |
| 73 mojom::IdentityPtr source, | |
| 74 uint32_t source_id, | |
| 75 mojom::InterfaceProviderRequest local_interfaces, | |
| 76 mojom::InterfaceProviderPtr remote_interfaces, | |
| 77 mojom::CapabilityRequestPtr allowed_capabilities, | |
| 78 const mojo::String& name) { | |
| 79 std::unique_ptr<internal::ConnectionImpl> registry( | |
| 80 new internal::ConnectionImpl(name, source.To<Identity>(), source_id, | |
| 81 allowed_capabilities.To<CapabilityRequest>(), | |
| 82 Connection::State::CONNECTED)); | |
| 83 | |
| 84 InterfaceRegistry* exposed_interfaces = | |
| 85 client_->GetInterfaceRegistryForConnection(); | |
| 86 if (exposed_interfaces) { | |
| 87 exposed_interfaces->Bind(std::move(local_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 | |
| 106 if (!client_->OnConnect(registry.get())) | |
| 107 return; | |
| 108 | |
| 109 // TODO(beng): it appears we never prune this list. We should, when the | |
| 110 // connection's remote service provider pipe breaks. | |
| 111 incoming_connections_.push_back(std::move(registry)); | |
| 112 } | |
| 113 | |
| 114 //////////////////////////////////////////////////////////////////////////////// | |
| 115 // ShellConnection, private: | |
| 116 | |
| 117 void ShellConnection::OnConnectionError() { | |
| 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 | |
| 120 // invalid. | |
| 121 should_run_connection_lost_closure_ = client_->OnStop(); | |
| 122 if (should_run_connection_lost_closure_ && | |
| 123 !connection_lost_closure_.is_null()) | |
| 124 connection_lost_closure_.Run(); | |
| 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. | |
| 127 } | |
| 128 | |
| 129 } // namespace shell | |
| OLD | NEW |