Chromium Code Reviews| 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/mojo", [ | 5 define("mojo/services/public/js/service-provider", [ |
|
Aaron Boodman
2014/12/02 01:13:22
We typically use underscores, not dashes, in filen
hansmuller
2014/12/02 15:55:00
JavaScript (and Sky) files typically use dashes. I
| |
| 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","console", |
| 9 "services/js/bridge", | 9 ], function(spModule, connectionModule, coreModule,console) { |
|
Aaron Boodman
2014/12/02 01:13:22
Missing space before 'console'.
hansmuller
2014/12/02 15:55:00
Oops, sorry about that. The console import was jus
| |
| 10 ], function(service, connection, core, bridge) { | |
| 11 | |
| 12 function Shell() { | |
| 13 this.applications_ = new Map(); | |
| 14 } | |
| 15 | |
| 16 Shell.prototype.connectToApplication = function(url) { | |
| 17 var application = this.applications_.get(url); | |
| 18 if (application) | |
| 19 return application; | |
| 20 application = new ServiceProvider(bridge.connectToApplication(url)); | |
| 21 this.applications_.set(url, application); | |
| 22 return application; | |
| 23 }; | |
| 24 | |
| 25 Shell.prototype.connectToService = function (url, service, client) { | |
| 26 return this.connectToApplication(url).connectToService(service, client); | |
| 27 }; | |
| 28 | |
| 29 Shell.prototype.close = function() { | |
| 30 shell().applications_.forEach(function(application, url) { | |
| 31 application.close(); | |
| 32 }); | |
| 33 shell().applications_.clear(); | |
| 34 }; | |
| 35 | |
| 36 var shellValue = null; | |
| 37 | |
| 38 function shell() { | |
| 39 if (!shellValue) | |
| 40 shellValue = new Shell(); | |
| 41 return shellValue; | |
| 42 } | |
| 43 | |
| 44 var requestorValue = null; | |
| 45 | |
| 46 function requestor() { | |
| 47 if (!requestorValue) { | |
| 48 var handle = bridge.requestorMessagePipeHandle(); | |
| 49 requestorValue = handle && new ServiceProvider(handle); | |
| 50 } | |
| 51 return requestorValue; | |
| 52 } | |
| 53 | 10 |
| 54 function connectToServiceImpl(serviceName, serviceHandle) { | 11 function connectToServiceImpl(serviceName, serviceHandle) { |
| 55 var provider = this.providers_.get(serviceName); | 12 var provider = this.providers_.get(serviceName); |
| 56 if (!provider) { | 13 if (!provider) { |
| 57 this.pendingRequests_.set(serviceName, serviceHandle); | 14 this.pendingRequests_.set(serviceName, serviceHandle); |
| 58 return; | 15 return; |
| 59 } | 16 } |
| 60 | 17 |
| 61 var serviceConnection = new connection.Connection( | 18 var serviceConnection = new connectionModule.Connection( |
| 62 serviceHandle, | 19 serviceHandle, |
| 63 provider.service.stubClass, | 20 provider.service.stubClass, |
| 64 provider.service.client && provider.service.client.proxyClass); | 21 provider.service.client && provider.service.client.proxyClass); |
| 65 | 22 |
| 66 serviceConnection.local.connection$ = serviceConnection; | 23 serviceConnection.local.connection$ = serviceConnection; |
| 67 serviceConnection.local.delegate$ = | 24 serviceConnection.local.delegate$ = |
| 68 new provider.factory(serviceConnection.remote); | 25 new provider.factory(serviceConnection.remote); |
| 69 | 26 |
| 70 provider.connections.push(serviceConnection); | 27 provider.connections.push(serviceConnection); |
| 71 } | 28 } |
| 72 | 29 |
| 73 function ServiceProvider(messagePipeHandle) { | 30 function ServiceProvider(messagePipeHandle) { |
| 74 // TODO(hansmuller): if messagePipeHandle is null, throw an exception. | 31 // TODO(hansmuller): if messagePipeHandle is null, throw an exception. |
| 75 this.connections_ = new Map(); | 32 this.connections_ = new Map(); |
| 76 this.providers_ = new Map(); | 33 this.providers_ = new Map(); |
| 77 this.pendingRequests_ = new Map(); | 34 this.pendingRequests_ = new Map(); |
| 78 this.connection_ = null; | 35 this.connection_ = null; |
| 79 this.handle_ = messagePipeHandle; | 36 this.handle_ = messagePipeHandle; |
| 80 this.connection_ = new connection.Connection( | 37 this.connection_ = new connectionModule.Connection( |
| 81 this.handle_, | 38 this.handle_, |
| 82 service.ServiceProvider.client.stubClass, | 39 spModule.ServiceProvider.client.stubClass, |
| 83 service.ServiceProvider.proxyClass); | 40 spModule.ServiceProvider.proxyClass); |
| 84 this.connection_.local.delegate$ = { | 41 this.connection_.local.delegate$ = { |
| 85 connectToService: connectToServiceImpl.bind(this) | 42 connectToService: connectToServiceImpl.bind(this) |
| 86 }; | 43 }; |
| 87 } | 44 } |
| 88 | 45 |
| 89 ServiceProvider.prototype.provideService = function(service, factory) { | 46 ServiceProvider.prototype.provideService = function(service, factory) { |
| 90 // TODO(hansmuller): if !factory, remove provider and close its connections. | 47 // TODO(hansmuller): if !factory, remove provider and close its connections. |
| 91 // TODO(hansmuller): if this.connection_ is null, throw an error. | 48 // TODO(hansmuller): if this.connection_ is null, throw an error. |
| 92 var provider = { | 49 var provider = { |
| 93 service: service, | 50 service: service, |
| (...skipping 10 matching lines...) Expand all Loading... | |
| 104 return this; | 61 return this; |
| 105 }; | 62 }; |
| 106 | 63 |
| 107 ServiceProvider.prototype.connectToService = function(service, client) { | 64 ServiceProvider.prototype.connectToService = function(service, client) { |
| 108 // TODO(hansmuler): if service.name isn't defined, throw an error. | 65 // TODO(hansmuler): if service.name isn't defined, throw an error. |
| 109 // TODO(hansmuller): if this.connection_ is null, throw an error. | 66 // TODO(hansmuller): if this.connection_ is null, throw an error. |
| 110 var serviceConnection = this.connections_.get(service.name); | 67 var serviceConnection = this.connections_.get(service.name); |
| 111 if (serviceConnection) | 68 if (serviceConnection) |
| 112 return serviceConnection.remote; | 69 return serviceConnection.remote; |
| 113 | 70 |
| 114 var pipe = core.createMessagePipe(); | 71 var pipe = coreModule.createMessagePipe(); |
| 115 this.connection_.remote.connectToService(service.name, pipe.handle1); | 72 this.connection_.remote.connectToService(service.name, pipe.handle1); |
| 116 var clientClass = client && service.client.stubClass; | 73 var clientClass = client && service.client.stubClass; |
| 117 var serviceConnection = | 74 var serviceConnection = new connectionModule.Connection( |
| 118 new connection.Connection(pipe.handle0, clientClass, service.proxyClass); | 75 pipe.handle0, clientClass, service.proxyClass); |
| 119 if (serviceConnection.local) | 76 if (serviceConnection.local) |
| 120 serviceConnection.local.delegate$ = client; | 77 serviceConnection.local.delegate$ = client; |
| 121 | 78 |
| 122 this.connections_.set(service.name, serviceConnection); | 79 this.connections_.set(service.name, serviceConnection); |
| 123 return serviceConnection.remote; | 80 return serviceConnection.remote; |
| 124 }; | 81 }; |
| 125 | 82 |
| 126 ServiceProvider.prototype.close = function() { | 83 ServiceProvider.prototype.close = function() { |
| 127 if (!this.connection_) | 84 if (!this.connection_) |
| 128 return; | 85 return; |
| 129 | 86 |
| 130 try { | 87 try { |
| 131 // Outgoing connections | 88 // Outgoing connections |
| 132 this.connections_.forEach(function(connection, serviceName) { | 89 this.connections_.forEach(function(connection, serviceName) { |
| 133 connection.close(); | 90 connection.close(); |
| 134 }); | 91 }); |
| 135 // Incoming connections | 92 // Incoming connections |
| 136 this.providers_.forEach(function(provider, serviceName) { | 93 this.providers_.forEach(function(provider, serviceName) { |
| 137 provider.connections.forEach(function(connection) { | 94 provider.connections.forEach(function(connection) { |
| 138 connection.close(); | 95 connection.close(); |
| 139 }); | 96 }); |
| 140 }); | 97 }); |
| 141 this.connection_.close(); | 98 this.connection_.close(); |
| 142 } finally { | 99 } finally { |
| 143 this.connections_ = null; | 100 this.connections_ = null; |
| 144 this.providers_ = null; | 101 this.providers_ = null; |
| 145 this.pendingRequests_ = null; | 102 this.pendingRequests_ = null; |
| 146 this.connection_ = null; | 103 this.connection_ = null; |
| 147 this.handle_ = null; | 104 this.handle_ = null; |
| 148 | |
| 149 shell().applications_.forEach(function(application, url) { | |
| 150 if (application === this) | |
| 151 shell().applications_.delete(url); | |
| 152 }, this); | |
| 153 } | 105 } |
| 154 }; | 106 }; |
| 155 | 107 |
| 156 function quit() { | |
| 157 if (requestorValue) | |
| 158 requestor().close(); | |
| 159 if (shellValue) | |
| 160 shell().close(); | |
| 161 bridge.quit(); | |
| 162 } | |
| 163 | |
| 164 var exports = {}; | 108 var exports = {}; |
| 165 exports.requestor = requestor; | 109 exports.ServiceProvider = ServiceProvider; |
| 166 exports.shell = shell; | |
| 167 exports.quit = quit; | |
| 168 return exports; | 110 return exports; |
| 169 }); | 111 }); |
| OLD | NEW |