Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(271)

Side by Side Diff: ppapi/proxy/dispatch_reply_message.h

Issue 11022005: Converted PluginResource reply message handling to use base::Callback (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: . Created 8 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « no previous file | ppapi/proxy/file_chooser_resource.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 // This file provides infrastructure for dispatching host resource reply 5 // This file provides infrastructure for dispatching host resource reply
6 // messages. Normal IPC Reply handlers can't take extra parameters. 6 // messages. Normal IPC Reply handlers can't take extra parameters.
7 // We want to take a ResourceMessageReplyParams as a parameter. 7 // We want to take a ResourceMessageReplyParams as a parameter.
8 8
9 #ifndef PPAPI_PROXY_DISPATCH_REPLY_MESSAGE_H_ 9 #ifndef PPAPI_PROXY_DISPATCH_REPLY_MESSAGE_H_
10 #define PPAPI_PROXY_DISPATCH_REPLY_MESSAGE_H_ 10 #define PPAPI_PROXY_DISPATCH_REPLY_MESSAGE_H_
11 11
12 #include "base/profiler/scoped_profile.h" // For TRACK_RUN_IN_IPC_HANDLER. 12 #include "base/profiler/scoped_profile.h" // For TRACK_RUN_IN_IPC_HANDLER.
13 #include "ipc/ipc_message_macros.h" 13 #include "ipc/ipc_message_macros.h"
14 #include "ppapi/c/pp_errors.h" 14 #include "ppapi/c/pp_errors.h"
15 15
16 namespace ppapi { 16 namespace ppapi {
17 namespace proxy { 17 namespace proxy {
18 18
19 struct Context;
20 class ResourceMessageReplyParams; 19 class ResourceMessageReplyParams;
21 20
22 template <class ObjT, class Method> 21 template <class ObjT, class Method>
23 inline void DispatchResourceReply(ObjT* obj, Method method, 22 inline void DispatchResourceReply(ObjT* obj, Method method,
24 const ResourceMessageReplyParams& params, 23 const ResourceMessageReplyParams& params,
25 const Tuple0& arg) { 24 const Tuple0& arg) {
26 (obj->*method)(params); 25 (obj->*method)(params);
27 } 26 }
28 27
29 template <class ObjT, class Method, class A> 28 template <class ObjT, class Method, class A>
(...skipping 24 matching lines...) Expand all
54 return (obj->*method)(params, arg.a, arg.b, arg.c, arg.d); 53 return (obj->*method)(params, arg.a, arg.b, arg.c, arg.d);
55 } 54 }
56 55
57 template<class ObjT, class Method, class A, class B, class C, class D, class E> 56 template<class ObjT, class Method, class A, class B, class C, class D, class E>
58 inline void DispatchResourceReply(ObjT* obj, Method method, 57 inline void DispatchResourceReply(ObjT* obj, Method method,
59 const ResourceMessageReplyParams& params, 58 const ResourceMessageReplyParams& params,
60 const Tuple5<A, B, C, D, E>& arg) { 59 const Tuple5<A, B, C, D, E>& arg) {
61 return (obj->*method)(params, arg.a, arg.b, arg.c, arg.d, arg.e); 60 return (obj->*method)(params, arg.a, arg.b, arg.c, arg.d, arg.e);
62 } 61 }
63 62
64 #define PPAPI_DISPATCH_RESOURCE_REPLY(msg_class, member_func) \ 63 // Used to dispatch resource replies. In most cases, you should not call this
65 case msg_class::ID: { \ 64 // function to dispatch a resource reply manually, but instead use
66 TRACK_RUN_IN_IPC_HANDLER(member_func); \ 65 // |PluginResource::CallBrowser|/|PluginResource::CallRenderer| with a
67 msg_class::Schema::Param p; \ 66 // |base::Callback| which will be called when a reply message is received
68 if (msg_class::Read(&ipc_message__, &p)) { \ 67 // (see plugin_resource.h).
69 ppapi::proxy::DispatchResourceReply( \ 68 //
70 this, \ 69 // This function will call your callback with the nested reply message's
71 &_IpcMessageHandlerClass::member_func, \ 70 // parameters on success. On failure, your callback will be called with each
72 params, p); \ 71 // parameter having its default constructed value.
73 } \ 72 //
74 break; \ 73 // Resource replies are a bit weird in that the host will automatically
75 } 74 // generate a reply in error cases (when the call handler returns error rather
75 // than returning "completion pending"). This makes it more convenient to write
76 // the call message handlers. But this also means that the reply handler has to
77 // handle both the success case (when all of the reply message paramaters are
78 // specified) and the error case (when the nested reply message is empty).
79 // In both cases the resource will want to issue completion callbacks to the
80 // plugin.
81 //
82 // This function handles the error case by calling your reply handler with the
83 // default value for each paramater in the error case. In most situations this
84 // will be the right thing. You should always dispatch completion callbacks
85 // using the result code present in the ResourceMessageReplyParams.
86 template<class MsgClass, class ObjT, class Method>
87 void DispatchResourceReplyOrDefaultParams(
88 ObjT* obj,
89 Method method,
90 const ResourceMessageReplyParams& reply_params,
91 const IPC::Message& msg) {
92 typename MsgClass::Schema::Param msg_params;
93 // We either expect the nested message type to match, or that there is no
94 // nested message. No nested message indicates a default reply sent from
95 // the host: when the resource message handler returns an error, a reply
96 // is implicitly sent with no nested message.
97 DCHECK(msg.type() == MsgClass::ID || msg.type() == 0)
98 << "Resource reply message of unexpected type.";
99 if (msg.type() == MsgClass::ID && MsgClass::Read(&msg, &msg_params)) {
100 // Message type matches and the parameters were successfully read.
101 DispatchResourceReply(obj, method, reply_params, msg_params);
102 } else {
103 // The nested message is empty because the host handler didn't explicitly
104 // send a reply (likely), or you screwed up and didn't use the correct
105 // message type when calling this function (you should have hit the
106 // assertion above, Einstein).
107 //
108 // Dispatch the reply function with the default parameters. We explicitly
109 // use a new Params() structure since if the Read failed due to an invalid
110 // message, the params could have been partially filled in.
111 DispatchResourceReply(obj, method, reply_params,
112 typename MsgClass::Schema::Param());
113 }
114 }
76 115
77 #define PPAPI_DISPATCH_RESOURCE_REPLY_0(msg_class, member_func) \ 116 // Template specialization for |Callback|s that only accept a
78 case msg_class::ID: { \ 117 // |ResourceMessageReplyParams|. In this case |msg| shouldn't contain any
79 TRACK_RUN_IN_IPC_HANDLER(member_func); \ 118 // arguments, so just call the |method| with the |reply_params|.
80 member_func(params); \ 119 template<class MsgClass, class Method>
81 break; \ 120 void DispatchResourceReplyOrDefaultParams(
82 } 121 base::Callback<void(const ResourceMessageReplyParams&)>* obj,
122 Method method,
123 const ResourceMessageReplyParams& reply_params,
124 const IPC::Message& msg) {
125 DCHECK(msg.type() == MsgClass::ID || msg.type() == 0)
126 << "Resource reply message of unexpected type.";
127 return (obj->*method)(reply_params);
128 }
83 129
84 } // namespace proxy 130 } // namespace proxy
85 } // namespace ppapi 131 } // namespace ppapi
86 132
87 #endif // PPAPI_PROXY_DISPATCH_REPLY_MESSAGE_H_ 133 #endif // PPAPI_PROXY_DISPATCH_REPLY_MESSAGE_H_
OLDNEW
« no previous file with comments | « no previous file | ppapi/proxy/file_chooser_resource.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698