2010-06-04 00:56:36 +04:00
|
|
|
/* -*- Mode: c++; tab-width: 40; indent-tabs-mode: nil; c-basic-offset: 4; -*- */
|
2012-05-21 15:12:37 +04:00
|
|
|
/* 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/. */
|
2010-06-04 00:56:36 +04:00
|
|
|
|
|
|
|
#ifndef nsAppShell_h__
|
|
|
|
#define nsAppShell_h__
|
|
|
|
|
2016-12-21 21:37:20 +03:00
|
|
|
#include <time.h>
|
|
|
|
|
2018-05-25 20:13:26 +03:00
|
|
|
#include "mozilla/HangMonitor.h"
|
2015-09-21 17:13:32 +03:00
|
|
|
#include "mozilla/LinkedList.h"
|
|
|
|
#include "mozilla/Monitor.h"
|
|
|
|
#include "mozilla/Move.h"
|
2016-01-13 22:35:27 +03:00
|
|
|
#include "mozilla/StaticPtr.h"
|
Bug 1343075 - Use GeckoEditableSupport from PuppetWidget; r=masayuki r=rbarker r=snorp r=esawin
Bug 1343075 - 1a. Add TextEventDispatcherListener::GetIMEUpdatePreference; r=masayuki
Add a GetIMEUpdatePreference method to TextEventDispatcherListener to
optionally control which IME notifications are received by NotifyIME.
This patch also makes nsBaseWidget forward its GetIMEUpdatePreference
call to the widget's native TextEventDispatcherListener.
Bug 1343075 - 1b. Implement GetIMEUpdatePreference for all TextEventDispatcherListener; r=masayuki
This patch implements GetIMEUpdatePreference for all
TextEventDispatcherListener implementations, by moving previous
implementations of nsIWidget::GetIMEUpdatePreference.
Bug 1343075 - 2. Allow setting a PuppetWidget's native TextEventDispatcherListener; r=masayuki
In PuppetWidget, add getter and setter for the widget's native
TextEventDispatcherListener. This allows overriding of PuppetWidget's
default IME handling. For example, on Android, the PuppetWidget's native
TextEventDispatcherListener will communicate directly with Java IME code
in the main process.
Bug 1343075 - 3. Add AIDL interface for main process; r=rbarker
Add AIDL definition and implementation for an interface for the main
process that child processes can access.
Bug 1343075 - 4. Set Gecko thread JNIEnv for child process; r=snorp
Add a JNIEnv* parameter to XRE_SetAndroidChildFds, which is used to set
the Gecko thread JNIEnv for child processes. XRE_SetAndroidChildFds is
the only Android-specific entry point for child processes, so I think
it's the most logical place to initialize JNI.
Bug 1343075 - 5. Support multiple remote GeckoEditableChild; r=esawin
Support remote GeckoEditableChild instances that are created in the
content processes and connect to the parent process GeckoEditableParent
through binders.
Support having multiple GeckoEditableChild instances in GeckoEditable by
keeping track of which child is currently focused, and only allow
calls to/from the focused child by using access tokens.
Bug 1343075 - 6. Add method to get GeckoEditableParent instance; r=esawin
Add IProcessManager.getEditableParent, which a content process can call
to get the GeckoEditableParent instance that corresponds to a given
content process tab, from the main process.
Bug 1343075 - 7. Support GeckoEditableSupport in content processes; r=esawin
Support creating and running GeckoEditableSupport attached to a
PuppetWidget in content processes.
Because we don't know PuppetWidget's lifetime as well as nsWindow's,
when attached to PuppetWidget, we need to attach/detach our native
object on focus/blur, respectively.
Bug 1343075 - 8. Connect GeckoEditableSupport on PuppetWidget creation; r=esawin
Listen to the "tab-child-created" notification and attach our content
process GeckoEditableSupport to the new PuppetWidget.
Bug 1343075 - 9. Update auto-generated bindings; r=me
2017-03-08 06:34:39 +03:00
|
|
|
#include "mozilla/TypeTraits.h"
|
2015-09-21 17:13:32 +03:00
|
|
|
#include "mozilla/UniquePtr.h"
|
2016-08-23 07:09:32 +03:00
|
|
|
#include "mozilla/Unused.h"
|
2015-09-21 17:13:33 +03:00
|
|
|
#include "mozilla/jni/Natives.h"
|
2010-06-04 00:56:36 +04:00
|
|
|
#include "nsBaseAppShell.h"
|
|
|
|
#include "nsCOMPtr.h"
|
|
|
|
#include "nsTArray.h"
|
2010-06-15 06:17:37 +04:00
|
|
|
#include "nsInterfaceHashtable.h"
|
2012-02-17 22:52:26 +04:00
|
|
|
#include "nsIAndroidBridge.h"
|
2010-06-04 00:56:36 +04:00
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
bool ProcessNextEvent();
|
|
|
|
void NotifyEvent();
|
|
|
|
}
|
|
|
|
|
2011-08-09 20:08:19 +04:00
|
|
|
class nsWindow;
|
|
|
|
|
2010-06-04 00:56:36 +04:00
|
|
|
class nsAppShell :
|
|
|
|
public nsBaseAppShell
|
|
|
|
{
|
|
|
|
public:
|
2015-09-21 17:13:32 +03:00
|
|
|
struct Event : mozilla::LinkedListElement<Event>
|
|
|
|
{
|
2015-10-23 00:45:47 +03:00
|
|
|
typedef mozilla::HangMonitor::ActivityType Type;
|
|
|
|
|
2016-12-21 21:37:20 +03:00
|
|
|
static uint64_t GetTime()
|
|
|
|
{
|
|
|
|
timespec time;
|
|
|
|
if (clock_gettime(CLOCK_MONOTONIC, &time)) {
|
|
|
|
return 0ull;
|
|
|
|
}
|
|
|
|
return uint64_t(time.tv_sec) * 1000000000ull + time.tv_nsec;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint64_t mPostTime{ 0 };
|
|
|
|
|
2015-09-21 17:13:32 +03:00
|
|
|
bool HasSameTypeAs(const Event* other) const
|
|
|
|
{
|
|
|
|
// Compare vtable addresses to determine same type.
|
|
|
|
return *reinterpret_cast<const uintptr_t*>(this)
|
|
|
|
== *reinterpret_cast<const uintptr_t*>(other);
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual ~Event() {}
|
|
|
|
virtual void Run() = 0;
|
|
|
|
|
|
|
|
virtual void PostTo(mozilla::LinkedList<Event>& queue)
|
|
|
|
{
|
|
|
|
queue.insertBack(this);
|
|
|
|
}
|
|
|
|
|
2015-10-23 00:45:47 +03:00
|
|
|
virtual Type ActivityType() const
|
2015-09-21 17:13:32 +03:00
|
|
|
{
|
2015-10-23 00:45:47 +03:00
|
|
|
return Type::kGeneralActivity;
|
2015-09-21 17:13:32 +03:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
class LambdaEvent : public Event
|
|
|
|
{
|
|
|
|
protected:
|
|
|
|
T lambda;
|
|
|
|
|
|
|
|
public:
|
2018-03-27 17:51:32 +03:00
|
|
|
explicit LambdaEvent(T&& l) : lambda(mozilla::Move(l)) {}
|
2015-09-21 17:13:32 +03:00
|
|
|
void Run() override { return lambda(); }
|
|
|
|
};
|
|
|
|
|
2015-12-24 06:03:35 +03:00
|
|
|
class ProxyEvent : public Event
|
|
|
|
{
|
|
|
|
protected:
|
|
|
|
mozilla::UniquePtr<Event> baseEvent;
|
|
|
|
|
|
|
|
public:
|
2018-03-27 17:51:32 +03:00
|
|
|
explicit ProxyEvent(mozilla::UniquePtr<Event>&& event)
|
2015-12-24 06:03:35 +03:00
|
|
|
: baseEvent(mozilla::Move(event))
|
|
|
|
{}
|
|
|
|
|
|
|
|
void PostTo(mozilla::LinkedList<Event>& queue) override
|
|
|
|
{
|
|
|
|
baseEvent->PostTo(queue);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Run() override
|
|
|
|
{
|
|
|
|
baseEvent->Run();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2015-12-24 06:03:35 +03:00
|
|
|
static nsAppShell* Get()
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
return sAppShell;
|
|
|
|
}
|
2010-06-04 00:56:36 +04:00
|
|
|
|
|
|
|
nsAppShell();
|
|
|
|
|
2010-12-22 00:28:45 +03:00
|
|
|
NS_DECL_ISUPPORTS_INHERITED
|
|
|
|
NS_DECL_NSIOBSERVER
|
|
|
|
|
2010-06-04 00:56:36 +04:00
|
|
|
nsresult Init();
|
|
|
|
|
|
|
|
void NotifyNativeEvent();
|
2015-09-21 17:13:32 +03:00
|
|
|
bool ProcessNextNativeEvent(bool mayWait) override;
|
|
|
|
|
|
|
|
// Post a subclass of Event.
|
|
|
|
// e.g. PostEvent(mozilla::MakeUnique<MyEvent>());
|
|
|
|
template<typename T, typename D>
|
2015-12-24 06:03:35 +03:00
|
|
|
static void PostEvent(mozilla::UniquePtr<T, D>&& event)
|
2015-09-21 17:13:32 +03:00
|
|
|
{
|
2016-01-13 22:35:27 +03:00
|
|
|
mozilla::MutexAutoLock lock(*sAppShellLock);
|
2015-12-24 06:03:35 +03:00
|
|
|
if (!sAppShell) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
sAppShell->mEventQueue.Post(mozilla::Move(event));
|
2015-09-21 17:13:32 +03:00
|
|
|
}
|
2010-06-04 00:56:36 +04:00
|
|
|
|
2015-09-21 17:13:32 +03:00
|
|
|
// Post a event that will call a lambda
|
|
|
|
// e.g. PostEvent([=] { /* do something */ });
|
|
|
|
template<typename T>
|
2015-12-24 06:03:35 +03:00
|
|
|
static void PostEvent(T&& lambda)
|
2015-09-21 17:13:32 +03:00
|
|
|
{
|
2016-01-13 22:35:27 +03:00
|
|
|
mozilla::MutexAutoLock lock(*sAppShellLock);
|
2015-12-24 06:03:35 +03:00
|
|
|
if (!sAppShell) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
sAppShell->mEventQueue.Post(mozilla::MakeUnique<LambdaEvent<T>>(
|
2015-09-21 17:13:32 +03:00
|
|
|
mozilla::Move(lambda)));
|
|
|
|
}
|
2010-06-04 00:56:36 +04:00
|
|
|
|
2015-12-24 06:03:35 +03:00
|
|
|
// Post a event and wait for it to finish running on the Gecko thread.
|
|
|
|
static void SyncRunEvent(Event&& event,
|
|
|
|
mozilla::UniquePtr<Event>(*eventFactory)(
|
|
|
|
mozilla::UniquePtr<Event>&&) = nullptr);
|
|
|
|
|
Bug 1343075 - Use GeckoEditableSupport from PuppetWidget; r=masayuki r=rbarker r=snorp r=esawin
Bug 1343075 - 1a. Add TextEventDispatcherListener::GetIMEUpdatePreference; r=masayuki
Add a GetIMEUpdatePreference method to TextEventDispatcherListener to
optionally control which IME notifications are received by NotifyIME.
This patch also makes nsBaseWidget forward its GetIMEUpdatePreference
call to the widget's native TextEventDispatcherListener.
Bug 1343075 - 1b. Implement GetIMEUpdatePreference for all TextEventDispatcherListener; r=masayuki
This patch implements GetIMEUpdatePreference for all
TextEventDispatcherListener implementations, by moving previous
implementations of nsIWidget::GetIMEUpdatePreference.
Bug 1343075 - 2. Allow setting a PuppetWidget's native TextEventDispatcherListener; r=masayuki
In PuppetWidget, add getter and setter for the widget's native
TextEventDispatcherListener. This allows overriding of PuppetWidget's
default IME handling. For example, on Android, the PuppetWidget's native
TextEventDispatcherListener will communicate directly with Java IME code
in the main process.
Bug 1343075 - 3. Add AIDL interface for main process; r=rbarker
Add AIDL definition and implementation for an interface for the main
process that child processes can access.
Bug 1343075 - 4. Set Gecko thread JNIEnv for child process; r=snorp
Add a JNIEnv* parameter to XRE_SetAndroidChildFds, which is used to set
the Gecko thread JNIEnv for child processes. XRE_SetAndroidChildFds is
the only Android-specific entry point for child processes, so I think
it's the most logical place to initialize JNI.
Bug 1343075 - 5. Support multiple remote GeckoEditableChild; r=esawin
Support remote GeckoEditableChild instances that are created in the
content processes and connect to the parent process GeckoEditableParent
through binders.
Support having multiple GeckoEditableChild instances in GeckoEditable by
keeping track of which child is currently focused, and only allow
calls to/from the focused child by using access tokens.
Bug 1343075 - 6. Add method to get GeckoEditableParent instance; r=esawin
Add IProcessManager.getEditableParent, which a content process can call
to get the GeckoEditableParent instance that corresponds to a given
content process tab, from the main process.
Bug 1343075 - 7. Support GeckoEditableSupport in content processes; r=esawin
Support creating and running GeckoEditableSupport attached to a
PuppetWidget in content processes.
Because we don't know PuppetWidget's lifetime as well as nsWindow's,
when attached to PuppetWidget, we need to attach/detach our native
object on focus/blur, respectively.
Bug 1343075 - 8. Connect GeckoEditableSupport on PuppetWidget creation; r=esawin
Listen to the "tab-child-created" notification and attach our content
process GeckoEditableSupport to the new PuppetWidget.
Bug 1343075 - 9. Update auto-generated bindings; r=me
2017-03-08 06:34:39 +03:00
|
|
|
template<typename T> static
|
|
|
|
typename mozilla::EnableIf<!mozilla::IsBaseOf<Event, T>::value, void>::Type
|
|
|
|
SyncRunEvent(T&& lambda)
|
|
|
|
{
|
|
|
|
SyncRunEvent(LambdaEvent<T>(mozilla::Forward<T>(lambda)));
|
|
|
|
}
|
|
|
|
|
2016-07-21 04:43:34 +03:00
|
|
|
static already_AddRefed<nsIURI> ResolveURI(const nsCString& aUriStr);
|
|
|
|
|
2012-02-17 22:52:26 +04:00
|
|
|
void SetBrowserApp(nsIAndroidBrowserApp* aBrowserApp) {
|
|
|
|
mBrowserApp = aBrowserApp;
|
|
|
|
}
|
|
|
|
|
2016-07-21 20:49:04 +03:00
|
|
|
nsIAndroidBrowserApp* GetBrowserApp() {
|
|
|
|
return mBrowserApp;
|
2012-02-17 22:52:26 +04:00
|
|
|
}
|
|
|
|
|
2010-06-04 00:56:36 +04:00
|
|
|
protected:
|
2015-12-24 06:03:35 +03:00
|
|
|
static nsAppShell* sAppShell;
|
2016-01-13 22:35:27 +03:00
|
|
|
static mozilla::StaticAutoPtr<mozilla::Mutex> sAppShellLock;
|
2015-12-24 06:03:35 +03:00
|
|
|
|
2016-12-21 21:37:20 +03:00
|
|
|
static void RecordLatencies();
|
|
|
|
|
2010-06-04 00:56:36 +04:00
|
|
|
virtual ~nsAppShell();
|
|
|
|
|
2015-09-21 17:13:32 +03:00
|
|
|
nsresult AddObserver(const nsAString &aObserverKey, nsIObserver *aObserver);
|
|
|
|
|
2015-09-28 19:07:09 +03:00
|
|
|
class NativeCallbackEvent : public Event
|
2015-09-21 17:13:32 +03:00
|
|
|
{
|
|
|
|
// Capturing the nsAppShell instance is safe because if the app
|
|
|
|
// shell is detroyed, this lambda will not be called either.
|
2015-09-28 19:07:09 +03:00
|
|
|
nsAppShell* const appShell;
|
|
|
|
|
|
|
|
public:
|
2018-03-27 17:51:32 +03:00
|
|
|
explicit NativeCallbackEvent(nsAppShell* as) : appShell(as) {}
|
2015-09-28 19:07:09 +03:00
|
|
|
void Run() override { appShell->NativeEventCallback(); }
|
|
|
|
};
|
|
|
|
|
|
|
|
void ScheduleNativeEventCallback() override
|
|
|
|
{
|
|
|
|
mEventQueue.Post(mozilla::MakeUnique<NativeCallbackEvent>(this));
|
2015-09-21 17:13:32 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
class Queue
|
|
|
|
{
|
2015-09-21 17:13:32 +03:00
|
|
|
private:
|
2015-09-28 19:07:09 +03:00
|
|
|
mozilla::Monitor mMonitor;
|
2015-09-21 17:13:32 +03:00
|
|
|
mozilla::LinkedList<Event> mQueue;
|
|
|
|
|
|
|
|
public:
|
2016-12-21 21:37:20 +03:00
|
|
|
enum {
|
|
|
|
LATENCY_UI,
|
|
|
|
LATENCY_OTHER,
|
|
|
|
LATENCY_COUNT
|
|
|
|
};
|
|
|
|
static uint32_t sLatencyCount[LATENCY_COUNT];
|
|
|
|
static uint64_t sLatencyTime[LATENCY_COUNT];
|
|
|
|
|
2015-09-21 17:13:32 +03:00
|
|
|
Queue() : mMonitor("nsAppShell.Queue")
|
|
|
|
{}
|
|
|
|
|
|
|
|
void Signal()
|
|
|
|
{
|
|
|
|
mozilla::MonitorAutoLock lock(mMonitor);
|
|
|
|
lock.NotifyAll();
|
|
|
|
}
|
|
|
|
|
|
|
|
void Post(mozilla::UniquePtr<Event>&& event)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(event && !event->isInList());
|
|
|
|
|
|
|
|
mozilla::MonitorAutoLock lock(mMonitor);
|
|
|
|
event->PostTo(mQueue);
|
|
|
|
if (event->isInList()) {
|
2016-12-21 21:37:20 +03:00
|
|
|
event->mPostTime = Event::GetTime();
|
2015-09-21 17:13:32 +03:00
|
|
|
// Ownership of event object transfers to the queue.
|
2015-11-02 08:53:26 +03:00
|
|
|
mozilla::Unused << event.release();
|
2015-09-21 17:13:32 +03:00
|
|
|
}
|
|
|
|
lock.NotifyAll();
|
|
|
|
}
|
|
|
|
|
|
|
|
mozilla::UniquePtr<Event> Pop(bool mayWait)
|
|
|
|
{
|
|
|
|
mozilla::MonitorAutoLock lock(mMonitor);
|
|
|
|
|
|
|
|
if (mayWait && mQueue.isEmpty()) {
|
2016-12-21 21:37:20 +03:00
|
|
|
#ifdef EARLY_BETA_OR_EARLIER
|
|
|
|
// Record latencies when we're about to be idle.
|
|
|
|
nsAppShell::RecordLatencies();
|
|
|
|
#endif
|
2015-09-21 17:13:32 +03:00
|
|
|
lock.Wait();
|
|
|
|
}
|
2016-12-21 21:37:20 +03:00
|
|
|
|
2015-09-21 17:13:32 +03:00
|
|
|
// Ownership of event object transfers to the return value.
|
2016-12-21 21:37:20 +03:00
|
|
|
mozilla::UniquePtr<Event> event(mQueue.popFirst());
|
|
|
|
if (!event || !event->mPostTime) {
|
|
|
|
return Move(event);
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef EARLY_BETA_OR_EARLIER
|
|
|
|
const size_t latencyType = (event->ActivityType() ==
|
|
|
|
Event::Type::kUIActivity) ? LATENCY_UI : LATENCY_OTHER;
|
|
|
|
const uint64_t latency = Event::GetTime() - event->mPostTime;
|
|
|
|
|
|
|
|
sLatencyCount[latencyType]++;
|
|
|
|
sLatencyTime[latencyType] += latency;
|
|
|
|
#endif
|
|
|
|
return Move(event);
|
2015-09-21 17:13:32 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
} mEventQueue;
|
|
|
|
|
2016-01-13 22:35:27 +03:00
|
|
|
mozilla::CondVar mSyncRunFinished;
|
2015-12-24 06:03:35 +03:00
|
|
|
bool mSyncRunQuit;
|
|
|
|
|
2012-08-09 23:54:36 +04:00
|
|
|
bool mAllowCoalescingTouches;
|
2012-02-17 22:52:26 +04:00
|
|
|
|
|
|
|
nsCOMPtr<nsIAndroidBrowserApp> mBrowserApp;
|
2015-09-21 17:13:32 +03:00
|
|
|
nsInterfaceHashtable<nsStringHashKey, nsIObserver> mObserversHash;
|
2010-06-04 00:56:36 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
#endif // nsAppShell_h__
|
|
|
|
|