| 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 class _ApplicationImpl implements application_mojom.Application { | 7 class _ApplicationImpl implements application_mojom.Application { |
| 8 application_mojom.ApplicationStub _stub; | 8 application_mojom.ApplicationStub _stub; |
| 9 shell_mojom.ShellProxy shell; | 9 shell_mojom.ShellProxy shell; |
| 10 Application _application; | 10 Application _application; |
| 11 | 11 |
| 12 _ApplicationImpl(Application application, | 12 _ApplicationImpl(Application application, |
| 13 core.MojoMessagePipeEndpoint endpoint) { | 13 core.MojoMessagePipeEndpoint endpoint, {Function onClosed}) { |
| 14 _application = application; | 14 _application = application; |
| 15 _stub = new application_mojom.ApplicationStub.fromEndpoint(endpoint) | 15 // We wrap the onClosed callback in a closure to ensure that all |
| 16 ..delegate = this | 16 // necessary cleanup is performed on a PEER_CLOSED signal. |
| 17 ..listen(); | 17 _stub = new application_mojom.ApplicationStub.fromEndpoint( |
| 18 endpoint, |
| 19 impl: this, |
| 20 onClosed: _closer(onClosed)); |
| 18 } | 21 } |
| 19 | 22 |
| 20 _ApplicationImpl.fromHandle(Application application, core.MojoHandle handle) { | 23 _ApplicationImpl.fromHandle(Application application, core.MojoHandle handle, |
| 24 {Function onClosed}) { |
| 21 _application = application; | 25 _application = application; |
| 22 _stub = new application_mojom.ApplicationStub.fromHandle(handle) | 26 _stub = new application_mojom.ApplicationStub.fromHandle( |
| 23 ..delegate = this | 27 handle, |
| 24 ..listen(); | 28 impl: this, |
| 29 onClosed: _closer(onClosed)); |
| 25 } | 30 } |
| 26 | 31 |
| 27 void initialize(bindings.ProxyBase shellProxy, List<String> args, | 32 void initialize(bindings.ProxyBase shellProxy, List<String> args, |
| 28 String url) { | 33 String url) { |
| 29 assert(shell == null); | 34 assert(shell == null); |
| 30 shell = shellProxy; | 35 shell = shellProxy; |
| 31 _application.initialize(args, url); | 36 _application.initialize(args, url); |
| 32 } | 37 } |
| 33 | 38 |
| 34 @override | 39 @override |
| 35 void acceptConnection(String requestorUrl, ServiceProviderStub services, | 40 void acceptConnection(String requestorUrl, ServiceProviderStub services, |
| 36 bindings.ProxyBase exposedServices, String resolvedUrl) => | 41 bindings.ProxyBase exposedServices, String resolvedUrl) => |
| 37 _application._acceptConnection(requestorUrl, services, exposedServices, | 42 _application._acceptConnection( |
| 38 resolvedUrl); | 43 requestorUrl, |
| 44 services, |
| 45 exposedServices, |
| 46 resolvedUrl); |
| 39 | 47 |
| 40 @override | 48 @override |
| 41 void requestQuit() => _application._requestQuitAndClose(); | 49 void requestQuit() => _application._requestQuitAndClose(); |
| 42 | 50 |
| 43 void close({bool nodefer: false}) => shell.close(); | 51 Function _closer(Function onClosed) { |
| 52 return (() { |
| 53 if (onClosed != null) { |
| 54 onClosed(); |
| 55 } |
| 56 close(); |
| 57 }); |
| 58 } |
| 59 |
| 60 void close({bool nodefer: false}) { |
| 61 shell.close(); |
| 62 _stub.close(); |
| 63 } |
| 44 } | 64 } |
| 45 | 65 |
| 46 // TODO(zra): Better documentation and examples. | 66 // TODO(zra): Better documentation and examples. |
| 47 // To implement, do the following: | 67 // To implement, do the following: |
| 48 // - Optionally override initialize() to process command-line args. | 68 // - Optionally override initialize() to process command-line args. |
| 49 // - Optionally override acceptConnection() if services are to be provided. | 69 // - Optionally override acceptConnection() if services are to be provided. |
| 50 // - Optionally override close() to clean up application resources. | 70 // - Optionally override close() to clean up application resources. |
| 51 abstract class Application { | 71 abstract class Application { |
| 52 _ApplicationImpl _applicationImpl; | 72 _ApplicationImpl _applicationImpl; |
| 53 List<ApplicationConnection> _applicationConnections; | 73 List<ApplicationConnection> _applicationConnections; |
| 54 | 74 |
| 55 Application(core.MojoMessagePipeEndpoint endpoint) { | 75 Application(core.MojoMessagePipeEndpoint endpoint, {Function onClosed}) { |
| 56 _applicationConnections = []; | 76 _applicationConnections = []; |
| 57 _applicationImpl = new _ApplicationImpl(this, endpoint); | 77 // We wrap the onClosed callback in a closure to ensure that all |
| 78 // necessary cleanup is performed on a PEER_CLOSED signal. |
| 79 _applicationImpl = |
| 80 new _ApplicationImpl(this, endpoint, onClosed: _closer(onClosed)); |
| 58 } | 81 } |
| 59 | 82 |
| 60 Application.fromHandle(core.MojoHandle appHandle) { | 83 Application.fromHandle(core.MojoHandle appHandle, {Function onClosed}) { |
| 61 _applicationConnections = []; | 84 _applicationConnections = []; |
| 62 _applicationImpl = new _ApplicationImpl.fromHandle(this, appHandle); | 85 _applicationImpl = |
| 86 new _ApplicationImpl.fromHandle(this, appHandle, onClosed: _closer(onClo
sed)); |
| 63 } | 87 } |
| 64 | 88 |
| 65 void initialize(List<String> args, String url) {} | 89 void initialize(List<String> args, String url) {} |
| 66 | 90 |
| 67 // TODO(skydart): This is a temporary fix to allow sky application to consume | 91 // TODO(skydart): This is a temporary fix to allow sky application to consume |
| 68 // mojo services. Do not use for any other purpose. | 92 // mojo services. Do not use for any other purpose. |
| 69 void initializeFromShellProxy(shell_mojom.ShellProxy shellProxy, | 93 void initializeFromShellProxy(shell_mojom.ShellProxy shellProxy, |
| 70 List<String> args, String url) => | 94 List<String> args, String url) => |
| 71 _applicationImpl.initialize(shellProxy, args, url); | 95 _applicationImpl.initialize(shellProxy, args, url); |
| 72 | 96 |
| (...skipping 11 matching lines...) Expand all Loading... |
| 84 connectToApplication(url).requestService(proxy); | 108 connectToApplication(url).requestService(proxy); |
| 85 } | 109 } |
| 86 | 110 |
| 87 void requestQuit() {} | 111 void requestQuit() {} |
| 88 | 112 |
| 89 void _requestQuitAndClose() { | 113 void _requestQuitAndClose() { |
| 90 requestQuit(); | 114 requestQuit(); |
| 91 close(); | 115 close(); |
| 92 } | 116 } |
| 93 | 117 |
| 118 Function _closer(Function onClose) { |
| 119 return (() { |
| 120 if (onClose != null) { |
| 121 onClose(); |
| 122 } |
| 123 close(); |
| 124 }); |
| 125 } |
| 126 |
| 94 void close() { | 127 void close() { |
| 95 assert(_applicationImpl != null); | 128 assert(_applicationImpl != null); |
| 96 _applicationConnections.forEach((c) => c.close()); | 129 _applicationConnections.forEach((c) => c.close()); |
| 97 _applicationConnections.clear(); | 130 _applicationConnections.clear(); |
| 98 _applicationImpl.close(); | 131 _applicationImpl.close(); |
| 99 } | 132 } |
| 100 | 133 |
| 101 void _acceptConnection(String requestorUrl, ServiceProviderStub services, | 134 void _acceptConnection(String requestorUrl, ServiceProviderStub services, |
| 102 ServiceProviderProxy exposedServices, String resolvedUrl) { | 135 ServiceProviderProxy exposedServices, String resolvedUrl) { |
| 103 var connection = new ApplicationConnection(services, exposedServices); | 136 var connection = new ApplicationConnection(services, exposedServices); |
| 104 _applicationConnections.add(connection); | 137 _applicationConnections.add(connection); |
| 105 acceptConnection(requestorUrl, resolvedUrl, connection); | 138 acceptConnection(requestorUrl, resolvedUrl, connection); |
| 106 } | 139 } |
| 107 | 140 |
| 108 // Override this method to provide services on |connection|. | 141 // Override this method to provide services on |connection|. |
| 109 // If you provide at least one service or set fallbackServiceProvider, | 142 // If you provide at least one service or set fallbackServiceProvider, |
| 110 // then you must invoke connection.listen(). | 143 // then you must invoke connection.listen(). |
| 111 void acceptConnection(String requestorUrl, String resolvedUrl, | 144 void acceptConnection(String requestorUrl, String resolvedUrl, |
| 112 ApplicationConnection connection) { | 145 ApplicationConnection connection) { |
| 113 } | 146 } |
| 114 } | 147 } |
| OLD | NEW |