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