Chromium Code Reviews| Index: ppapi/proxy/plugin_resource.h |
| diff --git a/ppapi/proxy/plugin_resource.h b/ppapi/proxy/plugin_resource.h |
| index 972908a6ddfdc9d99bdcb5f185f211116909f228..01855d2ac6eea8017cb6c9aed9d88e0262b7c373 100644 |
| --- a/ppapi/proxy/plugin_resource.h |
| +++ b/ppapi/proxy/plugin_resource.h |
| @@ -5,9 +5,12 @@ |
| #ifndef PPAPI_PROXY_PLUGIN_RESOURCE_H_ |
| #define PPAPI_PROXY_PLUGIN_RESOURCE_H_ |
| +#include <map> |
| + |
| #include "base/compiler_specific.h" |
| #include "ipc/ipc_sender.h" |
| #include "ppapi/proxy/connection.h" |
| +#include "ppapi/proxy/plugin_resource_callback.h" |
| #include "ppapi/proxy/ppapi_proxy_export.h" |
| #include "ppapi/shared_impl/resource.h" |
| @@ -31,6 +34,11 @@ class PPAPI_PROXY_EXPORT PluginResource : public Resource { |
| bool sent_create_to_browser() const { return sent_create_to_browser_; } |
| bool sent_create_to_renderer() const { return sent_create_to_renderer_; } |
| + // This handles a reply to a resource call. It works by looking up the |
| + // callback that was registered when CallBrowser/CallRenderer was called |
| + // and calling it with |params| and |msg|. |
| + virtual void OnReplyReceived(const proxy::ResourceMessageReplyParams& params, |
| + const IPC::Message& msg) OVERRIDE; |
| protected: |
| // Sends a create message to the browser or renderer for the current resource. |
| void SendCreateToBrowser(const IPC::Message& msg); |
| @@ -41,17 +49,41 @@ class PPAPI_PROXY_EXPORT PluginResource : public Resource { |
| void PostToBrowser(const IPC::Message& msg); |
| void PostToRenderer(const IPC::Message& msg); |
| - // Like PostToBrowser/Renderer but expects a response. |
| + // Like PostToBrowser/Renderer but expects a response. |callback| is |
| + // a |base::Callback| that will be run when a reply message with a sequence |
| + // number matching that of the call is received. |MsgClass| is the type of the |
| + // reply message that is expected. An example of usage: |
| + // |
| + // CallBrowser<PpapiPluginMsg_MyResourceType_MyReplyMessage>( |
| + // PpapiHostMsg_MyResourceType_MyRequestMessage(), |
| + // base::Bind(&MyPluginResource::ReplyHandler, this)); |
| + // |
| + // If a reply message to this call is received whose type does not match |
| + // |MsgClass| (for example, in the case of an error), the callback will still |
| + // be invoked but with the default values of the message parameters. |
| // |
| // Returns the new request's sequence number which can be used to identify |
| - // the callback. The host will reply and ppapi::Resource::OnReplyReceived |
| - // will be called. |
| + // the callback. |
| // |
| // Note that all integers (including 0 and -1) are valid request IDs. |
| - int32_t CallBrowser(const IPC::Message& msg); |
| - int32_t CallRenderer(const IPC::Message& msg); |
| + template<typename MsgClass, typename CallbackType> |
|
yzshen1
2012/10/01 23:41:24
nit: ReplyMsgClass might be more clear. (Here and
raymes
2012/10/02 16:13:36
Done.
|
| + int32_t CallBrowser(const IPC::Message& msg, const CallbackType& callback); |
| + template<typename MsgClass, typename CallbackType> |
| + int32_t CallRenderer(const IPC::Message& msg, const CallbackType& callback); |
| private: |
| + // Helper function to send a |PpapiHostMsg_ResourceCall| to the given sender |
| + // with |nested_msg| and |call_params|. |
| + bool SendResourceCall(IPC::Sender* sender, |
| + const ResourceMessageCallParams& call_params, |
| + const IPC::Message& nested_msg); |
| + |
| + // Helper function to make a Resource Call to a host with a callback. |
| + template<typename MsgClass, typename CallbackType> |
| + int32_t CallHost(IPC::Sender* sender, |
| + const IPC::Message& msg, |
| + const CallbackType& callback); |
| + |
| Connection connection_; |
| int32_t next_sequence_number_; |
| @@ -59,9 +91,43 @@ class PPAPI_PROXY_EXPORT PluginResource : public Resource { |
| bool sent_create_to_browser_; |
| bool sent_create_to_renderer_; |
| + typedef std::map<int32_t, scoped_refptr<PluginResourceCallbackBase> > |
| + CallbackMap; |
| + CallbackMap callbacks_; |
| + |
| DISALLOW_COPY_AND_ASSIGN(PluginResource); |
| }; |
| +template<typename MsgClass, typename CallbackType> |
| +int32_t PluginResource::CallBrowser(const IPC::Message& msg, |
| + const CallbackType& callback) { |
| + return CallHost<MsgClass, CallbackType>( |
| + connection_.browser_sender, msg, callback); |
| +} |
| + |
| +template<typename MsgClass, typename CallbackType> |
| +int32_t PluginResource::CallRenderer(const IPC::Message& msg, |
| + const CallbackType& callback) { |
| + return CallHost<MsgClass, CallbackType>( |
| + connection_.renderer_sender, msg, callback); |
| +} |
| + |
| +template<typename MsgClass, typename CallbackType> |
| +int32_t PluginResource::CallHost(IPC::Sender* sender, |
| + const IPC::Message& msg, |
| + const CallbackType& callback) { |
| + ResourceMessageCallParams params(pp_resource(), |
| + next_sequence_number_++); |
| + // Stash the |callback| in |callbacks_| identified by the sequence number of |
| + // the call. |
| + scoped_refptr<PluginResourceCallbackBase> plugin_callback( |
| + new PluginResourceCallback<MsgClass, CallbackType>(callback)); |
| + callbacks_.insert(std::make_pair(params.sequence(), plugin_callback)); |
| + params.set_has_callback(); |
| + SendResourceCall(sender, params, msg); |
| + return params.sequence(); |
| +} |
| + |
| } // namespace proxy |
| } // namespace ppapi |