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/mojo", [ | |
6 "mojo/public/interfaces/application/service_provider.mojom", | |
7 "mojo/public/js/connection", | |
8 "mojo/public/js/core", | |
9 "services/js/bridge", | |
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 | |
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.stubClass, | |
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.connections_ = new Map(); | |
76 this.providers_ = new Map(); | |
77 this.pendingRequests_ = new Map(); | |
78 this.connection_ = null; | |
79 this.handle_ = messagePipeHandle; | |
80 this.connection_ = new connection.Connection( | |
81 this.handle_, | |
82 service.ServiceProvider.client.stubClass, | |
83 service.ServiceProvider.proxyClass); | |
84 this.connection_.local.delegate$ = { | |
85 connectToService: connectToServiceImpl.bind(this) | |
86 }; | |
87 } | |
88 | |
89 ServiceProvider.prototype.provideService = function(service, factory) { | |
90 // TODO(hansmuller): if !factory, remove provider and close its connections. | |
91 // TODO(hansmuller): if this.connection_ is null, throw an error. | |
92 var provider = { | |
93 service: service, | |
94 factory: factory, | |
95 connections: [], | |
96 }; | |
97 this.providers_.set(service.name, provider); | |
98 | |
99 if (this.pendingRequests_.has(service.name)) { | |
100 connectToServiceImpl(service.name, pendingRequests_.get(service.name)); | |
101 pendingRequests_.delete(service.name); | |
102 } | |
103 | |
104 return this; | |
105 }; | |
106 | |
107 ServiceProvider.prototype.connectToService = function(service, client) { | |
108 // TODO(hansmuler): if service.name isn't defined, throw an error. | |
109 // TODO(hansmuller): if this.connection_ is null, throw an error. | |
110 var serviceConnection = this.connections_.get(service.name); | |
111 if (serviceConnection) | |
112 return serviceConnection.remote; | |
113 | |
114 var pipe = core.createMessagePipe(); | |
115 this.connection_.remote.connectToService(service.name, pipe.handle1); | |
116 var clientClass = client && service.client.stubClass; | |
117 var serviceConnection = | |
118 new connection.Connection(pipe.handle0, clientClass, service.proxyClass); | |
119 if (serviceConnection.local) | |
120 serviceConnection.local.delegate$ = client; | |
121 | |
122 this.connections_.set(service.name, serviceConnection); | |
123 return serviceConnection.remote; | |
124 }; | |
125 | |
126 ServiceProvider.prototype.close = function() { | |
127 if (!this.connection_) | |
128 return; | |
129 | |
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.connections_ = null; | |
144 this.providers_ = null; | |
145 this.pendingRequests_ = null; | |
146 this.connection_ = null; | |
147 this.handle_ = null; | |
148 | |
149 shell().applications_.forEach(function(application, url) { | |
150 if (application === this) | |
151 shell().applications_.delete(url); | |
152 }, this); | |
153 } | |
154 }; | |
155 | |
156 function quit() { | |
157 if (requestorValue) | |
158 requestor().close(); | |
159 if (shellValue) | |
160 shell().close(); | |
161 bridge.quit(); | |
162 } | |
163 | |
164 var exports = {}; | |
165 exports.requestor = requestor; | |
166 exports.shell = shell; | |
167 exports.quit = quit; | |
168 return exports; | |
169 }); | |
OLD | NEW |