Ben Murdoch | bb1529c | 2013-08-08 10:24:53 +0100 | [diff] [blame^] | 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. |
| 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 | |
| 10 | namespace { |
| 11 | const char kIdParam[] = "id"; |
| 12 | const char kMethodParam[] = "method"; |
| 13 | const char kParamsParam[] = "params"; |
| 14 | } // namespace |
| 15 | |
| 16 | DevToolsProtocol::Message::~Message() { |
| 17 | } |
| 18 | |
| 19 | DevToolsProtocol::Message::Message(const std::string& method, |
| 20 | base::DictionaryValue* params) |
| 21 | : method_(method), |
| 22 | params_(params ? params->DeepCopy() : NULL) { |
| 23 | } |
| 24 | |
| 25 | DevToolsProtocol::Command::Command(int id, |
| 26 | const std::string& method, |
| 27 | base::DictionaryValue* params) |
| 28 | : Message(method, params), |
| 29 | id_(id) { |
| 30 | } |
| 31 | |
| 32 | DevToolsProtocol::Command::~Command() { |
| 33 | } |
| 34 | |
| 35 | std::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 | |
| 47 | DevToolsProtocol::Notification::~Notification() { |
| 48 | } |
| 49 | |
| 50 | DevToolsProtocol::Notification::Notification(const std::string& method, |
| 51 | base::DictionaryValue* params) |
| 52 | : Message(method, params) { |
| 53 | } |
| 54 | |
| 55 | // static |
| 56 | DevToolsProtocol::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, ¶ms); |
| 71 | return new Notification(method, params); |
| 72 | } |