The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame^] | 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 | |
| 17 | package android.webkit; |
| 18 | |
| 19 | |
| 20 | public 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 | // This is the caller of the prompt and is the object that is waiting. |
| 30 | protected final CallbackProxy mProxy; |
| 31 | // This is the default value of the result. |
| 32 | private final boolean mDefaultValue; |
| 33 | |
| 34 | /** |
| 35 | * Handle the result if the user cancelled the dialog. |
| 36 | */ |
| 37 | public final void cancel() { |
| 38 | mResult = false; |
| 39 | wakeUp(); |
| 40 | } |
| 41 | |
| 42 | /** |
| 43 | * Handle a confirmation response from the user. |
| 44 | */ |
| 45 | public final void confirm() { |
| 46 | mResult = true; |
| 47 | wakeUp(); |
| 48 | } |
| 49 | |
| 50 | /*package*/ JsResult(CallbackProxy proxy, boolean defaultVal) { |
| 51 | mProxy = proxy; |
| 52 | mDefaultValue = defaultVal; |
| 53 | } |
| 54 | |
| 55 | /*package*/ final boolean getResult() { |
| 56 | return mResult; |
| 57 | } |
| 58 | |
| 59 | /*package*/ final void setReady() { |
| 60 | mReady = true; |
| 61 | if (mTriedToNotifyBeforeReady) { |
| 62 | wakeUp(); |
| 63 | } |
| 64 | } |
| 65 | |
| 66 | /*package*/ void handleDefault() { |
| 67 | setReady(); |
| 68 | mResult = mDefaultValue; |
| 69 | wakeUp(); |
| 70 | } |
| 71 | |
| 72 | /* Wake up the WebCore thread. */ |
| 73 | protected final void wakeUp() { |
| 74 | if (mReady) { |
| 75 | synchronized (mProxy) { |
| 76 | mProxy.notify(); |
| 77 | } |
| 78 | } else { |
| 79 | mTriedToNotifyBeforeReady = true; |
| 80 | } |
| 81 | } |
| 82 | } |