blob: 00c29a1711ac2d36e1ea46a9ab03925bf1bdf105 [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]eccf80312012-07-14 15:43:4210#include "base/compiler_specific.h"
11#include "ipc/ipc_sender.h"
[email protected]93df81e2012-08-10 22:22:4612#include "ppapi/proxy/connection.h"
[email protected]e1f5c9b2012-10-04 00:07:4413#include "ppapi/proxy/plugin_resource_callback.h"
[email protected]eccf80312012-07-14 15:43:4214#include "ppapi/proxy/ppapi_proxy_export.h"
15#include "ppapi/shared_impl/resource.h"
16
17namespace IPC {
18class Message;
19}
20
21namespace ppapi {
22namespace proxy {
23
24class PluginDispatcher;
25
[email protected]93df81e2012-08-10 22:22:4626class PPAPI_PROXY_EXPORT PluginResource : public Resource {
[email protected]eccf80312012-07-14 15:43:4227 public:
[email protected]93df81e2012-08-10 22:22:4628 PluginResource(Connection connection, PP_Instance instance);
[email protected]eccf80312012-07-14 15:43:4229 virtual ~PluginResource();
30
[email protected]93df81e2012-08-10 22:22:4631 // Returns true if we've previously sent a create message to the browser
32 // or renderer. Generally resources will use these to tell if they should
33 // lazily send create messages.
34 bool sent_create_to_browser() const { return sent_create_to_browser_; }
[email protected]eccf80312012-07-14 15:43:4235 bool sent_create_to_renderer() const { return sent_create_to_renderer_; }
36
[email protected]e1f5c9b2012-10-04 00:07:4437 // This handles a reply to a resource call. It works by looking up the
38 // callback that was registered when CallBrowser/CallRenderer was called
39 // and calling it with |params| and |msg|.
40 virtual void OnReplyReceived(const proxy::ResourceMessageReplyParams& params,
41 const IPC::Message& msg) OVERRIDE;
[email protected]eccf80312012-07-14 15:43:4242 protected:
[email protected]93df81e2012-08-10 22:22:4643 // Sends a create message to the browser or renderer for the current resource.
44 void SendCreateToBrowser(const IPC::Message& msg);
[email protected]eccf80312012-07-14 15:43:4245 void SendCreateToRenderer(const IPC::Message& msg);
46
47 // Sends the given IPC message as a resource request to the host
48 // corresponding to this resource object and does not expect a reply.
[email protected]93df81e2012-08-10 22:22:4649 void PostToBrowser(const IPC::Message& msg);
[email protected]eccf80312012-07-14 15:43:4250 void PostToRenderer(const IPC::Message& msg);
51
[email protected]e1f5c9b2012-10-04 00:07:4452 // Like PostToBrowser/Renderer but expects a response. |callback| is
53 // a |base::Callback| that will be run when a reply message with a sequence
54 // number matching that of the call is received. |ReplyMsgClass| is the type
55 // of the reply message that is expected. An example of usage:
56 //
57 // CallBrowser<PpapiPluginMsg_MyResourceType_MyReplyMessage>(
58 // PpapiHostMsg_MyResourceType_MyRequestMessage(),
59 // base::Bind(&MyPluginResource::ReplyHandler, this));
60 //
61 // If a reply message to this call is received whose type does not match
62 // |ReplyMsgClass| (for example, in the case of an error), the callback will
63 // still be invoked but with the default values of the message parameters.
[email protected]eccf80312012-07-14 15:43:4264 //
65 // Returns the new request's sequence number which can be used to identify
[email protected]e1f5c9b2012-10-04 00:07:4466 // the callback.
[email protected]eccf80312012-07-14 15:43:4267 //
68 // Note that all integers (including 0 and -1) are valid request IDs.
[email protected]e1f5c9b2012-10-04 00:07:4469 template<typename ReplyMsgClass, typename CallbackType>
70 int32_t CallBrowser(const IPC::Message& msg, const CallbackType& callback);
71 template<typename ReplyMsgClass, typename CallbackType>
72 int32_t CallRenderer(const IPC::Message& msg, const CallbackType& callback);
[email protected]eccf80312012-07-14 15:43:4273
[email protected]ff44fc12012-10-03 00:52:1674 // Call the browser/renderer with sync messages. The pepper error code from
75 // the call is returned and the reply message is stored in |reply_msg|.
76 int32_t CallBrowserSync(const IPC::Message& msg, IPC::Message* reply_msg);
77 int32_t CallRendererSync(const IPC::Message& msg, IPC::Message* reply_msg);
78
[email protected]eccf80312012-07-14 15:43:4279 private:
[email protected]e1f5c9b2012-10-04 00:07:4480 // Helper function to send a |PpapiHostMsg_ResourceCall| to the given sender
81 // with |nested_msg| and |call_params|.
82 bool SendResourceCall(IPC::Sender* sender,
83 const ResourceMessageCallParams& call_params,
84 const IPC::Message& nested_msg);
85
86 // Helper function to make a Resource Call to a host with a callback.
87 template<typename ReplyMsgClass, typename CallbackType>
88 int32_t CallHost(IPC::Sender* sender,
89 const IPC::Message& msg,
90 const CallbackType& callback);
91
[email protected]93df81e2012-08-10 22:22:4692 Connection connection_;
[email protected]eccf80312012-07-14 15:43:4293
94 int32_t next_sequence_number_;
95
[email protected]93df81e2012-08-10 22:22:4696 bool sent_create_to_browser_;
[email protected]eccf80312012-07-14 15:43:4297 bool sent_create_to_renderer_;
98
[email protected]e1f5c9b2012-10-04 00:07:4499 typedef std::map<int32_t, scoped_refptr<PluginResourceCallbackBase> >
100 CallbackMap;
101 CallbackMap callbacks_;
102
[email protected]eccf80312012-07-14 15:43:42103 DISALLOW_COPY_AND_ASSIGN(PluginResource);
104};
105
[email protected]e1f5c9b2012-10-04 00:07:44106template<typename ReplyMsgClass, typename CallbackType>
107int32_t PluginResource::CallBrowser(const IPC::Message& msg,
108 const CallbackType& callback) {
109 return CallHost<ReplyMsgClass, CallbackType>(
110 connection_.browser_sender, msg, callback);
111}
112
113template<typename ReplyMsgClass, typename CallbackType>
114int32_t PluginResource::CallRenderer(const IPC::Message& msg,
115 const CallbackType& callback) {
116 return CallHost<ReplyMsgClass, CallbackType>(
117 connection_.renderer_sender, msg, callback);
118}
119
120template<typename ReplyMsgClass, typename CallbackType>
121int32_t PluginResource::CallHost(IPC::Sender* sender,
122 const IPC::Message& msg,
123 const CallbackType& callback) {
124 ResourceMessageCallParams params(pp_resource(),
125 next_sequence_number_++);
126 // Stash the |callback| in |callbacks_| identified by the sequence number of
127 // the call.
128 scoped_refptr<PluginResourceCallbackBase> plugin_callback(
129 new PluginResourceCallback<ReplyMsgClass, CallbackType>(callback));
130 callbacks_.insert(std::make_pair(params.sequence(), plugin_callback));
131 params.set_has_callback();
132 SendResourceCall(sender, params, msg);
133 return params.sequence();
134}
135
[email protected]eccf80312012-07-14 15:43:42136} // namespace proxy
137} // namespace ppapi
138
139#endif // PPAPI_PROXY_PLUGIN_RESOURCE_H_