Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(583)

Unified Diff: content/browser/devtools/protocol/devtools_protocol_handler_generator.py

Issue 642263004: [DevTools] Make generated protocol structs wrappers around DictionaryValue (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 6 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: content/browser/devtools/protocol/devtools_protocol_handler_generator.py
diff --git a/content/browser/devtools/protocol/devtools_protocol_handler_generator.py b/content/browser/devtools/protocol/devtools_protocol_handler_generator.py
index 6aa39f8dc04113c55390558c2f23b2016366066f..c8fc7f3f1b13c20acb509ba5c40c9a71edb4ba15 100755
--- a/content/browser/devtools/protocol/devtools_protocol_handler_generator.py
+++ b/content/browser/devtools/protocol/devtools_protocol_handler_generator.py
@@ -34,6 +34,32 @@ namespace content {
class DevToolsProtocolHandlerImpl;
+class ListBuilderBase {
+ public:
+ static scoped_ptr<base::ListValue> ToValue(scoped_ptr<ListBuilderBase> src);
+
+ protected:
+ scoped_ptr<base::ListValue> list_;
+};
+
+template<class T> class ListBuilder : public ListBuilderBase {
dgozman 2014/10/17 10:51:57 nit: template<...> is usually on the separate line
vkuzkokov 2014/10/17 11:41:25 Done.
+ public:
+ typedef scoped_ptr<T> item_type;
+
+ void push_back(scoped_ptr<T> item) {
+ list_->Append(T::ToValue(item.Pass()).release());
+ }
+};
+
+template<> class ListBuilder<std::string> : public ListBuilderBase {
dgozman 2014/10/17 10:51:57 ditto
vkuzkokov 2014/10/17 11:41:25 Done.
+ public:
+ typedef const std::string& item_type;
+
+ void push_back(const std::string& item) {
+ list_->AppendString(item);
+ }
+};
+
namespace devtools {
${types}\
@@ -51,8 +77,6 @@ class DevToolsProtocolHandlerImpl : public DevToolsProtocol::Handler {
${setters}\
private:
-${friends}\
-
${methods}\
${fields}\
@@ -75,12 +99,16 @@ struct ${declared_name} {
public:
${declared_name}();
+ static scoped_ptr<base::DictionaryValue> ToValue(
+ scoped_ptr<${declared_name}> src);
+
${methods}\
private:
- friend class ::content::DevToolsProtocolHandlerImpl;
-
+ scoped_ptr<base::DictionaryValue> dict_;
${fields}\
+
+ DISALLOW_COPY_AND_ASSIGN(${declared_name});
};
} // namespace ${domain}
""")
@@ -90,7 +118,6 @@ tmpl_struct_setter = string.Template("""\
""")
tmpl_struct_field = string.Template("""\
- ${param_type} ${param}_;
bool has_${param}_;
""")
@@ -131,13 +158,13 @@ ${methods}\
tmpl_event = string.Template("""\
void ${Command}(
- const ${Command}Params& params);
+ scoped_ptr<${Command}Params> params);
""")
tmpl_response = string.Template("""\
void Send${Command}Response(
scoped_refptr<DevToolsProtocol::Command> command,
- const ${Command}Response& params);
+ scoped_ptr<${Command}Response> params);
""")
tmpl_setter = string.Template("""\
@@ -145,21 +172,12 @@ tmpl_setter = string.Template("""\
devtools::${domain}::${Domain}Handler* ${domain}_handler);
""")
-tmpl_friend = string.Template("""\
- friend class devtools::${domain}::Client;
-""")
-
tmpl_callback = string.Template("""\
scoped_refptr<DevToolsProtocol::Response>
On${Domain}${Command}(
scoped_refptr<DevToolsProtocol::Command> command);
""")
-tmpl_to_value = string.Template("""\
- static base::DictionaryValue* ToValue(
- const devtools::${domain}::${declared_name}& src);
-""")
-
tmpl_field = string.Template("""\
devtools::${domain}::${Domain}Handler* ${domain}_handler_;
""")
@@ -173,6 +191,12 @@ ${includes}\
namespace content {
+// static
+scoped_ptr<base::ListValue> ListBuilderBase::ToValue(
+ scoped_ptr<ListBuilderBase> src) {
+ return src ? src->list_.Pass() : nullptr;
+}
+
DevToolsProtocolHandlerImpl::DevToolsProtocolHandlerImpl()
: ${fields_init} {
}
@@ -190,7 +214,7 @@ bool CreateCommonResponse(
scoped_refptr<DevToolsProtocol::Response>* protocol_response) {
switch (response.status()) {
case ResponseStatus::RESPONSE_STATUS_FALLTHROUGH:
- *protocol_response = NULL;
+ *protocol_response = nullptr;
break;
case ResponseStatus::RESPONSE_STATUS_OK:
return false;
@@ -224,7 +248,7 @@ tmpl_include = string.Template("""\
#include "content/browser/devtools/protocol/${domain}_handler.h"
""")
-tmpl_field_init = string.Template("${domain}_handler_(NULL)")
+tmpl_field_init = string.Template("${domain}_handler_(nullptr)")
tmpl_setter_impl = string.Template("""\
void DevToolsProtocolHandlerImpl::Set${Domain}Handler(
@@ -261,9 +285,9 @@ ${prep}\
scoped_refptr<DevToolsProtocol::Response> protocol_response;
if (CreateCommonResponse(command, response, &protocol_response))
return protocol_response;
- base::DictionaryValue* dict = new base::DictionaryValue();
+ base::DictionaryValue* result = new base::DictionaryValue();
${wrap}\
- return command->SuccessResponse(dict);
+ return command->SuccessResponse(result);
}
""")
@@ -281,17 +305,16 @@ params_prep = """\
"""
tmpl_prep_req = string.Template("""\
- ${param_type} in_${param}${init};
- if (!params ||
- !params->Get${Type}("${proto_param}", &in_${param}))
+ ${storage_type} in_${param}${init};
+ if (!params || !params->Get${Type}("${proto_param}", &in_${param}))
return command->InvalidParamResponse("${proto_param}");
""")
tmpl_prep_req_list = string.Template("""\
- base::ListValue* list_${param} = NULL;
+ base::ListValue* list_${param} = nullptr;
if (!params || !params->GetList("${proto_param}", &list_${param}))
return command->InvalidParamResponse("${proto_param}");
- ${param_type} in_${param};
+ std::vector<${item_type}> in_${param};
for (base::ListValue::const_iterator it =
list_${param}->begin(); it != list_${param}->end(); ++it) {
${item_type} item${item_init};
@@ -302,36 +325,22 @@ tmpl_prep_req_list = string.Template("""\
""")
tmpl_prep_opt = string.Template("""\
- ${param_type} in_${param}${init};
+ ${storage_type} in_${param}${init};
bool ${param}_found = params && params->Get${Type}(
"${proto_param}",
&in_${param});
""")
tmpl_prep_output = string.Template("""\
- ${param_type} out_${param}${init};
+ ${storage_type} out_${param}${init};
""")
tmpl_arg_req = string.Template("in_${param}")
-tmpl_arg_opt = string.Template(
- "${param}_found ?\n &in_${param} : NULL")
-
-tmpl_arg_output = string.Template("&out_${param}")
-
-tmpl_to_value_impl = string.Template("""\
-// static
-base::DictionaryValue* DevToolsProtocolHandlerImpl::ToValue(
- const devtools::${domain}::${declared_name}& src) {
- base::DictionaryValue* dict = new base::DictionaryValue();
-${dchecks}\
-${wrap}\
- return dict;
-}
-""")
+tmpl_arg_opt = string.Template("${param}_found ? &in_${param} : nullptr")
dgozman 2014/10/17 10:51:57 nit: keep the line break
vkuzkokov 2014/10/17 11:41:25 Done.
tmpl_dcheck = string.Template("""\
- DCHECK(${cond_name});
+ DCHECK(src->has_${param}_);
""")
tmpl_struct_impl = string.Template("""\
@@ -340,6 +349,15 @@ namespace ${domain} {
${declared_name}::${declared_name}()${fields} {
}
+// static
+scoped_ptr<base::DictionaryValue> ${declared_name}::ToValue(
+ scoped_ptr<${declared_name}> src) {
+ if (!src)
+ return nullptr;
+${dchecks}\
+ return src->dict_.Pass();
+}
+
${methods}\
} // namespace ${domain}
@@ -350,7 +368,7 @@ tmpl_struct_field_init = string.Template("has_${param}_(false)")
tmpl_struct_setter_impl = string.Template("""\
void ${declared_name}::set_${param}(
${pass_type} ${param}) {
- ${param}_ = ${param};
+${wrap}\
has_${param}_ = true;
dgozman 2014/10/17 10:51:57 We should remove "has_${param}_" entirely, and use
vkuzkokov 2014/10/17 11:41:25 Done.
}
""")
@@ -373,58 +391,35 @@ ${methods}\
tmpl_event_impl = string.Template("""\
void Client::${Command}(
- const ${Command}Params& params) {
+ scoped_ptr<${Command}Params> params) {
SendNotification("${Domain}.${command}",
- DevToolsProtocolHandlerImpl::ToValue(params));
+ ${Command}Params::ToValue(params.Pass()).Pass());
}
""")
tmpl_response_impl = string.Template("""\
void Client::Send${Command}Response(
scoped_refptr<DevToolsProtocol::Command> command,
- const ${Command}Response& params) {
+ scoped_ptr<${Command}Response> params) {
SendAsyncResponse(
- command->SuccessResponse(DevToolsProtocolHandlerImpl::ToValue(params)));
+ command->SuccessResponse(
+ ${Command}Response::ToValue(params.Pass()).release()));
}
""")
tmpl_wrap = string.Template("""\
- dict->Set${Type}("${proto_param}", ${var_name});
+ ${dict_var}->Set${Type}("${proto_param}", ${var_name});
""")
tmpl_wrap_dict = string.Template("""\
- dict->Set("${proto_param}",
- DevToolsProtocolHandlerImpl::ToValue(${var_name}));
+ ${dict_var}->Set("${proto_param}",
+ ${param_type}::ToValue(${var_name}.Pass()).release());
""")
-tmpl_wrap_obj = string.Template("""\
- dict->Set("${proto_param}", ${var_name});
-""")
+tmpl_wrap_list = tmpl_wrap_dict;
-tmpl_wrap_list = string.Template("""\
- base::ListValue* list_${param} = new base::ListValue();
- for (${param_type}::const_iterator it =
- ${var_name}.begin(); it != ${var_name}.end(); ++it) {
-${append}\
- }
- dict->Set("${proto_param}", list_${param});
-""")
-
-tmpl_append = string.Template("""\
- list_${param}->Append${Type}(*it);
-""")
-
-tmpl_append_dict = string.Template("""\
- list_${param}->Append(DevToolsProtocolHandlerImpl::ToValue(*it));
-""")
-
-tmpl_append_obj = string.Template("""\
- list_${param}->Append(*it);
-""")
-
-tmpl_wrap_opt = string.Template("""\
- if (${cond_name})
- dict->Set${Type}("${proto_param}", ${var_name});
+tmpl_wrap_obj = string.Template("""\
+ ${dict_var}->Set("${proto_param}", ${var_name});
""")
tmpl_typename = string.Template("devtools::${domain}::${declared_name}")
@@ -464,30 +459,20 @@ def DeclareStruct(json_properties, mapping):
fields_init = []
method_impls = []
dchecks = []
- wrap = []
for json_prop in json_properties:
prop_map = mapping.copy()
prop_map["proto_param"] = json_prop["name"]
prop_map["param"] = Uncamelcase(json_prop["name"])
- prop_map["var_name"] = "src.%s_" % prop_map["param"]
- prop_map["cond_name"] = "src.has_%s_" % prop_map["param"]
+ prop_map["var_name"] = "%s" % prop_map["param"]
+ prop_map["dict_var"] = "dict_";
ResolveType(json_prop, prop_map)
prop_map["declared_name"] = mapping["declared_name"]
methods.append(tmpl_struct_setter.substitute(prop_map))
fields.append(tmpl_struct_field.substitute(prop_map))
fields_init.append(tmpl_struct_field_init.substitute(prop_map))
method_impls.append(tmpl_struct_setter_impl.substitute(prop_map))
- if json_prop.get("optional"):
- if param_map["Type"] in ["List", "Dictionary"]:
- # TODO(vkuzkokov) Implement.
- raise Exception(
- "Optional array and object properties are not implemented")
- wrap.append(tmpl_wrap_opt.substitute(prop_map))
- else:
+ if not json_prop.get("optional"):
dchecks.append(tmpl_dcheck.substitute(prop_map));
- if not "wrap" in prop_map:
- raise Exception("Arrays of arrays are not implemented")
- wrap.append(prop_map["wrap"])
type_decls.append(tmpl_struct.substitute(mapping,
methods = "".join(methods),
@@ -497,11 +482,8 @@ def DeclareStruct(json_properties, mapping):
fields_init_str = "\n : " + (",\n ".join(fields_init))
type_impls.append(tmpl_struct_impl.substitute(mapping,
fields = fields_init_str,
- methods = "\n".join(method_impls)))
- handler_methods.append(tmpl_to_value.substitute(mapping))
- handler_method_impls.append(tmpl_to_value_impl.substitute(mapping,
- dchecks = "".join(dchecks),
- wrap = "".join(wrap)))
+ methods = "\n".join(method_impls),
+ dchecks = "".join(dchecks)))
def ResolveRef(json, mapping):
dot_pos = json["$ref"].find(".")
@@ -532,17 +514,16 @@ def ResolveRef(json, mapping):
def ResolveArray(json, mapping):
items_map = mapping.copy()
ResolveType(json["items"], items_map)
- mapping["param_type"] = "std::vector<%s>" % items_map["param_type"]
+ mapping["param_type"] = "ListBuilder<%s>" % items_map["param_type"]
mapping["Type"] = "List"
- if "append" in items_map:
- mapping["wrap"] = tmpl_wrap_list.substitute(mapping,
- append = items_map["append"])
- mapping["pass_type"] = "const %s&" % mapping["param_type"]
+ mapping["wrap"] = tmpl_wrap_list.substitute(mapping)
+ mapping["pass_type"] = "scoped_ptr<%s>" % mapping["param_type"]
+ mapping["storage_type"] = mapping["pass_type"]
mapping["prep_req"] = tmpl_prep_req_list.substitute(mapping,
item_type = items_map["param_type"],
item_init = items_map["init"],
ItemType = items_map["Type"])
- # TODO(vkuzkokov) mapping["append"]: template for array of arrays.
+ mapping["arg_out"] = "%s.get()" % mapping["var_name"]
def ResolveObject(json, mapping):
mapping["Type"] = "Dictionary"
@@ -552,14 +533,13 @@ def ResolveObject(json, mapping):
(mapping["Command"], Capitalize(mapping["proto_param"])))
mapping["param_type"] = tmpl_typename.substitute(mapping)
DeclareStruct(json["properties"], mapping)
- mapping["append"] = tmpl_append_dict.substitute(mapping)
mapping["wrap"] = tmpl_wrap_dict.substitute(mapping)
- mapping["pass_type"] = "const %s&" % mapping["param_type"]
else:
- mapping["param_type"] = "base::DictionaryValue*"
- mapping["append"] = tmpl_append_obj.substitute(mapping)
+ mapping["param_type"] = "base::DictionaryValue"
mapping["wrap"] = tmpl_wrap_obj.substitute(mapping)
- mapping["pass_type"] = mapping["param_type"]
+ mapping["pass_type"] = "scoped_ptr<%s>" % mapping["param_type"]
+ mapping["storage_type"] = mapping["pass_type"]
+ mapping["arg_out"] = "%s.get()" % mapping["var_name"]
def ResolvePrimitive(json, mapping):
jsonrpc_type = json["type"]
@@ -600,10 +580,11 @@ def ResolvePrimitive(json, mapping):
else:
raise Exception("Unknown type: %s" % json_type)
mapping["wrap"] = tmpl_wrap.substitute(mapping)
- mapping["append"] = tmpl_append.substitute(mapping)
+ mapping["storage_type"] = mapping["param_type"]
mapping["prep_req"] = tmpl_prep_req.substitute(mapping)
if jsonrpc_type != "string":
mapping["pass_type"] = mapping["param_type"]
+ mapping["arg_out"] = "&%s" % mapping["var_name"]
def ResolveType(json, mapping):
mapping["init"] = ""
@@ -622,7 +603,6 @@ def ResolveType(json, mapping):
(mapping["Domain"], mapping["command"], mapping["proto_param"]))
setters = []
-friends = []
fields = []
includes = []
@@ -659,6 +639,7 @@ for json_domain in json_api["domains"]:
param_map["proto_param"] = json_param["name"]
param_map["param"] = Uncamelcase(json_param["name"])
param_map["var_name"] = "in_%s" % param_map["param"]
+ param_map["dict_var"] = "result"
ResolveType(json_param, param_map)
if len(prep) == 0:
@@ -700,13 +681,14 @@ for json_domain in json_api["domains"]:
param_map["proto_param"] = json_param["name"]
param_map["param"] = Uncamelcase(json_param["name"])
param_map["var_name"] = "out_%s" % param_map["param"]
+ param_map["dict_var"] = "result"
if json_param.get("optional"):
# TODO(vkuzkokov) Implement Optional<T> for value types.
raise Exception("Optional return values are not implemented")
ResolveType(json_param, param_map)
prep.append(tmpl_prep_output.substitute(param_map))
- args.append(tmpl_arg_output.substitute(param_map))
+ args.append(param_map["arg_out"])
if not "wrap" in param_map:
raise Exception("Arrays of arrays are not implemented")
wrap.append(param_map["wrap"])
@@ -753,7 +735,6 @@ for json_domain in json_api["domains"]:
if domain_needs_client:
type_decls.append(tmpl_client.substitute(domain_map,
methods = "".join(client_methods)))
- friends.append(tmpl_friend.substitute(domain_map))
initializations.append(tmpl_init_client.substitute(domain_map))
type_impls.append(tmpl_client_impl.substitute(domain_map,
methods = "\n".join(client_method_impls)))
@@ -767,7 +748,6 @@ output_cc_file = open(output_cc_path, "w")
output_h_file.write(template_h.substitute({},
types = "\n".join(type_decls),
setters = "".join(setters),
- friends = "".join(friends),
methods = "".join(handler_methods),
fields = "".join(fields)))
output_h_file.close()

Powered by Google App Engine
This is Rietveld 408576698