Address some additional cleanup work needed for the component flash updates on Linux.
[chromium-blink-merge.git] / ppapi / proxy / ppb_flash_message_loop_proxy.cc
blob1b332185293a5b5be7cbac8f908edf00a09d1004
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 "ppapi/proxy/ppb_flash_message_loop_proxy.h"
7 #include "base/bind.h"
8 #include "ppapi/c/pp_errors.h"
9 #include "ppapi/c/private/ppb_flash_message_loop.h"
10 #include "ppapi/proxy/enter_proxy.h"
11 #include "ppapi/proxy/plugin_dispatcher.h"
12 #include "ppapi/proxy/ppapi_messages.h"
13 #include "ppapi/shared_impl/resource.h"
14 #include "ppapi/thunk/enter.h"
15 #include "ppapi/thunk/ppb_flash_message_loop_api.h"
16 #include "ppapi/thunk/resource_creation_api.h"
18 using ppapi::thunk::PPB_Flash_MessageLoop_API;
20 namespace ppapi {
21 namespace proxy {
22 namespace {
24 class FlashMessageLoop : public PPB_Flash_MessageLoop_API, public Resource {
25 public:
26 explicit FlashMessageLoop(const HostResource& resource);
27 ~FlashMessageLoop() override;
29 // Resource overrides.
30 PPB_Flash_MessageLoop_API* AsPPB_Flash_MessageLoop_API() override;
32 // PPB_Flash_MesssageLoop_API implementation.
33 int32_t Run() override;
34 void Quit() override;
35 void RunFromHostProxy(const RunFromHostProxyCallback& callback) override;
37 private:
38 DISALLOW_COPY_AND_ASSIGN(FlashMessageLoop);
41 FlashMessageLoop::FlashMessageLoop(const HostResource& resource)
42 : Resource(OBJECT_IS_PROXY, resource) {
45 FlashMessageLoop::~FlashMessageLoop() {
48 PPB_Flash_MessageLoop_API* FlashMessageLoop::AsPPB_Flash_MessageLoop_API() {
49 return this;
52 int32_t FlashMessageLoop::Run() {
53 int32_t result = PP_ERROR_FAILED;
54 IPC::SyncMessage* msg = new PpapiHostMsg_PPBFlashMessageLoop_Run(
55 API_ID_PPB_FLASH_MESSAGELOOP, host_resource(), &result);
56 msg->EnableMessagePumping();
57 PluginDispatcher::GetForResource(this)->Send(msg);
58 return result;
61 void FlashMessageLoop::Quit() {
62 PluginDispatcher::GetForResource(this)->Send(
63 new PpapiHostMsg_PPBFlashMessageLoop_Quit(
64 API_ID_PPB_FLASH_MESSAGELOOP, host_resource()));
67 void FlashMessageLoop::RunFromHostProxy(
68 const RunFromHostProxyCallback& callback) {
69 // This should never be called on the plugin side.
70 NOTREACHED();
73 } // namespace
75 PPB_Flash_MessageLoop_Proxy::PPB_Flash_MessageLoop_Proxy(Dispatcher* dispatcher)
76 : InterfaceProxy(dispatcher) {
79 PPB_Flash_MessageLoop_Proxy::~PPB_Flash_MessageLoop_Proxy() {
82 // static
83 PP_Resource PPB_Flash_MessageLoop_Proxy::CreateProxyResource(
84 PP_Instance instance) {
85 PluginDispatcher* dispatcher = PluginDispatcher::GetForInstance(instance);
86 if (!dispatcher)
87 return 0;
89 HostResource result;
90 dispatcher->Send(new PpapiHostMsg_PPBFlashMessageLoop_Create(
91 API_ID_PPB_FLASH_MESSAGELOOP, instance, &result));
92 if (result.is_null())
93 return 0;
94 return (new FlashMessageLoop(result))->GetReference();
97 bool PPB_Flash_MessageLoop_Proxy::OnMessageReceived(const IPC::Message& msg) {
98 if (!dispatcher()->permissions().HasPermission(PERMISSION_FLASH))
99 return false;
101 bool handled = true;
102 IPC_BEGIN_MESSAGE_MAP(PPB_Flash_MessageLoop_Proxy, msg)
103 IPC_MESSAGE_HANDLER(PpapiHostMsg_PPBFlashMessageLoop_Create,
104 OnMsgCreate)
105 // We cannot use IPC_MESSAGE_HANDLER here. Because it tries to send the sync
106 // message reply after the handler returns. However, in this case, the
107 // PPB_Flash_MessageLoop_Proxy object may be destroyed before the handler
108 // returns.
109 IPC_MESSAGE_HANDLER_DELAY_REPLY(PpapiHostMsg_PPBFlashMessageLoop_Run,
110 OnMsgRun)
111 IPC_MESSAGE_HANDLER(PpapiHostMsg_PPBFlashMessageLoop_Quit,
112 OnMsgQuit)
113 IPC_MESSAGE_UNHANDLED(handled = false)
114 IPC_END_MESSAGE_MAP()
115 return handled;
118 void PPB_Flash_MessageLoop_Proxy::OnMsgCreate(PP_Instance instance,
119 HostResource* result) {
120 if (!dispatcher()->permissions().HasPermission(PERMISSION_FLASH))
121 return;
122 thunk::EnterResourceCreation enter(instance);
123 if (enter.succeeded()) {
124 result->SetHostResource(
125 instance, enter.functions()->CreateFlashMessageLoop(instance));
129 void PPB_Flash_MessageLoop_Proxy::OnMsgRun(
130 const HostResource& flash_message_loop,
131 IPC::Message* reply) {
132 if (!dispatcher()->permissions().HasPermission(PERMISSION_FLASH))
133 return;
135 PPB_Flash_MessageLoop_API::RunFromHostProxyCallback callback =
136 base::Bind(&PPB_Flash_MessageLoop_Proxy::WillQuitSoon, AsWeakPtr(),
137 base::Passed(scoped_ptr<IPC::Message>(reply)));
139 EnterHostFromHostResource<PPB_Flash_MessageLoop_API>
140 enter(flash_message_loop);
141 if (enter.succeeded())
142 enter.object()->RunFromHostProxy(callback);
143 else
144 callback.Run(PP_ERROR_BADRESOURCE);
147 void PPB_Flash_MessageLoop_Proxy::OnMsgQuit(
148 const ppapi::HostResource& flash_message_loop) {
149 EnterHostFromHostResource<PPB_Flash_MessageLoop_API>
150 enter(flash_message_loop);
151 if (enter.succeeded())
152 enter.object()->Quit();
155 void PPB_Flash_MessageLoop_Proxy::WillQuitSoon(
156 scoped_ptr<IPC::Message> reply_message,
157 int32_t result) {
158 PpapiHostMsg_PPBFlashMessageLoop_Run::WriteReplyParams(reply_message.get(),
159 result);
160 Send(reply_message.release());
163 } // namespace proxy
164 } // namespace ppapi