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