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

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

Issue 1730383003: DevTools: consistently use Maybe for optional values in the protocol generator. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: review comments addressed 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/JSONParser.h" 9 #include "platform/JSONParser.h"
10 #include "platform/inspector_protocol/FrontendChannel.h" 10 #include "platform/inspector_protocol/FrontendChannel.h"
11 #include "wtf/text/CString.h" 11 #include "wtf/text/CString.h"
12 12
13 namespace blink { 13 namespace blink {
14 namespace protocol { 14 namespace protocol {
15 15
16 using protocol::OptionalValue; 16 using protocol::Maybe;
17 17
18 class DispatcherImpl : public Dispatcher { 18 class DispatcherImpl : public Dispatcher {
19 public: 19 public:
20 DispatcherImpl(FrontendChannel* frontendChannel) 20 DispatcherImpl(FrontendChannel* frontendChannel)
21 : m_frontendChannel(frontendChannel) 21 : m_frontendChannel(frontendChannel)
22 {% for domain in api.domains %} 22 {% for domain in api.domains %}
23 , m_{{domain.domain | lower}}Agent(0) 23 , m_{{domain.domain | lower}}Agent(0)
24 {% endfor %} 24 {% endfor %}
25 { 25 {
26 {% for domain in api.domains %} 26 {% for domain in api.domains %}
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after
66 66
67 FrontendChannel* m_frontendChannel; 67 FrontendChannel* m_frontendChannel;
68 68
69 {% for domain in api.domains %} 69 {% for domain in api.domains %}
70 {{domain.domain}}CommandHandler* m_{{domain.domain | lower}}Agent; 70 {{domain.domain}}CommandHandler* m_{{domain.domain | lower}}Agent;
71 {% endfor %} 71 {% endfor %}
72 72
73 template<typename R, typename V, typename V0> 73 template<typename R, typename V, typename V0>
74 static R getPropertyValueImpl(JSONObject* object, const char* name, bool* va lueFound, JSONArray* protocolErrors, V0 initial_value, bool (*as_method)(PassRef Ptr<JSONValue>, V*), const char* type_name); 74 static R getPropertyValueImpl(JSONObject* object, const char* name, bool* va lueFound, JSONArray* protocolErrors, V0 initial_value, bool (*as_method)(PassRef Ptr<JSONValue>, V*), const char* type_name);
75 75
76 static OptionalValue<int> getInteger(JSONObject* object, const char* name, b ool isOptional, JSONArray* protocolErrors); 76 static Maybe<int> getInteger(JSONObject* object, const char* name, bool isOp tional, JSONArray* protocolErrors);
77 static OptionalValue<double> getNumber(JSONObject* object, const char* name, bool isOptional, JSONArray* protocolErrors); 77 static Maybe<double> getNumber(JSONObject* object, const char* name, bool is Optional, JSONArray* protocolErrors);
78 static OptionalValue<String> getString(JSONObject* object, const char* name, bool isOptional, JSONArray* protocolErrors); 78 static Maybe<String> getString(JSONObject* object, const char* name, bool is Optional, JSONArray* protocolErrors);
79 static OptionalValue<bool> getBoolean(JSONObject* object, const char* name, bool isOptional, JSONArray* protocolErrors); 79 static Maybe<bool> getBoolean(JSONObject* object, const char* name, bool isO ptional, JSONArray* protocolErrors);
80 static PassRefPtr<JSONObject> getObject(JSONObject* object, const char* name , bool isOptional, JSONArray* protocolErrors); 80 static PassRefPtr<JSONObject> getObject(JSONObject* object, const char* name , bool isOptional, JSONArray* protocolErrors);
81 static PassRefPtr<JSONArray> getArray(JSONObject* object, const char* name, bool isOptional, JSONArray* protocolErrors); 81 static PassRefPtr<JSONArray> getArray(JSONObject* object, const char* name, bool isOptional, JSONArray* protocolErrors);
82 82
83 void sendResponse(int sessionId, int callId, ErrorString invocationError, Pa ssRefPtr<JSONObject> result) 83 void sendResponse(int sessionId, int callId, ErrorString invocationError, Pa ssRefPtr<JSONObject> result)
84 { 84 {
85 sendResponse(sessionId, callId, invocationError, RefPtr<JSONValue>(), re sult); 85 sendResponse(sessionId, callId, invocationError, RefPtr<JSONValue>(), re sult);
86 } 86 }
87 87
88 void sendResponse(int sessionId, int callId, ErrorString invocationError) 88 void sendResponse(int sessionId, int callId, ErrorString invocationError)
89 { 89 {
(...skipping 11 matching lines...) Expand all
101 {% for command in domain.commands %} 101 {% for command in domain.commands %}
102 {% if "redirect" in command %}{% continue %}{% endif %} 102 {% if "redirect" in command %}{% continue %}{% endif %}
103 {% if "handlers" in command and not ("renderer" in command["handlers"]) %}{% continue %}{% endif %} 103 {% if "handlers" in command and not ("renderer" in command["handlers"]) %}{% continue %}{% endif %}
104 104
105 {% if "async" in command %} 105 {% if "async" in command %}
106 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) { } 106 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) { }
107 107
108 void Dispatcher::{{domain.domain}}CommandHandler::{{command.name | to_title_case }}Callback::sendSuccess( 108 void Dispatcher::{{domain.domain}}CommandHandler::{{command.name | to_title_case }}Callback::sendSuccess(
109 {%- for parameter in command.returns -%} 109 {%- for parameter in command.returns -%}
110 {%- if "optional" in parameter -%} 110 {%- if "optional" in parameter -%}
111 {{resolve_type(parameter).optional_pass_type}} {{parameter.name}} 111 const Maybe<{{resolve_type(parameter).raw_type}}>& {{parameter.name}}
112 {%- else -%} 112 {%- else -%}
113 {{resolve_type(parameter).pass_type}} {{parameter.name}} 113 {{resolve_type(parameter).pass_type}} {{parameter.name}}
114 {%- endif -%} 114 {%- endif -%}
115 {%- if not loop.last -%}, {% endif -%} 115 {%- if not loop.last -%}, {% endif -%}
116 {% endfor %}) 116 {% endfor %})
117 { 117 {
118 RefPtr<JSONObject> resultObject = JSONObject::create(); 118 RefPtr<JSONObject> resultObject = JSONObject::create();
119 {% for parameter in command.returns %} 119 {% for parameter in command.returns %}
120 {% if "optional" in parameter %} 120 {% if "optional" in parameter %}
121 {{resolve_type(parameter).optional_type}} opt_{{parameter.name}} = {{paramet er.name}}; 121 if ({{parameter.name}}.isJust())
122 if (hasValue({{parameter.name}})) 122 resultObject->setValue("{{parameter.name}}", toValue({{parameter.name}}) );
123 resultObject->setValue("{{parameter.name}}", toValue({{resolve_type(para meter).from_optional_out % ("opt_" + parameter.name)}}));
124 {% else %} 123 {% else %}
125 resultObject->setValue("{{parameter.name}}", toValue({{parameter.name}})); 124 resultObject->setValue("{{parameter.name}}", toValue({{parameter.name}}));
126 {% endif %} 125 {% endif %}
127 {% endfor %} 126 {% endfor %}
128 sendIfActive(resultObject.release(), ErrorString(), PassRefPtr<JSONValue>()) ; 127 sendIfActive(resultObject.release(), ErrorString(), PassRefPtr<JSONValue>()) ;
129 } 128 }
130 {% endif %} 129 {% endif %}
131 130
132 void DispatcherImpl::{{domain.domain}}_{{command.name}}(int sessionId, int callI d, JSONObject* requestMessageObject, JSONArray* protocolErrors) 131 void DispatcherImpl::{{domain.domain}}_{{command.name}}(int sessionId, int callI d, JSONObject* requestMessageObject, JSONArray* protocolErrors)
133 { 132 {
134 if (!m_{{domain.domain | lower}}Agent) 133 if (!m_{{domain.domain | lower}}Agent)
135 protocolErrors->pushString("Inspector handler is not available."); 134 protocolErrors->pushString("Inspector handler is not available.");
136 135
137 if (protocolErrors->length()) { 136 if (protocolErrors->length()) {
138 reportProtocolError(sessionId, callId, InvalidParams, String::format(Inv alidParamsFormatString, "{{domain.domain}}.{{command.name}}"), protocolErrors); 137 reportProtocolError(sessionId, callId, InvalidParams, String::format(Inv alidParamsFormatString, "{{domain.domain}}.{{command.name}}"), protocolErrors);
139 return; 138 return;
140 } 139 }
141 140
142 {% if "parameters" in command %} 141 {% if "parameters" in command %}
143 // Prepare input parameters. 142 // Prepare input parameters.
144 RefPtr<JSONObject> paramsContainer = requestMessageObject->getObject("params "); 143 RefPtr<JSONObject> paramsContainer = requestMessageObject->getObject("params ");
145 JSONObject* paramsContainerPtr = paramsContainer.get(); 144 JSONObject* paramsContainerPtr = paramsContainer.get();
146 {% for property in command.parameters %} 145 {% for property in command.parameters %}
147 {{resolve_type(property).optional_type}} in_{{property.name}} = {{resolve_ty pe(property).json_getter % ("paramsContainerPtr, \"" + property.name + "\", " + ("true" if "optional" in property else "false") + ", protocolErrors")}}; 146 Maybe<{{resolve_type(property).raw_type}}> in_{{property.name}} = {{resolve_ type(property).json_getter % ("paramsContainerPtr, \"" + property.name + "\", " + ("true" if "optional" in property else "false") + ", protocolErrors")}};
148 {% endfor %} 147 {% endfor %}
149 {% endif %} 148 {% endif %}
150 149
151 if (protocolErrors->length()) { 150 if (protocolErrors->length()) {
152 reportProtocolError(sessionId, callId, InvalidParams, String::format(Inv alidParamsFormatString, "{{domain.domain}}.{{command.name}}"), protocolErrors); 151 reportProtocolError(sessionId, callId, InvalidParams, String::format(Inv alidParamsFormatString, "{{domain.domain}}.{{command.name}}"), protocolErrors);
153 return; 152 return;
154 } 153 }
155 154
156 {% if "async" in command %} 155 {% if "async" in command %}
157 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)); 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));
158 {% elif "returns" in command %} 157 {% elif "returns" in command %}
159 // Declare output parameters. 158 // Declare output parameters.
160 RefPtr<JSONObject> result = JSONObject::create(); 159 RefPtr<JSONObject> result = JSONObject::create();
161 {% for property in command.returns %} 160 {% for property in command.returns %}
162 {% if "optional" in property %} 161 {% if "optional" in property %}
163 {{resolve_type(property).optional_type}} out_{{property.name}}; 162 Maybe<{{resolve_type(property).raw_type}}> out_{{property.name}};
164 {% else %} 163 {% else %}
165 {{resolve_type(property).type}} out_{{property.name}}; 164 {{resolve_type(property).type}} out_{{property.name}};
166 {% endif %} 165 {% endif %}
167 {% endfor %} 166 {% endfor %}
168 {% endif %} 167 {% endif %}
169 168
170 ErrorString error; 169 ErrorString error;
171 m_{{domain.domain | lower}}Agent->{{command.name}}(&error 170 m_{{domain.domain | lower}}Agent->{{command.name}}(&error
172 {%- for property in command.parameters -%} 171 {%- for property in command.parameters -%}
173 {%- if "optional" in property -%} 172 {%- if "optional" in property -%}
174 , {{resolve_type(property).to_pass_type % ("in_" + property.name)}} 173 , in_{{property.name}}
175 {%- else -%} 174 {%- else -%}
176 , {{resolve_type(property).from_optional_out % ("in_" + property.name)}} 175 , in_{{property.name}}.takeJust()
177 {%- endif -%} 176 {%- endif -%}
178 {%- endfor %} 177 {%- endfor %}
179 {%- if "async" in command -%} 178 {%- if "async" in command -%}
180 , callback.release() 179 , callback.release()
181 {%- elif "returns" in command %} 180 {%- elif "returns" in command %}
182 {%- for property in command.returns -%} 181 {%- for property in command.returns -%}
183 , &out_{{property.name}} 182 , &out_{{property.name}}
184 {%- endfor %} 183 {%- endfor %}
185 {% endif %}); 184 {% endif %});
186 {% if "returns" in command and not("async" in command) %} 185 {% if "returns" in command and not("async" in command) %}
187 if (!error.length()) { 186 if (!error.length()) {
188 {% for parameter in command.returns %} 187 {% for parameter in command.returns %}
189 {% if "optional" in parameter %} 188 {% if "optional" in parameter %}
190 if (hasValue(out_{{parameter.name}})) 189 if (out_{{parameter.name}}.isJust())
191 result->setValue("{{parameter.name}}", toValue({{resolve_type(parame ter).from_optional_out % ("out_" + parameter.name)}})); 190 result->setValue("{{parameter.name}}", toValue(out_{{parameter.name} }));
192 {% else %} 191 {% else %}
193 result->setValue("{{parameter.name}}", toValue(out_{{resolve_type(parame ter).to_pass_type % parameter.name}})); 192 result->setValue("{{parameter.name}}", toValue(out_{{resolve_type(parame ter).to_pass_type % parameter.name}}));
194 {% endif %} 193 {% endif %}
195 {% endfor %} 194 {% endfor %}
196 } 195 }
197 sendResponse(sessionId, callId, error, result); 196 sendResponse(sessionId, callId, error, result);
198 {% elif not("async" in command) %} 197 {% elif not("async" in command) %}
199 sendResponse(sessionId, callId, error); 198 sendResponse(sessionId, callId, error);
200 {% endif %} 199 {% endif %}
201 } 200 }
(...skipping 107 matching lines...) Expand 10 before | Expand all | Expand 10 after
309 308
310 struct AsMethodBridges { 309 struct AsMethodBridges {
311 static bool asInteger(PassRefPtr<JSONValue> value, int* output) { return val ue->asNumber(output); } 310 static bool asInteger(PassRefPtr<JSONValue> value, int* output) { return val ue->asNumber(output); }
312 static bool asNumber(PassRefPtr<JSONValue> value, double* output) { return v alue->asNumber(output); } 311 static bool asNumber(PassRefPtr<JSONValue> value, double* output) { return v alue->asNumber(output); }
313 static bool asString(PassRefPtr<JSONValue> value, String* output) { return v alue->asString(output); } 312 static bool asString(PassRefPtr<JSONValue> value, String* output) { return v alue->asString(output); }
314 static bool asBoolean(PassRefPtr<JSONValue> value, bool* output) { return va lue->asBoolean(output); } 313 static bool asBoolean(PassRefPtr<JSONValue> value, bool* output) { return va lue->asBoolean(output); }
315 static bool asObject(PassRefPtr<JSONValue> value, RefPtr<JSONObject>* output ) { *output = JSONObject::cast(value); return *output; } 314 static bool asObject(PassRefPtr<JSONValue> value, RefPtr<JSONObject>* output ) { *output = JSONObject::cast(value); return *output; }
316 static bool asArray(PassRefPtr<JSONValue> value, RefPtr<JSONArray>* output) { *output = JSONArray::cast(value); return *output; } 315 static bool asArray(PassRefPtr<JSONValue> value, RefPtr<JSONArray>* output) { *output = JSONArray::cast(value); return *output; }
317 }; 316 };
318 317
319 OptionalValue<int> DispatcherImpl::getInteger(JSONObject* object, const char* na me, bool isOptional, JSONArray* protocolErrors) 318 Maybe<int> DispatcherImpl::getInteger(JSONObject* object, const char* name, bool isOptional, JSONArray* protocolErrors)
320 { 319 {
321 bool valueFound = false; 320 bool valueFound = false;
322 int result = getPropertyValueImpl<int, int, int>(object, name, isOptional ? &valueFound : 0, protocolErrors, 0, AsMethodBridges::asInteger, "Number"); 321 int result = getPropertyValueImpl<int, int, int>(object, name, isOptional ? &valueFound : 0, protocolErrors, 0, AsMethodBridges::asInteger, "Number");
323 return valueFound || !isOptional ? OptionalValue<int>(result) : OptionalValu e<int>(); 322 return valueFound || !isOptional ? Maybe<int>(result) : Maybe<int>();
324 } 323 }
325 324
326 OptionalValue<double> DispatcherImpl::getNumber(JSONObject* object, const char* name, bool isOptional, JSONArray* protocolErrors) 325 Maybe<double> DispatcherImpl::getNumber(JSONObject* object, const char* name, bo ol isOptional, JSONArray* protocolErrors)
327 { 326 {
328 bool valueFound = false; 327 bool valueFound = false;
329 double result = getPropertyValueImpl<double, double, double>(object, name, i sOptional ? &valueFound : 0, protocolErrors, 0, AsMethodBridges::asNumber, "Numb er"); 328 double result = getPropertyValueImpl<double, double, double>(object, name, i sOptional ? &valueFound : 0, protocolErrors, 0, AsMethodBridges::asNumber, "Numb er");
330 return valueFound || !isOptional ? OptionalValue<double>(result) : OptionalV alue<double>(); 329 return valueFound || !isOptional ? Maybe<double>(result) : Maybe<double>();
331 } 330 }
332 331
333 OptionalValue<String> DispatcherImpl::getString(JSONObject* object, const char* name, bool isOptional, JSONArray* protocolErrors) 332 Maybe<String> DispatcherImpl::getString(JSONObject* object, const char* name, bo ol isOptional, JSONArray* protocolErrors)
334 { 333 {
335 bool valueFound = false; 334 bool valueFound = false;
336 String result = getPropertyValueImpl<String, String, String>(object, name, i sOptional ? &valueFound : 0, protocolErrors, "", AsMethodBridges::asString, "Str ing"); 335 String result = getPropertyValueImpl<String, String, String>(object, name, i sOptional ? &valueFound : 0, protocolErrors, "", AsMethodBridges::asString, "Str ing");
337 return valueFound || !isOptional ? OptionalValue<String>(result) : OptionalV alue<String>(); 336 return valueFound || !isOptional ? Maybe<String>(result) : Maybe<String>();
338 } 337 }
339 338
340 OptionalValue<bool> DispatcherImpl::getBoolean(JSONObject* object, const char* n ame, bool isOptional, JSONArray* protocolErrors) 339 Maybe<bool> DispatcherImpl::getBoolean(JSONObject* object, const char* name, boo l isOptional, JSONArray* protocolErrors)
341 { 340 {
342 bool valueFound = false; 341 bool valueFound = false;
343 bool result = getPropertyValueImpl<bool, bool, bool>(object, name, isOptiona l ? &valueFound : 0, protocolErrors, false, AsMethodBridges::asBoolean, "Boolean "); 342 bool result = getPropertyValueImpl<bool, bool, bool>(object, name, isOptiona l ? &valueFound : 0, protocolErrors, false, AsMethodBridges::asBoolean, "Boolean ");
344 return valueFound || !isOptional ? OptionalValue<bool>(result) : OptionalVal ue<bool>(); 343 return valueFound || !isOptional ? Maybe<bool>(result) : Maybe<bool>();
345 } 344 }
346 345
347 PassRefPtr<JSONObject> DispatcherImpl::getObject(JSONObject* object, const char* name, bool isOptional, JSONArray* protocolErrors) 346 PassRefPtr<JSONObject> DispatcherImpl::getObject(JSONObject* object, const char* name, bool isOptional, JSONArray* protocolErrors)
348 { 347 {
349 bool valueFound = false; 348 bool valueFound = false;
350 return getPropertyValueImpl<PassRefPtr<JSONObject>, RefPtr<JSONObject>, JSON Object*>(object, name, isOptional ? &valueFound : 0, protocolErrors, 0, AsMethod Bridges::asObject, "Object"); 349 return getPropertyValueImpl<PassRefPtr<JSONObject>, RefPtr<JSONObject>, JSON Object*>(object, name, isOptional ? &valueFound : 0, protocolErrors, 0, AsMethod Bridges::asObject, "Object");
351 } 350 }
352 351
353 PassRefPtr<JSONArray> DispatcherImpl::getArray(JSONObject* object, const char* n ame, bool isOptional, JSONArray* protocolErrors) 352 PassRefPtr<JSONArray> DispatcherImpl::getArray(JSONObject* object, const char* n ame, bool isOptional, JSONArray* protocolErrors)
354 { 353 {
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after
391 void Dispatcher::CallbackBase::sendIfActive(PassRefPtr<JSONObject> partialMessag e, const ErrorString& invocationError, PassRefPtr<JSONValue> errorData) 390 void Dispatcher::CallbackBase::sendIfActive(PassRefPtr<JSONObject> partialMessag e, const ErrorString& invocationError, PassRefPtr<JSONValue> errorData)
392 { 391 {
393 if (m_alreadySent) 392 if (m_alreadySent)
394 return; 393 return;
395 m_backendImpl->sendResponse(m_sessionId, m_id, invocationError, errorData, p artialMessage); 394 m_backendImpl->sendResponse(m_sessionId, m_id, invocationError, errorData, p artialMessage);
396 m_alreadySent = true; 395 m_alreadySent = true;
397 } 396 }
398 397
399 } // namespace protocol 398 } // namespace protocol
400 } // namespace blink 399 } // namespace blink
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698