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"
11 const char kIdParam
[] = "id";
12 const char kMethodParam
[] = "method";
13 const char kParamsParam
[] = "params";
14 const char kErrorParam
[] = "error";
15 const char kErrorCodeParam
[] = "code";
18 DevToolsProtocol::Message::~Message() {
21 DevToolsProtocol::Message::Message(const std::string
& method
,
22 base::DictionaryValue
* params
)
24 params_(params
? params
->DeepCopy() : NULL
) {
27 DevToolsProtocol::Command::Command(int id
,
28 const std::string
& method
,
29 base::DictionaryValue
* params
)
30 : Message(method
, params
),
34 DevToolsProtocol::Command::~Command() {
37 std::string
DevToolsProtocol::Command::Serialize() {
38 base::DictionaryValue command
;
39 command
.SetInteger(kIdParam
, id_
);
40 command
.SetString(kMethodParam
, method_
);
42 command
.Set(kParamsParam
, params_
->DeepCopy());
44 std::string json_command
;
45 base::JSONWriter::Write(&command
, &json_command
);
49 DevToolsProtocol::Notification::~Notification() {
52 DevToolsProtocol::Notification::Notification(const std::string
& method
,
53 base::DictionaryValue
* params
)
54 : Message(method
, params
) {
57 DevToolsProtocol::Response::~Response() {
60 DevToolsProtocol::Response::Response(int id
, int error_code
)
62 error_code_(error_code
) {
66 DevToolsProtocol::Notification
* DevToolsProtocol::ParseNotification(
67 const std::string
& json
) {
68 scoped_ptr
<base::Value
> value(base::JSONReader::Read(json
));
69 if (!value
|| !value
->IsType(base::Value::TYPE_DICTIONARY
))
72 scoped_ptr
<base::DictionaryValue
> dict(
73 static_cast<base::DictionaryValue
*>(value
.release()));
76 if (!dict
->GetString(kMethodParam
, &method
))
79 base::DictionaryValue
* params
= NULL
;
80 dict
->GetDictionary(kParamsParam
, ¶ms
);
81 return new Notification(method
, params
);
84 DevToolsProtocol::Response
* DevToolsProtocol::ParseResponse(
85 const std::string
& json
) {
86 scoped_ptr
<base::Value
> value(base::JSONReader::Read(json
));
87 if (!value
|| !value
->IsType(base::Value::TYPE_DICTIONARY
))
90 scoped_ptr
<base::DictionaryValue
> dict(
91 static_cast<base::DictionaryValue
*>(value
.release()));
94 if (!dict
->GetInteger(kIdParam
, &id
))
98 base::DictionaryValue
* error_dict
= NULL
;
99 if (dict
->GetDictionary(kErrorParam
, &error_dict
))
100 error_dict
->GetInteger(kErrorCodeParam
, &error_code
);
101 return new Response(id
, error_code
);