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

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

Issue 982673002: Dart: Removes need to call listen(). (Closed) Base URL: git@github.com:domokit/mojo.git@master
Patch Set: Merge 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 typedef Object ServiceFactory(core.MojoMessagePipeEndpoint endpoint); 7 typedef Object ServiceFactory(core.MojoMessagePipeEndpoint endpoint);
8 typedef void FallbackServiceFactory(String interfaceName, 8 typedef void FallbackServiceFactory(String interfaceName,
9 core.MojoMessagePipeEndpoint endpoint); 9 core.MojoMessagePipeEndpoint endpoint);
10 10
11 11
12 class LocalServiceProvider implements ServiceProvider { 12 class LocalServiceProvider implements ServiceProvider {
13 final ApplicationConnection connection; 13 final ApplicationConnection connection;
14 ServiceProviderStub _stub; 14 ServiceProviderStub _stub;
15 15
16 LocalServiceProvider(this.connection, this._stub) { 16 LocalServiceProvider(this.connection, this._stub) {
17 _stub.impl = this; 17 _stub.impl = this;
18 assert(_stub.isOpen);
18 } 19 }
19 20
20 listen({Function onClosed}) => _stub.listen(onClosed: _closer(onClosed)); 21 set onError(Function f) {
21 22 _stub.onError = f;
22 Function _closer(Function onClosed) {
23 return (() {
24 if (onClosed != null) {
25 onClosed();
26 }
27 close();
28 });
29 } 23 }
30 24
31 void close({bool nodefer: false}) => _stub.close(nodefer: nodefer); 25 void close({bool nodefer : false}) => _stub.close(nodefer: nodefer);
32 26
33 void connectToService(String interfaceName, 27 void connectToService(String interfaceName,
34 core.MojoMessagePipeEndpoint pipe) { 28 core.MojoMessagePipeEndpoint pipe) {
35 if (connection._nameToServiceFactory.containsKey(interfaceName)) { 29 if (connection._nameToServiceFactory.containsKey(interfaceName)) {
36 connection._nameToServiceFactory[interfaceName](pipe); 30 connection._nameToServiceFactory[interfaceName](pipe);
37 return; 31 return;
38 } 32 }
39 if (connection.fallbackServiceFactory != null) { 33 if (connection.fallbackServiceFactory != null) {
40 connection.fallbackServiceFactory(interfaceName, pipe); 34 connection.fallbackServiceFactory(interfaceName, pipe);
41 return; 35 return;
(...skipping 24 matching lines...) Expand all
66 // 60 //
67 // The fallbackServiceFactory is only used if a service wasn't specified 61 // The fallbackServiceFactory is only used if a service wasn't specified
68 // with provideService(). 62 // with provideService().
69 63
70 class ApplicationConnection { 64 class ApplicationConnection {
71 ServiceProviderProxy remoteServiceProvider; 65 ServiceProviderProxy remoteServiceProvider;
72 LocalServiceProvider _localServiceProvider; 66 LocalServiceProvider _localServiceProvider;
73 final _nameToServiceFactory = new Map<String, ServiceFactory>(); 67 final _nameToServiceFactory = new Map<String, ServiceFactory>();
74 FallbackServiceFactory _fallbackServiceFactory; 68 FallbackServiceFactory _fallbackServiceFactory;
75 69
76 ApplicationConnection(ServiceProviderStub stub, ServiceProviderProxy proxy) 70 ApplicationConnection(ServiceProviderStub stub, this.remoteServiceProvider) {
77 : remoteServiceProvider = proxy { 71 if (stub != null) {
78 if (stub != null) _localServiceProvider = 72 _localServiceProvider = new LocalServiceProvider(this, stub);
79 new LocalServiceProvider(this, stub); 73 _localServiceProvider.onError = close;
74 }
80 } 75 }
81 76
82 FallbackServiceFactory get fallbackServiceFactory => _fallbackServiceFactory; 77 FallbackServiceFactory get fallbackServiceFactory => _fallbackServiceFactory;
83 set fallbackServiceFactory(FallbackServiceFactory f) { 78 set fallbackServiceFactory(FallbackServiceFactory f) {
84 assert(_localServiceProvider != null); 79 assert(_localServiceProvider != null);
85 _fallbackServiceFactory = f; 80 _fallbackServiceFactory = f;
86 } 81 }
87 82
88 bindings.ProxyBase requestService(bindings.ProxyBase proxy) { 83 bindings.ProxyBase requestService(bindings.ProxyBase proxy) {
89 assert(!proxy.impl.isBound && 84 assert(!proxy.impl.isBound &&
90 (remoteServiceProvider != null) && 85 (remoteServiceProvider != null) &&
91 remoteServiceProvider.impl.isBound); 86 remoteServiceProvider.impl.isBound);
92 var pipe = new core.MojoMessagePipe(); 87 var pipe = new core.MojoMessagePipe();
93 proxy.impl.bind(pipe.endpoints[0]); 88 proxy.impl.bind(pipe.endpoints[0]);
94 remoteServiceProvider.ptr.connectToService(proxy.name, pipe.endpoints[1]); 89 remoteServiceProvider.ptr.connectToService(proxy.name, pipe.endpoints[1]);
95 return proxy; 90 return proxy;
96 } 91 }
97 92
98 void provideService(String interfaceName, ServiceFactory factory) { 93 void provideService(String interfaceName, ServiceFactory factory) {
99 assert(_localServiceProvider != null); 94 assert(_localServiceProvider != null);
100 _nameToServiceFactory[interfaceName] = factory; 95 _nameToServiceFactory[interfaceName] = factory;
101 } 96 }
102 97
103 void listen({Function onClosed}) { 98 set onError(Function f) {
104 assert(_localServiceProvider != null); 99 assert(_localServiceProvider != null);
105 _localServiceProvider.listen(onClosed: _closer(onClosed)); 100 _localServiceProvider.onError = f;
106 }
107
108 Function _closer(Function onClosed) {
109 return (() {
110 if (onClosed != null) {
111 onClosed();
112 }
113 close();
114 });
115 } 101 }
116 102
117 void close({bool nodefer: false}) { 103 void close({bool nodefer: false}) {
118 if (remoteServiceProvider != null) { 104 if (remoteServiceProvider != null) {
119 remoteServiceProvider.close(); 105 remoteServiceProvider.close();
120 remoteServiceProvider = null; 106 remoteServiceProvider = null;
121 } 107 }
122 if (_localServiceProvider != null) { 108 if (_localServiceProvider != null) {
123 _localServiceProvider.close(nodefer: nodefer); 109 _localServiceProvider.close(nodefer: nodefer);
124 _localServiceProvider = null; 110 _localServiceProvider = null;
125 } 111 }
126 112
127 } 113 }
128 } 114 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698