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