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_client.h"
7 #include "base/json/json_writer.h"
8 #include "base/strings/stringprintf.h"
14 const char kIdParam
[] = "id";
15 const char kMethodParam
[] = "method";
16 const char kParamsParam
[] = "params";
17 const char kResultParam
[] = "result";
18 const char kErrorParam
[] = "error";
19 const char kErrorCodeParam
[] = "code";
20 const char kErrorMessageParam
[] = "message";
23 const int kStatusOk
= -1;
24 const int kStatusFallThrough
= -2;
25 // JSON RPC 2.0 spec: http://www.jsonrpc.org/specification#error_object
26 const int kStatusInvalidParams
= -32602;
27 const int kStatusInternalError
= -32603;
28 const int kStatusServerError
= -32000;
33 const DevToolsCommandId
DevToolsProtocolClient::kNoId
= -1;
35 DevToolsProtocolClient::DevToolsProtocolClient(
36 const RawMessageCallback
& raw_message_callback
)
37 : raw_message_callback_(raw_message_callback
) {
40 DevToolsProtocolClient::~DevToolsProtocolClient() {
43 void DevToolsProtocolClient::SendRawMessage(const std::string
& message
) {
44 raw_message_callback_
.Run(message
);
47 void DevToolsProtocolClient::SendMessage(const base::DictionaryValue
& message
) {
48 std::string json_message
;
49 base::JSONWriter::Write(&message
, &json_message
);
50 SendRawMessage(json_message
);
53 void DevToolsProtocolClient::SendNotification(
54 const std::string
& method
,
55 scoped_ptr
<base::DictionaryValue
> params
) {
56 base::DictionaryValue notification
;
57 notification
.SetString(kMethodParam
, method
);
59 notification
.Set(kParamsParam
, params
.release());
61 SendMessage(notification
);
64 void DevToolsProtocolClient::SendSuccess(
65 DevToolsCommandId command_id
,
66 scoped_ptr
<base::DictionaryValue
> params
) {
67 base::DictionaryValue response
;
68 response
.SetInteger(kIdParam
, command_id
);
70 response
.Set(kResultParam
,
71 params
? params
.release() : new base::DictionaryValue());
73 SendMessage(response
);
76 bool DevToolsProtocolClient::SendError(DevToolsCommandId command_id
,
77 const Response
& response
) {
78 if (response
.status() == kStatusOk
||
79 response
.status() == kStatusFallThrough
) {
82 base::DictionaryValue dict
;
83 if (command_id
== kNoId
)
84 dict
.Set(kIdParam
, base::Value::CreateNullValue());
86 dict
.SetInteger(kIdParam
, command_id
);
88 base::DictionaryValue
* error_object
= new base::DictionaryValue();
89 error_object
->SetInteger(kErrorCodeParam
, response
.status());
90 if (!response
.message().empty())
91 error_object
->SetString(kErrorMessageParam
, response
.message());
93 dict
.Set(kErrorParam
, error_object
);
98 typedef DevToolsProtocolClient::Response Response
;
100 Response
Response::FallThrough() {
101 return Response(kStatusFallThrough
);
104 Response
Response::OK() {
105 return Response(kStatusOk
);
108 Response
Response::InvalidParams(const std::string
& param
) {
109 return Response(kStatusInvalidParams
,
110 base::StringPrintf("Missing or invalid '%s' parameter", param
.c_str()));
113 Response
Response::InternalError(const std::string
& message
) {
114 return Response(kStatusInternalError
, message
);
117 Response
Response::ServerError(const std::string
& message
) {
118 return Response(kStatusServerError
, message
);
121 int Response::status() const {
125 const std::string
& Response::message() const {
129 bool Response::IsFallThrough() const {
130 return status_
== kStatusFallThrough
;
133 Response::Response(int status
)
137 Response::Response(int status
, const std::string
& message
)
142 } // namespace content