Make castv2 performance test work.
[chromium-blink-merge.git] / ppapi / proxy / message_handler.cc
blobaf9a45f8380eea2def1013c930d6453c628b8534
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"
15 namespace ppapi {
16 namespace proxy {
17 namespace {
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,
24 PP_Instance instance,
25 void* user_data,
26 ScopedPPVar message_data) {
27 CallWhileUnlocked(function, instance, user_data,
28 &message_data.get());
31 void HandleBlockingMessageWrapper(HandleBlockingMessageFunc function,
32 PP_Instance instance,
33 void* user_data,
34 ScopedPPVar message_data,
35 scoped_ptr<IPC::Message> reply_msg) {
36 PluginDispatcher* dispatcher = PluginDispatcher::GetForInstance(instance);
37 if (!dispatcher)
38 return;
39 PP_Var result = PP_MakeUndefined();
40 MessageLoopResource::GetCurrent()->
41 set_currently_handling_blocking_message(true);
42 CallWhileUnlocked(
43 function, instance, user_data, &message_data.get(), &result);
44 MessageLoopResource::GetCurrent()->
45 set_currently_handling_blocking_message(false);
46 PpapiMsg_PPPMessageHandler_HandleBlockingMessage::WriteReplyParams(
47 reply_msg.get(),
48 SerializedVarReturnValue::Convert(dispatcher, result),
49 true /* was_handled */);
50 dispatcher->Send(reply_msg.release());
53 } // namespace
55 // static
56 scoped_ptr<MessageHandler> MessageHandler::Create(
57 PP_Instance instance,
58 const PPP_MessageHandler_0_2* handler_if,
59 void* user_data,
60 PP_Resource message_loop,
61 int32_t* error) {
62 scoped_ptr<MessageHandler> result;
63 // The interface and all function pointers must be valid.
64 if (!handler_if ||
65 !handler_if->HandleMessage ||
66 !handler_if->HandleBlockingMessage ||
67 !handler_if->Destroy) {
68 *error = PP_ERROR_BADARGUMENT;
69 return result.Pass();
71 thunk::EnterResourceNoLock<thunk::PPB_MessageLoop_API>
72 enter_loop(message_loop, true);
73 if (enter_loop.failed()) {
74 *error = PP_ERROR_BADRESOURCE;
75 return result.Pass();
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;
81 return result.Pass();
84 result.reset(new MessageHandler(
85 instance, handler_if, user_data, message_loop_resource));
86 *error = PP_OK;
87 return result.Pass();
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,
98 instance_,
99 user_data_));
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,
111 instance_,
112 user_data_,
113 var)));
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,
121 instance_,
122 user_data_,
123 var,
124 base::Passed(reply_msg.Pass()))));
127 MessageHandler::MessageHandler(
128 PP_Instance instance,
129 const PPP_MessageHandler_0_2* handler_if,
130 void* user_data,
131 scoped_refptr<MessageLoopResource> message_loop)
132 : instance_(instance),
133 handler_if_(handler_if),
134 user_data_(user_data),
135 message_loop_(message_loop) {
138 } // namespace proxy
139 } // namespace ppapi