| 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 assert(_stub.isOpen); |
| 17 _stub.impl = this; | 18 _stub.impl = this; |
| 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 23 matching lines...) Expand all Loading... |
| 65 // fallbackServiceFactory function doesn't bind the pipe, it should close it. | 59 // fallbackServiceFactory function doesn't bind the pipe, it should close it. |
| 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; |
| 69 core.ErrorHandler onError; |
| 75 | 70 |
| 76 ApplicationConnection(ServiceProviderStub stub, ServiceProviderProxy proxy) | 71 ApplicationConnection(ServiceProviderStub stub, this.remoteServiceProvider) { |
| 77 : remoteServiceProvider = proxy { | 72 if (stub != null) { |
| 78 if (stub != null) _localServiceProvider = | 73 _localServiceProvider = new LocalServiceProvider(this, stub); |
| 79 new LocalServiceProvider(this, stub); | 74 _localServiceProvider.onError = _errorHandler; |
| 75 } |
| 80 } | 76 } |
| 81 | 77 |
| 82 FallbackServiceFactory get fallbackServiceFactory => _fallbackServiceFactory; | 78 FallbackServiceFactory get fallbackServiceFactory => _fallbackServiceFactory; |
| 83 set fallbackServiceFactory(FallbackServiceFactory f) { | 79 set fallbackServiceFactory(FallbackServiceFactory f) { |
| 84 assert(_localServiceProvider != null); | 80 assert(_localServiceProvider != null); |
| 85 _fallbackServiceFactory = f; | 81 _fallbackServiceFactory = f; |
| 86 } | 82 } |
| 87 | 83 |
| 88 bindings.ProxyBase requestService(bindings.ProxyBase proxy) { | 84 bindings.ProxyBase requestService(bindings.ProxyBase proxy) { |
| 89 assert(!proxy.impl.isBound && | 85 assert(!proxy.impl.isBound && |
| 90 (remoteServiceProvider != null) && | 86 (remoteServiceProvider != null) && |
| 91 remoteServiceProvider.impl.isBound); | 87 remoteServiceProvider.impl.isBound); |
| 92 var pipe = new core.MojoMessagePipe(); | 88 var pipe = new core.MojoMessagePipe(); |
| 93 proxy.impl.bind(pipe.endpoints[0]); | 89 proxy.impl.bind(pipe.endpoints[0]); |
| 94 remoteServiceProvider.ptr.connectToService(proxy.name, pipe.endpoints[1]); | 90 remoteServiceProvider.ptr.connectToService(proxy.name, pipe.endpoints[1]); |
| 95 return proxy; | 91 return proxy; |
| 96 } | 92 } |
| 97 | 93 |
| 98 void provideService(String interfaceName, ServiceFactory factory) { | 94 void provideService(String interfaceName, ServiceFactory factory) { |
| 99 assert(_localServiceProvider != null); | 95 assert(_localServiceProvider != null); |
| 100 _nameToServiceFactory[interfaceName] = factory; | 96 _nameToServiceFactory[interfaceName] = factory; |
| 101 } | 97 } |
| 102 | 98 |
| 103 void listen({Function onClosed}) { | 99 void _errorHandler() { |
| 104 assert(_localServiceProvider != null); | 100 if (onError != null) onError(); |
| 105 _localServiceProvider.listen(onClosed: _closer(onClosed)); | 101 close(); |
| 106 } | |
| 107 | |
| 108 Function _closer(Function onClosed) { | |
| 109 return (() { | |
| 110 if (onClosed != null) { | |
| 111 onClosed(); | |
| 112 } | |
| 113 close(); | |
| 114 }); | |
| 115 } | 102 } |
| 116 | 103 |
| 117 void close({bool nodefer: false}) { | 104 void close({bool nodefer: false}) { |
| 118 if (remoteServiceProvider != null) { | 105 if (remoteServiceProvider != null) { |
| 119 remoteServiceProvider.close(); | 106 remoteServiceProvider.close(); |
| 120 remoteServiceProvider = null; | 107 remoteServiceProvider = null; |
| 121 } | 108 } |
| 122 if (_localServiceProvider != null) { | 109 if (_localServiceProvider != null) { |
| 123 _localServiceProvider.close(nodefer: nodefer); | 110 _localServiceProvider.close(nodefer: nodefer); |
| 124 _localServiceProvider = null; | 111 _localServiceProvider = null; |
| 125 } | 112 } |
| 126 | 113 |
| 127 } | 114 } |
| 128 } | 115 } |
| OLD | NEW |