1 // 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.
5 // 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.
10 #ifndef PPAPI_PROXY_DISPATCH_REPLY_MESSAGE_H_
11 #define PPAPI_PROXY_DISPATCH_REPLY_MESSAGE_H_
13 #include "base/callback.h"
14 #include "ipc/ipc_message_macros.h"
15 #include "ppapi/c/pp_errors.h"
20 class ResourceMessageReplyParams
;
22 template <class ObjT
, class Method
>
23 inline void DispatchResourceReply(ObjT
* obj
, Method method
,
24 const ResourceMessageReplyParams
& params
,
25 const base::Tuple
<>& arg
) {
26 (obj
->*method
)(params
);
29 template <class ObjT
, class Method
, class A
>
30 inline void DispatchResourceReply(ObjT
* obj
, Method method
,
31 const ResourceMessageReplyParams
& params
,
32 const base::Tuple
<A
>& arg
) {
33 (obj
->*method
)(params
, base::get
<0>(arg
));
36 template<class ObjT
, class Method
, class A
, class B
>
37 inline void DispatchResourceReply(ObjT
* obj
, Method method
,
38 const ResourceMessageReplyParams
& params
,
39 const base::Tuple
<A
, B
>& arg
) {
40 (obj
->*method
)(params
, base::get
<0>(arg
), base::get
<1>(arg
));
43 template<class ObjT
, class Method
, class A
, class B
, class C
>
44 inline void DispatchResourceReply(ObjT
* obj
, Method method
,
45 const ResourceMessageReplyParams
& params
,
46 const base::Tuple
<A
, B
, C
>& arg
) {
47 (obj
->*method
)(params
, base::get
<0>(arg
), base::get
<1>(arg
),
51 template<class ObjT
, class Method
, class A
, class B
, class C
, class D
>
52 inline void DispatchResourceReply(ObjT
* obj
, Method method
,
53 const ResourceMessageReplyParams
& params
,
54 const base::Tuple
<A
, B
, C
, D
>& arg
) {
55 (obj
->*method
)(params
, base::get
<0>(arg
), base::get
<1>(arg
),
56 base::get
<2>(arg
), base::get
<3>(arg
));
59 template<class ObjT
, class Method
, class A
, class B
, class C
, class D
, class E
>
60 inline void DispatchResourceReply(ObjT
* obj
, Method method
,
61 const ResourceMessageReplyParams
& params
,
62 const base::Tuple
<A
, B
, C
, D
, E
>& arg
) {
63 (obj
->*method
)(params
, base::get
<0>(arg
), base::get
<1>(arg
),
64 base::get
<2>(arg
), base::get
<3>(arg
), base::get
<4>(arg
));
67 // Used to dispatch resource replies. In most cases, you should not call this
68 // function to dispatch a resource reply manually, but instead use
69 // |PluginResource::CallBrowser|/|PluginResource::CallRenderer| with a
70 // |base::Callback| which will be called when a reply message is received
71 // (see plugin_resource.h).
73 // This function will call your callback with the nested reply message's
74 // parameters on success. On failure, your callback will be called with each
75 // parameter having its default constructed value.
77 // Resource replies are a bit weird in that the host will automatically
78 // generate a reply in error cases (when the call handler returns error rather
79 // than returning "completion pending"). This makes it more convenient to write
80 // the call message handlers. But this also means that the reply handler has to
81 // handle both the success case (when all of the reply message paramaters are
82 // specified) and the error case (when the nested reply message is empty).
83 // In both cases the resource will want to issue completion callbacks to the
86 // This function handles the error case by calling your reply handler with the
87 // default value for each paramater in the error case. In most situations this
88 // will be the right thing. You should always dispatch completion callbacks
89 // using the result code present in the ResourceMessageReplyParams.
90 template<class MsgClass
, class ObjT
, class Method
>
91 void DispatchResourceReplyOrDefaultParams(
94 const ResourceMessageReplyParams
& reply_params
,
95 const IPC::Message
& msg
) {
96 typename
MsgClass::Schema::Param msg_params
;
97 // We either expect the nested message type to match, or that there is no
98 // nested message. No nested message indicates a default reply sent from
99 // the host: when the resource message handler returns an error, a reply
100 // is implicitly sent with no nested message.
101 DCHECK(msg
.type() == MsgClass::ID
|| msg
.type() == 0)
102 << "Resource reply message of unexpected type.";
103 if (msg
.type() == MsgClass::ID
&& MsgClass::Read(&msg
, &msg_params
)) {
104 // Message type matches and the parameters were successfully read.
105 DispatchResourceReply(obj
, method
, reply_params
, msg_params
);
107 // The nested message is empty because the host handler didn't explicitly
108 // send a reply (likely), or you screwed up and didn't use the correct
109 // message type when calling this function (you should have hit the
110 // assertion above, Einstein).
112 // Dispatch the reply function with the default parameters. We explicitly
113 // use a new Params() structure since if the Read failed due to an invalid
114 // message, the params could have been partially filled in.
115 DispatchResourceReply(obj
, method
, reply_params
,
116 typename
MsgClass::Schema::Param());
120 // Template specialization for |Callback|s that only accept a
121 // |ResourceMessageReplyParams|. In this case |msg| shouldn't contain any
122 // arguments, so just call the |method| with the |reply_params|.
123 template<class MsgClass
, class Method
>
124 void DispatchResourceReplyOrDefaultParams(
125 base::Callback
<void(const ResourceMessageReplyParams
&)>* obj
,
127 const ResourceMessageReplyParams
& reply_params
,
128 const IPC::Message
& msg
) {
129 DCHECK(msg
.type() == MsgClass::ID
|| msg
.type() == 0)
130 << "Resource reply message of unexpected type.";
131 (obj
->*method
)(reply_params
);
134 // When using PPAPI_DISPATCH_PLUGIN_RESOURCE_CALL* below, use this macro to
135 // begin the map instead of IPC_BEGIN_MESSAGE_MAP. The reason is that the macros
136 // in src/ipc are all closely tied together, and there might be errors for
137 // unused variables or other errors if they're used with these macros.
138 #define PPAPI_BEGIN_MESSAGE_MAP(class_name, msg) \
140 typedef class_name _IpcMessageHandlerClass ALLOW_UNUSED_TYPE; \
141 const IPC::Message& ipc_message__ = msg; \
142 switch (ipc_message__.type()) { \
144 // Note that this only works for message with 1 or more parameters. For
145 // 0-parameter messages you need to use the _0 version below (since there are
146 // no params in the message).
147 #define PPAPI_DISPATCH_PLUGIN_RESOURCE_CALL(msg_class, member_func) \
148 case msg_class::ID: { \
149 msg_class::Schema::Param p; \
150 if (msg_class::Read(&ipc_message__, &p)) { \
151 ppapi::proxy::DispatchResourceReply( \
153 &_IpcMessageHandlerClass::member_func, \
161 #define PPAPI_DISPATCH_PLUGIN_RESOURCE_CALL_0(msg_class, member_func) \
162 case msg_class::ID: { \
163 member_func(params); \
167 #define PPAPI_DISPATCH_PLUGIN_RESOURCE_CALL_UNHANDLED(code) \
173 #define PPAPI_END_MESSAGE_MAP() \
180 #endif // PPAPI_PROXY_DISPATCH_REPLY_MESSAGE_H_