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
*);
23 void HandleMessageWrapper(HandleMessageFunc function
,
26 ScopedPPVar message_data
) {
27 CallWhileUnlocked(function
, instance
, user_data
,
31 void HandleBlockingMessageWrapper(HandleBlockingMessageFunc function
,
34 ScopedPPVar message_data
,
35 scoped_ptr
<IPC::Message
> reply_msg
) {
36 PluginDispatcher
* dispatcher
= PluginDispatcher::GetForInstance(instance
);
39 PP_Var result
= PP_MakeUndefined();
40 MessageLoopResource::GetCurrent()->
41 set_currently_handling_blocking_message(true);
43 function
, instance
, user_data
, &message_data
.get(), &result
);
44 MessageLoopResource::GetCurrent()->
45 set_currently_handling_blocking_message(false);
46 PpapiMsg_PPPMessageHandler_HandleBlockingMessage::WriteReplyParams(
48 SerializedVarReturnValue::Convert(dispatcher
, result
),
49 true /* was_handled */);
50 dispatcher
->Send(reply_msg
.release());
56 scoped_ptr
<MessageHandler
> MessageHandler::Create(
58 const PPP_MessageHandler_0_2
* handler_if
,
60 PP_Resource message_loop
,
62 scoped_ptr
<MessageHandler
> result
;
63 // The interface and all function pointers must be valid.
65 !handler_if
->HandleMessage
||
66 !handler_if
->HandleBlockingMessage
||
67 !handler_if
->Destroy
) {
68 *error
= PP_ERROR_BADARGUMENT
;
71 thunk::EnterResourceNoLock
<thunk::PPB_MessageLoop_API
>
72 enter_loop(message_loop
, true);
73 if (enter_loop
.failed()) {
74 *error
= PP_ERROR_BADRESOURCE
;
77 scoped_refptr
<MessageLoopResource
> message_loop_resource(
78 static_cast<MessageLoopResource
*>(enter_loop
.object()));
79 if (message_loop_resource
->is_main_thread_loop()) {
80 *error
= PP_ERROR_WRONG_THREAD
;
84 result
.reset(new MessageHandler(
85 instance
, handler_if
, user_data
, message_loop_resource
));
90 MessageHandler::~MessageHandler() {
91 // It's possible the message_loop_proxy is NULL if that loop has been quit.
92 // In that case, we unfortunately just can't call Destroy.
93 if (message_loop_
->message_loop_proxy().get()) {
94 // The posted task won't have the proxy lock, but that's OK, it doesn't
95 // touch any internal state; it's a direct call on the plugin's function.
96 message_loop_
->message_loop_proxy()->PostTask(FROM_HERE
,
97 base::Bind(handler_if_
->Destroy
,
103 bool MessageHandler::LoopIsValid() const {
104 return !!message_loop_
->message_loop_proxy().get();
107 void MessageHandler::HandleMessage(ScopedPPVar var
) {
108 message_loop_
->message_loop_proxy()->PostTask(FROM_HERE
,
109 RunWhileLocked(base::Bind(&HandleMessageWrapper
,
110 handler_if_
->HandleMessage
,
116 void MessageHandler::HandleBlockingMessage(ScopedPPVar var
,
117 scoped_ptr
<IPC::Message
> reply_msg
) {
118 message_loop_
->message_loop_proxy()->PostTask(FROM_HERE
,
119 RunWhileLocked(base::Bind(&HandleBlockingMessageWrapper
,
120 handler_if_
->HandleBlockingMessage
,
124 base::Passed(reply_msg
.Pass()))));
127 MessageHandler::MessageHandler(
128 PP_Instance instance
,
129 const PPP_MessageHandler_0_2
* handler_if
,
131 scoped_refptr
<MessageLoopResource
> message_loop
)
132 : instance_(instance
),
133 handler_if_(handler_if
),
134 user_data_(user_data
),
135 message_loop_(message_loop
) {