blob: eed5940b627c915ecdd48543f1965dcb695a9574 [file] [log] [blame]
Ben Murdochbb1529c2013-08-08 10:24:53 +01001// 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.
4
5#include "chrome/browser/devtools/devtools_protocol.h"
6
7#include "base/json/json_reader.h"
8#include "base/json/json_writer.h"
9
10namespace {
11const char kIdParam[] = "id";
12const char kMethodParam[] = "method";
13const char kParamsParam[] = "params";
14} // namespace
15
16DevToolsProtocol::Message::~Message() {
17}
18
19DevToolsProtocol::Message::Message(const std::string& method,
20 base::DictionaryValue* params)
21 : method_(method),
22 params_(params ? params->DeepCopy() : NULL) {
23}
24
25DevToolsProtocol::Command::Command(int id,
26 const std::string& method,
27 base::DictionaryValue* params)
28 : Message(method, params),
29 id_(id) {
30}
31
32DevToolsProtocol::Command::~Command() {
33}
34
35std::string DevToolsProtocol::Command::Serialize() {
36 base::DictionaryValue command;
37 command.SetInteger(kIdParam, id_);
38 command.SetString(kMethodParam, method_);
39 if (params_)
40 command.Set(kParamsParam, params_->DeepCopy());
41
42 std::string json_command;
43 base::JSONWriter::Write(&command, &json_command);
44 return json_command;
45}
46
47DevToolsProtocol::Notification::~Notification() {
48}
49
50DevToolsProtocol::Notification::Notification(const std::string& method,
51 base::DictionaryValue* params)
52 : Message(method, params) {
53}
54
55// static
56DevToolsProtocol::Notification* DevToolsProtocol::ParseNotification(
57 const std::string& json) {
58 scoped_ptr<base::Value> value(base::JSONReader::Read(json));
59 if (!value || !value->IsType(Value::TYPE_DICTIONARY))
60 return NULL;
61
62 scoped_ptr<base::DictionaryValue> dict(
63 static_cast<base::DictionaryValue*>(value.release()));
64
65 std::string method;
66 if (!dict->GetString(kMethodParam, &method))
67 return NULL;
68
69 base::DictionaryValue* params = NULL;
70 dict->GetDictionary(kParamsParam, &params);
71 return new Notification(method, params);
72}