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"
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
;
24 class FlashMessageLoop
: public PPB_Flash_MessageLoop_API
, public Resource
{
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
;
35 void RunFromHostProxy(const RunFromHostProxyCallback
& callback
) override
;
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() {
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
);
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.
75 PPB_Flash_MessageLoop_Proxy::PPB_Flash_MessageLoop_Proxy(Dispatcher
* dispatcher
)
76 : InterfaceProxy(dispatcher
) {
79 PPB_Flash_MessageLoop_Proxy::~PPB_Flash_MessageLoop_Proxy() {
83 PP_Resource
PPB_Flash_MessageLoop_Proxy::CreateProxyResource(
84 PP_Instance instance
) {
85 PluginDispatcher
* dispatcher
= PluginDispatcher::GetForInstance(instance
);
90 dispatcher
->Send(new PpapiHostMsg_PPBFlashMessageLoop_Create(
91 API_ID_PPB_FLASH_MESSAGELOOP
, instance
, &result
));
94 return (new FlashMessageLoop(result
))->GetReference();
97 bool PPB_Flash_MessageLoop_Proxy::OnMessageReceived(const IPC::Message
& msg
) {
98 if (!dispatcher()->permissions().HasPermission(PERMISSION_FLASH
))
102 IPC_BEGIN_MESSAGE_MAP(PPB_Flash_MessageLoop_Proxy
, msg
)
103 IPC_MESSAGE_HANDLER(PpapiHostMsg_PPBFlashMessageLoop_Create
,
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
109 IPC_MESSAGE_HANDLER_DELAY_REPLY(PpapiHostMsg_PPBFlashMessageLoop_Run
,
111 IPC_MESSAGE_HANDLER(PpapiHostMsg_PPBFlashMessageLoop_Quit
,
113 IPC_MESSAGE_UNHANDLED(handled
= false)
114 IPC_END_MESSAGE_MAP()
118 void PPB_Flash_MessageLoop_Proxy::OnMsgCreate(PP_Instance instance
,
119 HostResource
* result
) {
120 if (!dispatcher()->permissions().HasPermission(PERMISSION_FLASH
))
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
))
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
);
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
,
158 PpapiHostMsg_PPBFlashMessageLoop_Run::WriteReplyParams(reply_message
.get(),
160 Send(reply_message
.release());