Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(225)

Side by Side Diff: headless/lib/browser/domain_cc.template

Issue 2476723002: headless: Register event handlers on demand (Closed)
Patch Set: Created 4 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | headless/lib/browser/domain_h.template » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // This file is generated 1 // This file is generated
2 2
3 // Copyright 2016 The Chromium Authors. All rights reserved. 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 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 #include "headless/public/domains/{{domain.domain | camelcase_to_hacker_style}}. h" 7 #include "headless/public/domains/{{domain.domain | camelcase_to_hacker_style}}. h"
8 8
9 #include "base/bind.h" 9 #include "base/bind.h"
10 10
11 namespace headless { 11 namespace headless {
12 12
13 namespace {{domain.domain | camelcase_to_hacker_style}} { 13 namespace {{domain.domain | camelcase_to_hacker_style}} {
14 14
15 ExperimentalDomain* Domain::GetExperimental() { 15 ExperimentalDomain* Domain::GetExperimental() {
16 return static_cast<ExperimentalDomain*>(this); 16 return static_cast<ExperimentalDomain*>(this);
17 } 17 }
18 18
19 {% if "events" in domain %} 19 {% if "events" in domain %}
20 void Domain::AddObserver(Observer* observer) { 20 void Domain::AddObserver(Observer* observer) {
21 RegisterEventHandlersIfNeeded();
21 observers_.AddObserver(observer); 22 observers_.AddObserver(observer);
22 } 23 }
23 24
24 void Domain::RemoveObserver(Observer* observer) { 25 void Domain::RemoveObserver(Observer* observer) {
25 observers_.RemoveObserver(observer); 26 observers_.RemoveObserver(observer);
26 } 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 }
27 {% endif %} 41 {% endif %}
28 42
29 {% for command in domain.commands %} 43 {% for command in domain.commands %}
30 {# Skip redirected commands. #} 44 {# Skip redirected commands. #}
31 {% if "redirect" in command %}{% continue %}{% endif %} 45 {% if "redirect" in command %}{% continue %}{% endif %}
32 46
33 {% set class_name = 'ExperimentalDomain' if command.experimental else 'Domai n' %} 47 {% set class_name = 'ExperimentalDomain' if command.experimental else 'Domai n' %}
34 {% set method_name = command.name | to_title_case %} 48 {% set method_name = command.name | to_title_case %}
35 void {{class_name}}::{{method_name}}(std::unique_ptr<{{method_name}}Params> para ms, base::Callback<void(std::unique_ptr<{{method_name}}Result>)> callback) { 49 void {{class_name}}::{{method_name}}(std::unique_ptr<{{method_name}}Params> para ms, base::Callback<void(std::unique_ptr<{{method_name}}Result>)> callback) {
36 dispatcher_->SendMessage("{{domain.domain}}.{{command.name}}", params->Seriali ze(), base::Bind(&Domain::Handle{{method_name}}Response, callback)); 50 dispatcher_->SendMessage("{{domain.domain}}.{{command.name}}", params->Seriali ze(), base::Bind(&Domain::Handle{{method_name}}Response, callback));
(...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after
104 DCHECK(!errors.HasErrors()); 118 DCHECK(!errors.HasErrors());
105 for (ExperimentalObserver& observer : observers_) { 119 for (ExperimentalObserver& observer : observers_) {
106 observer.On{{event.name | to_title_case}}(*parsed_params); 120 observer.On{{event.name | to_title_case}}(*parsed_params);
107 } 121 }
108 } 122 }
109 {% endfor %} 123 {% endfor %}
110 {% endif %} 124 {% endif %}
111 125
112 Domain::Domain(internal::MessageDispatcher* dispatcher) 126 Domain::Domain(internal::MessageDispatcher* dispatcher)
113 : dispatcher_(dispatcher) { 127 : dispatcher_(dispatcher) {
114 {% if "events" in domain %}
115 {# Register all events in this domain. #}
116 {% for event in domain.events %}
117 dispatcher_->RegisterEventHandler(
118 "{{domain.domain}}.{{event.name}}",
119 base::Bind(&Domain::Dispatch{{event.name | to_title_case}}Event,
120 base::Unretained(this)));
121 {% endfor %}
122 {% endif %}
123 } 128 }
124 129
125 Domain::~Domain() {} 130 Domain::~Domain() {}
126 131
127 ExperimentalDomain::ExperimentalDomain(internal::MessageDispatcher* dispatcher) 132 ExperimentalDomain::ExperimentalDomain(internal::MessageDispatcher* dispatcher)
128 : Domain(dispatcher) {} 133 : Domain(dispatcher) {}
129 134
130 ExperimentalDomain::~ExperimentalDomain() {} 135 ExperimentalDomain::~ExperimentalDomain() {}
131 136
132 {% if "events" in domain %} 137 {% if "events" in domain %}
133 void ExperimentalDomain::AddObserver(ExperimentalObserver* observer) { 138 void ExperimentalDomain::AddObserver(ExperimentalObserver* observer) {
139 RegisterEventHandlersIfNeeded();
134 observers_.AddObserver(observer); 140 observers_.AddObserver(observer);
135 } 141 }
136 142
137 void ExperimentalDomain::RemoveObserver(ExperimentalObserver* observer) { 143 void ExperimentalDomain::RemoveObserver(ExperimentalObserver* observer) {
138 observers_.RemoveObserver(observer); 144 observers_.RemoveObserver(observer);
139 } 145 }
140 {% endif %} 146 {% endif %}
141 147
142 } // namespace {{domain.domain | camelcase_to_hacker_style}} 148 } // namespace {{domain.domain | camelcase_to_hacker_style}}
143 149
144 } // namespace headless 150 } // namespace headless
OLDNEW
« no previous file with comments | « no previous file | headless/lib/browser/domain_h.template » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698