|
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/. */ |
|
4 |
|
5 #ifndef mozilla_plugins_ScopedMethodFactory_h |
|
6 #define mozilla_plugins_ScopedMethodFactory_h |
|
7 |
|
8 #include <base/task.h> |
|
9 |
|
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 */ |
|
16 |
|
17 namespace mozilla { |
|
18 namespace plugins { |
|
19 |
|
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) { } |
|
29 |
|
30 virtual void Run() { |
|
31 if (!revocable_.revoked()) |
|
32 TaskType::Run(); |
|
33 } |
|
34 |
|
35 private: |
|
36 Revocable revocable_; |
|
37 }; |
|
38 |
|
39 public: |
|
40 explicit ScopedMethodFactory(T* object) : object_(object) { } |
|
41 |
|
42 template <class Method> |
|
43 inline Task* NewRunnableMethod(Method method) { |
|
44 typedef TaskWrapper<RunnableMethod<Method, Tuple0> > TaskWrapper; |
|
45 |
|
46 TaskWrapper* task = new TaskWrapper(this); |
|
47 task->Init(object_, method, MakeTuple()); |
|
48 return task; |
|
49 } |
|
50 |
|
51 template <class Method, class A> |
|
52 inline Task* NewRunnableMethod(Method method, const A& a) { |
|
53 typedef TaskWrapper<RunnableMethod<Method, Tuple1<A> > > TaskWrapper; |
|
54 |
|
55 TaskWrapper* task = new TaskWrapper(this); |
|
56 task->Init(object_, method, MakeTuple(a)); |
|
57 return task; |
|
58 } |
|
59 |
|
60 protected: |
|
61 template <class Method, class Params> |
|
62 class RunnableMethod : public Task { |
|
63 public: |
|
64 RunnableMethod() { } |
|
65 |
|
66 void Init(T* obj, Method meth, const Params& params) { |
|
67 obj_ = obj; |
|
68 meth_ = meth; |
|
69 params_ = params; |
|
70 } |
|
71 |
|
72 virtual void Run() { DispatchToMethod(obj_, meth_, params_); } |
|
73 |
|
74 private: |
|
75 T* obj_; |
|
76 Method meth_; |
|
77 Params params_; |
|
78 }; |
|
79 |
|
80 private: |
|
81 T* object_; |
|
82 }; |
|
83 |
|
84 } // namespace plugins |
|
85 } // namespace mozilla |
|
86 |
|
87 #endif // mozilla_plugins_ScopedMethodFactory_h |