OLD | NEW |
| (Empty) |
1 // This file is generated | |
2 | |
3 // Copyright (c) 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 #ifndef HEADLESS_PUBLIC_DOMAINS_{{domain.domain | camelcase_to_hacker_style | up
per}}_H_ | |
8 #define HEADLESS_PUBLIC_DOMAINS_{{domain.domain | camelcase_to_hacker_style | up
per}}_H_ | |
9 | |
10 #include "base/callback.h" | |
11 #include "base/observer_list.h" | |
12 #include "base/values.h" | |
13 #include "headless/public/domains/types.h" | |
14 #include "headless/public/headless_export.h" | |
15 #include "headless/public/internal/message_dispatcher.h" | |
16 | |
17 {# Macro for defining a member function for a given command. #} | |
18 {% macro command_decl(command) %} | |
19 {% set method_name = command.name | to_title_case %} | |
20 {% if command.description %} | |
21 // {{ command.description }} | |
22 {% endif %} | |
23 void {{method_name}}(std::unique_ptr<{{method_name}}Params> params, base::Call
back<void(std::unique_ptr<{{method_name}}Result>)> callback = base::Callback<voi
d(std::unique_ptr<{{method_name}}Result>)>()); | |
24 {# Generate convenience methods that take the required parameters directly. #} | |
25 {# Don't generate these for experimental commands. #} | |
26 {% if "parameters" in command and not command.experimental %} | |
27 void {{method_name}}({##} | |
28 {% for parameter in command.parameters -%} | |
29 {% if parameter.get("optional", False) -%} | |
30 {% break %} | |
31 {% endif %} | |
32 {% if not loop.first %}, {% endif %} | |
33 {{resolve_type(parameter).pass_type}} {{parameter.name | camelcase_to_hacker_sty
le -}} | |
34 {% endfor %} | |
35 {% if command.get("parameters", []) and not command.parameters[0].get("optio
nal", False) %}, {% endif %}{# -#} | |
36 {% if command.get("returns", []) -%} | |
37 base::Callback<void(std::unique_ptr<{{method_name}}Result>)> callback = ba
se::Callback<void(std::unique_ptr<{{method_name}}Result>)>(){##} | |
38 {% else -%} | |
39 base::Callback<void()> callback = base::Callback<void()>(){##} | |
40 {% endif %}); | |
41 {# If the command has no return value, generate a convenience method that #} | |
42 {# accepts a base::Callback<void()> together with the parameters object. #} | |
43 {% if not command.get("returns", []) %} | |
44 void {{method_name}}(std::unique_ptr<{{method_name}}Params> params, base::Call
back<void()> callback); | |
45 {% endif %} | |
46 {% endif %} | |
47 {% endmacro %} | |
48 | |
49 namespace headless { | |
50 namespace {{domain.domain | camelcase_to_hacker_style}} { | |
51 class ExperimentalDomain; | |
52 class ExperimentalObserver; | |
53 {% if "events" in domain %} | |
54 | |
55 class HEADLESS_EXPORT ExperimentalObserver { | |
56 public: | |
57 virtual ~ExperimentalObserver() {} | |
58 {% for event in domain.events %} | |
59 {% if event.description %} | |
60 // {{event.description}} | |
61 {% endif %} | |
62 virtual void On{{event.name | to_title_case}}(const {{event.name | to_title_ca
se}}Params& params) {} | |
63 {% endfor %} | |
64 }; | |
65 | |
66 class HEADLESS_EXPORT Observer : public ExperimentalObserver { | |
67 public: | |
68 virtual ~Observer() {} | |
69 {% for event in domain.events %} | |
70 {% if event.description %} | |
71 // {% if event.experimental %}Experimental: {% endif %}{{event.description}} | |
72 {% endif %} | |
73 virtual void On{{event.name | to_title_case}}(const {{event.name | to_title_ca
se}}Params& params) {% if event.experimental %}final {% endif %}{} | |
74 {% endfor %} | |
75 }; | |
76 {% endif %} | |
77 | |
78 {% if domain.description %} | |
79 // {{domain.description}} | |
80 {% endif %} | |
81 class HEADLESS_EXPORT Domain { | |
82 public: | |
83 {% if "events" in domain %} | |
84 // Add or remove an observer. |observer| must be removed before being | |
85 // destroyed. | |
86 void AddObserver(Observer* observer); | |
87 void RemoveObserver(Observer* observer); | |
88 {% endif %} | |
89 | |
90 // Return the experimental interface for this domain. Note that experimental | |
91 // commands may be changed or removed at any time. | |
92 ExperimentalDomain* GetExperimental(); | |
93 | |
94 {# Generate methods for each command. #} | |
95 {% for command in domain.commands %} | |
96 {# Skip redirected commands. #} | |
97 {% if "redirect" in command %}{% continue %}{% endif %} | |
98 {# Skip experimental commands. #} | |
99 {% if command.experimental %}{% continue %}{% endif %} | |
100 {{ command_decl(command) }} | |
101 {% endfor %} | |
102 protected: | |
103 Domain(internal::MessageDispatcher* dispatcher); | |
104 ~Domain(); | |
105 | |
106 {# Generate response handlers for commands that need them. #} | |
107 {% for command in domain.commands %} | |
108 {% if not "returns" in command %}{% continue %}{% endif %} | |
109 {% set method_name = command.name | to_title_case %} | |
110 static void Handle{{method_name}}Response(base::Callback<void(std::unique_ptr<
{{method_name}}Result>)> callback, const base::Value& response); | |
111 {% endfor %} | |
112 | |
113 {# Generate event dispatchers. #} | |
114 {% for event in domain.events %} | |
115 void Dispatch{{event.name | to_title_case}}Event(const base::Value& params); | |
116 {% endfor %} | |
117 | |
118 internal::MessageDispatcher* dispatcher_; // Not owned. | |
119 {% if "events" in domain %} | |
120 base::ObserverList<ExperimentalObserver> observers_; | |
121 {% endif %} | |
122 | |
123 {% if "events" in domain %} | |
124 protected: | |
125 void RegisterEventHandlersIfNeeded(); | |
126 | |
127 {% endif %} | |
128 private: | |
129 {% if "events" in domain %} | |
130 bool event_handlers_registered_ = false; | |
131 | |
132 {% endif %} | |
133 DISALLOW_COPY_AND_ASSIGN(Domain); | |
134 }; | |
135 | |
136 class ExperimentalDomain : public Domain { | |
137 public: | |
138 ExperimentalDomain(internal::MessageDispatcher* dispatcher); | |
139 ~ExperimentalDomain(); | |
140 | |
141 {% if "events" in domain %} | |
142 // Add or remove an observer. |observer| must be removed before being | |
143 // destroyed. | |
144 void AddObserver(ExperimentalObserver* observer); | |
145 void RemoveObserver(ExperimentalObserver* observer); | |
146 {% endif %} | |
147 | |
148 {# Generate methods for each experimental command. #} | |
149 {% for command in domain.commands %} | |
150 {# Skip redirected commands. #} | |
151 {% if "redirect" in command %}{% continue %}{% endif %} | |
152 {# Skip non-experimental commands. #} | |
153 {% if not command.experimental %}{% continue %}{% endif %} | |
154 {{ command_decl(command) }} | |
155 {% endfor %} | |
156 | |
157 private: | |
158 DISALLOW_COPY_AND_ASSIGN(ExperimentalDomain); | |
159 }; | |
160 | |
161 } // namespace {{domain.domain | camelcase_to_hacker_style}} | |
162 } // namespace headless | |
163 | |
164 #endif // HEADLESS_PUBLIC_DOMAINS_{{domain.domain | camelcase_to_hacker_style |
upper}}_H_ | |
OLD | NEW |