Index: mojo/bindings/java/src/org/chromium/mojo/bindings/DelegatingConnectionErrorHandler.java |
diff --git a/mojo/bindings/java/src/org/chromium/mojo/bindings/DelegatingConnectionErrorHandler.java b/mojo/bindings/java/src/org/chromium/mojo/bindings/DelegatingConnectionErrorHandler.java |
new file mode 100644 |
index 0000000000000000000000000000000000000000..094268677ea21b75ab485194c5f9a02a67efb206 |
--- /dev/null |
+++ b/mojo/bindings/java/src/org/chromium/mojo/bindings/DelegatingConnectionErrorHandler.java |
@@ -0,0 +1,49 @@ |
+// Copyright 2014 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.mojo.bindings; |
+ |
+import org.chromium.mojo.system.MojoException; |
+ |
+import java.util.Map; |
+import java.util.WeakHashMap; |
+ |
+/** |
+ * A {@link ConnectionErrorHandler} that delegate the errors to a list of registered handlers. This |
+ * class will use weak pointers to prevent keeping references to any handlers it delegates to. |
+ */ |
+public class DelegatingConnectionErrorHandler implements ConnectionErrorHandler { |
+ |
+ /** |
+ * The registered handlers. This uses a {@link WeakHashMap} so that it doesn't prevent the |
+ * handler to be garbage collected. |
+ */ |
+ private final Map<ConnectionErrorHandler, Boolean> |
+ mHandlers = new WeakHashMap<ConnectionErrorHandler, Boolean>(); |
+ |
+ /** |
+ * @see ConnectionErrorHandler#onConnectionError(MojoException) |
+ */ |
+ @Override |
+ public void onConnectionError(MojoException e) { |
+ for (ConnectionErrorHandler handler : mHandlers.keySet()) { |
+ handler.onConnectionError(e); |
+ } |
+ } |
+ |
+ /** |
+ * Add a handler that will be notified of any error this object receives. |
+ */ |
+ public void addConnectionErrorHandler(ConnectionErrorHandler handler) { |
+ mHandlers.put(handler, Boolean.TRUE); |
+ } |
+ |
+ /** |
+ * Remove a previously registered handler. |
+ */ |
+ public void removeConnectionErrorHandler(ConnectionErrorHandler handler) { |
+ mHandlers.remove(handler); |
+ } |
+ |
+} |