1/*
2 * Copyright 2007, The Android Open Source Project
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 *  * Redistributions of source code must retain the above copyright
8 *    notice, this list of conditions and the following disclaimer.
9 *  * Redistributions in binary form must reproduce the above copyright
10 *    notice, this list of conditions and the following disclaimer in the
11 *    documentation and/or other materials provided with the distribution.
12 *
13 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ``AS IS'' AND ANY
14 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR
17 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
18 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
20 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
21 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24 */
25
26#define LOG_TAG "webcoreglue"
27
28#include "config.h"
29#include "WebCoreJni.h"
30
31#include "NotImplemented.h"
32#include <JNIUtility.h>
33#include <jni.h>
34#include <utils/Log.h>
35
36namespace android {
37
38AutoJObject getRealObject(JNIEnv* env, jobject obj)
39{
40    jobject real = env->NewLocalRef(obj);
41    LOG_ASSERT(real, "The real object has been deleted!");
42    return AutoJObject(env, real);
43}
44
45/**
46 * Helper method for checking java exceptions
47 * @return true if an exception occurred.
48 */
49bool checkException(JNIEnv* env)
50{
51    if (env->ExceptionCheck() != 0)
52    {
53        LOGE("*** Uncaught exception returned from Java call!\n");
54        env->ExceptionDescribe();
55        return true;
56    }
57    return false;
58}
59
60// This method is safe to call from the ui thread and the WebCore thread.
61WebCore::String to_string(JNIEnv* env, jstring str)
62{
63    if (!str || !env)
64        return WebCore::String();
65    const jchar* s = env->GetStringChars(str, NULL);
66    if (!s)
67        return WebCore::String();
68    WebCore::String ret(s, env->GetStringLength(str));
69    env->ReleaseStringChars(str, s);
70    checkException(env);
71    return ret;
72}
73
74}
75