Chromium Code Reviews| 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/JSONParser.h" | 9 #include "platform/JSONParser.h" |
| 10 #include "platform/inspector_protocol/FrontendChannel.h" | 10 #include "platform/inspector_protocol/FrontendChannel.h" |
| (...skipping 24 matching lines...) Expand all Loading... | |
| 35 m_commonErrors.insert(ParseError, -32700); | 35 m_commonErrors.insert(ParseError, -32700); |
| 36 m_commonErrors.insert(InvalidRequest, -32600); | 36 m_commonErrors.insert(InvalidRequest, -32600); |
| 37 m_commonErrors.insert(MethodNotFound, -32601); | 37 m_commonErrors.insert(MethodNotFound, -32601); |
| 38 m_commonErrors.insert(InvalidParams, -32602); | 38 m_commonErrors.insert(InvalidParams, -32602); |
| 39 m_commonErrors.insert(InternalError, -32603); | 39 m_commonErrors.insert(InternalError, -32603); |
| 40 m_commonErrors.insert(ServerError, -32000); | 40 m_commonErrors.insert(ServerError, -32000); |
| 41 } | 41 } |
| 42 | 42 |
| 43 virtual void clearFrontend() { m_frontendChannel = 0; } | 43 virtual void clearFrontend() { m_frontendChannel = 0; } |
| 44 virtual void dispatch(int sessionId, const String& message); | 44 virtual void dispatch(int sessionId, const String& message); |
| 45 virtual void reportProtocolError(int sessionId, int callId, CommonErrorCode, const String& errorMessage, PassRefPtr<JSONValue> data) const; | 45 virtual void reportProtocolError(int sessionId, int callId, CommonErrorCode, const String& errorMessage, ErrorSupport* errors) const; |
| 46 using Dispatcher::reportProtocolError; | 46 using Dispatcher::reportProtocolError; |
| 47 | 47 |
| 48 void sendResponse(int sessionId, int callId, const ErrorString& invocationEr ror, PassRefPtr<JSONValue> errorData, PassRefPtr<JSONObject> result); | 48 void sendResponse(int sessionId, int callId, const ErrorString& invocationEr ror, ErrorSupport* errors, PassRefPtr<JSONObject> result); |
| 49 bool isActive() { return m_frontendChannel; } | 49 bool isActive() { return m_frontendChannel; } |
| 50 | 50 |
| 51 {% for domain in api.domains %} | 51 {% 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; } | 52 virtual void registerAgent({{domain.domain}}CommandHandler* agent) { ASSERT( !m_{{domain.domain | lower}}Agent); m_{{domain.domain | lower}}Agent = agent; } |
| 53 {% endfor %} | 53 {% endfor %} |
| 54 | 54 |
| 55 private: | 55 private: |
| 56 using CallHandler = void (DispatcherImpl::*)(int sessionId, int callId, JSON Object* messageObject, JSONArray* protocolErrors); | 56 using CallHandler = void (DispatcherImpl::*)(int sessionId, int callId, JSON Object* messageObject, ErrorSupport* errors); |
| 57 using DispatchMap = HashMap<String, CallHandler>; | 57 using DispatchMap = HashMap<String, CallHandler>; |
| 58 | 58 |
| 59 {% for domain in api.domains %} | 59 {% for domain in api.domains %} |
| 60 {% for command in domain.commands %} | 60 {% for command in domain.commands %} |
| 61 {% if "redirect" in command %}{% continue %}{% endif %} | 61 {% if "redirect" in command %}{% continue %}{% endif %} |
| 62 {% if "handlers" in command and not ("renderer" in command["handlers"]) %}{% continue %}{% endif %} | 62 {% if "handlers" in command and not ("renderer" in command["handlers"]) %}{% continue %}{% endif %} |
| 63 void {{domain.domain}}_{{command.name}}(int sessionId, int callId, JSONObjec t* requestMessageObject, JSONArray* protocolErrors); | 63 void {{domain.domain}}_{{command.name}}(int sessionId, int callId, JSONObjec t* requestMessageObject, ErrorSupport*); |
| 64 {% endfor %} | 64 {% endfor %} |
| 65 {% endfor %} | 65 {% endfor %} |
| 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> | |
| 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 | |
| 76 static Maybe<int> getInteger(JSONObject* object, const char* name, bool isOp tional, JSONArray* protocolErrors); | |
| 77 static Maybe<double> getNumber(JSONObject* object, const char* name, bool is Optional, JSONArray* protocolErrors); | |
| 78 static Maybe<String> getString(JSONObject* object, const char* name, bool is Optional, 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); | |
| 81 static PassRefPtr<JSONArray> getArray(JSONObject* object, const char* name, bool isOptional, JSONArray* protocolErrors); | |
| 82 | |
| 83 void sendResponse(int sessionId, int callId, ErrorString invocationError, Pa ssRefPtr<JSONObject> result) | 73 void sendResponse(int sessionId, int callId, ErrorString invocationError, Pa ssRefPtr<JSONObject> result) |
| 84 { | 74 { |
| 85 sendResponse(sessionId, callId, invocationError, RefPtr<JSONValue>(), re sult); | 75 sendResponse(sessionId, callId, invocationError, nullptr, result); |
| 86 } | 76 } |
| 87 | 77 |
| 88 void sendResponse(int sessionId, int callId, ErrorString invocationError) | 78 void sendResponse(int sessionId, int callId, ErrorString invocationError) |
| 89 { | 79 { |
| 90 sendResponse(sessionId, callId, invocationError, RefPtr<JSONValue>(), JS ONObject::create()); | 80 sendResponse(sessionId, callId, invocationError, nullptr, JSONObject::cr eate()); |
| 91 } | 81 } |
| 92 | 82 |
| 93 static const char InvalidParamsFormatString[]; | 83 static const char InvalidParamsFormatString[]; |
| 94 | 84 |
| 95 DispatchMap m_dispatchMap; | 85 DispatchMap m_dispatchMap; |
| 96 Vector<int> m_commonErrors; | 86 Vector<int> m_commonErrors; |
| 97 }; | 87 }; |
| 98 | 88 |
| 99 const char DispatcherImpl::InvalidParamsFormatString[] = "Some arguments of meth od '%s' can't be processed"; | 89 const char DispatcherImpl::InvalidParamsFormatString[] = "Some arguments of meth od '%s' can't be processed"; |
| 100 {% for domain in api.domains %} | 90 {% for domain in api.domains %} |
| (...skipping 11 matching lines...) Expand all Loading... | |
| 112 {%- else -%} | 102 {%- else -%} |
| 113 {{resolve_type(parameter).pass_type}} {{parameter.name}} | 103 {{resolve_type(parameter).pass_type}} {{parameter.name}} |
| 114 {%- endif -%} | 104 {%- endif -%} |
| 115 {%- if not loop.last -%}, {% endif -%} | 105 {%- if not loop.last -%}, {% endif -%} |
| 116 {% endfor %}) | 106 {% endfor %}) |
| 117 { | 107 { |
| 118 RefPtr<JSONObject> resultObject = JSONObject::create(); | 108 RefPtr<JSONObject> resultObject = JSONObject::create(); |
| 119 {% for parameter in command.returns %} | 109 {% for parameter in command.returns %} |
| 120 {% if "optional" in parameter %} | 110 {% if "optional" in parameter %} |
| 121 if ({{parameter.name}}.isJust()) | 111 if ({{parameter.name}}.isJust()) |
| 122 resultObject->setValue("{{parameter.name}}", toValue({{parameter.name}}) ); | 112 resultObject->setValue("{{parameter.name}}", toValue({{parameter.name}}. fromJust())); |
| 123 {% else %} | 113 {% else %} |
| 124 resultObject->setValue("{{parameter.name}}", toValue({{parameter.name}})); | 114 resultObject->setValue("{{parameter.name}}", toValue({{parameter.name}})); |
| 125 {% endif %} | 115 {% endif %} |
| 126 {% endfor %} | 116 {% endfor %} |
| 127 sendIfActive(resultObject.release(), ErrorString(), PassRefPtr<JSONValue>()) ; | 117 sendIfActive(resultObject.release(), ErrorString()); |
| 128 } | 118 } |
| 129 {% endif %} | 119 {% endif %} |
| 130 | 120 |
| 131 void DispatcherImpl::{{domain.domain}}_{{command.name}}(int sessionId, int callI d, JSONObject* requestMessageObject, JSONArray* protocolErrors) | 121 void DispatcherImpl::{{domain.domain}}_{{command.name}}(int sessionId, int callI d, JSONObject* requestMessageObject, ErrorSupport* errors) |
| 132 { | 122 { |
| 133 if (!m_{{domain.domain | lower}}Agent) | 123 if (!m_{{domain.domain | lower}}Agent) |
| 134 protocolErrors->pushString("Inspector handler is not available."); | 124 errors->addError("Inspector handler is not available."); |
|
dgozman
2016/02/25 21:40:36
Can we say {{domain.domain}} handler is not availa
| |
| 135 | 125 |
| 136 if (protocolErrors->length()) { | 126 if (errors->hasErrors()) { |
| 137 reportProtocolError(sessionId, callId, InvalidParams, String::format(Inv alidParamsFormatString, "{{domain.domain}}.{{command.name}}"), protocolErrors); | 127 reportProtocolError(sessionId, callId, InvalidParams, String::format(Inv alidParamsFormatString, "{{domain.domain}}.{{command.name}}"), errors); |
| 138 return; | 128 return; |
| 139 } | 129 } |
| 130 {% if "parameters" in command %} | |
| 140 | 131 |
| 141 {% if "parameters" in command %} | |
| 142 // Prepare input parameters. | 132 // Prepare input parameters. |
| 143 RefPtr<JSONObject> paramsContainer = requestMessageObject->getObject("params "); | 133 RefPtr<JSONObject> object = JSONObject::cast(requestMessageObject->get("para ms")); |
| 144 JSONObject* paramsContainerPtr = paramsContainer.get(); | 134 errors->push(); |
| 145 {% for property in command.parameters %} | 135 {% for property in command.parameters %} |
| 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")}}; | 136 RefPtr<JSONValue> {{property.name}}Value = object ? object->get("{{property. name}}") : nullptr; |
| 137 {% if property.optional %} | |
| 138 Maybe<{{resolve_type(property).raw_type}}> in_{{property.name}}; | |
| 139 if ({{property.name}}Value) { | |
| 140 errors->setName("{{property.name}}"); | |
| 141 in_{{property.name}} = FromValue<{{resolve_type(property).raw_type}}>::p arse({{property.name}}Value, errors); | |
| 142 } | |
| 143 {% else %} | |
| 144 errors->setName("{{property.name}}"); | |
| 145 {{resolve_type(property).type}} in_{{property.name}} = FromValue<{{resolve_t ype(property).raw_type}}>::parse({{property.name}}Value, errors); | |
| 146 {% endif %} | |
| 147 {% endfor %} | 147 {% endfor %} |
| 148 {% endif %} | 148 errors->pop(); |
| 149 | 149 if (errors->hasErrors()) { |
| 150 if (protocolErrors->length()) { | 150 reportProtocolError(sessionId, callId, InvalidParams, String::format(Inv alidParamsFormatString, "{{domain.domain}}.{{command.name}}"), errors); |
| 151 reportProtocolError(sessionId, callId, InvalidParams, String::format(Inv alidParamsFormatString, "{{domain.domain}}.{{command.name}}"), protocolErrors); | |
| 152 return; | 151 return; |
| 153 } | 152 } |
| 153 {% endif %} | |
| 154 | 154 |
| 155 {% if "async" in command %} | 155 {% 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)); | 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)); |
| 157 {% elif "returns" in command %} | 157 {% elif "returns" in command %} |
| 158 // Declare output parameters. | 158 // Declare output parameters. |
| 159 RefPtr<JSONObject> result = JSONObject::create(); | 159 RefPtr<JSONObject> result = JSONObject::create(); |
| 160 {% for property in command.returns %} | 160 {% for property in command.returns %} |
| 161 {% if "optional" in property %} | 161 {% if "optional" in property %} |
| 162 Maybe<{{resolve_type(property).raw_type}}> out_{{property.name}}; | 162 Maybe<{{resolve_type(property).raw_type}}> out_{{property.name}}; |
| 163 {% else %} | 163 {% else %} |
| 164 {{resolve_type(property).type}} out_{{property.name}}; | 164 {{resolve_type(property).type}} out_{{property.name}}; |
| 165 {% endif %} | 165 {% endif %} |
| 166 {% endfor %} | 166 {% endfor %} |
| 167 {% endif %} | 167 {% endif %} |
| 168 | 168 |
| 169 ErrorString error; | 169 ErrorString error; |
| 170 m_{{domain.domain | lower}}Agent->{{command.name}}(&error | 170 m_{{domain.domain | lower}}Agent->{{command.name}}(&error |
| 171 {%- for property in command.parameters -%} | 171 {%- for property in command.parameters -%} |
| 172 {%- if "optional" in property -%} | 172 {%- if "optional" in property -%} |
| 173 , in_{{property.name}} | 173 , in_{{property.name}} |
| 174 {%- else -%} | 174 {%- else -%} |
| 175 , in_{{property.name}}.takeJust() | 175 , {{resolve_type(property).to_pass_type % ("in_" + property.name)}} |
| 176 {%- endif -%} | 176 {%- endif -%} |
| 177 {%- endfor %} | 177 {%- endfor %} |
| 178 {%- if "async" in command -%} | 178 {%- if "async" in command -%} |
| 179 , callback.release() | 179 , callback.release() |
| 180 {%- elif "returns" in command %} | 180 {%- elif "returns" in command %} |
| 181 {%- for property in command.returns -%} | 181 {%- for property in command.returns -%} |
| 182 , &out_{{property.name}} | 182 , &out_{{property.name}} |
| 183 {%- endfor %} | 183 {%- endfor %} |
| 184 {% endif %}); | 184 {% endif %}); |
| 185 {% if "returns" in command and not("async" in command) %} | 185 {% if "returns" in command and not("async" in command) %} |
| 186 if (!error.length()) { | 186 if (!error.length()) { |
| 187 {% for parameter in command.returns %} | 187 {% for parameter in command.returns %} |
| 188 {% if "optional" in parameter %} | 188 {% if "optional" in parameter %} |
| 189 if (out_{{parameter.name}}.isJust()) | 189 if (out_{{parameter.name}}.isJust()) |
| 190 result->setValue("{{parameter.name}}", toValue(out_{{parameter.name} })); | 190 result->setValue("{{parameter.name}}", toValue(out_{{parameter.name} }.fromJust())); |
| 191 {% else %} | 191 {% else %} |
| 192 result->setValue("{{parameter.name}}", toValue(out_{{resolve_type(parame ter).to_pass_type % parameter.name}})); | 192 result->setValue("{{parameter.name}}", toValue(out_{{parameter.name}})); |
| 193 {% endif %} | 193 {% endif %} |
| 194 {% endfor %} | 194 {% endfor %} |
| 195 } | 195 } |
| 196 sendResponse(sessionId, callId, error, result); | 196 sendResponse(sessionId, callId, error, result); |
| 197 {% elif not("async" in command) %} | 197 {% elif not("async" in command) %} |
| 198 sendResponse(sessionId, callId, error); | 198 sendResponse(sessionId, callId, error); |
| 199 {% endif %} | 199 {% endif %} |
| 200 } | 200 } |
| 201 {% endfor %} | 201 {% endfor %} |
| 202 {% endfor %} | 202 {% endfor %} |
| (...skipping 20 matching lines...) Expand all Loading... | |
| 223 String method; | 223 String method; |
| 224 success = methodValue && methodValue->asString(&method); | 224 success = methodValue && methodValue->asString(&method); |
| 225 ASSERT_UNUSED(success, success); | 225 ASSERT_UNUSED(success, success); |
| 226 | 226 |
| 227 HashMap<String, CallHandler>::iterator it = m_dispatchMap.find(method); | 227 HashMap<String, CallHandler>::iterator it = m_dispatchMap.find(method); |
| 228 if (it == m_dispatchMap.end()) { | 228 if (it == m_dispatchMap.end()) { |
| 229 reportProtocolError(sessionId, callId, MethodNotFound, "'" + method + "' wasn't found"); | 229 reportProtocolError(sessionId, callId, MethodNotFound, "'" + method + "' wasn't found"); |
| 230 return; | 230 return; |
| 231 } | 231 } |
| 232 | 232 |
| 233 RefPtr<JSONArray> protocolErrors = JSONArray::create(); | 233 protocol::ErrorSupport errors; |
| 234 ((*this).*it->value)(sessionId, callId, messageObject.get(), protocolErrors. get()); | 234 ((*this).*it->value)(sessionId, callId, messageObject.get(), &errors); |
| 235 } | 235 } |
| 236 | 236 |
| 237 void DispatcherImpl::sendResponse(int sessionId, int callId, const ErrorString& invocationError, PassRefPtr<JSONValue> errorData, PassRefPtr<JSONObject> result) | 237 void DispatcherImpl::sendResponse(int sessionId, int callId, const ErrorString& invocationError, ErrorSupport* errors, PassRefPtr<JSONObject> result) |
| 238 { | 238 { |
| 239 if (invocationError.length()) { | 239 if (invocationError.length() || (errors && errors->hasErrors())) { |
| 240 reportProtocolError(sessionId, callId, ServerError, invocationError, err orData); | 240 reportProtocolError(sessionId, callId, ServerError, invocationError, err ors); |
| 241 return; | 241 return; |
| 242 } | 242 } |
| 243 | 243 |
| 244 RefPtr<JSONObject> responseMessage = JSONObject::create(); | 244 RefPtr<JSONObject> responseMessage = JSONObject::create(); |
| 245 responseMessage->setNumber("id", callId); | 245 responseMessage->setNumber("id", callId); |
| 246 responseMessage->setObject("result", result); | 246 responseMessage->setObject("result", result); |
| 247 if (m_frontendChannel) | 247 if (m_frontendChannel) |
| 248 m_frontendChannel->sendProtocolResponse(sessionId, callId, responseMessa ge.release()); | 248 m_frontendChannel->sendProtocolResponse(sessionId, callId, responseMessa ge.release()); |
| 249 } | 249 } |
| 250 | 250 |
| 251 void Dispatcher::reportProtocolError(int sessionId, int callId, CommonErrorCode code, const String& errorMessage) const | 251 void Dispatcher::reportProtocolError(int sessionId, int callId, CommonErrorCode code, const String& errorMessage) const |
| 252 { | 252 { |
| 253 reportProtocolError(sessionId, callId, code, errorMessage, PassRefPtr<JSONVa lue>()); | 253 ErrorSupport errors; |
| 254 reportProtocolError(sessionId, callId, code, errorMessage, &errors); | |
| 254 } | 255 } |
| 255 | 256 |
| 256 void DispatcherImpl::reportProtocolError(int sessionId, int callId, CommonErrorC ode code, const String& errorMessage, PassRefPtr<JSONValue> data) const | 257 void DispatcherImpl::reportProtocolError(int sessionId, int callId, CommonErrorC ode code, const String& errorMessage, ErrorSupport* errors) const |
| 257 { | 258 { |
| 258 ASSERT(code >=0); | 259 ASSERT(code >=0); |
| 259 ASSERT((unsigned)code < m_commonErrors.size()); | 260 ASSERT((unsigned)code < m_commonErrors.size()); |
| 260 ASSERT(m_commonErrors[code]); | 261 ASSERT(m_commonErrors[code]); |
| 261 RefPtr<JSONObject> error = JSONObject::create(); | 262 RefPtr<JSONObject> error = JSONObject::create(); |
| 262 error->setNumber("code", m_commonErrors[code]); | 263 error->setNumber("code", m_commonErrors[code]); |
| 263 error->setString("message", errorMessage); | 264 error->setString("message", errorMessage); |
| 264 ASSERT(error); | 265 ASSERT(error); |
| 265 if (data) | 266 if (errors && errors->hasErrors()) |
| 266 error->setValue("data", data); | 267 error->setString("data", errors->errors()); |
| 267 RefPtr<JSONObject> message = JSONObject::create(); | 268 RefPtr<JSONObject> message = JSONObject::create(); |
| 268 message->setObject("error", error); | 269 message->setObject("error", error); |
| 269 message->setNumber("id", callId); | 270 message->setNumber("id", callId); |
| 270 if (m_frontendChannel) | 271 if (m_frontendChannel) |
| 271 m_frontendChannel->sendProtocolResponse(sessionId, callId, message.relea se()); | 272 m_frontendChannel->sendProtocolResponse(sessionId, callId, message.relea se()); |
| 272 } | 273 } |
| 273 | 274 |
| 274 template<typename R, typename V, typename V0> | |
| 275 R DispatcherImpl::getPropertyValueImpl(JSONObject* object, const char* name, boo l* valueFound, JSONArray* protocolErrors, V0 initial_value, bool (*as_method)(Pa ssRefPtr<JSONValue>, V*), const char* type_name) | |
| 276 { | |
| 277 ASSERT(protocolErrors); | |
| 278 | |
| 279 if (valueFound) | |
| 280 *valueFound = false; | |
| 281 | |
| 282 V value = initial_value; | |
| 283 | |
| 284 if (!object) { | |
| 285 if (!valueFound) { | |
| 286 // Required parameter in missing params container. | |
| 287 protocolErrors->pushString(String::format("'params' object must cont ain required parameter '%s' with type '%s'.", name, type_name)); | |
| 288 } | |
| 289 return value; | |
| 290 } | |
| 291 | |
| 292 JSONObject::const_iterator end = object->end(); | |
| 293 JSONObject::const_iterator valueIterator = object->find(name); | |
| 294 | |
| 295 if (valueIterator == end) { | |
| 296 if (!valueFound) | |
| 297 protocolErrors->pushString(String::format("Parameter '%s' with type '%s' was not found.", name, type_name)); | |
| 298 return value; | |
| 299 } | |
| 300 | |
| 301 if (!as_method(valueIterator->value.get(), &value)) | |
| 302 protocolErrors->pushString(String::format("Parameter '%s' has wrong type . It must be '%s'.", name, type_name)); | |
| 303 else | |
| 304 if (valueFound) | |
| 305 *valueFound = true; | |
| 306 return value; | |
| 307 } | |
| 308 | |
| 309 struct AsMethodBridges { | |
| 310 static bool asInteger(PassRefPtr<JSONValue> value, int* output) { return val ue->asNumber(output); } | |
| 311 static bool asNumber(PassRefPtr<JSONValue> value, double* output) { return v alue->asNumber(output); } | |
| 312 static bool asString(PassRefPtr<JSONValue> value, String* output) { return v alue->asString(output); } | |
| 313 static bool asBoolean(PassRefPtr<JSONValue> value, bool* output) { return va lue->asBoolean(output); } | |
| 314 static bool asObject(PassRefPtr<JSONValue> value, RefPtr<JSONObject>* output ) { *output = JSONObject::cast(value); return *output; } | |
| 315 static bool asArray(PassRefPtr<JSONValue> value, RefPtr<JSONArray>* output) { *output = JSONArray::cast(value); return *output; } | |
| 316 }; | |
| 317 | |
| 318 Maybe<int> DispatcherImpl::getInteger(JSONObject* object, const char* name, bool isOptional, JSONArray* protocolErrors) | |
| 319 { | |
| 320 bool valueFound = false; | |
| 321 int result = getPropertyValueImpl<int, int, int>(object, name, isOptional ? &valueFound : 0, protocolErrors, 0, AsMethodBridges::asInteger, "Number"); | |
| 322 return valueFound || !isOptional ? Maybe<int>(result) : Maybe<int>(); | |
| 323 } | |
| 324 | |
| 325 Maybe<double> DispatcherImpl::getNumber(JSONObject* object, const char* name, bo ol isOptional, JSONArray* protocolErrors) | |
| 326 { | |
| 327 bool valueFound = false; | |
| 328 double result = getPropertyValueImpl<double, double, double>(object, name, i sOptional ? &valueFound : 0, protocolErrors, 0, AsMethodBridges::asNumber, "Numb er"); | |
| 329 return valueFound || !isOptional ? Maybe<double>(result) : Maybe<double>(); | |
| 330 } | |
| 331 | |
| 332 Maybe<String> DispatcherImpl::getString(JSONObject* object, const char* name, bo ol isOptional, JSONArray* protocolErrors) | |
| 333 { | |
| 334 bool valueFound = false; | |
| 335 String result = getPropertyValueImpl<String, String, String>(object, name, i sOptional ? &valueFound : 0, protocolErrors, "", AsMethodBridges::asString, "Str ing"); | |
| 336 return valueFound || !isOptional ? Maybe<String>(result) : Maybe<String>(); | |
| 337 } | |
| 338 | |
| 339 Maybe<bool> DispatcherImpl::getBoolean(JSONObject* object, const char* name, boo l isOptional, JSONArray* protocolErrors) | |
| 340 { | |
| 341 bool valueFound = false; | |
| 342 bool result = getPropertyValueImpl<bool, bool, bool>(object, name, isOptiona l ? &valueFound : 0, protocolErrors, false, AsMethodBridges::asBoolean, "Boolean "); | |
| 343 return valueFound || !isOptional ? Maybe<bool>(result) : Maybe<bool>(); | |
| 344 } | |
| 345 | |
| 346 PassRefPtr<JSONObject> DispatcherImpl::getObject(JSONObject* object, const char* name, bool isOptional, JSONArray* protocolErrors) | |
| 347 { | |
| 348 bool valueFound = false; | |
| 349 return getPropertyValueImpl<PassRefPtr<JSONObject>, RefPtr<JSONObject>, JSON Object*>(object, name, isOptional ? &valueFound : 0, protocolErrors, 0, AsMethod Bridges::asObject, "Object"); | |
| 350 } | |
| 351 | |
| 352 PassRefPtr<JSONArray> DispatcherImpl::getArray(JSONObject* object, const char* n ame, bool isOptional, JSONArray* protocolErrors) | |
| 353 { | |
| 354 bool valueFound = false; | |
| 355 return getPropertyValueImpl<PassRefPtr<JSONArray>, RefPtr<JSONArray>, JSONAr ray*>(object, name, isOptional ? &valueFound : 0, protocolErrors, 0, AsMethodBri dges::asArray, "Array"); | |
| 356 } | |
| 357 | |
| 358 bool Dispatcher::getCommandName(const String& message, String* result) | 275 bool Dispatcher::getCommandName(const String& message, String* result) |
| 359 { | 276 { |
| 360 RefPtr<JSONValue> value = parseJSON(message); | 277 RefPtr<JSONValue> value = parseJSON(message); |
| 361 if (!value) | 278 if (!value) |
| 362 return false; | 279 return false; |
| 363 | 280 |
| 364 RefPtr<JSONObject> object = JSONObject::cast(value.release()); | 281 RefPtr<JSONObject> object = JSONObject::cast(value.release()); |
| 365 if (!object) | 282 if (!object) |
| 366 return false; | 283 return false; |
| 367 | 284 |
| 368 if (!object->getString("method", result)) | 285 if (!object->getString("method", result)) |
| 369 return false; | 286 return false; |
| 370 | 287 |
| 371 return true; | 288 return true; |
| 372 } | 289 } |
| 373 | 290 |
| 374 Dispatcher::CallbackBase::CallbackBase(PassRefPtr<DispatcherImpl> backendImpl, i nt sessionId, int id) | 291 Dispatcher::CallbackBase::CallbackBase(PassRefPtr<DispatcherImpl> backendImpl, i nt sessionId, int id) |
| 375 : m_backendImpl(backendImpl), m_sessionId(sessionId), m_id(id), m_alreadySen t(false) { } | 292 : m_backendImpl(backendImpl), m_sessionId(sessionId), m_id(id), m_alreadySen t(false) { } |
| 376 | 293 |
| 377 Dispatcher::CallbackBase::~CallbackBase() { } | 294 Dispatcher::CallbackBase::~CallbackBase() { } |
| 378 | 295 |
| 379 void Dispatcher::CallbackBase::sendFailure(const ErrorString& error) | 296 void Dispatcher::CallbackBase::sendFailure(const ErrorString& error) |
| 380 { | 297 { |
| 381 ASSERT(error.length()); | 298 ASSERT(error.length()); |
| 382 sendIfActive(nullptr, error, PassRefPtr<JSONValue>()); | 299 sendIfActive(nullptr, error); |
| 383 } | 300 } |
| 384 | 301 |
| 385 bool Dispatcher::CallbackBase::isActive() | 302 bool Dispatcher::CallbackBase::isActive() |
| 386 { | 303 { |
| 387 return !m_alreadySent && m_backendImpl->isActive(); | 304 return !m_alreadySent && m_backendImpl->isActive(); |
| 388 } | 305 } |
| 389 | 306 |
| 390 void Dispatcher::CallbackBase::sendIfActive(PassRefPtr<JSONObject> partialMessag e, const ErrorString& invocationError, PassRefPtr<JSONValue> errorData) | 307 void Dispatcher::CallbackBase::sendIfActive(PassRefPtr<JSONObject> partialMessag e, const ErrorString& invocationError) |
| 391 { | 308 { |
| 392 if (m_alreadySent) | 309 if (m_alreadySent) |
| 393 return; | 310 return; |
| 394 m_backendImpl->sendResponse(m_sessionId, m_id, invocationError, errorData, p artialMessage); | 311 m_backendImpl->sendResponse(m_sessionId, m_id, invocationError, nullptr, par tialMessage); |
| 395 m_alreadySent = true; | 312 m_alreadySent = true; |
| 396 } | 313 } |
| 397 | 314 |
| 398 } // namespace protocol | 315 } // namespace protocol |
| 399 } // namespace blink | 316 } // namespace blink |
| OLD | NEW |