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 #include "platform/inspector_protocol/{{class_name}}.h" | 7 #include "platform/inspector_protocol/{{class_name}}.h" |
8 | 8 |
9 #include "platform/inspector_protocol/FrontendChannel.h" | 9 #include "platform/inspector_protocol/FrontendChannel.h" |
10 #include "platform/inspector_protocol/Parser.h" | 10 #include "platform/inspector_protocol/Parser.h" |
| 11 #include "wtf/HashSet.h" |
11 #include "wtf/text/CString.h" | 12 #include "wtf/text/CString.h" |
12 | 13 |
13 namespace blink { | 14 namespace blink { |
14 namespace protocol { | 15 namespace protocol { |
15 | 16 |
16 using protocol::Maybe; | 17 using protocol::Maybe; |
17 | 18 |
| 19 class DispatcherImpl; |
| 20 |
| 21 class DispatcherImplWeakPtr { |
| 22 public: |
| 23 DispatcherImplWeakPtr(DispatcherImpl* dispatcher) : m_dispatcher(dispatcher)
{ } |
| 24 ~DispatcherImplWeakPtr(); |
| 25 DispatcherImpl* get() { return m_dispatcher; } |
| 26 void dispose() { m_dispatcher = nullptr; } |
| 27 private: |
| 28 DispatcherImpl* m_dispatcher; |
| 29 }; |
| 30 |
18 class DispatcherImpl : public Dispatcher { | 31 class DispatcherImpl : public Dispatcher { |
19 public: | 32 public: |
20 DispatcherImpl(FrontendChannel* frontendChannel) | 33 DispatcherImpl(FrontendChannel* frontendChannel) |
21 : m_frontendChannel(frontendChannel) | 34 : m_frontendChannel(frontendChannel) |
22 {% for domain in api.domains %} | 35 {% for domain in api.domains %} |
23 , m_{{domain.domain | lower}}Agent(0) | 36 , m_{{domain.domain | lower}}Agent(0) |
24 {% endfor %} | 37 {% endfor %} |
25 { | 38 { |
26 {% for domain in api.domains %} | 39 {% for domain in api.domains %} |
27 {% for command in domain.commands %} | 40 {% for command in domain.commands %} |
28 {% if "redirect" in command %}{% continue %}{% endif %} | 41 {% if "redirect" in command %}{% continue %}{% endif %} |
29 {% if "handlers" in command and not ("renderer" in command["handlers"]) %}{%
continue %}{% endif %} | 42 {% if "handlers" in command and not ("renderer" in command["handlers"]) %}{%
continue %}{% endif %} |
30 m_dispatchMap.add("{{domain.domain}}.{{command.name}}", &DispatcherImpl:
:{{domain.domain}}_{{command.name}}); | 43 m_dispatchMap.add("{{domain.domain}}.{{command.name}}", &DispatcherImpl:
:{{domain.domain}}_{{command.name}}); |
31 {% endfor %} | 44 {% endfor %} |
32 {% endfor %} | 45 {% endfor %} |
33 | 46 |
34 // Initialize common errors. | 47 // Initialize common errors. |
35 m_commonErrors.insert(ParseError, -32700); | 48 m_commonErrors.insert(ParseError, -32700); |
36 m_commonErrors.insert(InvalidRequest, -32600); | 49 m_commonErrors.insert(InvalidRequest, -32600); |
37 m_commonErrors.insert(MethodNotFound, -32601); | 50 m_commonErrors.insert(MethodNotFound, -32601); |
38 m_commonErrors.insert(InvalidParams, -32602); | 51 m_commonErrors.insert(InvalidParams, -32602); |
39 m_commonErrors.insert(InternalError, -32603); | 52 m_commonErrors.insert(InternalError, -32603); |
40 m_commonErrors.insert(ServerError, -32000); | 53 m_commonErrors.insert(ServerError, -32000); |
41 } | 54 } |
42 | 55 |
43 virtual void clearFrontend() { m_frontendChannel = 0; } | 56 ~DispatcherImpl() { clearFrontend(); } |
| 57 |
| 58 virtual void clearFrontend() |
| 59 { |
| 60 m_frontendChannel = nullptr; |
| 61 for (auto& weak : m_weakPtrs) |
| 62 weak->dispose(); |
| 63 m_weakPtrs.clear(); |
| 64 } |
| 65 |
| 66 PassOwnPtr<DispatcherImplWeakPtr> weakPtr() |
| 67 { |
| 68 OwnPtr<DispatcherImplWeakPtr> weak = adoptPtr(new DispatcherImplWeakPtr(
this)); |
| 69 m_weakPtrs.add(weak.get()); |
| 70 return weak.release(); |
| 71 } |
| 72 |
44 virtual void dispatch(int sessionId, const String& message); | 73 virtual void dispatch(int sessionId, const String& message); |
45 virtual void reportProtocolError(int sessionId, int callId, CommonErrorCode,
const String& errorMessage, ErrorSupport* errors) const; | 74 virtual void reportProtocolError(int sessionId, int callId, CommonErrorCode,
const String& errorMessage, ErrorSupport* errors) const; |
46 using Dispatcher::reportProtocolError; | 75 using Dispatcher::reportProtocolError; |
47 | 76 |
48 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); |
49 bool isActive() { return m_frontendChannel; } | |
50 | 78 |
51 {% for domain in api.domains %} | 79 {% for domain in api.domains %} |
52 virtual void registerAgent({{domain.domain}}CommandHandler* agent) { ASSERT(
!m_{{domain.domain | lower}}Agent); m_{{domain.domain | lower}}Agent = agent; } | 80 virtual void registerAgent({{domain.domain}}CommandHandler* agent) { ASSERT(
!m_{{domain.domain | lower}}Agent); m_{{domain.domain | lower}}Agent = agent; } |
53 {% endfor %} | 81 {% endfor %} |
54 | 82 |
55 private: | 83 private: |
| 84 friend class CallbackBase; |
| 85 friend class DispatcherImplWeakPtr; |
56 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); |
57 using DispatchMap = HashMap<String, CallHandler>; | 87 using DispatchMap = HashMap<String, CallHandler>; |
58 | 88 |
59 {% for domain in api.domains %} | 89 {% for domain in api.domains %} |
60 {% for command in domain.commands %} | 90 {% for command in domain.commands %} |
61 {% if "redirect" in command %}{% continue %}{% endif %} | 91 {% if "redirect" in command %}{% continue %}{% endif %} |
62 {% 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 %} |
63 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*); |
64 {% endfor %} | 94 {% endfor %} |
65 {% endfor %} | 95 {% endfor %} |
(...skipping 11 matching lines...) Expand all Loading... |
77 | 107 |
78 void sendResponse(int sessionId, int callId, ErrorString invocationError) | 108 void sendResponse(int sessionId, int callId, ErrorString invocationError) |
79 { | 109 { |
80 sendResponse(sessionId, callId, invocationError, nullptr, DictionaryValu
e::create()); | 110 sendResponse(sessionId, callId, invocationError, nullptr, DictionaryValu
e::create()); |
81 } | 111 } |
82 | 112 |
83 static const char InvalidParamsFormatString[]; | 113 static const char InvalidParamsFormatString[]; |
84 | 114 |
85 DispatchMap m_dispatchMap; | 115 DispatchMap m_dispatchMap; |
86 Vector<int> m_commonErrors; | 116 Vector<int> m_commonErrors; |
| 117 HashSet<DispatcherImplWeakPtr*> m_weakPtrs; |
87 }; | 118 }; |
88 | 119 |
| 120 DispatcherImplWeakPtr::~DispatcherImplWeakPtr() |
| 121 { |
| 122 if (m_dispatcher) |
| 123 m_dispatcher->m_weakPtrs.remove(this); |
| 124 } |
| 125 |
89 const char DispatcherImpl::InvalidParamsFormatString[] = "Some arguments of meth
od '%s' can't be processed"; | 126 const char DispatcherImpl::InvalidParamsFormatString[] = "Some arguments of meth
od '%s' can't be processed"; |
90 {% for domain in api.domains %} | 127 {% for domain in api.domains %} |
91 {% for command in domain.commands %} | 128 {% for command in domain.commands %} |
92 {% if "redirect" in command %}{% continue %}{% endif %} | 129 {% if "redirect" in command %}{% continue %}{% endif %} |
93 {% if "handlers" in command and not ("renderer" in command["handlers"]) %}{%
continue %}{% endif %} | 130 {% if "handlers" in command and not ("renderer" in command["handlers"]) %}{%
continue %}{% endif %} |
94 | 131 |
95 {% if "async" in command %} | 132 {% if "async" in command %} |
96 Dispatcher::{{domain.domain}}CommandHandler::{{command.name | to_title_case}}Cal
lback::{{command.name | to_title_case}}Callback(PassRefPtr<DispatcherImpl> backe
ndImpl, int sessionId, int id) : CallbackBase(backendImpl, sessionId, id) { } | 133 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)
{ } |
97 | 134 |
98 void Dispatcher::{{domain.domain}}CommandHandler::{{command.name | to_title_case
}}Callback::sendSuccess( | 135 void Dispatcher::{{domain.domain}}CommandHandler::{{command.name | to_title_case
}}Callback::sendSuccess( |
99 {%- for parameter in command.returns -%} | 136 {%- for parameter in command.returns -%} |
100 {%- if "optional" in parameter -%} | 137 {%- if "optional" in parameter -%} |
101 const Maybe<{{resolve_type(parameter).raw_type}}>& {{parameter.name}} | 138 const Maybe<{{resolve_type(parameter).raw_type}}>& {{parameter.name}} |
102 {%- else -%} | 139 {%- else -%} |
103 {{resolve_type(parameter).pass_type}} {{parameter.name}} | 140 {{resolve_type(parameter).pass_type}} {{parameter.name}} |
104 {%- endif -%} | 141 {%- endif -%} |
105 {%- if not loop.last -%}, {% endif -%} | 142 {%- if not loop.last -%}, {% endif -%} |
106 {% endfor %}) | 143 {% endfor %}) |
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
146 {% endif %} | 183 {% endif %} |
147 {% endfor %} | 184 {% endfor %} |
148 errors->pop(); | 185 errors->pop(); |
149 if (errors->hasErrors()) { | 186 if (errors->hasErrors()) { |
150 reportProtocolError(sessionId, callId, InvalidParams, String::format(Inv
alidParamsFormatString, "{{domain.domain}}.{{command.name}}"), errors); | 187 reportProtocolError(sessionId, callId, InvalidParams, String::format(Inv
alidParamsFormatString, "{{domain.domain}}.{{command.name}}"), errors); |
151 return; | 188 return; |
152 } | 189 } |
153 {% endif %} | 190 {% endif %} |
154 | 191 |
155 {% if "async" in command %} | 192 {% if "async" in command %} |
156 RefPtr<{{domain.domain}}CommandHandler::{{command.name | to_title_case}}Call
back> callback = adoptRef(new {{domain.domain}}CommandHandler::{{command.name |
to_title_case}}Callback(this, sessionId, callId)); | 193 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)); |
157 {% elif "returns" in command %} | 194 {% elif "returns" in command %} |
158 // Declare output parameters. | 195 // Declare output parameters. |
159 OwnPtr<protocol::DictionaryValue> result = DictionaryValue::create(); | 196 OwnPtr<protocol::DictionaryValue> result = DictionaryValue::create(); |
160 {% for property in command.returns %} | 197 {% for property in command.returns %} |
161 {% if "optional" in property %} | 198 {% if "optional" in property %} |
162 Maybe<{{resolve_type(property).raw_type}}> out_{{property.name}}; | 199 Maybe<{{resolve_type(property).raw_type}}> out_{{property.name}}; |
163 {% else %} | 200 {% else %} |
164 {{resolve_type(property).type}} out_{{property.name}}; | 201 {{resolve_type(property).type}} out_{{property.name}}; |
165 {% endif %} | 202 {% endif %} |
166 {% endfor %} | 203 {% endfor %} |
167 {% endif %} | 204 {% endif %} |
168 | 205 |
| 206 OwnPtr<DispatcherImplWeakPtr> weak = weakPtr(); |
169 ErrorString error; | 207 ErrorString error; |
170 m_{{domain.domain | lower}}Agent->{{command.name}}(&error | 208 m_{{domain.domain | lower}}Agent->{{command.name}}(&error |
171 {%- for property in command.parameters -%} | 209 {%- for property in command.parameters -%} |
172 {%- if "optional" in property -%} | 210 {%- if "optional" in property -%} |
173 , in_{{property.name}} | 211 , in_{{property.name}} |
174 {%- else -%} | 212 {%- else -%} |
175 , {{resolve_type(property).to_pass_type % ("in_" + property.name)}} | 213 , {{resolve_type(property).to_pass_type % ("in_" + property.name)}} |
176 {%- endif -%} | 214 {%- endif -%} |
177 {%- endfor %} | 215 {%- endfor %} |
178 {%- if "async" in command -%} | 216 {%- if "async" in command -%} |
179 , callback.release() | 217 , callback.release() |
180 {%- elif "returns" in command %} | 218 {%- elif "returns" in command %} |
181 {%- for property in command.returns -%} | 219 {%- for property in command.returns -%} |
182 , &out_{{property.name}} | 220 , &out_{{property.name}} |
183 {%- endfor %} | 221 {%- endfor %} |
184 {% endif %}); | 222 {% endif %}); |
185 {% if "returns" in command and not("async" in command) %} | 223 {% if "returns" in command and not("async" in command) %} |
186 if (!error.length()) { | 224 if (!error.length()) { |
187 {% for parameter in command.returns %} | 225 {% for parameter in command.returns %} |
188 {% if "optional" in parameter %} | 226 {% if "optional" in parameter %} |
189 if (out_{{parameter.name}}.isJust()) | 227 if (out_{{parameter.name}}.isJust()) |
190 result->setValue("{{parameter.name}}", toValue(out_{{parameter.name}
}.fromJust())); | 228 result->setValue("{{parameter.name}}", toValue(out_{{parameter.name}
}.fromJust())); |
191 {% else %} | 229 {% else %} |
192 result->setValue("{{parameter.name}}", toValue({{resolve_type(parameter)
.to_raw_type % ("out_" + parameter.name)}})); | 230 result->setValue("{{parameter.name}}", toValue({{resolve_type(parameter)
.to_raw_type % ("out_" + parameter.name)}})); |
193 {% endif %} | 231 {% endif %} |
194 {% endfor %} | 232 {% endfor %} |
195 } | 233 } |
196 sendResponse(sessionId, callId, error, result.release()); | 234 if (weak->get()) |
| 235 weak->get()->sendResponse(sessionId, callId, error, result.release()); |
197 {% elif not("async" in command) %} | 236 {% elif not("async" in command) %} |
198 sendResponse(sessionId, callId, error); | 237 if (weak->get()) |
| 238 weak->get()->sendResponse(sessionId, callId, error); |
199 {% endif %} | 239 {% endif %} |
200 } | 240 } |
201 {% endfor %} | 241 {% endfor %} |
202 {% endfor %} | 242 {% endfor %} |
203 | 243 |
204 PassRefPtr<Dispatcher> Dispatcher::create(FrontendChannel* frontendChannel) | 244 PassOwnPtr<Dispatcher> Dispatcher::create(FrontendChannel* frontendChannel) |
205 { | 245 { |
206 return adoptRef(new DispatcherImpl(frontendChannel)); | 246 return adoptPtr(new DispatcherImpl(frontendChannel)); |
207 } | 247 } |
208 | 248 |
209 void DispatcherImpl::dispatch(int sessionId, const String& message) | 249 void DispatcherImpl::dispatch(int sessionId, const String& message) |
210 { | 250 { |
211 RefPtr<Dispatcher> protect(this); | |
212 int callId = 0; | 251 int callId = 0; |
213 OwnPtr<protocol::Value> parsedMessage = parseJSON(message); | 252 OwnPtr<protocol::Value> parsedMessage = parseJSON(message); |
214 ASSERT(parsedMessage); | 253 ASSERT(parsedMessage); |
215 OwnPtr<protocol::DictionaryValue> messageObject = DictionaryValue::cast(pars
edMessage.release()); | 254 OwnPtr<protocol::DictionaryValue> messageObject = DictionaryValue::cast(pars
edMessage.release()); |
216 ASSERT(messageObject); | 255 ASSERT(messageObject); |
217 | 256 |
218 protocol::Value* callIdValue = messageObject->get("id"); | 257 protocol::Value* callIdValue = messageObject->get("id"); |
219 bool success = callIdValue->asNumber(&callId); | 258 bool success = callIdValue->asNumber(&callId); |
220 ASSERT_UNUSED(success, success); | 259 ASSERT_UNUSED(success, success); |
221 | 260 |
(...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
281 protocol::DictionaryValue* object = DictionaryValue::cast(value.get()); | 320 protocol::DictionaryValue* object = DictionaryValue::cast(value.get()); |
282 if (!object) | 321 if (!object) |
283 return false; | 322 return false; |
284 | 323 |
285 if (!object->getString("method", result)) | 324 if (!object->getString("method", result)) |
286 return false; | 325 return false; |
287 | 326 |
288 return true; | 327 return true; |
289 } | 328 } |
290 | 329 |
291 Dispatcher::CallbackBase::CallbackBase(PassRefPtr<DispatcherImpl> backendImpl, i
nt sessionId, int id) | 330 Dispatcher::CallbackBase::CallbackBase(PassOwnPtr<DispatcherImplWeakPtr> backend
Impl, int sessionId, int id) |
292 : m_backendImpl(backendImpl), m_sessionId(sessionId), m_id(id), m_alreadySen
t(false) { } | 331 : m_backendImpl(backendImpl), m_sessionId(sessionId), m_id(id) { } |
293 | 332 |
294 Dispatcher::CallbackBase::~CallbackBase() { } | 333 Dispatcher::CallbackBase::~CallbackBase() { } |
295 | 334 |
296 void Dispatcher::CallbackBase::sendFailure(const ErrorString& error) | 335 void Dispatcher::CallbackBase::sendFailure(const ErrorString& error) |
297 { | 336 { |
298 ASSERT(error.length()); | 337 ASSERT(error.length()); |
299 sendIfActive(nullptr, error); | 338 sendIfActive(nullptr, error); |
300 } | 339 } |
301 | 340 |
302 bool Dispatcher::CallbackBase::isActive() | 341 void Dispatcher::CallbackBase::dispose() |
303 { | 342 { |
304 return !m_alreadySent && m_backendImpl->isActive(); | 343 m_backendImpl = nullptr; |
305 } | 344 } |
306 | 345 |
307 void Dispatcher::CallbackBase::sendIfActive(PassOwnPtr<protocol::DictionaryValue
> partialMessage, const ErrorString& invocationError) | 346 void Dispatcher::CallbackBase::sendIfActive(PassOwnPtr<protocol::DictionaryValue
> partialMessage, const ErrorString& invocationError) |
308 { | 347 { |
309 if (m_alreadySent) | 348 if (!m_backendImpl->get()) |
310 return; | 349 return; |
311 m_backendImpl->sendResponse(m_sessionId, m_id, invocationError, nullptr, par
tialMessage); | 350 m_backendImpl->get()->sendResponse(m_sessionId, m_id, invocationError, nullp
tr, partialMessage); |
312 m_alreadySent = true; | 351 m_backendImpl = nullptr; |
313 } | 352 } |
314 | 353 |
315 } // namespace protocol | 354 } // namespace protocol |
316 } // namespace blink | 355 } // namespace blink |
OLD | NEW |