Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(384)

Unified Diff: services/shell/public/java/src/org/chromium/services/shell/InterfaceRegistry.java

Issue 2214323002: Add a Java InterfaceRegistry. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 4 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « services/shell/public/java/src/org/chromium/services/shell/InterfaceFactory.java ('k') | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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);
+ }
+ }
+}
« no previous file with comments | « services/shell/public/java/src/org/chromium/services/shell/InterfaceFactory.java ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698