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 part of application; | 5 part of application; |
6 | 6 |
7 // The Application interface doesn't explicitly have a Shell as a Client, but | 7 class ApplicationImpl implements application.ApplicationStub { |
8 // that is what is at the other end of the MessagePipe. | 8 shell_mojom.ShellProxy shell; |
9 abstract class Application extends application.ApplicationStub | 9 Application _application; |
10 with shell.ShellCalls { | |
11 List<service_provider.ServiceProviderProxy> _proxies; | |
12 | 10 |
13 Application(core.MojoMessagePipeEndpoint endpoint) : | 11 ApplicationImpl(this._application); |
14 _proxies = [], | |
15 super(endpoint); | |
16 | 12 |
17 Application.fromHandle(core.MojoHandle shellHandle) : | 13 void initialize(shell_mojom.ShellProxy shellProxy, List<String> args) { |
18 _proxies = [], | 14 assert(shell == null); |
19 super.fromHandle(shellHandle); | 15 shell = shellProxy; |
20 | 16 _application.initialize(args); |
21 Function stubFactoryClosure() => (endpoint) => null; | |
22 | |
23 void initialize(List<String> args) { | |
24 } | 17 } |
25 | 18 |
26 void acceptConnection( | 19 void acceptConnection( |
27 String requestorUrl, | 20 String requestorUrl, |
28 service_provider.ServiceProviderStub services, | 21 service_provider.ServiceProviderStub services, |
| 22 service_provider.ServiceProviderProxy exposedServices) => |
| 23 _application._acceptConnection(requestorUrl, services, exposedServices); |
| 24 |
| 25 void close() => shell.close(); |
| 26 } |
| 27 |
| 28 // TODO(zra): Better documentation and examples. |
| 29 // To implement, provide a stubFactoryClosure() that returns a function |
| 30 // that takes a MojoMessagePipeEndpoint and returns a Stub that provides the |
| 31 // Application's services. The function may return null if the Application |
| 32 // provides no services. Optionally override initialize() when needed. Call |
| 33 // listen() on a newly created Application to begin providing services. Call |
| 34 // connectToService() to request services from the Shell. Calling close() |
| 35 // closes connections to any requested ServiceProviders and the Shell. |
| 36 abstract class Application { |
| 37 application.ApplicationStub _applicationStub; |
| 38 ApplicationImpl _applicationImpl; |
| 39 List<service_provider.ServiceProviderProxy> _proxies; |
| 40 |
| 41 Application(core.MojoMessagePipeEndpoint endpoint) { |
| 42 _proxies = []; |
| 43 _applicationImpl = new ApplicationImpl(this); |
| 44 _applicationStub = new application.ApplicationStub(endpoint) |
| 45 ..delegate = _applicationImpl; |
| 46 } |
| 47 |
| 48 Application.fromHandle(core.MojoHandle appHandle) { |
| 49 _proxies = []; |
| 50 _applicationImpl = new ApplicationImpl(this); |
| 51 _applicationStub = new application.ApplicationStub.fromHandle(appHandle) |
| 52 ..delegate = _applicationImpl; |
| 53 } |
| 54 |
| 55 Function stubFactoryClosure(); |
| 56 |
| 57 void initialize(List<String> args) {} |
| 58 |
| 59 void connectToService(String url, bindings.Proxy proxy) { |
| 60 assert(!proxy.isBound); |
| 61 var endpoint = _connectToServiceHelper(url, proxy.name); |
| 62 proxy.bind(endpoint); |
| 63 } |
| 64 |
| 65 listen() => _applicationStub.listen(); |
| 66 |
| 67 void close() { |
| 68 assert(_proxies != null); |
| 69 assert(_applicationImpl != null); |
| 70 _proxies.forEach((c) => c.close()); |
| 71 _proxies.clear(); |
| 72 _applicationImpl.close(); |
| 73 } |
| 74 |
| 75 void _acceptConnection( |
| 76 String requestorUrl, |
| 77 service_provider.ServiceProviderStub services, |
29 service_provider.ServiceProviderProxy exposedServices) { | 78 service_provider.ServiceProviderProxy exposedServices) { |
30 var closure = stubFactoryClosure(); | 79 var closure = stubFactoryClosure(); |
31 if (closure != null) { | 80 if (closure != null) { |
32 var serviceProvider = new ServiceProvider(closure); | 81 var serviceProvider = new ServiceProvider(closure); |
33 services.delegate = serviceProvider; | 82 services.delegate = serviceProvider; |
34 services.listen(); | 83 services.listen(); |
35 } | 84 } |
36 } | 85 } |
37 | 86 |
38 void connectToService(String url, bindings.Proxy proxy) { | |
39 assert(!proxy.isBound); | |
40 var endpoint = _connectToServiceHelper(url, proxy.name); | |
41 proxy.bind(endpoint); | |
42 } | |
43 | |
44 core.MojoMessagePipeEndpoint _connectToServiceHelper( | 87 core.MojoMessagePipeEndpoint _connectToServiceHelper( |
45 String url, String service) { | 88 String url, String service) { |
46 var applicationPipe = new core.MojoMessagePipe(); | 89 var applicationPipe = new core.MojoMessagePipe(); |
47 var proxyEndpoint = applicationPipe.endpoints[0]; | 90 var proxyEndpoint = applicationPipe.endpoints[0]; |
48 var applicationEndpoint = applicationPipe.endpoints[1]; | 91 var applicationEndpoint = applicationPipe.endpoints[1]; |
49 var serviceProviderProxy = | 92 var serviceProviderProxy = |
50 new service_provider.ServiceProviderProxy.unbound(); | 93 new service_provider.ServiceProviderProxy.unbound(); |
51 callConnectToApplication(url, serviceProviderProxy, null); | 94 _applicationImpl.shell.callConnectToApplication( |
| 95 url, serviceProviderProxy, null); |
52 serviceProviderProxy.callConnectToService(service, applicationEndpoint); | 96 serviceProviderProxy.callConnectToService(service, applicationEndpoint); |
53 _proxies.add(serviceProviderProxy); | 97 _proxies.add(serviceProviderProxy); |
54 return proxyEndpoint; | 98 return proxyEndpoint; |
55 } | 99 } |
56 | |
57 void close() { | |
58 _proxies.forEach((c) => c.close()); | |
59 _proxies.clear(); | |
60 super.close(); | |
61 } | |
62 } | 100 } |
OLD | NEW |