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 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294
|
// Copyright 2012 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef PPAPI_PROXY_PLUGIN_RESOURCE_H_
#define PPAPI_PROXY_PLUGIN_RESOURCE_H_
#include <stdint.h>
#include <map>
#include "base/compiler_specific.h"
#include "base/memory/scoped_refptr.h"
#include "ipc/ipc_message.h"
#include "ipc/ipc_sender.h"
#include "ppapi/c/pp_errors.h"
#include "ppapi/proxy/connection.h"
#include "ppapi/proxy/plugin_resource_callback.h"
#include "ppapi/proxy/ppapi_message_utils.h"
#include "ppapi/proxy/ppapi_proxy_export.h"
#include "ppapi/proxy/resource_message_params.h"
#include "ppapi/proxy/resource_reply_thread_registrar.h"
#include "ppapi/shared_impl/resource.h"
#include "ppapi/shared_impl/tracked_callback.h"
namespace ppapi {
namespace proxy {
// A "safe" way to run callbacks, doing nothing if they are not
// pending (active).
void SafeRunCallback(scoped_refptr<TrackedCallback>* callback, int32_t error);
class PPAPI_PROXY_EXPORT PluginResource : public Resource {
public:
enum Destination {
RENDERER = 0,
BROWSER = 1
};
PluginResource(Connection connection, PP_Instance instance);
PluginResource(const PluginResource&) = delete;
PluginResource& operator=(const PluginResource&) = delete;
~PluginResource() override;
// Returns true if we've previously sent a create message to the browser
// or renderer. Generally resources will use these to tell if they should
// lazily send create messages.
bool sent_create_to_browser() const { return sent_create_to_browser_; }
bool sent_create_to_renderer() const { return sent_create_to_renderer_; }
// This handles a reply to a resource call. It works by looking up the
// callback that was registered when CallBrowser/CallRenderer was called
// and calling it with |params| and |msg|.
void OnReplyReceived(const proxy::ResourceMessageReplyParams& params,
const IPC::Message& msg) override;
// Resource overrides.
// Note: Subclasses shouldn't override these methods directly. Instead, they
// should implement LastPluginRefWasDeleted() or InstanceWasDeleted() to get
// notified.
void NotifyLastPluginRefWasDeleted() override;
void NotifyInstanceWasDeleted() override;
// Sends a create message to the browser or renderer for the current resource.
void SendCreate(Destination dest, const IPC::Message& msg);
// When the host returnes a resource to the plugin, it will create a pending
// ResourceHost and send an ID back to the plugin that identifies the pending
// object. The plugin uses this function to connect the plugin resource with
// the pending host resource. See also PpapiHostMsg_AttachToPendingHost. This
// is in lieu of sending a create message.
void AttachToPendingHost(Destination dest, int pending_host_id);
// Sends the given IPC message as a resource request to the host
// corresponding to this resource object and does not expect a reply.
void Post(Destination dest, const IPC::Message& msg);
// Like Post() but expects a response. |callback| is a |base::OnceCallback|
// that will be run when a reply message with a sequence number matching that
// of the call is received. |ReplyMsgClass| is the type of the reply message
// that is expected. An example of usage:
//
// Call<PpapiPluginMsg_MyResourceType_MyReplyMessage>(
// BROWSER,
// PpapiHostMsg_MyResourceType_MyRequestMessage(),
// base::BindOnce(&MyPluginResource::ReplyHandler,
// base::Unretained(this)));
//
// If a reply message to this call is received whose type does not match
// |ReplyMsgClass| (for example, in the case of an error), the callback will
// still be invoked but with the default values of the message parameters.
//
// Returns the new request's sequence number which can be used to identify
// the callback. This value will never be 0, which you can use to identify
// an invalid callback.
//
// Note: 1) When all plugin references to this resource are gone or the
// corresponding plugin instance is deleted, all pending callbacks
// are abandoned.
// 2) It is *not* recommended to let |callback| hold any reference to
// |this|, in which it will be stored. Otherwise, this object will
// live forever if we fail to clean up the callback. It is safe to
// use base::Unretained(this) or a weak pointer, because this object
// will outlive the callback.
template <typename ReplyMsgClass, typename CallbackType>
int32_t Call(Destination dest,
const IPC::Message& msg,
CallbackType callback);
// Comparing with the previous Call() method, this method takes
// |reply_thread_hint| as a hint to determine which thread to handle the reply
// message.
//
// If |reply_thread_hint| is non-blocking, the reply message will be handled
// on the target thread of the callback; otherwise, it will be handled on the
// main thread.
//
// If handling a reply message will cause a TrackedCallback to be run, it is
// recommended to use this version of Call(). It eliminates unnecessary
// thread switching and therefore has better performance.
template <typename ReplyMsgClass, typename CallbackType>
int32_t Call(Destination dest,
const IPC::Message& msg,
CallbackType callback,
scoped_refptr<TrackedCallback> reply_thread_hint);
// Calls the browser/renderer with sync messages. Returns the pepper error
// code from the call.
// |ReplyMsgClass| is the type of the reply message that is expected. If it
// carries x parameters, then the method with x out parameters should be used.
// An example of usage:
//
// // Assuming the reply message carries a string and an integer.
// std::string param_1;
// int param_2 = 0;
// int32_t result = SyncCall<PpapiPluginMsg_MyResourceType_MyReplyMessage>(
// RENDERER, PpapiHostMsg_MyResourceType_MyRequestMessage(),
// ¶m_1, ¶m_2);
template <class ReplyMsgClass>
int32_t SyncCall(Destination dest, const IPC::Message& msg);
template <class ReplyMsgClass, class A>
int32_t SyncCall(Destination dest, const IPC::Message& msg, A* a);
template <class ReplyMsgClass, class A, class B>
int32_t SyncCall(Destination dest, const IPC::Message& msg, A* a, B* b);
template <class ReplyMsgClass, class A, class B, class C>
int32_t SyncCall(Destination dest, const IPC::Message& msg, A* a, B* b, C* c);
template <class ReplyMsgClass, class A, class B, class C, class D>
int32_t SyncCall(
Destination dest, const IPC::Message& msg, A* a, B* b, C* c, D* d);
template <class ReplyMsgClass, class A, class B, class C, class D, class E>
int32_t SyncCall(
Destination dest, const IPC::Message& msg, A* a, B* b, C* c, D* d, E* e);
int32_t GenericSyncCall(Destination dest,
const IPC::Message& msg,
IPC::Message* reply_msg,
ResourceMessageReplyParams* reply_params);
const Connection& connection() { return connection_; }
private:
IPC::Sender* GetSender(Destination dest) {
return dest == RENDERER ? connection_.GetRendererSender()
: connection_.browser_sender();
}
// Helper function to send a |PpapiHostMsg_ResourceCall| to the given
// destination with |nested_msg| and |call_params|.
bool SendResourceCall(Destination dest,
const ResourceMessageCallParams& call_params,
const IPC::Message& nested_msg);
int32_t GetNextSequence();
Connection connection_;
// Use GetNextSequence to retrieve the next value.
int32_t next_sequence_number_;
bool sent_create_to_browser_;
bool sent_create_to_renderer_;
typedef std::map<int32_t, std::unique_ptr<PluginResourceCallbackBase>>
CallbackMap;
CallbackMap callbacks_;
scoped_refptr<ResourceReplyThreadRegistrar> resource_reply_thread_registrar_;
};
template <typename ReplyMsgClass, typename CallbackType>
int32_t PluginResource::Call(Destination dest,
const IPC::Message& msg,
CallbackType callback) {
return Call<ReplyMsgClass>(dest, msg, std::move(callback), nullptr);
}
template <typename ReplyMsgClass, typename CallbackType>
int32_t PluginResource::Call(Destination dest,
const IPC::Message& msg,
CallbackType callback,
scoped_refptr<TrackedCallback> reply_thread_hint) {
TRACE_EVENT2("ppapi_proxy", "PluginResource::Call", "Class",
IPC_MESSAGE_ID_CLASS(msg.type()), "Line",
IPC_MESSAGE_ID_LINE(msg.type()));
ResourceMessageCallParams params(pp_resource(), next_sequence_number_++);
// Stash the |callback| in |callbacks_| identified by the sequence number of
// the call.
std::unique_ptr<PluginResourceCallbackBase> plugin_callback(
new PluginResourceCallback<ReplyMsgClass, CallbackType>(
std::move(callback)));
callbacks_.insert(
std::make_pair(params.sequence(), std::move(plugin_callback)));
params.set_has_callback();
if (resource_reply_thread_registrar_.get()) {
resource_reply_thread_registrar_->Register(
pp_resource(), params.sequence(), reply_thread_hint);
}
SendResourceCall(dest, params, msg);
return params.sequence();
}
template <class ReplyMsgClass>
int32_t PluginResource::SyncCall(Destination dest, const IPC::Message& msg) {
IPC::Message reply;
ResourceMessageReplyParams reply_params;
return GenericSyncCall(dest, msg, &reply, &reply_params);
}
template <class ReplyMsgClass, class A>
int32_t PluginResource::SyncCall(
Destination dest, const IPC::Message& msg, A* a) {
IPC::Message reply;
ResourceMessageReplyParams reply_params;
int32_t result = GenericSyncCall(dest, msg, &reply, &reply_params);
if (UnpackMessage<ReplyMsgClass>(reply, a))
return result;
return PP_ERROR_FAILED;
}
template <class ReplyMsgClass, class A, class B>
int32_t PluginResource::SyncCall(
Destination dest, const IPC::Message& msg, A* a, B* b) {
IPC::Message reply;
ResourceMessageReplyParams reply_params;
int32_t result = GenericSyncCall(dest, msg, &reply, &reply_params);
if (UnpackMessage<ReplyMsgClass>(reply, a, b))
return result;
return PP_ERROR_FAILED;
}
template <class ReplyMsgClass, class A, class B, class C>
int32_t PluginResource::SyncCall(
Destination dest, const IPC::Message& msg, A* a, B* b, C* c) {
IPC::Message reply;
ResourceMessageReplyParams reply_params;
int32_t result = GenericSyncCall(dest, msg, &reply, &reply_params);
if (UnpackMessage<ReplyMsgClass>(reply, a, b, c))
return result;
return PP_ERROR_FAILED;
}
template <class ReplyMsgClass, class A, class B, class C, class D>
int32_t PluginResource::SyncCall(
Destination dest, const IPC::Message& msg, A* a, B* b, C* c, D* d) {
IPC::Message reply;
ResourceMessageReplyParams reply_params;
int32_t result = GenericSyncCall(dest, msg, &reply, &reply_params);
if (UnpackMessage<ReplyMsgClass>(reply, a, b, c, d))
return result;
return PP_ERROR_FAILED;
}
template <class ReplyMsgClass, class A, class B, class C, class D, class E>
int32_t PluginResource::SyncCall(
Destination dest, const IPC::Message& msg, A* a, B* b, C* c, D* d, E* e) {
IPC::Message reply;
ResourceMessageReplyParams reply_params;
int32_t result = GenericSyncCall(dest, msg, &reply, &reply_params);
if (UnpackMessage<ReplyMsgClass>(reply, a, b, c, d, e))
return result;
return PP_ERROR_FAILED;
}
} // namespace proxy
} // namespace ppapi
#endif // PPAPI_PROXY_PLUGIN_RESOURCE_H_
|