Converted PluginResource reply message handling to use base::Callback
Previously each PluginResource had to write a reply handler (|OnReplyReceived|) for any replies to resource messages. This approach had several problems including the fact that the PluginResource had to track the state of any outstanding calls.
This change allows you to register a base::Callback when calling CallToBrowser/CallToRenderer. The callback will be run when a reply message is received with a sequence number matching the call. The parameters of the reply will be passed to the callback. 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 the expected reply message (for example, in the case of an error), the callback will still be invoked but with the default values of the message parameters.
BUG=
Review URL: https://chromiumcodereview.appspot.com/11022005
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@160015 0039d316-1c4b-4281-b951-d872f2087c98
diff --git a/ppapi/proxy/plugin_resource.cc b/ppapi/proxy/plugin_resource.cc
index f86bc3db..1cfc3f10 100644
--- a/ppapi/proxy/plugin_resource.cc
+++ b/ppapi/proxy/plugin_resource.cc
@@ -30,6 +30,20 @@
}
}
+void PluginResource::OnReplyReceived(
+ const proxy::ResourceMessageReplyParams& params,
+ const IPC::Message& msg) {
+ // Grab the callback for the reply sequence number and run it with |msg|.
+ CallbackMap::iterator it = callbacks_.find(params.sequence());
+ if (it == callbacks_.end()) {
+ DCHECK(false) << "Callback does not exist for an expected sequence number.";
+ } else {
+ scoped_refptr<PluginResourceCallbackBase> callback = it->second;
+ callbacks_.erase(it);
+ callback->Run(params, msg);
+ }
+}
+
void PluginResource::SendCreateToBrowser(const IPC::Message& msg) {
DCHECK(!sent_create_to_browser_);
sent_create_to_browser_ = true;
@@ -51,29 +65,20 @@
void PluginResource::PostToBrowser(const IPC::Message& msg) {
ResourceMessageCallParams params(pp_resource(),
next_sequence_number_++);
- connection_.browser_sender->Send(new PpapiHostMsg_ResourceCall(params, msg));
+ SendResourceCall(connection_.browser_sender, params, msg);
}
void PluginResource::PostToRenderer(const IPC::Message& msg) {
ResourceMessageCallParams params(pp_resource(),
next_sequence_number_++);
- connection_.renderer_sender->Send(new PpapiHostMsg_ResourceCall(params, msg));
+ SendResourceCall(connection_.renderer_sender, params, msg);
}
-int32_t PluginResource::CallBrowser(const IPC::Message& msg) {
- ResourceMessageCallParams params(pp_resource(),
- next_sequence_number_++);
- params.set_has_callback();
- connection_.browser_sender->Send(new PpapiHostMsg_ResourceCall(params, msg));
- return params.sequence();
-}
-
-int32_t PluginResource::CallRenderer(const IPC::Message& msg) {
- ResourceMessageCallParams params(pp_resource(),
- next_sequence_number_++);
- params.set_has_callback();
- connection_.renderer_sender->Send(new PpapiHostMsg_ResourceCall(params, msg));
- return params.sequence();
+bool PluginResource::SendResourceCall(
+ IPC::Sender* sender,
+ const ResourceMessageCallParams& call_params,
+ const IPC::Message& nested_msg) {
+ return sender->Send(new PpapiHostMsg_ResourceCall(call_params, nested_msg));
}
int32_t PluginResource::CallBrowserSync(const IPC::Message& msg,