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