Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(31)

Side by Side Diff: mojo/public/dart/src/application.dart

Issue 968243003: Dart: Adds optional named arguments for creating bindings. (Closed) Base URL: git@github.com:domokit/mojo.git@master
Patch Set: Binding -> Stub, delegate -> impl Created 5 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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,
14 {Function onClosed}) {
14 _application = application; 15 _application = application;
15 _stub = new application_mojom.ApplicationStub.fromEndpoint(endpoint) 16 // We wrap the onClosed callback in a closure to ensure that all
16 ..delegate = this 17 // necessary cleanup is performed on a PEER_CLOSED signal.
17 ..listen(); 18 _stub = new application_mojom.ApplicationStub.fromEndpoint(
19 endpoint, impl: this, onClosed: _closer(onClosed));
18 } 20 }
19 21
20 _ApplicationImpl.fromHandle(Application application, core.MojoHandle handle) { 22 _ApplicationImpl.fromHandle(Application application, core.MojoHandle handle,
23 {Function onClosed}) {
21 _application = application; 24 _application = application;
22 _stub = new application_mojom.ApplicationStub.fromHandle(handle) 25 _stub = new application_mojom.ApplicationStub.fromHandle(
23 ..delegate = this 26 handle, impl: this, onClosed: _closer(onClosed));
24 ..listen();
25 } 27 }
26 28
27 void initialize(bindings.ProxyBase shellProxy, List<String> args, 29 void initialize(
28 String url) { 30 bindings.ProxyBase shellProxy, List<String> args, String url) {
29 assert(shell == null); 31 assert(shell == null);
30 shell = shellProxy; 32 shell = shellProxy;
31 _application.initialize(args, url); 33 _application.initialize(args, url);
32 } 34 }
33 35
34 void acceptConnection(String requestorUrl, ServiceProviderStub services, 36 void acceptConnection(String requestorUrl,
35 bindings.ProxyBase exposedServices, String requested_url) => 37 ServiceProviderStub services,
36 _application._acceptConnection(requestorUrl, services, exposedServices); 38 bindings.ProxyBase exposedServices,
39 String resolvedUrl) =>
40 _application._acceptConnection(
41 requestorUrl, services, exposedServices, resolvedUrl);
37 42
38 void requestQuit() => _application._requestQuitAndClose(); 43 void requestQuit() => _application._requestQuitAndClose();
39 44
40 void close({bool nodefer: false}) => shell.close(); 45 Function _closer(Function onClosed) {
46 return (() {
47 if(onClosed != null) {
sky 2015/03/03 20:14:13 nit: space after if?
zra 2015/03/03 21:28:54 Done.
48 onClosed();
49 }
50 close();
51 });
52 }
53
54 void close({bool nodefer: false}) {
55 shell.close();
56 _stub.close();
57 }
41 } 58 }
42 59
43 // TODO(zra): Better documentation and examples. 60 // TODO(zra): Better documentation and examples.
44 // To implement, do the following: 61 // To implement, do the following:
45 // - Optionally override initialize() to process command-line args. 62 // - Optionally override initialize() to process command-line args.
46 // - Optionally override acceptConnection() if services are to be provided. 63 // - Optionally override acceptConnection() if services are to be provided.
47 // - Optionally override close() to clean up application resources. 64 // - Optionally override close() to clean up application resources.
48 abstract class Application { 65 abstract class Application {
49 _ApplicationImpl _applicationImpl; 66 _ApplicationImpl _applicationImpl;
50 List<ApplicationConnection> _applicationConnections; 67 List<ApplicationConnection> _applicationConnections;
51 68
52 Application(core.MojoMessagePipeEndpoint endpoint) { 69 Application(core.MojoMessagePipeEndpoint endpoint, {Function onClosed}) {
53 _applicationConnections = []; 70 _applicationConnections = [];
54 _applicationImpl = new _ApplicationImpl(this, endpoint); 71 // We wrap the onClosed callback in a closure to ensure that all
72 // necessary cleanup is performed on a PEER_CLOSED signal.
73 _applicationImpl = new _ApplicationImpl(
74 this, endpoint, onClosed: _closer(onClosed));
55 } 75 }
56 76
57 Application.fromHandle(core.MojoHandle appHandle) { 77 Application.fromHandle(core.MojoHandle appHandle, {Function onClosed}) {
58 _applicationConnections = []; 78 _applicationConnections = [];
59 _applicationImpl = new _ApplicationImpl.fromHandle(this, appHandle); 79 _applicationImpl = new _ApplicationImpl.fromHandle(
80 this, appHandle, onClosed: _closer(onClosed));
60 } 81 }
61 82
62 void initialize(List<String> args, String url) {} 83 void initialize(List<String> args, String url) {}
63 84
64 // TODO(skydart): This is a temporary fix to allow sky application to consume 85 // TODO(skydart): This is a temporary fix to allow sky application to consume
65 // mojo services. Do not use for any other purpose. 86 // mojo services. Do not use for any other purpose.
66 void initializeFromShellProxy(shell_mojom.ShellProxy shellProxy, 87 void initializeFromShellProxy(shell_mojom.ShellProxy shellProxy,
67 List<String> args, String url) => 88 List<String> args, String url) =>
68 _applicationImpl.initialize(shellProxy, args, url); 89 _applicationImpl.initialize(shellProxy, args, url);
69 90
70 // Returns a connection to the app at |url|. 91 // Returns a connection to the app at |url|.
71 ApplicationConnection connectToApplication(String url) { 92 ApplicationConnection connectToApplication(String url) {
72 var proxy = new ServiceProviderProxy.unbound(); 93 var proxy = new ServiceProviderProxy.unbound();
73 var stub = new ServiceProviderStub.unbound(); 94 var stub = new ServiceProviderStub.unbound();
74 _applicationImpl.shell.ptr.connectToApplication(url, proxy, stub); 95 _applicationImpl.shell.ptr.connectToApplication(url, proxy, stub);
75 var connection = new ApplicationConnection(stub, proxy); 96 var connection = new ApplicationConnection(stub, proxy);
76 _applicationConnections.add(connection); 97 _applicationConnections.add(connection);
77 return connection; 98 return connection;
78 } 99 }
79 100
80 void connectToService(String url, bindings.ProxyBase proxy) { 101 void connectToService(String url, bindings.ProxyBase proxy) {
81 connectToApplication(url).requestService(proxy); 102 connectToApplication(url).requestService(proxy);
82 } 103 }
83 104
84 void requestQuit() {} 105 void requestQuit() {}
85 106
86 void _requestQuitAndClose() { 107 void _requestQuitAndClose() {
87 requestQuit(); 108 requestQuit();
88 close(); 109 close();
89 } 110 }
90 111
112 Function _closer(Function onClose) {
113 return (() {
114 if (onClose != null) {
115 onClose();
116 }
117 close();
118 });
119 }
120
91 void close() { 121 void close() {
92 assert(_applicationImpl != null);
93 _applicationConnections.forEach((c) => c.close()); 122 _applicationConnections.forEach((c) => c.close());
94 _applicationConnections.clear(); 123 _applicationConnections.clear();
95 _applicationImpl.close(); 124 if (_applicationImpl != null) {
125 _applicationImpl.close();
126 _applicationImpl = null;
127 }
96 } 128 }
97 129
98 void _acceptConnection(String requestorUrl, ServiceProviderStub services, 130 void _acceptConnection(
99 ServiceProviderProxy exposedServices) { 131 String requestorUrl,
132 ServiceProviderStub services,
133 ServiceProviderProxy exposedServices,
134 String resolvedUrl) {
100 var connection = new ApplicationConnection(services, exposedServices); 135 var connection = new ApplicationConnection(services, exposedServices);
101 _applicationConnections.add(connection); 136 _applicationConnections.add(connection);
102 acceptConnection(requestorUrl, connection); 137 acceptConnection(requestorUrl, connection, resolvedUrl);
103 } 138 }
104 139
105 // Override this method to provide services on |connection|. 140 // Override this method to provide services on |connection|.
106 // If you provide at least one service or set fallbackServiceProvider, 141 // If you provide at least one service or set fallbackServiceProvider,
107 // then you must invoke connection.listen(). 142 // then you must invoke connection.listen().
hansmuller1 2015/03/03 20:32:16 Is it still necessary to call connection.listen()
zra 2015/03/03 21:28:54 Yes at the moment. listen() can't be called until
108 void acceptConnection(String requestorUrl, ApplicationConnection connection) { 143 void acceptConnection(String requestorUrl,
144 ApplicationConnection connection,
145 String resolvedUrl) {
109 } 146 }
110 } 147 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698