* Changing crash report limitation logic to have more control over extreme cases.
[chromium-blink-merge.git] / ppapi / proxy / message_handler.cc
blob9fe95b2154e14df3810c6ff0545386de1e830aaa
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 "base/location.h"
8 #include "base/single_thread_task_runner.h"
9 #include "ipc/ipc_message.h"
10 #include "ppapi/proxy/plugin_dispatcher.h"
11 #include "ppapi/proxy/ppapi_messages.h"
12 #include "ppapi/proxy/ppb_message_loop_proxy.h"
13 #include "ppapi/shared_impl/proxy_lock.h"
14 #include "ppapi/shared_impl/scoped_pp_var.h"
15 #include "ppapi/thunk/enter.h"
17 namespace ppapi {
18 namespace proxy {
19 namespace {
21 typedef void (*HandleMessageFunc)(PP_Instance, void*, const PP_Var*);
22 typedef void (*HandleBlockingMessageFunc)(
23 PP_Instance, void*, const PP_Var*, PP_Var*);
25 void HandleMessageWrapper(HandleMessageFunc function,
26 PP_Instance instance,
27 void* user_data,
28 ScopedPPVar message_data) {
29 CallWhileUnlocked(function, instance, user_data,
30 &message_data.get());
33 void HandleBlockingMessageWrapper(HandleBlockingMessageFunc function,
34 PP_Instance instance,
35 void* user_data,
36 ScopedPPVar message_data,
37 scoped_ptr<IPC::Message> reply_msg) {
38 PluginDispatcher* dispatcher = PluginDispatcher::GetForInstance(instance);
39 if (!dispatcher)
40 return;
41 PP_Var result = PP_MakeUndefined();
42 MessageLoopResource::GetCurrent()->
43 set_currently_handling_blocking_message(true);
44 CallWhileUnlocked(
45 function, instance, user_data, &message_data.get(), &result);
46 MessageLoopResource::GetCurrent()->
47 set_currently_handling_blocking_message(false);
48 PpapiMsg_PPPMessageHandler_HandleBlockingMessage::WriteReplyParams(
49 reply_msg.get(),
50 SerializedVarReturnValue::Convert(dispatcher, result),
51 true /* was_handled */);
52 dispatcher->Send(reply_msg.release());
55 } // namespace
57 // static
58 scoped_ptr<MessageHandler> MessageHandler::Create(
59 PP_Instance instance,
60 const PPP_MessageHandler_0_2* handler_if,
61 void* user_data,
62 PP_Resource message_loop,
63 int32_t* error) {
64 scoped_ptr<MessageHandler> result;
65 // The interface and all function pointers must be valid.
66 if (!handler_if ||
67 !handler_if->HandleMessage ||
68 !handler_if->HandleBlockingMessage ||
69 !handler_if->Destroy) {
70 *error = PP_ERROR_BADARGUMENT;
71 return result.Pass();
73 thunk::EnterResourceNoLock<thunk::PPB_MessageLoop_API>
74 enter_loop(message_loop, true);
75 if (enter_loop.failed()) {
76 *error = PP_ERROR_BADRESOURCE;
77 return result.Pass();
79 scoped_refptr<MessageLoopResource> message_loop_resource(
80 static_cast<MessageLoopResource*>(enter_loop.object()));
81 if (message_loop_resource->is_main_thread_loop()) {
82 *error = PP_ERROR_WRONG_THREAD;
83 return result.Pass();
86 result.reset(new MessageHandler(
87 instance, handler_if, user_data, message_loop_resource));
88 *error = PP_OK;
89 return result.Pass();
92 MessageHandler::~MessageHandler() {
93 // It's possible the message_loop_proxy is NULL if that loop has been quit.
94 // In that case, we unfortunately just can't call Destroy.
95 if (message_loop_->task_runner().get()) {
96 // The posted task won't have the proxy lock, but that's OK, it doesn't
97 // touch any internal state; it's a direct call on the plugin's function.
98 message_loop_->task_runner()->PostTask(
99 FROM_HERE, base::Bind(handler_if_->Destroy, instance_, user_data_));
103 bool MessageHandler::LoopIsValid() const {
104 return !!message_loop_->task_runner().get();
107 void MessageHandler::HandleMessage(ScopedPPVar var) {
108 message_loop_->task_runner()->PostTask(
109 FROM_HERE, RunWhileLocked(base::Bind(&HandleMessageWrapper,
110 handler_if_->HandleMessage,
111 instance_, user_data_, var)));
114 void MessageHandler::HandleBlockingMessage(ScopedPPVar var,
115 scoped_ptr<IPC::Message> reply_msg) {
116 message_loop_->task_runner()->PostTask(
117 FROM_HERE,
118 RunWhileLocked(base::Bind(
119 &HandleBlockingMessageWrapper, handler_if_->HandleBlockingMessage,
120 instance_, user_data_, var, base::Passed(reply_msg.Pass()))));
123 MessageHandler::MessageHandler(
124 PP_Instance instance,
125 const PPP_MessageHandler_0_2* handler_if,
126 void* user_data,
127 scoped_refptr<MessageLoopResource> message_loop)
128 : instance_(instance),
129 handler_if_(handler_if),
130 user_data_(user_data),
131 message_loop_(message_loop) {
134 } // namespace proxy
135 } // namespace ppapi