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() { |
altimin
2016/04/21 14:49:10
Nit: const?
Sami
2016/04/21 14:51:13
I thought about it but it doesn't quite work since
| |
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); |
dgozman
2016/04/21 17:30:45
How do you add an Observer to a list of Experiment
Sami
2016/04/21 17:39:01
It's a bit non-intuitive, but Observer inherits fr
| |
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 %} |
60 | 51 |
61 void Domain::{{method_name}}({##} | 52 void {{class_name}}::{{method_name}}({##} |
62 {% for parameter in command.parameters -%} | 53 {% for parameter in command.parameters -%} |
63 {% if parameter.get("optional", False) -%} | 54 {% if parameter.get("optional", False) -%} |
64 {% break %} | 55 {% break %} |
65 {% endif %} | 56 {% endif %} |
66 {% if not loop.first %}, {% endif %} | 57 {% if not loop.first %}, {% endif %} |
67 {{resolve_type(parameter).pass_type}} {{parameter.name | camelcase_to_hacker_sty le -}} | 58 {{resolve_type(parameter).pass_type}} {{parameter.name | camelcase_to_hacker_sty le -}} |
68 {% endfor %} | 59 {% endfor %} |
69 {% if "parameters" in command and not command.parameters[0].get("optional", False) %}, {% endif %}{# -#} | 60 {% if "parameters" in command and not command.parameters[0].get("optional", False) %}, {% endif %}{# -#} |
70 {% if "returns" in command -%} | 61 {% if "returns" in command -%} |
71 base::Callback<void(std::unique_ptr<{{method_name}}Result>)> callback{##} | 62 base::Callback<void(std::unique_ptr<{{method_name}}Result>)> callback{##} |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
106 | 97 |
107 {# Generate dispatchers which call registered observers for an event. #} | 98 {# Generate dispatchers which call registered observers for an event. #} |
108 void Domain::Dispatch{{event.name | to_title_case}}Event({# -#} | 99 void Domain::Dispatch{{event.name | to_title_case}}Event({# -#} |
109 {% if "parameters" in event %}{# -#} | 100 {% if "parameters" in event %}{# -#} |
110 const base::Value& params{# -#} | 101 const base::Value& params{# -#} |
111 {% endif %}) { | 102 {% endif %}) { |
112 {% if "parameters" in event %} | 103 {% if "parameters" in event %} |
113 ErrorReporter errors; | 104 ErrorReporter errors; |
114 std::unique_ptr<{{event.name | to_title_case}}Params> parsed_params({{event.na me | to_title_case}}Params::Parse(params, &errors)); | 105 std::unique_ptr<{{event.name | to_title_case}}Params> parsed_params({{event.na me | to_title_case}}Params::Parse(params, &errors)); |
115 DCHECK(!errors.HasErrors()); | 106 DCHECK(!errors.HasErrors()); |
116 FOR_EACH_OBSERVER(Observer, observers_, On{{event.name | to_title_case}}(*pars ed_params)); | 107 FOR_EACH_OBSERVER(ExperimentalObserver, observers_, On{{event.name | to_title_ case}}(*parsed_params)); |
117 {% else %} | 108 {% else %} |
118 FOR_EACH_OBSERVER(Observer, observers_, On{{event.name | to_title_case}}()); | 109 FOR_EACH_OBSERVER(ExperimentalObserver, observers_, On{{event.name | to_title_ case}}()); |
119 {% endif %} | 110 {% endif %} |
120 } | 111 } |
121 {% endfor %} | 112 {% endfor %} |
122 {% endif %} | 113 {% endif %} |
123 | 114 |
115 Domain::Domain(internal::MessageDispatcher* dispatcher) | |
116 : dispatcher_(dispatcher) { | |
117 {% if "events" in domain %} | |
118 {# Register all events in this domain. #} | |
119 {% for event in domain.events %} | |
120 dispatcher_->RegisterEventHandler( | |
121 "{{domain.domain}}.{{event.name}}", | |
122 base::Bind(&Domain::Dispatch{{event.name | to_title_case}}Event, | |
123 base::Unretained(this))); | |
124 {% endfor %} | |
125 {% endif %} | |
126 } | |
127 | |
128 Domain::~Domain() {} | |
129 | |
130 ExperimentalDomain::ExperimentalDomain(internal::MessageDispatcher* dispatcher) | |
131 : Domain(dispatcher) {} | |
132 | |
133 ExperimentalDomain::~ExperimentalDomain() {} | |
134 | |
135 {% if "events" in domain %} | |
136 void ExperimentalDomain::AddObserver(ExperimentalObserver* observer) { | |
137 observers_.AddObserver(observer); | |
138 } | |
139 | |
140 void ExperimentalDomain::RemoveObserver(ExperimentalObserver* observer) { | |
141 observers_.RemoveObserver(observer); | |
142 } | |
143 {% endif %} | |
144 | |
124 } // namespace {{domain.domain | camelcase_to_hacker_style}} | 145 } // namespace {{domain.domain | camelcase_to_hacker_style}} |
125 | 146 |
126 } // namespace headless | 147 } // namespace headless |
OLD | NEW |