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/apps/js/mojo", [ | 5 define("mojo/apps/js/mojo", [ |
| 6 "mojo/public/interfaces/application/service_provider.mojom", | |
| 6 "mojo/public/js/bindings/connection", | 7 "mojo/public/js/bindings/connection", |
| 7 "mojo/apps/js/bridge", | 8 "mojo/public/js/bindings/core", |
| 8 ], function(connection, mojo) { | 9 "mojo/apps/js/bridge", "console" |
|
Aaron Boodman
2014/10/24 22:17:19
Put each import on own line for readability.
hansmuller
2014/10/27 22:43:22
Sorry, the console import shouldn't have been ther
| |
| 10 ], function(service, connection, core, bridge, console) { | |
| 9 | 11 |
| 10 function connectToService(url, service, client) { | 12 function Shell() { |
| 11 var serviceHandle = mojo.connectToService(url, service.name); | 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; | |
|
Aaron Boodman
2014/10/24 22:17:19
hm. This is different than the C++ model where eve
hansmuller
2014/10/27 22:43:22
I don't understand the implications in terms of ex
| |
| 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 shell_ = null; | |
|
Aaron Boodman
2014/10/24 22:17:19
I have not seen the convention of trailing undersc
hansmuller
2014/10/27 22:43:22
I've replaced the underscores with a Value suffix.
| |
| 37 | |
| 38 function shell() { | |
| 39 if (!shell_) | |
| 40 shell_ = new Shell(); | |
| 41 return shell_; | |
| 42 } | |
| 43 | |
| 44 var requestor_ = null; | |
| 45 | |
| 46 function requestor() { | |
| 47 if (!requestor_) { | |
| 48 var handle = bridge.requestorMessagePipeHandle(); | |
| 49 requestor_ = handle && new ServiceProvider(handle); | |
| 50 } | |
| 51 return requestor_; | |
| 52 } | |
| 53 | |
| 54 function connectToServiceImpl(serviceName, serviceHandle) { | |
| 55 var provider = this.providers_.get(serviceName); | |
| 56 if (!provider) { | |
| 57 this.pendingRequests_.set(serviceName, serviceHandle); | |
| 58 return; | |
| 59 } | |
| 60 | |
| 61 var serviceConnection = new connection.Connection( | |
| 62 serviceHandle, | |
| 63 provider.service.delegatingStubClass, | |
| 64 provider.service.client && provider.service.client.proxyClass); | |
| 65 | |
| 66 serviceConnection.local.connection$ = serviceConnection; | |
| 67 serviceConnection.local.delegate$ = | |
| 68 new provider.factory(serviceConnection.remote); | |
| 69 | |
| 70 provider.connections.push(serviceConnection); | |
| 71 } | |
| 72 | |
| 73 function ServiceProvider(messagePipeHandle) { | |
| 74 // TODO(hansmuller): if messagePipeHandle is null, throw an exception. | |
| 75 this.initFields(); | |
| 76 this.handle_ = messagePipeHandle; | |
| 77 this.connection_ = new connection.Connection( | |
| 78 this.handle_, | |
| 79 service.ServiceProvider.client.delegatingStubClass, | |
| 80 service.ServiceProvider.proxyClass); | |
| 81 this.connection_.local.delegate$ = { | |
| 82 connectToService: connectToServiceImpl.bind(this) | |
| 83 }; | |
| 84 } | |
| 85 | |
| 86 ServiceProvider.prototype.initFields = function() { | |
| 87 this.connections_ = new Map(); | |
| 88 this.providers_ = new Map(); | |
| 89 this.pendingRequests_ = new Map(); | |
| 90 this.handle_ = null; | |
| 91 this.connection_ = null; | |
| 92 }; | |
| 93 | |
| 94 ServiceProvider.prototype.provideService = function(service, factory) { | |
| 95 // TODO(hansmuller): if !factory, remove provider and close its connections. | |
| 96 var provider = { | |
| 97 service: service, | |
| 98 factory: factory, | |
| 99 connections: [], | |
| 100 }; | |
| 101 this.providers_.set(service.name, provider); | |
| 102 | |
| 103 if (this.pendingRequests_.has(service.name)) { | |
| 104 connectToServiceImpl(service.name, pendingRequests_.get(service.name)); | |
| 105 pendingRequests_.delete(service.name); | |
| 106 } | |
| 107 | |
| 108 return this; | |
| 109 }; | |
| 110 | |
| 111 ServiceProvider.prototype.connectToService = function(service, client) { | |
| 112 // TODO(hansmuler): if service.name isn't defined, throw an exception | |
| 113 var serviceConnection = this.connections_.get(service.name); | |
| 114 if (serviceConnection) | |
| 115 return serviceConnection.remote; | |
| 116 | |
| 117 var pipe = core.createMessagePipe(); | |
| 118 this.connection_.remote.connectToService(service.name, pipe.handle1); | |
| 12 var clientClass = client && service.client.delegatingStubClass; | 119 var clientClass = client && service.client.delegatingStubClass; |
| 13 var serviceConnection = | 120 var serviceConnection = |
| 14 new connection.Connection(serviceHandle, clientClass, service.proxyClass); | 121 new connection.Connection(pipe.handle0, clientClass, service.proxyClass); |
| 15 if (serviceConnection.local) | 122 if (serviceConnection.local) |
| 16 serviceConnection.local.delegate$ = client; | 123 serviceConnection.local.delegate$ = client; |
| 17 serviceConnection.remote.connection$ = serviceConnection; | 124 |
| 125 this.connections_.set(service.name, serviceConnection); | |
| 18 return serviceConnection.remote; | 126 return serviceConnection.remote; |
| 127 }; | |
| 128 | |
| 129 ServiceProvider.prototype.close = function() { | |
| 130 try { | |
| 131 // Outgoing connections | |
| 132 this.connections_.forEach(function(connection, serviceName) { | |
| 133 connection.close(); | |
| 134 }); | |
| 135 // Incoming connections | |
| 136 this.providers_.forEach(function(provider, serviceName) { | |
| 137 provider.connections.forEach(function(connection) { | |
| 138 connection.close(); | |
| 139 }); | |
| 140 }); | |
| 141 this.connection_.close(); | |
| 142 } finally { | |
| 143 this.initFields(); | |
|
Aaron Boodman
2014/10/24 22:17:19
Almost seems like instead all the fields should be
hansmuller
2014/10/27 22:43:22
Good point; the objective here was just to drop al
| |
| 144 shell().applications_.forEach(function(application, url) { | |
| 145 if (application === this) | |
| 146 shell().applications_.delete(url); | |
| 147 }, this); | |
| 148 } | |
| 149 }; | |
| 150 | |
| 151 function quit() { | |
| 152 if (requestor_) | |
| 153 requestor().close(); | |
| 154 if (shell_) | |
| 155 shell().close(); | |
| 156 bridge.quit(); | |
| 19 } | 157 } |
| 20 | 158 |
| 21 var exports = {}; | 159 var exports = {}; |
| 22 exports.connectToService = connectToService; | 160 exports.requestor = requestor; |
| 23 exports.quit = mojo.quit; | 161 exports.shell = shell; |
| 162 exports.quit = quit; | |
| 24 return exports; | 163 return exports; |
| 25 }); | 164 }); |
| 26 | |
| OLD | NEW |