diff --git a/widget/android/PrefsHelper.h b/widget/android/PrefsHelper.h new file mode 100644 index 00000000000..06ad8e7e3a5 --- /dev/null +++ b/widget/android/PrefsHelper.h @@ -0,0 +1,78 @@ +/* -*- Mode: c++; c-basic-offset: 4; tab-width: 20; indent-tabs-mode: nil; -*- + * 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 PrefsHelper_h +#define PrefsHelper_h + +#include "GeneratedJNINatives.h" +#include "MainThreadUtils.h" +#include "nsAppShell.h" + +#include "mozilla/UniquePtr.h" + +namespace mozilla { + +struct PrefsHelper + : widget::PrefsHelper::Natives + , UsesGeckoThreadProxy +{ + PrefsHelper() = delete; + + static void GetPrefsById(const jni::ClassObject::LocalRef& cls, + int32_t requestId, + jni::ObjectArray::Param prefNames, + bool observe) + { + MOZ_ASSERT(NS_IsMainThread()); + MOZ_ASSERT(nsAppShell::gAppShell); + + nsTArray namesRefArray(prefNames.GetElements()); + const size_t len = namesRefArray.Length(); + + // Java strings are not null-terminated, but XPCOM expects + // null-terminated strings, so we copy the strings to nsString to + // ensure null-termination. + nsTArray namesStrArray(len); + nsTArray namesPtrArray(len); + + for (size_t i = 0; i < len; i++) { + if (!namesRefArray[i]) { + namesPtrArray.AppendElement(nullptr); + continue; + } + namesPtrArray.AppendElement(namesStrArray.AppendElement( + nsString(jni::String::LocalRef( + mozilla::Move(namesRefArray[i]))))->Data()); + } + + nsIAndroidBrowserApp* browserApp = nullptr; + nsAppShell::gAppShell->GetBrowserApp(&browserApp); + MOZ_ASSERT(browserApp); + + if (observe) { + browserApp->ObservePreferences( + requestId, len ? &namesPtrArray[0] : nullptr, len); + } else { + browserApp->GetPreferences( + requestId, len ? &namesPtrArray[0] : nullptr, len); + } + } + + static void RemovePrefsObserver(int32_t requestId) + { + MOZ_ASSERT(NS_IsMainThread()); + MOZ_ASSERT(nsAppShell::gAppShell); + + nsIAndroidBrowserApp* browserApp = nullptr; + nsAppShell::gAppShell->GetBrowserApp(&browserApp); + MOZ_ASSERT(browserApp); + + browserApp->RemovePreferenceObservers(requestId); + } +}; + +} // namespace + +#endif // PrefsHelper_h diff --git a/widget/android/nsAppShell.cpp b/widget/android/nsAppShell.cpp index 0e5dfa7fcf5..8dbb9144bd5 100644 --- a/widget/android/nsAppShell.cpp +++ b/widget/android/nsAppShell.cpp @@ -60,6 +60,7 @@ #endif #include "ANRReporter.h" +#include "PrefsHelper.h" #ifdef DEBUG_ANDROID_EVENTS #define EVLOG(args...) ALOG(args) @@ -195,6 +196,7 @@ nsAppShell::nsAppShell() AndroidBridge::ConstructBridge(); GeckoThreadNatives::Init(); mozilla::ANRReporter::Init(); + mozilla::PrefsHelper::Init(); nsWindow::InitNatives(); widget::GeckoThread::SetState(widget::GeckoThread::State::JNI_READY());