| Index: services/shell/public/java/src/org/chromium/services/shell/InterfaceRegistry.java
|
| diff --git a/services/shell/public/java/src/org/chromium/services/shell/InterfaceRegistry.java b/services/shell/public/java/src/org/chromium/services/shell/InterfaceRegistry.java
|
| new file mode 100644
|
| index 0000000000000000000000000000000000000000..90b7d904866a22e81d8ca4fb23df275d177e40ce
|
| --- /dev/null
|
| +++ b/services/shell/public/java/src/org/chromium/services/shell/InterfaceRegistry.java
|
| @@ -0,0 +1,73 @@
|
| +// Copyright 2016 The Chromium Authors. All rights reserved.
|
| +// Use of this source code is governed by a BSD-style license that can be
|
| +// found in the LICENSE file.
|
| +
|
| +package org.chromium.services.shell;
|
| +
|
| +import org.chromium.mojo.bindings.Interface;
|
| +import org.chromium.mojo.system.MessagePipeHandle;
|
| +import org.chromium.mojo.system.MojoException;
|
| +import org.chromium.mojom.shell.mojom.InterfaceProvider;
|
| +
|
| +import java.util.HashMap;
|
| +import java.util.Map;
|
| +
|
| +/**
|
| + * A registry where interfaces may be registered to be exposed to another application.
|
| + *
|
| + * To use, define a class that implements your specific interface. Then
|
| + * implement an InterfaceFactory that creates instances of your implementation
|
| + * and register that on the registry with a Manager for the interface like this:
|
| + *
|
| + * registry.addInterface(factory, InterfaceType.MANAGER);
|
| + */
|
| +public class InterfaceRegistry implements InterfaceProvider {
|
| + private final Map<String, InterfaceBinder> mBinders = new HashMap<String, InterfaceBinder>();
|
| +
|
| + public <I extends Interface> void addInterface(
|
| + Interface.Manager<I, ? extends I.Proxy> manager, InterfaceFactory<I> factory) {
|
| + mBinders.put(manager.getName(), new InterfaceBinder<I>(manager, factory));
|
| + }
|
| +
|
| + public static InterfaceRegistry create(MessagePipeHandle pipe) {
|
| + InterfaceRegistry registry = new InterfaceRegistry();
|
| + InterfaceProvider.MANAGER.bind(registry, pipe);
|
| + return registry;
|
| + }
|
| +
|
| + @Override
|
| + public void getInterface(String name, MessagePipeHandle pipe) {
|
| + InterfaceBinder binder = mBinders.get(name);
|
| + if (binder == null) {
|
| + return;
|
| + }
|
| + binder.bindToMessagePipe(pipe);
|
| + }
|
| +
|
| + @Override
|
| + public void close() {
|
| + mBinders.clear();
|
| + }
|
| +
|
| + @Override
|
| + public void onConnectionError(MojoException e) {
|
| + close();
|
| + }
|
| +
|
| + InterfaceRegistry() {}
|
| +
|
| + private static class InterfaceBinder<I extends Interface> {
|
| + private Interface.Manager<I, ? extends I.Proxy> mManager;
|
| + private InterfaceFactory<I> mFactory;
|
| +
|
| + public InterfaceBinder(
|
| + Interface.Manager<I, ? extends I.Proxy> manager, InterfaceFactory<I> factory) {
|
| + mManager = manager;
|
| + mFactory = factory;
|
| + }
|
| +
|
| + public void bindToMessagePipe(MessagePipeHandle pipe) {
|
| + mManager.bind(mFactory.createImpl(), pipe);
|
| + }
|
| + }
|
| +}
|
|
|