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_exchange", [ | 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 checkServiceExchange(exch) { | 15 function checkServiceProvider(sp) { |
16 if (!exch.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 ServiceExchange { | 20 class ServiceProvider { |
21 constructor(servicesRequest, exposedServicesProxy) { | 21 constructor(servicesRequest, exposedServicesProxy) { |
22 this.proxy = exposedServicesProxy; | 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) | 25 if (servicesRequest) |
26 StubBindings(servicesRequest).delegate = this; | 26 StubBindings(servicesRequest).delegate = this; |
27 } | 27 } |
28 | 28 |
29 // Incoming requests | 29 // Incoming requests |
30 connectToService(serviceName, serviceHandle) { | 30 connectToService(serviceName, serviceHandle) { |
31 if (!this.providers_) // We're closed. | 31 if (!this.providers_) // We're closed. |
32 return; | 32 return; |
33 | 33 |
34 var provider = this.providers_.get(serviceName); | 34 var provider = this.providers_.get(serviceName); |
35 if (!provider) { | 35 if (!provider) { |
36 this.pendingRequests_.set(serviceName, serviceHandle); | 36 this.pendingRequests_.set(serviceName, serviceHandle); |
37 return; | 37 return; |
38 } | 38 } |
39 | 39 |
40 var stub = connection.bindHandleToStub(serviceHandle, provider.service); | 40 var stub = connection.bindHandleToStub(serviceHandle, provider.service); |
41 StubBindings(stub).delegate = new provider.factory(); | 41 StubBindings(stub).delegate = new provider.factory(); |
42 provider.connections.push(StubBindings(stub).connection); | 42 provider.connections.push(StubBindings(stub).connection); |
43 } | 43 } |
44 | 44 |
45 provideService(service, factory) { | 45 provideService(service, factory) { |
46 checkServiceExchange(this); | 46 checkServiceProvider(this); |
47 | 47 |
48 var provider = { | 48 var provider = { |
49 service: service, // A JS bindings interface object. | 49 service: service, // A JS bindings interface object. |
50 factory: factory, // factory(clientProxy) => interface implemntation | 50 factory: factory, // factory(clientProxy) => interface implemntation |
51 connections: [], | 51 connections: [], |
52 }; | 52 }; |
53 this.providers_.set(service.name, provider); | 53 this.providers_.set(service.name, provider); |
54 | 54 |
55 if (this.pendingRequests_.has(service.name)) { | 55 if (this.pendingRequests_.has(service.name)) { |
56 this.connectToService(service.name, pendingRequests_.get(service.name)); | 56 this.connectToService(service.name, pendingRequests_.get(service.name)); |
57 pendingRequests_.delete(service.name); | 57 pendingRequests_.delete(service.name); |
58 } | 58 } |
59 return this; | 59 return this; |
60 } | 60 } |
61 | 61 |
62 // Outgoing requests | 62 // Outgoing requests |
63 requestService(interfaceObject, clientImpl) { | 63 requestService(interfaceObject, clientImpl) { |
64 checkServiceExchange(this); | 64 checkServiceProvider(this); |
65 if (!interfaceObject.name) | 65 if (!interfaceObject.name) |
66 throw new Error("Invalid service parameter"); | 66 throw new Error("Invalid service parameter"); |
67 if (!clientImpl && interfaceObject.client) | 67 if (!clientImpl && interfaceObject.client) |
68 throw new Error("Client implementation must be provided"); | 68 throw new Error("Client implementation must be provided"); |
69 | 69 |
70 var serviceProxy; | 70 var serviceProxy; |
71 var serviceHandle = connection.bindProxy( | 71 var serviceHandle = connection.bindProxy( |
72 function(sp) {serviceProxy = sp;}, interfaceObject); | 72 function(sp) {serviceProxy = sp;}, interfaceObject); |
73 this.proxy.connectToService(interfaceObject.name, serviceHandle); | 73 this.proxy.connectToService(interfaceObject.name, serviceHandle); |
74 return serviceProxy; | 74 return serviceProxy; |
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.ServiceExchange = ServiceExchange; | 84 exports.ServiceProvider = ServiceProvider; |
85 return exports; | 85 return exports; |
86 }); | 86 }); |
OLD | NEW |