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 "base/profiler/scoped_profile.h" // For TRACK_RUN_IN_IPC_HANDLER.
15 #include "ipc/ipc_message_macros.h"
16 #include "ppapi/c/pp_errors.h"
21 class ResourceMessageReplyParams
;
23 template <class ObjT
, class Method
>
24 inline void DispatchResourceReply(ObjT
* obj
, Method method
,
25 const ResourceMessageReplyParams
& params
,
27 (obj
->*method
)(params
);
30 template <class ObjT
, class Method
, class A
>
31 inline void DispatchResourceReply(ObjT
* obj
, Method method
,
32 const ResourceMessageReplyParams
& params
,
33 const Tuple1
<A
>& arg
) {
34 (obj
->*method
)(params
, arg
.a
);
37 template<class ObjT
, class Method
, class A
, class B
>
38 inline void DispatchResourceReply(ObjT
* obj
, Method method
,
39 const ResourceMessageReplyParams
& params
,
40 const Tuple2
<A
, B
>& arg
) {
41 (obj
->*method
)(params
, arg
.a
, arg
.b
);
44 template<class ObjT
, class Method
, class A
, class B
, class C
>
45 inline void DispatchResourceReply(ObjT
* obj
, Method method
,
46 const ResourceMessageReplyParams
& params
,
47 const Tuple3
<A
, B
, C
>& arg
) {
48 (obj
->*method
)(params
, arg
.a
, arg
.b
, arg
.c
);
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 Tuple4
<A
, B
, C
, D
>& arg
) {
55 (obj
->*method
)(params
, arg
.a
, arg
.b
, arg
.c
, arg
.d
);
58 template<class ObjT
, class Method
, class A
, class B
, class C
, class D
, class E
>
59 inline void DispatchResourceReply(ObjT
* obj
, Method method
,
60 const ResourceMessageReplyParams
& params
,
61 const Tuple5
<A
, B
, C
, D
, E
>& arg
) {
62 (obj
->*method
)(params
, arg
.a
, arg
.b
, arg
.c
, arg
.d
, arg
.e
);
65 // 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).
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.
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
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.
88 template<class MsgClass
, class ObjT
, class Method
>
89 void DispatchResourceReplyOrDefaultParams(
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
);
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).
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());
118 // 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|.
121 template<class MsgClass
, class Method
>
122 void DispatchResourceReplyOrDefaultParams(
123 base::Callback
<void(const ResourceMessageReplyParams
&)>* obj
,
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.";
129 (obj
->*method
)(reply_params
);
132 // 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) \
138 typedef class_name _IpcMessageHandlerClass ALLOW_UNUSED; \
139 const IPC::Message& ipc_message__ = msg; \
140 switch (ipc_message__.type()) { \
142 // 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( \
151 &_IpcMessageHandlerClass::member_func, \
159 #define PPAPI_DISPATCH_PLUGIN_RESOURCE_CALL_0(msg_class, member_func) \
160 case msg_class::ID: { \
161 member_func(params); \
165 #define PPAPI_DISPATCH_PLUGIN_RESOURCE_CALL_UNHANDLED(code) \
171 #define PPAPI_END_MESSAGE_MAP() \
178 #endif // PPAPI_PROXY_DISPATCH_REPLY_MESSAGE_H_