michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public
michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0:
michael@0: #ifndef mozilla_plugins_ScopedMethodFactory_h
michael@0: #define mozilla_plugins_ScopedMethodFactory_h
michael@0:
michael@0: #include
michael@0:
michael@0: /*
michael@0: * This is based on the ScopedRunnableMethodFactory from ipc/chromium/src/base/task.h
michael@0: * Chromiums factories assert if tasks are created and run on different threads,
michael@0: * which is something we need to do in PluginModuleParent (hang UI vs. main thread).
michael@0: * ScopedMethodFactory just provides cancellable tasks that don't assert this.
michael@0: */
michael@0:
michael@0: namespace mozilla {
michael@0: namespace plugins {
michael@0:
michael@0: template
michael@0: class ScopedMethodFactory : public RevocableStore
michael@0: {
michael@0: private:
michael@0: template
michael@0: class TaskWrapper : public TaskType
michael@0: {
michael@0: public:
michael@0: explicit TaskWrapper(RevocableStore* store) : revocable_(store) { }
michael@0:
michael@0: virtual void Run() {
michael@0: if (!revocable_.revoked())
michael@0: TaskType::Run();
michael@0: }
michael@0:
michael@0: private:
michael@0: Revocable revocable_;
michael@0: };
michael@0:
michael@0: public:
michael@0: explicit ScopedMethodFactory(T* object) : object_(object) { }
michael@0:
michael@0: template
michael@0: inline Task* NewRunnableMethod(Method method) {
michael@0: typedef TaskWrapper > TaskWrapper;
michael@0:
michael@0: TaskWrapper* task = new TaskWrapper(this);
michael@0: task->Init(object_, method, MakeTuple());
michael@0: return task;
michael@0: }
michael@0:
michael@0: template
michael@0: inline Task* NewRunnableMethod(Method method, const A& a) {
michael@0: typedef TaskWrapper > > TaskWrapper;
michael@0:
michael@0: TaskWrapper* task = new TaskWrapper(this);
michael@0: task->Init(object_, method, MakeTuple(a));
michael@0: return task;
michael@0: }
michael@0:
michael@0: protected:
michael@0: template
michael@0: class RunnableMethod : public Task {
michael@0: public:
michael@0: RunnableMethod() { }
michael@0:
michael@0: void Init(T* obj, Method meth, const Params& params) {
michael@0: obj_ = obj;
michael@0: meth_ = meth;
michael@0: params_ = params;
michael@0: }
michael@0:
michael@0: virtual void Run() { DispatchToMethod(obj_, meth_, params_); }
michael@0:
michael@0: private:
michael@0: T* obj_;
michael@0: Method meth_;
michael@0: Params params_;
michael@0: };
michael@0:
michael@0: private:
michael@0: T* object_;
michael@0: };
michael@0:
michael@0: } // namespace plugins
michael@0: } // namespace mozilla
michael@0:
michael@0: #endif // mozilla_plugins_ScopedMethodFactory_h