Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / content / renderer / pepper / ppb_flash_message_loop_impl.cc
blobb64abe66109ef6d966b3a12d3412b1fba18bafc1
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 "content/renderer/pepper/ppb_flash_message_loop_impl.h"
7 #include "base/callback.h"
8 #include "base/message_loop/message_loop.h"
9 #include "ppapi/c/pp_errors.h"
11 using ppapi::thunk::PPB_Flash_MessageLoop_API;
13 namespace content {
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) {}
20 int32_t result() const { return result_; }
21 void set_result(int32_t result) { result_ = result; }
23 bool run_called() const { return run_called_; }
24 void set_run_called() { run_called_ = true; }
26 bool quit_called() const { return quit_called_; }
27 void set_quit_called() { quit_called_ = true; }
29 const RunFromHostProxyCallback& run_callback() const { return run_callback_; }
30 void set_run_callback(const RunFromHostProxyCallback& run_callback) {
31 run_callback_ = run_callback;
34 private:
35 friend class base::RefCounted<State>;
36 virtual ~State() {}
38 int32_t result_;
39 bool run_called_;
40 bool quit_called_;
41 RunFromHostProxyCallback run_callback_;
44 PPB_Flash_MessageLoop_Impl::PPB_Flash_MessageLoop_Impl(PP_Instance instance)
45 : Resource(ppapi::OBJECT_IS_IMPL, instance), state_(new State()) {}
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);
53 // static
54 PP_Resource PPB_Flash_MessageLoop_Impl::Create(PP_Instance instance) {
55 return (new PPB_Flash_MessageLoop_Impl(instance))->GetReference();
58 PPB_Flash_MessageLoop_API*
59 PPB_Flash_MessageLoop_Impl::AsPPB_Flash_MessageLoop_API() {
60 return this;
63 int32_t PPB_Flash_MessageLoop_Impl::Run() {
64 return InternalRun(RunFromHostProxyCallback());
67 void PPB_Flash_MessageLoop_Impl::RunFromHostProxy(
68 const RunFromHostProxyCallback& callback) {
69 InternalRun(callback);
72 void PPB_Flash_MessageLoop_Impl::Quit() { InternalQuit(PP_OK); }
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;
81 state_->set_run_called();
82 state_->set_run_callback(callback);
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_);
88 base::MessageLoop::ScopedNestableTaskAllower allow(
89 base::MessageLoop::current());
90 base::MessageLoop::current()->Run();
92 // Don't access data members of the class below.
94 return state_protector->result();
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);
103 base::MessageLoop::current()->QuitNow();
105 if (!state_->run_callback().is_null())
106 state_->run_callback().Run(result);
109 } // namespace content