1 // Copyright (c) 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "chrome/browser/devtools/devtools_protocol.h"
7 #include "base/json/json_reader.h"
8 #include "base/json/json_writer.h"
9 #include "base/strings/stringprintf.h"
13 const char kErrorCodeParam
[] = "code";
14 const char kErrorParam
[] = "error";
15 const char kErrorMessageParam
[] = "message";
16 const char kIdParam
[] = "id";
17 const char kMethodParam
[] = "method";
18 const char kParamsParam
[] = "params";
19 const char kResultParam
[] = "result";
21 // JSON RPC 2.0 spec: http://www.jsonrpc.org/specification#error_object
23 kErrorInvalidParams
= -32602
29 std::string
DevToolsProtocol::SerializeCommand(
31 const std::string
& method
,
32 scoped_ptr
<base::DictionaryValue
> params
) {
33 base::DictionaryValue command
;
34 command
.SetInteger(kIdParam
, command_id
);
35 command
.SetString(kMethodParam
, method
);
37 command
.Set(kParamsParam
, params
.release());
39 std::string json_command
;
40 base::JSONWriter::Write(command
, &json_command
);
45 scoped_ptr
<base::DictionaryValue
>
46 DevToolsProtocol::CreateInvalidParamsResponse(int command_id
,
47 const std::string
& param
) {
48 scoped_ptr
<base::DictionaryValue
> response(new base::DictionaryValue());
49 base::DictionaryValue
* error_object
= new base::DictionaryValue();
50 response
->Set(kErrorParam
, error_object
);
51 error_object
->SetInteger(kErrorCodeParam
, kErrorInvalidParams
);
52 error_object
->SetString(kErrorMessageParam
,
53 base::StringPrintf("Missing or invalid '%s' parameter", param
.c_str()));
55 return response
.Pass();
59 scoped_ptr
<base::DictionaryValue
>
60 DevToolsProtocol::CreateSuccessResponse(
62 scoped_ptr
<base::DictionaryValue
> result
) {
63 scoped_ptr
<base::DictionaryValue
> response(new base::DictionaryValue());
64 response
->SetInteger(kIdParam
, command_id
);
65 response
->Set(kResultParam
,
66 result
? result
.release() : new base::DictionaryValue());
68 return response
.Pass();
72 bool DevToolsProtocol::ParseCommand(const base::DictionaryValue
* command
,
75 const base::DictionaryValue
** params
) {
79 if (!command
->GetInteger(kIdParam
, command_id
) || *command_id
< 0)
82 if (!command
->GetString(kMethodParam
, method
))
85 if (!command
->GetDictionary(kParamsParam
, params
))
92 bool DevToolsProtocol::ParseNotification(
93 const std::string
& json
,
95 scoped_ptr
<base::DictionaryValue
>* params
) {
96 scoped_ptr
<base::Value
> value
= base::JSONReader::Read(json
);
97 if (!value
|| !value
->IsType(base::Value::TYPE_DICTIONARY
))
100 scoped_ptr
<base::DictionaryValue
> dict(
101 static_cast<base::DictionaryValue
*>(value
.release()));
103 if (!dict
->GetString(kMethodParam
, method
))
106 scoped_ptr
<base::Value
> params_value
;
107 dict
->Remove(kParamsParam
, ¶ms_value
);
108 if (params_value
&& params_value
->IsType(base::Value::TYPE_DICTIONARY
))
109 params
->reset(static_cast<base::DictionaryValue
*>(params_value
.release()));
115 bool DevToolsProtocol::ParseResponse(const std::string
& json
,
118 scoped_ptr
<base::Value
> value
= base::JSONReader::Read(json
);
119 if (!value
|| !value
->IsType(base::Value::TYPE_DICTIONARY
))
122 scoped_ptr
<base::DictionaryValue
> dict(
123 static_cast<base::DictionaryValue
*>(value
.release()));
125 if (!dict
->GetInteger(kIdParam
, command_id
))
129 base::DictionaryValue
* error_dict
= nullptr;
130 if (dict
->GetDictionary(kErrorParam
, &error_dict
))
131 error_dict
->GetInteger(kErrorCodeParam
, error_code
);