1 // Copyright 2014 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/message_handler.h"
7 #include "ipc/ipc_message.h"
8 #include "ppapi/proxy/plugin_dispatcher.h"
9 #include "ppapi/proxy/ppapi_messages.h"
10 #include "ppapi/proxy/ppb_message_loop_proxy.h"
11 #include "ppapi/shared_impl/proxy_lock.h"
12 #include "ppapi/shared_impl/scoped_pp_var.h"
13 #include "ppapi/thunk/enter.h"
19 typedef void (*HandleMessageFunc
)(PP_Instance
, void*, const PP_Var
*);
20 typedef void (*HandleBlockingMessageFunc
)(
21 PP_Instance
, void*, const PP_Var
*, PP_Var
*);
22 typedef void (*HandleMessageFunc_0_1
)(PP_Instance
, void*, PP_Var
);
23 typedef PP_Var (*HandleBlockingMessageFunc_0_1
)(PP_Instance
, void*, PP_Var
);
25 void HandleMessageWrapper(HandleMessageFunc function
,
28 ScopedPPVar message_data
) {
29 CallWhileUnlocked(function
, instance
, user_data
,
33 void HandleBlockingMessageWrapper(HandleBlockingMessageFunc function
,
36 ScopedPPVar message_data
,
37 scoped_ptr
<IPC::Message
> reply_msg
) {
38 PluginDispatcher
* dispatcher
= PluginDispatcher::GetForInstance(instance
);
41 PP_Var result
= PP_MakeUndefined();
42 MessageLoopResource::GetCurrent()->
43 set_currently_handling_blocking_message(true);
45 function
, instance
, user_data
, &message_data
.get(), &result
);
46 MessageLoopResource::GetCurrent()->
47 set_currently_handling_blocking_message(false);
48 PpapiMsg_PPPMessageHandler_HandleBlockingMessage::WriteReplyParams(
50 SerializedVarReturnValue::Convert(dispatcher
, result
),
51 true /* was_handled */);
52 dispatcher
->Send(reply_msg
.release());
55 // TODO(dmichael): Remove the 0_1 verions; crbug.com/414398
56 void HandleMessageWrapper_0_1(HandleMessageFunc_0_1 function
,
59 ScopedPPVar message_data
) {
60 CallWhileUnlocked(function
, instance
, user_data
, message_data
.get());
63 void HandleBlockingMessageWrapper_0_1(HandleBlockingMessageFunc_0_1 function
,
66 ScopedPPVar message_data
,
67 scoped_ptr
<IPC::Message
> reply_msg
) {
68 PluginDispatcher
* dispatcher
= PluginDispatcher::GetForInstance(instance
);
71 MessageLoopResource::GetCurrent()->
72 set_currently_handling_blocking_message(true);
73 PP_Var return_value
= CallWhileUnlocked(function
,
77 MessageLoopResource::GetCurrent()->
78 set_currently_handling_blocking_message(false);
79 PpapiMsg_PPPMessageHandler_HandleBlockingMessage::WriteReplyParams(
81 SerializedVarReturnValue::Convert(dispatcher
, return_value
),
82 true /* was_handled */);
83 dispatcher
->Send(reply_msg
.release());
89 scoped_ptr
<MessageHandler
> MessageHandler::Create(
91 const PPP_MessageHandler_0_2
* handler_if
,
93 PP_Resource message_loop
,
95 scoped_ptr
<MessageHandler
> result
;
96 // The interface and all function pointers must be valid.
98 !handler_if
->HandleMessage
||
99 !handler_if
->HandleBlockingMessage
||
100 !handler_if
->Destroy
) {
101 *error
= PP_ERROR_BADARGUMENT
;
102 return result
.Pass();
104 thunk::EnterResourceNoLock
<thunk::PPB_MessageLoop_API
>
105 enter_loop(message_loop
, true);
106 if (enter_loop
.failed()) {
107 *error
= PP_ERROR_BADRESOURCE
;
108 return result
.Pass();
110 scoped_refptr
<MessageLoopResource
> message_loop_resource(
111 static_cast<MessageLoopResource
*>(enter_loop
.object()));
112 if (message_loop_resource
->is_main_thread_loop()) {
113 *error
= PP_ERROR_WRONG_THREAD
;
114 return result
.Pass();
117 result
.reset(new MessageHandler(
118 instance
, handler_if
, user_data
, message_loop_resource
));
120 return result
.Pass();
123 // CreateDeprecated is a near-exact copy of Create, but we'll just delete it
124 // when 0.1 is deprecated, so need to get fancy to avoid code duplication.
125 // TODO(dmichael) crbug.com/414398
127 scoped_ptr
<MessageHandler
> MessageHandler::CreateDeprecated(
128 PP_Instance instance
,
129 const PPP_MessageHandler_0_1_Deprecated
* handler_if
,
131 PP_Resource message_loop
,
133 scoped_ptr
<MessageHandler
> result
;
134 // The interface and all function pointers must be valid.
136 !handler_if
->HandleMessage
||
137 !handler_if
->HandleBlockingMessage
||
138 !handler_if
->Destroy
) {
139 *error
= PP_ERROR_BADARGUMENT
;
140 return result
.Pass();
142 thunk::EnterResourceNoLock
<thunk::PPB_MessageLoop_API
>
143 enter_loop(message_loop
, true);
144 if (enter_loop
.failed()) {
145 *error
= PP_ERROR_BADRESOURCE
;
146 return result
.Pass();
148 scoped_refptr
<MessageLoopResource
> message_loop_resource(
149 static_cast<MessageLoopResource
*>(enter_loop
.object()));
150 if (message_loop_resource
->is_main_thread_loop()) {
151 *error
= PP_ERROR_WRONG_THREAD
;
152 return result
.Pass();
155 result
.reset(new MessageHandler(
156 instance
, handler_if
, user_data
, message_loop_resource
));
158 return result
.Pass();
161 MessageHandler::~MessageHandler() {
162 // It's possible the message_loop_proxy is NULL if that loop has been quit.
163 // In that case, we unfortunately just can't call Destroy.
164 if (message_loop_
->message_loop_proxy().get()) {
165 // The posted task won't have the proxy lock, but that's OK, it doesn't
166 // touch any internal state; it's a direct call on the plugin's function.
167 if (handler_if_0_1_
) {
168 message_loop_
->message_loop_proxy()->PostTask(FROM_HERE
,
169 base::Bind(handler_if_0_1_
->Destroy
,
174 message_loop_
->message_loop_proxy()->PostTask(FROM_HERE
,
175 base::Bind(handler_if_
->Destroy
,
181 bool MessageHandler::LoopIsValid() const {
182 return !!message_loop_
->message_loop_proxy().get();
185 void MessageHandler::HandleMessage(ScopedPPVar var
) {
186 if (handler_if_0_1_
) {
187 // TODO(dmichael): Remove this code path. crbug.com/414398
188 message_loop_
->message_loop_proxy()->PostTask(FROM_HERE
,
189 RunWhileLocked(base::Bind(&HandleMessageWrapper_0_1
,
190 handler_if_0_1_
->HandleMessage
,
196 message_loop_
->message_loop_proxy()->PostTask(FROM_HERE
,
197 RunWhileLocked(base::Bind(&HandleMessageWrapper
,
198 handler_if_
->HandleMessage
,
204 void MessageHandler::HandleBlockingMessage(ScopedPPVar var
,
205 scoped_ptr
<IPC::Message
> reply_msg
) {
206 if (handler_if_0_1_
) {
207 // TODO(dmichael): Remove this code path. crbug.com/414398
208 message_loop_
->message_loop_proxy()->PostTask(FROM_HERE
,
209 RunWhileLocked(base::Bind(&HandleBlockingMessageWrapper_0_1
,
210 handler_if_0_1_
->HandleBlockingMessage
,
214 base::Passed(reply_msg
.Pass()))));
217 message_loop_
->message_loop_proxy()->PostTask(FROM_HERE
,
218 RunWhileLocked(base::Bind(&HandleBlockingMessageWrapper
,
219 handler_if_
->HandleBlockingMessage
,
223 base::Passed(reply_msg
.Pass()))));
226 MessageHandler::MessageHandler(
227 PP_Instance instance
,
228 const PPP_MessageHandler_0_2
* handler_if
,
230 scoped_refptr
<MessageLoopResource
> message_loop
)
231 : instance_(instance
),
232 handler_if_(handler_if
),
233 handler_if_0_1_(NULL
),
234 user_data_(user_data
),
235 message_loop_(message_loop
) {
238 MessageHandler::MessageHandler(
239 PP_Instance instance
,
240 const PPP_MessageHandler_0_1_Deprecated
* handler_if
,
242 scoped_refptr
<MessageLoopResource
> message_loop
)
243 : instance_(instance
),
245 handler_if_0_1_(handler_if
),
246 user_data_(user_data
),
247 message_loop_(message_loop
) {