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.
5 #include "webkit/plugins/ppapi/ppb_flash_message_loop_impl.h"
7 #include "base/callback.h"
8 #include "base/message_loop.h"
9 #include "ppapi/c/pp_errors.h"
11 using ppapi::thunk::PPB_Flash_MessageLoop_API
;
16 class PPB_Flash_MessageLoop_Impl::State
17 : public base::RefCounted
<PPB_Flash_MessageLoop_Impl::State
> {
19 State() : result_(PP_OK
), run_called_(false), quit_called_(false) {
22 int32_t result() const { return result_
; }
23 void set_result(int32_t result
) { result_
= result
; }
25 bool run_called() const { return run_called_
; }
26 void set_run_called() { run_called_
= true; }
28 bool quit_called() const { return quit_called_
; }
29 void set_quit_called() { quit_called_
= true; }
31 const RunFromHostProxyCallback
& run_callback() const { return run_callback_
; }
32 void set_run_callback(const RunFromHostProxyCallback
& run_callback
) {
33 run_callback_
= run_callback
;
40 RunFromHostProxyCallback run_callback_
;
43 PPB_Flash_MessageLoop_Impl::PPB_Flash_MessageLoop_Impl(PP_Instance instance
)
44 : Resource(::ppapi::OBJECT_IS_IMPL
, instance
),
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
);
55 PP_Resource
PPB_Flash_MessageLoop_Impl::Create(PP_Instance instance
) {
56 return (new PPB_Flash_MessageLoop_Impl(instance
))->GetReference();
59 PPB_Flash_MessageLoop_API
*
60 PPB_Flash_MessageLoop_Impl::AsPPB_Flash_MessageLoop_API() {
64 int32_t PPB_Flash_MessageLoop_Impl::Run() {
65 return InternalRun(RunFromHostProxyCallback());
68 void PPB_Flash_MessageLoop_Impl::RunFromHostProxy(
69 const RunFromHostProxyCallback
& callback
) {
70 InternalRun(callback
);
73 void PPB_Flash_MessageLoop_Impl::Quit() {
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
;
84 state_
->set_run_called();
85 state_
->set_run_callback(callback
);
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_
);
91 MessageLoop::ScopedNestableTaskAllower
allow(MessageLoop::current());
92 MessageLoop::current()->Run();
94 // Don't access data members of the class below.
96 return state_protector
->result();
99 void PPB_Flash_MessageLoop_Impl::InternalQuit(int32_t result
) {
100 if (!state_
->run_called() || state_
->quit_called())
102 state_
->set_quit_called();
103 state_
->set_result(result
);
105 MessageLoop::current()->QuitNow();
107 if (!state_
->run_callback().is_null())
108 state_
->run_callback().Run(result
);
112 } // namespace webkit