summaryrefslogtreecommitdiffstats
path: root/ppapi/proxy/message_handler.cc
blob: a0f5cccb18393e58f0971c97652505abc8b07a9a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "ppapi/proxy/message_handler.h"

#include "ipc/ipc_message.h"
#include "ppapi/proxy/plugin_dispatcher.h"
#include "ppapi/proxy/ppapi_messages.h"
#include "ppapi/proxy/ppb_message_loop_proxy.h"
#include "ppapi/shared_impl/proxy_lock.h"
#include "ppapi/shared_impl/scoped_pp_var.h"
#include "ppapi/thunk/enter.h"

namespace ppapi {
namespace proxy {
namespace {

typedef void (*HandleMessageFunc)(PP_Instance, void*, const PP_Var*);
typedef void (*HandleBlockingMessageFunc)(
    PP_Instance, void*, const PP_Var*, PP_Var*);
typedef void (*HandleMessageFunc_0_1)(PP_Instance, void*, PP_Var);
typedef PP_Var (*HandleBlockingMessageFunc_0_1)(PP_Instance, void*, PP_Var);

void HandleMessageWrapper(HandleMessageFunc function,
                          PP_Instance instance,
                          void* user_data,
                          ScopedPPVar message_data) {
  CallWhileUnlocked(function, instance, user_data,
                    &message_data.get());
}

void HandleBlockingMessageWrapper(HandleBlockingMessageFunc function,
                                  PP_Instance instance,
                                  void* user_data,
                                  ScopedPPVar message_data,
                                  scoped_ptr<IPC::Message> reply_msg) {
  PluginDispatcher* dispatcher = PluginDispatcher::GetForInstance(instance);
  if (!dispatcher)
    return;
  PP_Var result = PP_MakeUndefined();
  MessageLoopResource::GetCurrent()->
      set_currently_handling_blocking_message(true);
  CallWhileUnlocked(
      function, instance, user_data, &message_data.get(), &result);
  MessageLoopResource::GetCurrent()->
      set_currently_handling_blocking_message(false);
  PpapiMsg_PPPMessageHandler_HandleBlockingMessage::WriteReplyParams(
      reply_msg.get(),
      SerializedVarReturnValue::Convert(dispatcher, result),
      true /* was_handled */);
  dispatcher->Send(reply_msg.release());
}

// TODO(dmichael): Remove the 0_1 verions; crbug.com/414398
void HandleMessageWrapper_0_1(HandleMessageFunc_0_1 function,
                              PP_Instance instance,
                              void* user_data,
                              ScopedPPVar message_data) {
  CallWhileUnlocked(function, instance, user_data, message_data.get());
}

void HandleBlockingMessageWrapper_0_1(HandleBlockingMessageFunc_0_1 function,
                                      PP_Instance instance,
                                      void* user_data,
                                      ScopedPPVar message_data,
                                      scoped_ptr<IPC::Message> reply_msg) {
  PluginDispatcher* dispatcher = PluginDispatcher::GetForInstance(instance);
  if (!dispatcher)
    return;
  MessageLoopResource::GetCurrent()->
      set_currently_handling_blocking_message(true);
  PP_Var return_value = CallWhileUnlocked(function,
                                          instance,
                                          user_data,
                                          message_data.get());
  MessageLoopResource::GetCurrent()->
      set_currently_handling_blocking_message(false);
  PpapiMsg_PPPMessageHandler_HandleBlockingMessage::WriteReplyParams(
      reply_msg.get(),
      SerializedVarReturnValue::Convert(dispatcher, return_value),
      true /* was_handled */);
  dispatcher->Send(reply_msg.release());
}

}  // namespace

// static
scoped_ptr<MessageHandler> MessageHandler::Create(
      PP_Instance instance,
      const PPP_MessageHandler_0_2* handler_if,
      void* user_data,
      PP_Resource message_loop,
      int32_t* error) {
  scoped_ptr<MessageHandler> result;
  // The interface and all function pointers must be valid.
  if (!handler_if ||
      !handler_if->HandleMessage ||
      !handler_if->HandleBlockingMessage ||
      !handler_if->Destroy) {
    *error = PP_ERROR_BADARGUMENT;
    return result.Pass();
  }
  thunk::EnterResourceNoLock<thunk::PPB_MessageLoop_API>
      enter_loop(message_loop, true);
  if (enter_loop.failed()) {
    *error = PP_ERROR_BADRESOURCE;
    return result.Pass();
  }
  scoped_refptr<MessageLoopResource> message_loop_resource(
      static_cast<MessageLoopResource*>(enter_loop.object()));
  if (message_loop_resource->is_main_thread_loop()) {
    *error = PP_ERROR_WRONG_THREAD;
    return result.Pass();
  }

  result.reset(new MessageHandler(
      instance, handler_if, user_data, message_loop_resource));
  *error = PP_OK;
  return result.Pass();
}

// CreateDeprecated is a near-exact copy of Create, but we'll just delete it
// when 0.1 is deprecated, so need to get fancy to avoid code duplication.
// TODO(dmichael) crbug.com/414398
// static
scoped_ptr<MessageHandler> MessageHandler::CreateDeprecated(
      PP_Instance instance,
      const PPP_MessageHandler_0_1_Deprecated* handler_if,
      void* user_data,
      PP_Resource message_loop,
      int32_t* error) {
  scoped_ptr<MessageHandler> result;
  // The interface and all function pointers must be valid.
  if (!handler_if ||
      !handler_if->HandleMessage ||
      !handler_if->HandleBlockingMessage ||
      !handler_if->Destroy) {
    *error = PP_ERROR_BADARGUMENT;
    return result.Pass();
  }
  thunk::EnterResourceNoLock<thunk::PPB_MessageLoop_API>
      enter_loop(message_loop, true);
  if (enter_loop.failed()) {
    *error = PP_ERROR_BADRESOURCE;
    return result.Pass();
  }
  scoped_refptr<MessageLoopResource> message_loop_resource(
      static_cast<MessageLoopResource*>(enter_loop.object()));
  if (message_loop_resource->is_main_thread_loop()) {
    *error = PP_ERROR_WRONG_THREAD;
    return result.Pass();
  }

  result.reset(new MessageHandler(
      instance, handler_if, user_data, message_loop_resource));
  *error = PP_OK;
  return result.Pass();
}

MessageHandler::~MessageHandler() {
  // It's possible the message_loop_proxy is NULL if that loop has been quit.
  // In that case, we unfortunately just can't call Destroy.
  if (message_loop_->message_loop_proxy().get()) {
    // The posted task won't have the proxy lock, but that's OK, it doesn't
    // touch any internal state; it's a direct call on the plugin's function.
    if (handler_if_0_1_) {
      message_loop_->message_loop_proxy()->PostTask(FROM_HERE,
          base::Bind(handler_if_0_1_->Destroy,
                     instance_,
                     user_data_));
      return;
    }
    message_loop_->message_loop_proxy()->PostTask(FROM_HERE,
        base::Bind(handler_if_->Destroy,
                   instance_,
                   user_data_));
  }
}

bool MessageHandler::LoopIsValid() const {
  return !!message_loop_->message_loop_proxy().get();
}

void MessageHandler::HandleMessage(ScopedPPVar var) {
  if (handler_if_0_1_) {
    // TODO(dmichael): Remove this code path. crbug.com/414398
    message_loop_->message_loop_proxy()->PostTask(FROM_HERE,
        RunWhileLocked(base::Bind(&HandleMessageWrapper_0_1,
                                  handler_if_0_1_->HandleMessage,
                                  instance_,
                                  user_data_,
                                  var)));
    return;
  }
  message_loop_->message_loop_proxy()->PostTask(FROM_HERE,
      RunWhileLocked(base::Bind(&HandleMessageWrapper,
                                handler_if_->HandleMessage,
                                instance_,
                                user_data_,
                                var)));
}

void MessageHandler::HandleBlockingMessage(ScopedPPVar var,
                                           scoped_ptr<IPC::Message> reply_msg) {
  if (handler_if_0_1_) {
    // TODO(dmichael): Remove this code path. crbug.com/414398
    message_loop_->message_loop_proxy()->PostTask(FROM_HERE,
        RunWhileLocked(base::Bind(&HandleBlockingMessageWrapper_0_1,
                                  handler_if_0_1_->HandleBlockingMessage,
                                  instance_,
                                  user_data_,
                                  var,
                                  base::Passed(reply_msg.Pass()))));
    return;
  }
  message_loop_->message_loop_proxy()->PostTask(FROM_HERE,
      RunWhileLocked(base::Bind(&HandleBlockingMessageWrapper,
                                handler_if_->HandleBlockingMessage,
                                instance_,
                                user_data_,
                                var,
                                base::Passed(reply_msg.Pass()))));
}

MessageHandler::MessageHandler(
    PP_Instance instance,
    const PPP_MessageHandler_0_2* handler_if,
    void* user_data,
    scoped_refptr<MessageLoopResource> message_loop)
    : instance_(instance),
      handler_if_(handler_if),
      handler_if_0_1_(NULL),
      user_data_(user_data),
      message_loop_(message_loop) {
}

MessageHandler::MessageHandler(
    PP_Instance instance,
    const PPP_MessageHandler_0_1_Deprecated* handler_if,
    void* user_data,
    scoped_refptr<MessageLoopResource> message_loop)
    : instance_(instance),
      handler_if_(NULL),
      handler_if_0_1_(handler_if),
      user_data_(user_data),
      message_loop_(message_loop) {
}

}  // namespace proxy
}  // namespace ppapi