OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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_http_handler_impl.h" | 5 #include "content/browser/devtools/devtools_http_handler_impl.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 #include <utility> | 8 #include <utility> |
9 | 9 |
10 #include "base/bind.h" | 10 #include "base/bind.h" |
11 #include "base/compiler_specific.h" | 11 #include "base/compiler_specific.h" |
12 #include "base/files/file_util.h" | 12 #include "base/files/file_util.h" |
13 #include "base/json/json_writer.h" | 13 #include "base/json/json_writer.h" |
14 #include "base/logging.h" | 14 #include "base/logging.h" |
15 #include "base/message_loop/message_loop_proxy.h" | 15 #include "base/message_loop/message_loop_proxy.h" |
16 #include "base/stl_util.h" | 16 #include "base/stl_util.h" |
17 #include "base/strings/string_number_conversions.h" | 17 #include "base/strings/string_number_conversions.h" |
18 #include "base/threading/thread.h" | 18 #include "base/threading/thread.h" |
19 #include "base/values.h" | 19 #include "base/values.h" |
20 #include "content/browser/devtools/devtools_manager.h" | 20 #include "content/browser/devtools/devtools_manager.h" |
21 #include "content/browser/devtools/devtools_protocol.h" | 21 #include "content/browser/devtools/devtools_protocol.h" |
22 #include "content/browser/devtools/devtools_protocol_constants.h" | 22 #include "content/browser/devtools/devtools_protocol_constants.h" |
23 #include "content/browser/devtools/devtools_system_info_handler.h" | 23 #include "content/browser/devtools/devtools_system_info_handler.h" |
24 #include "content/browser/devtools/protocol/devtools_protocol_handler_impl.h" | 24 #include "content/browser/devtools/devtools_tracing_handler.h" |
25 #include "content/browser/devtools/protocol/tracing_handler.h" | |
26 #include "content/browser/devtools/tethering_handler.h" | 25 #include "content/browser/devtools/tethering_handler.h" |
27 #include "content/common/devtools_messages.h" | 26 #include "content/common/devtools_messages.h" |
28 #include "content/public/browser/browser_thread.h" | 27 #include "content/public/browser/browser_thread.h" |
29 #include "content/public/browser/devtools_agent_host.h" | 28 #include "content/public/browser/devtools_agent_host.h" |
30 #include "content/public/browser/devtools_http_handler_delegate.h" | 29 #include "content/public/browser/devtools_http_handler_delegate.h" |
31 #include "content/public/browser/devtools_target.h" | 30 #include "content/public/browser/devtools_target.h" |
32 #include "content/public/common/content_client.h" | 31 #include "content/public/common/content_client.h" |
33 #include "content/public/common/url_constants.h" | 32 #include "content/public/common/url_constants.h" |
34 #include "content/public/common/user_agent.h" | 33 #include "content/public/common/user_agent.h" |
35 #include "grit/devtools_resources_map.h" | 34 #include "grit/devtools_resources_map.h" |
(...skipping 116 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
152 | 151 |
153 // DevToolsHttpHandlerImpl::BrowserTarget ------------------------------------ | 152 // DevToolsHttpHandlerImpl::BrowserTarget ------------------------------------ |
154 | 153 |
155 class DevToolsHttpHandlerImpl::BrowserTarget { | 154 class DevToolsHttpHandlerImpl::BrowserTarget { |
156 public: | 155 public: |
157 BrowserTarget(base::MessageLoop* message_loop, | 156 BrowserTarget(base::MessageLoop* message_loop, |
158 net::HttpServer* server, | 157 net::HttpServer* server, |
159 int connection_id) | 158 int connection_id) |
160 : message_loop_(message_loop), | 159 : message_loop_(message_loop), |
161 server_(server), | 160 server_(server), |
162 connection_id_(connection_id), | 161 connection_id_(connection_id) { |
163 tracing_handler_(new devtools::tracing::TracingHandler( | |
164 devtools::tracing::TracingHandler::Browser)), | |
165 protocol_handler_(new DevToolsProtocolHandlerImpl()) { | |
166 protocol_handler_->SetNotifier( | |
167 base::Bind(&BrowserTarget::Respond, base::Unretained(this))); | |
168 protocol_handler_->SetTracingHandler(tracing_handler_.get()); | |
169 } | 162 } |
170 | 163 |
171 ~BrowserTarget() { | 164 ~BrowserTarget() { |
172 STLDeleteElements(&handlers_); | 165 STLDeleteElements(&handlers_); |
173 } | 166 } |
174 | 167 |
175 // Takes ownership. | 168 // Takes ownership. |
176 void RegisterHandler(DevToolsProtocol::Handler* handler) { | 169 void RegisterHandler(DevToolsProtocol::Handler* handler) { |
177 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 170 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
178 handler->SetNotifier( | 171 handler->SetNotifier( |
179 base::Bind(&BrowserTarget::Respond, base::Unretained(this))); | 172 base::Bind(&BrowserTarget::Respond, base::Unretained(this))); |
180 handlers_.push_back(handler); | 173 handlers_.push_back(handler); |
181 } | 174 } |
182 | 175 |
183 void HandleMessage(const std::string& message) { | 176 void HandleMessage(const std::string& message) { |
184 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 177 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
185 std::string error_response; | 178 std::string error_response; |
186 scoped_refptr<DevToolsProtocol::Command> command = | 179 scoped_refptr<DevToolsProtocol::Command> command = |
187 DevToolsProtocol::ParseCommand(message, &error_response); | 180 DevToolsProtocol::ParseCommand(message, &error_response); |
188 if (!command.get()) { | 181 if (!command.get()) { |
189 Respond(error_response); | 182 Respond(error_response); |
190 return; | 183 return; |
191 } | 184 } |
192 | 185 |
193 scoped_refptr<DevToolsProtocol::Response> response = | |
194 protocol_handler_->HandleCommand(command); | |
195 for (const auto& handler : handlers_) { | 186 for (const auto& handler : handlers_) { |
196 if (response.get()) | 187 scoped_refptr<DevToolsProtocol::Response> response = |
197 break; | 188 handler->HandleCommand(command); |
198 response = handler->HandleCommand(command); | 189 if (response.get()) { |
| 190 if (!response->is_async_promise()) |
| 191 Respond(response->Serialize()); |
| 192 return; |
| 193 } |
199 } | 194 } |
200 | 195 |
201 if (response.get()) { | 196 Respond(command->NoSuchMethodErrorResponse()->Serialize()); |
202 if (!response->is_async_promise()) | |
203 Respond(response->Serialize()); | |
204 } else { | |
205 Respond(command->NoSuchMethodErrorResponse()->Serialize()); | |
206 } | |
207 } | 197 } |
208 | 198 |
209 void Respond(const std::string& message) { | 199 void Respond(const std::string& message) { |
210 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 200 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
211 message_loop_->PostTask( | 201 message_loop_->PostTask( |
212 FROM_HERE, | 202 FROM_HERE, |
213 base::Bind(&net::HttpServer::SendOverWebSocket, | 203 base::Bind(&net::HttpServer::SendOverWebSocket, |
214 base::Unretained(server_), | 204 base::Unretained(server_), |
215 connection_id_, | 205 connection_id_, |
216 message)); | 206 message)); |
217 } | 207 } |
218 | 208 |
219 private: | 209 private: |
220 base::MessageLoop* const message_loop_; | 210 base::MessageLoop* const message_loop_; |
221 net::HttpServer* const server_; | 211 net::HttpServer* const server_; |
222 const int connection_id_; | 212 const int connection_id_; |
223 scoped_ptr<devtools::tracing::TracingHandler> tracing_handler_; | |
224 scoped_ptr<DevToolsProtocolHandlerImpl> protocol_handler_; | |
225 std::vector<DevToolsProtocol::Handler*> handlers_; | 213 std::vector<DevToolsProtocol::Handler*> handlers_; |
226 }; | 214 }; |
227 | 215 |
228 // DevToolsHttpHandler ------------------------------------------------------- | 216 // DevToolsHttpHandler ------------------------------------------------------- |
229 | 217 |
230 // static | 218 // static |
231 bool DevToolsHttpHandler::IsSupportedProtocolVersion( | 219 bool DevToolsHttpHandler::IsSupportedProtocolVersion( |
232 const std::string& version) { | 220 const std::string& version) { |
233 return devtools::IsSupportedProtocolVersion(version); | 221 return devtools::IsSupportedProtocolVersion(version); |
234 } | 222 } |
(...skipping 461 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
696 const net::HttpServerRequestInfo& request) { | 684 const net::HttpServerRequestInfo& request) { |
697 if (!thread_) | 685 if (!thread_) |
698 return; | 686 return; |
699 | 687 |
700 std::string browser_prefix = "/devtools/browser"; | 688 std::string browser_prefix = "/devtools/browser"; |
701 size_t browser_pos = request.path.find(browser_prefix); | 689 size_t browser_pos = request.path.find(browser_prefix); |
702 if (browser_pos == 0) { | 690 if (browser_pos == 0) { |
703 BrowserTarget* browser_target = new BrowserTarget( | 691 BrowserTarget* browser_target = new BrowserTarget( |
704 thread_->message_loop(), server_.get(), connection_id); | 692 thread_->message_loop(), server_.get(), connection_id); |
705 browser_target->RegisterHandler( | 693 browser_target->RegisterHandler( |
| 694 new DevToolsTracingHandler(DevToolsTracingHandler::Browser)); |
| 695 browser_target->RegisterHandler( |
706 new TetheringHandler(delegate_.get(), thread_->message_loop_proxy())); | 696 new TetheringHandler(delegate_.get(), thread_->message_loop_proxy())); |
707 browser_target->RegisterHandler( | 697 browser_target->RegisterHandler( |
708 new DevToolsSystemInfoHandler()); | 698 new DevToolsSystemInfoHandler()); |
709 browser_targets_[connection_id] = browser_target; | 699 browser_targets_[connection_id] = browser_target; |
710 AcceptWebSocket(connection_id, request); | 700 AcceptWebSocket(connection_id, request); |
711 return; | 701 return; |
712 } | 702 } |
713 | 703 |
714 size_t pos = request.path.find(kPageUrlPrefix); | 704 size_t pos = request.path.find(kPageUrlPrefix); |
715 if (pos != 0) { | 705 if (pos != 0) { |
(...skipping 253 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
969 id.c_str(), | 959 id.c_str(), |
970 host); | 960 host); |
971 dictionary->SetString( | 961 dictionary->SetString( |
972 kTargetDevtoolsFrontendUrlField, devtools_frontend_url); | 962 kTargetDevtoolsFrontendUrlField, devtools_frontend_url); |
973 } | 963 } |
974 | 964 |
975 return dictionary; | 965 return dictionary; |
976 } | 966 } |
977 | 967 |
978 } // namespace content | 968 } // namespace content |
OLD | NEW |