Add per-user preferences support.
[chromium-blink-merge.git] / third_party / mojo_services / src / public / js / service_exchange.js
blobf23c0ef018e335716f9a9e922efd3c6773d718fa
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.
5 define("mojo/services/public/js/service_exchange", [
6   "mojo/public/js/bindings",
7   "mojo/public/interfaces/application/service_provider.mojom",
8   "mojo/public/js/connection",
9 ], function(bindings, spMojom, connection) {
11   const ProxyBindings = bindings.ProxyBindings;
12   const StubBindings = bindings.StubBindings;
13   const ServiceProviderInterface = spMojom.ServiceProvider;
15   function checkServiceExchange(exch) {
16     if (!exch.providers_)
17       throw new Error("Service was closed");
18   }
20   class ServiceExchange {
21     constructor(servicesRequest, exposedServicesProxy) {
22       this.proxy = exposedServicesProxy;
23       this.providers_ = new Map(); // serviceName => see provideService() below
24       this.pendingRequests_ = new Map(); // serviceName => serviceHandle
25       if (servicesRequest)
26         StubBindings(servicesRequest).delegate = this;
27     }
29     // Incoming requests
30     connectToService(serviceName, serviceHandle) {
31       if (!this.providers_) // We're closed.
32         return;
34       var provider = this.providers_.get(serviceName);
35       if (!provider) {
36         this.pendingRequests_.set(serviceName, serviceHandle);
37         return;
38       }
40       var stub = connection.bindHandleToStub(serviceHandle, provider.service);
41       StubBindings(stub).delegate = new provider.factory();
42       provider.connections.push(StubBindings(stub).connection);
43     }
45     provideService(service, factory) {
46       checkServiceExchange(this);
48       var provider = {
49         service: service, // A JS bindings interface object.
50         factory: factory, // factory() => interface implemntation
51         connections: [],
52       };
53       this.providers_.set(service.name, provider);
55       if (this.pendingRequests_.has(service.name)) {
56         this.connectToService(service.name, pendingRequests_.get(service.name));
57         pendingRequests_.delete(service.name);
58       }
59       return this;
60     }
62     // Outgoing requests
63     requestService(interfaceObject) {
64       checkServiceExchange(this);
65       if (!interfaceObject.name)
66         throw new Error("Invalid service parameter");
68       var serviceProxy;
69       var serviceHandle = connection.bindProxy(
70           function(sp) {serviceProxy = sp;}, interfaceObject);
71       this.proxy.connectToService(interfaceObject.name, serviceHandle);
72       return serviceProxy;
73     };
75     close() {
76       this.providers_ = null;
77       this.pendingRequests_ = null;
78     }
79   }
81   var exports = {};
82   exports.ServiceExchange = ServiceExchange;
83   return exports;
84 });