OLD | NEW |
| (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 RegisterEventHandlersIfNeeded(); | |
22 observers_.AddObserver(observer); | |
23 } | |
24 | |
25 void Domain::RemoveObserver(Observer* observer) { | |
26 observers_.RemoveObserver(observer); | |
27 } | |
28 | |
29 void Domain::RegisterEventHandlersIfNeeded() { | |
30 if (event_handlers_registered_) | |
31 return; | |
32 event_handlers_registered_ = true; | |
33 {# Register all events in this domain. #} | |
34 {% for event in domain.events %} | |
35 dispatcher_->RegisterEventHandler( | |
36 "{{domain.domain}}.{{event.name}}", | |
37 base::Bind(&Domain::Dispatch{{event.name | to_title_case}}Event, | |
38 base::Unretained(this))); | |
39 {% endfor %} | |
40 } | |
41 {% endif %} | |
42 | |
43 {% for command in domain.commands %} | |
44 {# Skip redirected commands. #} | |
45 {% if "redirect" in command %}{% continue %}{% endif %} | |
46 | |
47 {% set class_name = 'ExperimentalDomain' if command.experimental else 'Domai
n' %} | |
48 {% set method_name = command.name | to_title_case %} | |
49 void {{class_name}}::{{method_name}}(std::unique_ptr<{{method_name}}Params> para
ms, base::Callback<void(std::unique_ptr<{{method_name}}Result>)> callback) { | |
50 dispatcher_->SendMessage("{{domain.domain}}.{{command.name}}", params->Seriali
ze(), base::Bind(&Domain::Handle{{method_name}}Response, callback)); | |
51 } | |
52 {# Generate convenience methods that take the required parameters directly.
#} | |
53 {% if not "parameters" in command %}{% continue %}{% endif %} | |
54 {# Don't generate these for experimental commands. #} | |
55 {% if command.experimental %}{% continue %}{% endif %} | |
56 | |
57 void {{class_name}}::{{method_name}}({##} | |
58 {% for parameter in command.parameters -%} | |
59 {% if parameter.get("optional", False) -%} | |
60 {% break %} | |
61 {% endif %} | |
62 {% if not loop.first %}, {% endif %} | |
63 {{resolve_type(parameter).pass_type}} {{parameter.name | camelcase_to_hacker_sty
le -}} | |
64 {% endfor %} | |
65 {% if command.get("parameters", []) and not command.parameters[0].get("optio
nal", False) %}, {% endif %}{# -#} | |
66 {% if command.get("returns", []) -%} | |
67 base::Callback<void(std::unique_ptr<{{method_name}}Result>)> callback{##} | |
68 {% else -%} | |
69 base::Callback<void()> callback{##} | |
70 {% endif %}) { | |
71 {# Build the parameters object. #} | |
72 std::unique_ptr<{{method_name}}Params> params = {{method_name}}Params::Builder
() | |
73 {% for parameter in command.parameters -%} | |
74 {% if parameter.get("optional", False) -%} | |
75 {% break %} | |
76 {% endif %} | |
77 .Set{{parameter.name | to_title_case}}(std::move({{parameter.name | camelc
ase_to_hacker_style}})) | |
78 {% endfor %} | |
79 .Build(); | |
80 {# Send the message. #} | |
81 {% if command.get("returns", []) -%} | |
82 dispatcher_->SendMessage("{{domain.domain}}.{{command.name}}", params->Seriali
ze(), base::Bind(&Domain::Handle{{method_name}}Response, callback)); | |
83 {% else %} | |
84 dispatcher_->SendMessage("{{domain.domain}}.{{command.name}}", params->Seriali
ze(), std::move(callback)); | |
85 {% endif %} | |
86 } | |
87 {# If the command has no return value, generate a convenience method that #} | |
88 {# accepts a base::Callback<void()> together with the parameters object. #} | |
89 {% if not command.get("returns", []) %} | |
90 void {{class_name}}::{{method_name}}(std::unique_ptr<{{method_name}}Params> para
ms, base::Callback<void()> callback) { | |
91 dispatcher_->SendMessage("{{domain.domain}}.{{command.name}}", params->Seriali
ze(), std::move(callback)); | |
92 } | |
93 {% endif %} | |
94 {% endfor %} | |
95 | |
96 {# Generate response handlers for commands that need them. #} | |
97 {% for command in domain.commands %} | |
98 {% if not "returns" in command %}{% continue %}{% endif %} | |
99 {% set method_name = command.name | to_title_case %} | |
100 | |
101 // static | |
102 void Domain::Handle{{method_name}}Response(base::Callback<void(std::unique_ptr<{
{method_name}}Result>)> callback, const base::Value& response) { | |
103 if (callback.is_null()) | |
104 return; | |
105 ErrorReporter errors; | |
106 std::unique_ptr<{{method_name}}Result> result = {{method_name}}Result::Parse(r
esponse, &errors); | |
107 DCHECK(!errors.HasErrors()); | |
108 callback.Run(std::move(result)); | |
109 } | |
110 {% endfor %} | |
111 {% if "events" in domain %} | |
112 {% for event in domain.events %} | |
113 | |
114 {# Generate dispatchers which call registered observers for an event. #} | |
115 void Domain::Dispatch{{event.name | to_title_case}}Event(const base::Value& para
ms) { | |
116 ErrorReporter errors; | |
117 std::unique_ptr<{{event.name | to_title_case}}Params> parsed_params({{event.na
me | to_title_case}}Params::Parse(params, &errors)); | |
118 DCHECK(!errors.HasErrors()); | |
119 for (ExperimentalObserver& observer : observers_) { | |
120 observer.On{{event.name | to_title_case}}(*parsed_params); | |
121 } | |
122 } | |
123 {% endfor %} | |
124 {% endif %} | |
125 | |
126 Domain::Domain(internal::MessageDispatcher* dispatcher) | |
127 : dispatcher_(dispatcher) { | |
128 } | |
129 | |
130 Domain::~Domain() {} | |
131 | |
132 ExperimentalDomain::ExperimentalDomain(internal::MessageDispatcher* dispatcher) | |
133 : Domain(dispatcher) {} | |
134 | |
135 ExperimentalDomain::~ExperimentalDomain() {} | |
136 | |
137 {% if "events" in domain %} | |
138 void ExperimentalDomain::AddObserver(ExperimentalObserver* observer) { | |
139 RegisterEventHandlersIfNeeded(); | |
140 observers_.AddObserver(observer); | |
141 } | |
142 | |
143 void ExperimentalDomain::RemoveObserver(ExperimentalObserver* observer) { | |
144 observers_.RemoveObserver(observer); | |
145 } | |
146 {% endif %} | |
147 | |
148 } // namespace {{domain.domain | camelcase_to_hacker_style}} | |
149 | |
150 } // namespace headless | |
OLD | NEW |