diff options
-rw-r--r-- | webkit/glue/devtools/devtools_rpc.h | 144 | ||||
-rw-r--r-- | webkit/glue/devtools/devtools_rpc_js.h | 48 | ||||
-rw-r--r-- | webkit/glue/devtools/devtools_rpc_unittest.cc | 21 |
3 files changed, 89 insertions, 124 deletions
diff --git a/webkit/glue/devtools/devtools_rpc.h b/webkit/glue/devtools/devtools_rpc.h index 1fca50d..8a28d0b 100644 --- a/webkit/glue/devtools/devtools_rpc.h +++ b/webkit/glue/devtools/devtools_rpc.h @@ -128,37 +128,30 @@ struct RpcTypeTrait<std::string> { RpcTypeTrait<T3>::ApiType t3, \ RpcTypeTrait<T4>::ApiType t4) = 0; -#define TOOLS_RPC_ENUM_LITERAL0(Method) METHOD_##Method, -#define TOOLS_RPC_ENUM_LITERAL1(Method, T1) METHOD_##Method, -#define TOOLS_RPC_ENUM_LITERAL2(Method, T1, T2) METHOD_##Method, -#define TOOLS_RPC_ENUM_LITERAL3(Method, T1, T2, T3) METHOD_##Method, -#define TOOLS_RPC_ENUM_LITERAL4(Method, T1, T2, T3, T4) METHOD_##Method, - /////////////////////////////////////////////////////// // RPC stub method implementations #define TOOLS_RPC_STUB_METHOD0(Method) \ virtual void Method() { \ - InvokeAsync(RpcTypeToNumber<CLASS>::number, METHOD_##Method); \ + InvokeAsync(class_name, #Method); \ } #define TOOLS_RPC_STUB_METHOD1(Method, T1) \ virtual void Method(RpcTypeTrait<T1>::ApiType t1) { \ - InvokeAsync(RpcTypeToNumber<CLASS>::number, METHOD_##Method, &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(RpcTypeToNumber<CLASS>::number, METHOD_##Method, &t1, &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(RpcTypeToNumber<CLASS>::number, METHOD_##Method, &t1, &t2, \ - &t3); \ + InvokeAsync(class_name, #Method, &t1, &t2, &t3); \ } #define TOOLS_RPC_STUB_METHOD4(Method, T1, T2, T3, T4) \ @@ -166,21 +159,20 @@ struct RpcTypeTrait<std::string> { RpcTypeTrait<T2>::ApiType t2, \ RpcTypeTrait<T3>::ApiType t3, \ RpcTypeTrait<T4>::ApiType t4) { \ - InvokeAsync(RpcTypeToNumber<CLASS>::number, METHOD_##Method, &t1, &t2, \ - &t3, &t4); \ + InvokeAsync(class_name, #Method, &t1, &t2, &t3, &t4); \ } /////////////////////////////////////////////////////// // RPC dispatch method implementations #define TOOLS_RPC_DISPATCH0(Method) \ -case CLASS::METHOD_##Method: { \ +if (method_name == #Method) { \ delegate->Method(); \ return true; \ } #define TOOLS_RPC_DISPATCH1(Method, T1) \ -case CLASS::METHOD_##Method: { \ +if (method_name == #Method) { \ RpcTypeTrait<T1>::DispatchType t1; \ DevToolsRpc::GetListValue(message, 2, &t1); \ delegate->Method( \ @@ -189,7 +181,7 @@ case CLASS::METHOD_##Method: { \ } #define TOOLS_RPC_DISPATCH2(Method, T1, T2) \ -case CLASS::METHOD_##Method: { \ +if (method_name == #Method) { \ RpcTypeTrait<T1>::DispatchType t1; \ RpcTypeTrait<T2>::DispatchType t2; \ DevToolsRpc::GetListValue(message, 2, &t1); \ @@ -202,7 +194,7 @@ case CLASS::METHOD_##Method: { \ } #define TOOLS_RPC_DISPATCH3(Method, T1, T2, T3) \ -case CLASS::METHOD_##Method: { \ +if (method_name == #Method) { \ RpcTypeTrait<T1>::DispatchType t1; \ RpcTypeTrait<T2>::DispatchType t2; \ RpcTypeTrait<T3>::DispatchType t3; \ @@ -218,7 +210,7 @@ case CLASS::METHOD_##Method: { \ } #define TOOLS_RPC_DISPATCH4(Method, T1, T2, T3, T4) \ -case CLASS::METHOD_##Method: { \ +if (method_name == #Method) { \ RpcTypeTrait<T1>::DispatchType t1; \ RpcTypeTrait<T2>::DispatchType t2; \ RpcTypeTrait<T3>::DispatchType t3; \ @@ -245,24 +237,18 @@ case CLASS::METHOD_##Method: { \ #define DEFINE_RPC_CLASS(Class, STRUCT) \ class Class {\ public: \ - Class() {} \ + Class() { \ + class_name = #Class; \ + } \ ~Class() {} \ \ - enum MethodNames { \ - STRUCT( \ - TOOLS_RPC_ENUM_LITERAL0, \ - TOOLS_RPC_ENUM_LITERAL1, \ - TOOLS_RPC_ENUM_LITERAL2, \ - TOOLS_RPC_ENUM_LITERAL3, \ - TOOLS_RPC_ENUM_LITERAL4) \ - }; \ - \ 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); \ }; \ @@ -294,50 +280,27 @@ class Class##Dispatch { \ } \ \ static bool Dispatch(Class* delegate, const ListValue& message) { \ - int class_id; \ - message.GetInteger(0, &class_id); \ - if (class_id != RpcTypeToNumber<Class>::number) { \ + std::string class_name; \ + message.GetString(0, &class_name); \ + if (class_name != #Class) { \ return false; \ } \ - int method; \ - message.GetInteger(1, &method); \ + std::string method_name; \ + message.GetString(1, &method_name); \ typedef Class CLASS; \ - switch (method) { \ - STRUCT( \ - TOOLS_RPC_DISPATCH0, \ - TOOLS_RPC_DISPATCH1, \ - TOOLS_RPC_DISPATCH2, \ - TOOLS_RPC_DISPATCH3, \ - TOOLS_RPC_DISPATCH4) \ - default: return false; \ - } \ + 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); \ }; /////////////////////////////////////////////////////// -// Following templates allow mapping types to numbers. - -template <typename T> -class RpcTypeToNumberCounter { - public: - static int next_number_; -}; -template <typename T> -int RpcTypeToNumberCounter<T>::next_number_ = 0; - -template <typename T> -class RpcTypeToNumber { - public: - static const int number; -}; - -template <typename T> -const int RpcTypeToNumber<T>::number = - RpcTypeToNumberCounter<void>::next_number_++; - -/////////////////////////////////////////////////////// // RPC base class class DevToolsRpc { public: @@ -353,45 +316,62 @@ class DevToolsRpc { explicit DevToolsRpc(Delegate* delegate); virtual ~DevToolsRpc(); - void InvokeAsync(int class_id, int method) { + void InvokeAsync( + const std::string& class_name, + const std::string& method_name) { ListValue message; - message.Append(CreateValue(&class_id)); - message.Append(CreateValue(&method)); + message.Append(CreateValue(&class_name)); + message.Append(CreateValue(&method_name)); SendValueMessage(message); } - template<class T1> - void InvokeAsync(int class_id, int method, T1 t1) { + + template<typename T1> + void InvokeAsync( + const std::string& class_name, + const std::string& method_name, + T1 t1) { ListValue message; - message.Append(CreateValue(&class_id)); - message.Append(CreateValue(&method)); + message.Append(CreateValue(&class_name)); + message.Append(CreateValue(&method_name)); message.Append(CreateValue(t1)); SendValueMessage(message); } - template<class T1, class T2> - void InvokeAsync(int class_id, int method, T1 t1, T2 t2) { + + 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(&class_id)); - message.Append(CreateValue(&method)); + message.Append(CreateValue(&class_name)); + message.Append(CreateValue(&method_name)); message.Append(CreateValue(t1)); message.Append(CreateValue(t2)); SendValueMessage(message); } - template<class T1, class T2, class T3> - void InvokeAsync(int class_id, int method, T1 t1, T2 t2, T3 t3) { + + 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(&class_id)); - message.Append(CreateValue(&method)); + message.Append(CreateValue(&class_name)); + message.Append(CreateValue(&method_name)); message.Append(CreateValue(t1)); message.Append(CreateValue(t2)); message.Append(CreateValue(t3)); SendValueMessage(message); } - template<class T1, class T2, class T3, class T4> - void InvokeAsync(int class_id, int method, T1 t1, T2 t2, T3 t3, T4 t4) { + 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(&class_id)); - message.Append(CreateValue(&method)); + message.Append(CreateValue(&class_name)); + message.Append(CreateValue(&method_name)); message.Append(CreateValue(t1)); message.Append(CreateValue(t2)); message.Append(CreateValue(t3)); diff --git a/webkit/glue/devtools/devtools_rpc_js.h b/webkit/glue/devtools/devtools_rpc_js.h index 6dffeca..e70672dd8c 100644 --- a/webkit/glue/devtools/devtools_rpc_js.h +++ b/webkit/glue/devtools/devtools_rpc_js.h @@ -71,14 +71,14 @@ struct RpcJsTypeTrait<std::string> { #define TOOLS_RPC_JS_STUB_METHOD0(Method) \ void Js##Method(const CppArgumentList& args, CppVariant* result) { \ - InvokeAsync(RpcTypeToNumber<CLASS>::number, METHOD_##Method); \ + InvokeAsync(class_name, #Method); \ result->SetNull(); \ } #define TOOLS_RPC_JS_STUB_METHOD1(Method, T1) \ void Js##Method(const CppArgumentList& args, CppVariant* result) { \ T1 t1 = RpcJsTypeTrait<T1>::Pass(args[0]); \ - InvokeAsync(RpcTypeToNumber<CLASS>::number, METHOD_##Method, &t1); \ + InvokeAsync(class_name, #Method, &t1); \ result->SetNull(); \ } @@ -86,7 +86,7 @@ struct RpcJsTypeTrait<std::string> { void Js##Method(const CppArgumentList& args, CppVariant* result) { \ T1 t1 = RpcJsTypeTrait<T1>::Pass(args[0]); \ T2 t2 = RpcJsTypeTrait<T2>::Pass(args[1]); \ - InvokeAsync(RpcTypeToNumber<CLASS>::number, METHOD_##Method, &t1, &t2); \ + InvokeAsync(class_name, #Method, &t1, &t2); \ result->SetNull(); \ } @@ -95,8 +95,7 @@ struct RpcJsTypeTrait<std::string> { T1 t1 = RpcJsTypeTrait<T1>::Pass(args[0]); \ T2 t2 = RpcJsTypeTrait<T2>::Pass(args[1]); \ T3 t3 = RpcJsTypeTrait<T3>::Pass(args[2]); \ - InvokeAsync(RpcTypeToNumber<CLASS>::number, METHOD_##Method, &t1, &t2, \ - &t3); \ + InvokeAsync(class_name, #Method, &t1, &t2, &t3); \ result->SetNull(); \ } @@ -106,8 +105,7 @@ struct RpcJsTypeTrait<std::string> { T2 t2 = RpcJsTypeTrait<T2>::Pass(args[1]); \ T3 t3 = RpcJsTypeTrait<T3>::Pass(args[2]); \ T4 t4 = RpcJsTypeTrait<T4>::Pass(args[3]); \ - InvokeAsync(RpcTypeToNumber<CLASS>::number, METHOD_##Method, &t1, &t2, \ - &t3, &t4); \ + InvokeAsync(class_name, #Method, &t1, &t2, &t3, &t4); \ result->SetNull(); \ } @@ -115,13 +113,13 @@ struct RpcJsTypeTrait<std::string> { // JS RPC dispatch method implementations #define TOOLS_RPC_JS_DISPATCH0(Method) \ -case CLASS::METHOD_##Method: { \ +if (method_name == #Method) { \ *expr = StringPrintf("%s.%s()", js_obj.c_str(), #Method); \ return true; \ } #define TOOLS_RPC_JS_DISPATCH1(Method, T1) \ -case CLASS::METHOD_##Method: { \ +if (method_name == #Method) { \ Value* t1; \ message.Get(2, &t1); \ *expr = StringPrintf("%s.%s(%s)", js_obj.c_str(), #Method, \ @@ -130,7 +128,7 @@ case CLASS::METHOD_##Method: { \ } #define TOOLS_RPC_JS_DISPATCH2(Method, T1, T2) \ -case CLASS::METHOD_##Method: { \ +if (method_name == #Method) { \ Value* t1; \ Value* t2; \ message.Get(2, &t1); \ @@ -142,7 +140,7 @@ case CLASS::METHOD_##Method: { \ } #define TOOLS_RPC_JS_DISPATCH3(Method, T1, T2, T3) \ -case CLASS::METHOD_##Method: { \ +if (method_name == #Method) { \ Value* t1; \ Value* t2; \ Value* t3; \ @@ -157,7 +155,7 @@ case CLASS::METHOD_##Method: { \ } #define TOOLS_RPC_JS_DISPATCH4(Method, T1, T2, T3, T4) \ -case CLASS::METHOD_##Method: { \ +if (method_name == #Method) { \ Value* t1; \ Value* t2; \ Value* t3; \ @@ -182,23 +180,21 @@ class Js##Class##Dispatch { \ virtual ~Js##Class##Dispatch() {} \ \ bool Dispatch(const ListValue& message, std::string* expr) { \ - int class_id; \ - message.GetInteger(0, &class_id); \ - if (class_id != RpcTypeToNumber<Class>::number) { \ + std::string class_name; \ + message.GetString(0, &class_name); \ + if (class_name != #Class) { \ return false; \ } \ - int method; \ - message.GetInteger(1, &method); \ + std::string method_name; \ + message.GetString(1, &method_name); \ typedef Class CLASS; \ - switch (method) { \ - STRUCT( \ - TOOLS_RPC_JS_DISPATCH0, \ - TOOLS_RPC_JS_DISPATCH1, \ - TOOLS_RPC_JS_DISPATCH2, \ - TOOLS_RPC_JS_DISPATCH3, \ - TOOLS_RPC_JS_DISPATCH4) \ - default: return false; \ - } \ + STRUCT( \ + TOOLS_RPC_JS_DISPATCH0, \ + TOOLS_RPC_JS_DISPATCH1, \ + TOOLS_RPC_JS_DISPATCH2, \ + TOOLS_RPC_JS_DISPATCH3, \ + TOOLS_RPC_JS_DISPATCH4) \ + return false; \ } \ private: \ std::string js_obj; \ diff --git a/webkit/glue/devtools/devtools_rpc_unittest.cc b/webkit/glue/devtools/devtools_rpc_unittest.cc index 5ca78d2a..d900fb8 100644 --- a/webkit/glue/devtools/devtools_rpc_unittest.cc +++ b/webkit/glue/devtools/devtools_rpc_unittest.cc @@ -52,21 +52,21 @@ class DevToolsRpcTests : public testing::Test { TEST_F(DevToolsRpcTests, TestSerialize) { MockTestRpcClass mock; mock.Method0(); - int id = RpcTypeToNumber<TestRpcClass>::number; - EXPECT_EQ(StringPrintf("[%d,0]", id), mock.get_log()); + EXPECT_EQ("[\"TestRpcClass\",\"Method0\"]", mock.get_log()); mock.Reset(); mock.Method1(10); - EXPECT_EQ(StringPrintf("[%d,1,10]", id), mock.get_log()); + EXPECT_EQ("[\"TestRpcClass\",\"Method1\",10]", mock.get_log()); mock.Reset(); mock.Method2(20, "foo"); - EXPECT_EQ(StringPrintf("[%d,2,20,\"foo\"]", id), mock.get_log()); + EXPECT_EQ("[\"TestRpcClass\",\"Method2\",20,\"foo\"]", mock.get_log()); mock.Reset(); StringValue value("bar"); mock.Method3(30, "foo", value); - EXPECT_EQ(StringPrintf("[%d,3,30,\"foo\",\"bar\"]", id), mock.get_log()); + EXPECT_EQ("[\"TestRpcClass\",\"Method3\",30,\"foo\",\"bar\"]", + mock.get_log()); mock.Reset(); } @@ -116,15 +116,4 @@ TEST_F(DevToolsRpcTests, TestDispatch) { remote.Verify(); } -// Tests class unique id. -TEST_F(DevToolsRpcTests, TestClassId) { - MockAnotherTestRpcClass mock; - ASSERT_TRUE(RpcTypeToNumber<TestRpcClass>::number != - RpcTypeToNumber<AnotherTestRpcClass>::number); - - int id = RpcTypeToNumber<AnotherTestRpcClass>::number; - mock.Method0(); - ASSERT_EQ(StringPrintf("[%d,0]", id), mock.get_log()); -} - } // namespace |