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

Side by Side Diff: third_party/WebKit/Source/platform/inspector_protocol/Dispatcher_cpp.template

Issue 1810843002: DevTools: split protocol Dispatcher into Backend interface and the dispatcher itself. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 4 years, 9 months 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
OLDNEW
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 #include "platform/inspector_protocol/{{class_name}}.h" 7 #include "platform/inspector_protocol/{{class_name}}.h"
8 8
9 #include "platform/inspector_protocol/Collections.h" 9 #include "platform/inspector_protocol/Collections.h"
10 #include "platform/inspector_protocol/FrontendChannel.h" 10 #include "platform/inspector_protocol/FrontendChannel.h"
(...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after
70 return weak.release(); 70 return weak.release();
71 } 71 }
72 72
73 virtual void dispatch(int sessionId, const String16& message); 73 virtual void dispatch(int sessionId, const String16& message);
74 virtual void reportProtocolError(int sessionId, int callId, CommonErrorCode, const String16& errorMessage, ErrorSupport* errors) const; 74 virtual void reportProtocolError(int sessionId, int callId, CommonErrorCode, const String16& errorMessage, ErrorSupport* errors) const;
75 using Dispatcher::reportProtocolError; 75 using Dispatcher::reportProtocolError;
76 76
77 void sendResponse(int sessionId, int callId, const ErrorString& invocationEr ror, ErrorSupport* errors, PassOwnPtr<protocol::DictionaryValue> result); 77 void sendResponse(int sessionId, int callId, const ErrorString& invocationEr ror, ErrorSupport* errors, PassOwnPtr<protocol::DictionaryValue> result);
78 78
79 {% for domain in api.domains %} 79 {% for domain in api.domains %}
80 virtual void registerAgent({{domain.domain}}CommandHandler* agent) { ASSERT( !m_{{domain.domain | lower}}Agent); m_{{domain.domain | lower}}Agent = agent; } 80 virtual void registerAgent(blink::protocol::Backend::{{domain.domain}}* agen t) { ASSERT(!m_{{domain.domain | lower}}Agent); m_{{domain.domain | lower}}Agent = agent; }
81 {% endfor %} 81 {% endfor %}
82 82
83 private: 83 private:
84 friend class CallbackBase; 84 friend class DispatcherCallbackBase;
85 friend class DispatcherImplWeakPtr; 85 friend class DispatcherImplWeakPtr;
86 using CallHandler = void (DispatcherImpl::*)(int sessionId, int callId, Pass OwnPtr<DictionaryValue> messageObject, ErrorSupport* errors); 86 using CallHandler = void (DispatcherImpl::*)(int sessionId, int callId, Pass OwnPtr<DictionaryValue> messageObject, ErrorSupport* errors);
87 using DispatchMap = protocol::HashMap<String16, CallHandler>; 87 using DispatchMap = protocol::HashMap<String16, CallHandler>;
88 88
89 {% for domain in api.domains %} 89 {% for domain in api.domains %}
90 {% for command in domain.commands %} 90 {% for command in domain.commands %}
91 {% if "redirect" in command %}{% continue %}{% endif %} 91 {% if "redirect" in command %}{% continue %}{% endif %}
92 {% if "handlers" in command and not ("renderer" in command["handlers"]) %}{% continue %}{% endif %} 92 {% if "handlers" in command and not ("renderer" in command["handlers"]) %}{% continue %}{% endif %}
93 void {{domain.domain}}_{{command.name}}(int sessionId, int callId, PassOwnPt r<DictionaryValue> requestMessageObject, ErrorSupport*); 93 void {{domain.domain}}_{{command.name}}(int sessionId, int callId, PassOwnPt r<DictionaryValue> requestMessageObject, ErrorSupport*);
94 {% endfor %} 94 {% endfor %}
95 {% endfor %} 95 {% endfor %}
96 96
97 FrontendChannel* m_frontendChannel; 97 FrontendChannel* m_frontendChannel;
98 98
99 {% for domain in api.domains %} 99 {% for domain in api.domains %}
100 {{domain.domain}}CommandHandler* m_{{domain.domain | lower}}Agent; 100 Backend::{{domain.domain}}* m_{{domain.domain | lower}}Agent;
101 {% endfor %} 101 {% endfor %}
102 102
103 void sendResponse(int sessionId, int callId, ErrorString invocationError, Pa ssOwnPtr<protocol::DictionaryValue> result) 103 void sendResponse(int sessionId, int callId, ErrorString invocationError, Pa ssOwnPtr<protocol::DictionaryValue> result)
104 { 104 {
105 sendResponse(sessionId, callId, invocationError, nullptr, result); 105 sendResponse(sessionId, callId, invocationError, nullptr, result);
106 } 106 }
107 107
108 void sendResponse(int sessionId, int callId, ErrorString invocationError) 108 void sendResponse(int sessionId, int callId, ErrorString invocationError)
109 { 109 {
110 sendResponse(sessionId, callId, invocationError, nullptr, DictionaryValu e::create()); 110 sendResponse(sessionId, callId, invocationError, nullptr, DictionaryValu e::create());
111 } 111 }
112 112
113 static const char kInvalidRequest[]; 113 static const char kInvalidRequest[];
114 114
115 DispatchMap m_dispatchMap; 115 DispatchMap m_dispatchMap;
116 protocol::Vector<int> m_commonErrors; 116 protocol::Vector<int> m_commonErrors;
117 protocol::HashSet<DispatcherImplWeakPtr*> m_weakPtrs; 117 protocol::HashSet<DispatcherImplWeakPtr*> m_weakPtrs;
118 }; 118 };
119 119
120 class PLATFORM_EXPORT DispatcherCallbackBase : public protocol::Backend::Callbac kBase {
121 public:
122 DispatcherCallbackBase(PassOwnPtr<DispatcherImplWeakPtr> backendImpl, int se ssionId, int id)
123 : m_backendImpl(backendImpl), m_sessionId(sessionId), m_id(id) { }
124 virtual ~DispatcherCallbackBase() { }
125 void dispose() { m_backendImpl = nullptr; }
126
127 protected:
128 void sendIfActive(PassOwnPtr<protocol::DictionaryValue> partialMessage, cons t ErrorString& invocationError)
129 {
130 if (!m_backendImpl->get())
131 return;
132 m_backendImpl->get()->sendResponse(m_sessionId, m_id, invocationError, n ullptr, partialMessage);
133 m_backendImpl = nullptr;
134 }
135
136 private:
137 OwnPtr<DispatcherImplWeakPtr> m_backendImpl;
138 int m_sessionId;
139 int m_id;
140 };
141
120 DispatcherImplWeakPtr::~DispatcherImplWeakPtr() 142 DispatcherImplWeakPtr::~DispatcherImplWeakPtr()
121 { 143 {
122 if (m_dispatcher) 144 if (m_dispatcher)
123 m_dispatcher->m_weakPtrs.remove(this); 145 m_dispatcher->m_weakPtrs.remove(this);
124 } 146 }
125 147
126 const char DispatcherImpl::kInvalidRequest[] = "Invalid request"; 148 const char DispatcherImpl::kInvalidRequest[] = "Invalid request";
127 149
128 {% for domain in api.domains %} 150 {% for domain in api.domains %}
129 {% for command in domain.commands %} 151 {% for command in domain.commands %}
130 {% if "redirect" in command %}{% continue %}{% endif %} 152 {% if "redirect" in command %}{% continue %}{% endif %}
131 {% if "handlers" in command and not ("renderer" in command["handlers"]) %}{% continue %}{% endif %} 153 {% if "handlers" in command and not ("renderer" in command["handlers"]) %}{% continue %}{% endif %}
132 154
133 {% if "async" in command %} 155 {% if "async" in command %}
134 Dispatcher::{{domain.domain}}CommandHandler::{{command.name | to_title_case}}Cal lback::{{command.name | to_title_case}}Callback(PassOwnPtr<DispatcherImplWeakPtr > backendImpl, int sessionId, int id) : CallbackBase(backendImpl, sessionId, id) { }
135 156
136 void Dispatcher::{{domain.domain}}CommandHandler::{{command.name | to_title_case }}Callback::sendSuccess( 157 class PLATFORM_EXPORT {{domain.domain}}{{command.name | to_title_case}}Callback : public Backend::{{domain.domain}}::{{command.name | to_title_case}}Callback, p ublic DispatcherCallbackBase {
137 {%- for parameter in command.returns -%} 158 public:
138 {%- if "optional" in parameter -%} 159 {{domain.domain}}{{command.name | to_title_case}}Callback(PassOwnPtr<Dispatc herImplWeakPtr> backendImpl, int sessionId, int id)
160 : DispatcherCallbackBase(backendImpl, sessionId, id) { }
161
162 void sendSuccess(
163 {%- for parameter in command.returns -%}
164 {%- if "optional" in parameter -%}
139 const Maybe<{{resolve_type(parameter).raw_type}}>& {{parameter.name}} 165 const Maybe<{{resolve_type(parameter).raw_type}}>& {{parameter.name}}
140 {%- else -%} 166 {%- else -%}
141 {{resolve_type(parameter).pass_type}} {{parameter.name}} 167 {{resolve_type(parameter).pass_type}} {{parameter.name}}
142 {%- endif -%} 168 {%- endif -%}
143 {%- if not loop.last -%}, {% endif -%} 169 {%- if not loop.last -%}, {% endif -%}
144 {% endfor %}) 170 {%- endfor -%}) override
145 { 171 {
146 OwnPtr<protocol::DictionaryValue> resultObject = DictionaryValue::create(); 172 OwnPtr<protocol::DictionaryValue> resultObject = DictionaryValue::create ();
147 {% for parameter in command.returns %} 173 {% for parameter in command.returns %}
148 {% if "optional" in parameter %} 174 {% if "optional" in parameter %}
149 if ({{parameter.name}}.isJust()) 175 if ({{parameter.name}}.isJust())
150 resultObject->setValue("{{parameter.name}}", toValue({{parameter.name}}. fromJust())); 176 resultObject->setValue("{{parameter.name}}", toValue({{parameter.nam e}}.fromJust()));
151 {% else %} 177 {% else %}
152 resultObject->setValue("{{parameter.name}}", toValue({{resolve_type(paramete r).to_raw_type % parameter.name}})); 178 resultObject->setValue("{{parameter.name}}", toValue({{resolve_type(para meter).to_raw_type % parameter.name}}));
153 {% endif %} 179 {% endif %}
154 {% endfor %} 180 {% endfor %}
155 sendIfActive(resultObject.release(), ErrorString()); 181 sendIfActive(resultObject.release(), ErrorString());
156 } 182 }
183
184 void sendFailure(const ErrorString& error) override
185 {
186 ASSERT(error.length());
187 sendIfActive(nullptr, error);
188 }
189 };
157 {% endif %} 190 {% endif %}
158 191
159 void DispatcherImpl::{{domain.domain}}_{{command.name}}(int sessionId, int callI d, PassOwnPtr<DictionaryValue> requestMessageObject, ErrorSupport* errors) 192 void DispatcherImpl::{{domain.domain}}_{{command.name}}(int sessionId, int callI d, PassOwnPtr<DictionaryValue> requestMessageObject, ErrorSupport* errors)
160 { 193 {
161 if (!m_{{domain.domain | lower}}Agent) 194 if (!m_{{domain.domain | lower}}Agent)
162 errors->addError("{{domain.domain}} handler is not available."); 195 errors->addError("{{domain.domain}} handler is not available.");
163 196
164 if (errors->hasErrors()) { 197 if (errors->hasErrors()) {
165 reportProtocolError(sessionId, callId, InvalidParams, kInvalidRequest, e rrors); 198 reportProtocolError(sessionId, callId, InvalidParams, kInvalidRequest, e rrors);
166 return; 199 return;
(...skipping 17 matching lines...) Expand all
184 {% endif %} 217 {% endif %}
185 {% endfor %} 218 {% endfor %}
186 errors->pop(); 219 errors->pop();
187 if (errors->hasErrors()) { 220 if (errors->hasErrors()) {
188 reportProtocolError(sessionId, callId, InvalidParams, kInvalidRequest, e rrors); 221 reportProtocolError(sessionId, callId, InvalidParams, kInvalidRequest, e rrors);
189 return; 222 return;
190 } 223 }
191 {% endif %} 224 {% endif %}
192 225
193 {% if "async" in command %} 226 {% if "async" in command %}
194 OwnPtr<{{domain.domain}}CommandHandler::{{command.name | to_title_case}}Call back> callback = adoptPtr(new {{domain.domain}}CommandHandler::{{command.name | to_title_case}}Callback(weakPtr(), sessionId, callId)); 227 OwnPtr<{{domain.domain}}{{command.name | to_title_case}}Callback> callback = adoptPtr(new {{domain.domain}}{{command.name | to_title_case}}Callback(weakPtr( ), sessionId, callId));
195 {% elif "returns" in command %} 228 {% elif "returns" in command %}
196 // Declare output parameters. 229 // Declare output parameters.
197 OwnPtr<protocol::DictionaryValue> result = DictionaryValue::create(); 230 OwnPtr<protocol::DictionaryValue> result = DictionaryValue::create();
198 {% for property in command.returns %} 231 {% for property in command.returns %}
199 {% if "optional" in property %} 232 {% if "optional" in property %}
200 Maybe<{{resolve_type(property).raw_type}}> out_{{property.name}}; 233 Maybe<{{resolve_type(property).raw_type}}> out_{{property.name}};
201 {% else %} 234 {% else %}
202 {{resolve_type(property).type}} out_{{property.name}}; 235 {{resolve_type(property).type}} out_{{property.name}};
203 {% endif %} 236 {% endif %}
204 {% endfor %} 237 {% endfor %}
(...skipping 116 matching lines...) Expand 10 before | Expand all | Expand 10 after
321 protocol::DictionaryValue* object = DictionaryValue::cast(value.get()); 354 protocol::DictionaryValue* object = DictionaryValue::cast(value.get());
322 if (!object) 355 if (!object)
323 return false; 356 return false;
324 357
325 if (!object->getString("method", result)) 358 if (!object->getString("method", result))
326 return false; 359 return false;
327 360
328 return true; 361 return true;
329 } 362 }
330 363
331 Dispatcher::CallbackBase::CallbackBase(PassOwnPtr<DispatcherImplWeakPtr> backend Impl, int sessionId, int id)
332 : m_backendImpl(backendImpl), m_sessionId(sessionId), m_id(id) { }
333
334 Dispatcher::CallbackBase::~CallbackBase() { }
335
336 void Dispatcher::CallbackBase::sendFailure(const ErrorString& error)
337 {
338 ASSERT(error.length());
339 sendIfActive(nullptr, error);
340 }
341
342 void Dispatcher::CallbackBase::dispose()
343 {
344 m_backendImpl = nullptr;
345 }
346
347 void Dispatcher::CallbackBase::sendIfActive(PassOwnPtr<protocol::DictionaryValue > partialMessage, const ErrorString& invocationError)
348 {
349 if (!m_backendImpl->get())
350 return;
351 m_backendImpl->get()->sendResponse(m_sessionId, m_id, invocationError, nullp tr, partialMessage);
352 m_backendImpl = nullptr;
353 }
354
355 } // namespace protocol 364 } // namespace protocol
356 } // namespace blink 365 } // namespace blink
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698