summaryrefslogtreecommitdiff
path: root/mojo/public/java/bindings/src/org/chromium/mojo/bindings/ExceptionHandler.java
blob: 8961d22d3ee4accc9914bf0f14883e0d70254623 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
// Copyright 2018 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;

/**
 * An {@link ExceptionHandler} is notified of any {@link RuntimeException} happening in the
 * bindings or any of the callbacks.
 */
public interface ExceptionHandler {
    /**
     * Receives a notification that an unhandled {@link RuntimeException} has been thrown in an
     * {@link Interface} implementation or one of the {@link Callbacks} internal classes.
     *
     * Normal implementations should either throw the exception or return whether the connection
     * should be kept alive or terminated.
     */
    public boolean handleException(RuntimeException e);

    /**
     * The default ExceptionHandler, which simply throws the exception upon receiving it. It can
     * also delegate the handling of the exceptions to another instance of ExceptionHandler.
     */
    public static class DefaultExceptionHandler implements ExceptionHandler {
        private ExceptionHandler mDelegate;

        @Override
        public boolean handleException(RuntimeException e) {
            if (mDelegate != null) {
                return mDelegate.handleException(e);
            }
            throw e;
        }

        private DefaultExceptionHandler() {}

        /**
         * Static class that implements the initialization-on-demand holder idiom.
         */
        private static class LazyHolder {
            static final DefaultExceptionHandler INSTANCE = new DefaultExceptionHandler();
        }

        /**
         * Gets the singleton instance for the DefaultExceptionHandler.
         */
        public static DefaultExceptionHandler getInstance() {
            return LazyHolder.INSTANCE;
        }

        /**
         * Sets a delegate ExceptionHandler, in case throwing an exception is not desirable.
         */
        public void setDelegate(ExceptionHandler exceptionHandler) {
            mDelegate = exceptionHandler;
        }
    }
}