1/*
2 * Copyright (C) 2007 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package android.webkit;
18
19
20public class JsResult {
21    // This prevents a user from interacting with the result before WebCore is
22    // ready to handle it.
23    private boolean mReady;
24    // Tells us if the user tried to confirm or cancel the result before WebCore
25    // is ready.
26    private boolean mTriedToNotifyBeforeReady;
27    // This is a basic result of a confirm or prompt dialog.
28    protected boolean mResult;
29    /**
30     *  This is the caller of the prompt and is the object that is waiting.
31     *  @hide
32     */
33    protected final CallbackProxy mProxy;
34    // This is the default value of the result.
35    private final boolean mDefaultValue;
36
37    /**
38     * Handle the result if the user cancelled the dialog.
39     */
40    public final void cancel() {
41        mResult = false;
42        wakeUp();
43    }
44
45    /**
46     * Handle a confirmation response from the user.
47     */
48    public final void confirm() {
49        mResult = true;
50        wakeUp();
51    }
52
53    /*package*/ JsResult(CallbackProxy proxy, boolean defaultVal) {
54        mProxy = proxy;
55        mDefaultValue = defaultVal;
56    }
57
58    /*package*/ final boolean getResult() {
59        return mResult;
60    }
61
62    /*package*/ final void setReady() {
63        mReady = true;
64        if (mTriedToNotifyBeforeReady) {
65            wakeUp();
66        }
67    }
68
69    /*package*/ void handleDefault() {
70        setReady();
71        mResult = mDefaultValue;
72        wakeUp();
73    }
74
75    /* Wake up the WebCore thread. */
76    protected final void wakeUp() {
77        if (mReady) {
78            synchronized (mProxy) {
79                mProxy.notify();
80            }
81        } else {
82            mTriedToNotifyBeforeReady = true;
83        }
84    }
85}
86