summaryrefslogtreecommitdiffstats
path: root/content/browser/devtools/devtools_protocol_handler.cc
blob: 86ccd8e5d2d05830c5a9ada8be073fbf4f68104b (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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
// Copyright 2014 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 "content/browser/devtools/devtools_protocol_handler.h"

#include "base/bind.h"
#include "base/json/json_reader.h"
#include "base/json/json_writer.h"
#include "content/browser/devtools/devtools_manager.h"
#include "content/public/browser/devtools_manager_delegate.h"

namespace content {

namespace {

const char kIdParam[] = "id";
const char kMethodParam[] = "method";
const char kParamsParam[] = "params";

// JSON RPC 2.0 spec: http://www.jsonrpc.org/specification#error_object
const int kStatusParseError = -32700;
const int kStatusInvalidRequest = -32600;
const int kStatusNoSuchMethod = -32601;

scoped_ptr<base::DictionaryValue> TakeDictionary(base::DictionaryValue* dict,
                                                 const std::string& key) {
  scoped_ptr<base::Value> value;
  dict->Remove(key, &value);
  base::DictionaryValue* result = nullptr;
  if (value)
    value.release()->GetAsDictionary(&result);
  return make_scoped_ptr(result);
}

}  // namespace

DevToolsProtocolHandler::DevToolsProtocolHandler(
    DevToolsAgentHost* agent_host, const Notifier& notifier)
    : agent_host_(agent_host),
      client_(notifier),
      dispatcher_(notifier) {
}

DevToolsProtocolHandler::~DevToolsProtocolHandler() {
}

void DevToolsProtocolHandler::HandleMessage(const std::string& message) {
  scoped_ptr<base::DictionaryValue> command = ParseCommand(message);
  if (!command)
    return;
  if (PassCommandToDelegate(command.get()))
    return;
  HandleCommand(command.Pass());
}

bool DevToolsProtocolHandler::HandleOptionalMessage(
    const std::string& message, int* call_id) {
  scoped_ptr<base::DictionaryValue> command = ParseCommand(message);
  if (!command)
    return true;
  if (PassCommandToDelegate(command.get()))
    return true;
  return HandleOptionalCommand(command.Pass(), call_id);
}

bool DevToolsProtocolHandler::PassCommandToDelegate(
    base::DictionaryValue* command) {
  DevToolsManagerDelegate* delegate =
      DevToolsManager::GetInstance()->delegate();
  if (!delegate)
    return false;

  scoped_ptr<base::DictionaryValue> response(
      delegate->HandleCommand(agent_host_, command));
  if (response) {
    std::string json_response;
    base::JSONWriter::Write(*response, &json_response);
    client_.SendRawMessage(json_response);
    return true;
  }

  return false;
}

scoped_ptr<base::DictionaryValue>
DevToolsProtocolHandler::ParseCommand(const std::string& message) {
  scoped_ptr<base::Value> value = base::JSONReader::Read(message);
  if (!value || !value->IsType(base::Value::TYPE_DICTIONARY)) {
    client_.SendError(DevToolsProtocolClient::kNoId,
                      Response(kStatusParseError,
                               "Message must be in JSON format"));
    return nullptr;
  }

  scoped_ptr<base::DictionaryValue> command =
      make_scoped_ptr(static_cast<base::DictionaryValue*>(value.release()));
  int id = DevToolsProtocolClient::kNoId;
  bool ok = command->GetInteger(kIdParam, &id) && id >= 0;
  if (!ok) {
    client_.SendError(id, Response(kStatusInvalidRequest,
                                   "The type of 'id' property must be number"));
    return nullptr;
  }

  std::string method;
  ok = command->GetString(kMethodParam, &method);
  if (!ok) {
    client_.SendError(id,
        Response(kStatusInvalidRequest,
                 "The type of 'method' property must be string"));
    return nullptr;
  }

  return command;
}

void DevToolsProtocolHandler::HandleCommand(
    scoped_ptr<base::DictionaryValue> command) {
  int id = DevToolsProtocolClient::kNoId;
  std::string method;
  command->GetInteger(kIdParam, &id);
  command->GetString(kMethodParam, &method);
  DevToolsProtocolDispatcher::CommandHandler command_handler(
      dispatcher_.FindCommandHandler(method));
  if (command_handler.is_null()) {
    client_.SendError(id, Response(kStatusNoSuchMethod, "No such method"));
    return;
  }

  bool result =
      command_handler.Run(id, TakeDictionary(command.get(), kParamsParam));
  DCHECK(result);
}

bool DevToolsProtocolHandler::HandleOptionalCommand(
    scoped_ptr<base::DictionaryValue> command, int* call_id) {
  *call_id = DevToolsProtocolClient::kNoId;
  std::string method;
  command->GetInteger(kIdParam, call_id);
  command->GetString(kMethodParam, &method);
  DevToolsProtocolDispatcher::CommandHandler command_handler(
      dispatcher_.FindCommandHandler(method));
  if (!command_handler.is_null()) {
    return command_handler.Run(
        *call_id, TakeDictionary(command.get(), kParamsParam));
  }
  return false;
}

}  // namespace content