Bug 1793629 - Implement attention indicator for the unified extensions button, r...
[gecko.git] / widget / android / jni / Conversions.cpp
blobff4517daf2ed3d99eb8d118900d9458f391dcf10
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
7 #include "Conversions.h"
8 #include "JavaBuiltins.h"
10 #include "mozilla/ipc/GeckoChildProcessHost.h"
12 namespace mozilla {
13 namespace jni {
15 template <class T>
16 jfieldID GetValueFieldID(JNIEnv* aEnv, const char* aType) {
17 const jfieldID id = aEnv->GetFieldID(
18 typename T::Context(aEnv, nullptr).ClassRef(), "value", aType);
19 aEnv->ExceptionClear();
20 return id;
23 // Cached locations of the primitive types within their standard boxed objects
24 // to skip doing that lookup on every get.
25 static jfieldID gBooleanValueField;
26 static jfieldID gIntValueField;
27 static jfieldID gDoubleValueField;
29 void InitConversionStatics() {
30 MOZ_ASSERT(NS_IsMainThread());
31 JNIEnv* const env = jni::GetGeckoThreadEnv();
32 gBooleanValueField = GetValueFieldID<java::sdk::Boolean>(env, "Z");
33 gIntValueField = GetValueFieldID<java::sdk::Integer>(env, "I");
34 gDoubleValueField = GetValueFieldID<java::sdk::Double>(env, "D");
37 template <>
38 bool Java2Native(mozilla::jni::Object::Param aData, JNIEnv* aEnv) {
39 MOZ_ASSERT(aData.IsInstanceOf<jni::Boolean>());
41 bool result = false;
42 if (gBooleanValueField) {
43 if (!aEnv) {
44 aEnv = jni::GetEnvForThread();
46 result =
47 aEnv->GetBooleanField(aData.Get(), gBooleanValueField) != JNI_FALSE;
48 MOZ_CATCH_JNI_EXCEPTION(aEnv);
49 } else {
50 result = java::sdk::Boolean::Ref::From(aData)->BooleanValue();
53 return result;
56 template <>
57 int Java2Native(mozilla::jni::Object::Param aData, JNIEnv* aEnv) {
58 MOZ_ASSERT(aData.IsInstanceOf<jni::Integer>());
60 int result = 0;
61 if (gIntValueField) {
62 if (!aEnv) {
63 aEnv = jni::GetEnvForThread();
65 result = aEnv->GetIntField(aData.Get(), gIntValueField);
66 MOZ_CATCH_JNI_EXCEPTION(aEnv);
67 } else {
68 result = java::sdk::Number::Ref::From(aData)->IntValue();
71 return result;
74 template <>
75 double Java2Native(mozilla::jni::Object::Param aData, JNIEnv* aEnv) {
76 MOZ_ASSERT(aData.IsInstanceOf<jni::Double>());
78 double result = 0;
79 if (gDoubleValueField) {
80 if (!aEnv) {
81 aEnv = jni::GetEnvForThread();
83 result = aEnv->GetDoubleField(aData.Get(), gDoubleValueField);
84 MOZ_CATCH_JNI_EXCEPTION(aEnv);
85 } else {
86 result = java::sdk::Number::Ref::From(aData)->DoubleValue();
89 return result;
92 template <>
93 ipc::LaunchError Java2Native(mozilla::jni::Object::Param aData, JNIEnv* aEnv) {
94 return ipc::LaunchError{};
97 template <>
98 nsString Java2Native(mozilla::jni::Object::Param aData, JNIEnv* aEnv) {
99 nsString result;
100 if (aData != NULL && aData.IsInstanceOf<jni::String>()) {
101 result = jni::String::Ref::From(aData)->ToString();
103 return result;
106 template <>
107 nsresult Java2Native(mozilla::jni::Object::Param aData, JNIEnv* aEnv) {
108 MOZ_ASSERT(aData.IsInstanceOf<jni::Throwable>());
109 return NS_ERROR_FAILURE;
112 } // namespace jni
113 } // namespace mozilla