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

Side by Side Diff: headless/lib/browser/domain_cc.template

Issue 2473073003: [headless] Refactor headless devtools client API. (Closed)
Patch Set: Deleted random files Created 4 years, 1 month 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 unified diff | Download patch
OLDNEW
(Empty)
1 // This file is generated
2
3 // Copyright 2016 The Chromium Authors. All rights reserved.
4 // Use of this source code is governed by a BSD-style license that can be
5 // found in the LICENSE file.
6
7 #include "headless/public/domains/{{domain.domain | camelcase_to_hacker_style}}. h"
8
9 #include "base/bind.h"
10
11 namespace headless {
12
13 namespace {{domain.domain | camelcase_to_hacker_style}} {
14
15 ExperimentalDomain* Domain::GetExperimental() {
16 return static_cast<ExperimentalDomain*>(this);
17 }
18
19 {% if "events" in domain %}
20 void Domain::AddObserver(Observer* observer) {
21 observers_.AddObserver(observer);
22 }
23
24 void Domain::RemoveObserver(Observer* observer) {
25 observers_.RemoveObserver(observer);
26 }
27 {% endif %}
28
29 {% for command in domain.commands %}
30 {# Skip redirected commands. #}
31 {% if "redirect" in command %}{% continue %}{% endif %}
32
33 {% set class_name = 'ExperimentalDomain' if command.experimental else 'Domai n' %}
34 {% set method_name = command.name | to_title_case %}
35 void {{class_name}}::{{method_name}}(std::unique_ptr<{{method_name}}Params> para ms, base::Callback<void(std::unique_ptr<{{method_name}}Result>)> callback) {
36 dispatcher_->SendMessage("{{domain.domain}}.{{command.name}}", params->Seriali ze(), base::Bind(&Domain::Handle{{method_name}}Response, callback));
37 }
38 {# Generate convenience methods that take the required parameters directly. #}
39 {% if not "parameters" in command %}{% continue %}{% endif %}
40 {# Don't generate these for experimental commands. #}
41 {% if command.experimental %}{% continue %}{% endif %}
42
43 void {{class_name}}::{{method_name}}({##}
44 {% for parameter in command.parameters -%}
45 {% if parameter.get("optional", False) -%}
46 {% break %}
47 {% endif %}
48 {% if not loop.first %}, {% endif %}
49 {{resolve_type(parameter).pass_type}} {{parameter.name | camelcase_to_hacker_sty le -}}
50 {% endfor %}
51 {% if command.get("parameters", []) and not command.parameters[0].get("optio nal", False) %}, {% endif %}{# -#}
52 {% if command.get("returns", []) -%}
53 base::Callback<void(std::unique_ptr<{{method_name}}Result>)> callback{##}
54 {% else -%}
55 base::Callback<void()> callback{##}
56 {% endif %}) {
57 {# Build the parameters object. #}
58 std::unique_ptr<{{method_name}}Params> params = {{method_name}}Params::Builder ()
59 {% for parameter in command.parameters -%}
60 {% if parameter.get("optional", False) -%}
61 {% break %}
62 {% endif %}
63 .Set{{parameter.name | to_title_case}}(std::move({{parameter.name | camelc ase_to_hacker_style}}))
64 {% endfor %}
65 .Build();
66 {# Send the message. #}
67 {% if command.get("returns", []) -%}
68 dispatcher_->SendMessage("{{domain.domain}}.{{command.name}}", params->Seriali ze(), base::Bind(&Domain::Handle{{method_name}}Response, callback));
69 {% else %}
70 dispatcher_->SendMessage("{{domain.domain}}.{{command.name}}", params->Seriali ze(), std::move(callback));
71 {% endif %}
72 }
73 {# If the command has no return value, generate a convenience method that #}
74 {# accepts a base::Callback<void()> together with the parameters object. #}
75 {% if not command.get("returns", []) %}
76 void {{class_name}}::{{method_name}}(std::unique_ptr<{{method_name}}Params> para ms, base::Callback<void()> callback) {
77 dispatcher_->SendMessage("{{domain.domain}}.{{command.name}}", params->Seriali ze(), std::move(callback));
78 }
79 {% endif %}
80 {% endfor %}
81
82 {# Generate response handlers for commands that need them. #}
83 {% for command in domain.commands %}
84 {% if not "returns" in command %}{% continue %}{% endif %}
85 {% set method_name = command.name | to_title_case %}
86
87 // static
88 void Domain::Handle{{method_name}}Response(base::Callback<void(std::unique_ptr<{ {method_name}}Result>)> callback, const base::Value& response) {
89 if (callback.is_null())
90 return;
91 ErrorReporter errors;
92 std::unique_ptr<{{method_name}}Result> result = {{method_name}}Result::Parse(r esponse, &errors);
93 DCHECK(!errors.HasErrors());
94 callback.Run(std::move(result));
95 }
96 {% endfor %}
97 {% if "events" in domain %}
98 {% for event in domain.events %}
99
100 {# Generate dispatchers which call registered observers for an event. #}
101 void Domain::Dispatch{{event.name | to_title_case}}Event(const base::Value& para ms) {
102 ErrorReporter errors;
103 std::unique_ptr<{{event.name | to_title_case}}Params> parsed_params({{event.na me | to_title_case}}Params::Parse(params, &errors));
104 DCHECK(!errors.HasErrors());
105 for (ExperimentalObserver& observer : observers_) {
106 observer.On{{event.name | to_title_case}}(*parsed_params);
107 }
108 }
109 {% endfor %}
110 {% endif %}
111
112 Domain::Domain(internal::MessageDispatcher* dispatcher)
113 : dispatcher_(dispatcher) {
114 {% if "events" in domain %}
115 {# Register all events in this domain. #}
116 {% for event in domain.events %}
117 dispatcher_->RegisterEventHandler(
118 "{{domain.domain}}.{{event.name}}",
119 base::Bind(&Domain::Dispatch{{event.name | to_title_case}}Event,
120 base::Unretained(this)));
121 {% endfor %}
122 {% endif %}
123 }
124
125 Domain::~Domain() {}
126
127 ExperimentalDomain::ExperimentalDomain(internal::MessageDispatcher* dispatcher)
128 : Domain(dispatcher) {}
129
130 ExperimentalDomain::~ExperimentalDomain() {}
131
132 {% if "events" in domain %}
133 void ExperimentalDomain::AddObserver(ExperimentalObserver* observer) {
134 observers_.AddObserver(observer);
135 }
136
137 void ExperimentalDomain::RemoveObserver(ExperimentalObserver* observer) {
138 observers_.RemoveObserver(observer);
139 }
140 {% endif %}
141
142 } // namespace {{domain.domain | camelcase_to_hacker_style}}
143
144 } // namespace headless
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698