| OLD | NEW |
| (Empty) |
| 1 // Copyright 2016 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 package org.chromium.services.shell; | |
| 6 | |
| 7 import org.chromium.mojo.bindings.Interface; | |
| 8 import org.chromium.mojo.system.MessagePipeHandle; | |
| 9 import org.chromium.mojo.system.MojoException; | |
| 10 import org.chromium.shell.mojom.InterfaceProvider; | |
| 11 | |
| 12 import java.util.HashMap; | |
| 13 import java.util.Map; | |
| 14 | |
| 15 /** | |
| 16 * A registry where interfaces may be registered to be exposed to another applic
ation. | |
| 17 * | |
| 18 * To use, define a class that implements your specific interface. Then | |
| 19 * implement an InterfaceFactory that creates instances of your implementation | |
| 20 * and register that on the registry with a Manager for the interface like this: | |
| 21 * | |
| 22 * registry.addInterface(InterfaceType.MANAGER, factory); | |
| 23 */ | |
| 24 public class InterfaceRegistry implements InterfaceProvider { | |
| 25 private final Map<String, InterfaceBinder> mBinders = new HashMap<String, In
terfaceBinder>(); | |
| 26 | |
| 27 public <I extends Interface> void addInterface( | |
| 28 Interface.Manager<I, ? extends I.Proxy> manager, InterfaceFactory<I>
factory) { | |
| 29 mBinders.put(manager.getName(), new InterfaceBinder<I>(manager, factory)
); | |
| 30 } | |
| 31 | |
| 32 public static InterfaceRegistry create(MessagePipeHandle pipe) { | |
| 33 InterfaceRegistry registry = new InterfaceRegistry(); | |
| 34 InterfaceProvider.MANAGER.bind(registry, pipe); | |
| 35 return registry; | |
| 36 } | |
| 37 | |
| 38 @Override | |
| 39 public void getInterface(String name, MessagePipeHandle pipe) { | |
| 40 InterfaceBinder binder = mBinders.get(name); | |
| 41 if (binder == null) { | |
| 42 return; | |
| 43 } | |
| 44 binder.bindToMessagePipe(pipe); | |
| 45 } | |
| 46 | |
| 47 @Override | |
| 48 public void close() { | |
| 49 mBinders.clear(); | |
| 50 } | |
| 51 | |
| 52 @Override | |
| 53 public void onConnectionError(MojoException e) { | |
| 54 close(); | |
| 55 } | |
| 56 | |
| 57 InterfaceRegistry() {} | |
| 58 | |
| 59 private static class InterfaceBinder<I extends Interface> { | |
| 60 private Interface.Manager<I, ? extends I.Proxy> mManager; | |
| 61 private InterfaceFactory<I> mFactory; | |
| 62 | |
| 63 public InterfaceBinder( | |
| 64 Interface.Manager<I, ? extends I.Proxy> manager, InterfaceFactor
y<I> factory) { | |
| 65 mManager = manager; | |
| 66 mFactory = factory; | |
| 67 } | |
| 68 | |
| 69 public void bindToMessagePipe(MessagePipeHandle pipe) { | |
| 70 I impl = mFactory.createImpl(); | |
| 71 if (impl == null) { | |
| 72 pipe.close(); | |
| 73 return; | |
| 74 } | |
| 75 mManager.bind(impl, pipe); | |
| 76 } | |
| 77 } | |
| 78 } | |
| OLD | NEW |