Upstream version 7.36.149.0
[platform/framework/web/crosswalk.git] / src / content / renderer / pepper / ppb_flash_message_loop_impl.cc
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 "content/renderer/pepper/ppb_flash_message_loop_impl.h"
6
7 #include "base/callback.h"
8 #include "base/message_loop/message_loop.h"
9 #include "ppapi/c/pp_errors.h"
10
11 using ppapi::thunk::PPB_Flash_MessageLoop_API;
12
13 namespace content {
14
15 class PPB_Flash_MessageLoop_Impl::State
16     : public base::RefCounted<PPB_Flash_MessageLoop_Impl::State> {
17  public:
18   State() : result_(PP_OK), run_called_(false), quit_called_(false) {}
19
20   int32_t result() const { return result_; }
21   void set_result(int32_t result) { result_ = result; }
22
23   bool run_called() const { return run_called_; }
24   void set_run_called() { run_called_ = true; }
25
26   bool quit_called() const { return quit_called_; }
27   void set_quit_called() { quit_called_ = true; }
28
29   const RunFromHostProxyCallback& run_callback() const { return run_callback_; }
30   void set_run_callback(const RunFromHostProxyCallback& run_callback) {
31     run_callback_ = run_callback;
32   }
33
34  private:
35   friend class base::RefCounted<State>;
36   virtual ~State() {}
37
38   int32_t result_;
39   bool run_called_;
40   bool quit_called_;
41   RunFromHostProxyCallback run_callback_;
42 };
43
44 PPB_Flash_MessageLoop_Impl::PPB_Flash_MessageLoop_Impl(PP_Instance instance)
45     : Resource(ppapi::OBJECT_IS_IMPL, instance), state_(new State()) {}
46
47 PPB_Flash_MessageLoop_Impl::~PPB_Flash_MessageLoop_Impl() {
48   // It is a no-op if either Run() hasn't been called or Quit() has been called
49   // to balance the call to Run().
50   InternalQuit(PP_ERROR_ABORTED);
51 }
52
53 // static
54 PP_Resource PPB_Flash_MessageLoop_Impl::Create(PP_Instance instance) {
55   return (new PPB_Flash_MessageLoop_Impl(instance))->GetReference();
56 }
57
58 PPB_Flash_MessageLoop_API*
59 PPB_Flash_MessageLoop_Impl::AsPPB_Flash_MessageLoop_API() {
60   return this;
61 }
62
63 int32_t PPB_Flash_MessageLoop_Impl::Run() {
64   return InternalRun(RunFromHostProxyCallback());
65 }
66
67 void PPB_Flash_MessageLoop_Impl::RunFromHostProxy(
68     const RunFromHostProxyCallback& callback) {
69   InternalRun(callback);
70 }
71
72 void PPB_Flash_MessageLoop_Impl::Quit() { InternalQuit(PP_OK); }
73
74 int32_t PPB_Flash_MessageLoop_Impl::InternalRun(
75     const RunFromHostProxyCallback& callback) {
76   if (state_->run_called()) {
77     if (!callback.is_null())
78       callback.Run(PP_ERROR_FAILED);
79     return PP_ERROR_FAILED;
80   }
81   state_->set_run_called();
82   state_->set_run_callback(callback);
83
84   // It is possible that the PPB_Flash_MessageLoop_Impl object has been
85   // destroyed when the nested message loop exits.
86   scoped_refptr<State> state_protector(state_);
87   {
88     base::MessageLoop::ScopedNestableTaskAllower allow(
89         base::MessageLoop::current());
90     base::MessageLoop::current()->Run();
91   }
92   // Don't access data members of the class below.
93
94   return state_protector->result();
95 }
96
97 void PPB_Flash_MessageLoop_Impl::InternalQuit(int32_t result) {
98   if (!state_->run_called() || state_->quit_called())
99     return;
100   state_->set_quit_called();
101   state_->set_result(result);
102
103   base::MessageLoop::current()->QuitNow();
104
105   if (!state_->run_callback().is_null())
106     state_->run_callback().Run(result);
107 }
108
109 }  // namespace content