OLD | NEW |
(Empty) | |
| 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 "ppapi/proxy/ppb_flash_message_loop_proxy.h" |
| 6 |
| 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" |
| 17 |
| 18 using ppapi::thunk::PPB_Flash_MessageLoop_API; |
| 19 |
| 20 namespace ppapi { |
| 21 namespace proxy { |
| 22 namespace { |
| 23 |
| 24 class FlashMessageLoop : public PPB_Flash_MessageLoop_API, public Resource { |
| 25 public: |
| 26 explicit FlashMessageLoop(const HostResource& resource); |
| 27 virtual ~FlashMessageLoop(); |
| 28 |
| 29 // Resource overrides. |
| 30 virtual PPB_Flash_MessageLoop_API* AsPPB_Flash_MessageLoop_API() OVERRIDE; |
| 31 |
| 32 // PPB_Flash_MesssageLoop_API implementation. |
| 33 virtual int32_t Run() OVERRIDE; |
| 34 virtual void Quit() OVERRIDE; |
| 35 virtual void RunFromHostProxy( |
| 36 const RunFromHostProxyCallback& callback) OVERRIDE; |
| 37 |
| 38 private: |
| 39 DISALLOW_COPY_AND_ASSIGN(FlashMessageLoop); |
| 40 }; |
| 41 |
| 42 FlashMessageLoop::FlashMessageLoop(const HostResource& resource) |
| 43 : Resource(resource) { |
| 44 } |
| 45 |
| 46 FlashMessageLoop::~FlashMessageLoop() { |
| 47 } |
| 48 |
| 49 PPB_Flash_MessageLoop_API* FlashMessageLoop::AsPPB_Flash_MessageLoop_API() { |
| 50 return this; |
| 51 } |
| 52 |
| 53 int32_t FlashMessageLoop::Run() { |
| 54 int32_t result = PP_ERROR_FAILED; |
| 55 IPC::SyncMessage* msg = new PpapiHostMsg_PPBFlashMessageLoop_Run( |
| 56 API_ID_PPB_FLASH_MESSAGELOOP, host_resource(), &result); |
| 57 msg->EnableMessagePumping(); |
| 58 PluginDispatcher::GetForResource(this)->Send(msg); |
| 59 return result; |
| 60 } |
| 61 |
| 62 void FlashMessageLoop::Quit() { |
| 63 PluginDispatcher::GetForResource(this)->Send( |
| 64 new PpapiHostMsg_PPBFlashMessageLoop_Quit( |
| 65 API_ID_PPB_FLASH_MESSAGELOOP, host_resource())); |
| 66 } |
| 67 |
| 68 void FlashMessageLoop::RunFromHostProxy( |
| 69 const RunFromHostProxyCallback& callback) { |
| 70 // This should never be called on the plugin side. |
| 71 NOTREACHED(); |
| 72 } |
| 73 |
| 74 } // namespace |
| 75 |
| 76 PPB_Flash_MessageLoop_Proxy::PPB_Flash_MessageLoop_Proxy(Dispatcher* dispatcher) |
| 77 : InterfaceProxy(dispatcher) { |
| 78 } |
| 79 |
| 80 PPB_Flash_MessageLoop_Proxy::~PPB_Flash_MessageLoop_Proxy() { |
| 81 } |
| 82 |
| 83 // static |
| 84 PP_Resource PPB_Flash_MessageLoop_Proxy::CreateProxyResource( |
| 85 PP_Instance instance) { |
| 86 PluginDispatcher* dispatcher = PluginDispatcher::GetForInstance(instance); |
| 87 if (!dispatcher) |
| 88 return 0; |
| 89 |
| 90 HostResource result; |
| 91 dispatcher->Send(new PpapiHostMsg_PPBFlashMessageLoop_Create( |
| 92 API_ID_PPB_FLASH_MESSAGELOOP, instance, &result)); |
| 93 if (result.is_null()) |
| 94 return 0; |
| 95 return (new FlashMessageLoop(result))->GetReference(); |
| 96 } |
| 97 |
| 98 bool PPB_Flash_MessageLoop_Proxy::OnMessageReceived(const IPC::Message& msg) { |
| 99 bool handled = true; |
| 100 IPC_BEGIN_MESSAGE_MAP(PPB_Flash_MessageLoop_Proxy, msg) |
| 101 IPC_MESSAGE_HANDLER(PpapiHostMsg_PPBFlashMessageLoop_Create, |
| 102 OnMsgCreate) |
| 103 // We cannot use IPC_MESSAGE_HANDLER here. Because it tries to send the sync |
| 104 // message reply after the handler returns. However, in this case, the |
| 105 // PPB_Flash_MessageLoop_Proxy object may be destroyed before the handler |
| 106 // returns. |
| 107 IPC_MESSAGE_HANDLER_DELAY_REPLY(PpapiHostMsg_PPBFlashMessageLoop_Run, |
| 108 OnMsgRun) |
| 109 IPC_MESSAGE_HANDLER(PpapiHostMsg_PPBFlashMessageLoop_Quit, |
| 110 OnMsgQuit) |
| 111 IPC_MESSAGE_UNHANDLED(handled = false) |
| 112 IPC_END_MESSAGE_MAP() |
| 113 return handled; |
| 114 } |
| 115 |
| 116 void PPB_Flash_MessageLoop_Proxy::OnMsgCreate(PP_Instance instance, |
| 117 HostResource* result) { |
| 118 thunk::EnterResourceCreation enter(instance); |
| 119 if (enter.succeeded()) { |
| 120 result->SetHostResource( |
| 121 instance, enter.functions()->CreateFlashMessageLoop(instance)); |
| 122 } |
| 123 } |
| 124 |
| 125 void PPB_Flash_MessageLoop_Proxy::OnMsgRun( |
| 126 const HostResource& flash_message_loop, |
| 127 IPC::Message* reply) { |
| 128 PPB_Flash_MessageLoop_API::RunFromHostProxyCallback callback = |
| 129 base::Bind(&PPB_Flash_MessageLoop_Proxy::WillQuitSoon, AsWeakPtr(), |
| 130 base::Passed(scoped_ptr<IPC::Message>(reply))); |
| 131 |
| 132 EnterHostFromHostResource<PPB_Flash_MessageLoop_API> |
| 133 enter(flash_message_loop); |
| 134 if (enter.succeeded()) |
| 135 enter.object()->RunFromHostProxy(callback); |
| 136 else |
| 137 callback.Run(PP_ERROR_BADRESOURCE); |
| 138 } |
| 139 |
| 140 void PPB_Flash_MessageLoop_Proxy::OnMsgQuit( |
| 141 const ppapi::HostResource& flash_message_loop) { |
| 142 EnterHostFromHostResource<PPB_Flash_MessageLoop_API> |
| 143 enter(flash_message_loop); |
| 144 if (enter.succeeded()) |
| 145 enter.object()->Quit(); |
| 146 } |
| 147 |
| 148 void PPB_Flash_MessageLoop_Proxy::WillQuitSoon( |
| 149 scoped_ptr<IPC::Message> reply_message, |
| 150 int32_t result) { |
| 151 PpapiHostMsg_PPBFlashMessageLoop_Run::WriteReplyParams(reply_message.get(), |
| 152 result); |
| 153 Send(reply_message.release()); |
| 154 } |
| 155 |
| 156 } // namespace proxy |
| 157 } // namespace ppapi |
OLD | NEW |