summaryrefslogtreecommitdiffstats
path: root/chrome/browser/devtools/devtools_protocol.cc
blob: 8df05578c5f639c8396645a63bb3dcbe549a86a0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
// Copyright (c) 2013 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chrome/browser/devtools/devtools_protocol.h"

#include "base/json/json_reader.h"
#include "base/json/json_writer.h"
#include "base/strings/stringprintf.h"

namespace {

const char kErrorCodeParam[] = "code";
const char kErrorParam[] = "error";
const char kErrorMessageParam[] = "message";
const char kIdParam[] = "id";
const char kMethodParam[] = "method";
const char kParamsParam[] = "params";
const char kResultParam[] = "result";

// JSON RPC 2.0 spec: http://www.jsonrpc.org/specification#error_object
enum Error {
  kErrorInvalidParams = -32602
};

}  // namespace

// static
std::string DevToolsProtocol::SerializeCommand(
    int command_id,
    const std::string& method,
    scoped_ptr<base::DictionaryValue> params) {
  base::DictionaryValue command;
  command.SetInteger(kIdParam, command_id);
  command.SetString(kMethodParam, method);
  if (params)
    command.Set(kParamsParam, params.release());

  std::string json_command;
  base::JSONWriter::Write(&command, &json_command);
  return json_command;
}

// static
scoped_ptr<base::DictionaryValue>
DevToolsProtocol::CreateInvalidParamsResponse(int command_id,
                                              const std::string& param) {
  scoped_ptr<base::DictionaryValue> response(new base::DictionaryValue());
  base::DictionaryValue* error_object = new base::DictionaryValue();
  response->Set(kErrorParam, error_object);
  error_object->SetInteger(kErrorCodeParam, kErrorInvalidParams);
  error_object->SetString(kErrorMessageParam,
      base::StringPrintf("Missing or invalid '%s' parameter", param.c_str()));

  return response.Pass();
}

// static
scoped_ptr<base::DictionaryValue>
DevToolsProtocol::CreateSuccessResponse(
    int command_id,
    scoped_ptr<base::DictionaryValue> result) {
  scoped_ptr<base::DictionaryValue> response(new base::DictionaryValue());
  response->SetInteger(kIdParam, command_id);
  response->Set(kResultParam,
                result ? result.release() : new base::DictionaryValue());

  return response.Pass();
}

// static
bool DevToolsProtocol::ParseCommand(const base::DictionaryValue* command,
                                    int* command_id,
                                    std::string* method,
                                    const base::DictionaryValue** params) {
  if (!command)
    return false;

  if (!command->GetInteger(kIdParam, command_id) || *command_id < 0)
    return false;

  if (!command->GetString(kMethodParam, method))
    return false;

  if (!command->GetDictionary(kParamsParam, params))
    *params = nullptr;

  return true;
}

// static
bool DevToolsProtocol::ParseNotification(
    const std::string& json,
    std::string* method,
    scoped_ptr<base::DictionaryValue>* params) {
  scoped_ptr<base::Value> value(base::JSONReader::Read(json));
  if (!value || !value->IsType(base::Value::TYPE_DICTIONARY))
    return false;

  scoped_ptr<base::DictionaryValue> dict(
      static_cast<base::DictionaryValue*>(value.release()));

  if (!dict->GetString(kMethodParam, method))
    return false;

  scoped_ptr<base::Value> params_value;
  dict->Remove(kParamsParam, &params_value);
  if (params_value && params_value->IsType(base::Value::TYPE_DICTIONARY))
    params->reset(static_cast<base::DictionaryValue*>(params_value.release()));

  return true;
}

// static
bool DevToolsProtocol::ParseResponse(const std::string& json,
                                     int* command_id,
                                     int* error_code) {
  scoped_ptr<base::Value> value(base::JSONReader::Read(json));
  if (!value || !value->IsType(base::Value::TYPE_DICTIONARY))
    return false;

  scoped_ptr<base::DictionaryValue> dict(
      static_cast<base::DictionaryValue*>(value.release()));

  if (!dict->GetInteger(kIdParam, command_id))
    return false;

  *error_code = 0;
  base::DictionaryValue* error_dict = nullptr;
  if (dict->GetDictionary(kErrorParam, &error_dict))
    error_dict->GetInteger(kErrorCodeParam, error_code);
  return true;
}