diff options
author | yzshen@chromium.org <yzshen@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2012-10-13 10:16:08 +0000 |
---|---|---|
committer | yzshen@chromium.org <yzshen@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2012-10-13 10:16:08 +0000 |
commit | 587869384ed4cf74d88974558500505f966adef0 (patch) | |
tree | 24413674058af424f3b944724f013833cdcc9910 /ppapi | |
parent | 4a359b11c7419835221c627705f8e72274b20058 (diff) | |
download | chromium_src-587869384ed4cf74d88974558500505f966adef0.zip chromium_src-587869384ed4cf74d88974558500505f966adef0.tar.gz chromium_src-587869384ed4cf74d88974558500505f966adef0.tar.bz2 |
Add template SyncCall() to PluginResource.
BUG=None
TEST=None
Review URL: https://chromiumcodereview.appspot.com/11028104
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@161749 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'ppapi')
-rw-r--r-- | ppapi/ppapi_proxy.gypi | 1 | ||||
-rw-r--r-- | ppapi/proxy/flash_font_file_resource.cc | 13 | ||||
-rw-r--r-- | ppapi/proxy/flash_resource.cc | 14 | ||||
-rw-r--r-- | ppapi/proxy/plugin_resource.cc | 23 | ||||
-rw-r--r-- | ppapi/proxy/plugin_resource.h | 113 | ||||
-rw-r--r-- | ppapi/proxy/ppapi_message_utils.h | 131 |
6 files changed, 252 insertions, 43 deletions
diff --git a/ppapi/ppapi_proxy.gypi b/ppapi/ppapi_proxy.gypi index ba6e8bf..5cb4a86 100644 --- a/ppapi/ppapi_proxy.gypi +++ b/ppapi/ppapi_proxy.gypi @@ -62,6 +62,7 @@ 'proxy/plugin_var_tracker.h', 'proxy/ppapi_command_buffer_proxy.h', 'proxy/ppapi_command_buffer_proxy.cc', + 'proxy/ppapi_message_utils.h', 'proxy/ppb_audio_input_proxy.cc', 'proxy/ppb_audio_input_proxy.h', 'proxy/ppb_audio_proxy.cc', diff --git a/ppapi/proxy/flash_font_file_resource.cc b/ppapi/proxy/flash_font_file_resource.cc index ecb8bc2..9dec65e 100644 --- a/ppapi/proxy/flash_font_file_resource.cc +++ b/ppapi/proxy/flash_font_file_resource.cc @@ -44,17 +44,14 @@ PP_Bool FlashFontFileResource::GetFontTable(uint32_t table, std::string* contents = GetFontTable(table); if (!contents) { - IPC::Message reply; - int32_t result = CallRendererSync( - PpapiHostMsg_FlashFontFile_GetFontTable(table), &reply); + std::string out_contents; + int32_t result = SyncCall<PpapiPluginMsg_FlashFontFile_GetFontTableReply>( + RENDERER, PpapiHostMsg_FlashFontFile_GetFontTable(table), + &out_contents); if (result != PP_OK) return PP_FALSE; - PpapiPluginMsg_FlashFontFile_GetFontTableReply::Param param; - if (!PpapiPluginMsg_FlashFontFile_GetFontTableReply::Read(&reply, ¶m)) - return PP_FALSE; - - contents = AddFontTable(table, param.a); + contents = AddFontTable(table, out_contents); } // If we are going to copy the data into |output|, it must be big enough. diff --git a/ppapi/proxy/flash_resource.cc b/ppapi/proxy/flash_resource.cc index 62e97e16..41bec16 100644 --- a/ppapi/proxy/flash_resource.cc +++ b/ppapi/proxy/flash_resource.cc @@ -39,17 +39,15 @@ int32_t FlashResource::EnumerateVideoCaptureDevices( if (enter.failed()) return PP_ERROR_NOINTERFACE; - IPC::Message reply; std::vector<ppapi::DeviceRefData> device_ref_data; - int32_t result = CallRendererSync( - PpapiHostMsg_Flash_EnumerateVideoCaptureDevices( - enter.resource()->host_resource()), &reply); + int32_t result = + SyncCall<PpapiPluginMsg_Flash_EnumerateVideoCaptureDevicesReply>( + RENDERER, + PpapiHostMsg_Flash_EnumerateVideoCaptureDevices( + enter.resource()->host_resource()), + &device_ref_data); if (result != PP_OK) return result; - PpapiPluginMsg_Flash_EnumerateVideoCaptureDevicesReply::Schema::Param p; - if (!PpapiPluginMsg_Flash_EnumerateVideoCaptureDevicesReply::Read(&reply, &p)) - return PP_ERROR_FAILED; - device_ref_data = p.a; std::vector<scoped_refptr<Resource> > device_resources; for (size_t i = 0; i < device_ref_data.size(); ++i) { diff --git a/ppapi/proxy/plugin_resource.cc b/ppapi/proxy/plugin_resource.cc index 1cfc3f1..99ade83 100644 --- a/ppapi/proxy/plugin_resource.cc +++ b/ppapi/proxy/plugin_resource.cc @@ -4,7 +4,6 @@ #include "ppapi/proxy/plugin_resource.h" -#include "ppapi/c/pp_errors.h" #include "ppapi/proxy/ppapi_messages.h" #include "ppapi/proxy/resource_message_params.h" @@ -81,29 +80,15 @@ bool PluginResource::SendResourceCall( return sender->Send(new PpapiHostMsg_ResourceCall(call_params, nested_msg)); } -int32_t PluginResource::CallBrowserSync(const IPC::Message& msg, +int32_t PluginResource::GenericSyncCall(Destination dest, + const IPC::Message& msg, IPC::Message* reply) { ResourceMessageCallParams params(pp_resource(), next_sequence_number_++); params.set_has_callback(); ResourceMessageReplyParams reply_params; - bool success = - connection_.browser_sender->Send(new PpapiHostMsg_ResourceSyncCall( - params, msg, &reply_params, reply)); - if (success) - return reply_params.result(); - return PP_ERROR_FAILED; -} - -int32_t PluginResource::CallRendererSync(const IPC::Message& msg, - IPC::Message* reply) { - ResourceMessageCallParams params(pp_resource(), - next_sequence_number_++); - params.set_has_callback(); - ResourceMessageReplyParams reply_params; - bool success = - connection_.renderer_sender->Send(new PpapiHostMsg_ResourceSyncCall( - params, msg, &reply_params, reply)); + bool success = GetSender(dest)->Send(new PpapiHostMsg_ResourceSyncCall( + params, msg, &reply_params, reply)); if (success) return reply_params.result(); return PP_ERROR_FAILED; diff --git a/ppapi/proxy/plugin_resource.h b/ppapi/proxy/plugin_resource.h index 00c29a1..727ff9e 100644 --- a/ppapi/proxy/plugin_resource.h +++ b/ppapi/proxy/plugin_resource.h @@ -7,17 +7,17 @@ #include <map> +#include "base/basictypes.h" #include "base/compiler_specific.h" +#include "ipc/ipc_message.h" #include "ipc/ipc_sender.h" +#include "ppapi/c/pp_errors.h" #include "ppapi/proxy/connection.h" #include "ppapi/proxy/plugin_resource_callback.h" +#include "ppapi/proxy/ppapi_message_utils.h" #include "ppapi/proxy/ppapi_proxy_export.h" #include "ppapi/shared_impl/resource.h" -namespace IPC { -class Message; -} - namespace ppapi { namespace proxy { @@ -40,6 +40,16 @@ class PPAPI_PROXY_EXPORT PluginResource : public Resource { virtual void OnReplyReceived(const proxy::ResourceMessageReplyParams& params, const IPC::Message& msg) OVERRIDE; protected: + enum Destination { + RENDERER = 0, + BROWSER = 1 + }; + + IPC::Sender* GetSender(Destination dest) { + return dest == RENDERER ? connection_.renderer_sender : + connection_.browser_sender; + } + // Sends a create message to the browser or renderer for the current resource. void SendCreateToBrowser(const IPC::Message& msg); void SendCreateToRenderer(const IPC::Message& msg); @@ -71,10 +81,32 @@ class PPAPI_PROXY_EXPORT PluginResource : public Resource { template<typename ReplyMsgClass, typename CallbackType> int32_t CallRenderer(const IPC::Message& msg, const CallbackType& callback); - // Call the browser/renderer with sync messages. The pepper error code from - // the call is returned and the reply message is stored in |reply_msg|. - int32_t CallBrowserSync(const IPC::Message& msg, IPC::Message* reply_msg); - int32_t CallRendererSync(const IPC::Message& msg, IPC::Message* reply_msg); + // Calls the browser/renderer with sync messages. Returns the pepper error + // code from the call. + // |ReplyMsgClass| is the type of the reply message that is expected. If it + // carries x parameters, then the method with x out parameters should be used. + // An example of usage: + // + // // Assuming the reply message carries a string and an integer. + // std::string param_1; + // int param_2 = 0; + // int32_t result = SyncCall<PpapiPluginMsg_MyResourceType_MyReplyMessage>( + // RENDERER, PpapiHostMsg_MyResourceType_MyRequestMessage(), + // ¶m_1, ¶m_2); + template <class ReplyMsgClass> + int32_t SyncCall(Destination dest, const IPC::Message& msg); + template <class ReplyMsgClass, class A> + int32_t SyncCall(Destination dest, const IPC::Message& msg, A* a); + template <class ReplyMsgClass, class A, class B> + int32_t SyncCall(Destination dest, const IPC::Message& msg, A* a, B* b); + template <class ReplyMsgClass, class A, class B, class C> + int32_t SyncCall(Destination dest, const IPC::Message& msg, A* a, B* b, C* c); + template <class ReplyMsgClass, class A, class B, class C, class D> + int32_t SyncCall( + Destination dest, const IPC::Message& msg, A* a, B* b, C* c, D* d); + template <class ReplyMsgClass, class A, class B, class C, class D, class E> + int32_t SyncCall( + Destination dest, const IPC::Message& msg, A* a, B* b, C* c, D* d, E* e); private: // Helper function to send a |PpapiHostMsg_ResourceCall| to the given sender @@ -89,6 +121,10 @@ class PPAPI_PROXY_EXPORT PluginResource : public Resource { const IPC::Message& msg, const CallbackType& callback); + int32_t GenericSyncCall(Destination dest, + const IPC::Message& msg, + IPC::Message* reply_msg); + Connection connection_; int32_t next_sequence_number_; @@ -133,6 +169,67 @@ int32_t PluginResource::CallHost(IPC::Sender* sender, return params.sequence(); } +template <class ReplyMsgClass> +int32_t PluginResource::SyncCall(Destination dest, const IPC::Message& msg) { + IPC::Message reply; + return GenericSyncCall(dest, msg, &reply); +} + +template <class ReplyMsgClass, class A> +int32_t PluginResource::SyncCall( + Destination dest, const IPC::Message& msg, A* a) { + IPC::Message reply; + int32_t result = GenericSyncCall(dest, msg, &reply); + + if (UnpackMessage<ReplyMsgClass>(reply, a)) + return result; + return PP_ERROR_FAILED; +} + +template <class ReplyMsgClass, class A, class B> +int32_t PluginResource::SyncCall( + Destination dest, const IPC::Message& msg, A* a, B* b) { + IPC::Message reply; + int32_t result = GenericSyncCall(dest, msg, &reply); + + if (UnpackMessage<ReplyMsgClass>(reply, a, b)) + return result; + return PP_ERROR_FAILED; +} + +template <class ReplyMsgClass, class A, class B, class C> +int32_t PluginResource::SyncCall( + Destination dest, const IPC::Message& msg, A* a, B* b, C* c) { + IPC::Message reply; + int32_t result = GenericSyncCall(dest, msg, &reply); + + if (UnpackMessage<ReplyMsgClass>(reply, a, b, c)) + return result; + return PP_ERROR_FAILED; +} + +template <class ReplyMsgClass, class A, class B, class C, class D> +int32_t PluginResource::SyncCall( + Destination dest, const IPC::Message& msg, A* a, B* b, C* c, D* d) { + IPC::Message reply; + int32_t result = GenericSyncCall(dest, msg, &reply); + + if (UnpackMessage<ReplyMsgClass>(reply, a, b, c, d)) + return result; + return PP_ERROR_FAILED; +} + +template <class ReplyMsgClass, class A, class B, class C, class D, class E> +int32_t PluginResource::SyncCall( + Destination dest, const IPC::Message& msg, A* a, B* b, C* c, D* d, E* e) { + IPC::Message reply; + int32_t result = GenericSyncCall(dest, msg, &reply); + + if (UnpackMessage<ReplyMsgClass>(reply, a, b, c, d, e)) + return result; + return PP_ERROR_FAILED; +} + } // namespace proxy } // namespace ppapi diff --git a/ppapi/proxy/ppapi_message_utils.h b/ppapi/proxy/ppapi_message_utils.h new file mode 100644 index 0000000..68667dd --- /dev/null +++ b/ppapi/proxy/ppapi_message_utils.h @@ -0,0 +1,131 @@ +// Copyright (c) 2012 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#ifndef PPAPI_PROXY_PPAPI_MESSAGE_UTILS_H_ +#define PPAPI_PROXY_PPAPI_MESSAGE_UTILS_H_ + +#include "base/basictypes.h" +#include "base/pickle.h" +#include "base/tuple.h" +#include "ipc/ipc_message.h" +#include "ipc/ipc_message_utils.h" + +namespace ppapi { + +namespace internal { + +// TupleTypeMatch* check whether a tuple type contains elements of the specified +// types. They are used to make sure the output parameters of UnpackMessage() +// match the corresponding message type. +template <class TupleType, class A> +struct TupleTypeMatch1 { + static const bool kValue = false; +}; +template <class A> +struct TupleTypeMatch1<Tuple1<A>, A> { + static const bool kValue = true; +}; + +template <class TupleType, class A, class B> +struct TupleTypeMatch2 { + static const bool kValue = false; +}; +template <class A, class B> +struct TupleTypeMatch2<Tuple2<A, B>, A, B> { + static const bool kValue = true; +}; + +template <class TupleType, class A, class B, class C> +struct TupleTypeMatch3 { + static const bool kValue = false; +}; +template <class A, class B, class C> +struct TupleTypeMatch3<Tuple3<A, B, C>, A, B, C> { + static const bool kValue = true; +}; + +template <class TupleType, class A, class B, class C, class D> +struct TupleTypeMatch4 { + static const bool kValue = false; +}; +template <class A, class B, class C, class D> +struct TupleTypeMatch4<Tuple4<A, B, C, D>, A, B, C, D> { + static const bool kValue = true; +}; + +template <class TupleType, class A, class B, class C, class D, class E> +struct TupleTypeMatch5 { + static const bool kValue = false; +}; +template <class A, class B, class C, class D, class E> +struct TupleTypeMatch5<Tuple5<A, B, C, D, E>, A, B, C, D, E> { + static const bool kValue = true; +}; + +} // namespace internal + +template <class MsgClass, class A> +bool UnpackMessage(const IPC::Message& msg, A* a) { + COMPILE_ASSERT( + (internal::TupleTypeMatch1<typename MsgClass::Param, A>::kValue), + tuple_types_dont_match); + + PickleIterator iter(msg); + return IPC::ReadParam(&msg, &iter, a); +} + +template <class MsgClass, class A, class B> +bool UnpackMessage(const IPC::Message& msg, A* a, B* b) { + COMPILE_ASSERT( + (internal::TupleTypeMatch2<typename MsgClass::Param, A, B>::kValue), + tuple_types_dont_match); + + PickleIterator iter(msg); + return IPC::ReadParam(&msg, &iter, a) && IPC::ReadParam(&msg, &iter, b); +} + +template <class MsgClass, class A, class B, class C> +bool UnpackMessage(const IPC::Message& msg, A* a, B* b, C* c) { + COMPILE_ASSERT( + (internal::TupleTypeMatch3<typename MsgClass::Param, A, B, C>::kValue), + tuple_types_dont_match); + + PickleIterator iter(msg); + return IPC::ReadParam(&msg, &iter, a) && + IPC::ReadParam(&msg, &iter, b) && + IPC::ReadParam(&msg, &iter, c); +} + +template <class MsgClass, class A, class B, class C, class D> +bool UnpackMessage(const IPC::Message& msg, A* a, B* b, C* c, D* d) { + COMPILE_ASSERT( + (internal::TupleTypeMatch4<typename MsgClass::Param, A, B, C, D>::kValue), + tuple_types_dont_match); + + PickleIterator iter(msg); + return IPC::ReadParam(&msg, &iter, a) && + IPC::ReadParam(&msg, &iter, b) && + IPC::ReadParam(&msg, &iter, c) && + IPC::ReadParam(&msg, &iter, d); +} + +template <class MsgClass, class A, class B, class C, class D, class E> +bool UnpackMessage(const IPC::Message& msg, A* a, B* b, C* c, D* d, E* e) { + COMPILE_ASSERT( + (internal::TupleTypeMatch5< + typename MsgClass::Param, A, B, C, D, E>::kValue), + tuple_types_dont_match); + + PickleIterator iter(msg); + return IPC::ReadParam(&msg, &iter, a) && + IPC::ReadParam(&msg, &iter, b) && + IPC::ReadParam(&msg, &iter, c) && + IPC::ReadParam(&msg, &iter, d) && + IPC::ReadParam(&msg, &iter, e); +} + +} // namespace ppapi + +#endif // PPAPI_PROXY_PPAPI_MESSAGE_UTILS_H_ + |