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::SendRawMessage(int session_id, |
44 raw_message_callback_.Run(message); | 44 const std::string& message) { |
45 notifier_->SendProtocolResponse(session_id, message); | |
dgozman
2015/11/07 03:15:29
Let's inline this.
kozy
2015/11/07 04:03:57
Done.
| |
45 } | 46 } |
46 | 47 |
47 void DevToolsProtocolClient::SendMessage(const base::DictionaryValue& message) { | 48 void DevToolsProtocolClient::SendRawNotification(const std::string& message) { |
49 notifier_->SendProtocolNotification(message); | |
50 } | |
51 | |
52 void DevToolsProtocolClient::SendMessage(int session_id, | |
53 const base::DictionaryValue& message) { | |
48 std::string json_message; | 54 std::string json_message; |
49 base::JSONWriter::Write(message, &json_message); | 55 base::JSONWriter::Write(message, &json_message); |
50 SendRawMessage(json_message); | 56 SendRawMessage(session_id, json_message); |
51 } | 57 } |
52 | 58 |
53 void DevToolsProtocolClient::SendNotification( | 59 void DevToolsProtocolClient::SendNotification( |
54 const std::string& method, | 60 const std::string& method, |
55 scoped_ptr<base::DictionaryValue> params) { | 61 scoped_ptr<base::DictionaryValue> params) { |
56 base::DictionaryValue notification; | 62 base::DictionaryValue notification; |
57 notification.SetString(kMethodParam, method); | 63 notification.SetString(kMethodParam, method); |
58 if (params) | 64 if (params) |
59 notification.Set(kParamsParam, params.release()); | 65 notification.Set(kParamsParam, params.release()); |
60 | 66 |
61 SendMessage(notification); | 67 std::string json_message; |
68 base::JSONWriter::Write(notification, &json_message); | |
69 SendRawNotification(json_message); | |
62 } | 70 } |
63 | 71 |
64 void DevToolsProtocolClient::SendSuccess( | 72 void DevToolsProtocolClient::SendSuccess( |
65 DevToolsCommandId command_id, | 73 DevToolsCommandId command_id, |
66 scoped_ptr<base::DictionaryValue> params) { | 74 scoped_ptr<base::DictionaryValue> params) { |
67 base::DictionaryValue response; | 75 base::DictionaryValue response; |
68 response.SetInteger(kIdParam, command_id); | 76 response.SetInteger(kIdParam, command_id.call_id); |
69 | 77 |
70 response.Set(kResultParam, | 78 response.Set(kResultParam, |
71 params ? params.release() : new base::DictionaryValue()); | 79 params ? params.release() : new base::DictionaryValue()); |
72 | 80 |
73 SendMessage(response); | 81 SendMessage(command_id.session_id, response); |
74 } | 82 } |
75 | 83 |
76 bool DevToolsProtocolClient::SendError(DevToolsCommandId command_id, | 84 bool DevToolsProtocolClient::SendError(DevToolsCommandId command_id, |
77 const Response& response) { | 85 const Response& response) { |
78 if (response.status() == kStatusOk || | 86 if (response.status() == kStatusOk || |
79 response.status() == kStatusFallThrough) { | 87 response.status() == kStatusFallThrough) { |
80 return false; | 88 return false; |
81 } | 89 } |
82 base::DictionaryValue dict; | 90 base::DictionaryValue dict; |
83 if (command_id == kNoId) | 91 if (command_id.call_id == DevToolsCommandId::kNoId) |
84 dict.Set(kIdParam, base::Value::CreateNullValue()); | 92 dict.Set(kIdParam, base::Value::CreateNullValue()); |
85 else | 93 else |
86 dict.SetInteger(kIdParam, command_id); | 94 dict.SetInteger(kIdParam, command_id.call_id); |
87 | 95 |
88 base::DictionaryValue* error_object = new base::DictionaryValue(); | 96 base::DictionaryValue* error_object = new base::DictionaryValue(); |
89 error_object->SetInteger(kErrorCodeParam, response.status()); | 97 error_object->SetInteger(kErrorCodeParam, response.status()); |
90 if (!response.message().empty()) | 98 if (!response.message().empty()) |
91 error_object->SetString(kErrorMessageParam, response.message()); | 99 error_object->SetString(kErrorMessageParam, response.message()); |
92 | 100 |
93 dict.Set(kErrorParam, error_object); | 101 dict.Set(kErrorParam, error_object); |
94 SendMessage(dict); | 102 SendMessage(command_id.session_id, dict); |
95 return true; | 103 return true; |
96 } | 104 } |
97 | 105 |
98 typedef DevToolsProtocolClient::Response Response; | 106 typedef DevToolsProtocolClient::Response Response; |
99 | 107 |
100 Response Response::FallThrough() { | 108 Response Response::FallThrough() { |
101 return Response(kStatusFallThrough); | 109 return Response(kStatusFallThrough); |
102 } | 110 } |
103 | 111 |
104 Response Response::OK() { | 112 Response Response::OK() { |
(...skipping 28 matching lines...) Expand all Loading... | |
133 Response::Response(int status) | 141 Response::Response(int status) |
134 : status_(status) { | 142 : status_(status) { |
135 } | 143 } |
136 | 144 |
137 Response::Response(int status, const std::string& message) | 145 Response::Response(int status, const std::string& message) |
138 : status_(status), | 146 : status_(status), |
139 message_(message) { | 147 message_(message) { |
140 } | 148 } |
141 | 149 |
142 } // namespace content | 150 } // namespace content |
OLD | NEW |