diff options
author | jam@chromium.org <jam@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2009-02-04 03:15:58 +0000 |
---|---|---|
committer | jam@chromium.org <jam@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2009-02-04 03:15:58 +0000 |
commit | 39b7b06790a776ee2247f391b1b2e3af1a335503 (patch) | |
tree | 8813c8831c92e6f00e8e72bbeaa95693e695c683 /chrome/common/ipc_message_utils.h | |
parent | d718a62d075bdd6f45a128bd5d82358d73799300 (diff) | |
download | chromium_src-39b7b06790a776ee2247f391b1b2e3af1a335503.zip chromium_src-39b7b06790a776ee2247f391b1b2e3af1a335503.tar.gz chromium_src-39b7b06790a776ee2247f391b1b2e3af1a335503.tar.bz2 |
Make it easier/less work/less error-prone to create new IPC channel types (i.e. renderer/plugin).Instead of having each message file include the internal one several times with different ifdefs, move that logic to ipc_message_macros.h. Also make the message class starting IDs come from an enum to ensure we don't use a value twice. I simplified the logging code a bit so we don't need X_messages.cc files.Clean up places that we were doing manual packing/unpacking. Most of this was in the automation code. I added a few new template functions to make it convenient to read the parameters from a message, and updated the code to use them.I also removed unnecessary includes of render/plugin_messages.h from headers to speed up compiling.I moved the traits of IPC structs beside the struct definition to make it more apparent what's going on, so we avoid people modifying the struct and forgetting to update the traits.Amit: please look at chrome/test/automation/tab_proxy.ccMarc-Antoine: chrome/browser/printing/*Matt: the rest
Review URL: http://codereview.chromium.org/20015
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@9123 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'chrome/common/ipc_message_utils.h')
-rw-r--r-- | chrome/common/ipc_message_utils.h | 447 |
1 files changed, 256 insertions, 191 deletions
diff --git a/chrome/common/ipc_message_utils.h b/chrome/common/ipc_message_utils.h index 39bb231..35146ee 100644 --- a/chrome/common/ipc_message_utils.h +++ b/chrome/common/ipc_message_utils.h @@ -34,70 +34,43 @@ namespace webkit_glue { struct WebApplicationInfo; } // namespace webkit_glue -namespace IPC { - -// Used by the message macros to register a logging function based on the -// message class. -typedef void (LogFunction)(uint16 type, - std::wstring* name, - const IPC::Message* msg, - std::wstring* params); -void RegisterMessageLogger(int msg_start, LogFunction* func); - - -//----------------------------------------------------------------------------- -// An iterator class for reading the fields contained within a Message. - -class MessageIterator { - public: - explicit MessageIterator(const Message& m) : msg_(m), iter_(NULL) { - } - int NextInt() const { - int val; - if (!msg_.ReadInt(&iter_, &val)) - NOTREACHED(); - return val; - } - intptr_t NextIntPtr() const { - intptr_t val; - if (!msg_.ReadIntPtr(&iter_, &val)) - NOTREACHED(); - return val; - } - const std::string NextString() const { - std::string val; - if (!msg_.ReadString(&iter_, &val)) - NOTREACHED(); - return val; - } - const std::wstring NextWString() const { - std::wstring val; - if (!msg_.ReadWString(&iter_, &val)) - NOTREACHED(); - return val; - } - const void NextData(const char** data, int* length) const { - if (!msg_.ReadData(&iter_, data, length)) { - NOTREACHED(); - } - } - private: - const Message& msg_; - mutable void* iter_; +// Used by IPC_BEGIN_MESSAGES so that each message class starts from a unique +// base. Messages have unique IDs across channels in order for the IPC logging +// code to figure out the message class from its ID. +enum IPCMessageStart { + // By using a start value of 0 for automation messages, we keep backward + // compatibility with old builds. + AutomationMsgStart = 0, + ViewMsgStart, + ViewHostMsgStart, + PluginProcessMsgStart, + PluginProcessHostMsgStart, + PluginMsgStart, + PluginHostMsgStart, + NPObjectMsgStart, + TestMsgStart, + // NOTE: When you add a new message class, also update + // IPCStatusView::IPCStatusView to ensure logging works. + // NOTE: this enum is used by IPC_MESSAGE_MACRO to generate a unique message + // id. Only 4 bits are used for the message type, so if this enum needs more + // than 16 entries, that code needs to be updated. + LastMsgIndex }; +COMPILE_ASSERT(LastMsgIndex <= 16, need_to_update_IPC_MESSAGE_MACRO); + //----------------------------------------------------------------------------- // ParamTraits specializations, etc. template <class P> struct ParamTraits {}; template <class P> -static inline void WriteParam(Message* m, const P& p) { +static inline void WriteParam(IPC::Message* m, const P& p) { ParamTraits<P>::Write(m, p); } template <class P> -static inline bool ReadParam(const Message* m, void** iter, P* p) { +static inline bool ReadParam(const IPC::Message* m, void** iter, P* p) { return ParamTraits<P>::Read(m, iter, p); } @@ -109,10 +82,10 @@ static inline void LogParam(const P& p, std::wstring* l) { template <> struct ParamTraits<bool> { typedef bool param_type; - static void Write(Message* m, const param_type& p) { + static void Write(IPC::Message* m, const param_type& p) { m->WriteBool(p); } - static bool Read(const Message* m, void** iter, param_type* r) { + static bool Read(const IPC::Message* m, void** iter, param_type* r) { return m->ReadBool(iter, r); } static void Log(const param_type& p, std::wstring* l) { @@ -123,10 +96,10 @@ struct ParamTraits<bool> { template <> struct ParamTraits<int> { typedef int param_type; - static void Write(Message* m, const param_type& p) { + static void Write(IPC::Message* m, const param_type& p) { m->WriteInt(p); } - static bool Read(const Message* m, void** iter, param_type* r) { + static bool Read(const IPC::Message* m, void** iter, param_type* r) { return m->ReadInt(iter, r); } static void Log(const param_type& p, std::wstring* l) { @@ -137,10 +110,10 @@ struct ParamTraits<int> { template <> struct ParamTraits<long> { typedef long param_type; - static void Write(Message* m, const param_type& p) { + static void Write(IPC::Message* m, const param_type& p) { m->WriteLong(p); } - static bool Read(const Message* m, void** iter, param_type* r) { + static bool Read(const IPC::Message* m, void** iter, param_type* r) { return m->ReadLong(iter, r); } static void Log(const param_type& p, std::wstring* l) { @@ -151,10 +124,10 @@ struct ParamTraits<long> { template <> struct ParamTraits<size_t> { typedef size_t param_type; - static void Write(Message* m, const param_type& p) { + static void Write(IPC::Message* m, const param_type& p) { m->WriteSize(p); } - static bool Read(const Message* m, void** iter, param_type* r) { + static bool Read(const IPC::Message* m, void** iter, param_type* r) { return m->ReadSize(iter, r); } static void Log(const param_type& p, std::wstring* l) { @@ -168,10 +141,10 @@ struct ParamTraits<size_t> { template <> struct ParamTraits<uint32> { typedef uint32 param_type; - static void Write(Message* m, const param_type& p) { + static void Write(IPC::Message* m, const param_type& p) { m->WriteUInt32(p); } - static bool Read(const Message* m, void** iter, param_type* r) { + static bool Read(const IPC::Message* m, void** iter, param_type* r) { return m->ReadUInt32(iter, r); } static void Log(const param_type& p, std::wstring* l) { @@ -183,10 +156,10 @@ struct ParamTraits<uint32> { template <> struct ParamTraits<int64> { typedef int64 param_type; - static void Write(Message* m, const param_type& p) { + static void Write(IPC::Message* m, const param_type& p) { m->WriteInt64(p); } - static bool Read(const Message* m, void** iter, param_type* r) { + static bool Read(const IPC::Message* m, void** iter, param_type* r) { return m->ReadInt64(iter, r); } static void Log(const param_type& p, std::wstring* l) { @@ -197,10 +170,10 @@ struct ParamTraits<int64> { template <> struct ParamTraits<uint64> { typedef uint64 param_type; - static void Write(Message* m, const param_type& p) { + static void Write(IPC::Message* m, const param_type& p) { m->WriteInt64(static_cast<int64>(p)); } - static bool Read(const Message* m, void** iter, param_type* r) { + static bool Read(const IPC::Message* m, void** iter, param_type* r) { return m->ReadInt64(iter, reinterpret_cast<int64*>(r)); } static void Log(const param_type& p, std::wstring* l) { @@ -211,10 +184,10 @@ struct ParamTraits<uint64> { template <> struct ParamTraits<double> { typedef double param_type; - static void Write(Message* m, const param_type& p) { + static void Write(IPC::Message* m, const param_type& p) { m->WriteData(reinterpret_cast<const char*>(&p), sizeof(param_type)); } - static bool Read(const Message* m, void** iter, param_type* r) { + static bool Read(const IPC::Message* m, void** iter, param_type* r) { const char *data; int data_size = 0; bool result = m->ReadData(iter, &data, &data_size); @@ -235,10 +208,10 @@ struct ParamTraits<double> { template <> struct ParamTraits<wchar_t> { typedef wchar_t param_type; - static void Write(Message* m, const param_type& p) { + static void Write(IPC::Message* m, const param_type& p) { m->WriteData(reinterpret_cast<const char*>(&p), sizeof(param_type)); } - static bool Read(const Message* m, void** iter, param_type* r) { + static bool Read(const IPC::Message* m, void** iter, param_type* r) { const char *data; int data_size = 0; bool result = m->ReadData(iter, &data, &data_size); @@ -259,10 +232,10 @@ struct ParamTraits<wchar_t> { template <> struct ParamTraits<base::Time> { typedef base::Time param_type; - static void Write(Message* m, const param_type& p) { + static void Write(IPC::Message* m, const param_type& p) { ParamTraits<int64>::Write(m, p.ToInternalValue()); } - static bool Read(const Message* m, void** iter, param_type* r) { + static bool Read(const IPC::Message* m, void** iter, param_type* r) { int64 value; if (!ParamTraits<int64>::Read(m, iter, &value)) return false; @@ -278,10 +251,10 @@ struct ParamTraits<base::Time> { template <> struct ParamTraits<LOGFONT> { typedef LOGFONT param_type; - static void Write(Message* m, const param_type& p) { + static void Write(IPC::Message* m, const param_type& p) { m->WriteData(reinterpret_cast<const char*>(&p), sizeof(LOGFONT)); } - static bool Read(const Message* m, void** iter, param_type* r) { + static bool Read(const IPC::Message* m, void** iter, param_type* r) { const char *data; int data_size = 0; bool result = m->ReadData(iter, &data, &data_size); @@ -302,10 +275,10 @@ struct ParamTraits<LOGFONT> { template <> struct ParamTraits<MSG> { typedef MSG param_type; - static void Write(Message* m, const param_type& p) { + static void Write(IPC::Message* m, const param_type& p) { m->WriteData(reinterpret_cast<const char*>(&p), sizeof(MSG)); } - static bool Read(const Message* m, void** iter, param_type* r) { + static bool Read(const IPC::Message* m, void** iter, param_type* r) { const char *data; int data_size = 0; bool result = m->ReadData(iter, &data, &data_size); @@ -324,11 +297,11 @@ struct ParamTraits<MSG> { template <> struct ParamTraits<SkBitmap> { typedef SkBitmap param_type; - static void Write(Message* m, const param_type& p); + static void Write(IPC::Message* m, const param_type& p); // Note: This function expects parameter |r| to be of type &SkBitmap since // r->SetConfig() and r->SetPixels() are called. - static bool Read(const Message* m, void** iter, param_type* r); + static bool Read(const IPC::Message* m, void** iter, param_type* r); static void Log(const param_type& p, std::wstring* l); }; @@ -336,10 +309,10 @@ struct ParamTraits<SkBitmap> { template <> struct ParamTraits<std::string> { typedef std::string param_type; - static void Write(Message* m, const param_type& p) { + static void Write(IPC::Message* m, const param_type& p) { m->WriteString(p); } - static bool Read(const Message* m, void** iter, param_type* r) { + static bool Read(const IPC::Message* m, void** iter, param_type* r) { return m->ReadString(iter, r); } static void Log(const param_type& p, std::wstring* l) { @@ -350,7 +323,7 @@ struct ParamTraits<std::string> { template <> struct ParamTraits<std::vector<unsigned char> > { typedef std::vector<unsigned char> param_type; - static void Write(Message* m, const param_type& p) { + static void Write(IPC::Message* m, const param_type& p) { if (p.size() == 0) { m->WriteData(NULL, 0); } else { @@ -358,7 +331,7 @@ struct ParamTraits<std::vector<unsigned char> > { static_cast<int>(p.size())); } } - static bool Read(const Message* m, void** iter, param_type* r) { + static bool Read(const IPC::Message* m, void** iter, param_type* r) { const char *data; int data_size = 0; if (!m->ReadData(iter, &data, &data_size) || data_size < 0) @@ -377,14 +350,14 @@ struct ParamTraits<std::vector<unsigned char> > { template <> struct ParamTraits<std::vector<char> > { typedef std::vector<char> param_type; - static void Write(Message* m, const param_type& p) { + static void Write(IPC::Message* m, const param_type& p) { if (p.size() == 0) { m->WriteData(NULL, 0); } else { m->WriteData(&p.front(), static_cast<int>(p.size())); } } - static bool Read(const Message* m, void** iter, param_type* r) { + static bool Read(const IPC::Message* m, void** iter, param_type* r) { const char *data; int data_size = 0; if (!m->ReadData(iter, &data, &data_size) || data_size < 0) @@ -403,12 +376,12 @@ struct ParamTraits<std::vector<char> > { template <class P> struct ParamTraits<std::vector<P> > { typedef std::vector<P> param_type; - static void Write(Message* m, const param_type& p) { + static void Write(IPC::Message* m, const param_type& p) { WriteParam(m, static_cast<int>(p.size())); for (size_t i = 0; i < p.size(); i++) WriteParam(m, p[i]); } - static bool Read(const Message* m, void** iter, param_type* r) { + static bool Read(const IPC::Message* m, void** iter, param_type* r) { int size; if (!m->ReadLength(iter, &size)) return false; @@ -442,7 +415,7 @@ struct ParamTraits<std::vector<P> > { template <class K, class V> struct ParamTraits<std::map<K, V> > { typedef std::map<K, V> param_type; - static void Write(Message* m, const param_type& p) { + static void Write(IPC::Message* m, const param_type& p) { WriteParam(m, static_cast<int>(p.size())); typename param_type::const_iterator iter; for (iter = p.begin(); iter != p.end(); ++iter) { @@ -450,7 +423,7 @@ struct ParamTraits<std::map<K, V> > { WriteParam(m, iter->second); } } - static bool Read(const Message* m, void** iter, param_type* r) { + static bool Read(const IPC::Message* m, void** iter, param_type* r) { int size; if (!ReadParam(m, iter, &size) || size < 0) return false; @@ -472,10 +445,10 @@ struct ParamTraits<std::map<K, V> > { template <> struct ParamTraits<std::wstring> { typedef std::wstring param_type; - static void Write(Message* m, const param_type& p) { + static void Write(IPC::Message* m, const param_type& p) { m->WriteWString(p); } - static bool Read(const Message* m, void** iter, param_type* r) { + static bool Read(const IPC::Message* m, void** iter, param_type* r) { return m->ReadWString(iter, r); } static void Log(const param_type& p, std::wstring* l) { @@ -486,8 +459,8 @@ struct ParamTraits<std::wstring> { template <> struct ParamTraits<GURL> { typedef GURL param_type; - static void Write(Message* m, const param_type& p); - static bool Read(const Message* m, void** iter, param_type* p); + static void Write(IPC::Message* m, const param_type& p); + static bool Read(const IPC::Message* m, void** iter, param_type* p); static void Log(const param_type& p, std::wstring* l); }; @@ -496,10 +469,10 @@ struct ParamTraits<GURL> { template <> struct ParamTraits<HANDLE> { typedef HANDLE param_type; - static void Write(Message* m, const param_type& p) { + static void Write(IPC::Message* m, const param_type& p) { m->WriteIntPtr(reinterpret_cast<intptr_t>(p)); } - static bool Read(const Message* m, void** iter, param_type* r) { + static bool Read(const IPC::Message* m, void** iter, param_type* r) { DCHECK_EQ(sizeof(param_type), sizeof(intptr_t)); return m->ReadIntPtr(iter, reinterpret_cast<intptr_t*>(r)); } @@ -511,10 +484,10 @@ struct ParamTraits<HANDLE> { template <> struct ParamTraits<HCURSOR> { typedef HCURSOR param_type; - static void Write(Message* m, const param_type& p) { + static void Write(IPC::Message* m, const param_type& p) { m->WriteIntPtr(reinterpret_cast<intptr_t>(p)); } - static bool Read(const Message* m, void** iter, param_type* r) { + static bool Read(const IPC::Message* m, void** iter, param_type* r) { DCHECK_EQ(sizeof(param_type), sizeof(intptr_t)); return m->ReadIntPtr(iter, reinterpret_cast<intptr_t*>(r)); } @@ -526,10 +499,10 @@ struct ParamTraits<HCURSOR> { template <> struct ParamTraits<HWND> { typedef HWND param_type; - static void Write(Message* m, const param_type& p) { + static void Write(IPC::Message* m, const param_type& p) { m->WriteIntPtr(reinterpret_cast<intptr_t>(p)); } - static bool Read(const Message* m, void** iter, param_type* r) { + static bool Read(const IPC::Message* m, void** iter, param_type* r) { DCHECK_EQ(sizeof(param_type), sizeof(intptr_t)); return m->ReadIntPtr(iter, reinterpret_cast<intptr_t*>(r)); } @@ -541,7 +514,7 @@ struct ParamTraits<HWND> { template <> struct ParamTraits<HRGN> { typedef HRGN param_type; - static void Write(Message* m, const param_type& p) { + static void Write(IPC::Message* m, const param_type& p) { int data_size = GetRegionData(p, 0, NULL); if (data_size) { char* bytes = new char[data_size]; @@ -552,7 +525,7 @@ struct ParamTraits<HRGN> { m->WriteData(NULL, 0); } } - static bool Read(const Message* m, void** iter, param_type* r) { + static bool Read(const IPC::Message* m, void** iter, param_type* r) { bool res = FALSE; const char *data; int data_size = 0; @@ -574,10 +547,10 @@ struct ParamTraits<HRGN> { template <> struct ParamTraits<HACCEL> { typedef HACCEL param_type; - static void Write(Message* m, const param_type& p) { + static void Write(IPC::Message* m, const param_type& p) { m->WriteIntPtr(reinterpret_cast<intptr_t>(p)); } - static bool Read(const Message* m, void** iter, param_type* r) { + static bool Read(const IPC::Message* m, void** iter, param_type* r) { DCHECK_EQ(sizeof(param_type), sizeof(intptr_t)); return m->ReadIntPtr(iter, reinterpret_cast<intptr_t*>(r)); } @@ -586,11 +559,11 @@ struct ParamTraits<HACCEL> { template <> struct ParamTraits<POINT> { typedef POINT param_type; - static void Write(Message* m, const param_type& p) { + static void Write(IPC::Message* m, const param_type& p) { m->WriteInt(p.x); m->WriteInt(p.y); } - static bool Read(const Message* m, void** iter, param_type* r) { + static bool Read(const IPC::Message* m, void** iter, param_type* r) { int x, y; if (!m->ReadInt(iter, &x) || !m->ReadInt(iter, &y)) return false; @@ -607,10 +580,10 @@ struct ParamTraits<POINT> { template <> struct ParamTraits<FilePath> { typedef FilePath param_type; - static void Write(Message* m, const param_type& p) { + static void Write(IPC::Message* m, const param_type& p) { ParamTraits<FilePath::StringType>::Write(m, p.value()); } - static bool Read(const Message* m, void** iter, param_type* r) { + static bool Read(const IPC::Message* m, void** iter, param_type* r) { FilePath::StringType value; if (!ParamTraits<FilePath::StringType>::Read(m, iter, &value)) return false; @@ -625,44 +598,44 @@ struct ParamTraits<FilePath> { template <> struct ParamTraits<gfx::Point> { typedef gfx::Point param_type; - static void Write(Message* m, const param_type& p); - static bool Read(const Message* m, void** iter, param_type* r); + static void Write(IPC::Message* m, const param_type& p); + static bool Read(const IPC::Message* m, void** iter, param_type* r); static void Log(const param_type& p, std::wstring* l); }; template <> struct ParamTraits<gfx::Rect> { typedef gfx::Rect param_type; - static void Write(Message* m, const param_type& p); - static bool Read(const Message* m, void** iter, param_type* r); + static void Write(IPC::Message* m, const param_type& p); + static bool Read(const IPC::Message* m, void** iter, param_type* r); static void Log(const param_type& p, std::wstring* l); }; template <> struct ParamTraits<gfx::Size> { typedef gfx::Size param_type; - static void Write(Message* m, const param_type& p); - static bool Read(const Message* m, void** iter, param_type* r); + static void Write(IPC::Message* m, const param_type& p); + static bool Read(const IPC::Message* m, void** iter, param_type* r); static void Log(const param_type& p, std::wstring* l); }; template<> struct ParamTraits<ThumbnailScore> { typedef ThumbnailScore param_type; - static void Write(Message* m, const param_type& p) { - IPC::ParamTraits<double>::Write(m, p.boring_score); - IPC::ParamTraits<bool>::Write(m, p.good_clipping); - IPC::ParamTraits<bool>::Write(m, p.at_top); - IPC::ParamTraits<base::Time>::Write(m, p.time_at_snapshot); + static void Write(IPC::Message* m, const param_type& p) { + ParamTraits<double>::Write(m, p.boring_score); + ParamTraits<bool>::Write(m, p.good_clipping); + ParamTraits<bool>::Write(m, p.at_top); + ParamTraits<base::Time>::Write(m, p.time_at_snapshot); } - static bool Read(const Message* m, void** iter, param_type* r) { + static bool Read(const IPC::Message* m, void** iter, param_type* r) { double boring_score; bool good_clipping, at_top; base::Time time_at_snapshot; - if (!IPC::ParamTraits<double>::Read(m, iter, &boring_score) || - !IPC::ParamTraits<bool>::Read(m, iter, &good_clipping) || - !IPC::ParamTraits<bool>::Read(m, iter, &at_top) || - !IPC::ParamTraits<base::Time>::Read(m, iter, &time_at_snapshot)) + if (!ParamTraits<double>::Read(m, iter, &boring_score) || + !ParamTraits<bool>::Read(m, iter, &good_clipping) || + !ParamTraits<bool>::Read(m, iter, &at_top) || + !ParamTraits<base::Time>::Read(m, iter, &time_at_snapshot)) return false; r->boring_score = boring_score; @@ -680,10 +653,10 @@ struct ParamTraits<ThumbnailScore> { template <> struct ParamTraits<WindowOpenDisposition> { typedef WindowOpenDisposition param_type; - static void Write(Message* m, const param_type& p) { + static void Write(IPC::Message* m, const param_type& p) { m->WriteInt(p); } - static bool Read(const Message* m, void** iter, param_type* r) { + static bool Read(const IPC::Message* m, void** iter, param_type* r) { int temp; bool res = m->ReadInt(iter, &temp); *r = static_cast<WindowOpenDisposition>(temp); @@ -697,10 +670,10 @@ struct ParamTraits<WindowOpenDisposition> { template <> struct ParamTraits<ConsoleMessageLevel> { typedef ConsoleMessageLevel param_type; - static void Write(Message* m, const param_type& p) { + static void Write(IPC::Message* m, const param_type& p) { m->WriteInt(p); } - static bool Read(const Message* m, void** iter, param_type* r) { + static bool Read(const IPC::Message* m, void** iter, param_type* r) { int temp; bool res = m->ReadInt(iter, &temp); *r = static_cast<ConsoleMessageLevel>(temp); @@ -714,13 +687,13 @@ struct ParamTraits<ConsoleMessageLevel> { template <> struct ParamTraits<CacheManager::ResourceTypeStat> { typedef CacheManager::ResourceTypeStat param_type; - static void Write(Message* m, const param_type& p) { + static void Write(IPC::Message* m, const param_type& p) { WriteParam(m, p.count); WriteParam(m, p.size); WriteParam(m, p.live_size); WriteParam(m, p.decoded_size); } - static bool Read(const Message* m, void** iter, param_type* r) { + static bool Read(const IPC::Message* m, void** iter, param_type* r) { bool result = ReadParam(m, iter, &r->count) && ReadParam(m, iter, &r->size) && @@ -737,14 +710,14 @@ struct ParamTraits<CacheManager::ResourceTypeStat> { template <> struct ParamTraits<CacheManager::ResourceTypeStats> { typedef CacheManager::ResourceTypeStats param_type; - static void Write(Message* m, const param_type& p) { + static void Write(IPC::Message* m, const param_type& p) { WriteParam(m, p.images); WriteParam(m, p.css_stylesheets); WriteParam(m, p.scripts); WriteParam(m, p.xsl_stylesheets); WriteParam(m, p.fonts); } - static bool Read(const Message* m, void** iter, param_type* r) { + static bool Read(const IPC::Message* m, void** iter, param_type* r) { bool result = ReadParam(m, iter, &r->images) && ReadParam(m, iter, &r->css_stylesheets) && @@ -768,10 +741,10 @@ struct ParamTraits<CacheManager::ResourceTypeStats> { template <> struct ParamTraits<XFORM> { typedef XFORM param_type; - static void Write(Message* m, const param_type& p) { + static void Write(IPC::Message* m, const param_type& p) { m->WriteData(reinterpret_cast<const char*>(&p), sizeof(XFORM)); } - static bool Read(const Message* m, void** iter, param_type* r) { + static bool Read(const IPC::Message* m, void** iter, param_type* r) { const char *data; int data_size = 0; bool result = m->ReadData(iter, &data, &data_size); @@ -793,10 +766,10 @@ struct ParamTraits<XFORM> { template <> struct ParamTraits<WebCursor> { typedef WebCursor param_type; - static void Write(Message* m, const param_type& p) { + static void Write(IPC::Message* m, const param_type& p) { p.Serialize(m); } - static bool Read(const Message* m, void** iter, param_type* r) { + static bool Read(const IPC::Message* m, void** iter, param_type* r) { return r->Deserialize(m, iter); } static void Log(const param_type& p, std::wstring* l) { @@ -804,6 +777,8 @@ struct ParamTraits<WebCursor> { } }; +namespace IPC { + struct LogData { std::wstring channel; uint16 type; @@ -816,10 +791,12 @@ struct LogData { std::wstring params; }; +} + template <> -struct ParamTraits<LogData> { - typedef LogData param_type; - static void Write(Message* m, const param_type& p) { +struct ParamTraits<IPC::LogData> { + typedef IPC::LogData param_type; + static void Write(IPC::Message* m, const param_type& p) { WriteParam(m, p.channel); WriteParam(m, static_cast<int>(p.type)); WriteParam(m, p.flags); @@ -828,7 +805,7 @@ struct ParamTraits<LogData> { WriteParam(m, p.dispatch); WriteParam(m, p.params); } - static bool Read(const Message* m, void** iter, param_type* r) { + static bool Read(const IPC::Message* m, void** iter, param_type* r) { int type; bool result = ReadParam(m, iter, &r->channel) && @@ -849,9 +826,9 @@ struct ParamTraits<LogData> { template <> struct ParamTraits<Tuple0> { typedef Tuple0 param_type; - static void Write(Message* m, const param_type& p) { + static void Write(IPC::Message* m, const param_type& p) { } - static bool Read(const Message* m, void** iter, param_type* r) { + static bool Read(const IPC::Message* m, void** iter, param_type* r) { return true; } static void Log(const param_type& p, std::wstring* l) { @@ -861,10 +838,10 @@ struct ParamTraits<Tuple0> { template <class A> struct ParamTraits< Tuple1<A> > { typedef Tuple1<A> param_type; - static void Write(Message* m, const param_type& p) { + static void Write(IPC::Message* m, const param_type& p) { WriteParam(m, p.a); } - static bool Read(const Message* m, void** iter, param_type* r) { + static bool Read(const IPC::Message* m, void** iter, param_type* r) { return ReadParam(m, iter, &r->a); } static void Log(const param_type& p, std::wstring* l) { @@ -875,11 +852,11 @@ struct ParamTraits< Tuple1<A> > { template <class A, class B> struct ParamTraits< Tuple2<A, B> > { typedef Tuple2<A, B> param_type; - static void Write(Message* m, const param_type& p) { + static void Write(IPC::Message* m, const param_type& p) { WriteParam(m, p.a); WriteParam(m, p.b); } - static bool Read(const Message* m, void** iter, param_type* r) { + static bool Read(const IPC::Message* m, void** iter, param_type* r) { return (ReadParam(m, iter, &r->a) && ReadParam(m, iter, &r->b)); } @@ -893,12 +870,12 @@ struct ParamTraits< Tuple2<A, B> > { template <class A, class B, class C> struct ParamTraits< Tuple3<A, B, C> > { typedef Tuple3<A, B, C> param_type; - static void Write(Message* m, const param_type& p) { + static void Write(IPC::Message* m, const param_type& p) { WriteParam(m, p.a); WriteParam(m, p.b); WriteParam(m, p.c); } - static bool Read(const Message* m, void** iter, param_type* r) { + static bool Read(const IPC::Message* m, void** iter, param_type* r) { return (ReadParam(m, iter, &r->a) && ReadParam(m, iter, &r->b) && ReadParam(m, iter, &r->c)); @@ -915,13 +892,13 @@ struct ParamTraits< Tuple3<A, B, C> > { template <class A, class B, class C, class D> struct ParamTraits< Tuple4<A, B, C, D> > { typedef Tuple4<A, B, C, D> param_type; - static void Write(Message* m, const param_type& p) { + static void Write(IPC::Message* m, const param_type& p) { WriteParam(m, p.a); WriteParam(m, p.b); WriteParam(m, p.c); WriteParam(m, p.d); } - static bool Read(const Message* m, void** iter, param_type* r) { + static bool Read(const IPC::Message* m, void** iter, param_type* r) { return (ReadParam(m, iter, &r->a) && ReadParam(m, iter, &r->b) && ReadParam(m, iter, &r->c) && @@ -941,14 +918,14 @@ struct ParamTraits< Tuple4<A, B, C, D> > { template <class A, class B, class C, class D, class E> struct ParamTraits< Tuple5<A, B, C, D, E> > { typedef Tuple5<A, B, C, D, E> param_type; - static void Write(Message* m, const param_type& p) { + static void Write(IPC::Message* m, const param_type& p) { WriteParam(m, p.a); WriteParam(m, p.b); WriteParam(m, p.c); WriteParam(m, p.d); WriteParam(m, p.e); } - static bool Read(const Message* m, void** iter, param_type* r) { + static bool Read(const IPC::Message* m, void** iter, param_type* r) { return (ReadParam(m, iter, &r->a) && ReadParam(m, iter, &r->b) && ReadParam(m, iter, &r->c) && @@ -971,7 +948,7 @@ struct ParamTraits< Tuple5<A, B, C, D, E> > { template <class A, class B, class C, class D, class E, class F> struct ParamTraits< Tuple6<A, B, C, D, E, F> > { typedef Tuple6<A, B, C, D, E, F> param_type; - static void Write(Message* m, const param_type& p) { + static void Write(IPC::Message* m, const param_type& p) { WriteParam(m, p.a); WriteParam(m, p.b); WriteParam(m, p.c); @@ -979,7 +956,7 @@ struct ParamTraits< Tuple6<A, B, C, D, E, F> > { WriteParam(m, p.e); WriteParam(m, p.f); } - static bool Read(const Message* m, void** iter, param_type* r) { + static bool Read(const IPC::Message* m, void** iter, param_type* r) { return (ReadParam(m, iter, &r->a) && ReadParam(m, iter, &r->b) && ReadParam(m, iter, &r->c) && @@ -1005,11 +982,77 @@ struct ParamTraits< Tuple6<A, B, C, D, E, F> > { template <> struct ParamTraits<webkit_glue::WebApplicationInfo> { typedef webkit_glue::WebApplicationInfo param_type; - static void Write(Message* m, const param_type& p); - static bool Read(const Message* m, void** iter, param_type* r); + static void Write(IPC::Message* m, const param_type& p); + static bool Read(const IPC::Message* m, void** iter, param_type* r); static void Log(const param_type& p, std::wstring* l); }; +// Traits for ViewMsg_FindInPageMsg_Request structure to pack/unpack. +template <> +struct ParamTraits<FindInPageRequest> { + typedef FindInPageRequest param_type; + static void Write(IPC::Message* m, const param_type& p) { + WriteParam(m, p.request_id); + WriteParam(m, p.search_string); + WriteParam(m, p.forward); + WriteParam(m, p.match_case); + WriteParam(m, p.find_next); + } + static bool Read(const IPC::Message* m, void** iter, param_type* p) { + return + ReadParam(m, iter, &p->request_id) && + ReadParam(m, iter, &p->search_string) && + ReadParam(m, iter, &p->forward) && + ReadParam(m, iter, &p->match_case) && + ReadParam(m, iter, &p->find_next); + } + static void Log(const param_type& p, std::wstring* l) { + l->append(L"<FindInPageRequest>"); + } +}; + +namespace IPC { + +//----------------------------------------------------------------------------- +// An iterator class for reading the fields contained within a Message. + +class MessageIterator { + public: + explicit MessageIterator(const Message& m) : msg_(m), iter_(NULL) { + } + int NextInt() const { + int val; + if (!msg_.ReadInt(&iter_, &val)) + NOTREACHED(); + return val; + } + intptr_t NextIntPtr() const { + intptr_t val; + if (!msg_.ReadIntPtr(&iter_, &val)) + NOTREACHED(); + return val; + } + const std::string NextString() const { + std::string val; + if (!msg_.ReadString(&iter_, &val)) + NOTREACHED(); + return val; + } + const std::wstring NextWString() const { + std::wstring val; + if (!msg_.ReadWString(&iter_, &val)) + NOTREACHED(); + return val; + } + const void NextData(const char** data, int* length) const { + if (!msg_.ReadData(&iter_, data, length)) { + NOTREACHED(); + } + } + private: + const Message& msg_; + mutable void* iter_; +}; //----------------------------------------------------------------------------- // Generic message subclasses @@ -1025,7 +1068,7 @@ class MessageWithTuple : public Message { WriteParam(this, p); } - static bool Read(const Message* msg, Param* p) { + static bool Read(const IPC::Message* msg, Param* p) { void* iter = NULL; bool rv = ReadParam(msg, &iter, p); DCHECK(rv) << "Error deserializing message " << msg->type(); @@ -1034,7 +1077,7 @@ class MessageWithTuple : public Message { // Generic dispatcher. Should cover most cases. template<class T, class Method> - static bool Dispatch(const Message* msg, T* obj, Method func) { + static bool Dispatch(const IPC::Message* msg, T* obj, Method func) { Param p; if (Read(msg, &p)) { DispatchToMethod(obj, func, p); @@ -1047,7 +1090,7 @@ class MessageWithTuple : public Message { // needs the message as well. They assume that "Param" is a type of Tuple // (except the one arg case, as there is no Tuple1). template<class T, typename TA> - static bool Dispatch(const Message* msg, T* obj, + static bool Dispatch(const IPC::Message* msg, T* obj, void (T::*func)(const Message&, TA)) { Param p; if (Read(msg, &p)) { @@ -1058,7 +1101,7 @@ class MessageWithTuple : public Message { } template<class T, typename TA, typename TB> - static bool Dispatch(const Message* msg, T* obj, + static bool Dispatch(const IPC::Message* msg, T* obj, void (T::*func)(const Message&, TA, TB)) { Param p; if (Read(msg, &p)) { @@ -1069,7 +1112,7 @@ class MessageWithTuple : public Message { } template<class T, typename TA, typename TB, typename TC> - static bool Dispatch(const Message* msg, T* obj, + static bool Dispatch(const IPC::Message* msg, T* obj, void (T::*func)(const Message&, TA, TB, TC)) { Param p; if (Read(msg, &p)) { @@ -1080,7 +1123,7 @@ class MessageWithTuple : public Message { } template<class T, typename TA, typename TB, typename TC, typename TD> - static bool Dispatch(const Message* msg, T* obj, + static bool Dispatch(const IPC::Message* msg, T* obj, void (T::*func)(const Message&, TA, TB, TC, TD)) { Param p; if (Read(msg, &p)) { @@ -1092,7 +1135,7 @@ class MessageWithTuple : public Message { template<class T, typename TA, typename TB, typename TC, typename TD, typename TE> - static bool Dispatch(const Message* msg, T* obj, + static bool Dispatch(const IPC::Message* msg, T* obj, void (T::*func)(const Message&, TA, TB, TC, TD, TE)) { Param p; if (Read(msg, &p)) { @@ -1102,11 +1145,59 @@ class MessageWithTuple : public Message { return false; } - static void Log(const Message* msg, std::wstring* l) { + static void Log(const IPC::Message* msg, std::wstring* l) { Param p; if (Read(msg, &p)) LogParam(p, l); } + + // Functions used to do manual unpacking. Only used by the automation code, + // these should go away once that code uses SyncChannel. + template<typename TA, typename TB> + static bool Read(const IPC::Message* msg, TA* a, TB* b) { + ParamType params; + if (!Read(msg, ¶ms)) + return false; + *a = params.a; + *b = params.b; + return true; + } + + template<typename TA, typename TB, typename TC> + static bool Read(const IPC::Message* msg, TA* a, TB* b, TC* c) { + ParamType params; + if (!Read(msg, ¶ms)) + return false; + *a = params.a; + *b = params.b; + *c = params.c; + return true; + } + + template<typename TA, typename TB, typename TC, typename TD> + static bool Read(const IPC::Message* msg, TA* a, TB* b, TC* c, TD* d) { + ParamType params; + if (!Read(msg, ¶ms)) + return false; + *a = params.a; + *b = params.b; + *c = params.c; + *d = params.d; + return true; + } + + template<typename TA, typename TB, typename TC, typename TD, typename TE> + static bool Read(const IPC::Message* msg, TA* a, TB* b, TC* c, TD* d, TE* e) { + ParamType params; + if (!Read(msg, ¶ms)) + return false; + *a = params.a; + *b = params.b; + *c = params.c; + *d = params.d; + *e = params.e; + return true; + } }; // This class assumes that its template argument is a RefTuple (a Tuple with @@ -1138,7 +1229,7 @@ class MessageWithReply : public SyncMessage { WriteParam(this, send); } - static void Log(const Message* msg, std::wstring* l) { + static void Log(const IPC::Message* msg, std::wstring* l) { if (msg->is_sync()) { SendParam p; void* iter = SyncMessage::GetDataIterator(msg); @@ -1163,7 +1254,7 @@ class MessageWithReply : public SyncMessage { } template<class T, class Method> - static bool Dispatch(const Message* msg, T* obj, Method func) { + static bool Dispatch(const IPC::Message* msg, T* obj, Method func) { SendParam send_params; void* iter = GetDataIterator(msg); Message* reply = GenerateReply(msg); @@ -1191,7 +1282,7 @@ class MessageWithReply : public SyncMessage { } template<class T, class Method> - static bool DispatchDelayReply(const Message* msg, T* obj, Method func) { + static bool DispatchDelayReply(const IPC::Message* msg, T* obj, Method func) { SendParam send_params; void* iter = GetDataIterator(msg); Message* reply = GenerateReply(msg); @@ -1252,32 +1343,6 @@ class MessageWithReply : public SyncMessage { } }; -// Traits for ViewMsg_FindInPageMsg_Request structure to pack/unpack. -template <> -struct ParamTraits<FindInPageRequest> { - typedef FindInPageRequest param_type; - static void Write(Message* m, const param_type& p) { - WriteParam(m, p.request_id); - WriteParam(m, p.search_string); - WriteParam(m, p.forward); - WriteParam(m, p.match_case); - WriteParam(m, p.find_next); - } - static bool Read(const Message* m, void** iter, param_type* p) { - return - ReadParam(m, iter, &p->request_id) && - ReadParam(m, iter, &p->search_string) && - ReadParam(m, iter, &p->forward) && - ReadParam(m, iter, &p->match_case) && - ReadParam(m, iter, &p->find_next); - } - static void Log(const param_type& p, std::wstring* l) { - l->append(L"<FindInPageRequest>"); - } -}; - -//----------------------------------------------------------------------------- - } // namespace IPC #endif // CHROME_COMMON_IPC_MESSAGE_UTILS_H_ |