| 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 InterfaceFactory(core.MojoMessagePipeEndpoint endpoint); | |
| 8 typedef void FallbackInterfaceFactory( | |
| 9 String interfaceName, core.MojoMessagePipeEndpoint endpoint); | |
| 10 | |
| 11 // ServiceProvider implementation used to provide services to a remote | |
| 12 // application. Register a factory for service creation using either of the | |
| 13 // following: | |
| 14 // 1. When you know the interface use registerFactory(), e.g. | |
| 15 // serviceProvider.registerFactory(ViewManagerClient.name, (pipe) => | |
| 16 // new ViewManagerClientImpl(pipe)); | |
| 17 // 2. To handle requests for any interface set the FallbackFactory. The | |
| 18 // FallbackFactory is passed the name of the requested interface. | |
| 19 // | |
| 20 // If a factory has been registered based on the name, it is used. If the | |
| 21 // factory returned null or there is no registered factory then the | |
| 22 // fallbackFactory is used. The fallbackFactory does not return a type; the | |
| 23 // factory takes ownership and if it does not want to use the pipe it must | |
| 24 // call close(). | |
| 25 class ServiceProvider extends service_provider.ServiceProvider { | |
| 26 FallbackInterfaceFactory fallbackFactory; | |
| 27 | |
| 28 service_provider.ServiceProviderProxy _proxy; | |
| 29 | |
| 30 Map<String, InterfaceFactory> _interfaceFactories; | |
| 31 | |
| 32 ServiceProvider( | |
| 33 service_provider.ServiceProviderStub services, | |
| 34 [service_provider.ServiceProviderProxy exposedServices = null]) | |
| 35 : _proxy = exposedServices, | |
| 36 _interfaceFactories = new Map(), | |
| 37 super.fromStub(services) { | |
| 38 delegate = this; | |
| 39 } | |
| 40 | |
| 41 connectToService(String interfaceName, core.MojoMessagePipeEndpoint pipe) { | |
| 42 if (_interfaceFactories.containsKey(interfaceName)) { | |
| 43 var listener = _interfaceFactories[interfaceName](pipe); | |
| 44 if (listener != null) { | |
| 45 listener.listen(); | |
| 46 return; | |
| 47 } | |
| 48 } | |
| 49 if (fallbackFactory != null) { | |
| 50 fallbackFactory(interfaceName, pipe); | |
| 51 return; | |
| 52 } | |
| 53 // If we get here the interface isn't known. This is legal. Close the pipe | |
| 54 // so the remote side sees we don't support this interface. | |
| 55 pipe.handle.close(); | |
| 56 } | |
| 57 | |
| 58 requestService(String name, bindings.Proxy clientImpl) { | |
| 59 assert(_proxy != null); | |
| 60 assert(!clientImpl.isBound); | |
| 61 var pipe = new core.MojoMessagePipe(); | |
| 62 clientImpl.bind(pipe.endpoints[0]); | |
| 63 _proxy.connectToService(name, pipe.endpoints[1]); | |
| 64 } | |
| 65 | |
| 66 registerFactory(String interfaceName, InterfaceFactory factory) { | |
| 67 _interfaceFactories[interfaceName] = factory; | |
| 68 } | |
| 69 | |
| 70 close({bool nodefer : false}) { | |
| 71 if (_proxy != null) { | |
| 72 _proxy.close(); | |
| 73 _proxy = null; | |
| 74 } | |
| 75 super.close(nodefer: nodefer); | |
| 76 } | |
| 77 } | |
| OLD | NEW |