OLD | NEW |
1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 // DevTools RPC subsystem is a simple string serialization-based rpc | 5 // DevTools RPC subsystem is a simple string serialization-based rpc |
6 // implementation. The client is responsible for defining the Rpc-enabled | 6 // implementation. The client is responsible for defining the Rpc-enabled |
7 // interface in terms of its macros: | 7 // interface in terms of its macros: |
8 // | 8 // |
9 // #define MYAPI_STRUCT(METHOD0, METHOD1, METHOD2, METHOD3, METHOD4) | 9 // #define MYAPI_STRUCT(METHOD0, METHOD1, METHOD2, METHOD3, METHOD4) |
10 // METHOD0(Method1) | 10 // METHOD0(Method1) |
(...skipping 156 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
167 | 167 |
168 #define TOOLS_RPC_DISPATCH0(Method) \ | 168 #define TOOLS_RPC_DISPATCH0(Method) \ |
169 if (method_name == #Method) { \ | 169 if (method_name == #Method) { \ |
170 delegate->Method(); \ | 170 delegate->Method(); \ |
171 return true; \ | 171 return true; \ |
172 } | 172 } |
173 | 173 |
174 #define TOOLS_RPC_DISPATCH1(Method, T1) \ | 174 #define TOOLS_RPC_DISPATCH1(Method, T1) \ |
175 if (method_name == #Method) { \ | 175 if (method_name == #Method) { \ |
176 RpcTypeTrait<T1>::DispatchType t1; \ | 176 RpcTypeTrait<T1>::DispatchType t1; \ |
177 DevToolsRpc::GetListValue(message, 2, &t1); \ | 177 DevToolsRpc::GetListValue(message, 0, &t1); \ |
178 delegate->Method( \ | 178 delegate->Method( \ |
179 RpcTypeTrait<T1>::Pass(t1)); \ | 179 RpcTypeTrait<T1>::Pass(t1)); \ |
180 return true; \ | 180 return true; \ |
181 } | 181 } |
182 | 182 |
183 #define TOOLS_RPC_DISPATCH2(Method, T1, T2) \ | 183 #define TOOLS_RPC_DISPATCH2(Method, T1, T2) \ |
184 if (method_name == #Method) { \ | 184 if (method_name == #Method) { \ |
185 RpcTypeTrait<T1>::DispatchType t1; \ | 185 RpcTypeTrait<T1>::DispatchType t1; \ |
186 RpcTypeTrait<T2>::DispatchType t2; \ | 186 RpcTypeTrait<T2>::DispatchType t2; \ |
187 DevToolsRpc::GetListValue(message, 2, &t1); \ | 187 DevToolsRpc::GetListValue(message, 0, &t1); \ |
188 DevToolsRpc::GetListValue(message, 3, &t2); \ | 188 DevToolsRpc::GetListValue(message, 1, &t2); \ |
189 delegate->Method( \ | 189 delegate->Method( \ |
190 RpcTypeTrait<T1>::Pass(t1), \ | 190 RpcTypeTrait<T1>::Pass(t1), \ |
191 RpcTypeTrait<T2>::Pass(t2) \ | 191 RpcTypeTrait<T2>::Pass(t2) \ |
192 ); \ | 192 ); \ |
193 return true; \ | 193 return true; \ |
194 } | 194 } |
195 | 195 |
196 #define TOOLS_RPC_DISPATCH3(Method, T1, T2, T3) \ | 196 #define TOOLS_RPC_DISPATCH3(Method, T1, T2, T3) \ |
197 if (method_name == #Method) { \ | 197 if (method_name == #Method) { \ |
198 RpcTypeTrait<T1>::DispatchType t1; \ | 198 RpcTypeTrait<T1>::DispatchType t1; \ |
199 RpcTypeTrait<T2>::DispatchType t2; \ | 199 RpcTypeTrait<T2>::DispatchType t2; \ |
200 RpcTypeTrait<T3>::DispatchType t3; \ | 200 RpcTypeTrait<T3>::DispatchType t3; \ |
201 DevToolsRpc::GetListValue(message, 2, &t1); \ | 201 DevToolsRpc::GetListValue(message, 0, &t1); \ |
202 DevToolsRpc::GetListValue(message, 3, &t2); \ | 202 DevToolsRpc::GetListValue(message, 1, &t2); \ |
203 DevToolsRpc::GetListValue(message, 4, &t3); \ | 203 DevToolsRpc::GetListValue(message, 2, &t3); \ |
204 delegate->Method( \ | 204 delegate->Method( \ |
205 RpcTypeTrait<T1>::Pass(t1), \ | 205 RpcTypeTrait<T1>::Pass(t1), \ |
206 RpcTypeTrait<T2>::Pass(t2), \ | 206 RpcTypeTrait<T2>::Pass(t2), \ |
207 RpcTypeTrait<T3>::Pass(t3) \ | 207 RpcTypeTrait<T3>::Pass(t3) \ |
208 ); \ | 208 ); \ |
209 return true; \ | 209 return true; \ |
210 } | 210 } |
211 | 211 |
212 #define TOOLS_RPC_DISPATCH4(Method, T1, T2, T3, T4) \ | 212 #define TOOLS_RPC_DISPATCH4(Method, T1, T2, T3, T4) \ |
213 if (method_name == #Method) { \ | 213 if (method_name == #Method) { \ |
214 RpcTypeTrait<T1>::DispatchType t1; \ | 214 RpcTypeTrait<T1>::DispatchType t1; \ |
215 RpcTypeTrait<T2>::DispatchType t2; \ | 215 RpcTypeTrait<T2>::DispatchType t2; \ |
216 RpcTypeTrait<T3>::DispatchType t3; \ | 216 RpcTypeTrait<T3>::DispatchType t3; \ |
217 RpcTypeTrait<T4>::DispatchType t4; \ | 217 RpcTypeTrait<T4>::DispatchType t4; \ |
218 DevToolsRpc::GetListValue(message, 2, &t1); \ | 218 DevToolsRpc::GetListValue(message, 0, &t1); \ |
219 DevToolsRpc::GetListValue(message, 3, &t2); \ | 219 DevToolsRpc::GetListValue(message, 1, &t2); \ |
220 DevToolsRpc::GetListValue(message, 4, &t3); \ | 220 DevToolsRpc::GetListValue(message, 2, &t3); \ |
221 DevToolsRpc::GetListValue(message, 5, &t4); \ | 221 DevToolsRpc::GetListValue(message, 3, &t4); \ |
222 delegate->Method( \ | 222 delegate->Method( \ |
223 RpcTypeTrait<T1>::Pass(t1), \ | 223 RpcTypeTrait<T1>::Pass(t1), \ |
224 RpcTypeTrait<T2>::Pass(t2), \ | 224 RpcTypeTrait<T2>::Pass(t2), \ |
225 RpcTypeTrait<T3>::Pass(t3), \ | 225 RpcTypeTrait<T3>::Pass(t3), \ |
226 RpcTypeTrait<T4>::Pass(t4) \ | 226 RpcTypeTrait<T4>::Pass(t4) \ |
227 ); \ | 227 ); \ |
228 return true; \ | 228 return true; \ |
229 } | 229 } |
230 | 230 |
231 #define TOOLS_END_RPC_DISPATCH() \ | 231 #define TOOLS_END_RPC_DISPATCH() \ |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
266 TOOLS_RPC_STUB_METHOD4) \ | 266 TOOLS_RPC_STUB_METHOD4) \ |
267 private: \ | 267 private: \ |
268 DISALLOW_COPY_AND_ASSIGN(Class##Stub); \ | 268 DISALLOW_COPY_AND_ASSIGN(Class##Stub); \ |
269 }; \ | 269 }; \ |
270 \ | 270 \ |
271 class Class##Dispatch { \ | 271 class Class##Dispatch { \ |
272 public: \ | 272 public: \ |
273 Class##Dispatch() {} \ | 273 Class##Dispatch() {} \ |
274 virtual ~Class##Dispatch() {} \ | 274 virtual ~Class##Dispatch() {} \ |
275 \ | 275 \ |
276 static bool Dispatch(Class* delegate, const std::string& raw_msg) { \ | 276 static bool Dispatch(Class* delegate, \ |
| 277 const std::string& class_name, \ |
| 278 const std::string& method_name, \ |
| 279 const std::string& raw_msg) { \ |
277 OwnPtr<ListValue> message( \ | 280 OwnPtr<ListValue> message( \ |
278 static_cast<ListValue*>(DevToolsRpc::ParseMessage(raw_msg))); \ | 281 static_cast<ListValue*>(DevToolsRpc::ParseMessage(raw_msg))); \ |
279 return Dispatch(delegate, *message.get()); \ | 282 return Dispatch(delegate, class_name, method_name, *message.get()); \ |
280 } \ | 283 } \ |
281 \ | 284 \ |
282 static bool Dispatch(Class* delegate, const ListValue& message) { \ | 285 static bool Dispatch(Class* delegate, \ |
283 std::string class_name; \ | 286 const std::string& class_name, \ |
284 message.GetString(0, &class_name); \ | 287 const std::string& method_name, \ |
| 288 const ListValue& message) { \ |
285 if (class_name != #Class) { \ | 289 if (class_name != #Class) { \ |
286 return false; \ | 290 return false; \ |
287 } \ | 291 } \ |
288 std::string method_name; \ | |
289 message.GetString(1, &method_name); \ | |
290 typedef Class CLASS; \ | 292 typedef Class CLASS; \ |
291 STRUCT( \ | 293 STRUCT( \ |
292 TOOLS_RPC_DISPATCH0, \ | 294 TOOLS_RPC_DISPATCH0, \ |
293 TOOLS_RPC_DISPATCH1, \ | 295 TOOLS_RPC_DISPATCH1, \ |
294 TOOLS_RPC_DISPATCH2, \ | 296 TOOLS_RPC_DISPATCH2, \ |
295 TOOLS_RPC_DISPATCH3, \ | 297 TOOLS_RPC_DISPATCH3, \ |
296 TOOLS_RPC_DISPATCH4) \ | 298 TOOLS_RPC_DISPATCH4) \ |
297 return false; \ | 299 return false; \ |
298 } \ | 300 } \ |
299 private: \ | 301 private: \ |
300 DISALLOW_COPY_AND_ASSIGN(Class##Dispatch); \ | 302 DISALLOW_COPY_AND_ASSIGN(Class##Dispatch); \ |
301 }; | 303 }; |
302 | 304 |
303 /////////////////////////////////////////////////////// | 305 /////////////////////////////////////////////////////// |
304 // RPC base class | 306 // RPC base class |
305 class DevToolsRpc { | 307 class DevToolsRpc { |
306 public: | 308 public: |
307 class Delegate { | 309 class Delegate { |
308 public: | 310 public: |
309 Delegate() {} | 311 Delegate() {} |
310 virtual ~Delegate() {} | 312 virtual ~Delegate() {} |
311 virtual void SendRpcMessage(const std::string& msg) = 0; | 313 virtual void SendRpcMessage(const std::string& class_name, |
| 314 const std::string& method_name, |
| 315 const std::string& raw_msg) = 0; |
312 private: | 316 private: |
313 DISALLOW_COPY_AND_ASSIGN(Delegate); | 317 DISALLOW_COPY_AND_ASSIGN(Delegate); |
314 }; | 318 }; |
315 | 319 |
316 explicit DevToolsRpc(Delegate* delegate); | 320 explicit DevToolsRpc(Delegate* delegate); |
317 virtual ~DevToolsRpc(); | 321 virtual ~DevToolsRpc(); |
318 | 322 |
319 void InvokeAsync( | 323 void InvokeAsync( |
320 const std::string& class_name, | 324 const std::string& class_name, |
321 const std::string& method_name) { | 325 const std::string& method_name) { |
322 ListValue message; | 326 ListValue message; |
323 message.Append(CreateValue(&class_name)); | 327 SendValueMessage(class_name, method_name, message); |
324 message.Append(CreateValue(&method_name)); | |
325 SendValueMessage(message); | |
326 } | 328 } |
327 | 329 |
328 template<typename T1> | 330 template<typename T1> |
329 void InvokeAsync( | 331 void InvokeAsync( |
330 const std::string& class_name, | 332 const std::string& class_name, |
331 const std::string& method_name, | 333 const std::string& method_name, |
332 T1 t1) { | 334 T1 t1) { |
333 ListValue message; | 335 ListValue message; |
334 message.Append(CreateValue(&class_name)); | |
335 message.Append(CreateValue(&method_name)); | |
336 message.Append(CreateValue(t1)); | 336 message.Append(CreateValue(t1)); |
337 SendValueMessage(message); | 337 SendValueMessage(class_name, method_name, message); |
338 } | 338 } |
339 | 339 |
340 template<typename T1, typename T2> | 340 template<typename T1, typename T2> |
341 void InvokeAsync( | 341 void InvokeAsync( |
342 const std::string& class_name, | 342 const std::string& class_name, |
343 const std::string& method_name, | 343 const std::string& method_name, |
344 T1 t1, T2 t2) { | 344 T1 t1, T2 t2) { |
345 ListValue message; | 345 ListValue message; |
346 message.Append(CreateValue(&class_name)); | |
347 message.Append(CreateValue(&method_name)); | |
348 message.Append(CreateValue(t1)); | 346 message.Append(CreateValue(t1)); |
349 message.Append(CreateValue(t2)); | 347 message.Append(CreateValue(t2)); |
350 SendValueMessage(message); | 348 SendValueMessage(class_name, method_name, message); |
351 } | 349 } |
352 | 350 |
353 template<typename T1, typename T2, typename T3> | 351 template<typename T1, typename T2, typename T3> |
354 void InvokeAsync( | 352 void InvokeAsync( |
355 const std::string& class_name, | 353 const std::string& class_name, |
356 const std::string& method_name, | 354 const std::string& method_name, |
357 T1 t1, T2 t2, T3 t3) { | 355 T1 t1, T2 t2, T3 t3) { |
358 ListValue message; | 356 ListValue message; |
359 message.Append(CreateValue(&class_name)); | |
360 message.Append(CreateValue(&method_name)); | |
361 message.Append(CreateValue(t1)); | 357 message.Append(CreateValue(t1)); |
362 message.Append(CreateValue(t2)); | 358 message.Append(CreateValue(t2)); |
363 message.Append(CreateValue(t3)); | 359 message.Append(CreateValue(t3)); |
364 SendValueMessage(message); | 360 SendValueMessage(class_name, method_name, message); |
365 } | 361 } |
366 | 362 |
367 template<typename T1, typename T2, typename T3, typename T4> | 363 template<typename T1, typename T2, typename T3, typename T4> |
368 void InvokeAsync( | 364 void InvokeAsync( |
369 const std::string& class_name, | 365 const std::string& class_name, |
370 const std::string& method_name, | 366 const std::string& method_name, |
371 T1 t1, T2 t2, T3 t3, T4 t4) { | 367 T1 t1, T2 t2, T3 t3, T4 t4) { |
372 ListValue message; | 368 ListValue message; |
373 message.Append(CreateValue(&class_name)); | |
374 message.Append(CreateValue(&method_name)); | |
375 message.Append(CreateValue(t1)); | 369 message.Append(CreateValue(t1)); |
376 message.Append(CreateValue(t2)); | 370 message.Append(CreateValue(t2)); |
377 message.Append(CreateValue(t3)); | 371 message.Append(CreateValue(t3)); |
378 message.Append(CreateValue(t4)); | 372 message.Append(CreateValue(t4)); |
379 SendValueMessage(message); | 373 SendValueMessage(class_name, method_name, message); |
380 } | 374 } |
381 | 375 |
382 static Value* ParseMessage(const std::string& raw_msg); | 376 static Value* ParseMessage(const std::string& raw_msg); |
383 static std::string Serialize(const Value& value); | 377 static std::string Serialize(const Value& value); |
384 static void GetListValue(const ListValue& message, int index, bool* value); | 378 static void GetListValue(const ListValue& message, int index, bool* value); |
385 static void GetListValue(const ListValue& message, int index, int* value); | 379 static void GetListValue(const ListValue& message, int index, int* value); |
386 static void GetListValue( | 380 static void GetListValue( |
387 const ListValue& message, | 381 const ListValue& message, |
388 int index, | 382 int index, |
389 String* value); | 383 String* value); |
390 static void GetListValue( | 384 static void GetListValue( |
391 const ListValue& message, | 385 const ListValue& message, |
392 int index, | 386 int index, |
393 std::string* value); | 387 std::string* value); |
394 static void GetListValue(const ListValue& message, int index, Value** value); | 388 static void GetListValue(const ListValue& message, int index, Value** value); |
395 | 389 |
396 protected: | 390 protected: |
397 // Primarily for unit testing. | 391 // Primarily for unit testing. |
398 void set_delegate(Delegate* delegate) { this->delegate_ = delegate; } | 392 void set_delegate(Delegate* delegate) { this->delegate_ = delegate; } |
399 | 393 |
400 private: | 394 private: |
401 // Value adapters for supported Rpc types. | 395 // Value adapters for supported Rpc types. |
402 static Value* CreateValue(const String* value); | 396 static Value* CreateValue(const String* value); |
403 static Value* CreateValue(const std::string* value); | 397 static Value* CreateValue(const std::string* value); |
404 static Value* CreateValue(int* value); | 398 static Value* CreateValue(int* value); |
405 static Value* CreateValue(bool* value); | 399 static Value* CreateValue(bool* value); |
406 static Value* CreateValue(const Value* value); | 400 static Value* CreateValue(const Value* value); |
407 | 401 |
408 void SendValueMessage(const Value& value); | 402 void SendValueMessage(const std::string& class_name, |
| 403 const std::string& method_name, |
| 404 const Value& value); |
409 | 405 |
410 Delegate* delegate_; | 406 Delegate* delegate_; |
411 DISALLOW_COPY_AND_ASSIGN(DevToolsRpc); | 407 DISALLOW_COPY_AND_ASSIGN(DevToolsRpc); |
412 }; | 408 }; |
413 | 409 |
414 #endif // WEBKIT_GLUE_DEVTOOLS_DEVTOOLS_RPC_H_ | 410 #endif // WEBKIT_GLUE_DEVTOOLS_DEVTOOLS_RPC_H_ |
OLD | NEW |