blob: 3e8b74f4db89c6acf413aad5b5e0e6bb0c33ce34 [file] [log] [blame]
[email protected]93df81e2012-08-10 22:22:461// Copyright (c) 2012 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.
4
[email protected]e8bd3722012-10-29 00:02:165// This file provides infrastructure for dispatching messasges from host
6// resource, inlcuding reply messages or unsolicited replies. Normal IPC Reply
7// handlers can't take extra parameters. We want to take a
8// ResourceMessageReplyParams as a parameter.
[email protected]93df81e2012-08-10 22:22:469
10#ifndef PPAPI_PROXY_DISPATCH_REPLY_MESSAGE_H_
11#define PPAPI_PROXY_DISPATCH_REPLY_MESSAGE_H_
12
[email protected]1f532162013-07-15 22:49:5613#include "base/callback.h"
[email protected]93df81e2012-08-10 22:22:4614#include "ipc/ipc_message_macros.h"
15#include "ppapi/c/pp_errors.h"
16
17namespace ppapi {
18namespace proxy {
19
[email protected]93df81e2012-08-10 22:22:4620class ResourceMessageReplyParams;
21
22template <class ObjT, class Method>
23inline void DispatchResourceReply(ObjT* obj, Method method,
24 const ResourceMessageReplyParams& params,
Avi Drissman95c2a1b72014-12-22 18:01:3225 const Tuple<>& arg) {
[email protected]93df81e2012-08-10 22:22:4626 (obj->*method)(params);
27}
28
29template <class ObjT, class Method, class A>
30inline void DispatchResourceReply(ObjT* obj, Method method,
31 const ResourceMessageReplyParams& params,
Avi Drissman95c2a1b72014-12-22 18:01:3232 const Tuple<A>& arg) {
33 (obj->*method)(params, get<0>(arg));
[email protected]93df81e2012-08-10 22:22:4634}
35
36template<class ObjT, class Method, class A, class B>
[email protected]7845e2882012-09-18 20:54:0737inline void DispatchResourceReply(ObjT* obj, Method method,
38 const ResourceMessageReplyParams& params,
Avi Drissman95c2a1b72014-12-22 18:01:3239 const Tuple<A, B>& arg) {
40 (obj->*method)(params, get<0>(arg), get<1>(arg));
[email protected]93df81e2012-08-10 22:22:4641}
42
43template<class ObjT, class Method, class A, class B, class C>
44inline void DispatchResourceReply(ObjT* obj, Method method,
45 const ResourceMessageReplyParams& params,
Avi Drissman95c2a1b72014-12-22 18:01:3246 const Tuple<A, B, C>& arg) {
47 (obj->*method)(params, get<0>(arg), get<1>(arg), get<2>(arg));
[email protected]93df81e2012-08-10 22:22:4648}
49
50template<class ObjT, class Method, class A, class B, class C, class D>
51inline void DispatchResourceReply(ObjT* obj, Method method,
52 const ResourceMessageReplyParams& params,
Avi Drissman95c2a1b72014-12-22 18:01:3253 const Tuple<A, B, C, D>& arg) {
54 (obj->*method)(params, get<0>(arg), get<1>(arg), get<2>(arg), get<3>(arg));
[email protected]93df81e2012-08-10 22:22:4655}
56
57template<class ObjT, class Method, class A, class B, class C, class D, class E>
58inline void DispatchResourceReply(ObjT* obj, Method method,
59 const ResourceMessageReplyParams& params,
Avi Drissman95c2a1b72014-12-22 18:01:3260 const Tuple<A, B, C, D, E>& arg) {
61 (obj->*method)(params, get<0>(arg), get<1>(arg), get<2>(arg), get<3>(arg),
62 get<4>(arg));
[email protected]93df81e2012-08-10 22:22:4663}
64
[email protected]e1f5c9b2012-10-04 00:07:4465// Used to dispatch resource replies. In most cases, you should not call this
66// function to dispatch a resource reply manually, but instead use
67// |PluginResource::CallBrowser|/|PluginResource::CallRenderer| with a
68// |base::Callback| which will be called when a reply message is received
69// (see plugin_resource.h).
70//
71// This function will call your callback with the nested reply message's
72// parameters on success. On failure, your callback will be called with each
73// parameter having its default constructed value.
74//
75// Resource replies are a bit weird in that the host will automatically
76// generate a reply in error cases (when the call handler returns error rather
77// than returning "completion pending"). This makes it more convenient to write
78// the call message handlers. But this also means that the reply handler has to
79// handle both the success case (when all of the reply message paramaters are
80// specified) and the error case (when the nested reply message is empty).
81// In both cases the resource will want to issue completion callbacks to the
82// plugin.
83//
84// This function handles the error case by calling your reply handler with the
85// default value for each paramater in the error case. In most situations this
86// will be the right thing. You should always dispatch completion callbacks
87// using the result code present in the ResourceMessageReplyParams.
88template<class MsgClass, class ObjT, class Method>
89void DispatchResourceReplyOrDefaultParams(
90 ObjT* obj,
91 Method method,
92 const ResourceMessageReplyParams& reply_params,
93 const IPC::Message& msg) {
94 typename MsgClass::Schema::Param msg_params;
95 // We either expect the nested message type to match, or that there is no
96 // nested message. No nested message indicates a default reply sent from
97 // the host: when the resource message handler returns an error, a reply
98 // is implicitly sent with no nested message.
99 DCHECK(msg.type() == MsgClass::ID || msg.type() == 0)
100 << "Resource reply message of unexpected type.";
101 if (msg.type() == MsgClass::ID && MsgClass::Read(&msg, &msg_params)) {
102 // Message type matches and the parameters were successfully read.
103 DispatchResourceReply(obj, method, reply_params, msg_params);
104 } else {
105 // The nested message is empty because the host handler didn't explicitly
106 // send a reply (likely), or you screwed up and didn't use the correct
107 // message type when calling this function (you should have hit the
108 // assertion above, Einstein).
109 //
110 // Dispatch the reply function with the default parameters. We explicitly
111 // use a new Params() structure since if the Read failed due to an invalid
112 // message, the params could have been partially filled in.
113 DispatchResourceReply(obj, method, reply_params,
114 typename MsgClass::Schema::Param());
115 }
116}
[email protected]00c0d042012-09-10 07:06:39117
[email protected]e1f5c9b2012-10-04 00:07:44118// Template specialization for |Callback|s that only accept a
119// |ResourceMessageReplyParams|. In this case |msg| shouldn't contain any
120// arguments, so just call the |method| with the |reply_params|.
121template<class MsgClass, class Method>
122void DispatchResourceReplyOrDefaultParams(
123 base::Callback<void(const ResourceMessageReplyParams&)>* obj,
124 Method method,
125 const ResourceMessageReplyParams& reply_params,
126 const IPC::Message& msg) {
127 DCHECK(msg.type() == MsgClass::ID || msg.type() == 0)
128 << "Resource reply message of unexpected type.";
[email protected]e8bd3722012-10-29 00:02:16129 (obj->*method)(reply_params);
[email protected]e1f5c9b2012-10-04 00:07:44130}
[email protected]93df81e2012-08-10 22:22:46131
[email protected]dade5f82014-05-13 21:59:21132// When using PPAPI_DISPATCH_PLUGIN_RESOURCE_CALL* below, use this macro to
133// begin the map instead of IPC_BEGIN_MESSAGE_MAP. The reason is that the macros
134// in src/ipc are all closely tied together, and there might be errors for
135// unused variables or other errors if they're used with these macros.
136#define PPAPI_BEGIN_MESSAGE_MAP(class_name, msg) \
137 { \
pkasting99867ef2014-10-16 23:49:24138 typedef class_name _IpcMessageHandlerClass ALLOW_UNUSED_TYPE; \
[email protected]dade5f82014-05-13 21:59:21139 const IPC::Message& ipc_message__ = msg; \
140 switch (ipc_message__.type()) { \
141
[email protected]e8bd3722012-10-29 00:02:16142// Note that this only works for message with 1 or more parameters. For
143// 0-parameter messages you need to use the _0 version below (since there are
144// no params in the message).
145#define PPAPI_DISPATCH_PLUGIN_RESOURCE_CALL(msg_class, member_func) \
146 case msg_class::ID: { \
147 msg_class::Schema::Param p; \
148 if (msg_class::Read(&ipc_message__, &p)) { \
149 ppapi::proxy::DispatchResourceReply( \
150 this, \
151 &_IpcMessageHandlerClass::member_func, \
152 params, p); \
153 } else { \
154 NOTREACHED(); \
155 } \
156 break; \
157 }
158
159#define PPAPI_DISPATCH_PLUGIN_RESOURCE_CALL_0(msg_class, member_func) \
160 case msg_class::ID: { \
161 member_func(params); \
162 break; \
163 }
164
165#define PPAPI_DISPATCH_PLUGIN_RESOURCE_CALL_UNHANDLED(code) \
166 default: { \
167 code; \
168 } \
169 break;
170
[email protected]dade5f82014-05-13 21:59:21171#define PPAPI_END_MESSAGE_MAP() \
172 } \
173}
174
[email protected]93df81e2012-08-10 22:22:46175} // namespace proxy
176} // namespace ppapi
177
178#endif // PPAPI_PROXY_DISPATCH_REPLY_MESSAGE_H_