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.
5 package org.chromium.mojo.bindings;
7 import org.chromium.mojo.system.MojoException;
9 import java.util.Collections;
11 import java.util.WeakHashMap;
14 * A {@link ConnectionErrorHandler} that delegate the errors to a list of registered handlers. This
15 * class will use weak pointers to prevent keeping references to any handlers it delegates to.
17 public class DelegatingConnectionErrorHandler implements ConnectionErrorHandler {
20 * The registered handlers. This uses a {@link WeakHashMap} so that it doesn't prevent the
21 * handler from being garbage collected.
23 private final Set<ConnectionErrorHandler> mHandlers =
24 Collections.newSetFromMap(new WeakHashMap<ConnectionErrorHandler, Boolean>());
27 * @see ConnectionErrorHandler#onConnectionError(MojoException)
30 public void onConnectionError(MojoException e) {
31 for (ConnectionErrorHandler handler : mHandlers) {
32 handler.onConnectionError(e);
37 * Add a handler that will be notified of any error this object receives.
39 public void addConnectionErrorHandler(ConnectionErrorHandler handler) {
40 mHandlers.add(handler);
44 * Remove a previously registered handler.
46 public void removeConnectionErrorHandler(ConnectionErrorHandler handler) {
47 mHandlers.remove(handler);