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