| 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/interfaces/application/service_provider.mojom", | 6 "mojo/public/interfaces/application/service_provider.mojom", |
| 7 "mojo/public/js/connection", | 7 "mojo/public/js/connection", |
| 8 "mojo/public/js/core", | 8 "mojo/public/js/core", |
| 9 ], function(spInterfaceModule, connectionModule, coreModule) { | 9 ], function(spInterfaceModule, connectionModule, coreModule) { |
| 10 | 10 |
| 11 // Implementation of the Mojo ServiceProvider interface. |
| 11 function connectToServiceImpl(serviceName, serviceHandle) { | 12 function connectToServiceImpl(serviceName, serviceHandle) { |
| 12 var provider = this.providers_.get(serviceName); | 13 var provider = this.providers_.get(serviceName); |
| 13 if (!provider) { | 14 if (!provider) { |
| 14 this.pendingRequests_.set(serviceName, serviceHandle); | 15 this.pendingRequests_.set(serviceName, serviceHandle); |
| 15 return; | 16 return; |
| 16 } | 17 } |
| 17 | 18 |
| 18 var serviceConnection = new connectionModule.Connection( | 19 var serviceConnection = new connectionModule.Connection( |
| 19 serviceHandle, | 20 serviceHandle, |
| 20 provider.service.stubClass, | 21 provider.service.stubClass, |
| 21 provider.service.client && provider.service.client.proxyClass); | 22 provider.service.client && provider.service.client.proxyClass); |
| 22 | 23 |
| 23 serviceConnection.local.connection$ = serviceConnection; | |
| 24 serviceConnection.local.delegate$ = | 24 serviceConnection.local.delegate$ = |
| 25 new provider.factory(serviceConnection.remote); | 25 new provider.factory(serviceConnection.remote); |
| 26 | 26 |
| 27 provider.connections.push(serviceConnection); | 27 provider.connections.push(serviceConnection); |
| 28 } | 28 } |
| 29 | 29 |
| 30 function checkServiceProvider(sp) { |
| 31 if (!sp.connections_) |
| 32 throw new Error("Service was closed"); |
| 33 } |
| 34 |
| 30 class ServiceProvider { | 35 class ServiceProvider { |
| 31 constructor(service) { | 36 constructor(service) { |
| 37 if (!(service instanceof spInterfaceModule.ServiceProvider.proxyClass)) |
| 38 throw new Error("service must be a ServiceProvider proxy"); |
| 39 |
| 40 service.client$ = { |
| 41 connectToService: connectToServiceImpl.bind(this) |
| 42 }; |
| 43 |
| 32 this.connections_ = new Map(); | 44 this.connections_ = new Map(); |
| 33 this.providers_ = new Map(); | 45 this.providers_ = new Map(); |
| 34 this.pendingRequests_ = new Map(); | 46 this.pendingRequests_ = new Map(); |
| 35 this.connection_ = null; | 47 this.connection_ = null; |
| 36 | 48 this.connection_ = service.getConnection$(); |
| 37 if (service instanceof spInterfaceModule.ServiceProvider.proxyClass) { | |
| 38 this.connection_ = service.getConnection$(); | |
| 39 } else { | |
| 40 this.handle_ = service; // service is-a MessagePipe handle | |
| 41 this.connection_ = new connectionModule.Connection( | |
| 42 this.handle_, | |
| 43 spInterfaceModule.ServiceProvider.client.stubClass, | |
| 44 spInterfaceModule.ServiceProvider.proxyClass); | |
| 45 }; | |
| 46 this.connection_.local.delegate$ = { | |
| 47 connectToService: connectToServiceImpl.bind(this) | |
| 48 } | |
| 49 } | 49 } |
| 50 | 50 |
| 51 provideService(service, factory) { | 51 provideService(service, factory) { |
| 52 // TODO(hansmuller): if !factory, remove provider and close its | 52 // TODO(hansmuller): if !factory, remove provider and close its |
| 53 // connections. | 53 // connections. |
| 54 // TODO(hansmuller): if this.connection_ is null, throw an error. | 54 checkServiceProvider(this); |
| 55 |
| 55 var provider = { | 56 var provider = { |
| 56 service: service, | 57 service: service, // A JS bindings interface object. |
| 57 factory: factory, | 58 factory: factory, // factory(clientProxy) => interface implemntation. |
| 58 connections: [], | 59 connections: [], |
| 59 }; | 60 }; |
| 60 this.providers_.set(service.name, provider); | 61 this.providers_.set(service.name, provider); |
| 61 | 62 |
| 62 if (this.pendingRequests_.has(service.name)) { | 63 if (this.pendingRequests_.has(service.name)) { |
| 63 connectToServiceImpl(service.name, pendingRequests_.get(service.name)); | 64 connectToServiceImpl(service.name, pendingRequests_.get(service.name)); |
| 64 pendingRequests_.delete(service.name); | 65 pendingRequests_.delete(service.name); |
| 65 } | 66 } |
| 66 | 67 |
| 67 return this; | 68 return this; |
| 68 } | 69 } |
| 69 | 70 |
| 70 connectToService(service, client) { | 71 connectToService(service, client) { |
| 71 // TODO(hansmuler): if service.name isn't defined, throw an error. | 72 checkServiceProvider(this); |
| 72 // TODO(hansmuller): if this.connection_ is null, throw an error. | 73 if (!service.name) |
| 74 throw new Error("Invalid service parameter"); |
| 75 |
| 73 var serviceConnection = this.connections_.get(service.name); | 76 var serviceConnection = this.connections_.get(service.name); |
| 74 if (serviceConnection) | 77 if (serviceConnection) |
| 75 return serviceConnection.remote; | 78 return serviceConnection.remote; |
| 76 | 79 |
| 77 var pipe = coreModule.createMessagePipe(); | 80 var pipe = coreModule.createMessagePipe(); |
| 78 this.connection_.remote.connectToService(service.name, pipe.handle1); | 81 this.connection_.remote.connectToService(service.name, pipe.handle1); |
| 79 var clientClass = client && service.client.stubClass; | 82 var clientClass = client && service.client.stubClass; |
| 80 var serviceConnection = new connectionModule.Connection( | 83 var serviceConnection = new connectionModule.Connection( |
| 81 pipe.handle0, clientClass, service.proxyClass); | 84 pipe.handle0, clientClass, service.proxyClass); |
| 82 if (serviceConnection.local) | 85 if (serviceConnection.local) |
| (...skipping 26 matching lines...) Expand all Loading... |
| 109 this.connection_ = null; | 112 this.connection_ = null; |
| 110 this.handle_ = null; | 113 this.handle_ = null; |
| 111 } | 114 } |
| 112 } | 115 } |
| 113 } | 116 } |
| 114 | 117 |
| 115 var exports = {}; | 118 var exports = {}; |
| 116 exports.ServiceProvider = ServiceProvider; | 119 exports.ServiceProvider = ServiceProvider; |
| 117 return exports; | 120 return exports; |
| 118 }); | 121 }); |
| OLD | NEW |