2017-10-27 02:05:47 +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: */
|
2013-09-23 21:34:00 +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/. */
|
|
|
|
|
2015-03-06 22:31:17 +03:00
|
|
|
#ifndef mozilla_plugins_TaskFactory_h
|
|
|
|
#define mozilla_plugins_TaskFactory_h
|
2013-09-23 21:34:00 +04:00
|
|
|
|
|
|
|
#include <base/task.h>
|
|
|
|
|
2015-04-30 23:23:24 +03:00
|
|
|
#include "mozilla/Move.h"
|
|
|
|
|
2013-09-23 21:34:00 +04:00
|
|
|
/*
|
|
|
|
* This is based on the ScopedRunnableMethodFactory from
|
2015-03-06 22:31:17 +03:00
|
|
|
* ipc/chromium/src/base/task.h Chromium's factories assert if tasks are created
|
2013-09-23 21:34:00 +04:00
|
|
|
* and run on different threads, which is something we need to do in
|
|
|
|
* PluginModuleParent (hang UI vs. main thread). TaskFactory just provides
|
2015-03-06 22:31:17 +03:00
|
|
|
* cancellable tasks that don't assert this. This version also allows both
|
|
|
|
* ScopedMethod and regular Tasks to be generated by the same Factory object.
|
2013-09-23 21:34:00 +04:00
|
|
|
*/
|
|
|
|
|
|
|
|
namespace mozilla {
|
2016-06-10 22:43:35 +03:00
|
|
|
namespace ipc {
|
2013-09-23 21:34:00 +04:00
|
|
|
|
|
|
|
template <class T>
|
2015-03-06 22:31:17 +03:00
|
|
|
class TaskFactory : public RevocableStore {
|
2013-09-23 21:34:00 +04:00
|
|
|
private:
|
|
|
|
template <class TaskType>
|
|
|
|
class TaskWrapper : public TaskType {
|
|
|
|
public:
|
2015-04-30 23:23:24 +03:00
|
|
|
template <typename... Args>
|
|
|
|
explicit TaskWrapper(RevocableStore* store, Args&&... args)
|
2018-06-01 19:30:30 +03:00
|
|
|
: TaskType(std::forward<Args>(args)...), revocable_(store) {}
|
2013-09-23 21:34:00 +04:00
|
|
|
|
2016-04-28 03:06:05 +03:00
|
|
|
NS_IMETHOD Run() override {
|
2013-09-23 21:34:00 +04:00
|
|
|
if (!revocable_.revoked()) TaskType::Run();
|
2016-04-28 03:06:05 +03:00
|
|
|
return NS_OK;
|
2013-09-23 21:34:00 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
Revocable revocable_;
|
|
|
|
};
|
|
|
|
|
|
|
|
public:
|
2015-03-06 22:31:17 +03:00
|
|
|
explicit TaskFactory(T* object) : object_(object) {}
|
|
|
|
|
2015-04-30 23:23:24 +03:00
|
|
|
template <typename TaskParamType, typename... Args>
|
2016-04-28 03:06:05 +03:00
|
|
|
inline already_AddRefed<TaskParamType> NewTask(Args&&... args) {
|
2015-03-06 22:31:17 +03:00
|
|
|
typedef TaskWrapper<TaskParamType> TaskWrapper;
|
2016-04-28 03:06:05 +03:00
|
|
|
RefPtr<TaskWrapper> task =
|
2018-06-01 19:30:30 +03:00
|
|
|
new TaskWrapper(this, std::forward<Args>(args)...);
|
2016-04-28 03:06:05 +03:00
|
|
|
return task.forget();
|
2015-03-06 22:31:17 +03:00
|
|
|
}
|
2013-09-23 21:34:00 +04:00
|
|
|
|
2017-03-06 23:36:06 +03:00
|
|
|
template <class Method, typename... Args>
|
|
|
|
inline already_AddRefed<Runnable> NewRunnableMethod(Method method,
|
|
|
|
Args&&... args) {
|
2018-06-01 19:30:30 +03:00
|
|
|
typedef decltype(base::MakeTuple(std::forward<Args>(args)...)) ArgTuple;
|
2017-03-06 23:36:06 +03:00
|
|
|
typedef RunnableMethod<Method, ArgTuple> RunnableMethod;
|
|
|
|
typedef TaskWrapper<RunnableMethod> TaskWrapper;
|
2013-09-23 21:34:00 +04:00
|
|
|
|
2017-03-06 23:36:06 +03:00
|
|
|
RefPtr<TaskWrapper> task = new TaskWrapper(
|
2018-06-01 19:30:30 +03:00
|
|
|
this, object_, method, base::MakeTuple(std::forward<Args>(args)...));
|
2016-07-01 16:05:28 +03:00
|
|
|
|
2016-04-28 03:06:05 +03:00
|
|
|
return task.forget();
|
2013-09-23 21:34:00 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
protected:
|
|
|
|
template <class Method, class Params>
|
2016-04-28 03:06:05 +03:00
|
|
|
class RunnableMethod : public Runnable {
|
2013-09-23 21:34:00 +04:00
|
|
|
public:
|
2017-06-12 22:34:10 +03:00
|
|
|
RunnableMethod(T* obj, Method meth, const Params& params)
|
|
|
|
: Runnable("ipc::TaskFactory::RunnableMethod"),
|
|
|
|
obj_(obj),
|
|
|
|
meth_(meth),
|
|
|
|
params_(params) {}
|
2013-09-23 21:34:00 +04:00
|
|
|
|
2016-04-28 03:06:05 +03:00
|
|
|
NS_IMETHOD Run() override {
|
|
|
|
DispatchToMethod(obj_, meth_, params_);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
2013-09-23 21:34:00 +04:00
|
|
|
|
|
|
|
private:
|
|
|
|
T* obj_;
|
|
|
|
Method meth_;
|
|
|
|
Params params_;
|
|
|
|
};
|
|
|
|
|
|
|
|
private:
|
|
|
|
T* object_;
|
|
|
|
};
|
|
|
|
|
2016-06-10 22:43:35 +03:00
|
|
|
} // namespace ipc
|
2013-09-23 21:34:00 +04:00
|
|
|
} // namespace mozilla
|
|
|
|
|
2015-03-06 22:31:17 +03:00
|
|
|
#endif // mozilla_plugins_TaskFactory_h
|