Added ToJson to JSON schema compiler.

Also re-factored the recently landed Debugger api (I believe it's the only API to have compiled events, so far)

BUG=138767


Review URL: https://chromiumcodereview.appspot.com/10796114

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@148878 0039d316-1c4b-4281-b951-d872f2087c98
parent 0d0c9cc5
...@@ -254,10 +254,7 @@ void ExtensionDevToolsClientHost::SendDetachedEvent() { ...@@ -254,10 +254,7 @@ void ExtensionDevToolsClientHost::SendDetachedEvent() {
Debuggee debuggee; Debuggee debuggee;
debuggee.tab_id = tab_id_; debuggee.tab_id = tab_id_;
scoped_ptr<base::ListValue> args(OnDetach::Create(debuggee)); std::string json_args = OnDetach::ToJson(debuggee);
std::string json_args;
base::JSONWriter::Write(args.get(), &json_args);
profile->GetExtensionEventRouter()->DispatchEventToExtension( profile->GetExtensionEventRouter()->DispatchEventToExtension(
extension_id_, keys::kOnDetach, json_args, profile, GURL()); extension_id_, keys::kOnDetach, json_args, profile, GURL());
} }
...@@ -310,9 +307,7 @@ void ExtensionDevToolsClientHost::DispatchOnInspectorFrontend( ...@@ -310,9 +307,7 @@ void ExtensionDevToolsClientHost::DispatchOnInspectorFrontend(
if (dictionary->GetDictionary("params", &params_value)) if (dictionary->GetDictionary("params", &params_value))
params.additional_properties.Swap(params_value); params.additional_properties.Swap(params_value);
scoped_ptr<ListValue> args(OnEvent::Create(debuggee, method_name, params)); std::string json_args = OnEvent::ToJson(debuggee, method_name, params);
std::string json_args;
base::JSONWriter::Write(args.get(), &json_args);
profile->GetExtensionEventRouter()->DispatchEventToExtension( profile->GetExtensionEventRouter()->DispatchEventToExtension(
extension_id_, keys::kOnEvent, json_args, profile, GURL()); extension_id_, keys::kOnEvent, json_args, profile, GURL());
......
...@@ -88,7 +88,7 @@ class CCGenerator(object): ...@@ -88,7 +88,7 @@ class CCGenerator(object):
) )
for event in self._namespace.events.values(): for event in self._namespace.events.values():
(c.Concat(self._GenerateCreateCallbackArguments( (c.Concat(self._GenerateCreateCallbackArguments(
cpp_util.Classname(event.name), event)) cpp_util.Classname(event.name), event, generate_to_json=True))
.Append() .Append()
) )
(c.Concat(self._cpp_type_generator.GetNamespaceEnd()) (c.Concat(self._cpp_type_generator.GetNamespaceEnd())
...@@ -689,7 +689,10 @@ class CCGenerator(object): ...@@ -689,7 +689,10 @@ class CCGenerator(object):
) )
return c return c
def _GenerateCreateCallbackArguments(self, function_scope, callback): def _GenerateCreateCallbackArguments(self,
function_scope,
callback,
generate_to_json=False):
"""Generate all functions to create Value parameters for a callback. """Generate all functions to create Value parameters for a callback.
E.g for function "Bar", generate Bar::Results::Create E.g for function "Bar", generate Bar::Results::Create
...@@ -698,6 +701,7 @@ class CCGenerator(object): ...@@ -698,6 +701,7 @@ class CCGenerator(object):
function_scope: the function scope path, e.g. Foo::Bar for the function function_scope: the function scope path, e.g. Foo::Bar for the function
Foo::Bar::Baz(). Foo::Bar::Baz().
callback: the Function object we are creating callback arguments for. callback: the Function object we are creating callback arguments for.
generate_to_json: Generate a ToJson method.
""" """
c = Code() c = Code()
params = callback.params params = callback.params
...@@ -724,9 +728,22 @@ class CCGenerator(object): ...@@ -724,9 +728,22 @@ class CCGenerator(object):
c.Append('return create_results.Pass();') c.Append('return create_results.Pass();')
c.Eblock('}') c.Eblock('}')
if generate_to_json:
c.Append()
(c.Sblock('std::string %(function_scope)s::'
'ToJson(%(declaration_list)s) {')
.Append('scoped_ptr<base::ListValue> create_results = '
'%(function_scope)s::Create(%(param_list)s);')
.Append('std::string json;')
.Append('base::JSONWriter::Write(create_results.get(), &json);')
.Append('return json;')
)
c.Eblock('}')
c.Substitute({ c.Substitute({
'function_scope': function_scope, 'function_scope': function_scope,
'declaration_list': ', '.join(declaration_list) 'declaration_list': ', '.join(declaration_list),
'param_list': ', '.join(param.unix_name for param in param_list)
}) })
return c return c
......
...@@ -222,6 +222,8 @@ class CppTypeGenerator(object): ...@@ -222,6 +222,8 @@ class CppTypeGenerator(object):
self._cpp_namespaces[dependency]) self._cpp_namespaces[dependency])
for dependency in self._NamespaceTypeDependencies().keys()]): for dependency in self._NamespaceTypeDependencies().keys()]):
c.Append('#include "%s"' % header) c.Append('#include "%s"' % header)
if self._namespace.events:
c.Append('#include "base/json/json_writer.h"')
return c return c
def _ResolveTypeNamespace(self, ref_type): def _ResolveTypeNamespace(self, ref_type):
......
...@@ -65,7 +65,7 @@ def GetParameterDeclaration(param, type_): ...@@ -65,7 +65,7 @@ def GetParameterDeclaration(param, type_):
type. type.
""" """
if param.type_ in (PropertyType.REF, PropertyType.OBJECT, PropertyType.ARRAY, if param.type_ in (PropertyType.REF, PropertyType.OBJECT, PropertyType.ARRAY,
PropertyType.STRING): PropertyType.STRING, PropertyType.ANY):
arg = '%(type)s& %(name)s' arg = '%(type)s& %(name)s'
else: else:
arg = '%(type)s %(name)s' arg = '%(type)s %(name)s'
......
...@@ -230,7 +230,8 @@ class HGenerator(object): ...@@ -230,7 +230,8 @@ class HGenerator(object):
""" """
c = Code() c = Code()
(c.Sblock('namespace %s {' % cpp_util.Classname(event.name)) (c.Sblock('namespace %s {' % cpp_util.Classname(event.name))
.Concat(self._GenerateCreateCallbackArguments(event)) .Concat(self._GenerateCreateCallbackArguments(event,
generate_to_json=True))
.Eblock('};') .Eblock('};')
) )
return c return c
...@@ -324,7 +325,7 @@ class HGenerator(object): ...@@ -324,7 +325,7 @@ class HGenerator(object):
cpp_util.Classname(prop.name))) cpp_util.Classname(prop.name)))
return c return c
def _GenerateCreateCallbackArguments(self, function): def _GenerateCreateCallbackArguments(self, function, generate_to_json=False):
"""Generates functions for passing paramaters to a callback. """Generates functions for passing paramaters to a callback.
""" """
c = Code() c = Code()
...@@ -341,6 +342,8 @@ class HGenerator(object): ...@@ -341,6 +342,8 @@ class HGenerator(object):
param, self._cpp_type_generator.GetType(param))) param, self._cpp_type_generator.GetType(param)))
c.Append('scoped_ptr<base::ListValue> Create(%s);' % c.Append('scoped_ptr<base::ListValue> Create(%s);' %
', '.join(declaration_list)) ', '.join(declaration_list))
if generate_to_json:
c.Append('std::string ToJson(%s);' % ', '.join(declaration_list))
return c return c
def _GenerateFunctionResults(self, callback): def _GenerateFunctionResults(self, callback):
......
This diff is collapsed.
...@@ -4,6 +4,7 @@ ...@@ -4,6 +4,7 @@
#include "tools/json_schema_compiler/test/objects.h" #include "tools/json_schema_compiler/test/objects.h"
#include "base/json/json_writer.h"
#include "testing/gtest/include/gtest/gtest.h" #include "testing/gtest/include/gtest/gtest.h"
using namespace test::api::objects; using namespace test::api::objects;
...@@ -67,4 +68,9 @@ TEST(JsonSchemaCompilerObjectsTest, OnObjectFiredCreate) { ...@@ -67,4 +68,9 @@ TEST(JsonSchemaCompilerObjectsTest, OnObjectFiredCreate) {
DictionaryValue* result = NULL; DictionaryValue* result = NULL;
ASSERT_TRUE(results->GetDictionary(0, &result)); ASSERT_TRUE(results->GetDictionary(0, &result));
ASSERT_TRUE(result->Equals(&expected)); ASSERT_TRUE(result->Equals(&expected));
std::string json1 = OnObjectFired::ToJson(object);
std::string json2;
base::JSONWriter::Write(results.get(), &json2);
ASSERT_EQ(json1, json2);
} }
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment