Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(30)

Side by Side Diff: ppapi/proxy/message_handler.cc

Issue 564573002: PPAPI: Make PPP_MessageHandler work in PNaCl (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Merge Created 6 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « ppapi/proxy/message_handler.h ('k') | ppapi/proxy/ppb_instance_proxy.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2014 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "ppapi/proxy/message_handler.h" 5 #include "ppapi/proxy/message_handler.h"
6 6
7 #include "ipc/ipc_message.h" 7 #include "ipc/ipc_message.h"
8 #include "ppapi/proxy/plugin_dispatcher.h" 8 #include "ppapi/proxy/plugin_dispatcher.h"
9 #include "ppapi/proxy/ppapi_messages.h" 9 #include "ppapi/proxy/ppapi_messages.h"
10 #include "ppapi/proxy/ppb_message_loop_proxy.h" 10 #include "ppapi/proxy/ppb_message_loop_proxy.h"
11 #include "ppapi/shared_impl/proxy_lock.h" 11 #include "ppapi/shared_impl/proxy_lock.h"
12 #include "ppapi/shared_impl/scoped_pp_var.h" 12 #include "ppapi/shared_impl/scoped_pp_var.h"
13 #include "ppapi/thunk/enter.h" 13 #include "ppapi/thunk/enter.h"
14 14
15 namespace ppapi { 15 namespace ppapi {
16 namespace proxy { 16 namespace proxy {
17 namespace { 17 namespace {
18 18
19 typedef void (*HandleMessageFunc)(PP_Instance, void*, PP_Var); 19 typedef void (*HandleMessageFunc)(PP_Instance, void*, const PP_Var*);
20 typedef PP_Var (*HandleBlockingMessageFunc)(PP_Instance, void*, 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);
21 24
22 void HandleMessageWrapper(HandleMessageFunc function, 25 void HandleMessageWrapper(HandleMessageFunc function,
23 PP_Instance instance, 26 PP_Instance instance,
24 void* user_data, 27 void* user_data,
25 ScopedPPVar message_data) { 28 ScopedPPVar message_data) {
26 CallWhileUnlocked(function, instance, user_data, message_data.get()); 29 CallWhileUnlocked(function, instance, user_data,
30 &message_data.get());
27 } 31 }
28 32
29 void HandleBlockingMessageWrapper(HandleBlockingMessageFunc function, 33 void HandleBlockingMessageWrapper(HandleBlockingMessageFunc function,
30 PP_Instance instance, 34 PP_Instance instance,
31 void* user_data, 35 void* user_data,
32 ScopedPPVar message_data, 36 ScopedPPVar message_data,
33 scoped_ptr<IPC::Message> reply_msg) { 37 scoped_ptr<IPC::Message> reply_msg) {
34 PluginDispatcher* dispatcher = PluginDispatcher::GetForInstance(instance); 38 PluginDispatcher* dispatcher = PluginDispatcher::GetForInstance(instance);
35 if (!dispatcher) 39 if (!dispatcher)
36 return; 40 return;
41 PP_Var result = PP_MakeUndefined();
42 CallWhileUnlocked(
43 function, instance, user_data, &message_data.get(), &result);
44 PpapiMsg_PPPMessageHandler_HandleBlockingMessage::WriteReplyParams(
45 reply_msg.get(),
46 SerializedVarReturnValue::Convert(dispatcher, result),
47 true /* was_handled */);
48 dispatcher->Send(reply_msg.release());
49 }
50
51 // TODO(dmichael): Remove the 0_1 verions; crbug.com/414398
52 void HandleMessageWrapper_0_1(HandleMessageFunc_0_1 function,
53 PP_Instance instance,
54 void* user_data,
55 ScopedPPVar message_data) {
56 CallWhileUnlocked(function, instance, user_data, message_data.get());
57 }
58
59 void HandleBlockingMessageWrapper_0_1(HandleBlockingMessageFunc_0_1 function,
60 PP_Instance instance,
61 void* user_data,
62 ScopedPPVar message_data,
63 scoped_ptr<IPC::Message> reply_msg) {
64 PluginDispatcher* dispatcher = PluginDispatcher::GetForInstance(instance);
65 if (!dispatcher)
66 return;
37 PP_Var return_value = CallWhileUnlocked(function, 67 PP_Var return_value = CallWhileUnlocked(function,
38 instance, 68 instance,
39 user_data, 69 user_data,
40 message_data.get()); 70 message_data.get());
41 PpapiMsg_PPPMessageHandler_HandleBlockingMessage::WriteReplyParams( 71 PpapiMsg_PPPMessageHandler_HandleBlockingMessage::WriteReplyParams(
42 reply_msg.get(), 72 reply_msg.get(),
43 SerializedVarReturnValue::Convert(dispatcher, return_value), 73 SerializedVarReturnValue::Convert(dispatcher, return_value),
44 true /* was_handled */); 74 true /* was_handled */);
45 dispatcher->Send(reply_msg.release()); 75 dispatcher->Send(reply_msg.release());
46 } 76 }
47 77
48 } // namespace 78 } // namespace
49 79
50 // static 80 // static
51 scoped_ptr<MessageHandler> MessageHandler::Create( 81 scoped_ptr<MessageHandler> MessageHandler::Create(
52 PP_Instance instance, 82 PP_Instance instance,
53 const PPP_MessageHandler_0_1* handler_if, 83 const PPP_MessageHandler_0_2* handler_if,
54 void* user_data, 84 void* user_data,
55 PP_Resource message_loop, 85 PP_Resource message_loop,
56 int32_t* error) { 86 int32_t* error) {
87 scoped_ptr<MessageHandler> result;
88 // The interface and all function pointers must be valid.
89 if (!handler_if ||
90 !handler_if->HandleMessage ||
91 !handler_if->HandleBlockingMessage ||
92 !handler_if->Destroy) {
93 *error = PP_ERROR_BADARGUMENT;
94 return result.Pass();
95 }
96 thunk::EnterResourceNoLock<thunk::PPB_MessageLoop_API>
97 enter_loop(message_loop, true);
98 if (enter_loop.failed()) {
99 *error = PP_ERROR_BADRESOURCE;
100 return result.Pass();
101 }
102 scoped_refptr<MessageLoopResource> message_loop_resource(
103 static_cast<MessageLoopResource*>(enter_loop.object()));
104 if (message_loop_resource->is_main_thread_loop()) {
105 *error = PP_ERROR_WRONG_THREAD;
106 return result.Pass();
107 }
108
109 result.reset(new MessageHandler(
110 instance, handler_if, user_data, message_loop_resource));
111 *error = PP_OK;
112 return result.Pass();
113 }
114
115 // CreateDeprecated is a near-exact copy of Create, but we'll just delete it
116 // when 0.1 is deprecated, so need to get fancy to avoid code duplication.
117 // TODO(dmichael) crbug.com/414398
118 // static
119 scoped_ptr<MessageHandler> MessageHandler::CreateDeprecated(
120 PP_Instance instance,
121 const PPP_MessageHandler_0_1_Deprecated* handler_if,
122 void* user_data,
123 PP_Resource message_loop,
124 int32_t* error) {
57 scoped_ptr<MessageHandler> result; 125 scoped_ptr<MessageHandler> result;
58 // The interface and all function pointers must be valid. 126 // The interface and all function pointers must be valid.
59 if (!handler_if || 127 if (!handler_if ||
60 !handler_if->HandleMessage || 128 !handler_if->HandleMessage ||
61 !handler_if->HandleBlockingMessage || 129 !handler_if->HandleBlockingMessage ||
62 !handler_if->Destroy) { 130 !handler_if->Destroy) {
63 *error = PP_ERROR_BADARGUMENT; 131 *error = PP_ERROR_BADARGUMENT;
64 return result.Pass(); 132 return result.Pass();
65 } 133 }
66 thunk::EnterResourceNoLock<thunk::PPB_MessageLoop_API> 134 thunk::EnterResourceNoLock<thunk::PPB_MessageLoop_API>
(...skipping 14 matching lines...) Expand all
81 *error = PP_OK; 149 *error = PP_OK;
82 return result.Pass(); 150 return result.Pass();
83 } 151 }
84 152
85 MessageHandler::~MessageHandler() { 153 MessageHandler::~MessageHandler() {
86 // It's possible the message_loop_proxy is NULL if that loop has been quit. 154 // It's possible the message_loop_proxy is NULL if that loop has been quit.
87 // In that case, we unfortunately just can't call Destroy. 155 // In that case, we unfortunately just can't call Destroy.
88 if (message_loop_->message_loop_proxy().get()) { 156 if (message_loop_->message_loop_proxy().get()) {
89 // The posted task won't have the proxy lock, but that's OK, it doesn't 157 // The posted task won't have the proxy lock, but that's OK, it doesn't
90 // touch any internal state; it's a direct call on the plugin's function. 158 // touch any internal state; it's a direct call on the plugin's function.
159 if (handler_if_0_1_) {
160 message_loop_->message_loop_proxy()->PostTask(FROM_HERE,
161 base::Bind(handler_if_0_1_->Destroy,
162 instance_,
163 user_data_));
164 return;
165 }
91 message_loop_->message_loop_proxy()->PostTask(FROM_HERE, 166 message_loop_->message_loop_proxy()->PostTask(FROM_HERE,
92 base::Bind(handler_if_->Destroy, 167 base::Bind(handler_if_->Destroy,
93 instance_, 168 instance_,
94 user_data_)); 169 user_data_));
95 } 170 }
96 } 171 }
97 172
98 bool MessageHandler::LoopIsValid() const { 173 bool MessageHandler::LoopIsValid() const {
99 return !!message_loop_->message_loop_proxy().get(); 174 return !!message_loop_->message_loop_proxy().get();
100 } 175 }
101 176
102 void MessageHandler::HandleMessage(ScopedPPVar var) { 177 void MessageHandler::HandleMessage(ScopedPPVar var) {
178 if (handler_if_0_1_) {
179 // TODO(dmichael): Remove this code path. crbug.com/414398
180 message_loop_->message_loop_proxy()->PostTask(FROM_HERE,
181 RunWhileLocked(base::Bind(&HandleMessageWrapper_0_1,
182 handler_if_0_1_->HandleMessage,
183 instance_,
184 user_data_,
185 var)));
186 return;
187 }
103 message_loop_->message_loop_proxy()->PostTask(FROM_HERE, 188 message_loop_->message_loop_proxy()->PostTask(FROM_HERE,
104 RunWhileLocked(base::Bind(&HandleMessageWrapper, 189 RunWhileLocked(base::Bind(&HandleMessageWrapper,
105 handler_if_->HandleMessage, 190 handler_if_->HandleMessage,
106 instance_, 191 instance_,
107 user_data_, 192 user_data_,
108 var))); 193 var)));
109 } 194 }
110 195
111 void MessageHandler::HandleBlockingMessage(ScopedPPVar var, 196 void MessageHandler::HandleBlockingMessage(ScopedPPVar var,
112 scoped_ptr<IPC::Message> reply_msg) { 197 scoped_ptr<IPC::Message> reply_msg) {
198 if (handler_if_0_1_) {
199 // TODO(dmichael): Remove this code path. crbug.com/414398
200 message_loop_->message_loop_proxy()->PostTask(FROM_HERE,
201 RunWhileLocked(base::Bind(&HandleBlockingMessageWrapper_0_1,
202 handler_if_0_1_->HandleBlockingMessage,
203 instance_,
204 user_data_,
205 var,
206 base::Passed(reply_msg.Pass()))));
207 return;
208 }
113 message_loop_->message_loop_proxy()->PostTask(FROM_HERE, 209 message_loop_->message_loop_proxy()->PostTask(FROM_HERE,
114 RunWhileLocked(base::Bind(&HandleBlockingMessageWrapper, 210 RunWhileLocked(base::Bind(&HandleBlockingMessageWrapper,
115 handler_if_->HandleBlockingMessage, 211 handler_if_->HandleBlockingMessage,
116 instance_, 212 instance_,
117 user_data_, 213 user_data_,
118 var, 214 var,
119 base::Passed(reply_msg.Pass())))); 215 base::Passed(reply_msg.Pass()))));
120 } 216 }
121 217
122 MessageHandler::MessageHandler( 218 MessageHandler::MessageHandler(
123 PP_Instance instance, 219 PP_Instance instance,
124 const PPP_MessageHandler_0_1* handler_if, 220 const PPP_MessageHandler_0_2* handler_if,
125 void* user_data, 221 void* user_data,
126 scoped_refptr<MessageLoopResource> message_loop) 222 scoped_refptr<MessageLoopResource> message_loop)
127 : instance_(instance), 223 : instance_(instance),
128 handler_if_(handler_if), 224 handler_if_(handler_if),
225 handler_if_0_1_(NULL),
129 user_data_(user_data), 226 user_data_(user_data),
130 message_loop_(message_loop) { 227 message_loop_(message_loop) {
131 } 228 }
229
230 MessageHandler::MessageHandler(
231 PP_Instance instance,
232 const PPP_MessageHandler_0_1_Deprecated* handler_if,
233 void* user_data,
234 scoped_refptr<MessageLoopResource> message_loop)
235 : instance_(instance),
236 handler_if_(NULL),
237 handler_if_0_1_(handler_if),
238 user_data_(user_data),
239 message_loop_(message_loop) {
240 }
132 241
133 } // namespace proxy 242 } // namespace proxy
134 } // namespace ppapi 243 } // namespace ppapi
OLDNEW
« no previous file with comments | « ppapi/proxy/message_handler.h ('k') | ppapi/proxy/ppb_instance_proxy.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698