2016-12-01 01:59:57 +03:00
|
|
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* 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/. */
|
|
|
|
|
|
|
|
#include "AndroidBridge.h"
|
|
|
|
#include "base/message_loop.h"
|
2017-01-13 22:53:29 +03:00
|
|
|
#include "mozilla/Atomics.h"
|
2016-12-01 01:59:57 +03:00
|
|
|
#include "mozilla/Monitor.h"
|
|
|
|
#include "mozilla/RefPtr.h"
|
|
|
|
#include "mozilla/StaticPtr.h"
|
|
|
|
#include "nsThread.h"
|
|
|
|
#include "nsThreadManager.h"
|
|
|
|
#include "nsThreadUtils.h"
|
|
|
|
|
|
|
|
using namespace mozilla;
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
class AndroidUiThread;
|
|
|
|
|
|
|
|
StaticRefPtr<AndroidUiThread> sThread;
|
|
|
|
static MessageLoop* sMessageLoop;
|
2017-01-13 22:53:29 +03:00
|
|
|
static Atomic<Monitor*> sMessageLoopAccessMonitor;
|
2016-12-01 01:59:57 +03:00
|
|
|
|
|
|
|
/*
|
|
|
|
* The AndroidUiThread is derived from nsThread so that nsIRunnable objects that get
|
|
|
|
* dispatched may be intercepted. Only nsIRunnable objects that need to be synchronously
|
|
|
|
* executed are passed into the nsThread to be queued. All other nsIRunnable object
|
|
|
|
* are immediately dispatched to the Android UI thread via the AndroidBridge.
|
|
|
|
* AndroidUiThread is derived from nsThread instead of being an nsIEventTarget
|
|
|
|
* wrapper that contains an nsThread object because if nsIRunnable objects with a
|
|
|
|
* delay were dispatch directly to an nsThread object, such as obtained from
|
|
|
|
* nsThreadManager::GetCurrentThread(), the nsIRunnable could get stuck in the
|
|
|
|
* nsThread nsIRunnable queue. This is due to the fact that Android controls the
|
|
|
|
* event loop in the Android UI thread and has no knowledge of when the nsThread
|
|
|
|
* needs to be drained.
|
|
|
|
*/
|
|
|
|
|
|
|
|
class AndroidUiThread : public nsThread
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
NS_DECL_ISUPPORTS_INHERITED
|
|
|
|
AndroidUiThread() : nsThread(nsThread::NOT_MAIN_THREAD, 0)
|
|
|
|
{}
|
|
|
|
|
|
|
|
nsresult Dispatch(already_AddRefed<nsIRunnable> aEvent, uint32_t aFlags) override;
|
|
|
|
nsresult DelayedDispatch(already_AddRefed<nsIRunnable> aEvent, uint32_t aDelayMs) override;
|
|
|
|
|
|
|
|
private:
|
|
|
|
~AndroidUiThread()
|
|
|
|
{}
|
|
|
|
};
|
|
|
|
|
|
|
|
NS_IMPL_ISUPPORTS_INHERITED0(AndroidUiThread, nsThread)
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
AndroidUiThread::Dispatch(already_AddRefed<nsIRunnable> aEvent, uint32_t aFlags)
|
|
|
|
{
|
|
|
|
if (aFlags & NS_DISPATCH_SYNC) {
|
|
|
|
return nsThread::Dispatch(Move(aEvent), aFlags);
|
|
|
|
} else {
|
|
|
|
AndroidBridge::Bridge()->PostTaskToUiThread(Move(aEvent), 0);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
AndroidUiThread::DelayedDispatch(already_AddRefed<nsIRunnable> aEvent, uint32_t aDelayMs)
|
|
|
|
{
|
|
|
|
AndroidBridge::Bridge()->PostTaskToUiThread(Move(aEvent), aDelayMs);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
PumpEvents() {
|
|
|
|
NS_ProcessPendingEvents(sThread.get());
|
|
|
|
}
|
|
|
|
|
|
|
|
class ThreadObserver : public nsIThreadObserver
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
NS_DECL_THREADSAFE_ISUPPORTS
|
|
|
|
NS_DECL_NSITHREADOBSERVER
|
|
|
|
|
|
|
|
ThreadObserver()
|
|
|
|
{}
|
|
|
|
|
|
|
|
private:
|
|
|
|
virtual ~ThreadObserver()
|
|
|
|
{}
|
|
|
|
};
|
|
|
|
|
|
|
|
NS_IMPL_ISUPPORTS(ThreadObserver, nsIThreadObserver)
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
ThreadObserver::OnDispatchedEvent(nsIThreadInternal *thread)
|
|
|
|
{
|
|
|
|
AndroidBridge::Bridge()->PostTaskToUiThread(NS_NewRunnableFunction(&PumpEvents), 0);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
ThreadObserver::OnProcessNextEvent(nsIThreadInternal *thread, bool mayWait)
|
|
|
|
{
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
ThreadObserver::AfterProcessNextEvent(nsIThreadInternal *thread, bool eventWasProcessed)
|
|
|
|
{
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
class CreateOnUiThread : public Runnable {
|
|
|
|
public:
|
2016-12-29 01:12:41 +03:00
|
|
|
CreateOnUiThread()
|
2016-12-01 01:59:57 +03:00
|
|
|
{}
|
|
|
|
|
|
|
|
NS_IMETHOD Run() override {
|
2016-12-29 01:12:41 +03:00
|
|
|
MOZ_ASSERT(sMessageLoopAccessMonitor);
|
|
|
|
MonitorAutoLock lock(*sMessageLoopAccessMonitor);
|
2016-12-01 01:59:57 +03:00
|
|
|
sThread = new AndroidUiThread();
|
|
|
|
sThread->InitCurrentThread();
|
|
|
|
sThread->SetObserver(new ThreadObserver());
|
|
|
|
sMessageLoop = new MessageLoop(MessageLoop::TYPE_MOZILLA_ANDROID_UI, sThread.get());
|
|
|
|
lock.NotifyAll();
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
class DestroyOnUiThread : public Runnable {
|
|
|
|
public:
|
2016-12-29 01:12:41 +03:00
|
|
|
DestroyOnUiThread() : mDestroyed(false)
|
2016-12-01 01:59:57 +03:00
|
|
|
{}
|
|
|
|
|
|
|
|
NS_IMETHOD Run() override {
|
2016-12-29 01:12:41 +03:00
|
|
|
MOZ_ASSERT(sMessageLoopAccessMonitor);
|
|
|
|
MonitorAutoLock lock(*sMessageLoopAccessMonitor);
|
2016-12-01 01:59:57 +03:00
|
|
|
|
|
|
|
delete sMessageLoop;
|
|
|
|
sMessageLoop = nullptr;
|
|
|
|
MOZ_ASSERT(sThread);
|
|
|
|
nsThreadManager::get().UnregisterCurrentThread(*sThread);
|
|
|
|
sThread = nullptr;
|
|
|
|
mDestroyed = true;
|
|
|
|
lock.NotifyAll();
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
void WaitForDestruction()
|
|
|
|
{
|
2016-12-29 01:12:41 +03:00
|
|
|
MOZ_ASSERT(sMessageLoopAccessMonitor);
|
|
|
|
MonitorAutoLock lock(*sMessageLoopAccessMonitor);
|
2016-12-01 01:59:57 +03:00
|
|
|
while (!mDestroyed) {
|
|
|
|
lock.Wait();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
bool mDestroyed;
|
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
|
|
|
|
void
|
|
|
|
CreateAndroidUiThread()
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(!sThread);
|
2016-12-29 01:12:41 +03:00
|
|
|
MOZ_ASSERT(!sMessageLoopAccessMonitor);
|
|
|
|
sMessageLoopAccessMonitor = new Monitor("AndroidUiThreadMessageLoopAccessMonitor");
|
2016-12-01 01:59:57 +03:00
|
|
|
RefPtr<CreateOnUiThread> runnable = new CreateOnUiThread;
|
|
|
|
AndroidBridge::Bridge()->PostTaskToUiThread(do_AddRef(runnable), 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
DestroyAndroidUiThread()
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(sThread);
|
|
|
|
// Insure the Android bridge has not already been deconstructed.
|
|
|
|
MOZ_ASSERT(AndroidBridge::Bridge() != nullptr);
|
2016-12-29 01:12:41 +03:00
|
|
|
RefPtr<DestroyOnUiThread> runnable = new DestroyOnUiThread;
|
2016-12-01 01:59:57 +03:00
|
|
|
AndroidBridge::Bridge()->PostTaskToUiThread(do_AddRef(runnable), 0);
|
|
|
|
runnable->WaitForDestruction();
|
2016-12-29 01:12:41 +03:00
|
|
|
delete sMessageLoopAccessMonitor;
|
|
|
|
sMessageLoopAccessMonitor = nullptr;
|
2016-12-01 01:59:57 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
MessageLoop*
|
|
|
|
GetAndroidUiThreadMessageLoop()
|
|
|
|
{
|
2017-01-13 22:53:29 +03:00
|
|
|
if (!sMessageLoopAccessMonitor) {
|
2016-12-29 01:12:41 +03:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2017-01-13 22:53:29 +03:00
|
|
|
MonitorAutoLock lock(*sMessageLoopAccessMonitor);
|
|
|
|
while (!sMessageLoop) {
|
|
|
|
lock.Wait();
|
2016-12-29 01:12:41 +03:00
|
|
|
}
|
|
|
|
|
2016-12-01 01:59:57 +03:00
|
|
|
return sMessageLoop;
|
|
|
|
}
|
|
|
|
|
2016-12-29 01:12:41 +03:00
|
|
|
RefPtr<nsThread>
|
|
|
|
GetAndroidUiThread()
|
|
|
|
{
|
2017-01-13 22:53:29 +03:00
|
|
|
if (!sMessageLoopAccessMonitor) {
|
2016-12-29 01:12:41 +03:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2017-01-13 22:53:29 +03:00
|
|
|
MonitorAutoLock lock(*sMessageLoopAccessMonitor);
|
|
|
|
while (!sThread) {
|
|
|
|
lock.Wait();
|
2016-12-29 01:12:41 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return sThread;
|
|
|
|
}
|
|
|
|
|
2016-12-01 01:59:57 +03:00
|
|
|
} // namespace mozilla
|