[email protected] | 225c8f5 | 2010-02-05 22:23:20 | [diff] [blame] | 1 | // Copyright (c) 2010 The Chromium Authors. All rights reserved. |
license.bot | bf09a50 | 2008-08-24 00:55:55 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 4 | |
[email protected] | 946d1b2 | 2009-07-22 23:57:21 | [diff] [blame] | 5 | #ifndef IPC_IPC_MESSAGE_UTILS_H_ |
| 6 | #define IPC_IPC_MESSAGE_UTILS_H_ |
[email protected] | 32b76ef | 2010-07-26 23:08:24 | [diff] [blame] | 7 | #pragma once |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 8 | |
[email protected] | 379e7a5 | 2010-03-09 00:38:41 | [diff] [blame] | 9 | #include <algorithm> |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 10 | #include <string> |
| 11 | #include <vector> |
| 12 | #include <map> |
[email protected] | 96da696 | 2010-05-13 19:10:34 | [diff] [blame] | 13 | #include <set> |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 14 | |
[email protected] | dce5df5 | 2009-06-29 17:58:25 | [diff] [blame] | 15 | #include "base/format_macros.h" |
[email protected] | eb47a13 | 2009-03-04 00:39:56 | [diff] [blame] | 16 | #include "base/string16.h" |
[email protected] | dce5df5 | 2009-06-29 17:58:25 | [diff] [blame] | 17 | #include "base/string_util.h" |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 18 | #include "base/tuple.h" |
[email protected] | 939856a | 2010-08-24 20:29:02 | [diff] [blame] | 19 | #include "ipc/ipc_param_traits.h" |
[email protected] | 0cfe5dae | 2010-08-17 00:24:54 | [diff] [blame] | 20 | #include "ipc/ipc_sync_message.h" |
[email protected] | 55b4e21 | 2010-08-13 20:43:58 | [diff] [blame] | 21 | |
[email protected] | 7a4de7a6 | 2010-08-17 18:38:24 | [diff] [blame] | 22 | #if defined(COMPILER_GCC) |
| 23 | // GCC "helpfully" tries to inline template methods in release mode. Except we |
| 24 | // want the majority of the template junk being expanded once in the |
| 25 | // implementation file (and only provide the definitions in |
| 26 | // ipc_message_utils_impl.h in those files) and exported, instead of expanded |
| 27 | // at every call site. Special note: GCC happily accepts the attribute before |
| 28 | // the method declaration, but only acts on it if it is after. |
[email protected] | efafbbd | 2010-08-19 20:23:25 | [diff] [blame] | 29 | #if (__GNUC__ * 10000 + __GNUC_MINOR__ * 100) >= 40500 |
| 30 | // Starting in gcc 4.5, the noinline no longer implies the concept covered by |
| 31 | // the introduced noclone attribute, which will create specialized versions of |
| 32 | // functions/methods when certain types are constant. |
| 33 | // www.gnu.org/software/gcc/gcc-4.5/changes.html |
| 34 | #define IPC_MSG_NOINLINE __attribute__((noinline, noclone)); |
| 35 | #else |
[email protected] | 7a4de7a6 | 2010-08-17 18:38:24 | [diff] [blame] | 36 | #define IPC_MSG_NOINLINE __attribute__((noinline)); |
[email protected] | efafbbd | 2010-08-19 20:23:25 | [diff] [blame] | 37 | #endif |
[email protected] | 7a4de7a6 | 2010-08-17 18:38:24 | [diff] [blame] | 38 | #elif defined(COMPILER_MSVC) |
| 39 | // MSVC++ doesn't do this. |
| 40 | #define IPC_MSG_NOINLINE |
| 41 | #else |
| 42 | #error "Please add the noinline property for your new compiler here." |
| 43 | #endif |
| 44 | |
[email protected] | f91cb99 | 2009-02-04 20:10:12 | [diff] [blame] | 45 | // Used by IPC_BEGIN_MESSAGES so that each message class starts from a unique |
| 46 | // base. Messages have unique IDs across channels in order for the IPC logging |
| 47 | // code to figure out the message class from its ID. |
| 48 | enum IPCMessageStart { |
| 49 | // By using a start value of 0 for automation messages, we keep backward |
| 50 | // compatibility with old builds. |
| 51 | AutomationMsgStart = 0, |
| 52 | ViewMsgStart, |
| 53 | ViewHostMsgStart, |
| 54 | PluginProcessMsgStart, |
| 55 | PluginProcessHostMsgStart, |
| 56 | PluginMsgStart, |
| 57 | PluginHostMsgStart, |
[email protected] | 96da696 | 2010-05-13 19:10:34 | [diff] [blame] | 58 | ProfileImportProcessMsgStart, |
| 59 | ProfileImportProcessHostMsgStart, |
[email protected] | f91cb99 | 2009-02-04 20:10:12 | [diff] [blame] | 60 | NPObjectMsgStart, |
| 61 | TestMsgStart, |
[email protected] | 503683f | 2009-02-26 09:13:01 | [diff] [blame] | 62 | DevToolsAgentMsgStart, |
| 63 | DevToolsClientMsgStart, |
[email protected] | eb47a13 | 2009-03-04 00:39:56 | [diff] [blame] | 64 | WorkerProcessMsgStart, |
| 65 | WorkerProcessHostMsgStart, |
| 66 | WorkerMsgStart, |
| 67 | WorkerHostMsgStart, |
[email protected] | d032f49 | 2009-09-29 00:33:46 | [diff] [blame] | 68 | NaClProcessMsgStart, |
[email protected] | 246a7045 | 2010-03-05 21:53:50 | [diff] [blame] | 69 | GpuCommandBufferMsgStart, |
[email protected] | 60f1439 | 2009-12-15 20:46:32 | [diff] [blame] | 70 | UtilityMsgStart, |
| 71 | UtilityHostMsgStart, |
[email protected] | c0fc094 | 2010-01-13 00:55:37 | [diff] [blame] | 72 | GpuMsgStart, |
| 73 | GpuHostMsgStart, |
[email protected] | 246a7045 | 2010-03-05 21:53:50 | [diff] [blame] | 74 | GpuChannelMsgStart, |
[email protected] | ee68378a | 2010-08-10 01:05:41 | [diff] [blame] | 75 | GpuVideoDecoderHostMsgStart, |
| 76 | GpuVideoDecoderMsgStart, |
[email protected] | 38fe196 | 2010-07-31 07:57:00 | [diff] [blame] | 77 | ServiceMsgStart, |
| 78 | ServiceHostMsgStart, |
[email protected] | 709a847e | 2010-11-10 01:16:11 | [diff] [blame] | 79 | PpapiMsgStart, |
| 80 | PpapiHostMsgStart, |
[email protected] | f91cb99 | 2009-02-04 20:10:12 | [diff] [blame] | 81 | // NOTE: When you add a new message class, also update |
| 82 | // IPCStatusView::IPCStatusView to ensure logging works. |
[email protected] | f91cb99 | 2009-02-04 20:10:12 | [diff] [blame] | 83 | LastMsgIndex |
| 84 | }; |
| 85 | |
[email protected] | 7a4de7a6 | 2010-08-17 18:38:24 | [diff] [blame] | 86 | class DictionaryValue; |
| 87 | class FilePath; |
| 88 | class ListValue; |
| 89 | class NullableString16; |
| 90 | |
| 91 | namespace base { |
| 92 | class Time; |
[email protected] | d84e48b | 2010-10-21 22:04:52 | [diff] [blame] | 93 | class TimeDelta; |
[email protected] | 7a4de7a6 | 2010-08-17 18:38:24 | [diff] [blame] | 94 | struct FileDescriptor; |
| 95 | } |
| 96 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 97 | namespace IPC { |
| 98 | |
[email protected] | 7a4de7a6 | 2010-08-17 18:38:24 | [diff] [blame] | 99 | struct ChannelHandle; |
| 100 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 101 | //----------------------------------------------------------------------------- |
| 102 | // An iterator class for reading the fields contained within a Message. |
| 103 | |
| 104 | class MessageIterator { |
| 105 | public: |
[email protected] | e1981f43 | 2008-08-12 15:22:13 | [diff] [blame] | 106 | explicit MessageIterator(const Message& m) : msg_(m), iter_(NULL) { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 107 | } |
| 108 | int NextInt() const { |
[email protected] | 9422b7dc | 2009-12-30 20:09:02 | [diff] [blame] | 109 | int val = -1; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 110 | if (!msg_.ReadInt(&iter_, &val)) |
| 111 | NOTREACHED(); |
| 112 | return val; |
| 113 | } |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 114 | const std::string NextString() const { |
| 115 | std::string val; |
| 116 | if (!msg_.ReadString(&iter_, &val)) |
| 117 | NOTREACHED(); |
| 118 | return val; |
| 119 | } |
| 120 | const std::wstring NextWString() const { |
| 121 | std::wstring val; |
| 122 | if (!msg_.ReadWString(&iter_, &val)) |
| 123 | NOTREACHED(); |
| 124 | return val; |
| 125 | } |
[email protected] | 225c8f5 | 2010-02-05 22:23:20 | [diff] [blame] | 126 | void NextData(const char** data, int* length) const { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 127 | if (!msg_.ReadData(&iter_, data, length)) { |
| 128 | NOTREACHED(); |
| 129 | } |
| 130 | } |
| 131 | private: |
| 132 | const Message& msg_; |
| 133 | mutable void* iter_; |
| 134 | }; |
| 135 | |
| 136 | //----------------------------------------------------------------------------- |
[email protected] | 7d5c3ac | 2009-02-04 08:58:19 | [diff] [blame] | 137 | // ParamTraits specializations, etc. |
| 138 | |
[email protected] | 7d5c3ac | 2009-02-04 08:58:19 | [diff] [blame] | 139 | template <class P> |
| 140 | static inline void WriteParam(Message* m, const P& p) { |
[email protected] | 7b291f9 | 2009-08-14 05:43:53 | [diff] [blame] | 141 | typedef typename SimilarTypeTraits<P>::Type Type; |
[email protected] | 46ce5b56 | 2010-06-16 18:39:53 | [diff] [blame] | 142 | ParamTraits<Type>::Write(m, static_cast<const Type& >(p)); |
[email protected] | 7d5c3ac | 2009-02-04 08:58:19 | [diff] [blame] | 143 | } |
| 144 | |
| 145 | template <class P> |
[email protected] | 1e86aa6 | 2009-04-24 21:22:33 | [diff] [blame] | 146 | static inline bool WARN_UNUSED_RESULT ReadParam(const Message* m, void** iter, |
| 147 | P* p) { |
[email protected] | 7b291f9 | 2009-08-14 05:43:53 | [diff] [blame] | 148 | typedef typename SimilarTypeTraits<P>::Type Type; |
| 149 | return ParamTraits<Type>::Read(m, iter, reinterpret_cast<Type* >(p)); |
[email protected] | 7d5c3ac | 2009-02-04 08:58:19 | [diff] [blame] | 150 | } |
| 151 | |
| 152 | template <class P> |
[email protected] | 252cad6 | 2010-08-18 18:33:57 | [diff] [blame] | 153 | static inline void LogParam(const P& p, std::string* l) { |
[email protected] | 7b291f9 | 2009-08-14 05:43:53 | [diff] [blame] | 154 | typedef typename SimilarTypeTraits<P>::Type Type; |
[email protected] | 46ce5b56 | 2010-06-16 18:39:53 | [diff] [blame] | 155 | ParamTraits<Type>::Log(static_cast<const Type& >(p), l); |
[email protected] | 7d5c3ac | 2009-02-04 08:58:19 | [diff] [blame] | 156 | } |
| 157 | |
| 158 | template <> |
| 159 | struct ParamTraits<bool> { |
| 160 | typedef bool param_type; |
| 161 | static void Write(Message* m, const param_type& p) { |
| 162 | m->WriteBool(p); |
| 163 | } |
| 164 | static bool Read(const Message* m, void** iter, param_type* r) { |
| 165 | return m->ReadBool(iter, r); |
| 166 | } |
[email protected] | 252cad6 | 2010-08-18 18:33:57 | [diff] [blame] | 167 | static void Log(const param_type& p, std::string* l) { |
| 168 | l->append(p ? "true" : "false"); |
[email protected] | 7d5c3ac | 2009-02-04 08:58:19 | [diff] [blame] | 169 | } |
| 170 | }; |
| 171 | |
| 172 | template <> |
| 173 | struct ParamTraits<int> { |
| 174 | typedef int param_type; |
| 175 | static void Write(Message* m, const param_type& p) { |
| 176 | m->WriteInt(p); |
| 177 | } |
| 178 | static bool Read(const Message* m, void** iter, param_type* r) { |
| 179 | return m->ReadInt(iter, r); |
| 180 | } |
[email protected] | 252cad6 | 2010-08-18 18:33:57 | [diff] [blame] | 181 | static void Log(const param_type& p, std::string* l); |
[email protected] | 7d5c3ac | 2009-02-04 08:58:19 | [diff] [blame] | 182 | }; |
| 183 | |
| 184 | template <> |
[email protected] | 63263f9 | 2009-07-28 19:35:08 | [diff] [blame] | 185 | struct ParamTraits<unsigned int> { |
| 186 | typedef unsigned int param_type; |
| 187 | static void Write(Message* m, const param_type& p) { |
| 188 | m->WriteInt(p); |
| 189 | } |
| 190 | static bool Read(const Message* m, void** iter, param_type* r) { |
| 191 | return m->ReadInt(iter, reinterpret_cast<int*>(r)); |
| 192 | } |
[email protected] | 252cad6 | 2010-08-18 18:33:57 | [diff] [blame] | 193 | static void Log(const param_type& p, std::string* l); |
[email protected] | 63263f9 | 2009-07-28 19:35:08 | [diff] [blame] | 194 | }; |
| 195 | |
| 196 | template <> |
[email protected] | 7d5c3ac | 2009-02-04 08:58:19 | [diff] [blame] | 197 | struct ParamTraits<long> { |
| 198 | typedef long param_type; |
| 199 | static void Write(Message* m, const param_type& p) { |
| 200 | m->WriteLong(p); |
| 201 | } |
| 202 | static bool Read(const Message* m, void** iter, param_type* r) { |
| 203 | return m->ReadLong(iter, r); |
| 204 | } |
[email protected] | 252cad6 | 2010-08-18 18:33:57 | [diff] [blame] | 205 | static void Log(const param_type& p, std::string* l); |
[email protected] | 7d5c3ac | 2009-02-04 08:58:19 | [diff] [blame] | 206 | }; |
| 207 | |
[email protected] | 140c303 | 2009-06-26 18:22:54 | [diff] [blame] | 208 | template <> |
| 209 | struct ParamTraits<unsigned long> { |
| 210 | typedef unsigned long param_type; |
| 211 | static void Write(Message* m, const param_type& p) { |
| 212 | m->WriteLong(p); |
| 213 | } |
| 214 | static bool Read(const Message* m, void** iter, param_type* r) { |
[email protected] | 63263f9 | 2009-07-28 19:35:08 | [diff] [blame] | 215 | return m->ReadLong(iter, reinterpret_cast<long*>(r)); |
[email protected] | 140c303 | 2009-06-26 18:22:54 | [diff] [blame] | 216 | } |
[email protected] | 252cad6 | 2010-08-18 18:33:57 | [diff] [blame] | 217 | static void Log(const param_type& p, std::string* l); |
[email protected] | 7d5c3ac | 2009-02-04 08:58:19 | [diff] [blame] | 218 | }; |
| 219 | |
| 220 | template <> |
[email protected] | 63263f9 | 2009-07-28 19:35:08 | [diff] [blame] | 221 | struct ParamTraits<long long> { |
| 222 | typedef long long param_type; |
[email protected] | 7d5c3ac | 2009-02-04 08:58:19 | [diff] [blame] | 223 | static void Write(Message* m, const param_type& p) { |
| 224 | m->WriteInt64(static_cast<int64>(p)); |
| 225 | } |
| 226 | static bool Read(const Message* m, void** iter, param_type* r) { |
| 227 | return m->ReadInt64(iter, reinterpret_cast<int64*>(r)); |
| 228 | } |
[email protected] | 252cad6 | 2010-08-18 18:33:57 | [diff] [blame] | 229 | static void Log(const param_type& p, std::string* l); |
[email protected] | 63263f9 | 2009-07-28 19:35:08 | [diff] [blame] | 230 | }; |
| 231 | |
| 232 | template <> |
| 233 | struct ParamTraits<unsigned long long> { |
| 234 | typedef unsigned long long param_type; |
| 235 | static void Write(Message* m, const param_type& p) { |
| 236 | m->WriteInt64(p); |
| 237 | } |
| 238 | static bool Read(const Message* m, void** iter, param_type* r) { |
| 239 | return m->ReadInt64(iter, reinterpret_cast<int64*>(r)); |
| 240 | } |
[email protected] | 252cad6 | 2010-08-18 18:33:57 | [diff] [blame] | 241 | static void Log(const param_type& p, std::string* l); |
[email protected] | 7d5c3ac | 2009-02-04 08:58:19 | [diff] [blame] | 242 | }; |
| 243 | |
[email protected] | 43a4020 | 2010-11-12 16:25:01 | [diff] [blame^] | 244 | template <> |
| 245 | struct ParamTraits<unsigned short> { |
| 246 | typedef unsigned short param_type; |
| 247 | static void Write(Message* m, const param_type& p); |
| 248 | static bool Read(const Message* m, void** iter, param_type* r); |
| 249 | static void Log(const param_type& p, std::string* l); |
| 250 | }; |
| 251 | |
[email protected] | 2019966 | 2010-06-17 03:29:26 | [diff] [blame] | 252 | // Note that the IPC layer doesn't sanitize NaNs and +/- INF values. Clients |
| 253 | // should be sure to check the sanity of these values after receiving them over |
| 254 | // IPC. |
| 255 | template <> |
| 256 | struct ParamTraits<float> { |
| 257 | typedef float param_type; |
| 258 | static void Write(Message* m, const param_type& p) { |
| 259 | m->WriteData(reinterpret_cast<const char*>(&p), sizeof(param_type)); |
| 260 | } |
| 261 | static bool Read(const Message* m, void** iter, param_type* r) { |
| 262 | const char *data; |
| 263 | int data_size; |
| 264 | if (!m->ReadData(iter, &data, &data_size) || |
| 265 | data_size != sizeof(param_type)) { |
| 266 | NOTREACHED(); |
| 267 | return false; |
| 268 | } |
| 269 | memcpy(r, data, sizeof(param_type)); |
| 270 | return true; |
| 271 | } |
[email protected] | 252cad6 | 2010-08-18 18:33:57 | [diff] [blame] | 272 | static void Log(const param_type& p, std::string* l) { |
| 273 | l->append(StringPrintf("%e", p)); |
[email protected] | 2019966 | 2010-06-17 03:29:26 | [diff] [blame] | 274 | } |
| 275 | }; |
| 276 | |
[email protected] | 7d5c3ac | 2009-02-04 08:58:19 | [diff] [blame] | 277 | template <> |
| 278 | struct ParamTraits<double> { |
| 279 | typedef double param_type; |
| 280 | static void Write(Message* m, const param_type& p) { |
| 281 | m->WriteData(reinterpret_cast<const char*>(&p), sizeof(param_type)); |
| 282 | } |
| 283 | static bool Read(const Message* m, void** iter, param_type* r) { |
| 284 | const char *data; |
[email protected] | 2019966 | 2010-06-17 03:29:26 | [diff] [blame] | 285 | int data_size; |
| 286 | if (!m->ReadData(iter, &data, &data_size) || |
| 287 | data_size != sizeof(param_type)) { |
[email protected] | 7d5c3ac | 2009-02-04 08:58:19 | [diff] [blame] | 288 | NOTREACHED(); |
[email protected] | 2019966 | 2010-06-17 03:29:26 | [diff] [blame] | 289 | return false; |
[email protected] | 7d5c3ac | 2009-02-04 08:58:19 | [diff] [blame] | 290 | } |
[email protected] | 2019966 | 2010-06-17 03:29:26 | [diff] [blame] | 291 | memcpy(r, data, sizeof(param_type)); |
| 292 | return true; |
[email protected] | 7d5c3ac | 2009-02-04 08:58:19 | [diff] [blame] | 293 | } |
[email protected] | 252cad6 | 2010-08-18 18:33:57 | [diff] [blame] | 294 | static void Log(const param_type& p, std::string* l) { |
| 295 | l->append(StringPrintf("%e", p)); |
[email protected] | 7d5c3ac | 2009-02-04 08:58:19 | [diff] [blame] | 296 | } |
| 297 | }; |
| 298 | |
| 299 | template <> |
[email protected] | 7d5c3ac | 2009-02-04 08:58:19 | [diff] [blame] | 300 | struct ParamTraits<base::Time> { |
| 301 | typedef base::Time param_type; |
[email protected] | 7a4de7a6 | 2010-08-17 18:38:24 | [diff] [blame] | 302 | static void Write(Message* m, const param_type& p); |
| 303 | static bool Read(const Message* m, void** iter, param_type* r); |
[email protected] | 252cad6 | 2010-08-18 18:33:57 | [diff] [blame] | 304 | static void Log(const param_type& p, std::string* l); |
[email protected] | 7d5c3ac | 2009-02-04 08:58:19 | [diff] [blame] | 305 | }; |
| 306 | |
[email protected] | d84e48b | 2010-10-21 22:04:52 | [diff] [blame] | 307 | template <> |
| 308 | struct ParamTraits<base::TimeDelta> { |
| 309 | typedef base::TimeDelta param_type; |
| 310 | static void Write(Message* m, const param_type& p); |
| 311 | static bool Read(const Message* m, void** iter, param_type* r); |
| 312 | static void Log(const param_type& p, std::string* l); |
| 313 | }; |
| 314 | |
[email protected] | 7d5c3ac | 2009-02-04 08:58:19 | [diff] [blame] | 315 | #if defined(OS_WIN) |
| 316 | template <> |
| 317 | struct ParamTraits<LOGFONT> { |
| 318 | typedef LOGFONT param_type; |
| 319 | static void Write(Message* m, const param_type& p) { |
| 320 | m->WriteData(reinterpret_cast<const char*>(&p), sizeof(LOGFONT)); |
| 321 | } |
| 322 | static bool Read(const Message* m, void** iter, param_type* r) { |
| 323 | const char *data; |
| 324 | int data_size = 0; |
| 325 | bool result = m->ReadData(iter, &data, &data_size); |
| 326 | if (result && data_size == sizeof(LOGFONT)) { |
| 327 | memcpy(r, data, sizeof(LOGFONT)); |
| 328 | } else { |
| 329 | result = false; |
| 330 | NOTREACHED(); |
| 331 | } |
| 332 | |
| 333 | return result; |
| 334 | } |
[email protected] | 252cad6 | 2010-08-18 18:33:57 | [diff] [blame] | 335 | static void Log(const param_type& p, std::string* l) { |
| 336 | l->append(StringPrintf("<LOGFONT>")); |
[email protected] | 7d5c3ac | 2009-02-04 08:58:19 | [diff] [blame] | 337 | } |
| 338 | }; |
| 339 | |
| 340 | template <> |
| 341 | struct ParamTraits<MSG> { |
| 342 | typedef MSG param_type; |
| 343 | static void Write(Message* m, const param_type& p) { |
| 344 | m->WriteData(reinterpret_cast<const char*>(&p), sizeof(MSG)); |
| 345 | } |
| 346 | static bool Read(const Message* m, void** iter, param_type* r) { |
| 347 | const char *data; |
| 348 | int data_size = 0; |
| 349 | bool result = m->ReadData(iter, &data, &data_size); |
| 350 | if (result && data_size == sizeof(MSG)) { |
| 351 | memcpy(r, data, sizeof(MSG)); |
| 352 | } else { |
| 353 | result = false; |
| 354 | NOTREACHED(); |
| 355 | } |
| 356 | |
| 357 | return result; |
| 358 | } |
[email protected] | 252cad6 | 2010-08-18 18:33:57 | [diff] [blame] | 359 | static void Log(const param_type& p, std::string* l) { |
| 360 | l->append("<MSG>"); |
[email protected] | 7a4de7a6 | 2010-08-17 18:38:24 | [diff] [blame] | 361 | } |
[email protected] | 7d5c3ac | 2009-02-04 08:58:19 | [diff] [blame] | 362 | }; |
| 363 | #endif // defined(OS_WIN) |
| 364 | |
| 365 | template <> |
[email protected] | 584f2b2 | 2009-05-21 01:01:59 | [diff] [blame] | 366 | struct ParamTraits<DictionaryValue> { |
| 367 | typedef DictionaryValue param_type; |
| 368 | static void Write(Message* m, const param_type& p); |
| 369 | static bool Read(const Message* m, void** iter, param_type* r); |
[email protected] | 252cad6 | 2010-08-18 18:33:57 | [diff] [blame] | 370 | static void Log(const param_type& p, std::string* l); |
[email protected] | 584f2b2 | 2009-05-21 01:01:59 | [diff] [blame] | 371 | }; |
| 372 | |
| 373 | template <> |
| 374 | struct ParamTraits<ListValue> { |
| 375 | typedef ListValue param_type; |
| 376 | static void Write(Message* m, const param_type& p); |
| 377 | static bool Read(const Message* m, void** iter, param_type* r); |
[email protected] | 252cad6 | 2010-08-18 18:33:57 | [diff] [blame] | 378 | static void Log(const param_type& p, std::string* l); |
[email protected] | 584f2b2 | 2009-05-21 01:01:59 | [diff] [blame] | 379 | }; |
| 380 | |
| 381 | template <> |
[email protected] | 7d5c3ac | 2009-02-04 08:58:19 | [diff] [blame] | 382 | struct ParamTraits<std::string> { |
| 383 | typedef std::string param_type; |
| 384 | static void Write(Message* m, const param_type& p) { |
| 385 | m->WriteString(p); |
| 386 | } |
| 387 | static bool Read(const Message* m, void** iter, param_type* r) { |
| 388 | return m->ReadString(iter, r); |
| 389 | } |
[email protected] | 252cad6 | 2010-08-18 18:33:57 | [diff] [blame] | 390 | static void Log(const param_type& p, std::string* l) { |
| 391 | l->append(p); |
[email protected] | 7d5c3ac | 2009-02-04 08:58:19 | [diff] [blame] | 392 | } |
| 393 | }; |
| 394 | |
[email protected] | 3dd7a7a | 2009-07-27 21:09:07 | [diff] [blame] | 395 | template<typename CharType> |
[email protected] | 252cad6 | 2010-08-18 18:33:57 | [diff] [blame] | 396 | static void LogBytes(const std::vector<CharType>& data, std::string* out) { |
[email protected] | 3dd7a7a | 2009-07-27 21:09:07 | [diff] [blame] | 397 | #if defined(OS_WIN) |
| 398 | // Windows has a GUI for logging, which can handle arbitrary binary data. |
| 399 | for (size_t i = 0; i < data.size(); ++i) |
| 400 | out->push_back(data[i]); |
| 401 | #else |
| 402 | // On POSIX, we log to stdout, which we assume can display ASCII. |
| 403 | static const size_t kMaxBytesToLog = 100; |
| 404 | for (size_t i = 0; i < std::min(data.size(), kMaxBytesToLog); ++i) { |
| 405 | if (isprint(data[i])) |
| 406 | out->push_back(data[i]); |
| 407 | else |
[email protected] | 252cad6 | 2010-08-18 18:33:57 | [diff] [blame] | 408 | out->append(StringPrintf("[%02X]", static_cast<unsigned char>(data[i]))); |
[email protected] | 3dd7a7a | 2009-07-27 21:09:07 | [diff] [blame] | 409 | } |
| 410 | if (data.size() > kMaxBytesToLog) { |
| 411 | out->append( |
[email protected] | 252cad6 | 2010-08-18 18:33:57 | [diff] [blame] | 412 | StringPrintf(" and %u more bytes", |
[email protected] | 3dd7a7a | 2009-07-27 21:09:07 | [diff] [blame] | 413 | static_cast<unsigned>(data.size() - kMaxBytesToLog))); |
| 414 | } |
| 415 | #endif |
| 416 | } |
| 417 | |
[email protected] | 7d5c3ac | 2009-02-04 08:58:19 | [diff] [blame] | 418 | template <> |
| 419 | struct ParamTraits<std::vector<unsigned char> > { |
| 420 | typedef std::vector<unsigned char> param_type; |
| 421 | static void Write(Message* m, const param_type& p) { |
| 422 | if (p.size() == 0) { |
| 423 | m->WriteData(NULL, 0); |
| 424 | } else { |
| 425 | m->WriteData(reinterpret_cast<const char*>(&p.front()), |
| 426 | static_cast<int>(p.size())); |
| 427 | } |
| 428 | } |
| 429 | static bool Read(const Message* m, void** iter, param_type* r) { |
| 430 | const char *data; |
| 431 | int data_size = 0; |
| 432 | if (!m->ReadData(iter, &data, &data_size) || data_size < 0) |
| 433 | return false; |
| 434 | r->resize(data_size); |
| 435 | if (data_size) |
| 436 | memcpy(&r->front(), data, data_size); |
| 437 | return true; |
| 438 | } |
[email protected] | 252cad6 | 2010-08-18 18:33:57 | [diff] [blame] | 439 | static void Log(const param_type& p, std::string* l) { |
[email protected] | 3dd7a7a | 2009-07-27 21:09:07 | [diff] [blame] | 440 | LogBytes(p, l); |
[email protected] | 7d5c3ac | 2009-02-04 08:58:19 | [diff] [blame] | 441 | } |
| 442 | }; |
| 443 | |
| 444 | template <> |
| 445 | struct ParamTraits<std::vector<char> > { |
| 446 | typedef std::vector<char> param_type; |
| 447 | static void Write(Message* m, const param_type& p) { |
| 448 | if (p.size() == 0) { |
| 449 | m->WriteData(NULL, 0); |
| 450 | } else { |
| 451 | m->WriteData(&p.front(), static_cast<int>(p.size())); |
| 452 | } |
| 453 | } |
| 454 | static bool Read(const Message* m, void** iter, param_type* r) { |
| 455 | const char *data; |
| 456 | int data_size = 0; |
| 457 | if (!m->ReadData(iter, &data, &data_size) || data_size < 0) |
| 458 | return false; |
| 459 | r->resize(data_size); |
| 460 | if (data_size) |
| 461 | memcpy(&r->front(), data, data_size); |
| 462 | return true; |
| 463 | } |
[email protected] | 252cad6 | 2010-08-18 18:33:57 | [diff] [blame] | 464 | static void Log(const param_type& p, std::string* l) { |
[email protected] | 3dd7a7a | 2009-07-27 21:09:07 | [diff] [blame] | 465 | LogBytes(p, l); |
[email protected] | 7d5c3ac | 2009-02-04 08:58:19 | [diff] [blame] | 466 | } |
| 467 | }; |
| 468 | |
| 469 | template <class P> |
| 470 | struct ParamTraits<std::vector<P> > { |
| 471 | typedef std::vector<P> param_type; |
| 472 | static void Write(Message* m, const param_type& p) { |
| 473 | WriteParam(m, static_cast<int>(p.size())); |
| 474 | for (size_t i = 0; i < p.size(); i++) |
| 475 | WriteParam(m, p[i]); |
| 476 | } |
| 477 | static bool Read(const Message* m, void** iter, param_type* r) { |
| 478 | int size; |
[email protected] | 86440f5 | 2009-12-31 05:17:23 | [diff] [blame] | 479 | // ReadLength() checks for < 0 itself. |
[email protected] | 7d5c3ac | 2009-02-04 08:58:19 | [diff] [blame] | 480 | if (!m->ReadLength(iter, &size)) |
| 481 | return false; |
| 482 | // Resizing beforehand is not safe, see BUG 1006367 for details. |
[email protected] | 86440f5 | 2009-12-31 05:17:23 | [diff] [blame] | 483 | if (INT_MAX / sizeof(P) <= static_cast<size_t>(size)) |
| 484 | return false; |
| 485 | r->resize(size); |
| 486 | for (int i = 0; i < size; i++) { |
| 487 | if (!ReadParam(m, iter, &(*r)[i])) |
| 488 | return false; |
[email protected] | 7d5c3ac | 2009-02-04 08:58:19 | [diff] [blame] | 489 | } |
| 490 | return true; |
| 491 | } |
[email protected] | 252cad6 | 2010-08-18 18:33:57 | [diff] [blame] | 492 | static void Log(const param_type& p, std::string* l) { |
[email protected] | 7d5c3ac | 2009-02-04 08:58:19 | [diff] [blame] | 493 | for (size_t i = 0; i < p.size(); ++i) { |
| 494 | if (i != 0) |
[email protected] | 252cad6 | 2010-08-18 18:33:57 | [diff] [blame] | 495 | l->append(" "); |
[email protected] | 7d5c3ac | 2009-02-04 08:58:19 | [diff] [blame] | 496 | LogParam((p[i]), l); |
| 497 | } |
| 498 | } |
| 499 | }; |
| 500 | |
[email protected] | 96da696 | 2010-05-13 19:10:34 | [diff] [blame] | 501 | template <class P> |
| 502 | struct ParamTraits<std::set<P> > { |
| 503 | typedef std::set<P> param_type; |
| 504 | static void Write(Message* m, const param_type& p) { |
| 505 | WriteParam(m, static_cast<int>(p.size())); |
| 506 | typename param_type::const_iterator iter; |
| 507 | for (iter = p.begin(); iter != p.end(); ++iter) |
| 508 | WriteParam(m, *iter); |
| 509 | } |
| 510 | static bool Read(const Message* m, void** iter, param_type* r) { |
| 511 | int size; |
| 512 | if (!m->ReadLength(iter, &size)) |
| 513 | return false; |
| 514 | for (int i = 0; i < size; ++i) { |
| 515 | P item; |
| 516 | if (!ReadParam(m, iter, &item)) |
| 517 | return false; |
| 518 | r->insert(item); |
| 519 | } |
| 520 | return true; |
| 521 | } |
[email protected] | 252cad6 | 2010-08-18 18:33:57 | [diff] [blame] | 522 | static void Log(const param_type& p, std::string* l) { |
| 523 | l->append("<std::set>"); |
[email protected] | 96da696 | 2010-05-13 19:10:34 | [diff] [blame] | 524 | } |
| 525 | }; |
| 526 | |
| 527 | |
[email protected] | 7d5c3ac | 2009-02-04 08:58:19 | [diff] [blame] | 528 | template <class K, class V> |
| 529 | struct ParamTraits<std::map<K, V> > { |
| 530 | typedef std::map<K, V> param_type; |
| 531 | static void Write(Message* m, const param_type& p) { |
| 532 | WriteParam(m, static_cast<int>(p.size())); |
| 533 | typename param_type::const_iterator iter; |
| 534 | for (iter = p.begin(); iter != p.end(); ++iter) { |
| 535 | WriteParam(m, iter->first); |
| 536 | WriteParam(m, iter->second); |
| 537 | } |
| 538 | } |
| 539 | static bool Read(const Message* m, void** iter, param_type* r) { |
| 540 | int size; |
| 541 | if (!ReadParam(m, iter, &size) || size < 0) |
| 542 | return false; |
| 543 | for (int i = 0; i < size; ++i) { |
| 544 | K k; |
| 545 | if (!ReadParam(m, iter, &k)) |
| 546 | return false; |
| 547 | V& value = (*r)[k]; |
| 548 | if (!ReadParam(m, iter, &value)) |
| 549 | return false; |
| 550 | } |
| 551 | return true; |
| 552 | } |
[email protected] | 252cad6 | 2010-08-18 18:33:57 | [diff] [blame] | 553 | static void Log(const param_type& p, std::string* l) { |
| 554 | l->append("<std::map>"); |
[email protected] | 7d5c3ac | 2009-02-04 08:58:19 | [diff] [blame] | 555 | } |
| 556 | }; |
| 557 | |
[email protected] | eb47a13 | 2009-03-04 00:39:56 | [diff] [blame] | 558 | |
[email protected] | 7d5c3ac | 2009-02-04 08:58:19 | [diff] [blame] | 559 | template <> |
| 560 | struct ParamTraits<std::wstring> { |
| 561 | typedef std::wstring param_type; |
| 562 | static void Write(Message* m, const param_type& p) { |
| 563 | m->WriteWString(p); |
| 564 | } |
| 565 | static bool Read(const Message* m, void** iter, param_type* r) { |
| 566 | return m->ReadWString(iter, r); |
| 567 | } |
[email protected] | 252cad6 | 2010-08-18 18:33:57 | [diff] [blame] | 568 | static void Log(const param_type& p, std::string* l); |
[email protected] | 7d5c3ac | 2009-02-04 08:58:19 | [diff] [blame] | 569 | }; |
| 570 | |
[email protected] | a5da6d61 | 2009-08-04 02:00:56 | [diff] [blame] | 571 | template <class A, class B> |
| 572 | struct ParamTraits<std::pair<A, B> > { |
| 573 | typedef std::pair<A, B> param_type; |
| 574 | static void Write(Message* m, const param_type& p) { |
| 575 | WriteParam(m, p.first); |
| 576 | WriteParam(m, p.second); |
| 577 | } |
| 578 | static bool Read(const Message* m, void** iter, param_type* r) { |
| 579 | return ReadParam(m, iter, &r->first) && ReadParam(m, iter, &r->second); |
| 580 | } |
[email protected] | 252cad6 | 2010-08-18 18:33:57 | [diff] [blame] | 581 | static void Log(const param_type& p, std::string* l) { |
| 582 | l->append("("); |
[email protected] | a5da6d61 | 2009-08-04 02:00:56 | [diff] [blame] | 583 | LogParam(p.first, l); |
[email protected] | 252cad6 | 2010-08-18 18:33:57 | [diff] [blame] | 584 | l->append(", "); |
[email protected] | a5da6d61 | 2009-08-04 02:00:56 | [diff] [blame] | 585 | LogParam(p.second, l); |
[email protected] | 252cad6 | 2010-08-18 18:33:57 | [diff] [blame] | 586 | l->append(")"); |
[email protected] | a5da6d61 | 2009-08-04 02:00:56 | [diff] [blame] | 587 | } |
| 588 | }; |
| 589 | |
[email protected] | 15bf871 | 2009-08-27 00:55:02 | [diff] [blame] | 590 | template <> |
| 591 | struct ParamTraits<NullableString16> { |
| 592 | typedef NullableString16 param_type; |
[email protected] | 7a4de7a6 | 2010-08-17 18:38:24 | [diff] [blame] | 593 | static void Write(Message* m, const param_type& p); |
| 594 | static bool Read(const Message* m, void** iter, param_type* r); |
[email protected] | 252cad6 | 2010-08-18 18:33:57 | [diff] [blame] | 595 | static void Log(const param_type& p, std::string* l); |
[email protected] | 15bf871 | 2009-08-27 00:55:02 | [diff] [blame] | 596 | }; |
| 597 | |
[email protected] | eb47a13 | 2009-03-04 00:39:56 | [diff] [blame] | 598 | // If WCHAR_T_IS_UTF16 is defined, then string16 is a std::wstring so we don't |
| 599 | // need this trait. |
| 600 | #if !defined(WCHAR_T_IS_UTF16) |
[email protected] | eb47a13 | 2009-03-04 00:39:56 | [diff] [blame] | 601 | template <> |
| 602 | struct ParamTraits<string16> { |
| 603 | typedef string16 param_type; |
| 604 | static void Write(Message* m, const param_type& p) { |
[email protected] | 3a2a5d2 | 2009-03-04 03:36:36 | [diff] [blame] | 605 | m->WriteString16(p); |
[email protected] | eb47a13 | 2009-03-04 00:39:56 | [diff] [blame] | 606 | } |
| 607 | static bool Read(const Message* m, void** iter, param_type* r) { |
[email protected] | 3a2a5d2 | 2009-03-04 03:36:36 | [diff] [blame] | 608 | return m->ReadString16(iter, r); |
[email protected] | eb47a13 | 2009-03-04 00:39:56 | [diff] [blame] | 609 | } |
[email protected] | 252cad6 | 2010-08-18 18:33:57 | [diff] [blame] | 610 | static void Log(const param_type& p, std::string* l); |
[email protected] | eb47a13 | 2009-03-04 00:39:56 | [diff] [blame] | 611 | }; |
[email protected] | eb47a13 | 2009-03-04 00:39:56 | [diff] [blame] | 612 | #endif |
| 613 | |
[email protected] | 7d5c3ac | 2009-02-04 08:58:19 | [diff] [blame] | 614 | // and, a few more useful types... |
| 615 | #if defined(OS_WIN) |
| 616 | template <> |
| 617 | struct ParamTraits<HANDLE> { |
| 618 | typedef HANDLE param_type; |
| 619 | static void Write(Message* m, const param_type& p) { |
[email protected] | 3747791 | 2010-02-09 08:06:35 | [diff] [blame] | 620 | // Note that HWNDs/HANDLE/HCURSOR/HACCEL etc are always 32 bits, even on 64 |
| 621 | // bit systems. |
| 622 | m->WriteUInt32(reinterpret_cast<uint32>(p)); |
[email protected] | 7d5c3ac | 2009-02-04 08:58:19 | [diff] [blame] | 623 | } |
| 624 | static bool Read(const Message* m, void** iter, param_type* r) { |
[email protected] | 3747791 | 2010-02-09 08:06:35 | [diff] [blame] | 625 | DCHECK_EQ(sizeof(param_type), sizeof(uint32)); |
| 626 | return m->ReadUInt32(iter, reinterpret_cast<uint32*>(r)); |
[email protected] | 7d5c3ac | 2009-02-04 08:58:19 | [diff] [blame] | 627 | } |
[email protected] | 252cad6 | 2010-08-18 18:33:57 | [diff] [blame] | 628 | static void Log(const param_type& p, std::string* l) { |
| 629 | l->append(StringPrintf("0x%X", p)); |
[email protected] | 7d5c3ac | 2009-02-04 08:58:19 | [diff] [blame] | 630 | } |
| 631 | }; |
| 632 | |
| 633 | template <> |
| 634 | struct ParamTraits<HCURSOR> { |
| 635 | typedef HCURSOR param_type; |
| 636 | static void Write(Message* m, const param_type& p) { |
[email protected] | 3747791 | 2010-02-09 08:06:35 | [diff] [blame] | 637 | m->WriteUInt32(reinterpret_cast<uint32>(p)); |
[email protected] | 7d5c3ac | 2009-02-04 08:58:19 | [diff] [blame] | 638 | } |
| 639 | static bool Read(const Message* m, void** iter, param_type* r) { |
[email protected] | 3747791 | 2010-02-09 08:06:35 | [diff] [blame] | 640 | DCHECK_EQ(sizeof(param_type), sizeof(uint32)); |
| 641 | return m->ReadUInt32(iter, reinterpret_cast<uint32*>(r)); |
[email protected] | 7d5c3ac | 2009-02-04 08:58:19 | [diff] [blame] | 642 | } |
[email protected] | 252cad6 | 2010-08-18 18:33:57 | [diff] [blame] | 643 | static void Log(const param_type& p, std::string* l) { |
| 644 | l->append(StringPrintf("0x%X", p)); |
[email protected] | 7d5c3ac | 2009-02-04 08:58:19 | [diff] [blame] | 645 | } |
| 646 | }; |
| 647 | |
| 648 | template <> |
[email protected] | 7d5c3ac | 2009-02-04 08:58:19 | [diff] [blame] | 649 | struct ParamTraits<HACCEL> { |
| 650 | typedef HACCEL param_type; |
| 651 | static void Write(Message* m, const param_type& p) { |
[email protected] | 3747791 | 2010-02-09 08:06:35 | [diff] [blame] | 652 | m->WriteUInt32(reinterpret_cast<uint32>(p)); |
[email protected] | 7d5c3ac | 2009-02-04 08:58:19 | [diff] [blame] | 653 | } |
| 654 | static bool Read(const Message* m, void** iter, param_type* r) { |
[email protected] | 3747791 | 2010-02-09 08:06:35 | [diff] [blame] | 655 | DCHECK_EQ(sizeof(param_type), sizeof(uint32)); |
| 656 | return m->ReadUInt32(iter, reinterpret_cast<uint32*>(r)); |
[email protected] | 7d5c3ac | 2009-02-04 08:58:19 | [diff] [blame] | 657 | } |
| 658 | }; |
| 659 | |
| 660 | template <> |
| 661 | struct ParamTraits<POINT> { |
| 662 | typedef POINT param_type; |
| 663 | static void Write(Message* m, const param_type& p) { |
| 664 | m->WriteInt(p.x); |
| 665 | m->WriteInt(p.y); |
| 666 | } |
| 667 | static bool Read(const Message* m, void** iter, param_type* r) { |
| 668 | int x, y; |
| 669 | if (!m->ReadInt(iter, &x) || !m->ReadInt(iter, &y)) |
| 670 | return false; |
| 671 | r->x = x; |
| 672 | r->y = y; |
| 673 | return true; |
| 674 | } |
[email protected] | 252cad6 | 2010-08-18 18:33:57 | [diff] [blame] | 675 | static void Log(const param_type& p, std::string* l) { |
| 676 | l->append(StringPrintf("(%d, %d)", p.x, p.y)); |
[email protected] | 7d5c3ac | 2009-02-04 08:58:19 | [diff] [blame] | 677 | } |
| 678 | }; |
| 679 | #endif // defined(OS_WIN) |
| 680 | |
| 681 | template <> |
| 682 | struct ParamTraits<FilePath> { |
| 683 | typedef FilePath param_type; |
[email protected] | 7a4de7a6 | 2010-08-17 18:38:24 | [diff] [blame] | 684 | static void Write(Message* m, const param_type& p); |
| 685 | static bool Read(const Message* m, void** iter, param_type* r); |
[email protected] | 252cad6 | 2010-08-18 18:33:57 | [diff] [blame] | 686 | static void Log(const param_type& p, std::string* l); |
[email protected] | 7d5c3ac | 2009-02-04 08:58:19 | [diff] [blame] | 687 | }; |
| 688 | |
[email protected] | 526776c | 2009-02-07 00:39:26 | [diff] [blame] | 689 | #if defined(OS_POSIX) |
[email protected] | 2749885f | 2009-03-05 21:40:11 | [diff] [blame] | 690 | // FileDescriptors may be serialised over IPC channels on POSIX. On the |
| 691 | // receiving side, the FileDescriptor is a valid duplicate of the file |
| 692 | // descriptor which was transmitted: *it is not just a copy of the integer like |
| 693 | // HANDLEs on Windows*. The only exception is if the file descriptor is < 0. In |
| 694 | // this case, the receiving end will see a value of -1. *Zero is a valid file |
| 695 | // descriptor*. |
| 696 | // |
| 697 | // The received file descriptor will have the |auto_close| flag set to true. The |
| 698 | // code which handles the message is responsible for taking ownership of it. |
| 699 | // File descriptors are OS resources and must be closed when no longer needed. |
| 700 | // |
| 701 | // When sending a file descriptor, the file descriptor must be valid at the time |
| 702 | // of transmission. Since transmission is not synchronous, one should consider |
| 703 | // dup()ing any file descriptors to be transmitted and setting the |auto_close| |
| 704 | // flag, which causes the file descriptor to be closed after writing. |
[email protected] | 526776c | 2009-02-07 00:39:26 | [diff] [blame] | 705 | template<> |
[email protected] | 5fe733de | 2009-02-11 18:59:20 | [diff] [blame] | 706 | struct ParamTraits<base::FileDescriptor> { |
| 707 | typedef base::FileDescriptor param_type; |
[email protected] | 7a4de7a6 | 2010-08-17 18:38:24 | [diff] [blame] | 708 | static void Write(Message* m, const param_type& p); |
| 709 | static bool Read(const Message* m, void** iter, param_type* r); |
[email protected] | 252cad6 | 2010-08-18 18:33:57 | [diff] [blame] | 710 | static void Log(const param_type& p, std::string* l); |
[email protected] | 526776c | 2009-02-07 00:39:26 | [diff] [blame] | 711 | }; |
[email protected] | 379e7a5 | 2010-03-09 00:38:41 | [diff] [blame] | 712 | #endif // defined(OS_POSIX) |
[email protected] | 526776c | 2009-02-07 00:39:26 | [diff] [blame] | 713 | |
[email protected] | d2e884d | 2009-06-22 20:37:52 | [diff] [blame] | 714 | // A ChannelHandle is basically a platform-inspecific wrapper around the |
| 715 | // fact that IPC endpoints are handled specially on POSIX. See above comments |
| 716 | // on FileDescriptor for more background. |
| 717 | template<> |
| 718 | struct ParamTraits<IPC::ChannelHandle> { |
| 719 | typedef ChannelHandle param_type; |
[email protected] | 7a4de7a6 | 2010-08-17 18:38:24 | [diff] [blame] | 720 | static void Write(Message* m, const param_type& p); |
| 721 | static bool Read(const Message* m, void** iter, param_type* r); |
[email protected] | 252cad6 | 2010-08-18 18:33:57 | [diff] [blame] | 722 | static void Log(const param_type& p, std::string* l); |
[email protected] | d2e884d | 2009-06-22 20:37:52 | [diff] [blame] | 723 | }; |
| 724 | |
[email protected] | 7d5c3ac | 2009-02-04 08:58:19 | [diff] [blame] | 725 | #if defined(OS_WIN) |
| 726 | template <> |
| 727 | struct ParamTraits<XFORM> { |
| 728 | typedef XFORM param_type; |
| 729 | static void Write(Message* m, const param_type& p) { |
| 730 | m->WriteData(reinterpret_cast<const char*>(&p), sizeof(XFORM)); |
| 731 | } |
| 732 | static bool Read(const Message* m, void** iter, param_type* r) { |
| 733 | const char *data; |
| 734 | int data_size = 0; |
| 735 | bool result = m->ReadData(iter, &data, &data_size); |
| 736 | if (result && data_size == sizeof(XFORM)) { |
| 737 | memcpy(r, data, sizeof(XFORM)); |
| 738 | } else { |
| 739 | result = false; |
| 740 | NOTREACHED(); |
| 741 | } |
| 742 | |
| 743 | return result; |
| 744 | } |
[email protected] | 252cad6 | 2010-08-18 18:33:57 | [diff] [blame] | 745 | static void Log(const param_type& p, std::string* l) { |
| 746 | l->append("<XFORM>"); |
[email protected] | 7d5c3ac | 2009-02-04 08:58:19 | [diff] [blame] | 747 | } |
| 748 | }; |
| 749 | #endif // defined(OS_WIN) |
| 750 | |
[email protected] | 7d5c3ac | 2009-02-04 08:58:19 | [diff] [blame] | 751 | struct LogData { |
[email protected] | 20f0487a | 2010-09-30 20:06:30 | [diff] [blame] | 752 | LogData(); |
| 753 | ~LogData(); |
| 754 | |
[email protected] | 9a3a293b | 2009-06-04 22:28:16 | [diff] [blame] | 755 | std::string channel; |
[email protected] | 8bef70e | 2009-04-14 19:11:24 | [diff] [blame] | 756 | int32 routing_id; |
[email protected] | 168ae92 | 2009-12-04 18:08:45 | [diff] [blame] | 757 | uint32 type; // "User-defined" message type, from ipc_message.h. |
[email protected] | 252cad6 | 2010-08-18 18:33:57 | [diff] [blame] | 758 | std::string flags; |
[email protected] | 7d5c3ac | 2009-02-04 08:58:19 | [diff] [blame] | 759 | int64 sent; // Time that the message was sent (i.e. at Send()). |
| 760 | int64 receive; // Time before it was dispatched (i.e. before calling |
| 761 | // OnMessageReceived). |
| 762 | int64 dispatch; // Time after it was dispatched (i.e. after calling |
| 763 | // OnMessageReceived). |
[email protected] | 252cad6 | 2010-08-18 18:33:57 | [diff] [blame] | 764 | std::string message_name; |
| 765 | std::string params; |
[email protected] | 7d5c3ac | 2009-02-04 08:58:19 | [diff] [blame] | 766 | }; |
| 767 | |
| 768 | template <> |
| 769 | struct ParamTraits<LogData> { |
| 770 | typedef LogData param_type; |
[email protected] | 20f0487a | 2010-09-30 20:06:30 | [diff] [blame] | 771 | static void Write(Message* m, const param_type& p); |
| 772 | static bool Read(const Message* m, void** iter, param_type* r); |
[email protected] | 252cad6 | 2010-08-18 18:33:57 | [diff] [blame] | 773 | static void Log(const param_type& p, std::string* l) { |
[email protected] | 7d5c3ac | 2009-02-04 08:58:19 | [diff] [blame] | 774 | // Doesn't make sense to implement this! |
| 775 | } |
| 776 | }; |
| 777 | |
[email protected] | eb47a13 | 2009-03-04 00:39:56 | [diff] [blame] | 778 | template <> |
[email protected] | 503683f | 2009-02-26 09:13:01 | [diff] [blame] | 779 | struct ParamTraits<Message> { |
| 780 | static void Write(Message* m, const Message& p) { |
| 781 | m->WriteInt(p.size()); |
| 782 | m->WriteData(reinterpret_cast<const char*>(p.data()), p.size()); |
| 783 | } |
| 784 | static bool Read(const Message* m, void** iter, Message* r) { |
| 785 | int size; |
| 786 | if (!m->ReadInt(iter, &size)) |
| 787 | return false; |
| 788 | const char* data; |
| 789 | if (!m->ReadData(iter, &data, &size)) |
| 790 | return false; |
| 791 | *r = Message(data, size); |
| 792 | return true; |
| 793 | } |
[email protected] | 252cad6 | 2010-08-18 18:33:57 | [diff] [blame] | 794 | static void Log(const Message& p, std::string* l) { |
| 795 | l->append("<IPC::Message>"); |
[email protected] | 503683f | 2009-02-26 09:13:01 | [diff] [blame] | 796 | } |
| 797 | }; |
| 798 | |
| 799 | template <> |
[email protected] | 7d5c3ac | 2009-02-04 08:58:19 | [diff] [blame] | 800 | struct ParamTraits<Tuple0> { |
| 801 | typedef Tuple0 param_type; |
| 802 | static void Write(Message* m, const param_type& p) { |
| 803 | } |
| 804 | static bool Read(const Message* m, void** iter, param_type* r) { |
| 805 | return true; |
| 806 | } |
[email protected] | 252cad6 | 2010-08-18 18:33:57 | [diff] [blame] | 807 | static void Log(const param_type& p, std::string* l) { |
[email protected] | 7d5c3ac | 2009-02-04 08:58:19 | [diff] [blame] | 808 | } |
| 809 | }; |
| 810 | |
| 811 | template <class A> |
| 812 | struct ParamTraits< Tuple1<A> > { |
| 813 | typedef Tuple1<A> param_type; |
| 814 | static void Write(Message* m, const param_type& p) { |
| 815 | WriteParam(m, p.a); |
| 816 | } |
| 817 | static bool Read(const Message* m, void** iter, param_type* r) { |
| 818 | return ReadParam(m, iter, &r->a); |
| 819 | } |
[email protected] | 252cad6 | 2010-08-18 18:33:57 | [diff] [blame] | 820 | static void Log(const param_type& p, std::string* l) { |
[email protected] | 7d5c3ac | 2009-02-04 08:58:19 | [diff] [blame] | 821 | LogParam(p.a, l); |
| 822 | } |
| 823 | }; |
| 824 | |
| 825 | template <class A, class B> |
| 826 | struct ParamTraits< Tuple2<A, B> > { |
| 827 | typedef Tuple2<A, B> param_type; |
| 828 | static void Write(Message* m, const param_type& p) { |
| 829 | WriteParam(m, p.a); |
| 830 | WriteParam(m, p.b); |
| 831 | } |
| 832 | static bool Read(const Message* m, void** iter, param_type* r) { |
| 833 | return (ReadParam(m, iter, &r->a) && |
| 834 | ReadParam(m, iter, &r->b)); |
| 835 | } |
[email protected] | 252cad6 | 2010-08-18 18:33:57 | [diff] [blame] | 836 | static void Log(const param_type& p, std::string* l) { |
[email protected] | 7d5c3ac | 2009-02-04 08:58:19 | [diff] [blame] | 837 | LogParam(p.a, l); |
[email protected] | 252cad6 | 2010-08-18 18:33:57 | [diff] [blame] | 838 | l->append(", "); |
[email protected] | 7d5c3ac | 2009-02-04 08:58:19 | [diff] [blame] | 839 | LogParam(p.b, l); |
| 840 | } |
| 841 | }; |
| 842 | |
| 843 | template <class A, class B, class C> |
| 844 | struct ParamTraits< Tuple3<A, B, C> > { |
| 845 | typedef Tuple3<A, B, C> param_type; |
| 846 | static void Write(Message* m, const param_type& p) { |
| 847 | WriteParam(m, p.a); |
| 848 | WriteParam(m, p.b); |
| 849 | WriteParam(m, p.c); |
| 850 | } |
| 851 | static bool Read(const Message* m, void** iter, param_type* r) { |
| 852 | return (ReadParam(m, iter, &r->a) && |
| 853 | ReadParam(m, iter, &r->b) && |
| 854 | ReadParam(m, iter, &r->c)); |
| 855 | } |
[email protected] | 252cad6 | 2010-08-18 18:33:57 | [diff] [blame] | 856 | static void Log(const param_type& p, std::string* l) { |
[email protected] | 7d5c3ac | 2009-02-04 08:58:19 | [diff] [blame] | 857 | LogParam(p.a, l); |
[email protected] | 252cad6 | 2010-08-18 18:33:57 | [diff] [blame] | 858 | l->append(", "); |
[email protected] | 7d5c3ac | 2009-02-04 08:58:19 | [diff] [blame] | 859 | LogParam(p.b, l); |
[email protected] | 252cad6 | 2010-08-18 18:33:57 | [diff] [blame] | 860 | l->append(", "); |
[email protected] | 7d5c3ac | 2009-02-04 08:58:19 | [diff] [blame] | 861 | LogParam(p.c, l); |
| 862 | } |
| 863 | }; |
| 864 | |
| 865 | template <class A, class B, class C, class D> |
| 866 | struct ParamTraits< Tuple4<A, B, C, D> > { |
| 867 | typedef Tuple4<A, B, C, D> param_type; |
| 868 | static void Write(Message* m, const param_type& p) { |
| 869 | WriteParam(m, p.a); |
| 870 | WriteParam(m, p.b); |
| 871 | WriteParam(m, p.c); |
| 872 | WriteParam(m, p.d); |
| 873 | } |
| 874 | static bool Read(const Message* m, void** iter, param_type* r) { |
| 875 | return (ReadParam(m, iter, &r->a) && |
| 876 | ReadParam(m, iter, &r->b) && |
| 877 | ReadParam(m, iter, &r->c) && |
| 878 | ReadParam(m, iter, &r->d)); |
| 879 | } |
[email protected] | 252cad6 | 2010-08-18 18:33:57 | [diff] [blame] | 880 | static void Log(const param_type& p, std::string* l) { |
[email protected] | 7d5c3ac | 2009-02-04 08:58:19 | [diff] [blame] | 881 | LogParam(p.a, l); |
[email protected] | 252cad6 | 2010-08-18 18:33:57 | [diff] [blame] | 882 | l->append(", "); |
[email protected] | 7d5c3ac | 2009-02-04 08:58:19 | [diff] [blame] | 883 | LogParam(p.b, l); |
[email protected] | 252cad6 | 2010-08-18 18:33:57 | [diff] [blame] | 884 | l->append(", "); |
[email protected] | 7d5c3ac | 2009-02-04 08:58:19 | [diff] [blame] | 885 | LogParam(p.c, l); |
[email protected] | 252cad6 | 2010-08-18 18:33:57 | [diff] [blame] | 886 | l->append(", "); |
[email protected] | 7d5c3ac | 2009-02-04 08:58:19 | [diff] [blame] | 887 | LogParam(p.d, l); |
| 888 | } |
| 889 | }; |
| 890 | |
| 891 | template <class A, class B, class C, class D, class E> |
| 892 | struct ParamTraits< Tuple5<A, B, C, D, E> > { |
| 893 | typedef Tuple5<A, B, C, D, E> param_type; |
| 894 | static void Write(Message* m, const param_type& p) { |
| 895 | WriteParam(m, p.a); |
| 896 | WriteParam(m, p.b); |
| 897 | WriteParam(m, p.c); |
| 898 | WriteParam(m, p.d); |
| 899 | WriteParam(m, p.e); |
| 900 | } |
| 901 | static bool Read(const Message* m, void** iter, param_type* r) { |
| 902 | return (ReadParam(m, iter, &r->a) && |
| 903 | ReadParam(m, iter, &r->b) && |
| 904 | ReadParam(m, iter, &r->c) && |
| 905 | ReadParam(m, iter, &r->d) && |
| 906 | ReadParam(m, iter, &r->e)); |
| 907 | } |
[email protected] | 252cad6 | 2010-08-18 18:33:57 | [diff] [blame] | 908 | static void Log(const param_type& p, std::string* l) { |
[email protected] | 7d5c3ac | 2009-02-04 08:58:19 | [diff] [blame] | 909 | LogParam(p.a, l); |
[email protected] | 252cad6 | 2010-08-18 18:33:57 | [diff] [blame] | 910 | l->append(", "); |
[email protected] | 7d5c3ac | 2009-02-04 08:58:19 | [diff] [blame] | 911 | LogParam(p.b, l); |
[email protected] | 252cad6 | 2010-08-18 18:33:57 | [diff] [blame] | 912 | l->append(", "); |
[email protected] | 7d5c3ac | 2009-02-04 08:58:19 | [diff] [blame] | 913 | LogParam(p.c, l); |
[email protected] | 252cad6 | 2010-08-18 18:33:57 | [diff] [blame] | 914 | l->append(", "); |
[email protected] | 7d5c3ac | 2009-02-04 08:58:19 | [diff] [blame] | 915 | LogParam(p.d, l); |
[email protected] | 252cad6 | 2010-08-18 18:33:57 | [diff] [blame] | 916 | l->append(", "); |
[email protected] | 7d5c3ac | 2009-02-04 08:58:19 | [diff] [blame] | 917 | LogParam(p.e, l); |
| 918 | } |
| 919 | }; |
| 920 | |
[email protected] | 7d5c3ac | 2009-02-04 08:58:19 | [diff] [blame] | 921 | //----------------------------------------------------------------------------- |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 922 | // Generic message subclasses |
| 923 | |
| 924 | // Used for asynchronous messages. |
[email protected] | 81a3441 | 2009-01-05 19:17:24 | [diff] [blame] | 925 | template <class ParamType> |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 926 | class MessageWithTuple : public Message { |
| 927 | public: |
[email protected] | 81a3441 | 2009-01-05 19:17:24 | [diff] [blame] | 928 | typedef ParamType Param; |
[email protected] | 7a4de7a6 | 2010-08-17 18:38:24 | [diff] [blame] | 929 | typedef typename TupleTypes<ParamType>::ParamTuple RefParam; |
[email protected] | 81a3441 | 2009-01-05 19:17:24 | [diff] [blame] | 930 | |
[email protected] | 7a4de7a6 | 2010-08-17 18:38:24 | [diff] [blame] | 931 | // The constructor and the Read() method's templated implementations are in |
| 932 | // ipc_message_utils_impl.h. The subclass constructor and Log() methods call |
| 933 | // the templated versions of these and make sure there are instantiations in |
| 934 | // those translation units. |
| 935 | MessageWithTuple(int32 routing_id, uint32 type, const RefParam& p); |
[email protected] | 071b4b9 | 2010-08-09 16:06:58 | [diff] [blame] | 936 | |
[email protected] | 7a4de7a6 | 2010-08-17 18:38:24 | [diff] [blame] | 937 | static bool Read(const Message* msg, Param* p) IPC_MSG_NOINLINE; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 938 | |
| 939 | // Generic dispatcher. Should cover most cases. |
| 940 | template<class T, class Method> |
[email protected] | 7d5c3ac | 2009-02-04 08:58:19 | [diff] [blame] | 941 | static bool Dispatch(const Message* msg, T* obj, Method func) { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 942 | Param p; |
| 943 | if (Read(msg, &p)) { |
| 944 | DispatchToMethod(obj, func, p); |
| 945 | return true; |
| 946 | } |
| 947 | return false; |
| 948 | } |
| 949 | |
| 950 | // The following dispatchers exist for the case where the callback function |
| 951 | // needs the message as well. They assume that "Param" is a type of Tuple |
| 952 | // (except the one arg case, as there is no Tuple1). |
| 953 | template<class T, typename TA> |
[email protected] | 7d5c3ac | 2009-02-04 08:58:19 | [diff] [blame] | 954 | static bool Dispatch(const Message* msg, T* obj, |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 955 | void (T::*func)(const Message&, TA)) { |
| 956 | Param p; |
| 957 | if (Read(msg, &p)) { |
[email protected] | c2fe3154 | 2009-05-20 18:24:14 | [diff] [blame] | 958 | (obj->*func)(*msg, p.a); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 959 | return true; |
| 960 | } |
| 961 | return false; |
| 962 | } |
| 963 | |
| 964 | template<class T, typename TA, typename TB> |
[email protected] | 7d5c3ac | 2009-02-04 08:58:19 | [diff] [blame] | 965 | static bool Dispatch(const Message* msg, T* obj, |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 966 | void (T::*func)(const Message&, TA, TB)) { |
| 967 | Param p; |
| 968 | if (Read(msg, &p)) { |
| 969 | (obj->*func)(*msg, p.a, p.b); |
| 970 | return true; |
| 971 | } |
| 972 | return false; |
| 973 | } |
| 974 | |
| 975 | template<class T, typename TA, typename TB, typename TC> |
[email protected] | 7d5c3ac | 2009-02-04 08:58:19 | [diff] [blame] | 976 | static bool Dispatch(const Message* msg, T* obj, |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 977 | void (T::*func)(const Message&, TA, TB, TC)) { |
| 978 | Param p; |
| 979 | if (Read(msg, &p)) { |
| 980 | (obj->*func)(*msg, p.a, p.b, p.c); |
| 981 | return true; |
| 982 | } |
| 983 | return false; |
| 984 | } |
| 985 | |
| 986 | template<class T, typename TA, typename TB, typename TC, typename TD> |
[email protected] | 7d5c3ac | 2009-02-04 08:58:19 | [diff] [blame] | 987 | static bool Dispatch(const Message* msg, T* obj, |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 988 | void (T::*func)(const Message&, TA, TB, TC, TD)) { |
| 989 | Param p; |
| 990 | if (Read(msg, &p)) { |
| 991 | (obj->*func)(*msg, p.a, p.b, p.c, p.d); |
| 992 | return true; |
| 993 | } |
| 994 | return false; |
| 995 | } |
| 996 | |
| 997 | template<class T, typename TA, typename TB, typename TC, typename TD, |
| 998 | typename TE> |
[email protected] | 7d5c3ac | 2009-02-04 08:58:19 | [diff] [blame] | 999 | static bool Dispatch(const Message* msg, T* obj, |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1000 | void (T::*func)(const Message&, TA, TB, TC, TD, TE)) { |
| 1001 | Param p; |
| 1002 | if (Read(msg, &p)) { |
| 1003 | (obj->*func)(*msg, p.a, p.b, p.c, p.d, p.e); |
| 1004 | return true; |
| 1005 | } |
| 1006 | return false; |
| 1007 | } |
| 1008 | |
[email protected] | deb5740 | 2009-02-06 01:35:30 | [diff] [blame] | 1009 | // Functions used to do manual unpacking. Only used by the automation code, |
| 1010 | // these should go away once that code uses SyncChannel. |
| 1011 | template<typename TA, typename TB> |
| 1012 | static bool Read(const IPC::Message* msg, TA* a, TB* b) { |
| 1013 | ParamType params; |
| 1014 | if (!Read(msg, ¶ms)) |
| 1015 | return false; |
| 1016 | *a = params.a; |
| 1017 | *b = params.b; |
| 1018 | return true; |
| 1019 | } |
| 1020 | |
| 1021 | template<typename TA, typename TB, typename TC> |
| 1022 | static bool Read(const IPC::Message* msg, TA* a, TB* b, TC* c) { |
| 1023 | ParamType params; |
| 1024 | if (!Read(msg, ¶ms)) |
| 1025 | return false; |
| 1026 | *a = params.a; |
| 1027 | *b = params.b; |
| 1028 | *c = params.c; |
| 1029 | return true; |
| 1030 | } |
| 1031 | |
| 1032 | template<typename TA, typename TB, typename TC, typename TD> |
| 1033 | static bool Read(const IPC::Message* msg, TA* a, TB* b, TC* c, TD* d) { |
| 1034 | ParamType params; |
| 1035 | if (!Read(msg, ¶ms)) |
| 1036 | return false; |
| 1037 | *a = params.a; |
| 1038 | *b = params.b; |
| 1039 | *c = params.c; |
| 1040 | *d = params.d; |
| 1041 | return true; |
| 1042 | } |
| 1043 | |
| 1044 | template<typename TA, typename TB, typename TC, typename TD, typename TE> |
| 1045 | static bool Read(const IPC::Message* msg, TA* a, TB* b, TC* c, TD* d, TE* e) { |
| 1046 | ParamType params; |
| 1047 | if (!Read(msg, ¶ms)) |
| 1048 | return false; |
| 1049 | *a = params.a; |
| 1050 | *b = params.b; |
| 1051 | *c = params.c; |
| 1052 | *d = params.d; |
| 1053 | *e = params.e; |
| 1054 | return true; |
| 1055 | } |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1056 | }; |
| 1057 | |
[email protected] | 7a4de7a6 | 2010-08-17 18:38:24 | [diff] [blame] | 1058 | // defined in ipc_logging.cc |
| 1059 | void GenerateLogData(const std::string& channel, const Message& message, |
| 1060 | LogData* data); |
| 1061 | |
| 1062 | |
| 1063 | #if defined(IPC_MESSAGE_LOG_ENABLED) |
[email protected] | 252cad6 | 2010-08-18 18:33:57 | [diff] [blame] | 1064 | inline void AddOutputParamsToLog(const Message* msg, std::string* l) { |
| 1065 | const std::string& output_params = msg->output_params(); |
[email protected] | 7a4de7a6 | 2010-08-17 18:38:24 | [diff] [blame] | 1066 | if (!l->empty() && !output_params.empty()) |
[email protected] | 252cad6 | 2010-08-18 18:33:57 | [diff] [blame] | 1067 | l->append(", "); |
[email protected] | 7a4de7a6 | 2010-08-17 18:38:24 | [diff] [blame] | 1068 | |
| 1069 | l->append(output_params); |
| 1070 | } |
| 1071 | |
| 1072 | template <class ReplyParamType> |
| 1073 | inline void LogReplyParamsToMessage(const ReplyParamType& reply_params, |
| 1074 | const Message* msg) { |
| 1075 | if (msg->received_time() != 0) { |
[email protected] | 252cad6 | 2010-08-18 18:33:57 | [diff] [blame] | 1076 | std::string output_params; |
[email protected] | 7a4de7a6 | 2010-08-17 18:38:24 | [diff] [blame] | 1077 | LogParam(reply_params, &output_params); |
| 1078 | msg->set_output_params(output_params); |
| 1079 | } |
| 1080 | } |
| 1081 | |
| 1082 | inline void ConnectMessageAndReply(const Message* msg, Message* reply) { |
| 1083 | if (msg->sent_time()) { |
| 1084 | // Don't log the sync message after dispatch, as we don't have the |
| 1085 | // output parameters at that point. Instead, save its data and log it |
| 1086 | // with the outgoing reply message when it's sent. |
| 1087 | LogData* data = new LogData; |
| 1088 | GenerateLogData("", *msg, data); |
| 1089 | msg->set_dont_log(); |
| 1090 | reply->set_sync_log_data(data); |
| 1091 | } |
| 1092 | } |
| 1093 | #else |
[email protected] | 252cad6 | 2010-08-18 18:33:57 | [diff] [blame] | 1094 | inline void AddOutputParamsToLog(const Message* msg, std::string* l) {} |
[email protected] | 7a4de7a6 | 2010-08-17 18:38:24 | [diff] [blame] | 1095 | |
| 1096 | template <class ReplyParamType> |
| 1097 | inline void LogReplyParamsToMessage(const ReplyParamType& reply_params, |
| 1098 | const Message* msg) {} |
| 1099 | |
| 1100 | inline void ConnectMessageAndReply(const Message* msg, Message* reply) {} |
| 1101 | #endif |
| 1102 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1103 | // This class assumes that its template argument is a RefTuple (a Tuple with |
[email protected] | 7a4de7a6 | 2010-08-17 18:38:24 | [diff] [blame] | 1104 | // reference elements). This would go into ipc_message_utils_impl.h, but it is |
| 1105 | // also used by chrome_frame. |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1106 | template <class RefTuple> |
| 1107 | class ParamDeserializer : public MessageReplyDeserializer { |
| 1108 | public: |
[email protected] | e1981f43 | 2008-08-12 15:22:13 | [diff] [blame] | 1109 | explicit ParamDeserializer(const RefTuple& out) : out_(out) { } |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1110 | |
| 1111 | bool SerializeOutputParameters(const IPC::Message& msg, void* iter) { |
| 1112 | return ReadParam(&msg, &iter, &out_); |
| 1113 | } |
| 1114 | |
| 1115 | RefTuple out_; |
| 1116 | }; |
| 1117 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1118 | // Used for synchronous messages. |
[email protected] | 75e5a87 | 2009-04-02 23:56:11 | [diff] [blame] | 1119 | template <class SendParamType, class ReplyParamType> |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1120 | class MessageWithReply : public SyncMessage { |
| 1121 | public: |
[email protected] | 75e5a87 | 2009-04-02 23:56:11 | [diff] [blame] | 1122 | typedef SendParamType SendParam; |
[email protected] | 7a4de7a6 | 2010-08-17 18:38:24 | [diff] [blame] | 1123 | typedef typename TupleTypes<SendParam>::ParamTuple RefSendParam; |
[email protected] | 75e5a87 | 2009-04-02 23:56:11 | [diff] [blame] | 1124 | typedef ReplyParamType ReplyParam; |
| 1125 | |
[email protected] | 168ae92 | 2009-12-04 18:08:45 | [diff] [blame] | 1126 | MessageWithReply(int32 routing_id, uint32 type, |
[email protected] | 7a4de7a6 | 2010-08-17 18:38:24 | [diff] [blame] | 1127 | const RefSendParam& send, const ReplyParam& reply); |
| 1128 | static bool ReadSendParam(const Message* msg, SendParam* p) IPC_MSG_NOINLINE; |
| 1129 | static bool ReadReplyParam( |
| 1130 | const Message* msg, |
| 1131 | typename TupleTypes<ReplyParam>::ValueTuple* p) IPC_MSG_NOINLINE; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1132 | |
| 1133 | template<class T, class Method> |
[email protected] | 7d5c3ac | 2009-02-04 08:58:19 | [diff] [blame] | 1134 | static bool Dispatch(const Message* msg, T* obj, Method func) { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1135 | SendParam send_params; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1136 | Message* reply = GenerateReply(msg); |
| 1137 | bool error; |
[email protected] | 7a4de7a6 | 2010-08-17 18:38:24 | [diff] [blame] | 1138 | if (ReadSendParam(msg, &send_params)) { |
| 1139 | typename TupleTypes<ReplyParam>::ValueTuple reply_params; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1140 | DispatchToMethod(obj, func, send_params, &reply_params); |
| 1141 | WriteParam(reply, reply_params); |
| 1142 | error = false; |
[email protected] | 7a4de7a6 | 2010-08-17 18:38:24 | [diff] [blame] | 1143 | LogReplyParamsToMessage(reply_params, msg); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1144 | } else { |
| 1145 | NOTREACHED() << "Error deserializing message " << msg->type(); |
| 1146 | reply->set_reply_error(); |
| 1147 | error = true; |
| 1148 | } |
| 1149 | |
| 1150 | obj->Send(reply); |
| 1151 | return !error; |
| 1152 | } |
| 1153 | |
| 1154 | template<class T, class Method> |
[email protected] | 7d5c3ac | 2009-02-04 08:58:19 | [diff] [blame] | 1155 | static bool DispatchDelayReply(const Message* msg, T* obj, Method func) { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1156 | SendParam send_params; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1157 | Message* reply = GenerateReply(msg); |
| 1158 | bool error; |
[email protected] | 7a4de7a6 | 2010-08-17 18:38:24 | [diff] [blame] | 1159 | if (ReadSendParam(msg, &send_params)) { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1160 | Tuple1<Message&> t = MakeRefTuple(*reply); |
[email protected] | 7a4de7a6 | 2010-08-17 18:38:24 | [diff] [blame] | 1161 | ConnectMessageAndReply(msg, reply); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1162 | DispatchToMethod(obj, func, send_params, &t); |
| 1163 | error = false; |
| 1164 | } else { |
| 1165 | NOTREACHED() << "Error deserializing message " << msg->type(); |
| 1166 | reply->set_reply_error(); |
| 1167 | obj->Send(reply); |
| 1168 | error = true; |
| 1169 | } |
| 1170 | return !error; |
| 1171 | } |
| 1172 | |
| 1173 | template<typename TA> |
| 1174 | static void WriteReplyParams(Message* reply, TA a) { |
| 1175 | ReplyParam p(a); |
| 1176 | WriteParam(reply, p); |
| 1177 | } |
| 1178 | |
| 1179 | template<typename TA, typename TB> |
| 1180 | static void WriteReplyParams(Message* reply, TA a, TB b) { |
| 1181 | ReplyParam p(a, b); |
| 1182 | WriteParam(reply, p); |
| 1183 | } |
| 1184 | |
| 1185 | template<typename TA, typename TB, typename TC> |
| 1186 | static void WriteReplyParams(Message* reply, TA a, TB b, TC c) { |
| 1187 | ReplyParam p(a, b, c); |
| 1188 | WriteParam(reply, p); |
| 1189 | } |
| 1190 | |
| 1191 | template<typename TA, typename TB, typename TC, typename TD> |
| 1192 | static void WriteReplyParams(Message* reply, TA a, TB b, TC c, TD d) { |
| 1193 | ReplyParam p(a, b, c, d); |
| 1194 | WriteParam(reply, p); |
| 1195 | } |
| 1196 | |
| 1197 | template<typename TA, typename TB, typename TC, typename TD, typename TE> |
| 1198 | static void WriteReplyParams(Message* reply, TA a, TB b, TC c, TD d, TE e) { |
| 1199 | ReplyParam p(a, b, c, d, e); |
| 1200 | WriteParam(reply, p); |
| 1201 | } |
| 1202 | }; |
| 1203 | |
[email protected] | 7d5c3ac | 2009-02-04 08:58:19 | [diff] [blame] | 1204 | //----------------------------------------------------------------------------- |
| 1205 | |
[email protected] | 3178f4e2 | 2008-08-05 21:20:41 | [diff] [blame] | 1206 | } // namespace IPC |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1207 | |
[email protected] | 946d1b2 | 2009-07-22 23:57:21 | [diff] [blame] | 1208 | #endif // IPC_IPC_MESSAGE_UTILS_H_ |