Thu, 22 Jan 2015 13:21:57 +0100
Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6
michael@0 | 1 | /* This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 2 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
michael@0 | 3 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 4 | |
michael@0 | 5 | #ifndef mozilla_plugins_ScopedMethodFactory_h |
michael@0 | 6 | #define mozilla_plugins_ScopedMethodFactory_h |
michael@0 | 7 | |
michael@0 | 8 | #include <base/task.h> |
michael@0 | 9 | |
michael@0 | 10 | /* |
michael@0 | 11 | * This is based on the ScopedRunnableMethodFactory from ipc/chromium/src/base/task.h |
michael@0 | 12 | * Chromiums factories assert if tasks are created and run on different threads, |
michael@0 | 13 | * which is something we need to do in PluginModuleParent (hang UI vs. main thread). |
michael@0 | 14 | * ScopedMethodFactory just provides cancellable tasks that don't assert this. |
michael@0 | 15 | */ |
michael@0 | 16 | |
michael@0 | 17 | namespace mozilla { |
michael@0 | 18 | namespace plugins { |
michael@0 | 19 | |
michael@0 | 20 | template<class T> |
michael@0 | 21 | class ScopedMethodFactory : public RevocableStore |
michael@0 | 22 | { |
michael@0 | 23 | private: |
michael@0 | 24 | template<class TaskType> |
michael@0 | 25 | class TaskWrapper : public TaskType |
michael@0 | 26 | { |
michael@0 | 27 | public: |
michael@0 | 28 | explicit TaskWrapper(RevocableStore* store) : revocable_(store) { } |
michael@0 | 29 | |
michael@0 | 30 | virtual void Run() { |
michael@0 | 31 | if (!revocable_.revoked()) |
michael@0 | 32 | TaskType::Run(); |
michael@0 | 33 | } |
michael@0 | 34 | |
michael@0 | 35 | private: |
michael@0 | 36 | Revocable revocable_; |
michael@0 | 37 | }; |
michael@0 | 38 | |
michael@0 | 39 | public: |
michael@0 | 40 | explicit ScopedMethodFactory(T* object) : object_(object) { } |
michael@0 | 41 | |
michael@0 | 42 | template <class Method> |
michael@0 | 43 | inline Task* NewRunnableMethod(Method method) { |
michael@0 | 44 | typedef TaskWrapper<RunnableMethod<Method, Tuple0> > TaskWrapper; |
michael@0 | 45 | |
michael@0 | 46 | TaskWrapper* task = new TaskWrapper(this); |
michael@0 | 47 | task->Init(object_, method, MakeTuple()); |
michael@0 | 48 | return task; |
michael@0 | 49 | } |
michael@0 | 50 | |
michael@0 | 51 | template <class Method, class A> |
michael@0 | 52 | inline Task* NewRunnableMethod(Method method, const A& a) { |
michael@0 | 53 | typedef TaskWrapper<RunnableMethod<Method, Tuple1<A> > > TaskWrapper; |
michael@0 | 54 | |
michael@0 | 55 | TaskWrapper* task = new TaskWrapper(this); |
michael@0 | 56 | task->Init(object_, method, MakeTuple(a)); |
michael@0 | 57 | return task; |
michael@0 | 58 | } |
michael@0 | 59 | |
michael@0 | 60 | protected: |
michael@0 | 61 | template <class Method, class Params> |
michael@0 | 62 | class RunnableMethod : public Task { |
michael@0 | 63 | public: |
michael@0 | 64 | RunnableMethod() { } |
michael@0 | 65 | |
michael@0 | 66 | void Init(T* obj, Method meth, const Params& params) { |
michael@0 | 67 | obj_ = obj; |
michael@0 | 68 | meth_ = meth; |
michael@0 | 69 | params_ = params; |
michael@0 | 70 | } |
michael@0 | 71 | |
michael@0 | 72 | virtual void Run() { DispatchToMethod(obj_, meth_, params_); } |
michael@0 | 73 | |
michael@0 | 74 | private: |
michael@0 | 75 | T* obj_; |
michael@0 | 76 | Method meth_; |
michael@0 | 77 | Params params_; |
michael@0 | 78 | }; |
michael@0 | 79 | |
michael@0 | 80 | private: |
michael@0 | 81 | T* object_; |
michael@0 | 82 | }; |
michael@0 | 83 | |
michael@0 | 84 | } // namespace plugins |
michael@0 | 85 | } // namespace mozilla |
michael@0 | 86 | |
michael@0 | 87 | #endif // mozilla_plugins_ScopedMethodFactory_h |