gecko-dev/widget/android/PrefsHelper.h
Jim Chen 008886b9a1 Bug 1233812 - Fix possible race in accessing nsAppShell instance; r=snorp
When getting nsAppShell from another thread, there could be a race with
nsAppShell being destroyed on the main thread. This patch makes the raw
nsAppShell pointer only accessible from the main thread, and use a
static mutex to coordinate accessing nsAppShell from other threads.
2015-12-23 22:03:35 -05:00

83 lines
2.5 KiB
C++

/* -*- 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<PrefsHelper>
, UsesGeckoThreadProxy
{
PrefsHelper() = delete;
static void GetPrefsById(const jni::ClassObject::LocalRef& cls,
int32_t requestId,
jni::ObjectArray::Param prefNames,
bool observe)
{
MOZ_ASSERT(NS_IsMainThread());
nsAppShell* const appShell = nsAppShell::Get();
MOZ_ASSERT(appShell);
nsTArray<jni::Object::LocalRef> 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<nsString> namesStrArray(len);
nsTArray<const char16_t*> 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;
appShell->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());
nsAppShell* const appShell = nsAppShell::Get();
MOZ_ASSERT(appShell);
nsIAndroidBrowserApp* browserApp = nullptr;
appShell->GetBrowserApp(&browserApp);
MOZ_ASSERT(browserApp);
browserApp->RemovePreferenceObservers(requestId);
}
};
} // namespace
#endif // PrefsHelper_h