[email protected] | 93df81e | 2012-08-10 22:22:46 | [diff] [blame] | 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. |
| 4 | |
[email protected] | e8bd372 | 2012-10-29 00:02:16 | [diff] [blame] | 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. |
[email protected] | 93df81e | 2012-08-10 22:22:46 | [diff] [blame] | 9 | |
| 10 | #ifndef PPAPI_PROXY_DISPATCH_REPLY_MESSAGE_H_ |
| 11 | #define PPAPI_PROXY_DISPATCH_REPLY_MESSAGE_H_ |
| 12 | |
[email protected] | 1f53216 | 2013-07-15 22:49:56 | [diff] [blame] | 13 | #include "base/callback.h" |
[email protected] | 93df81e | 2012-08-10 22:22:46 | [diff] [blame] | 14 | #include "ipc/ipc_message_macros.h" |
| 15 | #include "ppapi/c/pp_errors.h" |
| 16 | |
| 17 | namespace ppapi { |
| 18 | namespace proxy { |
| 19 | |
[email protected] | 93df81e | 2012-08-10 22:22:46 | [diff] [blame] | 20 | class ResourceMessageReplyParams; |
| 21 | |
| 22 | template <class ObjT, class Method> |
| 23 | inline void DispatchResourceReply(ObjT* obj, Method method, |
| 24 | const ResourceMessageReplyParams& params, |
Avi Drissman | 95c2a1b7 | 2014-12-22 18:01:32 | [diff] [blame] | 25 | const Tuple<>& arg) { |
[email protected] | 93df81e | 2012-08-10 22:22:46 | [diff] [blame] | 26 | (obj->*method)(params); |
| 27 | } |
| 28 | |
| 29 | template <class ObjT, class Method, class A> |
| 30 | inline void DispatchResourceReply(ObjT* obj, Method method, |
| 31 | const ResourceMessageReplyParams& params, |
Avi Drissman | 95c2a1b7 | 2014-12-22 18:01:32 | [diff] [blame] | 32 | const Tuple<A>& arg) { |
| 33 | (obj->*method)(params, get<0>(arg)); |
[email protected] | 93df81e | 2012-08-10 22:22:46 | [diff] [blame] | 34 | } |
| 35 | |
| 36 | template<class ObjT, class Method, class A, class B> |
[email protected] | 7845e288 | 2012-09-18 20:54:07 | [diff] [blame] | 37 | inline void DispatchResourceReply(ObjT* obj, Method method, |
| 38 | const ResourceMessageReplyParams& params, |
Avi Drissman | 95c2a1b7 | 2014-12-22 18:01:32 | [diff] [blame] | 39 | const Tuple<A, B>& arg) { |
| 40 | (obj->*method)(params, get<0>(arg), get<1>(arg)); |
[email protected] | 93df81e | 2012-08-10 22:22:46 | [diff] [blame] | 41 | } |
| 42 | |
| 43 | template<class ObjT, class Method, class A, class B, class C> |
| 44 | inline void DispatchResourceReply(ObjT* obj, Method method, |
| 45 | const ResourceMessageReplyParams& params, |
Avi Drissman | 95c2a1b7 | 2014-12-22 18:01:32 | [diff] [blame] | 46 | const Tuple<A, B, C>& arg) { |
| 47 | (obj->*method)(params, get<0>(arg), get<1>(arg), get<2>(arg)); |
[email protected] | 93df81e | 2012-08-10 22:22:46 | [diff] [blame] | 48 | } |
| 49 | |
| 50 | template<class ObjT, class Method, class A, class B, class C, class D> |
| 51 | inline void DispatchResourceReply(ObjT* obj, Method method, |
| 52 | const ResourceMessageReplyParams& params, |
Avi Drissman | 95c2a1b7 | 2014-12-22 18:01:32 | [diff] [blame] | 53 | 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] | 93df81e | 2012-08-10 22:22:46 | [diff] [blame] | 55 | } |
| 56 | |
| 57 | template<class ObjT, class Method, class A, class B, class C, class D, class E> |
| 58 | inline void DispatchResourceReply(ObjT* obj, Method method, |
| 59 | const ResourceMessageReplyParams& params, |
Avi Drissman | 95c2a1b7 | 2014-12-22 18:01:32 | [diff] [blame] | 60 | 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] | 93df81e | 2012-08-10 22:22:46 | [diff] [blame] | 63 | } |
| 64 | |
[email protected] | e1f5c9b | 2012-10-04 00:07:44 | [diff] [blame] | 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). |
| 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. |
| 88 | template<class MsgClass, class ObjT, class Method> |
| 89 | void 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] | 00c0d04 | 2012-09-10 07:06:39 | [diff] [blame] | 117 | |
[email protected] | e1f5c9b | 2012-10-04 00:07:44 | [diff] [blame] | 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, |
| 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] | e8bd372 | 2012-10-29 00:02:16 | [diff] [blame] | 129 | (obj->*method)(reply_params); |
[email protected] | e1f5c9b | 2012-10-04 00:07:44 | [diff] [blame] | 130 | } |
[email protected] | 93df81e | 2012-08-10 22:22:46 | [diff] [blame] | 131 | |
[email protected] | dade5f8 | 2014-05-13 21:59:21 | [diff] [blame] | 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) \ |
| 137 | { \ |
pkasting | 99867ef | 2014-10-16 23:49:24 | [diff] [blame] | 138 | typedef class_name _IpcMessageHandlerClass ALLOW_UNUSED_TYPE; \ |
[email protected] | dade5f8 | 2014-05-13 21:59:21 | [diff] [blame] | 139 | const IPC::Message& ipc_message__ = msg; \ |
| 140 | switch (ipc_message__.type()) { \ |
| 141 | |
[email protected] | e8bd372 | 2012-10-29 00:02:16 | [diff] [blame] | 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( \ |
| 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] | dade5f8 | 2014-05-13 21:59:21 | [diff] [blame] | 171 | #define PPAPI_END_MESSAGE_MAP() \ |
| 172 | } \ |
| 173 | } |
| 174 | |
[email protected] | 93df81e | 2012-08-10 22:22:46 | [diff] [blame] | 175 | } // namespace proxy |
| 176 | } // namespace ppapi |
| 177 | |
| 178 | #endif // PPAPI_PROXY_DISPATCH_REPLY_MESSAGE_H_ |