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