2016-05-28 00:54:31 +03:00
|
|
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
|
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/. */
|
2009-06-29 22:38:29 +04:00
|
|
|
|
|
|
|
#ifndef __IPC_GLUE_MESSAGEPUMP_H__
|
|
|
|
#define __IPC_GLUE_MESSAGEPUMP_H__
|
|
|
|
|
|
|
|
#include "base/message_pump_default.h"
|
2014-09-12 18:49:38 +04:00
|
|
|
#if defined(XP_WIN)
|
|
|
|
# include "base/message_pump_win.h"
|
|
|
|
#endif
|
|
|
|
|
2009-11-24 00:01:12 +03:00
|
|
|
#include "base/time.h"
|
2013-10-23 12:28:24 +04:00
|
|
|
#include "mozilla/Attributes.h"
|
2014-09-12 18:49:38 +04:00
|
|
|
#include "mozilla/Mutex.h"
|
2009-07-02 01:19:32 +04:00
|
|
|
#include "nsCOMPtr.h"
|
2014-09-12 18:49:38 +04:00
|
|
|
#include "nsIThreadInternal.h"
|
2009-07-02 01:19:32 +04:00
|
|
|
|
2017-08-11 02:00:32 +03:00
|
|
|
class nsIEventTarget;
|
2013-10-23 12:28:24 +04:00
|
|
|
class nsITimer;
|
2009-07-02 01:19:32 +04:00
|
|
|
|
2009-06-29 22:38:29 +04:00
|
|
|
namespace mozilla {
|
|
|
|
namespace ipc {
|
|
|
|
|
2013-10-23 12:28:24 +04:00
|
|
|
class DoWorkRunnable;
|
2009-11-24 00:01:12 +03:00
|
|
|
|
2013-10-23 12:28:24 +04:00
|
|
|
class MessagePump : public base::MessagePumpDefault {
|
|
|
|
friend class DoWorkRunnable;
|
|
|
|
|
2009-11-24 00:01:12 +03:00
|
|
|
public:
|
2017-08-11 02:00:32 +03:00
|
|
|
explicit MessagePump(nsIEventTarget* aEventTarget);
|
2009-11-24 00:01:12 +03:00
|
|
|
|
2013-10-23 12:28:24 +04:00
|
|
|
// From base::MessagePump.
|
2015-03-21 19:28:04 +03:00
|
|
|
virtual void Run(base::MessagePump::Delegate* aDelegate) override;
|
2009-11-24 00:01:12 +03:00
|
|
|
|
2013-10-23 12:28:24 +04:00
|
|
|
// From base::MessagePump.
|
2015-03-21 19:28:04 +03:00
|
|
|
virtual void ScheduleWork() override;
|
2009-11-24 00:01:12 +03:00
|
|
|
|
2013-10-23 12:28:24 +04:00
|
|
|
// From base::MessagePump.
|
2015-03-21 19:28:04 +03:00
|
|
|
virtual void ScheduleWorkForNestedLoop() override;
|
2009-11-24 00:01:12 +03:00
|
|
|
|
2013-10-23 12:28:24 +04:00
|
|
|
// From base::MessagePump.
|
2015-03-21 19:28:04 +03:00
|
|
|
virtual void ScheduleDelayedWork(
|
|
|
|
const base::TimeTicks& aDelayedWorkTime) override;
|
2009-06-29 22:38:29 +04:00
|
|
|
|
2016-05-13 01:15:43 +03:00
|
|
|
virtual nsIEventTarget* GetXPCOMThread() override;
|
|
|
|
|
2013-10-23 12:28:24 +04:00
|
|
|
protected:
|
|
|
|
virtual ~MessagePump();
|
2009-11-24 00:01:12 +03:00
|
|
|
|
2013-10-23 12:28:24 +04:00
|
|
|
private:
|
|
|
|
// Only called by DoWorkRunnable.
|
2009-11-24 00:01:12 +03:00
|
|
|
void DoDelayedWork(base::MessagePump::Delegate* aDelegate);
|
2009-07-02 01:19:32 +04:00
|
|
|
|
2013-10-23 12:28:24 +04:00
|
|
|
protected:
|
2017-08-11 02:00:32 +03:00
|
|
|
nsIEventTarget* mEventTarget;
|
2016-03-30 22:20:20 +03:00
|
|
|
|
2017-08-11 02:00:32 +03:00
|
|
|
// mDelayedWorkTimer and mEventTarget are set in Run() by this class or its
|
2013-10-23 12:28:24 +04:00
|
|
|
// subclasses.
|
2009-11-24 00:01:12 +03:00
|
|
|
nsCOMPtr<nsITimer> mDelayedWorkTimer;
|
2013-10-23 12:28:24 +04:00
|
|
|
|
|
|
|
private:
|
|
|
|
// Only accessed by this class.
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<DoWorkRunnable> mDoWorkEvent;
|
2009-06-29 22:38:29 +04:00
|
|
|
};
|
|
|
|
|
2015-03-21 19:28:04 +03:00
|
|
|
class MessagePumpForChildProcess final : public MessagePump {
|
2009-06-29 22:38:29 +04:00
|
|
|
public:
|
|
|
|
MessagePumpForChildProcess() : MessagePump(nullptr), mFirstRun(true) {}
|
|
|
|
|
2015-03-21 19:28:04 +03:00
|
|
|
virtual void Run(base::MessagePump::Delegate* aDelegate) override;
|
2009-06-29 22:38:29 +04:00
|
|
|
|
|
|
|
private:
|
2013-10-23 12:28:24 +04:00
|
|
|
~MessagePumpForChildProcess() {}
|
|
|
|
|
2009-06-29 22:38:29 +04:00
|
|
|
bool mFirstRun;
|
|
|
|
};
|
|
|
|
|
2015-03-21 19:28:04 +03:00
|
|
|
class MessagePumpForNonMainThreads final : public MessagePump {
|
2013-10-23 16:01:24 +04:00
|
|
|
public:
|
2017-08-11 02:00:32 +03:00
|
|
|
explicit MessagePumpForNonMainThreads(nsIEventTarget* aEventTarget)
|
|
|
|
: MessagePump(aEventTarget) {}
|
2013-10-23 16:01:24 +04:00
|
|
|
|
2015-03-21 19:28:04 +03:00
|
|
|
virtual void Run(base::MessagePump::Delegate* aDelegate) override;
|
2013-10-23 16:01:24 +04:00
|
|
|
|
|
|
|
private:
|
|
|
|
~MessagePumpForNonMainThreads() {}
|
|
|
|
};
|
|
|
|
|
2014-09-12 18:49:38 +04:00
|
|
|
#if defined(XP_WIN)
|
|
|
|
// Extends the TYPE_UI message pump to process xpcom events. Currently only
|
|
|
|
// implemented for Win.
|
2015-03-21 19:28:04 +03:00
|
|
|
class MessagePumpForNonMainUIThreads final : public base::MessagePumpForUI,
|
2014-09-12 18:49:38 +04:00
|
|
|
public nsIThreadObserver {
|
|
|
|
public:
|
|
|
|
// We don't want xpcom refing, chromium controls our lifetime via
|
|
|
|
// RefCountedThreadSafe.
|
2015-12-16 18:46:06 +03:00
|
|
|
NS_IMETHOD_(MozExternalRefCountType) AddRef(void) override { return 2; }
|
|
|
|
NS_IMETHOD_(MozExternalRefCountType) Release(void) override { return 1; }
|
|
|
|
NS_IMETHOD QueryInterface(REFNSIID aIID, void** aInstancePtr) override;
|
|
|
|
|
|
|
|
NS_DECL_NSITHREADOBSERVER
|
2014-09-12 18:49:38 +04:00
|
|
|
|
|
|
|
public:
|
2017-08-11 02:00:32 +03:00
|
|
|
explicit MessagePumpForNonMainUIThreads(nsIEventTarget* aEventTarget)
|
2014-09-12 18:49:38 +04:00
|
|
|
: mInWait(false), mWaitLock("mInWait") {}
|
|
|
|
|
|
|
|
// The main run loop for this thread.
|
2015-12-16 18:46:06 +03:00
|
|
|
virtual void DoRunLoop() override;
|
2014-09-12 18:49:38 +04:00
|
|
|
|
2016-05-13 01:15:43 +03:00
|
|
|
virtual nsIEventTarget* GetXPCOMThread() override {
|
|
|
|
return nullptr; // not sure what to do with this one
|
|
|
|
}
|
|
|
|
|
2014-09-12 18:49:38 +04:00
|
|
|
protected:
|
|
|
|
void SetInWait() {
|
|
|
|
MutexAutoLock lock(mWaitLock);
|
|
|
|
mInWait = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ClearInWait() {
|
|
|
|
MutexAutoLock lock(mWaitLock);
|
|
|
|
mInWait = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool GetInWait() {
|
|
|
|
MutexAutoLock lock(mWaitLock);
|
|
|
|
return mInWait;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
~MessagePumpForNonMainUIThreads() {}
|
|
|
|
|
|
|
|
bool mInWait;
|
|
|
|
mozilla::Mutex mWaitLock;
|
|
|
|
};
|
|
|
|
#endif // defined(XP_WIN)
|
|
|
|
|
2016-12-01 01:56:53 +03:00
|
|
|
#if defined(MOZ_WIDGET_ANDROID)
|
|
|
|
/*`
|
|
|
|
* The MessagePumpForAndroidUI exists to enable IPDL in the Android UI thread.
|
|
|
|
* The Android UI thread event loop is controlled by Android. This prevents
|
|
|
|
* running an existing MessagePump implementation in the Android UI thread. In
|
|
|
|
* order to enable IPDL on the Android UI thread it is necessary to have a
|
|
|
|
* non-looping MessagePump. This class enables forwarding of nsIRunnables from
|
|
|
|
* MessageLoop::PostTask_Helper to the registered nsIEventTarget with out the
|
|
|
|
* need to control the event loop. The only member function that should be
|
|
|
|
* invoked is GetXPCOMThread. All other member functions will invoke MOZ_CRASH
|
|
|
|
*/
|
|
|
|
class MessagePumpForAndroidUI : public base::MessagePump {
|
|
|
|
public:
|
2018-03-27 17:51:32 +03:00
|
|
|
explicit MessagePumpForAndroidUI(nsIEventTarget* aEventTarget)
|
2016-12-01 01:56:53 +03:00
|
|
|
: mEventTarget(aEventTarget) {}
|
|
|
|
|
|
|
|
virtual void Run(Delegate* delegate);
|
|
|
|
virtual void Quit();
|
|
|
|
virtual void ScheduleWork();
|
|
|
|
virtual void ScheduleDelayedWork(const base::TimeTicks& delayed_work_time);
|
|
|
|
virtual nsIEventTarget* GetXPCOMThread() { return mEventTarget; }
|
|
|
|
|
|
|
|
private:
|
|
|
|
~MessagePumpForAndroidUI() {}
|
|
|
|
MessagePumpForAndroidUI() {}
|
|
|
|
|
|
|
|
nsIEventTarget* mEventTarget;
|
|
|
|
};
|
|
|
|
#endif // defined(MOZ_WIDGET_ANDROID)
|
|
|
|
|
2009-06-29 22:38:29 +04:00
|
|
|
} /* namespace ipc */
|
|
|
|
} /* namespace mozilla */
|
|
|
|
|
|
|
|
#endif /* __IPC_GLUE_MESSAGEPUMP_H__ */
|