diff options
author | brettw@chromium.org <brettw@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2012-01-05 06:18:12 +0000 |
---|---|---|
committer | brettw@chromium.org <brettw@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2012-01-05 06:18:12 +0000 |
commit | 614888b73113f628dc9aeaba24347ae1301faeaa (patch) | |
tree | 77c000f91e036f7256d60788863f2cf4805d8a8d /ppapi | |
parent | cb4282a066df8b0e1c09c90d3780625579c0a569 (diff) | |
download | chromium_src-614888b73113f628dc9aeaba24347ae1301faeaa.zip chromium_src-614888b73113f628dc9aeaba24347ae1301faeaa.tar.gz chromium_src-614888b73113f628dc9aeaba24347ae1301faeaa.tar.bz2 |
Convert ppapi/shared to use TrackedCallback.
Convert all classes in ppapi/shared_impl to use the new shared TrackedCallback
class for the PP_CompletionCallbacks.
This required a few changes to some shared objects to make them resources.
BUG=
TEST=
Review URL: http://codereview.chromium.org/9053003
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@116467 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'ppapi')
-rw-r--r-- | ppapi/proxy/ppb_graphics_3d_proxy.cc | 2 | ||||
-rw-r--r-- | ppapi/proxy/ppb_graphics_3d_proxy.h | 7 | ||||
-rw-r--r-- | ppapi/proxy/ppb_video_decoder_proxy.cc | 12 | ||||
-rw-r--r-- | ppapi/shared_impl/ppb_file_io_shared.cc | 19 | ||||
-rw-r--r-- | ppapi/shared_impl/ppb_file_io_shared.h | 4 | ||||
-rw-r--r-- | ppapi/shared_impl/ppb_graphics_3d_shared.cc | 23 | ||||
-rw-r--r-- | ppapi/shared_impl/ppb_graphics_3d_shared.h | 15 | ||||
-rw-r--r-- | ppapi/shared_impl/ppb_video_decoder_shared.cc | 40 | ||||
-rw-r--r-- | ppapi/shared_impl/ppb_video_decoder_shared.h | 16 | ||||
-rw-r--r-- | ppapi/shared_impl/private/tcp_socket_private_impl.cc | 81 | ||||
-rw-r--r-- | ppapi/shared_impl/private/tcp_socket_private_impl.h | 11 | ||||
-rw-r--r-- | ppapi/shared_impl/private/udp_socket_private_impl.cc | 58 | ||||
-rw-r--r-- | ppapi/shared_impl/private/udp_socket_private_impl.h | 9 |
13 files changed, 140 insertions, 157 deletions
diff --git a/ppapi/proxy/ppb_graphics_3d_proxy.cc b/ppapi/proxy/ppb_graphics_3d_proxy.cc index b5a2dee..a62f3c7 100644 --- a/ppapi/proxy/ppb_graphics_3d_proxy.cc +++ b/ppapi/proxy/ppb_graphics_3d_proxy.cc @@ -289,7 +289,7 @@ gpu::CommandBuffer::State GPUStateFromPPState( } // namespace Graphics3D::Graphics3D(const HostResource& resource) - : Resource(resource) { + : PPB_Graphics3D_Shared(resource) { } Graphics3D::~Graphics3D() { diff --git a/ppapi/proxy/ppb_graphics_3d_proxy.h b/ppapi/proxy/ppb_graphics_3d_proxy.h index 5a6c334..f4110b6 100644 --- a/ppapi/proxy/ppb_graphics_3d_proxy.h +++ b/ppapi/proxy/ppb_graphics_3d_proxy.h @@ -23,18 +23,13 @@ class HostResource; namespace proxy { -class Graphics3D : public Resource, public PPB_Graphics3D_Shared { +class Graphics3D : public PPB_Graphics3D_Shared { public: explicit Graphics3D(const HostResource& resource); virtual ~Graphics3D(); bool Init(); - // Resource overrides. - virtual thunk::PPB_Graphics3D_API* AsPPB_Graphics3D_API() OVERRIDE { - return this; - } - // Graphics3DTrusted API. These are not implemented in the proxy. virtual PP_Bool InitCommandBuffer() OVERRIDE; virtual PP_Bool SetGetBuffer(int32_t shm_id) OVERRIDE; diff --git a/ppapi/proxy/ppb_video_decoder_proxy.cc b/ppapi/proxy/ppb_video_decoder_proxy.cc index 850ae6e..3f5cdc4 100644 --- a/ppapi/proxy/ppb_video_decoder_proxy.cc +++ b/ppapi/proxy/ppb_video_decoder_proxy.cc @@ -23,7 +23,7 @@ using ppapi::thunk::PPB_VideoDecoder_API; namespace ppapi { namespace proxy { -class VideoDecoder : public Resource, public PPB_VideoDecoder_Shared { +class VideoDecoder : public PPB_VideoDecoder_Shared { public: // You must call Init() before using this class. explicit VideoDecoder(const HostResource& resource); @@ -33,9 +33,6 @@ class VideoDecoder : public Resource, public PPB_VideoDecoder_Shared { PP_Resource graphics_context, PP_VideoDecoder_Profile profile); - // Resource overrides. - virtual PPB_VideoDecoder_API* AsPPB_VideoDecoder_API() OVERRIDE; - // PPB_VideoDecoder_API implementation. virtual int32_t Decode(const PP_VideoBitstreamBuffer_Dev* bitstream_buffer, PP_CompletionCallback callback) OVERRIDE; @@ -59,16 +56,13 @@ class VideoDecoder : public Resource, public PPB_VideoDecoder_Shared { DISALLOW_COPY_AND_ASSIGN(VideoDecoder); }; -VideoDecoder::VideoDecoder(const HostResource& decoder) : Resource(decoder) { +VideoDecoder::VideoDecoder(const HostResource& decoder) + : PPB_VideoDecoder_Shared(decoder) { } VideoDecoder::~VideoDecoder() { } -PPB_VideoDecoder_API* VideoDecoder::AsPPB_VideoDecoder_API() { - return this; -} - int32_t VideoDecoder::Decode( const PP_VideoBitstreamBuffer_Dev* bitstream_buffer, PP_CompletionCallback callback) { diff --git a/ppapi/shared_impl/ppb_file_io_shared.cc b/ppapi/shared_impl/ppb_file_io_shared.cc index dcad4d7..7eeab92 100644 --- a/ppapi/shared_impl/ppb_file_io_shared.cc +++ b/ppapi/shared_impl/ppb_file_io_shared.cc @@ -24,8 +24,6 @@ using thunk::PPB_FileRef_API; PPB_FileIO_Shared::CallbackEntry::CallbackEntry() : read_buffer(NULL), info(NULL) { - callback.func = NULL; - callback.user_data = NULL; } PPB_FileIO_Shared::CallbackEntry::CallbackEntry(const CallbackEntry& entry) @@ -52,19 +50,6 @@ PPB_FileIO_Shared::PPB_FileIO_Shared(const HostResource& host_resource) } PPB_FileIO_Shared::~PPB_FileIO_Shared() { - // The callbacks list should have been cleared by LastPluginRefWasDeleted. - DCHECK(callbacks_.empty()); -} - -void PPB_FileIO_Shared::LastPluginRefWasDeleted() { - // Abort all pending callbacks. Do this by posting a task to avoid reentering - // the plugin's Release() call that probably deleted this object. - for (size_t i = 0; i < callbacks_.size(); i++) { - MessageLoop::current()->PostTask(FROM_HERE, base::Bind( - callbacks_[i].callback.func, callbacks_[i].callback.user_data, - static_cast<int32_t>(PP_ERROR_ABORTED))); - } - callbacks_.erase(callbacks_.begin(), callbacks_.end()); } thunk::PPB_FileIO_API* PPB_FileIO_Shared::AsPPB_FileIO_API() { @@ -217,7 +202,7 @@ void PPB_FileIO_Shared::RegisterCallback(OperationType op, (pending_op_ != OPERATION_EXCLUSIVE && pending_op_ == op)); CallbackEntry entry; - entry.callback = callback; + entry.callback = new TrackedCallback(this, callback); entry.read_buffer = read_buffer; entry.info = info; callbacks_.push_back(entry); @@ -233,7 +218,7 @@ void PPB_FileIO_Shared::RunAndRemoveFirstPendingCallback(int32_t result) { if (callbacks_.empty()) pending_op_ = OPERATION_NONE; - PP_RunCompletionCallback(&front.callback, result); + front.callback->Run(result); } } // namespace ppapi diff --git a/ppapi/shared_impl/ppb_file_io_shared.h b/ppapi/shared_impl/ppb_file_io_shared.h index 341db46..4470a94 100644 --- a/ppapi/shared_impl/ppb_file_io_shared.h +++ b/ppapi/shared_impl/ppb_file_io_shared.h @@ -10,6 +10,7 @@ #include "base/compiler_specific.h" #include "ppapi/shared_impl/ppapi_shared_export.h" #include "ppapi/shared_impl/resource.h" +#include "ppapi/shared_impl/tracked_callback.h" #include "ppapi/thunk/ppb_file_io_api.h" namespace ppapi { @@ -26,7 +27,6 @@ class PPAPI_SHARED_EXPORT PPB_FileIO_Shared : public Resource, ~PPB_FileIO_Shared(); // Resource overrides. - virtual void LastPluginRefWasDeleted() OVERRIDE; virtual thunk::PPB_FileIO_API* AsPPB_FileIO_API() OVERRIDE; // PPB_FileIO_API implementation. @@ -62,7 +62,7 @@ class PPAPI_SHARED_EXPORT PPB_FileIO_Shared : public Resource, CallbackEntry(const CallbackEntry& entry); ~CallbackEntry(); - PP_CompletionCallback callback; + scoped_refptr<TrackedCallback> callback; // Pointer back to the caller's read buffer; only used by |Read()|, NULL // for non-read operations. Not owned. diff --git a/ppapi/shared_impl/ppb_graphics_3d_shared.cc b/ppapi/shared_impl/ppb_graphics_3d_shared.cc index 9e0c155..d444516 100644 --- a/ppapi/shared_impl/ppb_graphics_3d_shared.cc +++ b/ppapi/shared_impl/ppb_graphics_3d_shared.cc @@ -11,9 +11,14 @@ namespace ppapi { -PPB_Graphics3D_Shared::PPB_Graphics3D_Shared() - : transfer_buffer_id_(-1), - swap_callback_(PP_BlockUntilComplete()) { +PPB_Graphics3D_Shared::PPB_Graphics3D_Shared(PP_Instance instance) + : Resource(instance), + transfer_buffer_id_(-1) { +} + +PPB_Graphics3D_Shared::PPB_Graphics3D_Shared(const HostResource& host_resource) + : Resource(host_resource), + transfer_buffer_id_(-1) { } PPB_Graphics3D_Shared::~PPB_Graphics3D_Shared() { @@ -23,6 +28,10 @@ PPB_Graphics3D_Shared::~PPB_Graphics3D_Shared() { DCHECK(!gles2_impl_.get()); } +thunk::PPB_Graphics3D_API* PPB_Graphics3D_Shared::AsPPB_Graphics3D_API() { + return this; +} + int32_t PPB_Graphics3D_Shared::GetAttribs(int32_t* attrib_list) { // TODO(alokp): Implement me. return PP_ERROR_FAILED; @@ -59,7 +68,7 @@ int32_t PPB_Graphics3D_Shared::SwapBuffers(PP_CompletionCallback callback) { return PP_ERROR_INPROGRESS; } - swap_callback_ = callback; + swap_callback_ = new TrackedCallback(this, callback); return DoSwapBuffers(); } @@ -82,7 +91,11 @@ void PPB_Graphics3D_Shared::UnmapTexSubImage2DCHROMIUM(const void* mem) { void PPB_Graphics3D_Shared::SwapBuffersACK(int32_t pp_error) { DCHECK(HasPendingSwap()); - PP_RunAndClearCompletionCallback(&swap_callback_, pp_error); + TrackedCallback::ClearAndRun(&swap_callback_, pp_error); +} + +bool PPB_Graphics3D_Shared::HasPendingSwap() const { + return TrackedCallback::IsPending(swap_callback_); } bool PPB_Graphics3D_Shared::CreateGLES2Impl(int32 command_buffer_size, diff --git a/ppapi/shared_impl/ppb_graphics_3d_shared.h b/ppapi/shared_impl/ppb_graphics_3d_shared.h index 6f3a04f..8804181 100644 --- a/ppapi/shared_impl/ppb_graphics_3d_shared.h +++ b/ppapi/shared_impl/ppb_graphics_3d_shared.h @@ -9,6 +9,8 @@ #include "base/memory/scoped_ptr.h" #include "ppapi/c/pp_completion_callback.h" #include "ppapi/shared_impl/ppapi_shared_export.h" +#include "ppapi/shared_impl/resource.h" +#include "ppapi/shared_impl/tracked_callback.h" #include "ppapi/thunk/ppb_graphics_3d_api.h" namespace gpu { @@ -22,8 +24,12 @@ class GLES2Implementation; namespace ppapi { class PPAPI_SHARED_EXPORT PPB_Graphics3D_Shared - : public thunk::PPB_Graphics3D_API { + : public Resource, + public thunk::PPB_Graphics3D_API { public: + // Resource overrides. + virtual thunk::PPB_Graphics3D_API* AsPPB_Graphics3D_API() OVERRIDE; + // PPB_Graphics3D_API implementation. virtual int32_t GetAttribs(int32_t* attrib_list) OVERRIDE; virtual int32_t SetAttribs(int32_t* attrib_list) OVERRIDE; @@ -49,13 +55,14 @@ class PPAPI_SHARED_EXPORT PPB_Graphics3D_Shared void SwapBuffersACK(int32_t pp_error); protected: - PPB_Graphics3D_Shared(); + PPB_Graphics3D_Shared(PP_Instance instance); + PPB_Graphics3D_Shared(const HostResource& host_resource); virtual ~PPB_Graphics3D_Shared(); virtual gpu::CommandBuffer* GetCommandBuffer() = 0; virtual int32 DoSwapBuffers() = 0; - bool HasPendingSwap() { return swap_callback_.func != NULL; } + bool HasPendingSwap() const; bool CreateGLES2Impl(int32 command_buffer_size, int32 transfer_buffer_size); void DestroyGLES2Impl(); @@ -66,7 +73,7 @@ class PPAPI_SHARED_EXPORT PPB_Graphics3D_Shared scoped_ptr<gpu::gles2::GLES2Implementation> gles2_impl_; // Callback that needs to be executed when swap-buffers is completed. - PP_CompletionCallback swap_callback_; + scoped_refptr<TrackedCallback> swap_callback_; DISALLOW_COPY_AND_ASSIGN(PPB_Graphics3D_Shared); }; diff --git a/ppapi/shared_impl/ppb_video_decoder_shared.cc b/ppapi/shared_impl/ppb_video_decoder_shared.cc index 190654e..39a4088 100644 --- a/ppapi/shared_impl/ppb_video_decoder_shared.cc +++ b/ppapi/shared_impl/ppb_video_decoder_shared.cc @@ -12,9 +12,15 @@ namespace ppapi { -PPB_VideoDecoder_Shared::PPB_VideoDecoder_Shared() - : flush_callback_(PP_MakeCompletionCallback(NULL, NULL)), - reset_callback_(PP_MakeCompletionCallback(NULL, NULL)), +PPB_VideoDecoder_Shared::PPB_VideoDecoder_Shared(PP_Instance instance) + : Resource(instance), + graphics_context_(0), + gles2_impl_(NULL) { +} + +PPB_VideoDecoder_Shared::PPB_VideoDecoder_Shared( + const HostResource& host_resource) + : Resource(host_resource), graphics_context_(0), gles2_impl_(NULL) { } @@ -22,6 +28,10 @@ PPB_VideoDecoder_Shared::PPB_VideoDecoder_Shared() PPB_VideoDecoder_Shared::~PPB_VideoDecoder_Shared() { } +thunk::PPB_VideoDecoder_API* PPB_VideoDecoder_Shared::AsPPB_VideoDecoder_API() { + return this; +} + void PPB_VideoDecoder_Shared::InitCommon( PP_Resource graphics_context, gpu::gles2::GLES2Implementation* gles2_impl) { @@ -40,34 +50,34 @@ void PPB_VideoDecoder_Shared::Destroy() { bool PPB_VideoDecoder_Shared::SetFlushCallback(PP_CompletionCallback callback) { CHECK(callback.func); - if (flush_callback_.func) + if (flush_callback_.get()) return false; - flush_callback_ = callback; + flush_callback_ = new TrackedCallback(this, callback); return true; } bool PPB_VideoDecoder_Shared::SetResetCallback(PP_CompletionCallback callback) { CHECK(callback.func); - if (reset_callback_.func) + if (TrackedCallback::IsPending(reset_callback_)) return false; - reset_callback_ = callback; + reset_callback_ = new TrackedCallback(this, callback); return true; } bool PPB_VideoDecoder_Shared::SetBitstreamBufferCallback( - int32 bitstream_buffer_id, PP_CompletionCallback callback) { + int32 bitstream_buffer_id, + PP_CompletionCallback callback) { return bitstream_buffer_callbacks_.insert( - std::make_pair(bitstream_buffer_id, callback)).second; + std::make_pair(bitstream_buffer_id, + new TrackedCallback(this, callback))).second; } void PPB_VideoDecoder_Shared::RunFlushCallback(int32 result) { - DCHECK(flush_callback_.func); - PP_RunAndClearCompletionCallback(&flush_callback_, result); + TrackedCallback::ClearAndRun(&flush_callback_, result); } void PPB_VideoDecoder_Shared::RunResetCallback(int32 result) { - DCHECK(reset_callback_.func); - PP_RunAndClearCompletionCallback(&reset_callback_, result); + TrackedCallback::ClearAndRun(&reset_callback_, result); } void PPB_VideoDecoder_Shared::RunBitstreamBufferCallback( @@ -75,9 +85,9 @@ void PPB_VideoDecoder_Shared::RunBitstreamBufferCallback( CallbackById::iterator it = bitstream_buffer_callbacks_.find(bitstream_buffer_id); DCHECK(it != bitstream_buffer_callbacks_.end()); - PP_CompletionCallback cc = it->second; + scoped_refptr<TrackedCallback> cc = it->second; bitstream_buffer_callbacks_.erase(it); - PP_RunCompletionCallback(&cc, PP_OK); + cc->Run(PP_OK); } void PPB_VideoDecoder_Shared::FlushCommandBuffer() { diff --git a/ppapi/shared_impl/ppb_video_decoder_shared.h b/ppapi/shared_impl/ppb_video_decoder_shared.h index 55b629c..9f1aa46 100644 --- a/ppapi/shared_impl/ppb_video_decoder_shared.h +++ b/ppapi/shared_impl/ppb_video_decoder_shared.h @@ -12,6 +12,7 @@ #include "base/compiler_specific.h" #include "ppapi/c/dev/ppb_video_decoder_dev.h" #include "ppapi/shared_impl/resource.h" +#include "ppapi/shared_impl/tracked_callback.h" #include "ppapi/thunk/ppb_video_decoder_api.h" namespace gpu { @@ -25,11 +26,16 @@ namespace ppapi { // Implements the logic to set and run callbacks for various video decoder // events. Both the proxy and the renderer implementation share this code. class PPAPI_SHARED_EXPORT PPB_VideoDecoder_Shared - : NON_EXPORTED_BASE(public thunk::PPB_VideoDecoder_API) { + : public Resource, + NON_EXPORTED_BASE(public thunk::PPB_VideoDecoder_API) { public: - PPB_VideoDecoder_Shared(); + explicit PPB_VideoDecoder_Shared(PP_Instance instance); + explicit PPB_VideoDecoder_Shared(const HostResource& host_resource); virtual ~PPB_VideoDecoder_Shared(); + // Resource overrides. + virtual thunk::PPB_VideoDecoder_API* AsPPB_VideoDecoder_API() OVERRIDE; + // PPB_VideoDecoder_API implementation. virtual void Destroy() OVERRIDE; @@ -53,10 +59,10 @@ class PPAPI_SHARED_EXPORT PPB_VideoDecoder_Shared private: // Key: bitstream_buffer_id, value: callback to run when bitstream decode is // done. - typedef std::map<int32, PP_CompletionCallback> CallbackById; + typedef std::map<int32, scoped_refptr<TrackedCallback> > CallbackById; - PP_CompletionCallback flush_callback_; - PP_CompletionCallback reset_callback_; + scoped_refptr<TrackedCallback> flush_callback_; + scoped_refptr<TrackedCallback> reset_callback_; CallbackById bitstream_buffer_callbacks_; // The resource ID of the underlying Graphics3D object being used. Used only diff --git a/ppapi/shared_impl/private/tcp_socket_private_impl.cc b/ppapi/shared_impl/private/tcp_socket_private_impl.cc index 7ec1535..a268801 100644 --- a/ppapi/shared_impl/private/tcp_socket_private_impl.cc +++ b/ppapi/shared_impl/private/tcp_socket_private_impl.cc @@ -17,14 +17,6 @@ namespace ppapi { -namespace { - -void AbortCallback(PP_CompletionCallback callback) { - PP_RunCompletionCallback(&callback, PP_ERROR_ABORTED); -} - -} // namespace - const int32_t TCPSocketPrivateImpl::kMaxReadSize = 1024 * 1024; const int32_t TCPSocketPrivateImpl::kMaxWriteSize = 1024 * 1024; @@ -57,10 +49,10 @@ int32_t TCPSocketPrivateImpl::Connect(const char* host, return PP_ERROR_BLOCKS_MAIN_THREAD; if (connection_state_ != BEFORE_CONNECT) return PP_ERROR_FAILED; - if (connect_callback_.func) + if (TrackedCallback::IsPending(connect_callback_)) return PP_ERROR_INPROGRESS; // Can only have one pending request. - connect_callback_ = callback; + connect_callback_ = new TrackedCallback(this, callback); // Send the request, the browser will call us back via ConnectACK. SendConnect(host, port); return PP_OK_COMPLETIONPENDING; @@ -75,10 +67,10 @@ int32_t TCPSocketPrivateImpl::ConnectWithNetAddress( return PP_ERROR_BLOCKS_MAIN_THREAD; if (connection_state_ != BEFORE_CONNECT) return PP_ERROR_FAILED; - if (connect_callback_.func) + if (TrackedCallback::IsPending(connect_callback_)) return PP_ERROR_INPROGRESS; // Can only have one pending request. - connect_callback_ = callback; + connect_callback_ = new TrackedCallback(this, callback); // Send the request, the browser will call us back via ConnectACK. SendConnectWithNetAddress(*addr); return PP_OK_COMPLETIONPENDING; @@ -112,11 +104,12 @@ int32_t TCPSocketPrivateImpl::SSLHandshake(const char* server_name, if (connection_state_ != CONNECTED) return PP_ERROR_FAILED; - if (ssl_handshake_callback_.func || read_callback_.func || - write_callback_.func) + if (TrackedCallback::IsPending(ssl_handshake_callback_) || + TrackedCallback::IsPending(read_callback_) || + TrackedCallback::IsPending(write_callback_)) return PP_ERROR_INPROGRESS; - ssl_handshake_callback_ = callback; + ssl_handshake_callback_ = new TrackedCallback(this, callback); // Send the request, the browser will call us back via SSLHandshakeACK. SendSSLHandshake(server_name, server_port); @@ -133,13 +126,14 @@ int32_t TCPSocketPrivateImpl::Read(char* buffer, if (!IsConnected()) return PP_ERROR_FAILED; - if (read_callback_.func || ssl_handshake_callback_.func) + if (TrackedCallback::IsPending(read_callback_) || + TrackedCallback::IsPending(ssl_handshake_callback_)) return PP_ERROR_INPROGRESS; // TODO(dmichael): use some other strategy for determining if an // operation is in progress read_buffer_ = buffer; bytes_to_read_ = std::min(bytes_to_read, kMaxReadSize); - read_callback_ = callback; + read_callback_ = new TrackedCallback(this, callback); // Send the request, the browser will call us back via ReadACK. SendRead(bytes_to_read_); @@ -156,13 +150,14 @@ int32_t TCPSocketPrivateImpl::Write(const char* buffer, if (!IsConnected()) return PP_ERROR_FAILED; - if (write_callback_.func || ssl_handshake_callback_.func) + if (TrackedCallback::IsPending(write_callback_) || + TrackedCallback::IsPending(ssl_handshake_callback_)) return PP_ERROR_INPROGRESS; if (bytes_to_write > kMaxWriteSize) bytes_to_write = kMaxWriteSize; - write_callback_ = callback; + write_callback_ = new TrackedCallback(this, callback); // Send the request, the browser will call us back via WriteACK. SendWrite(std::string(buffer, bytes_to_write)); @@ -178,10 +173,10 @@ void TCPSocketPrivateImpl::Disconnect() { SendDisconnect(); socket_id_ = 0; - PostAbortAndClearIfNecessary(&connect_callback_); - PostAbortAndClearIfNecessary(&ssl_handshake_callback_); - PostAbortAndClearIfNecessary(&read_callback_); - PostAbortAndClearIfNecessary(&write_callback_); + PostAbortIfNecessary(&connect_callback_); + PostAbortIfNecessary(&ssl_handshake_callback_); + PostAbortIfNecessary(&read_callback_); + PostAbortIfNecessary(&write_callback_); read_buffer_ = NULL; bytes_to_read_ = -1; } @@ -190,7 +185,8 @@ void TCPSocketPrivateImpl::OnConnectCompleted( bool succeeded, const PP_NetAddress_Private& local_addr, const PP_NetAddress_Private& remote_addr) { - if (connection_state_ != BEFORE_CONNECT || !connect_callback_.func) { + if (connection_state_ != BEFORE_CONNECT || + !TrackedCallback::IsPending(connect_callback_)) { NOTREACHED(); return; } @@ -200,28 +196,29 @@ void TCPSocketPrivateImpl::OnConnectCompleted( remote_addr_ = remote_addr; connection_state_ = CONNECTED; } - PP_RunAndClearCompletionCallback(&connect_callback_, - succeeded ? PP_OK : PP_ERROR_FAILED); + TrackedCallback::ClearAndRun(&connect_callback_, + succeeded ? PP_OK : PP_ERROR_FAILED); } void TCPSocketPrivateImpl::OnSSLHandshakeCompleted(bool succeeded) { - if (connection_state_ != CONNECTED || !ssl_handshake_callback_.func) { + if (connection_state_ != CONNECTED || + !TrackedCallback::IsPending(ssl_handshake_callback_)) { NOTREACHED(); return; } if (succeeded) { connection_state_ = SSL_CONNECTED; - PP_RunAndClearCompletionCallback(&ssl_handshake_callback_, PP_OK); + TrackedCallback::ClearAndRun(&ssl_handshake_callback_, PP_OK); } else { - PP_RunAndClearCompletionCallback(&ssl_handshake_callback_, PP_ERROR_FAILED); + TrackedCallback::ClearAndRun(&ssl_handshake_callback_, PP_ERROR_FAILED); Disconnect(); } } void TCPSocketPrivateImpl::OnReadCompleted(bool succeeded, const std::string& data) { - if (!read_callback_.func || !read_buffer_) { + if (!TrackedCallback::IsPending(read_callback_) || !read_buffer_) { NOTREACHED(); return; } @@ -234,7 +231,7 @@ void TCPSocketPrivateImpl::OnReadCompleted(bool succeeded, read_buffer_ = NULL; bytes_to_read_ = -1; - PP_RunAndClearCompletionCallback( + TrackedCallback::ClearAndRun( &read_callback_, succeeded ? static_cast<int32_t>(data.size()) : static_cast<int32_t>(PP_ERROR_FAILED)); @@ -242,12 +239,13 @@ void TCPSocketPrivateImpl::OnReadCompleted(bool succeeded, void TCPSocketPrivateImpl::OnWriteCompleted(bool succeeded, int32_t bytes_written) { - if (!write_callback_.func || (succeeded && bytes_written < 0)) { + if (!TrackedCallback::IsPending(write_callback_) || + (succeeded && bytes_written < 0)) { NOTREACHED(); return; } - PP_RunAndClearCompletionCallback( + TrackedCallback::ClearAndRun( &write_callback_, succeeded ? bytes_written : static_cast<int32_t>(PP_ERROR_FAILED)); } @@ -256,10 +254,6 @@ void TCPSocketPrivateImpl::Init(uint32 socket_id) { DCHECK(socket_id != 0); socket_id_ = socket_id; connection_state_ = BEFORE_CONNECT; - connect_callback_ = PP_BlockUntilComplete(); - ssl_handshake_callback_ = PP_BlockUntilComplete(); - read_callback_ = PP_BlockUntilComplete(); - write_callback_ = PP_BlockUntilComplete(); read_buffer_ = NULL; bytes_to_read_ = -1; @@ -275,15 +269,10 @@ bool TCPSocketPrivateImpl::IsConnected() const { return connection_state_ == CONNECTED || connection_state_ == SSL_CONNECTED; } -void TCPSocketPrivateImpl::PostAbortAndClearIfNecessary( - PP_CompletionCallback* callback) { - DCHECK(callback); - - if (callback->func) { - MessageLoop::current()->PostTask(FROM_HERE, - base::Bind(&AbortCallback, *callback)); - *callback = PP_BlockUntilComplete(); - } +void TCPSocketPrivateImpl::PostAbortIfNecessary( + scoped_refptr<TrackedCallback>* callback) { + if (callback->get()) + (*callback)->PostAbort(); } } // namespace ppapi diff --git a/ppapi/shared_impl/private/tcp_socket_private_impl.h b/ppapi/shared_impl/private/tcp_socket_private_impl.h index 095f155..c9a65a7 100644 --- a/ppapi/shared_impl/private/tcp_socket_private_impl.h +++ b/ppapi/shared_impl/private/tcp_socket_private_impl.h @@ -9,6 +9,7 @@ #include "base/compiler_specific.h" #include "ppapi/shared_impl/resource.h" +#include "ppapi/shared_impl/tracked_callback.h" #include "ppapi/thunk/ppb_tcp_socket_private_api.h" namespace ppapi { @@ -92,15 +93,15 @@ class PPAPI_SHARED_EXPORT TCPSocketPrivateImpl void Init(uint32 socket_id); bool IsConnected() const; - void PostAbortAndClearIfNecessary(PP_CompletionCallback* callback); + void PostAbortIfNecessary(scoped_refptr<TrackedCallback>* callback); uint32 socket_id_; ConnectionState connection_state_; - PP_CompletionCallback connect_callback_; - PP_CompletionCallback ssl_handshake_callback_; - PP_CompletionCallback read_callback_; - PP_CompletionCallback write_callback_; + scoped_refptr<TrackedCallback> connect_callback_; + scoped_refptr<TrackedCallback> ssl_handshake_callback_; + scoped_refptr<TrackedCallback> read_callback_; + scoped_refptr<TrackedCallback> write_callback_; char* read_buffer_; int32_t bytes_to_read_; diff --git a/ppapi/shared_impl/private/udp_socket_private_impl.cc b/ppapi/shared_impl/private/udp_socket_private_impl.cc index 388d159..23f125d 100644 --- a/ppapi/shared_impl/private/udp_socket_private_impl.cc +++ b/ppapi/shared_impl/private/udp_socket_private_impl.cc @@ -17,14 +17,6 @@ namespace ppapi { -namespace { - -void AbortCallback(PP_CompletionCallback callback) { - PP_RunCompletionCallback(&callback, PP_ERROR_ABORTED); -} - -} // namespace - const int32_t UDPSocketPrivateImpl::kMaxReadSize = 1024 * 1024; const int32_t UDPSocketPrivateImpl::kMaxWriteSize = 1024 * 1024; @@ -54,12 +46,12 @@ int32_t UDPSocketPrivateImpl::Bind(const PP_NetAddress_Private* addr, return PP_ERROR_BADARGUMENT; if (bound_ || closed_) return PP_ERROR_FAILED; - if (bind_callback_.func) + if (TrackedCallback::IsPending(bind_callback_)) return PP_ERROR_INPROGRESS; // TODO(dmichael): use some other strategy for determining if an // operation is in progress - bind_callback_ = callback; + bind_callback_ = new TrackedCallback(this, callback); // Send the request, the browser will call us back via BindACK. SendBind(*addr); @@ -73,12 +65,12 @@ int32_t UDPSocketPrivateImpl::RecvFrom(char* buffer, return PP_ERROR_BADARGUMENT; if (!bound_) return PP_ERROR_FAILED; - if (recvfrom_callback_.func) + if (TrackedCallback::IsPending(recvfrom_callback_)) return PP_ERROR_INPROGRESS; read_buffer_ = buffer; bytes_to_read_ = std::min(num_bytes, kMaxReadSize); - recvfrom_callback_ = callback; + recvfrom_callback_ = new TrackedCallback(this, callback); // Send the request, the browser will call us back via RecvFromACK. SendRecvFrom(bytes_to_read_); @@ -101,13 +93,13 @@ int32_t UDPSocketPrivateImpl::SendTo(const char* buffer, return PP_ERROR_BADARGUMENT; if (!bound_) return PP_ERROR_FAILED; - if (sendto_callback_.func) + if (TrackedCallback::IsPending(sendto_callback_)) return PP_ERROR_INPROGRESS; if (num_bytes > kMaxWriteSize) num_bytes = kMaxWriteSize; - sendto_callback_ = callback; + sendto_callback_ = new TrackedCallback(this, callback); // Send the request, the browser will call us back via SendToACK. SendSendTo(std::string(buffer, num_bytes), *addr); @@ -125,13 +117,13 @@ void UDPSocketPrivateImpl::Close() { socket_id_ = 0; - PostAbortAndClearIfNecessary(&bind_callback_); - PostAbortAndClearIfNecessary(&recvfrom_callback_); - PostAbortAndClearIfNecessary(&sendto_callback_); + PostAbortIfNecessary(&bind_callback_); + PostAbortIfNecessary(&recvfrom_callback_); + PostAbortIfNecessary(&sendto_callback_); } void UDPSocketPrivateImpl::OnBindCompleted(bool succeeded) { - if (!bind_callback_.func) { + if (!TrackedCallback::IsPending(bind_callback_)) { NOTREACHED(); return; } @@ -139,15 +131,15 @@ void UDPSocketPrivateImpl::OnBindCompleted(bool succeeded) { if (succeeded) bound_ = true; - PP_RunAndClearCompletionCallback(&bind_callback_, - succeeded ? PP_OK : PP_ERROR_FAILED); + TrackedCallback::ClearAndRun(&bind_callback_, + succeeded ? PP_OK : PP_ERROR_FAILED); } void UDPSocketPrivateImpl::OnRecvFromCompleted( bool succeeded, const std::string& data, const PP_NetAddress_Private& addr) { - if (!recvfrom_callback_.func || !read_buffer_) { + if (!TrackedCallback::IsPending(recvfrom_callback_) || !read_buffer_) { NOTREACHED(); return; } @@ -161,21 +153,19 @@ void UDPSocketPrivateImpl::OnRecvFromCompleted( bytes_to_read_ = -1; recvfrom_addr_ = addr; - PP_RunAndClearCompletionCallback( - &recvfrom_callback_, + TrackedCallback::ClearAndRun(&recvfrom_callback_, succeeded ? static_cast<int32_t>(data.size()) : static_cast<int32_t>(PP_ERROR_FAILED)); } void UDPSocketPrivateImpl::OnSendToCompleted(bool succeeded, int32_t bytes_written) { - if (!sendto_callback_.func) { + if (!TrackedCallback::IsPending(sendto_callback_)) { NOTREACHED(); return; } - PP_RunAndClearCompletionCallback( - &sendto_callback_, + TrackedCallback::ClearAndRun(&sendto_callback_, succeeded ? bytes_written : static_cast<int32_t>(PP_ERROR_FAILED)); } @@ -184,9 +174,6 @@ void UDPSocketPrivateImpl::Init(uint32 socket_id) { socket_id_ = socket_id; bound_ = false; closed_ = false; - bind_callback_ = PP_BlockUntilComplete(); - recvfrom_callback_ = PP_BlockUntilComplete(); - sendto_callback_ = PP_BlockUntilComplete(); read_buffer_ = NULL; bytes_to_read_ = -1; @@ -195,15 +182,10 @@ void UDPSocketPrivateImpl::Init(uint32 socket_id) { arraysize(recvfrom_addr_.data) * sizeof(*recvfrom_addr_.data)); } -void UDPSocketPrivateImpl::PostAbortAndClearIfNecessary( - PP_CompletionCallback* callback) { - DCHECK(callback); - - if (callback->func) { - MessageLoop::current()->PostTask(FROM_HERE, - base::Bind(&AbortCallback, *callback)); - *callback = PP_BlockUntilComplete(); - } +void UDPSocketPrivateImpl::PostAbortIfNecessary( + scoped_refptr<TrackedCallback>* callback) { + if (callback->get()) + (*callback)->PostAbort(); } } // namespace ppapi diff --git a/ppapi/shared_impl/private/udp_socket_private_impl.h b/ppapi/shared_impl/private/udp_socket_private_impl.h index 0f06e06..01242c1 100644 --- a/ppapi/shared_impl/private/udp_socket_private_impl.h +++ b/ppapi/shared_impl/private/udp_socket_private_impl.h @@ -9,6 +9,7 @@ #include "base/compiler_specific.h" #include "ppapi/shared_impl/resource.h" +#include "ppapi/shared_impl/tracked_callback.h" #include "ppapi/thunk/ppb_udp_socket_private_api.h" namespace ppapi { @@ -68,16 +69,16 @@ class PPAPI_SHARED_EXPORT UDPSocketPrivateImpl protected: void Init(uint32 socket_id); - void PostAbortAndClearIfNecessary(PP_CompletionCallback* callback); + void PostAbortIfNecessary(scoped_refptr<TrackedCallback>* callback); uint32 socket_id_; bool bound_; bool closed_; - PP_CompletionCallback bind_callback_; - PP_CompletionCallback recvfrom_callback_; - PP_CompletionCallback sendto_callback_; + scoped_refptr<TrackedCallback> bind_callback_; + scoped_refptr<TrackedCallback> recvfrom_callback_; + scoped_refptr<TrackedCallback> sendto_callback_; char* read_buffer_; int32_t bytes_to_read_; |