| 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 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.delegate = this; | 17 _stub.impl = this; |
| 18 } | 18 } |
| 19 | 19 |
| 20 listen() => _stub.listen(); | 20 listen({Function onClosed}) => _stub.listen(onClosed: _closer(onClosed)); |
| 21 | 21 |
| 22 void close({bool nodefer : false}) => _stub.close(nodefer: nodefer); | 22 Function _closer(Function onClosed) { |
| 23 return (() { |
| 24 if (onClosed != null) { |
| 25 onClosed(); |
| 26 } |
| 27 close(); |
| 28 }); |
| 29 } |
| 30 |
| 31 void close({bool nodefer: false}) => _stub.close(nodefer: nodefer); |
| 23 | 32 |
| 24 void connectToService(String interfaceName, | 33 void connectToService(String interfaceName, |
| 25 core.MojoMessagePipeEndpoint pipe) { | 34 core.MojoMessagePipeEndpoint pipe) { |
| 26 if (connection._nameToServiceFactory.containsKey(interfaceName)) { | 35 if (connection._nameToServiceFactory.containsKey(interfaceName)) { |
| 27 connection._nameToServiceFactory[interfaceName](pipe); | 36 connection._nameToServiceFactory[interfaceName](pipe); |
| 28 return; | 37 return; |
| 29 } | 38 } |
| 30 if (connection.fallbackServiceFactory != null) { | 39 if (connection.fallbackServiceFactory != null) { |
| 31 connection.fallbackServiceFactory(interfaceName, pipe); | 40 connection.fallbackServiceFactory(interfaceName, pipe); |
| 32 return; | 41 return; |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 64 final _nameToServiceFactory = new Map<String, ServiceFactory>(); | 73 final _nameToServiceFactory = new Map<String, ServiceFactory>(); |
| 65 FallbackServiceFactory _fallbackServiceFactory; | 74 FallbackServiceFactory _fallbackServiceFactory; |
| 66 | 75 |
| 67 ApplicationConnection(ServiceProviderStub stub, ServiceProviderProxy proxy) | 76 ApplicationConnection(ServiceProviderStub stub, ServiceProviderProxy proxy) |
| 68 : remoteServiceProvider = proxy { | 77 : remoteServiceProvider = proxy { |
| 69 if (stub != null) _localServiceProvider = | 78 if (stub != null) _localServiceProvider = |
| 70 new LocalServiceProvider(this, stub); | 79 new LocalServiceProvider(this, stub); |
| 71 } | 80 } |
| 72 | 81 |
| 73 FallbackServiceFactory get fallbackServiceFactory => _fallbackServiceFactory; | 82 FallbackServiceFactory get fallbackServiceFactory => _fallbackServiceFactory; |
| 74 set fallbackServiceFactory(FallbackServiceFactory f) { | 83 set fallbackServiceFactory(FallbackServiceFactory f) { |
| 75 assert(_localServiceProvider != null); | 84 assert(_localServiceProvider != null); |
| 76 _fallbackServiceFactory = f; | 85 _fallbackServiceFactory = f; |
| 77 } | 86 } |
| 78 | 87 |
| 79 bindings.ProxyBase requestService(bindings.ProxyBase proxy) { | 88 bindings.ProxyBase requestService(bindings.ProxyBase proxy) { |
| 80 assert(!proxy.impl.isBound && | 89 assert(!proxy.impl.isBound && |
| 81 (remoteServiceProvider != null) && | 90 (remoteServiceProvider != null) && |
| 82 remoteServiceProvider.impl.isBound); | 91 remoteServiceProvider.impl.isBound); |
| 83 var pipe = new core.MojoMessagePipe(); | 92 var pipe = new core.MojoMessagePipe(); |
| 84 proxy.impl.bind(pipe.endpoints[0]); | 93 proxy.impl.bind(pipe.endpoints[0]); |
| 85 remoteServiceProvider.ptr.connectToService( | 94 remoteServiceProvider.ptr.connectToService(proxy.name, pipe.endpoints[1]); |
| 86 proxy.name, pipe.endpoints[1]); | |
| 87 return proxy; | 95 return proxy; |
| 88 } | 96 } |
| 89 | 97 |
| 90 void provideService(String interfaceName, ServiceFactory factory) { | 98 void provideService(String interfaceName, ServiceFactory factory) { |
| 91 assert(_localServiceProvider != null); | 99 assert(_localServiceProvider != null); |
| 92 _nameToServiceFactory[interfaceName] = factory; | 100 _nameToServiceFactory[interfaceName] = factory; |
| 93 } | 101 } |
| 94 | 102 |
| 95 void listen() { | 103 void listen({Function onClosed}) { |
| 96 assert(_localServiceProvider != null); | 104 assert(_localServiceProvider != null); |
| 97 _localServiceProvider.listen(); | 105 _localServiceProvider.listen(onClosed: _closer(onClosed)); |
| 106 } |
| 107 |
| 108 Function _closer(Function onClosed) { |
| 109 return (() { |
| 110 if (onClosed != null) { |
| 111 onClosed(); |
| 112 } |
| 113 close(); |
| 114 }); |
| 98 } | 115 } |
| 99 | 116 |
| 100 void close({bool nodefer: false}) { | 117 void close({bool nodefer: false}) { |
| 101 if (remoteServiceProvider != null) { | 118 if (remoteServiceProvider != null) { |
| 102 remoteServiceProvider.close(); | 119 remoteServiceProvider.close(); |
| 103 remoteServiceProvider = null; | 120 remoteServiceProvider = null; |
| 104 } | 121 } |
| 105 if (_localServiceProvider != null) { | 122 if (_localServiceProvider != null) { |
| 106 _localServiceProvider.close(nodefer: nodefer); | 123 _localServiceProvider.close(nodefer: nodefer); |
| 107 _localServiceProvider = null; | 124 _localServiceProvider = null; |
| 108 } | 125 } |
| 109 | 126 |
| 110 } | 127 } |
| 111 } | 128 } |
| OLD | NEW |