1 /*
2 * Copyright 2012, 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 // The functions in this file are used to configure the mock Geolocation client
27 // for the LayoutTests.
28
29 #include "config.h"
30
31 #include "WebViewCore.h"
32
33 #include <GeolocationError.h>
34 #include <GeolocationPosition.h>
35 #include "JNIHelp.h"
36 #include "ScopedLocalRef.h"
37 #include <wtf/CurrentTime.h>
38
39 using namespace WebCore;
40
41 namespace android {
42
43 static const char* javaMockGeolocationClass = "android/webkit/MockGeolocation";
44
getWebViewCore(JNIEnv * env,jobject webViewCore)45 WebViewCore* getWebViewCore(JNIEnv* env, jobject webViewCore)
46 {
47 ScopedLocalRef<jclass> webViewCoreClass(env, env->FindClass("android/webkit/WebViewCore"));
48 jfieldID nativeClassField = env->GetFieldID(webViewCoreClass.get(), "mNativeClass", "I");
49 return reinterpret_cast<WebViewCore*>(env->GetIntField(webViewCore, nativeClassField));
50 }
51
setUseMock(JNIEnv * env,jobject,jobject webViewCore)52 static void setUseMock(JNIEnv* env, jobject, jobject webViewCore)
53 {
54 getWebViewCore(env, webViewCore)->geolocationManager()->setUseMock();
55 }
56
setPosition(JNIEnv * env,jobject,jobject webViewCore,double latitude,double longitude,double accuracy)57 static void setPosition(JNIEnv* env, jobject, jobject webViewCore, double latitude, double longitude, double accuracy)
58 {
59 getWebViewCore(env, webViewCore)->geolocationManager()->setMockPosition(GeolocationPosition::create(WTF::currentTime(),
60 latitude,
61 longitude,
62 accuracy,
63 false, 0.0, // altitude,
64 false, 0.0, // altitudeAccuracy,
65 false, 0.0, // heading
66 false, 0.0)); // speed
67 }
68
setError(JNIEnv * env,jobject,jobject webViewCore,int code,jstring message)69 static void setError(JNIEnv* env, jobject, jobject webViewCore, int code, jstring message)
70 {
71 GeolocationError::ErrorCode codeEnum = static_cast<GeolocationError::ErrorCode>(code);
72 getWebViewCore(env, webViewCore)->geolocationManager()->setMockError(GeolocationError::create(codeEnum, jstringToWtfString(env, message)));
73 }
74
setPermission(JNIEnv * env,jobject,jobject webViewCore,bool allow)75 static void setPermission(JNIEnv* env, jobject, jobject webViewCore, bool allow)
76 {
77 getWebViewCore(env, webViewCore)->geolocationManager()->setMockPermission(allow);
78 }
79
80 static JNINativeMethod gMockGeolocationMethods[] = {
81 { "nativeSetUseMock", "(Landroid/webkit/WebViewCore;)V", (void*) setUseMock },
82 { "nativeSetPosition", "(Landroid/webkit/WebViewCore;DDD)V", (void*) setPosition },
83 { "nativeSetError", "(Landroid/webkit/WebViewCore;ILjava/lang/String;)V", (void*) setError },
84 { "nativeSetPermission", "(Landroid/webkit/WebViewCore;Z)V", (void*) setPermission },
85 };
86
registerMockGeolocation(JNIEnv * env)87 int registerMockGeolocation(JNIEnv* env)
88 {
89 #ifndef NDEBUG
90 jclass mockGeolocation = env->FindClass(javaMockGeolocationClass);
91 ALOG_ASSERT(mockGeolocation, "Unable to find class");
92 env->DeleteLocalRef(mockGeolocation);
93 #endif
94
95 return jniRegisterNativeMethods(env, javaMockGeolocationClass, gMockGeolocationMethods, NELEM(gMockGeolocationMethods));
96 }
97
98 }
99