From add8d8267f2a4afa1fe2a02a5e5534da6fe1ff38 Mon Sep 17 00:00:00 2001 From: Botond Ballo Date: Wed, 16 Sep 2015 20:29:41 -0400 Subject: [PATCH] Bug 1200063 - Add a generic implementation of nsITimerCallback that's usable with a lambda or other function object. r=kats We can consider moving this to xpcom in the future. --- gfx/layers/apz/util/APZThreadUtils.cpp | 2 ++ gfx/layers/apz/util/APZThreadUtils.h | 41 ++++++++++++++++++++++++++ 2 files changed, 43 insertions(+) diff --git a/gfx/layers/apz/util/APZThreadUtils.cpp b/gfx/layers/apz/util/APZThreadUtils.cpp index f7c42688a7e..25ab61d8557 100644 --- a/gfx/layers/apz/util/APZThreadUtils.cpp +++ b/gfx/layers/apz/util/APZThreadUtils.cpp @@ -81,5 +81,7 @@ APZThreadUtils::RunOnControllerThread(Task* aTask) #endif } +NS_IMPL_ISUPPORTS(GenericTimerCallbackBase, nsITimerCallback) + } // namespace layers } // namespace mozilla diff --git a/gfx/layers/apz/util/APZThreadUtils.h b/gfx/layers/apz/util/APZThreadUtils.h index 1457482a6ac..258d22f5d85 100644 --- a/gfx/layers/apz/util/APZThreadUtils.h +++ b/gfx/layers/apz/util/APZThreadUtils.h @@ -7,6 +7,7 @@ #define mozilla_layers_APZThreadUtils_h #include "base/message_loop.h" +#include "nsITimer.h" class Task; @@ -51,6 +52,46 @@ public: static void RunOnControllerThread(Task* aTask); }; +// A base class for GenericTimerCallback. +// This is necessary because NS_IMPL_ISUPPORTS doesn't work for a class +// template. +class GenericTimerCallbackBase : public nsITimerCallback +{ +public: + NS_DECL_THREADSAFE_ISUPPORTS + +protected: + virtual ~GenericTimerCallbackBase() {} +}; + +// An nsITimerCallback implementation that can be used with any function +// object that's callable with no arguments. +template +class GenericTimerCallback final : public GenericTimerCallbackBase +{ +public: + explicit GenericTimerCallback(const Function& aFunction) : mFunction(aFunction) {} + + NS_IMETHODIMP Notify(nsITimer*) override + { + mFunction(); + return NS_OK; + } +private: + Function mFunction; +}; + +// Convenience function for constructing a GenericTimerCallback. +// Returns a raw pointer, suitable for passing directly as an argument to +// nsITimer::InitWithCallback(). The intention is to enable the following +// terse inline usage: +// timer->InitWithCallback(NewTimerCallback([](){ ... }), delay); +template +GenericTimerCallback* NewTimerCallback(const Function& aFunction) +{ + return new GenericTimerCallback(aFunction); +} + } // namespace layers } // namespace mozilla