summaryrefslogtreecommitdiffstats
path: root/widget/android/jni/GeckoResultUtils.h
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-07 19:33:14 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-07 19:33:14 +0000
commit36d22d82aa202bb199967e9512281e9a53db42c9 (patch)
tree105e8c98ddea1c1e4784a60a5a6410fa416be2de /widget/android/jni/GeckoResultUtils.h
parentInitial commit. (diff)
downloadfirefox-esr-36d22d82aa202bb199967e9512281e9a53db42c9.tar.xz
firefox-esr-36d22d82aa202bb199967e9512281e9a53db42c9.zip
Adding upstream version 115.7.0esr.upstream/115.7.0esr
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'widget/android/jni/GeckoResultUtils.h')
-rw-r--r--widget/android/jni/GeckoResultUtils.h54
1 files changed, 54 insertions, 0 deletions
diff --git a/widget/android/jni/GeckoResultUtils.h b/widget/android/jni/GeckoResultUtils.h
new file mode 100644
index 0000000000..da103f35ee
--- /dev/null
+++ b/widget/android/jni/GeckoResultUtils.h
@@ -0,0 +1,54 @@
+/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
+/* vim: set ts=8 sts=2 et sw=2 tw=80: */
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+
+#ifndef mozilla_jni_GeckoResultUtils_h
+#define mozilla_jni_GeckoResultUtils_h
+
+#include "mozilla/java/GeckoResultNatives.h"
+#include "mozilla/jni/Conversions.h"
+
+namespace mozilla {
+namespace jni {
+
+// C++-side object bound to Java's GeckoResult.GeckoCallback.
+//
+// Note that we can't template this class because that breaks JNI dispatch
+// (surprisingly: it compiles, but selects the wrong method specialization
+// during dispatch). So instead we use a templated factory function, which
+// bundles the per-ArgType conversion logic into the callback.
+class GeckoResultCallback final
+ : public java::GeckoResult::GeckoCallback::Natives<GeckoResultCallback> {
+ public:
+ typedef java::GeckoResult::GeckoCallback::Natives<GeckoResultCallback> Base;
+ typedef std::function<void(mozilla::jni::Object::Param)> OuterCallback;
+
+ void Call(mozilla::jni::Object::Param aArg) { mCallback(aArg); }
+
+ template <typename ArgType>
+ static java::GeckoResult::GeckoCallback::LocalRef CreateAndAttach(
+ std::function<void(ArgType)>&& aInnerCallback) {
+ auto java = java::GeckoResult::GeckoCallback::New();
+ OuterCallback outerCallback =
+ [inner{std::move(aInnerCallback)}](mozilla::jni::Object::Param aParam) {
+ ArgType converted = Java2Native<ArgType>(aParam);
+ inner(std::move(converted));
+ };
+ auto native = MakeUnique<GeckoResultCallback>(std::move(outerCallback));
+ Base::AttachNative(java, std::move(native));
+ return java;
+ }
+
+ explicit GeckoResultCallback(OuterCallback&& aCallback)
+ : mCallback(std::move(aCallback)) {}
+
+ private:
+ OuterCallback mCallback;
+};
+
+} // namespace jni
+} // namespace mozilla
+
+#endif // mozilla_jni_GeckoResultUtils_h