2016-10-29 01:25:08 +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: */
|
|
|
|
/* 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 "mozilla/dom/Dispatcher.h"
|
|
|
|
#include "mozilla/Move.h"
|
2016-11-23 08:14:39 +03:00
|
|
|
#include "nsINamed.h"
|
2016-11-04 21:13:52 +03:00
|
|
|
#include "nsQueryObject.h"
|
2016-10-29 01:25:08 +03:00
|
|
|
|
|
|
|
using namespace mozilla;
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
DispatcherTrait::Dispatch(const char* aName,
|
|
|
|
TaskCategory aCategory,
|
|
|
|
already_AddRefed<nsIRunnable>&& aRunnable)
|
|
|
|
{
|
2016-11-23 08:14:39 +03:00
|
|
|
nsCOMPtr<nsIRunnable> runnable(aRunnable);
|
|
|
|
if (aName) {
|
|
|
|
if (nsCOMPtr<nsINamed> named = do_QueryInterface(runnable)) {
|
|
|
|
named->SetName(aName);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (NS_IsMainThread()) {
|
|
|
|
return NS_DispatchToCurrentThread(runnable.forget());
|
|
|
|
} else {
|
|
|
|
return NS_DispatchToMainThread(runnable.forget());
|
|
|
|
}
|
2016-10-29 01:25:08 +03:00
|
|
|
}
|
2016-10-29 01:57:50 +03:00
|
|
|
|
2016-12-16 00:48:14 +03:00
|
|
|
nsIEventTarget*
|
2016-11-23 08:14:39 +03:00
|
|
|
DispatcherTrait::EventTargetFor(TaskCategory aCategory) const
|
2016-10-29 01:57:50 +03:00
|
|
|
{
|
|
|
|
nsCOMPtr<nsIEventTarget> main = do_GetMainThread();
|
2016-12-16 00:48:14 +03:00
|
|
|
return main;
|
2016-10-29 01:57:50 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
2016-11-04 21:13:52 +03:00
|
|
|
#define NS_DISPATCHEREVENTTARGET_IID \
|
|
|
|
{ 0xbf4e36c8, 0x7d04, 0x4ef4, \
|
|
|
|
{ 0xbb, 0xd8, 0x11, 0x09, 0x0a, 0xdb, 0x4d, 0xf7 } }
|
|
|
|
|
2016-10-29 01:57:50 +03:00
|
|
|
class DispatcherEventTarget final : public nsIEventTarget
|
|
|
|
{
|
|
|
|
RefPtr<dom::Dispatcher> mDispatcher;
|
|
|
|
TaskCategory mCategory;
|
|
|
|
|
|
|
|
public:
|
2016-11-04 21:13:52 +03:00
|
|
|
NS_DECLARE_STATIC_IID_ACCESSOR(NS_DISPATCHEREVENTTARGET_IID)
|
|
|
|
|
2016-11-23 08:14:39 +03:00
|
|
|
DispatcherEventTarget(dom::Dispatcher* aDispatcher, TaskCategory aCategory)
|
2016-10-29 01:57:50 +03:00
|
|
|
: mDispatcher(aDispatcher)
|
|
|
|
, mCategory(aCategory)
|
|
|
|
{}
|
|
|
|
|
|
|
|
NS_DECL_THREADSAFE_ISUPPORTS
|
|
|
|
NS_DECL_NSIEVENTTARGET
|
|
|
|
|
|
|
|
dom::Dispatcher* Dispatcher() const { return mDispatcher; }
|
|
|
|
|
|
|
|
private:
|
|
|
|
virtual ~DispatcherEventTarget() {}
|
|
|
|
};
|
|
|
|
|
2016-11-04 21:13:52 +03:00
|
|
|
NS_DEFINE_STATIC_IID_ACCESSOR(DispatcherEventTarget, NS_DISPATCHEREVENTTARGET_IID)
|
|
|
|
|
2016-10-29 01:57:50 +03:00
|
|
|
} // namespace
|
|
|
|
|
2016-11-04 21:13:52 +03:00
|
|
|
NS_IMPL_ISUPPORTS(DispatcherEventTarget, DispatcherEventTarget, nsIEventTarget)
|
2016-10-29 01:57:50 +03:00
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
DispatcherEventTarget::DispatchFromScript(nsIRunnable* aRunnable, uint32_t aFlags)
|
|
|
|
{
|
|
|
|
return Dispatch(do_AddRef(aRunnable), aFlags);
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
DispatcherEventTarget::Dispatch(already_AddRefed<nsIRunnable> aRunnable, uint32_t aFlags)
|
|
|
|
{
|
|
|
|
if (NS_WARN_IF(aFlags != NS_DISPATCH_NORMAL)) {
|
|
|
|
return NS_ERROR_UNEXPECTED;
|
|
|
|
}
|
2016-11-23 08:14:39 +03:00
|
|
|
return mDispatcher->Dispatch(nullptr, mCategory, Move(aRunnable));
|
2016-10-29 01:57:50 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
DispatcherEventTarget::DelayedDispatch(already_AddRefed<nsIRunnable>, uint32_t)
|
|
|
|
{
|
|
|
|
return NS_ERROR_NOT_IMPLEMENTED;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
DispatcherEventTarget::IsOnCurrentThread(bool* aIsOnCurrentThread)
|
|
|
|
{
|
|
|
|
*aIsOnCurrentThread = NS_IsMainThread();
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
already_AddRefed<nsIEventTarget>
|
2016-11-23 08:14:39 +03:00
|
|
|
Dispatcher::CreateEventTargetFor(TaskCategory aCategory)
|
2016-10-29 01:57:50 +03:00
|
|
|
{
|
|
|
|
RefPtr<DispatcherEventTarget> target =
|
2016-11-23 08:14:39 +03:00
|
|
|
new DispatcherEventTarget(this, aCategory);
|
2016-10-29 01:57:50 +03:00
|
|
|
return target.forget();
|
|
|
|
}
|
2016-11-04 21:13:52 +03:00
|
|
|
|
|
|
|
/* static */ Dispatcher*
|
|
|
|
Dispatcher::FromEventTarget(nsIEventTarget* aEventTarget)
|
|
|
|
{
|
|
|
|
RefPtr<DispatcherEventTarget> target = do_QueryObject(aEventTarget);
|
|
|
|
if (!target) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
return target->Dispatcher();
|
|
|
|
}
|