Remove aura enum from DesktopMediaID to fix desktop mirroring audio (CrOS).
[chromium-blink-merge.git] / chrome / browser / devtools / devtools_protocol.cc
blobc478040e29b358493133b16cdd7357e18c230eec
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"
11 namespace {
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
22 enum Error {
23 kErrorInvalidParams = -32602
26 } // namespace
28 // static
29 std::string DevToolsProtocol::SerializeCommand(
30 int command_id,
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);
36 if (params)
37 command.Set(kParamsParam, params.release());
39 std::string json_command;
40 base::JSONWriter::Write(command, &json_command);
41 return json_command;
44 // static
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();
58 // static
59 scoped_ptr<base::DictionaryValue>
60 DevToolsProtocol::CreateSuccessResponse(
61 int command_id,
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();
71 // static
72 bool DevToolsProtocol::ParseCommand(const base::DictionaryValue* command,
73 int* command_id,
74 std::string* method,
75 const base::DictionaryValue** params) {
76 if (!command)
77 return false;
79 if (!command->GetInteger(kIdParam, command_id) || *command_id < 0)
80 return false;
82 if (!command->GetString(kMethodParam, method))
83 return false;
85 if (!command->GetDictionary(kParamsParam, params))
86 *params = nullptr;
88 return true;
91 // static
92 bool DevToolsProtocol::ParseNotification(
93 const std::string& json,
94 std::string* method,
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))
98 return false;
100 scoped_ptr<base::DictionaryValue> dict(
101 static_cast<base::DictionaryValue*>(value.release()));
103 if (!dict->GetString(kMethodParam, method))
104 return false;
106 scoped_ptr<base::Value> params_value;
107 dict->Remove(kParamsParam, &params_value);
108 if (params_value && params_value->IsType(base::Value::TYPE_DICTIONARY))
109 params->reset(static_cast<base::DictionaryValue*>(params_value.release()));
111 return true;
114 // static
115 bool DevToolsProtocol::ParseResponse(const std::string& json,
116 int* command_id,
117 int* error_code) {
118 scoped_ptr<base::Value> value = base::JSONReader::Read(json);
119 if (!value || !value->IsType(base::Value::TYPE_DICTIONARY))
120 return false;
122 scoped_ptr<base::DictionaryValue> dict(
123 static_cast<base::DictionaryValue*>(value.release()));
125 if (!dict->GetInteger(kIdParam, command_id))
126 return false;
128 *error_code = 0;
129 base::DictionaryValue* error_dict = nullptr;
130 if (dict->GetDictionary(kErrorParam, &error_dict))
131 error_dict->GetInteger(kErrorCodeParam, error_code);
132 return true;