diff options
43 files changed, 638 insertions, 597 deletions
diff --git a/gpu/demos/framework/pepper.cc b/gpu/demos/framework/pepper.cc index 831e094..462679e 100644 --- a/gpu/demos/framework/pepper.cc +++ b/gpu/demos/framework/pepper.cc @@ -13,6 +13,7 @@ #include "ppapi/cpp/rect.h" #include "ppapi/cpp/size.h" #include "ppapi/lib/gl/gles2/gl2ext_ppapi.h" +#include "ppapi/utility/completion_callback_factory.h" namespace gpu { namespace demos { diff --git a/ppapi/cpp/completion_callback.h b/ppapi/cpp/completion_callback.h index 2e5f38b..b4564e1 100644 --- a/ppapi/cpp/completion_callback.h +++ b/ppapi/cpp/completion_callback.h @@ -8,7 +8,7 @@ #include "ppapi/c/pp_completion_callback.h" #include "ppapi/c/pp_errors.h" #include "ppapi/cpp/logging.h" -#include "ppapi/cpp/non_thread_safe_ref_count.h" +#include "ppapi/cpp/module.h" /// @file /// This file defines the API to create and run a callback. @@ -156,555 +156,6 @@ class CompletionCallback { /// @return A <code>CompletionCallback</code> corresponding to a NULL callback. CompletionCallback BlockUntilComplete(); -/// CompletionCallbackFactory<T> may be used to create CompletionCallback -/// objects that are bound to member functions. -/// -/// If a factory is destroyed, then any pending callbacks will be cancelled -/// preventing any bound member functions from being called. The CancelAll() -/// method allows pending callbacks to be cancelled without destroying the -/// factory. -/// -/// <strong>Note: </strong><code>CompletionCallbackFactory<T></code> isn't -/// thread safe, but you can make it more thread-friendly by passing a -/// thread-safe refcounting class as the second template element. However, it -/// only guarantees safety for creating a callback from another thread, the -/// callback itself needs to execute on the same thread as the thread that -/// creates/destroys the factory. With this restriction, it is safe to create -/// the <code>CompletionCallbackFactory</code> on the main thread, create -/// callbacks from any thread and pass them to CallOnMainThread(). -/// -/// <strong>Example: </strong> -/// -/// @code -/// -/// class MyHandler { -/// public: -/// // If an compiler warns on following using |this| in the initializer -/// // list, use PP_ALLOW_THIS_IN_INITIALIZER_LIST macro. -/// MyHandler() : factory_(this), offset_(0) { -/// } -/// -/// void ProcessFile(const FileRef& file) { -/// CompletionCallback cc = factory_.NewRequiredCallback( -/// &MyHandler::DidOpen); -/// int32_t rv = fio_.Open(file, PP_FileOpenFlag_Read, cc); -/// CHECK(rv == PP_OK_COMPLETIONPENDING); -/// } -/// -/// private: -/// CompletionCallback NewCallback() { -/// return factory_.NewCallback(&MyHandler::DidCompleteIO); -/// } -/// -/// void DidOpen(int32_t result) { -/// if (result == PP_OK) { -/// // The file is open, and we can begin reading. -/// offset_ = 0; -/// ReadMore(); -/// } else { -/// // Failed to open the file with error given by 'result'. -/// } -/// } -/// -/// void DidRead(int32_t result) { -/// if (result > 0) { -/// // buf_ now contains 'result' number of bytes from the file. -/// ProcessBytes(buf_, result); -/// offset_ += result; -/// ReadMore(); -/// } else { -/// // Done reading (possibly with an error given by 'result'). -/// } -/// } -/// -/// void ReadMore() { -/// CompletionCallback cc = -/// factory_.NewOptionalCallback(&MyHandler::DidRead); -/// int32_t rv = fio_.Read(offset_, buf_, sizeof(buf_), -/// cc.pp_completion_callback()); -/// if (rv != PP_OK_COMPLETIONPENDING) -/// cc.Run(rv); -/// } -/// -/// void ProcessBytes(const char* bytes, int32_t length) { -/// // Do work ... -/// } -/// -/// pp::CompletionCallbackFactory<MyHandler> factory_; -/// pp::FileIO fio_; -/// char buf_[4096]; -/// int64_t offset_; -/// }; -/// -/// @endcode -/// -template <typename T, typename RefCount = NonThreadSafeRefCount> -class CompletionCallbackFactory { - public: - - /// This constructor creates a <code>CompletionCallbackFactory</code> - /// bound to an object. If the constructor is called without an argument, - /// the default value of <code>NULL</code> is used. The user then must call - /// Initialize() to initialize the object. - /// - /// param[in] object Optional parameter. An object whose member functions - /// are to be bound to CompletionCallbacks created by this - /// <code>CompletionCallbackFactory</code>. The default value of this - /// parameter is <code>NULL</code>. - explicit CompletionCallbackFactory(T* object = NULL) - : object_(object) { - InitBackPointer(); - } - - /// Destructor. - ~CompletionCallbackFactory() { - ResetBackPointer(); - } - - /// CancelAll() cancels all <code>CompletionCallbacks</code> allocated from - /// this factory. - void CancelAll() { - ResetBackPointer(); - InitBackPointer(); - } - /// Initialize() binds the <code>CallbackFactory</code> to a particular - /// object. Use this when the object is not available at - /// <code>CallbackFactory</code> creation, and the <code>NULL</code> default - /// is passed to the constructor. The object may only be initialized once, - /// either by the constructor, or by a call to Initialize(). - /// - /// @param[in] object The object whose member functions are to be bound to - /// the <code>CompletionCallback</code> created by this - /// <code>CompletionCallbackFactory</code>. - void Initialize(T* object) { - PP_DCHECK(object); - PP_DCHECK(!object_); // May only initialize once! - object_ = object; - } - - /// GetObject() returns the object that was passed at initialization to - /// Intialize(). - /// - /// @return the object passed to the constructor or Intialize(). - T* GetObject() { - return object_; - } - - /// NewCallback allocates a new, single-use <code>CompletionCallback</code>. - /// The <code>CompletionCallback</code> must be run in order for the memory - /// allocated by the methods to be freed. - /// NewCallback() is equivalent to NewRequiredCallback() below. - /// - /// @param[in] method The method to be invoked upon completion of the - /// operation. - /// - /// @return A <code>CompletionCallback</code>. - template <typename Method> - CompletionCallback NewCallback(Method method) { - PP_DCHECK(object_); - return NewCallbackHelper(Dispatcher0<Method>(method)); - } - - /// NewRequiredCallback() allocates a new, single-use - /// <code>CompletionCallback</code> that will always run. The - /// <code>CompletionCallback</code> must be run in order for the memory - /// allocated by the methods to be freed. - /// - /// @param[in] method The method to be invoked upon completion of the - /// operation. - /// - /// @return A <code>CompletionCallback</code>. - template <typename Method> - CompletionCallback NewRequiredCallback(Method method) { - CompletionCallback cc = NewCallback(method); - cc.set_flags(cc.flags() & ~PP_COMPLETIONCALLBACK_FLAG_OPTIONAL); - return cc; - } - - /// NewOptionalCallback() allocates a new, single-use - /// <code>CompletionCallback</code> that might not run if the method - /// taking it can complete synchronously. Thus, if after passing the - /// CompletionCallback to a Pepper method, the method does not return - /// PP_OK_COMPLETIONPENDING, then you should manually call the - /// CompletionCallback's Run method, or memory will be leaked. - /// - /// @param[in] method The method to be invoked upon completion of the - /// operation. - /// - /// @return A <code>CompletionCallback</code>. - template <typename Method> - CompletionCallback NewOptionalCallback(Method method) { - CompletionCallback cc = NewCallback(method); - cc.set_flags(cc.flags() | PP_COMPLETIONCALLBACK_FLAG_OPTIONAL); - return cc; - } - - /// NewCallback() allocates a new, single-use <code>CompletionCallback</code>. - /// The <code>CompletionCallback</code> must be run in order for the memory - /// allocated by the methods to be freed. - /// NewCallback() is equivalent to NewRequiredCallback() below. - /// - /// @param[in] method The method to be invoked upon completion of the - /// operation. Method should be of type: - /// <code>void (T::*)(int32_t result, const A& a)</code> - /// - /// @param[in] a Passed to <code>method</code> when the completion callback - /// runs. - /// - /// @return A <code>CompletionCallback</code>. - template <typename Method, typename A> - CompletionCallback NewCallback(Method method, const A& a) { - PP_DCHECK(object_); - return NewCallbackHelper(Dispatcher1<Method, A>(method, a)); - } - - /// NewRequiredCallback() allocates a new, single-use - /// <code>CompletionCallback</code> that will always run. The - /// <code>CompletionCallback</code> must be run in order for the memory - /// allocated by the methods to be freed. - /// - /// @param[in] method The method to be invoked upon completion of the - /// operation. Method should be of type: - /// <code>void (T::*)(int32_t result, const A& a)</code> - /// - /// @param[in] a Passed to <code>method</code> when the completion callback - /// runs. - /// - /// @return A <code>CompletionCallback</code>. - template <typename Method, typename A> - CompletionCallback NewRequiredCallback(Method method, const A& a) { - CompletionCallback cc = NewCallback(method, a); - cc.set_flags(cc.flags() & ~PP_COMPLETIONCALLBACK_FLAG_OPTIONAL); - return cc; - } - - /// NewOptionalCallback() allocates a new, single-use - /// <code>CompletionCallback</code> that might not run if the method - /// taking it can complete synchronously. Thus, if after passing the - /// CompletionCallback to a Pepper method, the method does not return - /// PP_OK_COMPLETIONPENDING, then you should manually call the - /// CompletionCallback's Run method, or memory will be leaked. - /// - /// @param[in] method The method to be invoked upon completion of the - /// operation. Method should be of type: - /// <code>void (T::*)(int32_t result, const A& a)</code> - /// - /// @param[in] a Passed to <code>method</code> when the completion callback - /// runs. - /// - /// @return A <code>CompletionCallback</code>. - template <typename Method, typename A> - CompletionCallback NewOptionalCallback(Method method, const A& a) { - CompletionCallback cc = NewCallback(method, a); - cc.set_flags(cc.flags() | PP_COMPLETIONCALLBACK_FLAG_OPTIONAL); - return cc; - } - - /// NewCallback() allocates a new, single-use - /// <code>CompletionCallback</code>. - /// The <code>CompletionCallback</code> must be run in order for the memory - /// allocated by the methods to be freed. - /// NewCallback() is equivalent to NewRequiredCallback() below. - /// - /// @param method The method taking the callback. Method should be of type: - /// <code>void (T::*)(int32_t result, const A& a, const B& b)</code> - /// - /// @param[in] a Passed to <code>method</code> when the completion callback - /// runs. - /// - /// @param[in] b Passed to <code>method</code> when the completion callback - /// runs. - /// - /// @return A <code>CompletionCallback</code>. - template <typename Method, typename A, typename B> - CompletionCallback NewCallback(Method method, const A& a, const B& b) { - PP_DCHECK(object_); - return NewCallbackHelper(Dispatcher2<Method, A, B>(method, a, b)); - } - - /// NewRequiredCallback() allocates a new, single-use - /// <code>CompletionCallback</code> that will always run. The - /// <code>CompletionCallback</code> must be run in order for the memory - /// allocated by the methods to be freed. - /// - /// @param method The method taking the callback. Method should be of type: - /// <code>void (T::*)(int32_t result, const A& a, const B& b)</code> - /// - /// @param[in] a Passed to <code>method</code> when the completion callback - /// runs. - /// - /// @param[in] b Passed to <code>method</code> when the completion callback - /// runs. - /// - /// @return A <code>CompletionCallback</code>. - template <typename Method, typename A, typename B> - CompletionCallback NewRequiredCallback(Method method, const A& a, - const B& b) { - CompletionCallback cc = NewCallback(method, a, b); - cc.set_flags(cc.flags() & ~PP_COMPLETIONCALLBACK_FLAG_OPTIONAL); - return cc; - } - - /// NewOptionalCallback() allocates a new, single-use - /// <code>CompletionCallback</code> that might not run if the method - /// taking it can complete synchronously. Thus, if after passing the - /// CompletionCallback to a Pepper method, the method does not return - /// PP_OK_COMPLETIONPENDING, then you should manually call the - /// CompletionCallback's Run method, or memory will be leaked. - /// - /// @param[in] method The method taking the callback. Method should be of - /// type: - /// <code>void (T::*)(int32_t result, const A& a, const B& b)</code> - /// - /// @param[in] a Passed to <code>method</code> when the completion callback - /// runs. - /// - /// @param[in] b Passed to <code>method</code> when the completion callback - /// runs. - /// - /// @return A <code>CompletionCallback</code>. - template <typename Method, typename A, typename B> - CompletionCallback NewOptionalCallback(Method method, const A& a, - const B& b) { - CompletionCallback cc = NewCallback(method, a, b); - cc.set_flags(cc.flags() | PP_COMPLETIONCALLBACK_FLAG_OPTIONAL); - return cc; - } - - /// NewCallback() allocates a new, single-use - /// <code>CompletionCallback</code>. - /// The <code>CompletionCallback</code> must be run in order for the memory - /// allocated by the methods to be freed. - /// NewCallback() is equivalent to NewRequiredCallback() below. - /// - /// @param method The method taking the callback. Method should be of type: - /// <code> - /// void (T::*)(int32_t result, const A& a, const B& b, const C& c) - /// </code> - /// - /// @param[in] a Passed to <code>method</code> when the completion callback - /// runs. - /// - /// @param[in] b Passed to <code>method</code> when the completion callback - /// runs. - /// - /// @param[in] c Passed to <code>method</code> when the completion callback - /// runs. - /// - /// @return A <code>CompletionCallback</code>. - template <typename Method, typename A, typename B, typename C> - CompletionCallback NewCallback(Method method, const A& a, const B& b, - const C& c) { - PP_DCHECK(object_); - return NewCallbackHelper(Dispatcher3<Method, A, B, C>(method, a, b, c)); - } - - /// NewRequiredCallback() allocates a new, single-use - /// <code>CompletionCallback</code> that will always run. The - /// <code>CompletionCallback</code> must be run in order for the memory - /// allocated by the methods to be freed. - /// - /// @param method The method taking the callback. Method should be of type: - /// <code> - /// void (T::*)(int32_t result, const A& a, const B& b, const C& c) - /// </code> - /// - /// @param[in] a Passed to <code>method</code> when the completion callback - /// runs. - /// - /// @param[in] b Passed to <code>method</code> when the completion callback - /// runs. - /// - /// @param[in] c Passed to <code>method</code> when the completion callback - /// runs. - /// - /// @return A <code>CompletionCallback</code>. - template <typename Method, typename A, typename B, typename C> - CompletionCallback NewRequiredCallback(Method method, const A& a, - const B& b, const C& c) { - CompletionCallback cc = NewCallback(method, a, b, c); - cc.set_flags(cc.flags() & ~PP_COMPLETIONCALLBACK_FLAG_OPTIONAL); - return cc; - } - - /// NewOptionalCallback() allocates a new, single-use - /// <code>CompletionCallback</code> that might not run if the method - /// taking it can complete synchronously. Thus, if after passing the - /// CompletionCallback to a Pepper method, the method does not return - /// PP_OK_COMPLETIONPENDING, then you should manually call the - /// CompletionCallback's Run method, or memory will be leaked. - /// - /// @param[in] method The method taking the callback. Method should be of - /// type: - /// <code> - /// void (T::*)(int32_t result, const A& a, const B& b, const C& c) - /// </code> - /// - /// @param[in] a Passed to <code>method</code> when the completion callback - /// runs. - /// - /// @param[in] b Passed to <code>method</code> when the completion callback - /// runs. - /// - /// @param[in] c Passed to <code>method</code> when the completion callback - /// runs. - /// - /// @return A <code>CompletionCallback</code>. - template <typename Method, typename A, typename B, typename C> - CompletionCallback NewOptionalCallback(Method method, const A& a, - const B& b, const C& c) { - CompletionCallback cc = NewCallback(method, a, b, c); - cc.set_flags(cc.flags() | PP_COMPLETIONCALLBACK_FLAG_OPTIONAL); - return cc; - } - - private: - class BackPointer { - public: - typedef CompletionCallbackFactory<T, RefCount> FactoryType; - - BackPointer(FactoryType* factory) - : factory_(factory) { - } - - void AddRef() { - ref_.AddRef(); - } - - void Release() { - if (ref_.Release() == 0) - delete this; - } - - void DropFactory() { - factory_ = NULL; - } - - T* GetObject() { - return factory_ ? factory_->GetObject() : NULL; - } - - private: - RefCount ref_; - FactoryType* factory_; - }; - - template <typename Dispatcher> - class CallbackData { - public: - CallbackData(BackPointer* back_pointer, const Dispatcher& dispatcher) - : back_pointer_(back_pointer), - dispatcher_(dispatcher) { - back_pointer_->AddRef(); - } - - ~CallbackData() { - back_pointer_->Release(); - } - - static void Thunk(void* user_data, int32_t result) { - Self* self = static_cast<Self*>(user_data); - T* object = self->back_pointer_->GetObject(); - if (object) - self->dispatcher_(object, result); - delete self; - } - - private: - typedef CallbackData<Dispatcher> Self; - BackPointer* back_pointer_; - Dispatcher dispatcher_; - }; - - template <typename Method> - class Dispatcher0 { - public: - Dispatcher0(Method method) : method_(method) { - } - void operator()(T* object, int32_t result) { - (object->*method_)(result); - } - private: - Method method_; - }; - - template <typename Method, typename A> - class Dispatcher1 { - public: - Dispatcher1(Method method, const A& a) - : method_(method), - a_(a) { - } - void operator()(T* object, int32_t result) { - (object->*method_)(result, a_); - } - private: - Method method_; - A a_; - }; - - template <typename Method, typename A, typename B> - class Dispatcher2 { - public: - Dispatcher2(Method method, const A& a, const B& b) - : method_(method), - a_(a), - b_(b) { - } - void operator()(T* object, int32_t result) { - (object->*method_)(result, a_, b_); - } - private: - Method method_; - A a_; - B b_; - }; - - template <typename Method, typename A, typename B, typename C> - class Dispatcher3 { - public: - Dispatcher3(Method method, const A& a, const B& b, const C& c) - : method_(method), - a_(a), - b_(b), - c_(c) { - } - void operator()(T* object, int32_t result) { - (object->*method_)(result, a_, b_, c_); - } - private: - Method method_; - A a_; - B b_; - C c_; - }; - - void InitBackPointer() { - back_pointer_ = new BackPointer(this); - back_pointer_->AddRef(); - } - - void ResetBackPointer() { - back_pointer_->DropFactory(); - back_pointer_->Release(); - } - - template <typename Dispatcher> - CompletionCallback NewCallbackHelper(const Dispatcher& dispatcher) { - PP_DCHECK(object_); // Expects a non-null object! - return CompletionCallback( - &CallbackData<Dispatcher>::Thunk, - new CallbackData<Dispatcher>(back_pointer_, dispatcher)); - } - - // Disallowed: - CompletionCallbackFactory(const CompletionCallbackFactory&); - CompletionCallbackFactory& operator=(const CompletionCallbackFactory&); - - T* object_; - BackPointer* back_pointer_; -}; - } // namespace pp #endif // PPAPI_CPP_COMPLETION_CALLBACK_H_ diff --git a/ppapi/example/example.cc b/ppapi/example/example.cc index 72ca3ac..6ba7983 100644 --- a/ppapi/example/example.cc +++ b/ppapi/example/example.cc @@ -32,6 +32,7 @@ #include "ppapi/cpp/url_request_info.h" #include "ppapi/cpp/var.h" #include "ppapi/cpp/view.h" +#include "ppapi/utility/completion_callback_factory.h" static const int kStepsPerCircle = 800; diff --git a/ppapi/examples/2d/paint_manager_example.cc b/ppapi/examples/2d/paint_manager_example.cc index fdf5a8b..d3530cb 100644 --- a/ppapi/examples/2d/paint_manager_example.cc +++ b/ppapi/examples/2d/paint_manager_example.cc @@ -8,8 +8,8 @@ #include "ppapi/cpp/input_event.h" #include "ppapi/cpp/instance.h" #include "ppapi/cpp/module.h" -#include "ppapi/cpp/paint_manager.h" #include "ppapi/cpp/size.h" +#include "ppapi/utility/graphics/paint_manager.h" // Number of pixels to each side of the center of the square that we draw. static const int kSquareRadius = 2; diff --git a/ppapi/examples/2d/scroll.cc b/ppapi/examples/2d/scroll.cc index f8c365a..5dffa57 100644 --- a/ppapi/examples/2d/scroll.cc +++ b/ppapi/examples/2d/scroll.cc @@ -4,14 +4,14 @@ #include <math.h> -#include "ppapi/cpp/completion_callback.h" #include "ppapi/cpp/graphics_2d.h" #include "ppapi/cpp/image_data.h" #include "ppapi/cpp/instance.h" #include "ppapi/cpp/module.h" -#include "ppapi/cpp/paint_manager.h" #include "ppapi/cpp/rect.h" #include "ppapi/cpp/var.h" +#include "ppapi/utility/completion_callback_factory.h" +#include "ppapi/utility/graphics/paint_manager.h" static const int kSquareSpacing = 98; static const int kSquareSize = 5; diff --git a/ppapi/examples/audio_input/audio_input.cc b/ppapi/examples/audio_input/audio_input.cc index 13b1237..74bc08e 100644 --- a/ppapi/examples/audio_input/audio_input.cc +++ b/ppapi/examples/audio_input/audio_input.cc @@ -8,7 +8,6 @@ #include <limits> #include "ppapi/cpp/audio_config.h" -#include "ppapi/cpp/completion_callback.h" #include "ppapi/cpp/dev/audio_input_dev.h" #include "ppapi/cpp/graphics_2d.h" #include "ppapi/cpp/image_data.h" @@ -17,6 +16,7 @@ #include "ppapi/cpp/module.h" #include "ppapi/cpp/rect.h" #include "ppapi/cpp/size.h" +#include "ppapi/utility/completion_callback_factory.h" class MyInstance : public pp::Instance { public: diff --git a/ppapi/examples/file_chooser/file_chooser.cc b/ppapi/examples/file_chooser/file_chooser.cc index 8c2e675..136ff31 100644 --- a/ppapi/examples/file_chooser/file_chooser.cc +++ b/ppapi/examples/file_chooser/file_chooser.cc @@ -11,6 +11,7 @@ #include "ppapi/cpp/module.h" #include "ppapi/cpp/private/instance_private.h" #include "ppapi/cpp/private/var_private.h" +#include "ppapi/utility/completion_callback_factory.h" class MyInstance : public pp::InstancePrivate { public: diff --git a/ppapi/examples/gles2/gles2.cc b/ppapi/examples/gles2/gles2.cc index 220a6f4..0de2eda 100644 --- a/ppapi/examples/gles2/gles2.cc +++ b/ppapi/examples/gles2/gles2.cc @@ -24,6 +24,7 @@ #include "ppapi/cpp/var.h" #include "ppapi/examples/gles2/testdata.h" #include "ppapi/lib/gl/include/GLES2/gl2.h" +#include "ppapi/utility/completion_callback_factory.h" // Use assert as a poor-man's CHECK, even in non-debug mode. // Since <assert.h> redefines assert on every inclusion (it doesn't use diff --git a/ppapi/examples/mouse_lock/mouse_lock.cc b/ppapi/examples/mouse_lock/mouse_lock.cc index 68aa4ac..6a934ba 100644 --- a/ppapi/examples/mouse_lock/mouse_lock.cc +++ b/ppapi/examples/mouse_lock/mouse_lock.cc @@ -8,7 +8,6 @@ #include "ppapi/c/dev/ppb_console_dev.h" #include "ppapi/c/ppb_input_event.h" -#include "ppapi/cpp/completion_callback.h" #include "ppapi/cpp/graphics_2d.h" #include "ppapi/cpp/image_data.h" #include "ppapi/cpp/input_event.h" @@ -18,6 +17,7 @@ #include "ppapi/cpp/mouse_lock.h" #include "ppapi/cpp/rect.h" #include "ppapi/cpp/var.h" +#include "ppapi/utility/completion_callback_factory.h" class MyInstance : public pp::Instance, public pp::MouseLock { public: diff --git a/ppapi/examples/url_loader/streaming.cc b/ppapi/examples/url_loader/streaming.cc index b6df6cb..b7186ae 100644 --- a/ppapi/examples/url_loader/streaming.cc +++ b/ppapi/examples/url_loader/streaming.cc @@ -11,12 +11,12 @@ // URLLoader.FinishSthreamingToFile once the "Open" callback is complete, and // then call URLResponseInfo.GetBodyAsFileRef once the file stream is complete. -#include "ppapi/cpp/completion_callback.h" #include "ppapi/cpp/instance.h" #include "ppapi/cpp/module.h" #include "ppapi/cpp/url_loader.h" #include "ppapi/cpp/url_request_info.h" #include "ppapi/cpp/url_response_info.h" +#include "ppapi/utility/completion_callback_factory.h" // Buffer size for reading network data. const int kBufSize = 1024; diff --git a/ppapi/examples/video_capture/video_capture.cc b/ppapi/examples/video_capture/video_capture.cc index 5d474bd..63e2cb8 100644 --- a/ppapi/examples/video_capture/video_capture.cc +++ b/ppapi/examples/video_capture/video_capture.cc @@ -20,6 +20,7 @@ #include "ppapi/cpp/module.h" #include "ppapi/cpp/rect.h" #include "ppapi/lib/gl/include/GLES2/gl2.h" +#include "ppapi/utility/completion_callback_factory.h" // Assert |context_| isn't holding any GL Errors. Done as a macro instead of a // function to preserve line number information in the failure message. diff --git a/ppapi/native_client/src/trusted/plugin/file_downloader.h b/ppapi/native_client/src/trusted/plugin/file_downloader.h index a913527..399db7a 100644 --- a/ppapi/native_client/src/trusted/plugin/file_downloader.h +++ b/ppapi/native_client/src/trusted/plugin/file_downloader.h @@ -11,10 +11,10 @@ #include "native_client/src/include/nacl_string.h" #include "ppapi/c/trusted/ppb_file_io_trusted.h" #include "ppapi/c/trusted/ppb_url_loader_trusted.h" -#include "ppapi/cpp/completion_callback.h" #include "ppapi/cpp/file_io.h" #include "ppapi/cpp/url_loader.h" #include "ppapi/cpp/instance.h" +#include "ppapi/utility/completion_callback_factory.h" namespace plugin { diff --git a/ppapi/native_client/src/trusted/plugin/pnacl_resources.h b/ppapi/native_client/src/trusted/plugin/pnacl_resources.h index f6d35a7..2d8b356 100644 --- a/ppapi/native_client/src/trusted/plugin/pnacl_resources.h +++ b/ppapi/native_client/src/trusted/plugin/pnacl_resources.h @@ -14,8 +14,7 @@ #include "native_client/src/trusted/desc/nacl_desc_wrapper.h" #include "native_client/src/trusted/plugin/delayed_callback.h" #include "native_client/src/trusted/plugin/plugin_error.h" - -#include "ppapi/cpp/completion_callback.h" +#include "ppapi/utility/completion_callback_factory.h" namespace plugin { diff --git a/ppapi/ppapi_sources.gypi b/ppapi/ppapi_sources.gypi index 84b354d..69ed449 100644 --- a/ppapi/ppapi_sources.gypi +++ b/ppapi/ppapi_sources.gypi @@ -151,11 +151,6 @@ 'cpp/module_impl.h', 'cpp/mouse_lock.cc', 'cpp/mouse_lock.h', - 'cpp/non_thread_safe_ref_count.h', - 'cpp/paint_aggregator.cc', - 'cpp/paint_aggregator.h', - 'cpp/paint_manager.cc', - 'cpp/paint_manager.h', 'cpp/point.h', 'cpp/rect.cc', 'cpp/rect.h', @@ -252,6 +247,14 @@ 'cpp/trusted/file_chooser_trusted.h', 'cpp/trusted/file_io_trusted.cc', 'cpp/trusted/file_io_trusted.h', + + # Utility sources. + 'utility/completion_callback_factory.h', + 'utility/non_thread_safe_ref_count.h', + 'utility/graphics/paint_aggregator.cc', + 'utility/graphics/paint_aggregator.h', + 'utility/graphics/paint_manager.cc', + 'utility/graphics/paint_manager.h', ], # # Common Testing source for trusted and untrusted (NaCl) pugins. diff --git a/ppapi/proxy/ppb_audio_input_proxy.h b/ppapi/proxy/ppb_audio_input_proxy.h index 6244e55..5fb6637 100644 --- a/ppapi/proxy/ppb_audio_input_proxy.h +++ b/ppapi/proxy/ppb_audio_input_proxy.h @@ -13,9 +13,9 @@ #include "ipc/ipc_platform_file.h" #include "ppapi/c/dev/ppb_audio_input_dev.h" #include "ppapi/c/ppb_audio_config.h" -#include "ppapi/cpp/completion_callback.h" #include "ppapi/proxy/interface_proxy.h" #include "ppapi/proxy/proxy_non_thread_safe_ref_count.h" +#include "ppapi/utility/completion_callback_factory.h" struct PPB_AudioInput_Dev; diff --git a/ppapi/proxy/ppb_audio_proxy.h b/ppapi/proxy/ppb_audio_proxy.h index feee8dc..590f7e7 100644 --- a/ppapi/proxy/ppb_audio_proxy.h +++ b/ppapi/proxy/ppb_audio_proxy.h @@ -16,9 +16,9 @@ #include "ppapi/c/pp_resource.h" #include "ppapi/c/ppb_audio.h" #include "ppapi/c/ppb_audio_config.h" -#include "ppapi/cpp/completion_callback.h" #include "ppapi/proxy/interface_proxy.h" #include "ppapi/proxy/proxy_non_thread_safe_ref_count.h" +#include "ppapi/utility/completion_callback_factory.h" struct PPB_Audio; diff --git a/ppapi/proxy/ppb_broker_proxy.h b/ppapi/proxy/ppb_broker_proxy.h index d2f039a..a04903c7 100644 --- a/ppapi/proxy/ppb_broker_proxy.h +++ b/ppapi/proxy/ppb_broker_proxy.h @@ -8,9 +8,9 @@ #include "base/sync_socket.h" #include "ipc/ipc_platform_file.h" #include "ppapi/c/pp_instance.h" -#include "ppapi/cpp/completion_callback.h" #include "ppapi/proxy/interface_proxy.h" #include "ppapi/proxy/proxy_non_thread_safe_ref_count.h" +#include "ppapi/utility/completion_callback_factory.h" namespace ppapi { diff --git a/ppapi/proxy/ppb_file_chooser_proxy.h b/ppapi/proxy/ppb_file_chooser_proxy.h index 887a32b..da45a2c 100644 --- a/ppapi/proxy/ppb_file_chooser_proxy.h +++ b/ppapi/proxy/ppb_file_chooser_proxy.h @@ -10,10 +10,10 @@ #include "base/basictypes.h" #include "ppapi/c/pp_instance.h" -#include "ppapi/cpp/completion_callback.h" #include "ppapi/proxy/interface_proxy.h" #include "ppapi/proxy/proxy_non_thread_safe_ref_count.h" #include "ppapi/thunk/ppb_file_chooser_api.h" +#include "ppapi/utility/completion_callback_factory.h" namespace ppapi { diff --git a/ppapi/proxy/ppb_file_io_proxy.h b/ppapi/proxy/ppb_file_io_proxy.h index fbc8a16..86d822c 100644 --- a/ppapi/proxy/ppb_file_io_proxy.h +++ b/ppapi/proxy/ppb_file_io_proxy.h @@ -11,6 +11,7 @@ #include "ppapi/c/pp_file_info.h" #include "ppapi/proxy/interface_proxy.h" #include "ppapi/proxy/proxy_non_thread_safe_ref_count.h" +#include "ppapi/utility/completion_callback_factory.h" namespace ppapi { diff --git a/ppapi/proxy/ppb_file_ref_proxy.h b/ppapi/proxy/ppb_file_ref_proxy.h index c0fa23c..d15536e 100644 --- a/ppapi/proxy/ppb_file_ref_proxy.h +++ b/ppapi/proxy/ppb_file_ref_proxy.h @@ -11,9 +11,9 @@ #include "ppapi/c/pp_module.h" #include "ppapi/c/pp_resource.h" #include "ppapi/c/pp_time.h" -#include "ppapi/cpp/completion_callback.h" #include "ppapi/proxy/interface_proxy.h" #include "ppapi/proxy/proxy_non_thread_safe_ref_count.h" +#include "ppapi/utility/completion_callback_factory.h" namespace ppapi { diff --git a/ppapi/proxy/ppb_file_system_proxy.h b/ppapi/proxy/ppb_file_system_proxy.h index a07b824..86eb48e 100644 --- a/ppapi/proxy/ppb_file_system_proxy.h +++ b/ppapi/proxy/ppb_file_system_proxy.h @@ -12,9 +12,9 @@ #include "ppapi/c/pp_resource.h" #include "ppapi/c/pp_time.h" #include "ppapi/c/ppb_file_system.h" -#include "ppapi/cpp/completion_callback.h" #include "ppapi/proxy/interface_proxy.h" #include "ppapi/proxy/proxy_non_thread_safe_ref_count.h" +#include "ppapi/utility/completion_callback_factory.h" namespace ppapi { diff --git a/ppapi/proxy/ppb_flash_menu_proxy.h b/ppapi/proxy/ppb_flash_menu_proxy.h index 7e72fde..f0b132e 100644 --- a/ppapi/proxy/ppb_flash_menu_proxy.h +++ b/ppapi/proxy/ppb_flash_menu_proxy.h @@ -7,6 +7,7 @@ #include "ppapi/proxy/interface_proxy.h" #include "ppapi/proxy/proxy_non_thread_safe_ref_count.h" +#include "ppapi/utility/completion_callback_factory.h" struct PP_Flash_Menu; struct PP_Point; diff --git a/ppapi/proxy/ppb_flash_net_connector_proxy.h b/ppapi/proxy/ppb_flash_net_connector_proxy.h index 0f24466..8d290a1 100644 --- a/ppapi/proxy/ppb_flash_net_connector_proxy.h +++ b/ppapi/proxy/ppb_flash_net_connector_proxy.h @@ -8,9 +8,9 @@ #include "base/platform_file.h" #include "ipc/ipc_platform_file.h" #include "ppapi/c/pp_instance.h" -#include "ppapi/cpp/completion_callback.h" #include "ppapi/proxy/interface_proxy.h" #include "ppapi/proxy/proxy_non_thread_safe_ref_count.h" +#include "ppapi/utility/completion_callback_factory.h" struct PPB_Flash_NetConnector; diff --git a/ppapi/proxy/ppb_graphics_2d_proxy.h b/ppapi/proxy/ppb_graphics_2d_proxy.h index 2897595..a8227f0 100644 --- a/ppapi/proxy/ppb_graphics_2d_proxy.h +++ b/ppapi/proxy/ppb_graphics_2d_proxy.h @@ -12,10 +12,10 @@ #include "ppapi/c/pp_resource.h" #include "ppapi/c/pp_size.h" #include "ppapi/c/pp_var.h" -#include "ppapi/cpp/completion_callback.h" #include "ppapi/proxy/interface_proxy.h" #include "ppapi/proxy/proxy_non_thread_safe_ref_count.h" #include "ppapi/shared_impl/host_resource.h" +#include "ppapi/utility/completion_callback_factory.h" struct PPB_Graphics2D; struct PP_Point; diff --git a/ppapi/proxy/ppb_graphics_3d_proxy.h b/ppapi/proxy/ppb_graphics_3d_proxy.h index 5a6c334..b8c8d09 100644 --- a/ppapi/proxy/ppb_graphics_3d_proxy.h +++ b/ppapi/proxy/ppb_graphics_3d_proxy.h @@ -11,11 +11,11 @@ #include "gpu/command_buffer/common/command_buffer.h" #include "ppapi/c/pp_graphics_3d.h" #include "ppapi/c/pp_instance.h" -#include "ppapi/cpp/completion_callback.h" #include "ppapi/proxy/interface_proxy.h" #include "ppapi/proxy/proxy_non_thread_safe_ref_count.h" #include "ppapi/shared_impl/ppb_graphics_3d_shared.h" #include "ppapi/shared_impl/resource.h" +#include "ppapi/utility/completion_callback_factory.h" namespace ppapi { diff --git a/ppapi/proxy/ppb_instance_proxy.h b/ppapi/proxy/ppb_instance_proxy.h index de10f80..a88bd6f 100644 --- a/ppapi/proxy/ppb_instance_proxy.h +++ b/ppapi/proxy/ppb_instance_proxy.h @@ -8,13 +8,13 @@ #include "ppapi/c/pp_instance.h" #include "ppapi/c/pp_resource.h" #include "ppapi/c/pp_var.h" -#include "ppapi/cpp/completion_callback.h" #include "ppapi/proxy/interface_proxy.h" #include "ppapi/proxy/proxy_non_thread_safe_ref_count.h" #include "ppapi/shared_impl/function_group_base.h" #include "ppapi/shared_impl/host_resource.h" #include "ppapi/shared_impl/ppb_instance_shared.h" #include "ppapi/thunk/ppb_instance_api.h" +#include "ppapi/utility/completion_callback_factory.h" // Windows headers interfere with this file. #ifdef PostMessage diff --git a/ppapi/proxy/ppb_url_loader_proxy.h b/ppapi/proxy/ppb_url_loader_proxy.h index e13b1a3..da35350 100644 --- a/ppapi/proxy/ppb_url_loader_proxy.h +++ b/ppapi/proxy/ppb_url_loader_proxy.h @@ -11,10 +11,10 @@ #include "ppapi/c/pp_resource.h" #include "ppapi/c/pp_size.h" #include "ppapi/c/pp_var.h" -#include "ppapi/cpp/completion_callback.h" #include "ppapi/proxy/interface_proxy.h" #include "ppapi/proxy/proxy_non_thread_safe_ref_count.h" #include "ppapi/shared_impl/host_resource.h" +#include "ppapi/utility/completion_callback_factory.h" struct PPB_URLLoader; struct PPB_URLLoaderTrusted; diff --git a/ppapi/proxy/ppb_video_decoder_proxy.h b/ppapi/proxy/ppb_video_decoder_proxy.h index 1185d16..7461a1f 100644 --- a/ppapi/proxy/ppb_video_decoder_proxy.h +++ b/ppapi/proxy/ppb_video_decoder_proxy.h @@ -6,11 +6,11 @@ #define PPAPI_PROXY_PPB_VIDEO_DECODER_PROXY_H_ #include "ppapi/c/pp_instance.h" -#include "ppapi/cpp/completion_callback.h" #include "ppapi/proxy/interface_proxy.h" #include "ppapi/proxy/proxy_non_thread_safe_ref_count.h" #include "ppapi/shared_impl/ppb_video_decoder_shared.h" #include "ppapi/thunk/ppb_video_decoder_api.h" +#include "ppapi/utility/completion_callback_factory.h" namespace ppapi { namespace proxy { diff --git a/ppapi/tests/DEPS b/ppapi/tests/DEPS index d679198..e901f41 100644 --- a/ppapi/tests/DEPS +++ b/ppapi/tests/DEPS @@ -10,6 +10,7 @@ include_rules = [ "+ppapi/c", "+ppapi/cpp", "+ppapi/tests", + "+ppapi/utility", ] # checkdeps.py shouldn't check include paths for files in clang, which aren't # part of the chrome build. diff --git a/ppapi/tests/all_cpp_includes.h b/ppapi/tests/all_cpp_includes.h index ff408b0..8ba62a1 100644 --- a/ppapi/tests/all_cpp_includes.h +++ b/ppapi/tests/all_cpp_includes.h @@ -47,9 +47,6 @@ #include "ppapi/cpp/module.h" #include "ppapi/cpp/module_impl.h" #include "ppapi/cpp/mouse_lock.h" -#include "ppapi/cpp/non_thread_safe_ref_count.h" -#include "ppapi/cpp/paint_aggregator.h" -#include "ppapi/cpp/paint_manager.h" #include "ppapi/cpp/private/flash_fullscreen.h" #include "ppapi/cpp/private/instance_private.h" #include "ppapi/cpp/point.h" @@ -60,5 +57,8 @@ #include "ppapi/cpp/url_request_info.h" #include "ppapi/cpp/url_response_info.h" #include "ppapi/cpp/var.h" +#include "ppapi/utility/graphics/paint_aggregator.h" +#include "ppapi/utility/graphics/paint_manager.h" +#include "ppapi/utility/non_thread_safe_ref_count.h" #endif // PPAPI_TESTS_ALL_CPP_INCLUDES_H_ diff --git a/ppapi/tests/test_flash.h b/ppapi/tests/test_flash.h index 5dfed88..7275ff5 100644 --- a/ppapi/tests/test_flash.h +++ b/ppapi/tests/test_flash.h @@ -7,9 +7,9 @@ #include <string> -#include "ppapi/cpp/completion_callback.h" #include "ppapi/c/pp_stdint.h" #include "ppapi/tests/test_case.h" +#include "ppapi/utility/completion_callback_factory.h" struct PPB_Flash; diff --git a/ppapi/tests/test_paint_aggregator.cc b/ppapi/tests/test_paint_aggregator.cc index 77e2174..a04cff8 100644 --- a/ppapi/tests/test_paint_aggregator.cc +++ b/ppapi/tests/test_paint_aggregator.cc @@ -4,8 +4,8 @@ #include "ppapi/tests/test_paint_aggregator.h" -#include "ppapi/cpp/paint_aggregator.h" #include "ppapi/tests/testing_instance.h" +#include "ppapi/utility/graphics/paint_aggregator.h" REGISTER_TEST_CASE(PaintAggregator); diff --git a/ppapi/tests/test_transport.cc b/ppapi/tests/test_transport.cc index 52d072e..7970050 100644 --- a/ppapi/tests/test_transport.cc +++ b/ppapi/tests/test_transport.cc @@ -13,13 +13,13 @@ #include "ppapi/c/dev/ppb_testing_dev.h" #include "ppapi/c/pp_errors.h" #include "ppapi/c/pp_macros.h" -#include "ppapi/cpp/completion_callback.h" #include "ppapi/cpp/dev/transport_dev.h" #include "ppapi/cpp/instance.h" #include "ppapi/cpp/module.h" #include "ppapi/cpp/var.h" #include "ppapi/tests/test_utils.h" #include "ppapi/tests/testing_instance.h" +#include "ppapi/utility/completion_callback_factory.h" REGISTER_TEST_CASE(Transport); diff --git a/ppapi/tests/testing_instance.h b/ppapi/tests/testing_instance.h index 397c1c7..a112c41 100644 --- a/ppapi/tests/testing_instance.h +++ b/ppapi/tests/testing_instance.h @@ -7,7 +7,7 @@ #include <string> -#include "ppapi/cpp/completion_callback.h" +#include "ppapi/utility/completion_callback_factory.h" #if defined(__native_client__) #include "ppapi/cpp/instance.h" diff --git a/ppapi/utility/DEPS b/ppapi/utility/DEPS new file mode 100644 index 0000000..2983acb --- /dev/null +++ b/ppapi/utility/DEPS @@ -0,0 +1,14 @@ +# ppapi/cpp should not be dependent on other parts of chromium; it should stay +# browser-neutral as much as possible. +include_rules = [ + "-base", + "-build", + "-ipc", + "-ppapi", + "+ppapi/c", + "-ppapi/c/private", + "-ppapi/c/trusted", + "+ppapi/cpp", + "-ppapi/cpp/private", + "-ppapi/cpp/trusted", +] diff --git a/ppapi/utility/README.txt b/ppapi/utility/README.txt new file mode 100644 index 0000000..5b72c93 --- /dev/null +++ b/ppapi/utility/README.txt @@ -0,0 +1,2 @@ +The classes in ppapi/utility are helper classes that provide higher-level
+features on top of the C++ wrappers. Using these classes is optional.
diff --git a/ppapi/utility/completion_callback_factory.h b/ppapi/utility/completion_callback_factory.h new file mode 100644 index 0000000..b656024 --- /dev/null +++ b/ppapi/utility/completion_callback_factory.h @@ -0,0 +1,564 @@ +// Copyright (c) 2011 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#ifndef PPAPI_UTILITY_COMPLETION_CALLBACK_FACTORY_H_
+#define PPAPI_UTILITY_COMPLETION_CALLBACK_FACTORY_H_
+
+#include "ppapi/cpp/completion_callback.h"
+#include "ppapi/utility/non_thread_safe_ref_count.h"
+
+namespace pp {
+
+/// CompletionCallbackFactory<T> may be used to create CompletionCallback
+/// objects that are bound to member functions.
+///
+/// If a factory is destroyed, then any pending callbacks will be cancelled
+/// preventing any bound member functions from being called. The CancelAll()
+/// method allows pending callbacks to be cancelled without destroying the
+/// factory.
+///
+/// <strong>Note: </strong><code>CompletionCallbackFactory<T></code> isn't
+/// thread safe, but you can make it more thread-friendly by passing a
+/// thread-safe refcounting class as the second template element. However, it
+/// only guarantees safety for creating a callback from another thread, the
+/// callback itself needs to execute on the same thread as the thread that
+/// creates/destroys the factory. With this restriction, it is safe to create
+/// the <code>CompletionCallbackFactory</code> on the main thread, create
+/// callbacks from any thread and pass them to CallOnMainThread().
+///
+/// <strong>Example: </strong>
+///
+/// @code
+///
+/// class MyHandler {
+/// public:
+/// // If an compiler warns on following using |this| in the initializer
+/// // list, use PP_ALLOW_THIS_IN_INITIALIZER_LIST macro.
+/// MyHandler() : factory_(this), offset_(0) {
+/// }
+///
+/// void ProcessFile(const FileRef& file) {
+/// CompletionCallback cc = factory_.NewRequiredCallback(
+/// &MyHandler::DidOpen);
+/// int32_t rv = fio_.Open(file, PP_FileOpenFlag_Read, cc);
+/// CHECK(rv == PP_OK_COMPLETIONPENDING);
+/// }
+///
+/// private:
+/// CompletionCallback NewCallback() {
+/// return factory_.NewCallback(&MyHandler::DidCompleteIO);
+/// }
+///
+/// void DidOpen(int32_t result) {
+/// if (result == PP_OK) {
+/// // The file is open, and we can begin reading.
+/// offset_ = 0;
+/// ReadMore();
+/// } else {
+/// // Failed to open the file with error given by 'result'.
+/// }
+/// }
+///
+/// void DidRead(int32_t result) {
+/// if (result > 0) {
+/// // buf_ now contains 'result' number of bytes from the file.
+/// ProcessBytes(buf_, result);
+/// offset_ += result;
+/// ReadMore();
+/// } else {
+/// // Done reading (possibly with an error given by 'result').
+/// }
+/// }
+///
+/// void ReadMore() {
+/// CompletionCallback cc =
+/// factory_.NewOptionalCallback(&MyHandler::DidRead);
+/// int32_t rv = fio_.Read(offset_, buf_, sizeof(buf_),
+/// cc.pp_completion_callback());
+/// if (rv != PP_OK_COMPLETIONPENDING)
+/// cc.Run(rv);
+/// }
+///
+/// void ProcessBytes(const char* bytes, int32_t length) {
+/// // Do work ...
+/// }
+///
+/// pp::CompletionCallbackFactory<MyHandler> factory_;
+/// pp::FileIO fio_;
+/// char buf_[4096];
+/// int64_t offset_;
+/// };
+///
+/// @endcode
+///
+template <typename T, typename RefCount = NonThreadSafeRefCount>
+class CompletionCallbackFactory {
+ public:
+
+ /// This constructor creates a <code>CompletionCallbackFactory</code>
+ /// bound to an object. If the constructor is called without an argument,
+ /// the default value of <code>NULL</code> is used. The user then must call
+ /// Initialize() to initialize the object.
+ ///
+ /// param[in] object Optional parameter. An object whose member functions
+ /// are to be bound to CompletionCallbacks created by this
+ /// <code>CompletionCallbackFactory</code>. The default value of this
+ /// parameter is <code>NULL</code>.
+ explicit CompletionCallbackFactory(T* object = NULL)
+ : object_(object) {
+ InitBackPointer();
+ }
+
+ /// Destructor.
+ ~CompletionCallbackFactory() {
+ ResetBackPointer();
+ }
+
+ /// CancelAll() cancels all <code>CompletionCallbacks</code> allocated from
+ /// this factory.
+ void CancelAll() {
+ ResetBackPointer();
+ InitBackPointer();
+ }
+ /// Initialize() binds the <code>CallbackFactory</code> to a particular
+ /// object. Use this when the object is not available at
+ /// <code>CallbackFactory</code> creation, and the <code>NULL</code> default
+ /// is passed to the constructor. The object may only be initialized once,
+ /// either by the constructor, or by a call to Initialize().
+ ///
+ /// @param[in] object The object whose member functions are to be bound to
+ /// the <code>CompletionCallback</code> created by this
+ /// <code>CompletionCallbackFactory</code>.
+ void Initialize(T* object) {
+ PP_DCHECK(object);
+ PP_DCHECK(!object_); // May only initialize once!
+ object_ = object;
+ }
+
+ /// GetObject() returns the object that was passed at initialization to
+ /// Intialize().
+ ///
+ /// @return the object passed to the constructor or Intialize().
+ T* GetObject() {
+ return object_;
+ }
+
+ /// NewCallback allocates a new, single-use <code>CompletionCallback</code>.
+ /// The <code>CompletionCallback</code> must be run in order for the memory
+ /// allocated by the methods to be freed.
+ /// NewCallback() is equivalent to NewRequiredCallback() below.
+ ///
+ /// @param[in] method The method to be invoked upon completion of the
+ /// operation.
+ ///
+ /// @return A <code>CompletionCallback</code>.
+ template <typename Method>
+ CompletionCallback NewCallback(Method method) {
+ PP_DCHECK(object_);
+ return NewCallbackHelper(Dispatcher0<Method>(method));
+ }
+
+ /// NewRequiredCallback() allocates a new, single-use
+ /// <code>CompletionCallback</code> that will always run. The
+ /// <code>CompletionCallback</code> must be run in order for the memory
+ /// allocated by the methods to be freed.
+ ///
+ /// @param[in] method The method to be invoked upon completion of the
+ /// operation.
+ ///
+ /// @return A <code>CompletionCallback</code>.
+ template <typename Method>
+ CompletionCallback NewRequiredCallback(Method method) {
+ CompletionCallback cc = NewCallback(method);
+ cc.set_flags(cc.flags() & ~PP_COMPLETIONCALLBACK_FLAG_OPTIONAL);
+ return cc;
+ }
+
+ /// NewOptionalCallback() allocates a new, single-use
+ /// <code>CompletionCallback</code> that might not run if the method
+ /// taking it can complete synchronously. Thus, if after passing the
+ /// CompletionCallback to a Pepper method, the method does not return
+ /// PP_OK_COMPLETIONPENDING, then you should manually call the
+ /// CompletionCallback's Run method, or memory will be leaked.
+ ///
+ /// @param[in] method The method to be invoked upon completion of the
+ /// operation.
+ ///
+ /// @return A <code>CompletionCallback</code>.
+ template <typename Method>
+ CompletionCallback NewOptionalCallback(Method method) {
+ CompletionCallback cc = NewCallback(method);
+ cc.set_flags(cc.flags() | PP_COMPLETIONCALLBACK_FLAG_OPTIONAL);
+ return cc;
+ }
+
+ /// NewCallback() allocates a new, single-use <code>CompletionCallback</code>.
+ /// The <code>CompletionCallback</code> must be run in order for the memory
+ /// allocated by the methods to be freed.
+ /// NewCallback() is equivalent to NewRequiredCallback() below.
+ ///
+ /// @param[in] method The method to be invoked upon completion of the
+ /// operation. Method should be of type:
+ /// <code>void (T::*)(int32_t result, const A& a)</code>
+ ///
+ /// @param[in] a Passed to <code>method</code> when the completion callback
+ /// runs.
+ ///
+ /// @return A <code>CompletionCallback</code>.
+ template <typename Method, typename A>
+ CompletionCallback NewCallback(Method method, const A& a) {
+ PP_DCHECK(object_);
+ return NewCallbackHelper(Dispatcher1<Method, A>(method, a));
+ }
+
+ /// NewRequiredCallback() allocates a new, single-use
+ /// <code>CompletionCallback</code> that will always run. The
+ /// <code>CompletionCallback</code> must be run in order for the memory
+ /// allocated by the methods to be freed.
+ ///
+ /// @param[in] method The method to be invoked upon completion of the
+ /// operation. Method should be of type:
+ /// <code>void (T::*)(int32_t result, const A& a)</code>
+ ///
+ /// @param[in] a Passed to <code>method</code> when the completion callback
+ /// runs.
+ ///
+ /// @return A <code>CompletionCallback</code>.
+ template <typename Method, typename A>
+ CompletionCallback NewRequiredCallback(Method method, const A& a) {
+ CompletionCallback cc = NewCallback(method, a);
+ cc.set_flags(cc.flags() & ~PP_COMPLETIONCALLBACK_FLAG_OPTIONAL);
+ return cc;
+ }
+
+ /// NewOptionalCallback() allocates a new, single-use
+ /// <code>CompletionCallback</code> that might not run if the method
+ /// taking it can complete synchronously. Thus, if after passing the
+ /// CompletionCallback to a Pepper method, the method does not return
+ /// PP_OK_COMPLETIONPENDING, then you should manually call the
+ /// CompletionCallback's Run method, or memory will be leaked.
+ ///
+ /// @param[in] method The method to be invoked upon completion of the
+ /// operation. Method should be of type:
+ /// <code>void (T::*)(int32_t result, const A& a)</code>
+ ///
+ /// @param[in] a Passed to <code>method</code> when the completion callback
+ /// runs.
+ ///
+ /// @return A <code>CompletionCallback</code>.
+ template <typename Method, typename A>
+ CompletionCallback NewOptionalCallback(Method method, const A& a) {
+ CompletionCallback cc = NewCallback(method, a);
+ cc.set_flags(cc.flags() | PP_COMPLETIONCALLBACK_FLAG_OPTIONAL);
+ return cc;
+ }
+
+ /// NewCallback() allocates a new, single-use
+ /// <code>CompletionCallback</code>.
+ /// The <code>CompletionCallback</code> must be run in order for the memory
+ /// allocated by the methods to be freed.
+ /// NewCallback() is equivalent to NewRequiredCallback() below.
+ ///
+ /// @param method The method taking the callback. Method should be of type:
+ /// <code>void (T::*)(int32_t result, const A& a, const B& b)</code>
+ ///
+ /// @param[in] a Passed to <code>method</code> when the completion callback
+ /// runs.
+ ///
+ /// @param[in] b Passed to <code>method</code> when the completion callback
+ /// runs.
+ ///
+ /// @return A <code>CompletionCallback</code>.
+ template <typename Method, typename A, typename B>
+ CompletionCallback NewCallback(Method method, const A& a, const B& b) {
+ PP_DCHECK(object_);
+ return NewCallbackHelper(Dispatcher2<Method, A, B>(method, a, b));
+ }
+
+ /// NewRequiredCallback() allocates a new, single-use
+ /// <code>CompletionCallback</code> that will always run. The
+ /// <code>CompletionCallback</code> must be run in order for the memory
+ /// allocated by the methods to be freed.
+ ///
+ /// @param method The method taking the callback. Method should be of type:
+ /// <code>void (T::*)(int32_t result, const A& a, const B& b)</code>
+ ///
+ /// @param[in] a Passed to <code>method</code> when the completion callback
+ /// runs.
+ ///
+ /// @param[in] b Passed to <code>method</code> when the completion callback
+ /// runs.
+ ///
+ /// @return A <code>CompletionCallback</code>.
+ template <typename Method, typename A, typename B>
+ CompletionCallback NewRequiredCallback(Method method, const A& a,
+ const B& b) {
+ CompletionCallback cc = NewCallback(method, a, b);
+ cc.set_flags(cc.flags() & ~PP_COMPLETIONCALLBACK_FLAG_OPTIONAL);
+ return cc;
+ }
+
+ /// NewOptionalCallback() allocates a new, single-use
+ /// <code>CompletionCallback</code> that might not run if the method
+ /// taking it can complete synchronously. Thus, if after passing the
+ /// CompletionCallback to a Pepper method, the method does not return
+ /// PP_OK_COMPLETIONPENDING, then you should manually call the
+ /// CompletionCallback's Run method, or memory will be leaked.
+ ///
+ /// @param[in] method The method taking the callback. Method should be of
+ /// type:
+ /// <code>void (T::*)(int32_t result, const A& a, const B& b)</code>
+ ///
+ /// @param[in] a Passed to <code>method</code> when the completion callback
+ /// runs.
+ ///
+ /// @param[in] b Passed to <code>method</code> when the completion callback
+ /// runs.
+ ///
+ /// @return A <code>CompletionCallback</code>.
+ template <typename Method, typename A, typename B>
+ CompletionCallback NewOptionalCallback(Method method, const A& a,
+ const B& b) {
+ CompletionCallback cc = NewCallback(method, a, b);
+ cc.set_flags(cc.flags() | PP_COMPLETIONCALLBACK_FLAG_OPTIONAL);
+ return cc;
+ }
+
+ /// NewCallback() allocates a new, single-use
+ /// <code>CompletionCallback</code>.
+ /// The <code>CompletionCallback</code> must be run in order for the memory
+ /// allocated by the methods to be freed.
+ /// NewCallback() is equivalent to NewRequiredCallback() below.
+ ///
+ /// @param method The method taking the callback. Method should be of type:
+ /// <code>
+ /// void (T::*)(int32_t result, const A& a, const B& b, const C& c)
+ /// </code>
+ ///
+ /// @param[in] a Passed to <code>method</code> when the completion callback
+ /// runs.
+ ///
+ /// @param[in] b Passed to <code>method</code> when the completion callback
+ /// runs.
+ ///
+ /// @param[in] c Passed to <code>method</code> when the completion callback
+ /// runs.
+ ///
+ /// @return A <code>CompletionCallback</code>.
+ template <typename Method, typename A, typename B, typename C>
+ CompletionCallback NewCallback(Method method, const A& a, const B& b,
+ const C& c) {
+ PP_DCHECK(object_);
+ return NewCallbackHelper(Dispatcher3<Method, A, B, C>(method, a, b, c));
+ }
+
+ /// NewRequiredCallback() allocates a new, single-use
+ /// <code>CompletionCallback</code> that will always run. The
+ /// <code>CompletionCallback</code> must be run in order for the memory
+ /// allocated by the methods to be freed.
+ ///
+ /// @param method The method taking the callback. Method should be of type:
+ /// <code>
+ /// void (T::*)(int32_t result, const A& a, const B& b, const C& c)
+ /// </code>
+ ///
+ /// @param[in] a Passed to <code>method</code> when the completion callback
+ /// runs.
+ ///
+ /// @param[in] b Passed to <code>method</code> when the completion callback
+ /// runs.
+ ///
+ /// @param[in] c Passed to <code>method</code> when the completion callback
+ /// runs.
+ ///
+ /// @return A <code>CompletionCallback</code>.
+ template <typename Method, typename A, typename B, typename C>
+ CompletionCallback NewRequiredCallback(Method method, const A& a,
+ const B& b, const C& c) {
+ CompletionCallback cc = NewCallback(method, a, b, c);
+ cc.set_flags(cc.flags() & ~PP_COMPLETIONCALLBACK_FLAG_OPTIONAL);
+ return cc;
+ }
+
+ /// NewOptionalCallback() allocates a new, single-use
+ /// <code>CompletionCallback</code> that might not run if the method
+ /// taking it can complete synchronously. Thus, if after passing the
+ /// CompletionCallback to a Pepper method, the method does not return
+ /// PP_OK_COMPLETIONPENDING, then you should manually call the
+ /// CompletionCallback's Run method, or memory will be leaked.
+ ///
+ /// @param[in] method The method taking the callback. Method should be of
+ /// type:
+ /// <code>
+ /// void (T::*)(int32_t result, const A& a, const B& b, const C& c)
+ /// </code>
+ ///
+ /// @param[in] a Passed to <code>method</code> when the completion callback
+ /// runs.
+ ///
+ /// @param[in] b Passed to <code>method</code> when the completion callback
+ /// runs.
+ ///
+ /// @param[in] c Passed to <code>method</code> when the completion callback
+ /// runs.
+ ///
+ /// @return A <code>CompletionCallback</code>.
+ template <typename Method, typename A, typename B, typename C>
+ CompletionCallback NewOptionalCallback(Method method, const A& a,
+ const B& b, const C& c) {
+ CompletionCallback cc = NewCallback(method, a, b, c);
+ cc.set_flags(cc.flags() | PP_COMPLETIONCALLBACK_FLAG_OPTIONAL);
+ return cc;
+ }
+
+ private:
+ class BackPointer {
+ public:
+ typedef CompletionCallbackFactory<T, RefCount> FactoryType;
+
+ BackPointer(FactoryType* factory)
+ : factory_(factory) {
+ }
+
+ void AddRef() {
+ ref_.AddRef();
+ }
+
+ void Release() {
+ if (ref_.Release() == 0)
+ delete this;
+ }
+
+ void DropFactory() {
+ factory_ = NULL;
+ }
+
+ T* GetObject() {
+ return factory_ ? factory_->GetObject() : NULL;
+ }
+
+ private:
+ RefCount ref_;
+ FactoryType* factory_;
+ };
+
+ template <typename Dispatcher>
+ class CallbackData {
+ public:
+ CallbackData(BackPointer* back_pointer, const Dispatcher& dispatcher)
+ : back_pointer_(back_pointer),
+ dispatcher_(dispatcher) {
+ back_pointer_->AddRef();
+ }
+
+ ~CallbackData() {
+ back_pointer_->Release();
+ }
+
+ static void Thunk(void* user_data, int32_t result) {
+ Self* self = static_cast<Self*>(user_data);
+ T* object = self->back_pointer_->GetObject();
+ if (object)
+ self->dispatcher_(object, result);
+ delete self;
+ }
+
+ private:
+ typedef CallbackData<Dispatcher> Self;
+ BackPointer* back_pointer_;
+ Dispatcher dispatcher_;
+ };
+
+ template <typename Method>
+ class Dispatcher0 {
+ public:
+ Dispatcher0(Method method) : method_(method) {
+ }
+ void operator()(T* object, int32_t result) {
+ (object->*method_)(result);
+ }
+ private:
+ Method method_;
+ };
+
+ template <typename Method, typename A>
+ class Dispatcher1 {
+ public:
+ Dispatcher1(Method method, const A& a)
+ : method_(method),
+ a_(a) {
+ }
+ void operator()(T* object, int32_t result) {
+ (object->*method_)(result, a_);
+ }
+ private:
+ Method method_;
+ A a_;
+ };
+
+ template <typename Method, typename A, typename B>
+ class Dispatcher2 {
+ public:
+ Dispatcher2(Method method, const A& a, const B& b)
+ : method_(method),
+ a_(a),
+ b_(b) {
+ }
+ void operator()(T* object, int32_t result) {
+ (object->*method_)(result, a_, b_);
+ }
+ private:
+ Method method_;
+ A a_;
+ B b_;
+ };
+
+ template <typename Method, typename A, typename B, typename C>
+ class Dispatcher3 {
+ public:
+ Dispatcher3(Method method, const A& a, const B& b, const C& c)
+ : method_(method),
+ a_(a),
+ b_(b),
+ c_(c) {
+ }
+ void operator()(T* object, int32_t result) {
+ (object->*method_)(result, a_, b_, c_);
+ }
+ private:
+ Method method_;
+ A a_;
+ B b_;
+ C c_;
+ };
+
+ void InitBackPointer() {
+ back_pointer_ = new BackPointer(this);
+ back_pointer_->AddRef();
+ }
+
+ void ResetBackPointer() {
+ back_pointer_->DropFactory();
+ back_pointer_->Release();
+ }
+
+ template <typename Dispatcher>
+ CompletionCallback NewCallbackHelper(const Dispatcher& dispatcher) {
+ PP_DCHECK(object_); // Expects a non-null object!
+ return CompletionCallback(
+ &CallbackData<Dispatcher>::Thunk,
+ new CallbackData<Dispatcher>(back_pointer_, dispatcher));
+ }
+
+ // Disallowed:
+ CompletionCallbackFactory(const CompletionCallbackFactory&);
+ CompletionCallbackFactory& operator=(const CompletionCallbackFactory&);
+
+ T* object_;
+ BackPointer* back_pointer_;
+};
+
+} // namespace pp
+
+#endif // PPAPI_UTILITY_COMPLETION_CALLBACK_FACTORY_H_
diff --git a/ppapi/cpp/paint_aggregator.cc b/ppapi/utility/graphics/paint_aggregator.cc index d0ab03e..d4fc721 100644 --- a/ppapi/cpp/paint_aggregator.cc +++ b/ppapi/utility/graphics/paint_aggregator.cc @@ -2,7 +2,7 @@ // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. -#include "ppapi/cpp/paint_aggregator.h" +#include "ppapi/utility/graphics/paint_aggregator.h" #include <algorithm> diff --git a/ppapi/cpp/paint_aggregator.h b/ppapi/utility/graphics/paint_aggregator.h index db1e04c..74e24da 100644 --- a/ppapi/cpp/paint_aggregator.h +++ b/ppapi/utility/graphics/paint_aggregator.h @@ -2,8 +2,8 @@ // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. -#ifndef PPAPI_CPP_PAINT_AGGREGATOR_H_ -#define PPAPI_CPP_PAINT_AGGREGATOR_H_ +#ifndef PPAPI_UTILITY_GRAPHICS_PAINT_AGGREGATOR_H_ +#define PPAPI_UTILITY_GRAPHICS_PAINT_AGGREGATOR_H_ #include <stddef.h> #include <vector> @@ -171,4 +171,4 @@ class PaintAggregator { } // namespace pp -#endif // PPAPI_CPP_PAINT_AGGREGATOR_H_ +#endif // PPAPI_UTILITY_PAINT_AGGREGATOR_H_ diff --git a/ppapi/cpp/paint_manager.cc b/ppapi/utility/graphics/paint_manager.cc index 23f66d5..1141ac6 100644 --- a/ppapi/cpp/paint_manager.cc +++ b/ppapi/utility/graphics/paint_manager.cc @@ -2,7 +2,7 @@ // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. -#include "ppapi/cpp/paint_manager.h" +#include "ppapi/utility/graphics/paint_manager.h" #include "ppapi/c/pp_errors.h" #include "ppapi/cpp/instance.h" diff --git a/ppapi/cpp/paint_manager.h b/ppapi/utility/graphics/paint_manager.h index fbddd9f..c3787e8 100644 --- a/ppapi/cpp/paint_manager.h +++ b/ppapi/utility/graphics/paint_manager.h @@ -2,14 +2,14 @@ // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. -#ifndef PPAPI_CPP_PAINT_MANAGER_H_ -#define PPAPI_CPP_PAINT_MANAGER_H_ +#ifndef PPAPI_UTILITY_GRAPHICS_PAINT_MANAGER_H_ +#define PPAPI_UTILITY_GRAPHICS_PAINT_MANAGER_H_ #include <vector> -#include "ppapi/cpp/completion_callback.h" #include "ppapi/cpp/graphics_2d.h" -#include "ppapi/cpp/paint_aggregator.h" +#include "ppapi/utility/completion_callback_factory.h" +#include "ppapi/utility/graphics/paint_aggregator.h" /// @file /// This file defines the API to convert the "plugin push" model of painting @@ -294,4 +294,4 @@ class PaintManager { } // namespace pp -#endif // PPAPI_CPP_PAINT_MANAGER_H_ +#endif // PPAPI_UTILITY_GRAPHICS_PAINT_MANAGER_H_ diff --git a/ppapi/cpp/non_thread_safe_ref_count.h b/ppapi/utility/non_thread_safe_ref_count.h index 0844eb7..db00b7d 100644 --- a/ppapi/cpp/non_thread_safe_ref_count.h +++ b/ppapi/utility/non_thread_safe_ref_count.h @@ -2,8 +2,8 @@ // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. -#ifndef PPAPI_CPP_NON_THREAD_SAFE_REF_COUNT_H_ -#define PPAPI_CPP_NON_THREAD_SAFE_REF_COUNT_H_ +#ifndef PPAPI_UTILITY_NON_THREAD_SAFE_REF_COUNT_H_ +#define PPAPI_UTILITY_NON_THREAD_SAFE_REF_COUNT_H_ #include "ppapi/cpp/core.h" #include "ppapi/cpp/logging.h" @@ -57,4 +57,4 @@ class NonThreadSafeRefCount { } // namespace pp -#endif // PPAPI_CPP_NON_THREAD_SAFE_REF_COUNT_H_ +#endif // PPAPI_UTILITY_NON_THREAD_SAFE_REF_COUNT_H_ diff --git a/remoting/protocol/pepper_transport_socket_adapter.h b/remoting/protocol/pepper_transport_socket_adapter.h index bd84bb8..5888268 100644 --- a/remoting/protocol/pepper_transport_socket_adapter.h +++ b/remoting/protocol/pepper_transport_socket_adapter.h @@ -14,7 +14,7 @@ #include "net/base/net_log.h" #include "net/socket/stream_socket.h" #include "ppapi/c/pp_stdint.h" -#include "ppapi/cpp/completion_callback.h" +#include "ppapi/utility/completion_callback_factory.h" namespace pp { class Transport_Dev; |