Supervised user import: Listen for profile creation/deletion
[chromium-blink-merge.git] / content / browser / devtools / protocol / devtools_protocol_handler.cc
blob28412195702e4b2f0d541ce1a03709da67de119a
1 // Copyright 2014 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 "content/browser/devtools/protocol/devtools_protocol_handler.h"
7 #include "base/bind.h"
8 #include "base/json/json_reader.h"
10 namespace content {
12 namespace {
14 const char kIdParam[] = "id";
15 const char kMethodParam[] = "method";
16 const char kParamsParam[] = "params";
18 // JSON RPC 2.0 spec: http://www.jsonrpc.org/specification#error_object
19 const int kStatusParseError = -32700;
20 const int kStatusInvalidRequest = -32600;
21 const int kStatusNoSuchMethod = -32601;
23 scoped_ptr<base::DictionaryValue> TakeDictionary(base::DictionaryValue* dict,
24 const std::string& key) {
25 scoped_ptr<base::Value> value;
26 dict->Remove(key, &value);
27 base::DictionaryValue* result = nullptr;
28 if (value)
29 value.release()->GetAsDictionary(&result);
30 return make_scoped_ptr(result);
33 } // namespace
35 DevToolsProtocolHandler::DevToolsProtocolHandler(const Notifier& notifier)
36 : client_(notifier),
37 dispatcher_(notifier) {
40 DevToolsProtocolHandler::~DevToolsProtocolHandler() {
43 scoped_ptr<base::DictionaryValue>
44 DevToolsProtocolHandler::ParseCommand(const std::string& message) {
45 scoped_ptr<base::Value> value(base::JSONReader::Read(message));
46 if (!value || !value->IsType(base::Value::TYPE_DICTIONARY)) {
47 client_.SendError(DevToolsProtocolClient::kNoId,
48 Response(kStatusParseError,
49 "Message must be in JSON format"));
50 return nullptr;
53 scoped_ptr<base::DictionaryValue> command =
54 make_scoped_ptr(static_cast<base::DictionaryValue*>(value.release()));
55 int id = DevToolsProtocolClient::kNoId;
56 bool ok = command->GetInteger(kIdParam, &id) && id >= 0;
57 if (!ok) {
58 client_.SendError(id, Response(kStatusInvalidRequest,
59 "The type of 'id' property must be number"));
60 return nullptr;
63 std::string method;
64 ok = command->GetString(kMethodParam, &method);
65 if (!ok) {
66 client_.SendError(id,
67 Response(kStatusInvalidRequest,
68 "The type of 'method' property must be string"));
69 return nullptr;
72 return command;
75 void DevToolsProtocolHandler::HandleCommand(
76 scoped_ptr<base::DictionaryValue> command) {
77 int id = DevToolsProtocolClient::kNoId;
78 std::string method;
79 command->GetInteger(kIdParam, &id);
80 command->GetString(kMethodParam, &method);
81 DevToolsProtocolDispatcher::CommandHandler command_handler(
82 dispatcher_.FindCommandHandler(method));
83 if (command_handler.is_null()) {
84 client_.SendError(id, Response(kStatusNoSuchMethod, "No such method"));
85 return;
87 bool result =
88 command_handler.Run(id, TakeDictionary(command.get(), kParamsParam));
89 DCHECK(result);
92 bool DevToolsProtocolHandler::HandleOptionalCommand(
93 scoped_ptr<base::DictionaryValue> command) {
94 int id = DevToolsProtocolClient::kNoId;
95 std::string method;
96 command->GetInteger(kIdParam, &id);
97 command->GetString(kMethodParam, &method);
98 DevToolsProtocolDispatcher::CommandHandler command_handler(
99 dispatcher_.FindCommandHandler(method));
100 if (!command_handler.is_null())
101 return command_handler.Run(id, TakeDictionary(command.get(), kParamsParam));
102 return false;
105 } // namespace content