Disable view source for Developer Tools.
[chromium-blink-merge.git] / chrome / browser / devtools / devtools_protocol.cc
blobf14864ccf7645253bb4a729af3678a4009bf5fd0
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"
10 namespace {
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";
16 } // namespace
18 DevToolsProtocol::Message::~Message() {
21 DevToolsProtocol::Message::Message(const std::string& method,
22 base::DictionaryValue* params)
23 : method_(method),
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),
31 id_(id) {
34 DevToolsProtocol::Command::~Command() {
37 std::string DevToolsProtocol::Command::Serialize() {
38 base::DictionaryValue command;
39 command.SetInteger(kIdParam, id_);
40 command.SetString(kMethodParam, method_);
41 if (params_)
42 command.Set(kParamsParam, params_->DeepCopy());
44 std::string json_command;
45 base::JSONWriter::Write(&command, &json_command);
46 return 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)
61 : id_(id),
62 error_code_(error_code) {
65 // static
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))
70 return NULL;
72 scoped_ptr<base::DictionaryValue> dict(
73 static_cast<base::DictionaryValue*>(value.release()));
75 std::string method;
76 if (!dict->GetString(kMethodParam, &method))
77 return NULL;
79 base::DictionaryValue* params = NULL;
80 dict->GetDictionary(kParamsParam, &params);
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))
88 return NULL;
90 scoped_ptr<base::DictionaryValue> dict(
91 static_cast<base::DictionaryValue*>(value.release()));
93 int id;
94 if (!dict->GetInteger(kIdParam, &id))
95 return NULL;
97 int error_code = 0;
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);