1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/dom/plugins/ipc/ScopedMethodFactory.h Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,87 @@ 1.4 +/* This Source Code Form is subject to the terms of the Mozilla Public 1.5 + * License, v. 2.0. If a copy of the MPL was not distributed with this 1.6 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.7 + 1.8 +#ifndef mozilla_plugins_ScopedMethodFactory_h 1.9 +#define mozilla_plugins_ScopedMethodFactory_h 1.10 + 1.11 +#include <base/task.h> 1.12 + 1.13 +/* 1.14 + * This is based on the ScopedRunnableMethodFactory from ipc/chromium/src/base/task.h 1.15 + * Chromiums factories assert if tasks are created and run on different threads, 1.16 + * which is something we need to do in PluginModuleParent (hang UI vs. main thread). 1.17 + * ScopedMethodFactory just provides cancellable tasks that don't assert this. 1.18 + */ 1.19 + 1.20 +namespace mozilla { 1.21 +namespace plugins { 1.22 + 1.23 +template<class T> 1.24 +class ScopedMethodFactory : public RevocableStore 1.25 +{ 1.26 +private: 1.27 + template<class TaskType> 1.28 + class TaskWrapper : public TaskType 1.29 + { 1.30 + public: 1.31 + explicit TaskWrapper(RevocableStore* store) : revocable_(store) { } 1.32 + 1.33 + virtual void Run() { 1.34 + if (!revocable_.revoked()) 1.35 + TaskType::Run(); 1.36 + } 1.37 + 1.38 + private: 1.39 + Revocable revocable_; 1.40 + }; 1.41 + 1.42 +public: 1.43 + explicit ScopedMethodFactory(T* object) : object_(object) { } 1.44 + 1.45 + template <class Method> 1.46 + inline Task* NewRunnableMethod(Method method) { 1.47 + typedef TaskWrapper<RunnableMethod<Method, Tuple0> > TaskWrapper; 1.48 + 1.49 + TaskWrapper* task = new TaskWrapper(this); 1.50 + task->Init(object_, method, MakeTuple()); 1.51 + return task; 1.52 + } 1.53 + 1.54 + template <class Method, class A> 1.55 + inline Task* NewRunnableMethod(Method method, const A& a) { 1.56 + typedef TaskWrapper<RunnableMethod<Method, Tuple1<A> > > TaskWrapper; 1.57 + 1.58 + TaskWrapper* task = new TaskWrapper(this); 1.59 + task->Init(object_, method, MakeTuple(a)); 1.60 + return task; 1.61 + } 1.62 + 1.63 +protected: 1.64 + template <class Method, class Params> 1.65 + class RunnableMethod : public Task { 1.66 + public: 1.67 + RunnableMethod() { } 1.68 + 1.69 + void Init(T* obj, Method meth, const Params& params) { 1.70 + obj_ = obj; 1.71 + meth_ = meth; 1.72 + params_ = params; 1.73 + } 1.74 + 1.75 + virtual void Run() { DispatchToMethod(obj_, meth_, params_); } 1.76 + 1.77 + private: 1.78 + T* obj_; 1.79 + Method meth_; 1.80 + Params params_; 1.81 + }; 1.82 + 1.83 +private: 1.84 + T* object_; 1.85 +}; 1.86 + 1.87 +} // namespace plugins 1.88 +} // namespace mozilla 1.89 + 1.90 +#endif // mozilla_plugins_ScopedMethodFactory_h