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/devtools_protocol_handler.h" | 5 #include "content/browser/devtools/devtools_protocol_handler.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/json/json_reader.h" | 8 #include "base/json/json_reader.h" |
9 #include "base/json/json_writer.h" | 9 #include "base/json/json_writer.h" |
| 10 #include "content/browser/devtools/devtools_agent_host_impl.h" |
10 #include "content/browser/devtools/devtools_manager.h" | 11 #include "content/browser/devtools/devtools_manager.h" |
11 #include "content/public/browser/devtools_manager_delegate.h" | 12 #include "content/public/browser/devtools_manager_delegate.h" |
12 | 13 |
13 namespace content { | 14 namespace content { |
14 | 15 |
15 namespace { | 16 namespace { |
16 | 17 |
17 const char kIdParam[] = "id"; | 18 const char kIdParam[] = "id"; |
18 const char kMethodParam[] = "method"; | 19 const char kMethodParam[] = "method"; |
19 const char kParamsParam[] = "params"; | 20 const char kParamsParam[] = "params"; |
20 | 21 |
21 // JSON RPC 2.0 spec: http://www.jsonrpc.org/specification#error_object | 22 // JSON RPC 2.0 spec: http://www.jsonrpc.org/specification#error_object |
22 const int kStatusParseError = -32700; | 23 const int kStatusParseError = -32700; |
23 const int kStatusInvalidRequest = -32600; | 24 const int kStatusInvalidRequest = -32600; |
24 const int kStatusNoSuchMethod = -32601; | 25 const int kStatusNoSuchMethod = -32601; |
25 | 26 |
26 scoped_ptr<base::DictionaryValue> TakeDictionary(base::DictionaryValue* dict, | 27 scoped_ptr<base::DictionaryValue> TakeDictionary(base::DictionaryValue* dict, |
27 const std::string& key) { | 28 const std::string& key) { |
28 scoped_ptr<base::Value> value; | 29 scoped_ptr<base::Value> value; |
29 dict->Remove(key, &value); | 30 dict->Remove(key, &value); |
30 base::DictionaryValue* result = nullptr; | 31 base::DictionaryValue* result = nullptr; |
31 if (value) | 32 if (value) |
32 value.release()->GetAsDictionary(&result); | 33 value.release()->GetAsDictionary(&result); |
33 return make_scoped_ptr(result); | 34 return make_scoped_ptr(result); |
34 } | 35 } |
35 | 36 |
36 } // namespace | 37 } // namespace |
37 | 38 |
38 DevToolsProtocolHandler::DevToolsProtocolHandler( | 39 DevToolsProtocolHandler::DevToolsProtocolHandler( |
39 DevToolsAgentHost* agent_host, const Notifier& notifier) | 40 DevToolsAgentHostImpl* agent_host) |
40 : agent_host_(agent_host), | 41 : agent_host_(agent_host), client_(agent_host), dispatcher_(agent_host) {} |
41 client_(notifier), | |
42 dispatcher_(notifier) { | |
43 } | |
44 | 42 |
45 DevToolsProtocolHandler::~DevToolsProtocolHandler() { | 43 DevToolsProtocolHandler::~DevToolsProtocolHandler() { |
46 } | 44 } |
47 | 45 |
48 void DevToolsProtocolHandler::HandleMessage(const std::string& message) { | 46 void DevToolsProtocolHandler::HandleMessage(int session_id, |
49 scoped_ptr<base::DictionaryValue> command = ParseCommand(message); | 47 const std::string& message) { |
| 48 scoped_ptr<base::DictionaryValue> command = ParseCommand(session_id, message); |
50 if (!command) | 49 if (!command) |
51 return; | 50 return; |
52 if (PassCommandToDelegate(command.get())) | 51 if (PassCommandToDelegate(session_id, command.get())) |
53 return; | 52 return; |
54 HandleCommand(command.Pass()); | 53 HandleCommand(session_id, command.Pass()); |
55 } | 54 } |
56 | 55 |
57 bool DevToolsProtocolHandler::HandleOptionalMessage( | 56 bool DevToolsProtocolHandler::HandleOptionalMessage(int session_id, |
58 const std::string& message, int* call_id) { | 57 const std::string& message, |
59 scoped_ptr<base::DictionaryValue> command = ParseCommand(message); | 58 int* call_id) { |
| 59 scoped_ptr<base::DictionaryValue> command = ParseCommand(session_id, message); |
60 if (!command) | 60 if (!command) |
61 return true; | 61 return true; |
62 if (PassCommandToDelegate(command.get())) | 62 if (PassCommandToDelegate(session_id, command.get())) |
63 return true; | 63 return true; |
64 return HandleOptionalCommand(command.Pass(), call_id); | 64 return HandleOptionalCommand(session_id, command.Pass(), call_id); |
65 } | 65 } |
66 | 66 |
67 bool DevToolsProtocolHandler::PassCommandToDelegate( | 67 bool DevToolsProtocolHandler::PassCommandToDelegate( |
| 68 int session_id, |
68 base::DictionaryValue* command) { | 69 base::DictionaryValue* command) { |
69 DevToolsManagerDelegate* delegate = | 70 DevToolsManagerDelegate* delegate = |
70 DevToolsManager::GetInstance()->delegate(); | 71 DevToolsManager::GetInstance()->delegate(); |
71 if (!delegate) | 72 if (!delegate) |
72 return false; | 73 return false; |
73 | 74 |
74 scoped_ptr<base::DictionaryValue> response( | 75 scoped_ptr<base::DictionaryValue> response( |
75 delegate->HandleCommand(agent_host_, command)); | 76 delegate->HandleCommand(agent_host_, command)); |
76 if (response) { | 77 if (response) { |
77 std::string json_response; | 78 client_.SendMessage(session_id, *response); |
78 base::JSONWriter::Write(*response, &json_response); | |
79 client_.SendRawMessage(json_response); | |
80 return true; | 79 return true; |
81 } | 80 } |
82 | 81 |
83 return false; | 82 return false; |
84 } | 83 } |
85 | 84 |
86 scoped_ptr<base::DictionaryValue> | 85 scoped_ptr<base::DictionaryValue> DevToolsProtocolHandler::ParseCommand( |
87 DevToolsProtocolHandler::ParseCommand(const std::string& message) { | 86 int session_id, |
| 87 const std::string& message) { |
88 scoped_ptr<base::Value> value = base::JSONReader::Read(message); | 88 scoped_ptr<base::Value> value = base::JSONReader::Read(message); |
89 if (!value || !value->IsType(base::Value::TYPE_DICTIONARY)) { | 89 if (!value || !value->IsType(base::Value::TYPE_DICTIONARY)) { |
90 client_.SendError(DevToolsProtocolClient::kNoId, | 90 client_.SendError( |
91 Response(kStatusParseError, | 91 DevToolsCommandId(DevToolsCommandId::kNoId, session_id), |
92 "Message must be in JSON format")); | 92 Response(kStatusParseError, "Message must be in JSON format")); |
93 return nullptr; | 93 return nullptr; |
94 } | 94 } |
95 | 95 |
96 scoped_ptr<base::DictionaryValue> command = | 96 scoped_ptr<base::DictionaryValue> command = |
97 make_scoped_ptr(static_cast<base::DictionaryValue*>(value.release())); | 97 make_scoped_ptr(static_cast<base::DictionaryValue*>(value.release())); |
98 int id = DevToolsProtocolClient::kNoId; | 98 int call_id = DevToolsCommandId::kNoId; |
99 bool ok = command->GetInteger(kIdParam, &id) && id >= 0; | 99 bool ok = command->GetInteger(kIdParam, &call_id) && call_id >= 0; |
100 if (!ok) { | 100 if (!ok) { |
101 client_.SendError(id, Response(kStatusInvalidRequest, | 101 client_.SendError(DevToolsCommandId(call_id, session_id), |
102 "The type of 'id' property must be number")); | 102 Response(kStatusInvalidRequest, |
| 103 "The type of 'id' property must be number")); |
103 return nullptr; | 104 return nullptr; |
104 } | 105 } |
105 | 106 |
106 std::string method; | 107 std::string method; |
107 ok = command->GetString(kMethodParam, &method); | 108 ok = command->GetString(kMethodParam, &method); |
108 if (!ok) { | 109 if (!ok) { |
109 client_.SendError(id, | 110 client_.SendError(DevToolsCommandId(call_id, session_id), |
110 Response(kStatusInvalidRequest, | 111 Response(kStatusInvalidRequest, |
111 "The type of 'method' property must be string")); | 112 "The type of 'method' property must be string")); |
112 return nullptr; | 113 return nullptr; |
113 } | 114 } |
114 | 115 |
115 return command; | 116 return command; |
116 } | 117 } |
117 | 118 |
118 void DevToolsProtocolHandler::HandleCommand( | 119 void DevToolsProtocolHandler::HandleCommand( |
| 120 int session_id, |
119 scoped_ptr<base::DictionaryValue> command) { | 121 scoped_ptr<base::DictionaryValue> command) { |
120 int id = DevToolsProtocolClient::kNoId; | 122 int call_id = DevToolsCommandId::kNoId; |
121 std::string method; | 123 std::string method; |
122 command->GetInteger(kIdParam, &id); | 124 command->GetInteger(kIdParam, &call_id); |
123 command->GetString(kMethodParam, &method); | 125 command->GetString(kMethodParam, &method); |
124 DevToolsProtocolDispatcher::CommandHandler command_handler( | 126 DevToolsProtocolDispatcher::CommandHandler command_handler( |
125 dispatcher_.FindCommandHandler(method)); | 127 dispatcher_.FindCommandHandler(method)); |
126 if (command_handler.is_null()) { | 128 if (command_handler.is_null()) { |
127 client_.SendError(id, Response(kStatusNoSuchMethod, "No such method")); | 129 client_.SendError(DevToolsCommandId(call_id, session_id), |
| 130 Response(kStatusNoSuchMethod, "No such method")); |
128 return; | 131 return; |
129 } | 132 } |
130 | 133 |
131 bool result = | 134 bool result = |
132 command_handler.Run(id, TakeDictionary(command.get(), kParamsParam)); | 135 command_handler.Run(DevToolsCommandId(call_id, session_id), |
| 136 TakeDictionary(command.get(), kParamsParam)); |
133 DCHECK(result); | 137 DCHECK(result); |
134 } | 138 } |
135 | 139 |
136 bool DevToolsProtocolHandler::HandleOptionalCommand( | 140 bool DevToolsProtocolHandler::HandleOptionalCommand( |
137 scoped_ptr<base::DictionaryValue> command, int* call_id) { | 141 int session_id, |
138 *call_id = DevToolsProtocolClient::kNoId; | 142 scoped_ptr<base::DictionaryValue> command, |
| 143 int* call_id) { |
| 144 *call_id = DevToolsCommandId::kNoId; |
139 std::string method; | 145 std::string method; |
140 command->GetInteger(kIdParam, call_id); | 146 command->GetInteger(kIdParam, call_id); |
141 command->GetString(kMethodParam, &method); | 147 command->GetString(kMethodParam, &method); |
142 DevToolsProtocolDispatcher::CommandHandler command_handler( | 148 DevToolsProtocolDispatcher::CommandHandler command_handler( |
143 dispatcher_.FindCommandHandler(method)); | 149 dispatcher_.FindCommandHandler(method)); |
144 if (!command_handler.is_null()) { | 150 if (!command_handler.is_null()) { |
145 return command_handler.Run( | 151 return command_handler.Run(DevToolsCommandId(*call_id, session_id), |
146 *call_id, TakeDictionary(command.get(), kParamsParam)); | 152 TakeDictionary(command.get(), kParamsParam)); |
147 } | 153 } |
148 return false; | 154 return false; |
149 } | 155 } |
150 | 156 |
151 } // namespace content | 157 } // namespace content |
OLD | NEW |