| // Copyright 2014 The Chromium Authors. All rights reserved. |
| // Use of this source code is governed by a BSD-style license that can be |
| // found in the LICENSE file. |
| |
| define("mojo/services/public/js/service_provider", [ |
| "mojo/public/interfaces/application/service_provider.mojom", |
| "mojo/public/js/connection", |
| ], function(serviceProviderMojom, connection) { |
| |
| const ServiceProviderInterface = serviceProviderMojom.ServiceProvider; |
| |
| function checkServiceProvider(sp) { |
| if (!sp.providers_) |
| throw new Error("Service was closed"); |
| } |
| |
| class ServiceProvider { |
| constructor(service) { |
| if (!(service instanceof ServiceProviderInterface.proxyClass)) |
| throw new Error("service must be a ServiceProvider proxy"); |
| service.local$ = this; // Implicitly sets this.remote$ to service. |
| this.providers_ = new Map(); // serviceName => see provideService() below |
| this.pendingRequests_ = new Map(); // serviceName => serviceHandle |
| } |
| |
| // Incoming requests |
| connectToService(serviceName, serviceHandle) { |
| if (!this.providers_) // We're closed. |
| return; |
| |
| var provider = this.providers_.get(serviceName); |
| if (!provider) { |
| this.pendingRequests_.set(serviceName, serviceHandle); |
| return; |
| } |
| var proxy = connection.bindProxyHandle( |
| serviceHandle, provider.service, provider.service.client); |
| proxy.local$ = new provider.factory(proxy); |
| provider.connections.push(proxy.connection$); |
| } |
| |
| provideService(service, factory) { |
| checkServiceProvider(this); |
| |
| var provider = { |
| service: service, // A JS bindings interface object. |
| factory: factory, // factory(clientProxy) => interface implemntation |
| connections: [], |
| }; |
| this.providers_.set(service.name, provider); |
| |
| if (this.pendingRequests_.has(service.name)) { |
| this.connectToService(service.name, pendingRequests_.get(service.name)); |
| pendingRequests_.delete(service.name); |
| } |
| return this; |
| } |
| |
| // Outgoing requests |
| requestService(interfaceObject, clientImpl) { |
| checkServiceProvider(this); |
| if (!interfaceObject.name) |
| throw new Error("Invalid service parameter"); |
| if (!clientImpl && interfaceObject.client) |
| throw new Error("Client implementation must be provided"); |
| |
| if (!clientImpl) |
| clientImpl = {}; |
| var messagePipeHandle = connection.bindProxyClient( |
| clientImpl, interfaceObject.client, interfaceObject); |
| this.remote$.connectToService(interfaceObject.name, messagePipeHandle); |
| return clientImpl.remote$; |
| }; |
| |
| close() { |
| this.providers_ = null; |
| this.pendingRequests_ = null; |
| } |
| } |
| |
| var exports = {}; |
| exports.ServiceProvider = ServiceProvider; |
| return exports; |
| }); |