blob: b763d1a548bcc8a71a2edc9374d8f08761434816 [file] [log] [blame]
[email protected]eccf80312012-07-14 15:43:421// 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
5#ifndef PPAPI_PROXY_PLUGIN_RESOURCE_H_
6#define PPAPI_PROXY_PLUGIN_RESOURCE_H_
7
[email protected]e1f5c9b2012-10-04 00:07:448#include <map>
9
[email protected]58786932012-10-13 10:16:0810#include "base/basictypes.h"
[email protected]eccf80312012-07-14 15:43:4211#include "base/compiler_specific.h"
[email protected]58786932012-10-13 10:16:0812#include "ipc/ipc_message.h"
[email protected]eccf80312012-07-14 15:43:4213#include "ipc/ipc_sender.h"
[email protected]58786932012-10-13 10:16:0814#include "ppapi/c/pp_errors.h"
[email protected]93df81e2012-08-10 22:22:4615#include "ppapi/proxy/connection.h"
[email protected]e1f5c9b2012-10-04 00:07:4416#include "ppapi/proxy/plugin_resource_callback.h"
[email protected]58786932012-10-13 10:16:0817#include "ppapi/proxy/ppapi_message_utils.h"
[email protected]eccf80312012-07-14 15:43:4218#include "ppapi/proxy/ppapi_proxy_export.h"
[email protected]9164da32012-10-16 03:40:5719#include "ppapi/proxy/resource_message_params.h"
[email protected]eccf80312012-07-14 15:43:4220#include "ppapi/shared_impl/resource.h"
21
[email protected]eccf80312012-07-14 15:43:4222namespace ppapi {
23namespace proxy {
24
25class PluginDispatcher;
26
[email protected]93df81e2012-08-10 22:22:4627class PPAPI_PROXY_EXPORT PluginResource : public Resource {
[email protected]eccf80312012-07-14 15:43:4228 public:
[email protected]93df81e2012-08-10 22:22:4629 PluginResource(Connection connection, PP_Instance instance);
[email protected]eccf80312012-07-14 15:43:4230 virtual ~PluginResource();
31
[email protected]93df81e2012-08-10 22:22:4632 // Returns true if we've previously sent a create message to the browser
33 // or renderer. Generally resources will use these to tell if they should
34 // lazily send create messages.
35 bool sent_create_to_browser() const { return sent_create_to_browser_; }
[email protected]eccf80312012-07-14 15:43:4236 bool sent_create_to_renderer() const { return sent_create_to_renderer_; }
37
[email protected]e1f5c9b2012-10-04 00:07:4438 // This handles a reply to a resource call. It works by looking up the
39 // callback that was registered when CallBrowser/CallRenderer was called
40 // and calling it with |params| and |msg|.
41 virtual void OnReplyReceived(const proxy::ResourceMessageReplyParams& params,
42 const IPC::Message& msg) OVERRIDE;
[email protected]eccf80312012-07-14 15:43:4243 protected:
[email protected]58786932012-10-13 10:16:0844 enum Destination {
45 RENDERER = 0,
46 BROWSER = 1
47 };
48
49 IPC::Sender* GetSender(Destination dest) {
50 return dest == RENDERER ? connection_.renderer_sender :
51 connection_.browser_sender;
52 }
53
[email protected]93df81e2012-08-10 22:22:4654 // Sends a create message to the browser or renderer for the current resource.
[email protected]9164da32012-10-16 03:40:5755 void SendCreate(Destination dest, const IPC::Message& msg);
[email protected]eccf80312012-07-14 15:43:4256
57 // Sends the given IPC message as a resource request to the host
58 // corresponding to this resource object and does not expect a reply.
[email protected]9164da32012-10-16 03:40:5759 void Post(Destination dest, const IPC::Message& msg);
[email protected]eccf80312012-07-14 15:43:4260
[email protected]9164da32012-10-16 03:40:5761 // Like Post() but expects a response. |callback| is a |base::Callback| that
62 // will be run when a reply message with a sequence number matching that of
63 // the call is received. |ReplyMsgClass| is the type of the reply message that
64 // is expected. An example of usage:
[email protected]e1f5c9b2012-10-04 00:07:4465 //
[email protected]9164da32012-10-16 03:40:5766 // Call<PpapiPluginMsg_MyResourceType_MyReplyMessage>(
67 // BROWSER,
[email protected]e1f5c9b2012-10-04 00:07:4468 // PpapiHostMsg_MyResourceType_MyRequestMessage(),
69 // base::Bind(&MyPluginResource::ReplyHandler, this));
70 //
71 // If a reply message to this call is received whose type does not match
72 // |ReplyMsgClass| (for example, in the case of an error), the callback will
73 // still be invoked but with the default values of the message parameters.
[email protected]eccf80312012-07-14 15:43:4274 //
75 // Returns the new request's sequence number which can be used to identify
[email protected]e1f5c9b2012-10-04 00:07:4476 // the callback.
[email protected]eccf80312012-07-14 15:43:4277 //
78 // Note that all integers (including 0 and -1) are valid request IDs.
[email protected]e1f5c9b2012-10-04 00:07:4479 template<typename ReplyMsgClass, typename CallbackType>
[email protected]9164da32012-10-16 03:40:5780 int32_t Call(Destination dest,
81 const IPC::Message& msg,
82 const CallbackType& callback);
[email protected]eccf80312012-07-14 15:43:4283
[email protected]58786932012-10-13 10:16:0884 // Calls the browser/renderer with sync messages. Returns the pepper error
85 // code from the call.
86 // |ReplyMsgClass| is the type of the reply message that is expected. If it
87 // carries x parameters, then the method with x out parameters should be used.
88 // An example of usage:
89 //
90 // // Assuming the reply message carries a string and an integer.
91 // std::string param_1;
92 // int param_2 = 0;
93 // int32_t result = SyncCall<PpapiPluginMsg_MyResourceType_MyReplyMessage>(
94 // RENDERER, PpapiHostMsg_MyResourceType_MyRequestMessage(),
95 // &param_1, &param_2);
96 template <class ReplyMsgClass>
97 int32_t SyncCall(Destination dest, const IPC::Message& msg);
98 template <class ReplyMsgClass, class A>
99 int32_t SyncCall(Destination dest, const IPC::Message& msg, A* a);
100 template <class ReplyMsgClass, class A, class B>
101 int32_t SyncCall(Destination dest, const IPC::Message& msg, A* a, B* b);
102 template <class ReplyMsgClass, class A, class B, class C>
103 int32_t SyncCall(Destination dest, const IPC::Message& msg, A* a, B* b, C* c);
104 template <class ReplyMsgClass, class A, class B, class C, class D>
105 int32_t SyncCall(
106 Destination dest, const IPC::Message& msg, A* a, B* b, C* c, D* d);
107 template <class ReplyMsgClass, class A, class B, class C, class D, class E>
108 int32_t SyncCall(
109 Destination dest, const IPC::Message& msg, A* a, B* b, C* c, D* d, E* e);
[email protected]ff44fc12012-10-03 00:52:16110
[email protected]eccf80312012-07-14 15:43:42111 private:
[email protected]9164da32012-10-16 03:40:57112 // Helper function to send a |PpapiHostMsg_ResourceCall| to the given
113 // destination with |nested_msg| and |call_params|.
114 bool SendResourceCall(Destination dest,
[email protected]e1f5c9b2012-10-04 00:07:44115 const ResourceMessageCallParams& call_params,
116 const IPC::Message& nested_msg);
117
[email protected]58786932012-10-13 10:16:08118 int32_t GenericSyncCall(Destination dest,
119 const IPC::Message& msg,
120 IPC::Message* reply_msg);
121
[email protected]93df81e2012-08-10 22:22:46122 Connection connection_;
[email protected]eccf80312012-07-14 15:43:42123
124 int32_t next_sequence_number_;
125
[email protected]93df81e2012-08-10 22:22:46126 bool sent_create_to_browser_;
[email protected]eccf80312012-07-14 15:43:42127 bool sent_create_to_renderer_;
128
[email protected]e1f5c9b2012-10-04 00:07:44129 typedef std::map<int32_t, scoped_refptr<PluginResourceCallbackBase> >
130 CallbackMap;
131 CallbackMap callbacks_;
132
[email protected]eccf80312012-07-14 15:43:42133 DISALLOW_COPY_AND_ASSIGN(PluginResource);
134};
135
[email protected]e1f5c9b2012-10-04 00:07:44136template<typename ReplyMsgClass, typename CallbackType>
[email protected]9164da32012-10-16 03:40:57137int32_t PluginResource::Call(Destination dest,
138 const IPC::Message& msg,
139 const CallbackType& callback) {
140 ResourceMessageCallParams params(pp_resource(), next_sequence_number_++);
[email protected]e1f5c9b2012-10-04 00:07:44141 // Stash the |callback| in |callbacks_| identified by the sequence number of
142 // the call.
143 scoped_refptr<PluginResourceCallbackBase> plugin_callback(
144 new PluginResourceCallback<ReplyMsgClass, CallbackType>(callback));
145 callbacks_.insert(std::make_pair(params.sequence(), plugin_callback));
146 params.set_has_callback();
[email protected]9164da32012-10-16 03:40:57147 SendResourceCall(dest, params, msg);
[email protected]e1f5c9b2012-10-04 00:07:44148 return params.sequence();
149}
150
[email protected]58786932012-10-13 10:16:08151template <class ReplyMsgClass>
152int32_t PluginResource::SyncCall(Destination dest, const IPC::Message& msg) {
153 IPC::Message reply;
154 return GenericSyncCall(dest, msg, &reply);
155}
156
157template <class ReplyMsgClass, class A>
158int32_t PluginResource::SyncCall(
159 Destination dest, const IPC::Message& msg, A* a) {
160 IPC::Message reply;
161 int32_t result = GenericSyncCall(dest, msg, &reply);
162
163 if (UnpackMessage<ReplyMsgClass>(reply, a))
164 return result;
165 return PP_ERROR_FAILED;
166}
167
168template <class ReplyMsgClass, class A, class B>
169int32_t PluginResource::SyncCall(
170 Destination dest, const IPC::Message& msg, A* a, B* b) {
171 IPC::Message reply;
172 int32_t result = GenericSyncCall(dest, msg, &reply);
173
174 if (UnpackMessage<ReplyMsgClass>(reply, a, b))
175 return result;
176 return PP_ERROR_FAILED;
177}
178
179template <class ReplyMsgClass, class A, class B, class C>
180int32_t PluginResource::SyncCall(
181 Destination dest, const IPC::Message& msg, A* a, B* b, C* c) {
182 IPC::Message reply;
183 int32_t result = GenericSyncCall(dest, msg, &reply);
184
185 if (UnpackMessage<ReplyMsgClass>(reply, a, b, c))
186 return result;
187 return PP_ERROR_FAILED;
188}
189
190template <class ReplyMsgClass, class A, class B, class C, class D>
191int32_t PluginResource::SyncCall(
192 Destination dest, const IPC::Message& msg, A* a, B* b, C* c, D* d) {
193 IPC::Message reply;
194 int32_t result = GenericSyncCall(dest, msg, &reply);
195
196 if (UnpackMessage<ReplyMsgClass>(reply, a, b, c, d))
197 return result;
198 return PP_ERROR_FAILED;
199}
200
201template <class ReplyMsgClass, class A, class B, class C, class D, class E>
202int32_t PluginResource::SyncCall(
203 Destination dest, const IPC::Message& msg, A* a, B* b, C* c, D* d, E* e) {
204 IPC::Message reply;
205 int32_t result = GenericSyncCall(dest, msg, &reply);
206
207 if (UnpackMessage<ReplyMsgClass>(reply, a, b, c, d, e))
208 return result;
209 return PP_ERROR_FAILED;
210}
211
[email protected]eccf80312012-07-14 15:43:42212} // namespace proxy
213} // namespace ppapi
214
215#endif // PPAPI_PROXY_PLUGIN_RESOURCE_H_