OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "content/browser/devtools/protocol/devtools_protocol_client.h" | 5 #include "content/browser/devtools/protocol/devtools_protocol_client.h" |
6 | 6 |
7 #include "base/json/json_writer.h" | 7 #include "base/json/json_writer.h" |
8 #include "base/strings/stringprintf.h" | 8 #include "base/strings/stringprintf.h" |
| 9 #include "content/browser/devtools/protocol/devtools_protocol_delegate.h" |
9 | 10 |
10 namespace content { | 11 namespace content { |
11 | 12 |
12 namespace { | 13 namespace { |
13 | 14 |
14 const char kIdParam[] = "id"; | 15 const char kIdParam[] = "id"; |
15 const char kMethodParam[] = "method"; | 16 const char kMethodParam[] = "method"; |
16 const char kParamsParam[] = "params"; | 17 const char kParamsParam[] = "params"; |
17 const char kResultParam[] = "result"; | 18 const char kResultParam[] = "result"; |
18 const char kErrorParam[] = "error"; | 19 const char kErrorParam[] = "error"; |
19 const char kErrorCodeParam[] = "code"; | 20 const char kErrorCodeParam[] = "code"; |
20 const char kErrorMessageParam[] = "message"; | 21 const char kErrorMessageParam[] = "message"; |
21 | 22 |
22 // Special values. | 23 // Special values. |
23 const int kStatusOk = -1; | 24 const int kStatusOk = -1; |
24 const int kStatusFallThrough = -2; | 25 const int kStatusFallThrough = -2; |
25 // JSON RPC 2.0 spec: http://www.jsonrpc.org/specification#error_object | 26 // JSON RPC 2.0 spec: http://www.jsonrpc.org/specification#error_object |
26 const int kStatusInvalidParams = -32602; | 27 const int kStatusInvalidParams = -32602; |
27 const int kStatusInternalError = -32603; | 28 const int kStatusInternalError = -32603; |
28 const int kStatusServerError = -32000; | 29 const int kStatusServerError = -32000; |
29 | 30 |
30 } // namespace | 31 } // namespace |
31 | 32 |
32 // static | 33 // static |
33 const DevToolsCommandId DevToolsProtocolClient::kNoId = -1; | 34 const int DevToolsCommandId::kNoId = -1; |
34 | 35 |
35 DevToolsProtocolClient::DevToolsProtocolClient( | 36 DevToolsProtocolClient::DevToolsProtocolClient( |
36 const RawMessageCallback& raw_message_callback) | 37 DevToolsProtocolDelegate* notifier) |
37 : raw_message_callback_(raw_message_callback) { | 38 : notifier_(notifier) {} |
38 } | |
39 | 39 |
40 DevToolsProtocolClient::~DevToolsProtocolClient() { | 40 DevToolsProtocolClient::~DevToolsProtocolClient() { |
41 } | 41 } |
42 | 42 |
43 void DevToolsProtocolClient::SendRawMessage(const std::string& message) { | 43 void DevToolsProtocolClient::SendRawNotification(const std::string& message) { |
44 raw_message_callback_.Run(message); | 44 notifier_->SendProtocolNotification(message); |
45 } | 45 } |
46 | 46 |
47 void DevToolsProtocolClient::SendMessage(const base::DictionaryValue& message) { | 47 void DevToolsProtocolClient::SendMessage(int session_id, |
| 48 const base::DictionaryValue& message) { |
48 std::string json_message; | 49 std::string json_message; |
49 base::JSONWriter::Write(message, &json_message); | 50 base::JSONWriter::Write(message, &json_message); |
50 SendRawMessage(json_message); | 51 notifier_->SendProtocolResponse(session_id, json_message); |
51 } | 52 } |
52 | 53 |
53 void DevToolsProtocolClient::SendNotification( | 54 void DevToolsProtocolClient::SendNotification( |
54 const std::string& method, | 55 const std::string& method, |
55 scoped_ptr<base::DictionaryValue> params) { | 56 scoped_ptr<base::DictionaryValue> params) { |
56 base::DictionaryValue notification; | 57 base::DictionaryValue notification; |
57 notification.SetString(kMethodParam, method); | 58 notification.SetString(kMethodParam, method); |
58 if (params) | 59 if (params) |
59 notification.Set(kParamsParam, params.release()); | 60 notification.Set(kParamsParam, params.release()); |
60 | 61 |
61 SendMessage(notification); | 62 std::string json_message; |
| 63 base::JSONWriter::Write(notification, &json_message); |
| 64 SendRawNotification(json_message); |
62 } | 65 } |
63 | 66 |
64 void DevToolsProtocolClient::SendSuccess( | 67 void DevToolsProtocolClient::SendSuccess( |
65 DevToolsCommandId command_id, | 68 DevToolsCommandId command_id, |
66 scoped_ptr<base::DictionaryValue> params) { | 69 scoped_ptr<base::DictionaryValue> params) { |
67 base::DictionaryValue response; | 70 base::DictionaryValue response; |
68 response.SetInteger(kIdParam, command_id); | 71 response.SetInteger(kIdParam, command_id.call_id); |
69 | 72 |
70 response.Set(kResultParam, | 73 response.Set(kResultParam, |
71 params ? params.release() : new base::DictionaryValue()); | 74 params ? params.release() : new base::DictionaryValue()); |
72 | 75 |
73 SendMessage(response); | 76 SendMessage(command_id.session_id, response); |
74 } | 77 } |
75 | 78 |
76 bool DevToolsProtocolClient::SendError(DevToolsCommandId command_id, | 79 bool DevToolsProtocolClient::SendError(DevToolsCommandId command_id, |
77 const Response& response) { | 80 const Response& response) { |
78 if (response.status() == kStatusOk || | 81 if (response.status() == kStatusOk || |
79 response.status() == kStatusFallThrough) { | 82 response.status() == kStatusFallThrough) { |
80 return false; | 83 return false; |
81 } | 84 } |
82 base::DictionaryValue dict; | 85 base::DictionaryValue dict; |
83 if (command_id == kNoId) | 86 if (command_id.call_id == DevToolsCommandId::kNoId) |
84 dict.Set(kIdParam, base::Value::CreateNullValue()); | 87 dict.Set(kIdParam, base::Value::CreateNullValue()); |
85 else | 88 else |
86 dict.SetInteger(kIdParam, command_id); | 89 dict.SetInteger(kIdParam, command_id.call_id); |
87 | 90 |
88 base::DictionaryValue* error_object = new base::DictionaryValue(); | 91 base::DictionaryValue* error_object = new base::DictionaryValue(); |
89 error_object->SetInteger(kErrorCodeParam, response.status()); | 92 error_object->SetInteger(kErrorCodeParam, response.status()); |
90 if (!response.message().empty()) | 93 if (!response.message().empty()) |
91 error_object->SetString(kErrorMessageParam, response.message()); | 94 error_object->SetString(kErrorMessageParam, response.message()); |
92 | 95 |
93 dict.Set(kErrorParam, error_object); | 96 dict.Set(kErrorParam, error_object); |
94 SendMessage(dict); | 97 SendMessage(command_id.session_id, dict); |
95 return true; | 98 return true; |
96 } | 99 } |
97 | 100 |
98 typedef DevToolsProtocolClient::Response Response; | 101 typedef DevToolsProtocolClient::Response Response; |
99 | 102 |
100 Response Response::FallThrough() { | 103 Response Response::FallThrough() { |
101 return Response(kStatusFallThrough); | 104 return Response(kStatusFallThrough); |
102 } | 105 } |
103 | 106 |
104 Response Response::OK() { | 107 Response Response::OK() { |
(...skipping 28 matching lines...) Expand all Loading... |
133 Response::Response(int status) | 136 Response::Response(int status) |
134 : status_(status) { | 137 : status_(status) { |
135 } | 138 } |
136 | 139 |
137 Response::Response(int status, const std::string& message) | 140 Response::Response(int status, const std::string& message) |
138 : status_(status), | 141 : status_(status), |
139 message_(message) { | 142 message_(message) { |
140 } | 143 } |
141 | 144 |
142 } // namespace content | 145 } // namespace content |
OLD | NEW |