OLD | NEW |
1 // This file is generated | 1 // This file is generated |
2 | 2 |
3 // Copyright 2016 The Chromium Authors. All rights reserved. | 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 | 4 // Use of this source code is governed by a BSD-style license that can be |
5 // found in the LICENSE file. | 5 // found in the LICENSE file. |
6 | 6 |
7 #include "headless/public/domains/{{domain.domain | camelcase_to_hacker_style}}.
h" | 7 #include "headless/public/domains/{{domain.domain | camelcase_to_hacker_style}}.
h" |
8 | 8 |
9 #include "base/bind.h" | 9 #include "base/bind.h" |
10 | 10 |
11 namespace headless { | 11 namespace headless { |
12 | 12 |
13 namespace {{domain.domain | camelcase_to_hacker_style}} { | 13 namespace {{domain.domain | camelcase_to_hacker_style}} { |
14 | 14 |
15 Domain::Domain(internal::MessageDispatcher* dispatcher) | 15 ExperimentalDomain* Domain::GetExperimental() { |
16 : dispatcher_(dispatcher) { | 16 return static_cast<ExperimentalDomain*>(this); |
17 {% if "events" in domain %} | |
18 {# Register all events in this domain. #} | |
19 {% for event in domain.events %} | |
20 dispatcher_->RegisterEventHandler( | |
21 "{{domain.domain}}.{{event.name}}", | |
22 base::Bind(&Domain::Dispatch{{event.name | to_title_case}}Event, | |
23 base::Unretained(this))); | |
24 {% endfor %} | |
25 {% endif %} | |
26 } | 17 } |
27 | 18 |
28 {% if "events" in domain %} | 19 {% if "events" in domain %} |
29 void Domain::AddObserver(Observer* observer) { | 20 void Domain::AddObserver(Observer* observer) { |
30 observers_.AddObserver(observer); | 21 observers_.AddObserver(observer); |
31 } | 22 } |
32 | 23 |
33 void Domain::RemoveObserver(Observer* observer) { | 24 void Domain::RemoveObserver(Observer* observer) { |
34 observers_.RemoveObserver(observer); | 25 observers_.RemoveObserver(observer); |
35 } | 26 } |
36 {% endif %} | 27 {% endif %} |
37 | 28 |
38 Domain::~Domain() {} | |
39 {% for command in domain.commands %} | 29 {% for command in domain.commands %} |
40 {# Skip redirected commands. #} | 30 {# Skip redirected commands. #} |
41 {% if "redirect" in command %}{% continue %}{% endif %} | 31 {% if "redirect" in command %}{% continue %}{% endif %} |
42 | 32 |
| 33 {% set class_name = 'ExperimentalDomain' if command.hidden else 'Domain' %} |
43 {% set method_name = command.name | to_title_case %} | 34 {% set method_name = command.name | to_title_case %} |
44 {% if "parameters" in command and "returns" in command %} | 35 {% if "parameters" in command and "returns" in command %} |
45 void Domain::{{method_name}}(std::unique_ptr<{{method_name}}Params> params, base
::Callback<void(std::unique_ptr<{{method_name}}Result>)> callback) { | 36 void {{class_name}}::{{method_name}}(std::unique_ptr<{{method_name}}Params> para
ms, base::Callback<void(std::unique_ptr<{{method_name}}Result>)> callback) { |
46 dispatcher_->SendMessage("{{domain.domain}}.{{command.name}}", params->Seriali
ze(), base::Bind(&Domain::Handle{{method_name}}Response, callback)); | 37 dispatcher_->SendMessage("{{domain.domain}}.{{command.name}}", params->Seriali
ze(), base::Bind(&Domain::Handle{{method_name}}Response, callback)); |
47 {% elif "parameters" in command %} | 38 {% elif "parameters" in command %} |
48 void Domain::{{method_name}}(std::unique_ptr<{{method_name}}Params> params, base
::Callback<void()> callback) { | 39 void {{class_name}}::{{method_name}}(std::unique_ptr<{{method_name}}Params> para
ms, base::Callback<void()> callback) { |
49 dispatcher_->SendMessage("{{domain.domain}}.{{command.name}}", params->Seriali
ze(), std::move(callback)); | 40 dispatcher_->SendMessage("{{domain.domain}}.{{command.name}}", params->Seriali
ze(), std::move(callback)); |
50 {% elif "returns" in command %} | 41 {% elif "returns" in command %} |
51 void Domain::{{method_name}}(base::Callback<void(std::unique_ptr<{{method_name}}
Result>)> callback) { | 42 void {{class_name}}::{{method_name}}(base::Callback<void(std::unique_ptr<{{metho
d_name}}Result>)> callback) { |
52 dispatcher_->SendMessage("{{domain.domain}}.{{command.name}}", base::Bind(&Dom
ain::Handle{{method_name}}Response, std::move(callback))); | 43 dispatcher_->SendMessage("{{domain.domain}}.{{command.name}}", base::Bind(&Dom
ain::Handle{{method_name}}Response, std::move(callback))); |
53 {% else %} | 44 {% else %} |
54 void Domain::{{method_name}}(base::Callback<void()> callback) { | 45 void {{class_name}}::{{method_name}}(base::Callback<void()> callback) { |
55 dispatcher_->SendMessage("{{domain.domain}}.{{command.name}}", std::move(callb
ack)); | 46 dispatcher_->SendMessage("{{domain.domain}}.{{command.name}}", std::move(callb
ack)); |
56 {% endif %} | 47 {% endif %} |
57 } | 48 } |
58 {# Generate convenience methods that take the required parameters directly.
#} | 49 {# Generate convenience methods that take the required parameters directly.
#} |
59 {% if not "parameters" in command %}{% continue %}{% endif %} | 50 {% if not "parameters" in command %}{% continue %}{% endif %} |
| 51 {# Don't generate these for hidden commands. #} |
| 52 {% if command.hidden %}{% continue %}{% endif %} |
60 | 53 |
61 void Domain::{{method_name}}({##} | 54 void {{class_name}}::{{method_name}}({##} |
62 {% for parameter in command.parameters -%} | 55 {% for parameter in command.parameters -%} |
63 {% if parameter.get("optional", False) -%} | 56 {% if parameter.get("optional", False) -%} |
64 {% break %} | 57 {% break %} |
65 {% endif %} | 58 {% endif %} |
66 {% if not loop.first %}, {% endif %} | 59 {% if not loop.first %}, {% endif %} |
67 {{resolve_type(parameter).pass_type}} {{parameter.name | camelcase_to_hacker_sty
le -}} | 60 {{resolve_type(parameter).pass_type}} {{parameter.name | camelcase_to_hacker_sty
le -}} |
68 {% endfor %} | 61 {% endfor %} |
69 {% if "parameters" in command and not command.parameters[0].get("optional",
False) %}, {% endif %}{# -#} | 62 {% if "parameters" in command and not command.parameters[0].get("optional",
False) %}, {% endif %}{# -#} |
70 {% if "returns" in command -%} | 63 {% if "returns" in command -%} |
71 base::Callback<void(std::unique_ptr<{{method_name}}Result>)> callback{##} | 64 base::Callback<void(std::unique_ptr<{{method_name}}Result>)> callback{##} |
(...skipping 30 matching lines...) Expand all Loading... |
102 } | 95 } |
103 {% endfor %} | 96 {% endfor %} |
104 {% if "events" in domain %} | 97 {% if "events" in domain %} |
105 {% for event in domain.events %} | 98 {% for event in domain.events %} |
106 | 99 |
107 {# Generate dispatchers which call registered observers for an event. #} | 100 {# Generate dispatchers which call registered observers for an event. #} |
108 void Domain::Dispatch{{event.name | to_title_case}}Event(const base::Value& para
ms) { | 101 void Domain::Dispatch{{event.name | to_title_case}}Event(const base::Value& para
ms) { |
109 ErrorReporter errors; | 102 ErrorReporter errors; |
110 std::unique_ptr<{{event.name | to_title_case}}Params> parsed_params({{event.na
me | to_title_case}}Params::Parse(params, &errors)); | 103 std::unique_ptr<{{event.name | to_title_case}}Params> parsed_params({{event.na
me | to_title_case}}Params::Parse(params, &errors)); |
111 DCHECK(!errors.HasErrors()); | 104 DCHECK(!errors.HasErrors()); |
112 FOR_EACH_OBSERVER(Observer, observers_, On{{event.name | to_title_case}}(*pars
ed_params)); | 105 FOR_EACH_OBSERVER(ExperimentalObserver, observers_, On{{event.name | to_title_
case}}(*parsed_params)); |
113 } | 106 } |
114 {% endfor %} | 107 {% endfor %} |
115 {% endif %} | 108 {% endif %} |
116 | 109 |
| 110 Domain::Domain(internal::MessageDispatcher* dispatcher) |
| 111 : dispatcher_(dispatcher) { |
| 112 {% if "events" in domain %} |
| 113 {# Register all events in this domain. #} |
| 114 {% for event in domain.events %} |
| 115 dispatcher_->RegisterEventHandler( |
| 116 "{{domain.domain}}.{{event.name}}", |
| 117 base::Bind(&Domain::Dispatch{{event.name | to_title_case}}Event, |
| 118 base::Unretained(this))); |
| 119 {% endfor %} |
| 120 {% endif %} |
| 121 } |
| 122 |
| 123 Domain::~Domain() {} |
| 124 |
| 125 ExperimentalDomain::ExperimentalDomain(internal::MessageDispatcher* dispatcher) |
| 126 : Domain(dispatcher) {} |
| 127 |
| 128 ExperimentalDomain::~ExperimentalDomain() {} |
| 129 |
| 130 {% if "events" in domain %} |
| 131 void ExperimentalDomain::AddObserver(ExperimentalObserver* observer) { |
| 132 observers_.AddObserver(observer); |
| 133 } |
| 134 |
| 135 void ExperimentalDomain::RemoveObserver(ExperimentalObserver* observer) { |
| 136 observers_.RemoveObserver(observer); |
| 137 } |
| 138 {% endif %} |
| 139 |
117 } // namespace {{domain.domain | camelcase_to_hacker_style}} | 140 } // namespace {{domain.domain | camelcase_to_hacker_style}} |
118 | 141 |
119 } // namespace headless | 142 } // namespace headless |
OLD | NEW |