OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "webkit/plugins/ppapi/ppb_flash_message_loop_impl.h" |
| 6 |
| 7 #include "base/callback.h" |
| 8 #include "base/message_loop.h" |
| 9 #include "ppapi/c/pp_errors.h" |
| 10 |
| 11 using ppapi::thunk::PPB_Flash_MessageLoop_API; |
| 12 |
| 13 namespace webkit { |
| 14 namespace ppapi { |
| 15 |
| 16 class PPB_Flash_MessageLoop_Impl::State |
| 17 : public base::RefCounted<PPB_Flash_MessageLoop_Impl::State> { |
| 18 public: |
| 19 State() : result_(PP_OK), run_called_(false), quit_called_(false) { |
| 20 } |
| 21 |
| 22 int32_t result() const { return result_; } |
| 23 void set_result(int32_t result) { result_ = result; } |
| 24 |
| 25 bool run_called() const { return run_called_; } |
| 26 void set_run_called() { run_called_ = true; } |
| 27 |
| 28 bool quit_called() const { return quit_called_; } |
| 29 void set_quit_called() { quit_called_ = true; } |
| 30 |
| 31 const RunFromHostProxyCallback& run_callback() const { return run_callback_; } |
| 32 void set_run_callback(const RunFromHostProxyCallback& run_callback) { |
| 33 run_callback_ = run_callback; |
| 34 } |
| 35 |
| 36 private: |
| 37 int32_t result_; |
| 38 bool run_called_; |
| 39 bool quit_called_; |
| 40 RunFromHostProxyCallback run_callback_; |
| 41 }; |
| 42 |
| 43 PPB_Flash_MessageLoop_Impl::PPB_Flash_MessageLoop_Impl(PP_Instance instance) |
| 44 : Resource(instance), |
| 45 state_(new State()) { |
| 46 } |
| 47 |
| 48 PPB_Flash_MessageLoop_Impl::~PPB_Flash_MessageLoop_Impl() { |
| 49 // It is a no-op if either Run() hasn't been called or Quit() has been called |
| 50 // to balance the call to Run(). |
| 51 InternalQuit(PP_ERROR_ABORTED); |
| 52 } |
| 53 |
| 54 // static |
| 55 PP_Resource PPB_Flash_MessageLoop_Impl::Create(PP_Instance instance) { |
| 56 return (new PPB_Flash_MessageLoop_Impl(instance))->GetReference(); |
| 57 } |
| 58 |
| 59 PPB_Flash_MessageLoop_API* |
| 60 PPB_Flash_MessageLoop_Impl::AsPPB_Flash_MessageLoop_API() { |
| 61 return this; |
| 62 } |
| 63 |
| 64 int32_t PPB_Flash_MessageLoop_Impl::Run() { |
| 65 return InternalRun(RunFromHostProxyCallback()); |
| 66 } |
| 67 |
| 68 void PPB_Flash_MessageLoop_Impl::RunFromHostProxy( |
| 69 const RunFromHostProxyCallback& callback) { |
| 70 InternalRun(callback); |
| 71 } |
| 72 |
| 73 void PPB_Flash_MessageLoop_Impl::Quit() { |
| 74 InternalQuit(PP_OK); |
| 75 } |
| 76 |
| 77 int32_t PPB_Flash_MessageLoop_Impl::InternalRun( |
| 78 const RunFromHostProxyCallback& callback) { |
| 79 if (state_->run_called()) { |
| 80 if (!callback.is_null()) |
| 81 callback.Run(PP_ERROR_FAILED); |
| 82 return PP_ERROR_FAILED; |
| 83 } |
| 84 state_->set_run_called(); |
| 85 state_->set_run_callback(callback); |
| 86 |
| 87 // It is possible that the PPB_Flash_MessageLoop_Impl object has been |
| 88 // destroyed when the nested message loop exits. |
| 89 scoped_refptr<State> state_protector(state_); |
| 90 |
| 91 bool old_value = MessageLoop::current()->NestableTasksAllowed(); |
| 92 MessageLoop::current()->SetNestableTasksAllowed(true); |
| 93 MessageLoop::current()->Run(); |
| 94 |
| 95 // Don't access data members of the class below. |
| 96 |
| 97 MessageLoop::current()->SetNestableTasksAllowed(old_value); |
| 98 return state_protector->result(); |
| 99 } |
| 100 |
| 101 void PPB_Flash_MessageLoop_Impl::InternalQuit(int32_t result) { |
| 102 if (!state_->run_called() || state_->quit_called()) |
| 103 return; |
| 104 state_->set_quit_called(); |
| 105 state_->set_result(result); |
| 106 |
| 107 MessageLoop::current()->QuitNow(); |
| 108 |
| 109 if (!state_->run_callback().is_null()) |
| 110 state_->run_callback().Run(result); |
| 111 } |
| 112 |
| 113 } // namespace ppapi |
| 114 } // namespace webkit |
OLD | NEW |