| 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 core.Listener ServiceFactory(core.MojoMessagePipeEndpoint endpoint); |
| 8 typedef void FallbackServiceFactory(String interfaceName, |
| 9 core.MojoMessagePipeEndpoint endpoint); |
| 10 |
| 11 |
| 12 class LocalServiceProvider extends ServiceProvider { |
| 13 final ApplicationConnection connection; |
| 14 |
| 15 LocalServiceProvider(ApplicationConnection this.connection, |
| 16 ServiceProviderStub stub) |
| 17 : super.fromStub(stub) { |
| 18 delegate = this; |
| 19 } |
| 20 |
| 21 void connectToService(String interfaceName, |
| 22 core.MojoMessagePipeEndpoint pipe) { |
| 23 if (connection._nameToServiceFactory.containsKey(interfaceName)) { |
| 24 var listener = connection._nameToServiceFactory[interfaceName](pipe); |
| 25 if (listener != null) { |
| 26 listener.listen(); |
| 27 return; |
| 28 } |
| 29 } |
| 30 if (connection.fallbackServiceFactory != null) { |
| 31 connection.fallbackServiceFactory(interfaceName, pipe); |
| 32 return; |
| 33 } |
| 34 // The specified interface isn't provided. Close the pipe so the |
| 35 // remote endpoint sees that we don't support this interface. |
| 36 pipe.handle.close(); |
| 37 } |
| 38 } |
| 39 |
| 40 // Encapsulates a pair of ServiceProviders that enable services (interface |
| 41 // implementations) to be provided to a remote application as well as exposing |
| 42 // services provided by a remote application. ApplicationConnection |
| 43 // objects are returned by the Application ConnectToApplication() method |
| 44 // and they're passed to the Application AcceptConnection() method. |
| 45 // |
| 46 // To request a service from the remote application: |
| 47 // var proxy = applicationConnection.requestService(ViewManagerClient.name); |
| 48 // |
| 49 // To provide a service to the remote application, specify a function that |
| 50 // returns a service. For example: |
| 51 // applicationConnection.provideService(ViewManagerClient.name, (pipe) => |
| 52 // new ViewManagerClientImpl(pipe)); |
| 53 // |
| 54 // To handle requests for any interface, set fallbackServiceFactory to a |
| 55 // function that takes ownership of the incoming message pipe endpoint. If the |
| 56 // fallbackServiceFactory function doesn't bind the pipe, it should close it. |
| 57 // |
| 58 // The fallbackServiceFactory is only used if a service wasn't specified |
| 59 // with provideService(). |
| 60 |
| 61 class ApplicationConnection { |
| 62 ServiceProviderProxy _remoteServiceProvider; |
| 63 LocalServiceProvider _localServiceProvider; |
| 64 final _nameToServiceFactory = new Map<String, ServiceFactory>(); |
| 65 FallbackServiceFactory fallbackServiceFactory; |
| 66 |
| 67 ApplicationConnection(ServiceProviderStub stub, ServiceProviderProxy proxy) |
| 68 : _remoteServiceProvider = proxy { |
| 69 if (stub != null) _localServiceProvider = |
| 70 new LocalServiceProvider(this, stub); |
| 71 } |
| 72 |
| 73 bindings.Proxy requestService(bindings.Proxy proxy) { |
| 74 assert(!proxy.isBound && |
| 75 (_remoteServiceProvider != null) && |
| 76 _remoteServiceProvider.isBound); |
| 77 var applicationPipe = new core.MojoMessagePipe(); |
| 78 var proxyEndpoint = applicationPipe.endpoints[0]; |
| 79 var applicationEndpoint = applicationPipe.endpoints[1]; |
| 80 proxy.bind(proxyEndpoint); |
| 81 _remoteServiceProvider.connectToService(proxy.name, applicationEndpoint); |
| 82 return proxy; |
| 83 } |
| 84 |
| 85 void provideService(String interfaceName, ServiceFactory factory) { |
| 86 assert(_localServiceProvider != null); |
| 87 _nameToServiceFactory[interfaceName] = factory; |
| 88 } |
| 89 |
| 90 void listen() { |
| 91 if (_localServiceProvider != null) _localServiceProvider.listen(); |
| 92 } |
| 93 |
| 94 void close({bool nodefer: false}) { |
| 95 if (_remoteServiceProvider != null) { |
| 96 _remoteServiceProvider.close(); |
| 97 _remoteServiceProvider = null; |
| 98 } |
| 99 if (_localServiceProvider != null) { |
| 100 _localServiceProvider.close(nodefer: nodefer); |
| 101 _localServiceProvider = null; |
| 102 } |
| 103 |
| 104 } |
| 105 } |
| OLD | NEW |