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 define("mojo/services/public/js/service_provider", [ | 5 define("mojo/services/public/js/service_provider", [ |
6 "mojo/public/js/bindings", | 6 "mojo/public/js/bindings", |
7 "mojo/public/interfaces/application/service_provider.mojom", | 7 "mojo/public/interfaces/application/service_provider.mojom", |
8 "mojo/public/js/connection", | 8 "mojo/public/js/connection", |
9 ], function(bindings, spMojom, connection) { | 9 ], function(bindings, spMojom, connection) { |
10 | 10 |
11 const ProxyBindings = bindings.ProxyBindings; | 11 const ProxyBindings = bindings.ProxyBindings; |
12 const StubBindings = bindings.StubBindings; | 12 const StubBindings = bindings.StubBindings; |
13 const ServiceProviderInterface = spMojom.ServiceProvider; | 13 const ServiceProviderInterface = spMojom.ServiceProvider; |
14 | 14 |
15 function checkServiceProvider(sp) { | 15 function checkServiceProvider(sp) { |
16 if (!sp.providers_) | 16 if (!sp.providers_) |
17 throw new Error("Service was closed"); | 17 throw new Error("Service was closed"); |
18 } | 18 } |
19 | 19 |
20 class ServiceProvider { | 20 class ServiceProvider { |
21 constructor(service) { | 21 constructor(servicesRequest, exposedServicesProxy) { |
22 this.proxy = service; | 22 this.proxy = exposedServicesProxy; |
23 this.providers_ = new Map(); // serviceName => see provideService() below | 23 this.providers_ = new Map(); // serviceName => see provideService() below |
24 this.pendingRequests_ = new Map(); // serviceName => serviceHandle | 24 this.pendingRequests_ = new Map(); // serviceName => serviceHandle |
| 25 if (servicesRequest) |
| 26 StubBindings(servicesRequest).delegate = this; |
25 } | 27 } |
26 | 28 |
27 // Incoming requests | 29 // Incoming requests |
28 connectToService(serviceName, serviceHandle) { | 30 connectToService(serviceName, serviceHandle) { |
29 if (!this.providers_) // We're closed. | 31 if (!this.providers_) // We're closed. |
30 return; | 32 return; |
31 | 33 |
32 var provider = this.providers_.get(serviceName); | 34 var provider = this.providers_.get(serviceName); |
33 if (!provider) { | 35 if (!provider) { |
34 this.pendingRequests_.set(serviceName, serviceHandle); | 36 this.pendingRequests_.set(serviceName, serviceHandle); |
35 return; | 37 return; |
36 } | 38 } |
37 var proxy = connection.bindProxyHandle( | 39 |
38 serviceHandle, provider.service, provider.service.client); | 40 var stub = connection.bindHandleToStub(serviceHandle, provider.service); |
39 if (ProxyBindings(proxy).local) | 41 StubBindings(stub).delegate = new provider.factory(); |
40 ProxyBindings(proxy).setLocalDelegate(new provider.factory(proxy)); | 42 provider.connections.push(StubBindings(stub).connection); |
41 provider.connections.push(ProxyBindings(proxy).connection); | |
42 } | 43 } |
43 | 44 |
44 provideService(service, factory) { | 45 provideService(service, factory) { |
45 checkServiceProvider(this); | 46 checkServiceProvider(this); |
46 | 47 |
47 var provider = { | 48 var provider = { |
48 service: service, // A JS bindings interface object. | 49 service: service, // A JS bindings interface object. |
49 factory: factory, // factory(clientProxy) => interface implemntation | 50 factory: factory, // factory(clientProxy) => interface implemntation |
50 connections: [], | 51 connections: [], |
51 }; | 52 }; |
52 this.providers_.set(service.name, provider); | 53 this.providers_.set(service.name, provider); |
53 | 54 |
54 if (this.pendingRequests_.has(service.name)) { | 55 if (this.pendingRequests_.has(service.name)) { |
55 this.connectToService(service.name, pendingRequests_.get(service.name)); | 56 this.connectToService(service.name, pendingRequests_.get(service.name)); |
56 pendingRequests_.delete(service.name); | 57 pendingRequests_.delete(service.name); |
57 } | 58 } |
58 return this; | 59 return this; |
59 } | 60 } |
60 | 61 |
61 // Outgoing requests | 62 // Outgoing requests |
62 requestService(interfaceObject, clientImpl) { | 63 requestService(interfaceObject, clientImpl) { |
63 checkServiceProvider(this); | 64 checkServiceProvider(this); |
64 if (!interfaceObject.name) | 65 if (!interfaceObject.name) |
65 throw new Error("Invalid service parameter"); | 66 throw new Error("Invalid service parameter"); |
66 if (!clientImpl && interfaceObject.client) | 67 if (!clientImpl && interfaceObject.client) |
67 throw new Error("Client implementation must be provided"); | 68 throw new Error("Client implementation must be provided"); |
68 | 69 |
69 var remoteProxy; | 70 var serviceProxy; |
70 var clientFactory = function(x) {remoteProxy = x; return clientImpl;}; | 71 var serviceHandle = connection.bindProxy( |
71 var messagePipeHandle = connection.bindProxyClient( | 72 function(sp) {serviceProxy = sp;}, interfaceObject); |
72 clientFactory, interfaceObject.client, interfaceObject); | 73 this.proxy.connectToService(interfaceObject.name, serviceHandle); |
73 this.proxy.connectToService(interfaceObject.name, messagePipeHandle); | 74 return serviceProxy; |
74 return remoteProxy; | |
75 }; | 75 }; |
76 | 76 |
77 close() { | 77 close() { |
78 this.providers_ = null; | 78 this.providers_ = null; |
79 this.pendingRequests_ = null; | 79 this.pendingRequests_ = null; |
80 } | 80 } |
81 } | 81 } |
82 | 82 |
83 var exports = {}; | 83 var exports = {}; |
84 exports.ServiceProvider = ServiceProvider; | 84 exports.ServiceProvider = ServiceProvider; |
85 return exports; | 85 return exports; |
86 }); | 86 }); |
OLD | NEW |