OLD | NEW |
(Empty) | |
| 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. |
| 4 |
| 5 define("mojo/services/public/js/service_provider", [ |
| 6 "mojo/public/interfaces/application/service_provider.mojom", |
| 7 "mojo/public/js/connection", |
| 8 "mojo/public/js/core", |
| 9 ], function(spInterfaceModule, connectionModule, coreModule) { |
| 10 |
| 11 function connectToServiceImpl(serviceName, serviceHandle) { |
| 12 var provider = this.providers_.get(serviceName); |
| 13 if (!provider) { |
| 14 this.pendingRequests_.set(serviceName, serviceHandle); |
| 15 return; |
| 16 } |
| 17 |
| 18 var serviceConnection = new connectionModule.Connection( |
| 19 serviceHandle, |
| 20 provider.service.stubClass, |
| 21 provider.service.client && provider.service.client.proxyClass); |
| 22 |
| 23 serviceConnection.local.connection$ = serviceConnection; |
| 24 serviceConnection.local.delegate$ = |
| 25 new provider.factory(serviceConnection.remote); |
| 26 |
| 27 provider.connections.push(serviceConnection); |
| 28 } |
| 29 |
| 30 class ServiceProvider { |
| 31 constructor(service) { |
| 32 this.connections_ = new Map(); |
| 33 this.providers_ = new Map(); |
| 34 this.pendingRequests_ = new Map(); |
| 35 this.connection_ = null; |
| 36 |
| 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 } |
| 50 |
| 51 provideService(service, factory) { |
| 52 // TODO(hansmuller): if !factory, remove provider and close its |
| 53 // connections. |
| 54 // TODO(hansmuller): if this.connection_ is null, throw an error. |
| 55 var provider = { |
| 56 service: service, |
| 57 factory: factory, |
| 58 connections: [], |
| 59 }; |
| 60 this.providers_.set(service.name, provider); |
| 61 |
| 62 if (this.pendingRequests_.has(service.name)) { |
| 63 connectToServiceImpl(service.name, pendingRequests_.get(service.name)); |
| 64 pendingRequests_.delete(service.name); |
| 65 } |
| 66 |
| 67 return this; |
| 68 } |
| 69 |
| 70 connectToService(service, client) { |
| 71 // TODO(hansmuler): if service.name isn't defined, throw an error. |
| 72 // TODO(hansmuller): if this.connection_ is null, throw an error. |
| 73 var serviceConnection = this.connections_.get(service.name); |
| 74 if (serviceConnection) |
| 75 return serviceConnection.remote; |
| 76 |
| 77 var pipe = coreModule.createMessagePipe(); |
| 78 this.connection_.remote.connectToService(service.name, pipe.handle1); |
| 79 var clientClass = client && service.client.stubClass; |
| 80 var serviceConnection = new connectionModule.Connection( |
| 81 pipe.handle0, clientClass, service.proxyClass); |
| 82 if (serviceConnection.local) |
| 83 serviceConnection.local.delegate$ = client; |
| 84 |
| 85 this.connections_.set(service.name, serviceConnection); |
| 86 return serviceConnection.remote; |
| 87 }; |
| 88 |
| 89 close() { |
| 90 if (!this.connection_) |
| 91 return; |
| 92 |
| 93 try { |
| 94 // Outgoing connections |
| 95 this.connections_.forEach(function(connection, serviceName) { |
| 96 connection.close(); |
| 97 }); |
| 98 // Incoming connections |
| 99 this.providers_.forEach(function(provider, serviceName) { |
| 100 provider.connections.forEach(function(connection) { |
| 101 connection.close(); |
| 102 }); |
| 103 }); |
| 104 this.connection_.close(); |
| 105 } finally { |
| 106 this.connections_ = null; |
| 107 this.providers_ = null; |
| 108 this.pendingRequests_ = null; |
| 109 this.connection_ = null; |
| 110 this.handle_ = null; |
| 111 } |
| 112 } |
| 113 } |
| 114 |
| 115 var exports = {}; |
| 116 exports.ServiceProvider = ServiceProvider; |
| 117 return exports; |
| 118 }); |
OLD | NEW |