Chromium Code Reviews| Index: webkit/plugins/ppapi/ppb_flash_message_loop_impl.cc |
| diff --git a/webkit/plugins/ppapi/ppb_flash_message_loop_impl.cc b/webkit/plugins/ppapi/ppb_flash_message_loop_impl.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..154f63f6189b04efd09bb1c148a070b6a1026008 |
| --- /dev/null |
| +++ b/webkit/plugins/ppapi/ppb_flash_message_loop_impl.cc |
| @@ -0,0 +1,119 @@ |
| +// Copyright (c) 2012 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. |
| + |
| +#include "webkit/plugins/ppapi/ppb_flash_message_loop_impl.h" |
| + |
| +#include "base/message_loop.h" |
| +#include "ppapi/c/pp_errors.h" |
| + |
| +using ppapi::thunk::PPB_Flash_MessageLoop_API; |
| + |
| +namespace webkit { |
| +namespace ppapi { |
| + |
| +class PPB_Flash_MessageLoop_Impl::State |
| + : public RefCounted<PPB_Flash_MessageLoop_Impl::State> { |
| + public: |
| + State() : result_(PP_ERROR_ABORTED), |
| + run_called_(false), |
| + quit_called_(false), |
| + run_callback_(PP_BlockUntilComplete()) { |
| + } |
| + |
| + int32_t result() const { return result_; } |
| + void set_result(int32_t result) { result_ = result; } |
| + |
| + bool run_called() const { return run_called_; } |
| + void set_run_called() { run_called_ = true; } |
| + |
| + bool quit_called() const { return quit_called_; } |
| + void set_quit_called() { quit_called_ = true; } |
| + |
| + const PP_CompletionCallback& run_callback() const { return run_callback_; } |
| + void set_run_callback(const PP_CompletionCallback& run_callback) { |
| + run_callback_ = run_callback; |
| + } |
| + |
| + private: |
| + int32_t result_; |
| + bool run_called_; |
| + bool quit_called_; |
| + PP_CompletionCallback run_callback_; |
| +}; |
| + |
| +PPB_Flash_MessageLoop_Impl::PPB_Flash_MessageLoop_Impl(PP_Instance instance) |
| + : Resource(instance), |
| + state_(new State()) { |
| +} |
| + |
| +PPB_Flash_MessageLoop_Impl::~PPB_Flash_MessageLoop_Impl() { |
| + // It is a no-op if either Run() hasn't been called or Quit() has been called |
| + // to balance the call to Run(). |
| + InternalQuit(PP_ERROR_ABORTED); |
| +} |
| + |
| +// static |
| +PP_Resource PPB_Flash_MessageLoop_Impl::Create(PP_Instance instance) { |
| + return (new PPB_Flash_MessageLoop_Impl(instance))->GetReference(); |
| +} |
| + |
| +PPB_Flash_MessageLoop_API* |
| + PPB_Flash_MessageLoop_Impl::AsPPB_Flash_MessageLoop_API() { |
| + return this; |
| +} |
| + |
| +int32_t PPB_Flash_MessageLoop_Impl::Run() { |
| + return InternalRun(PP_BlockUntilComplete()); |
|
viettrungluu
2012/01/17 21:26:06
Using PP_BlockUntilComplete seems kind of hacky (a
yzshen1
2012/01/18 07:43:35
Thanks. I changed to PP_MakeCompletionCallback. An
|
| +} |
| + |
| +void PPB_Flash_MessageLoop_Impl::RunFromHostProxy( |
| + PP_CompletionCallback callback) { |
| + if (!callback.func) |
|
viettrungluu
2012/01/17 21:26:06
Should this even happen?
yzshen1
2012/01/18 07:43:35
Removed. It shouldn't happen. And it is the caller
|
| + return; |
| + InternalRun(callback); |
| +} |
| + |
| +void PPB_Flash_MessageLoop_Impl::Quit() { |
| + InternalQuit(PP_OK); |
| +} |
| + |
| +int32_t PPB_Flash_MessageLoop_Impl::InternalRun( |
| + PP_CompletionCallback callback) { |
| + if (state_->run_called()) { |
| + if (callback.func) |
| + PP_RunCompletionCallback(&callback, PP_ERROR_FAILED); |
| + return PP_ERROR_FAILED; |
| + } |
| + state_->set_run_called(); |
| + state_->set_run_callback(callback); |
| + |
| + // It is possible that the PPB_Flash_MessageLoop_Impl object has been |
| + // destroyed when the nested message loop exits. |
| + scoped_refptr<State> state_protector(state_); |
| + |
| + bool old_value = MessageLoop::current()->NestableTasksAllowed(); |
| + MessageLoop::current()->SetNestableTasksAllowed(true); |
| + MessageLoop::current()->Run(); |
| + |
| + // Don't access data members of the class below. |
| + |
| + MessageLoop::current()->SetNestableTasksAllowed(old_value); |
| + return state_protector->result(); |
| +} |
| + |
| +void PPB_Flash_MessageLoop_Impl::InternalQuit(int32_t result) { |
| + if (!state_->run_called() || state_->quit_called()) |
| + return; |
| + state_->set_quit_called(); |
| + state_->set_result(result); |
| + |
| + MessageLoop::current()->QuitNow(); |
| + |
| + PP_CompletionCallback callback = state_->run_callback(); |
| + if (callback.func) |
| + PP_RunCompletionCallback(&callback, result); |
| +} |
| + |
| +} // namespace ppapi |
| +} // namespace webkit |