| 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 private: | |
| 124 DISALLOW_COPY_AND_ASSIGN(Domain); | |
| 125 }; | |
| 126 | |
| 127 class ExperimentalDomain : public Domain { | |
| 128 public: | |
| 129 ExperimentalDomain(internal::MessageDispatcher* dispatcher); | |
| 130 ~ExperimentalDomain(); | |
| 131 | |
| 132 {% if "events" in domain %} | |
| 133 // Add or remove an observer. |observer| must be removed before being | |
| 134 // destroyed. | |
| 135 void AddObserver(ExperimentalObserver* observer); | |
| 136 void RemoveObserver(ExperimentalObserver* observer); | |
| 137 {% endif %} | |
| 138 | |
| 139 {# Generate methods for each experimental command. #} | |
| 140 {% for command in domain.commands %} | |
| 141 {# Skip redirected commands. #} | |
| 142 {% if "redirect" in command %}{% continue %}{% endif %} | |
| 143 {# Skip non-experimental commands. #} | |
| 144 {% if not command.experimental %}{% continue %}{% endif %} | |
| 145 {{ command_decl(command) }} | |
| 146 {% endfor %} | |
| 147 | |
| 148 private: | |
| 149 DISALLOW_COPY_AND_ASSIGN(ExperimentalDomain); | |
| 150 }; | |
| 151 | |
| 152 } // namespace {{domain.domain | camelcase_to_hacker_style}} | |
| 153 } // namespace headless | |
| 154 | |
| 155 #endif // HEADLESS_PUBLIC_DOMAINS_{{domain.domain | camelcase_to_hacker_style |
upper}}_H_ | |
| OLD | NEW |