| OLD | NEW |
| (Empty) |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 part of application; | |
| 6 | |
| 7 typedef Object ServiceFactory(core.MojoMessagePipeEndpoint endpoint); | |
| 8 typedef void FallbackServiceFactory( | |
| 9 String interfaceName, core.MojoMessagePipeEndpoint endpoint); | |
| 10 | |
| 11 class LocalServiceProvider implements ServiceProvider { | |
| 12 final ApplicationConnection connection; | |
| 13 ServiceProviderStub _stub; | |
| 14 | |
| 15 LocalServiceProvider(this.connection, this._stub) { | |
| 16 assert(_stub.isOpen); | |
| 17 _stub.impl = this; | |
| 18 } | |
| 19 | |
| 20 set onError(Function f) { | |
| 21 _stub.onError = f; | |
| 22 } | |
| 23 | |
| 24 Future close({bool immediate: false}) => _stub.close(immediate: immediate); | |
| 25 | |
| 26 void connectToService( | |
| 27 String interfaceName, core.MojoMessagePipeEndpoint pipe) { | |
| 28 if (connection._nameToServiceFactory.containsKey(interfaceName)) { | |
| 29 connection._nameToServiceFactory[interfaceName](pipe); | |
| 30 return; | |
| 31 } | |
| 32 if (connection.fallbackServiceFactory != null) { | |
| 33 connection.fallbackServiceFactory(interfaceName, pipe); | |
| 34 return; | |
| 35 } | |
| 36 // The specified interface isn't provided. Close the pipe so the | |
| 37 // remote endpoint sees that we don't support this interface. | |
| 38 pipe.close(); | |
| 39 } | |
| 40 } | |
| 41 | |
| 42 // Encapsulates a pair of ServiceProviders that enable services (interface | |
| 43 // implementations) to be provided to a remote application as well as exposing | |
| 44 // services provided by a remote application. ApplicationConnection | |
| 45 // objects are returned by the Application ConnectToApplication() method | |
| 46 // and they're passed to the Application AcceptConnection() method. | |
| 47 // | |
| 48 // To request a service from the remote application: | |
| 49 // var proxy = applicationConnection.requestService(ViewManagerClientName); | |
| 50 // | |
| 51 // To provide a service to the remote application, specify a function that | |
| 52 // returns a service. For example: | |
| 53 // applicationConnection.provideService(ViewManagerClientName, (pipe) => | |
| 54 // new ViewManagerClientImpl(pipe)); | |
| 55 // | |
| 56 // To handle requests for any interface, set fallbackServiceFactory to a | |
| 57 // function that takes ownership of the incoming message pipe endpoint. If the | |
| 58 // fallbackServiceFactory function doesn't bind the pipe, it should close it. | |
| 59 // | |
| 60 // The fallbackServiceFactory is only used if a service wasn't specified | |
| 61 // with provideService(). | |
| 62 | |
| 63 class ApplicationConnection { | |
| 64 ServiceProviderProxy remoteServiceProvider; | |
| 65 LocalServiceProvider _localServiceProvider; | |
| 66 final _nameToServiceFactory = new Map<String, ServiceFactory>(); | |
| 67 FallbackServiceFactory _fallbackServiceFactory; | |
| 68 core.ErrorHandler onError; | |
| 69 | |
| 70 ApplicationConnection(ServiceProviderStub stub, this.remoteServiceProvider) { | |
| 71 if (stub != null) { | |
| 72 _localServiceProvider = new LocalServiceProvider(this, stub); | |
| 73 _localServiceProvider.onError = _errorHandler; | |
| 74 } | |
| 75 } | |
| 76 | |
| 77 FallbackServiceFactory get fallbackServiceFactory => _fallbackServiceFactory; | |
| 78 set fallbackServiceFactory(FallbackServiceFactory f) { | |
| 79 assert(_localServiceProvider != null); | |
| 80 _fallbackServiceFactory = f; | |
| 81 } | |
| 82 | |
| 83 bindings.ProxyBase requestService(bindings.ProxyBase proxy) { | |
| 84 assert(!proxy.impl.isBound && | |
| 85 (remoteServiceProvider != null) && | |
| 86 remoteServiceProvider.impl.isBound); | |
| 87 var pipe = new core.MojoMessagePipe(); | |
| 88 proxy.impl.bind(pipe.endpoints[0]); | |
| 89 remoteServiceProvider.ptr.connectToService(proxy.name, pipe.endpoints[1]); | |
| 90 return proxy; | |
| 91 } | |
| 92 | |
| 93 void provideService(String interfaceName, ServiceFactory factory) { | |
| 94 assert(_localServiceProvider != null); | |
| 95 _nameToServiceFactory[interfaceName] = factory; | |
| 96 } | |
| 97 | |
| 98 void _errorHandler() { | |
| 99 close().then((_) { | |
| 100 if (onError != null) onError(); | |
| 101 }); | |
| 102 } | |
| 103 | |
| 104 Future close({bool immediate: false}) { | |
| 105 var rspCloseFuture; | |
| 106 var lspCloseFuture; | |
| 107 if (remoteServiceProvider != null) { | |
| 108 rspCloseFuture = remoteServiceProvider.close(); | |
| 109 remoteServiceProvider = null; | |
| 110 } else { | |
| 111 rspCloseFuture = new Future.value(null); | |
| 112 } | |
| 113 if (_localServiceProvider != null) { | |
| 114 lspCloseFuture = _localServiceProvider.close(immediate: immediate); | |
| 115 _localServiceProvider = null; | |
| 116 } else { | |
| 117 lspCloseFuture = new Future.value(null); | |
| 118 } | |
| 119 return rspCloseFuture.then((_) => lspCloseFuture); | |
| 120 } | |
| 121 } | |
| OLD | NEW |