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