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