summaryrefslogtreecommitdiffstats
path: root/webkit/glue/devtools/devtools_rpc.h
blob: b61caf55195df48818f2cae51e643f078554c3a0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
// Copyright (c) 2009 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

// DevTools RPC subsystem is a simple string serialization-based rpc
// implementation. The client is responsible for defining the Rpc-enabled
// interface in terms of its macros:
//
// #define MYAPI_STRUCT(METHOD0, METHOD1, METHOD2, METHOD3, METHOD4)
//   METHOD0(Method1)
//   METHOD3(Method2, int, String, Value)
//   METHOD1(Method3, int)
// (snippet above should be multiline macro, add trailing backslashes)
//
// DEFINE_RPC_CLASS(MyApi, MYAPI_STRUCT)
//
// The snippet above will generate three classes: MyApi, MyApiStub and
// MyApiDispatch.
//
// 1. For each method defined in the marco MyApi will have a
// pure virtual function generated, so that MyApi would look like:
//
// class MyApi {
//  private:
//   MyApi() {}
//   ~MyApi() {}
//   virtual void Method1() = 0;
//   virtual void Method2(
//       int param1,
//       const String& param2,
//       const Value& param3) = 0;
//   virtual void Method3(int param1) = 0;
// };
//
// 2. MyApiStub will implement MyApi interface and would serialize all calls
// into the string-based calls of the underlying transport:
//
// DevToolsRpc::Delegate* transport;
// my_api = new MyApiStub(transport);
// my_api->Method1();
// my_api->Method3(2);
//
// 3. MyApiDelegate is capable of dispatching the calls and convert them to the
// calls to the underlying MyApi methods:
//
// MyApi* real_object;
// MyApiDispatch::Dispatch(real_object, raw_string_call_generated_by_stub);
//
// will make corresponding calls to the real object.

#ifndef WEBKIT_GLUE_DEVTOOLS_DEVTOOLS_RPC_H_
#define WEBKIT_GLUE_DEVTOOLS_DEVTOOLS_RPC_H_

#include <string>

#include <wtf/OwnPtr.h>

#include "base/basictypes.h"
#include "base/logging.h"
#include "base/values.h"

namespace WebCore {
class String;
}

using WebCore::String;

///////////////////////////////////////////////////////
// RPC dispatch macro

template<typename T>
struct RpcTypeTrait {
  typedef T ApiType;
  typedef T DispatchType;
  static const DispatchType& Pass(const DispatchType& t) {
    return t;
  }
};

template<>
struct RpcTypeTrait<Value> {
  typedef const Value& ApiType;
  typedef Value* DispatchType;
  static const Value& Pass(Value* t) {
    return *t;
  }
};

template<>
struct RpcTypeTrait<String> {
  typedef const String& ApiType;
  typedef String DispatchType;
  static const DispatchType& Pass(const DispatchType& t) {
    return t;
  }
};

template<>
struct RpcTypeTrait<std::string> {
  typedef const std::string& ApiType;
  typedef std::string DispatchType;
  static const DispatchType& Pass(const DispatchType& t) {
    return t;
  }
};

///////////////////////////////////////////////////////
// RPC Api method declarations

#define TOOLS_RPC_API_METHOD0(Method) \
  virtual void Method() = 0;

#define TOOLS_RPC_API_METHOD1(Method, T1) \
  virtual void Method(RpcTypeTrait<T1>::ApiType t1) = 0;

#define TOOLS_RPC_API_METHOD2(Method, T1, T2) \
  virtual void Method(RpcTypeTrait<T1>::ApiType t1, \
                      RpcTypeTrait<T2>::ApiType t2) = 0;

#define TOOLS_RPC_API_METHOD3(Method, T1, T2, T3) \
  virtual void Method(RpcTypeTrait<T1>::ApiType t1, \
                      RpcTypeTrait<T2>::ApiType t2, \
                      RpcTypeTrait<T3>::ApiType t3) = 0;

#define TOOLS_RPC_API_METHOD4(Method, T1, T2, T3, T4) \
  virtual void Method(RpcTypeTrait<T1>::ApiType t1, \
                      RpcTypeTrait<T2>::ApiType t2, \
                      RpcTypeTrait<T3>::ApiType t3, \
                      RpcTypeTrait<T4>::ApiType t4) = 0;

///////////////////////////////////////////////////////
// RPC stub method implementations

#define TOOLS_RPC_STUB_METHOD0(Method) \
  virtual void Method() { \
    InvokeAsync(class_name, #Method); \
  }

#define TOOLS_RPC_STUB_METHOD1(Method, T1) \
  virtual void Method(RpcTypeTrait<T1>::ApiType t1) { \
    InvokeAsync(class_name, #Method, &t1); \
  }

#define TOOLS_RPC_STUB_METHOD2(Method, T1, T2) \
  virtual void Method(RpcTypeTrait<T1>::ApiType t1, \
                      RpcTypeTrait<T2>::ApiType t2) { \
    InvokeAsync(class_name, #Method, &t1, &t2); \
  }

#define TOOLS_RPC_STUB_METHOD3(Method, T1, T2, T3) \
  virtual void Method(RpcTypeTrait<T1>::ApiType t1, \
                      RpcTypeTrait<T2>::ApiType t2, \
                      RpcTypeTrait<T3>::ApiType t3) { \
    InvokeAsync(class_name, #Method, &t1, &t2, &t3); \
  }

#define TOOLS_RPC_STUB_METHOD4(Method, T1, T2, T3, T4) \
  virtual void Method(RpcTypeTrait<T1>::ApiType t1, \
                      RpcTypeTrait<T2>::ApiType t2, \
                      RpcTypeTrait<T3>::ApiType t3, \
                      RpcTypeTrait<T4>::ApiType t4) { \
    InvokeAsync(class_name, #Method, &t1, &t2, &t3, &t4); \
  }

///////////////////////////////////////////////////////
// RPC dispatch method implementations

#define TOOLS_RPC_DISPATCH0(Method) \
if (method_name == #Method) { \
  delegate->Method(); \
  return true; \
}

#define TOOLS_RPC_DISPATCH1(Method, T1) \
if (method_name == #Method) { \
  RpcTypeTrait<T1>::DispatchType t1; \
  DevToolsRpc::GetListValue(message, 0, &t1); \
  delegate->Method( \
      RpcTypeTrait<T1>::Pass(t1)); \
  return true; \
}

#define TOOLS_RPC_DISPATCH2(Method, T1, T2) \
if (method_name == #Method) { \
  RpcTypeTrait<T1>::DispatchType t1; \
  RpcTypeTrait<T2>::DispatchType t2; \
  DevToolsRpc::GetListValue(message, 0, &t1); \
  DevToolsRpc::GetListValue(message, 1, &t2); \
  delegate->Method( \
      RpcTypeTrait<T1>::Pass(t1), \
      RpcTypeTrait<T2>::Pass(t2) \
  ); \
  return true; \
}

#define TOOLS_RPC_DISPATCH3(Method, T1, T2, T3) \
if (method_name == #Method) { \
  RpcTypeTrait<T1>::DispatchType t1; \
  RpcTypeTrait<T2>::DispatchType t2; \
  RpcTypeTrait<T3>::DispatchType t3; \
  DevToolsRpc::GetListValue(message, 0, &t1); \
  DevToolsRpc::GetListValue(message, 1, &t2); \
  DevToolsRpc::GetListValue(message, 2, &t3); \
  delegate->Method( \
      RpcTypeTrait<T1>::Pass(t1), \
      RpcTypeTrait<T2>::Pass(t2), \
      RpcTypeTrait<T3>::Pass(t3) \
  ); \
  return true; \
}

#define TOOLS_RPC_DISPATCH4(Method, T1, T2, T3, T4) \
if (method_name == #Method) { \
  RpcTypeTrait<T1>::DispatchType t1; \
  RpcTypeTrait<T2>::DispatchType t2; \
  RpcTypeTrait<T3>::DispatchType t3; \
  RpcTypeTrait<T4>::DispatchType t4; \
  DevToolsRpc::GetListValue(message, 0, &t1); \
  DevToolsRpc::GetListValue(message, 1, &t2); \
  DevToolsRpc::GetListValue(message, 2, &t3); \
  DevToolsRpc::GetListValue(message, 3, &t4); \
  delegate->Method( \
      RpcTypeTrait<T1>::Pass(t1), \
      RpcTypeTrait<T2>::Pass(t2), \
      RpcTypeTrait<T3>::Pass(t3), \
      RpcTypeTrait<T4>::Pass(t4) \
  ); \
  return true; \
}

#define TOOLS_END_RPC_DISPATCH() \
}

// This macro defines three classes: Class with the Api, ClassStub that is
// serializing method calls and ClassDispatch that is capable of dispatching
// the serialized message into its delegate.
#define DEFINE_RPC_CLASS(Class, STRUCT) \
class Class {\
 public: \
  Class() { \
    class_name = #Class; \
  } \
  ~Class() {} \
  \
  STRUCT( \
      TOOLS_RPC_API_METHOD0, \
      TOOLS_RPC_API_METHOD1, \
      TOOLS_RPC_API_METHOD2, \
      TOOLS_RPC_API_METHOD3, \
      TOOLS_RPC_API_METHOD4) \
  std::string class_name; \
 private: \
  DISALLOW_COPY_AND_ASSIGN(Class); \
}; \
\
class Class##Stub : public Class, public DevToolsRpc { \
 public: \
  explicit Class##Stub(Delegate* delegate) : DevToolsRpc(delegate) {} \
  virtual ~Class##Stub() {} \
  typedef Class CLASS; \
  STRUCT( \
      TOOLS_RPC_STUB_METHOD0, \
      TOOLS_RPC_STUB_METHOD1, \
      TOOLS_RPC_STUB_METHOD2, \
      TOOLS_RPC_STUB_METHOD3, \
      TOOLS_RPC_STUB_METHOD4) \
 private: \
  DISALLOW_COPY_AND_ASSIGN(Class##Stub); \
}; \
\
class Class##Dispatch { \
 public: \
  Class##Dispatch() {} \
  virtual ~Class##Dispatch() {} \
  \
  static bool Dispatch(Class* delegate, \
                       const std::string& class_name, \
                       const std::string& method_name, \
                       const std::string& raw_msg) { \
    OwnPtr<ListValue> message( \
        static_cast<ListValue*>(DevToolsRpc::ParseMessage(raw_msg))); \
    return Dispatch(delegate, class_name, method_name, *message.get()); \
  } \
  \
  static bool Dispatch(Class* delegate, \
                       const std::string& class_name, \
                       const std::string& method_name, \
                       const ListValue& message) { \
    if (class_name != #Class) { \
      return false; \
    } \
    typedef Class CLASS; \
    STRUCT( \
        TOOLS_RPC_DISPATCH0, \
        TOOLS_RPC_DISPATCH1, \
        TOOLS_RPC_DISPATCH2, \
        TOOLS_RPC_DISPATCH3, \
        TOOLS_RPC_DISPATCH4) \
    return false; \
  } \
 private: \
  DISALLOW_COPY_AND_ASSIGN(Class##Dispatch); \
};

///////////////////////////////////////////////////////
// RPC base class
class DevToolsRpc {
 public:
  class Delegate {
   public:
    Delegate() {}
    virtual ~Delegate() {}
    virtual void SendRpcMessage(const std::string& class_name,
                                const std::string& method_name,
                                const std::string& raw_msg) = 0;
   private:
    DISALLOW_COPY_AND_ASSIGN(Delegate);
  };

  explicit DevToolsRpc(Delegate* delegate);
  virtual ~DevToolsRpc();

  void InvokeAsync(
      const std::string& class_name,
      const std::string& method_name) {
    ListValue message;
    SendValueMessage(class_name, method_name, message);
  }

  template<typename T1>
  void InvokeAsync(
      const std::string& class_name,
      const std::string& method_name,
      T1 t1) {
    ListValue message;
    message.Append(CreateValue(t1));
    SendValueMessage(class_name, method_name, message);
  }

  template<typename T1, typename T2>
  void InvokeAsync(
      const std::string& class_name,
      const std::string& method_name,
      T1 t1, T2 t2) {
    ListValue message;
    message.Append(CreateValue(t1));
    message.Append(CreateValue(t2));
    SendValueMessage(class_name, method_name, message);
  }

  template<typename T1, typename T2, typename T3>
  void InvokeAsync(
      const std::string& class_name,
      const std::string& method_name,
      T1 t1, T2 t2, T3 t3) {
    ListValue message;
    message.Append(CreateValue(t1));
    message.Append(CreateValue(t2));
    message.Append(CreateValue(t3));
    SendValueMessage(class_name, method_name, message);
  }

  template<typename T1, typename T2, typename T3, typename T4>
  void InvokeAsync(
      const std::string& class_name,
      const std::string& method_name,
      T1 t1, T2 t2, T3 t3, T4 t4) {
    ListValue message;
    message.Append(CreateValue(t1));
    message.Append(CreateValue(t2));
    message.Append(CreateValue(t3));
    message.Append(CreateValue(t4));
    SendValueMessage(class_name, method_name, message);
  }

  static Value* ParseMessage(const std::string& raw_msg);
  static std::string Serialize(const Value& value);
  static void GetListValue(const ListValue& message, int index, bool* value);
  static void GetListValue(const ListValue& message, int index, int* value);
  static void GetListValue(
      const ListValue& message,
      int index,
      String* value);
  static void GetListValue(
      const ListValue& message,
      int index,
      std::string* value);
  static void GetListValue(const ListValue& message, int index, Value** value);

 protected:
  // Primarily for unit testing.
  void set_delegate(Delegate* delegate) { this->delegate_ = delegate; }

 private:
  // Value adapters for supported Rpc types.
  static Value* CreateValue(const String* value);
  static Value* CreateValue(const std::string* value);
  static Value* CreateValue(int* value);
  static Value* CreateValue(bool* value);
  static Value* CreateValue(const Value* value);

  void SendValueMessage(const std::string& class_name,
                        const std::string& method_name,
                        const Value& value);

  Delegate* delegate_;
  DISALLOW_COPY_AND_ASSIGN(DevToolsRpc);
};

#endif  // WEBKIT_GLUE_DEVTOOLS_DEVTOOLS_RPC_H_