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 <utility> | 7 #include <utility> |
8 | 8 |
9 #include "base/bind.h" | 9 #include "base/bind.h" |
10 #include "base/json/json_reader.h" | |
11 #include "base/json/json_writer.h" | 10 #include "base/json/json_writer.h" |
12 #include "base/memory/ptr_util.h" | 11 #include "base/memory/ptr_util.h" |
| 12 #include "base/values.h" |
13 #include "content/browser/devtools/devtools_agent_host_impl.h" | 13 #include "content/browser/devtools/devtools_agent_host_impl.h" |
14 #include "content/browser/devtools/devtools_manager.h" | 14 #include "content/browser/devtools/devtools_manager.h" |
15 #include "content/public/browser/devtools_manager_delegate.h" | 15 #include "content/public/browser/devtools_manager_delegate.h" |
16 | 16 |
17 namespace content { | 17 namespace content { |
18 | 18 |
19 namespace { | 19 namespace { |
20 | 20 |
21 const char kIdParam[] = "id"; | 21 const char kIdParam[] = "id"; |
22 const char kMethodParam[] = "method"; | 22 const char kMethodParam[] = "method"; |
(...skipping 17 matching lines...) Expand all Loading... |
40 | 40 |
41 } // namespace | 41 } // namespace |
42 | 42 |
43 DevToolsProtocolHandler::DevToolsProtocolHandler( | 43 DevToolsProtocolHandler::DevToolsProtocolHandler( |
44 DevToolsAgentHostImpl* agent_host) | 44 DevToolsAgentHostImpl* agent_host) |
45 : agent_host_(agent_host), client_(agent_host), dispatcher_(agent_host) {} | 45 : agent_host_(agent_host), client_(agent_host), dispatcher_(agent_host) {} |
46 | 46 |
47 DevToolsProtocolHandler::~DevToolsProtocolHandler() { | 47 DevToolsProtocolHandler::~DevToolsProtocolHandler() { |
48 } | 48 } |
49 | 49 |
50 void DevToolsProtocolHandler::HandleMessage(int session_id, | 50 void DevToolsProtocolHandler::HandleMessage( |
51 const std::string& message) { | 51 int session_id, |
| 52 std::unique_ptr<base::Value> message) { |
52 std::unique_ptr<base::DictionaryValue> command = | 53 std::unique_ptr<base::DictionaryValue> command = |
53 ParseCommand(session_id, message); | 54 ParseCommand(session_id, std::move(message)); |
54 if (!command) | 55 if (!command) |
55 return; | 56 return; |
56 if (PassCommandToDelegate(session_id, command.get())) | 57 if (PassCommandToDelegate(session_id, command.get())) |
57 return; | 58 return; |
58 HandleCommand(session_id, std::move(command)); | 59 HandleCommand(session_id, std::move(command)); |
59 } | 60 } |
60 | 61 |
61 bool DevToolsProtocolHandler::HandleOptionalMessage(int session_id, | 62 bool DevToolsProtocolHandler::HandleOptionalMessage( |
62 const std::string& message, | 63 int session_id, |
63 int* call_id, | 64 std::unique_ptr<base::Value> message, |
64 std::string* method) { | 65 int* call_id, |
| 66 std::string* method) { |
65 std::unique_ptr<base::DictionaryValue> command = | 67 std::unique_ptr<base::DictionaryValue> command = |
66 ParseCommand(session_id, message); | 68 ParseCommand(session_id, std::move(message)); |
67 if (!command) | 69 if (!command) |
68 return true; | 70 return true; |
69 if (PassCommandToDelegate(session_id, command.get())) | 71 if (PassCommandToDelegate(session_id, command.get())) |
70 return true; | 72 return true; |
71 return HandleOptionalCommand(session_id, std::move(command), call_id, method); | 73 return HandleOptionalCommand(session_id, std::move(command), call_id, method); |
72 } | 74 } |
73 | 75 |
74 bool DevToolsProtocolHandler::PassCommandToDelegate( | 76 bool DevToolsProtocolHandler::PassCommandToDelegate( |
75 int session_id, | 77 int session_id, |
76 base::DictionaryValue* command) { | 78 base::DictionaryValue* command) { |
77 DevToolsManagerDelegate* delegate = | 79 DevToolsManagerDelegate* delegate = |
78 DevToolsManager::GetInstance()->delegate(); | 80 DevToolsManager::GetInstance()->delegate(); |
79 if (!delegate) | 81 if (!delegate) |
80 return false; | 82 return false; |
81 | 83 |
82 std::unique_ptr<base::DictionaryValue> response( | 84 std::unique_ptr<base::DictionaryValue> response( |
83 delegate->HandleCommand(agent_host_, command)); | 85 delegate->HandleCommand(agent_host_, command)); |
84 if (response) { | 86 if (response) { |
85 client_.SendMessage(session_id, *response); | 87 client_.SendMessage(session_id, *response); |
86 return true; | 88 return true; |
87 } | 89 } |
88 | 90 |
89 return false; | 91 return false; |
90 } | 92 } |
91 | 93 |
92 std::unique_ptr<base::DictionaryValue> DevToolsProtocolHandler::ParseCommand( | 94 std::unique_ptr<base::DictionaryValue> DevToolsProtocolHandler::ParseCommand( |
93 int session_id, | 95 int session_id, |
94 const std::string& message) { | 96 std::unique_ptr<base::Value> value) { |
95 std::unique_ptr<base::Value> value = base::JSONReader::Read(message); | |
96 if (!value || !value->IsType(base::Value::TYPE_DICTIONARY)) { | 97 if (!value || !value->IsType(base::Value::TYPE_DICTIONARY)) { |
97 client_.SendError( | 98 client_.SendError( |
98 DevToolsCommandId(DevToolsCommandId::kNoId, session_id), | 99 DevToolsCommandId(DevToolsCommandId::kNoId, session_id), |
99 Response(kStatusParseError, "Message must be in JSON format")); | 100 Response(kStatusParseError, "Message must be in JSON format")); |
100 return nullptr; | 101 return nullptr; |
101 } | 102 } |
102 | 103 |
103 std::unique_ptr<base::DictionaryValue> command = | 104 std::unique_ptr<base::DictionaryValue> command = |
104 base::WrapUnique(static_cast<base::DictionaryValue*>(value.release())); | 105 base::WrapUnique(static_cast<base::DictionaryValue*>(value.release())); |
105 int call_id = DevToolsCommandId::kNoId; | 106 int call_id = DevToolsCommandId::kNoId; |
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
155 DevToolsProtocolDispatcher::CommandHandler command_handler( | 156 DevToolsProtocolDispatcher::CommandHandler command_handler( |
156 dispatcher_.FindCommandHandler(*method)); | 157 dispatcher_.FindCommandHandler(*method)); |
157 if (!command_handler.is_null()) { | 158 if (!command_handler.is_null()) { |
158 return command_handler.Run(DevToolsCommandId(*call_id, session_id), | 159 return command_handler.Run(DevToolsCommandId(*call_id, session_id), |
159 TakeDictionary(command.get(), kParamsParam)); | 160 TakeDictionary(command.get(), kParamsParam)); |
160 } | 161 } |
161 return false; | 162 return false; |
162 } | 163 } |
163 | 164 |
164 } // namespace content | 165 } // namespace content |
OLD | NEW |