| 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/service_context.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/interface_registry.h" | |
| 14 #include "services/shell/public/cpp/lib/connector_impl.h" | |
| 15 #include "services/shell/public/cpp/service.h" | |
| 16 | |
| 17 namespace shell { | |
| 18 | |
| 19 //////////////////////////////////////////////////////////////////////////////// | |
| 20 // ServiceContext, public: | |
| 21 | |
| 22 ServiceContext::ServiceContext(shell::Service* service, | |
| 23 mojom::ServiceRequest request, | |
| 24 std::unique_ptr<Connector> connector, | |
| 25 mojom::ConnectorRequest connector_request) | |
| 26 : pending_connector_request_(std::move(connector_request)), | |
| 27 service_(service), | |
| 28 binding_(this, std::move(request)), | |
| 29 connector_(std::move(connector)) { | |
| 30 DCHECK(binding_.is_bound()); | |
| 31 binding_.set_connection_error_handler( | |
| 32 base::Bind(&ServiceContext::OnConnectionError, base::Unretained(this))); | |
| 33 if (!connector_) { | |
| 34 connector_ = Connector::Create(&pending_connector_request_); | |
| 35 } else { | |
| 36 DCHECK(pending_connector_request_.is_pending()); | |
| 37 } | |
| 38 } | |
| 39 | |
| 40 ServiceContext::~ServiceContext() {} | |
| 41 | |
| 42 void ServiceContext::SetConnectionLostClosure(const base::Closure& closure) { | |
| 43 connection_lost_closure_ = closure; | |
| 44 if (should_run_connection_lost_closure_ && | |
| 45 !connection_lost_closure_.is_null()) | |
| 46 connection_lost_closure_.Run(); | |
| 47 } | |
| 48 | |
| 49 //////////////////////////////////////////////////////////////////////////////// | |
| 50 // ServiceContext, mojom::Service implementation: | |
| 51 | |
| 52 void ServiceContext::OnStart(const shell::Identity& identity, | |
| 53 const OnStartCallback& callback) { | |
| 54 identity_ = identity; | |
| 55 if (!initialize_handler_.is_null()) | |
| 56 initialize_handler_.Run(); | |
| 57 | |
| 58 callback.Run(std::move(pending_connector_request_)); | |
| 59 | |
| 60 service_->OnStart(identity_); | |
| 61 } | |
| 62 | |
| 63 void ServiceContext::OnConnect( | |
| 64 const Identity& source, | |
| 65 mojom::InterfaceProviderRequest interfaces, | |
| 66 const Interfaces& allowed_interfaces, | |
| 67 const Classes& allowed_classes) { | |
| 68 // TODO(beng): do something with |allowed_classes|. | |
| 69 std::unique_ptr<InterfaceRegistry> registry( | |
| 70 new InterfaceRegistry(identity_, source, allowed_interfaces)); | |
| 71 registry->Bind(std::move(interfaces)); | |
| 72 | |
| 73 if (!service_->OnConnect(source, registry.get())) | |
| 74 return; | |
| 75 | |
| 76 // TODO(beng): it appears we never prune this list. We should, when the | |
| 77 // registry's remote interface provider pipe breaks. | |
| 78 incoming_connections_.push_back(std::move(registry)); | |
| 79 } | |
| 80 | |
| 81 //////////////////////////////////////////////////////////////////////////////// | |
| 82 // ServiceContext, private: | |
| 83 | |
| 84 void ServiceContext::OnConnectionError() { | |
| 85 // Note that the Service doesn't technically have to quit now, it may live | |
| 86 // on to service existing connections. All existing Connectors however are | |
| 87 // invalid. | |
| 88 should_run_connection_lost_closure_ = service_->OnStop(); | |
| 89 if (should_run_connection_lost_closure_ && | |
| 90 !connection_lost_closure_.is_null()) | |
| 91 connection_lost_closure_.Run(); | |
| 92 // We don't reset the connector as clients may have taken a raw pointer to it. | |
| 93 // Connect() will return nullptr if they try to connect to anything. | |
| 94 } | |
| 95 | |
| 96 } // namespace shell | |
| OLD | NEW |