OLD | NEW |
| (Empty) |
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 | |
3 // found in the LICENSE file. | |
4 | |
5 #include <stddef.h> | |
6 #include <stdint.h> | |
7 | |
8 #include <algorithm> | |
9 #include <utility> | |
10 | |
11 #include "base/bind.h" | |
12 #include "base/compiler_specific.h" | |
13 #include "base/files/file_util.h" | |
14 #include "base/json/json_writer.h" | |
15 #include "base/location.h" | |
16 #include "base/logging.h" | |
17 #include "base/single_thread_task_runner.h" | |
18 #include "base/stl_util.h" | |
19 #include "base/strings/string_number_conversions.h" | |
20 #include "base/strings/string_util.h" | |
21 #include "base/strings/stringprintf.h" | |
22 #include "base/threading/thread.h" | |
23 #include "base/values.h" | |
24 #include "build/build_config.h" | |
25 #include "components/devtools_http_handler/devtools_http_handler.h" | |
26 #include "components/devtools_http_handler/devtools_http_handler_delegate.h" | |
27 #include "content/public/browser/browser_thread.h" | |
28 #include "content/public/browser/devtools_external_agent_proxy_delegate.h" | |
29 #include "content/public/browser/devtools_manager_delegate.h" | |
30 #include "content/public/browser/devtools_socket_factory.h" | |
31 #include "content/public/common/url_constants.h" | |
32 #include "content/public/common/user_agent.h" | |
33 #include "net/base/escape.h" | |
34 #include "net/base/io_buffer.h" | |
35 #include "net/base/ip_endpoint.h" | |
36 #include "net/base/net_errors.h" | |
37 #include "net/server/http_server.h" | |
38 #include "net/server/http_server_request_info.h" | |
39 #include "net/server/http_server_response_info.h" | |
40 #include "net/socket/server_socket.h" | |
41 | |
42 #if defined(OS_ANDROID) | |
43 #include "base/android/build_info.h" | |
44 #endif | |
45 | |
46 using content::BrowserThread; | |
47 using content::DevToolsAgentHost; | |
48 using content::DevToolsAgentHostClient; | |
49 | |
50 namespace devtools_http_handler { | |
51 | |
52 namespace { | |
53 | |
54 const base::FilePath::CharType kDevToolsActivePortFileName[] = | |
55 FILE_PATH_LITERAL("DevToolsActivePort"); | |
56 | |
57 const char kDevToolsHandlerThreadName[] = "Chrome_DevToolsHandlerThread"; | |
58 | |
59 const char kPageUrlPrefix[] = "/devtools/page/"; | |
60 | |
61 const char kTargetIdField[] = "id"; | |
62 const char kTargetParentIdField[] = "parentId"; | |
63 const char kTargetTypeField[] = "type"; | |
64 const char kTargetTitleField[] = "title"; | |
65 const char kTargetDescriptionField[] = "description"; | |
66 const char kTargetUrlField[] = "url"; | |
67 const char kTargetFaviconUrlField[] = "faviconUrl"; | |
68 const char kTargetWebSocketDebuggerUrlField[] = "webSocketDebuggerUrl"; | |
69 const char kTargetDevtoolsFrontendUrlField[] = "devtoolsFrontendUrl"; | |
70 | |
71 // Maximum write buffer size of devtools http/websocket connections. | |
72 // TODO(rmcilroy/pfieldman): Reduce this back to 100Mb when we have | |
73 // added back pressure on the TraceComplete message protocol - crbug.com/456845. | |
74 const int32_t kSendBufferSizeForDevTools = 256 * 1024 * 1024; // 256Mb | |
75 | |
76 } // namespace | |
77 | |
78 // ServerWrapper ------------------------------------------------------------- | |
79 // All methods in this class are only called on handler thread. | |
80 class ServerWrapper : net::HttpServer::Delegate { | |
81 public: | |
82 ServerWrapper(base::WeakPtr<DevToolsHttpHandler> handler, | |
83 std::unique_ptr<net::ServerSocket> socket, | |
84 const base::FilePath& frontend_dir, | |
85 bool bundles_resources); | |
86 | |
87 int GetLocalAddress(net::IPEndPoint* address); | |
88 | |
89 void AcceptWebSocket(int connection_id, | |
90 const net::HttpServerRequestInfo& request); | |
91 void SendOverWebSocket(int connection_id, const std::string& message); | |
92 void SendResponse(int connection_id, | |
93 const net::HttpServerResponseInfo& response); | |
94 void Send200(int connection_id, | |
95 const std::string& data, | |
96 const std::string& mime_type); | |
97 void Send404(int connection_id); | |
98 void Send500(int connection_id, const std::string& message); | |
99 void Close(int connection_id); | |
100 | |
101 void WriteActivePortToUserProfile(const base::FilePath& output_directory); | |
102 | |
103 ~ServerWrapper() override {} | |
104 | |
105 private: | |
106 // net::HttpServer::Delegate implementation. | |
107 void OnConnect(int connection_id) override {} | |
108 void OnHttpRequest(int connection_id, | |
109 const net::HttpServerRequestInfo& info) override; | |
110 void OnWebSocketRequest(int connection_id, | |
111 const net::HttpServerRequestInfo& info) override; | |
112 void OnWebSocketMessage(int connection_id, | |
113 const std::string& data) override; | |
114 void OnClose(int connection_id) override; | |
115 | |
116 base::WeakPtr<DevToolsHttpHandler> handler_; | |
117 std::unique_ptr<net::HttpServer> server_; | |
118 base::FilePath frontend_dir_; | |
119 bool bundles_resources_; | |
120 }; | |
121 | |
122 ServerWrapper::ServerWrapper(base::WeakPtr<DevToolsHttpHandler> handler, | |
123 std::unique_ptr<net::ServerSocket> socket, | |
124 const base::FilePath& frontend_dir, | |
125 bool bundles_resources) | |
126 : handler_(handler), | |
127 server_(new net::HttpServer(std::move(socket), this)), | |
128 frontend_dir_(frontend_dir), | |
129 bundles_resources_(bundles_resources) {} | |
130 | |
131 int ServerWrapper::GetLocalAddress(net::IPEndPoint* address) { | |
132 return server_->GetLocalAddress(address); | |
133 } | |
134 | |
135 void ServerWrapper::AcceptWebSocket(int connection_id, | |
136 const net::HttpServerRequestInfo& request) { | |
137 server_->SetSendBufferSize(connection_id, kSendBufferSizeForDevTools); | |
138 server_->AcceptWebSocket(connection_id, request); | |
139 } | |
140 | |
141 void ServerWrapper::SendOverWebSocket(int connection_id, | |
142 const std::string& message) { | |
143 server_->SendOverWebSocket(connection_id, message); | |
144 } | |
145 | |
146 void ServerWrapper::SendResponse(int connection_id, | |
147 const net::HttpServerResponseInfo& response) { | |
148 server_->SendResponse(connection_id, response); | |
149 } | |
150 | |
151 void ServerWrapper::Send200(int connection_id, | |
152 const std::string& data, | |
153 const std::string& mime_type) { | |
154 server_->Send200(connection_id, data, mime_type); | |
155 } | |
156 | |
157 void ServerWrapper::Send404(int connection_id) { | |
158 server_->Send404(connection_id); | |
159 } | |
160 | |
161 void ServerWrapper::Send500(int connection_id, | |
162 const std::string& message) { | |
163 server_->Send500(connection_id, message); | |
164 } | |
165 | |
166 void ServerWrapper::Close(int connection_id) { | |
167 server_->Close(connection_id); | |
168 } | |
169 | |
170 // Thread and ServerWrapper lifetime management ------------------------------ | |
171 | |
172 void TerminateOnUI(base::Thread* thread, | |
173 ServerWrapper* server_wrapper, | |
174 content::DevToolsSocketFactory* socket_factory) { | |
175 DCHECK_CURRENTLY_ON(BrowserThread::UI); | |
176 if (server_wrapper) { | |
177 DCHECK(thread); | |
178 thread->task_runner()->DeleteSoon(FROM_HERE, server_wrapper); | |
179 } | |
180 if (socket_factory) { | |
181 DCHECK(thread); | |
182 thread->task_runner()->DeleteSoon(FROM_HERE, socket_factory); | |
183 } | |
184 if (thread) { | |
185 BrowserThread::DeleteSoon(BrowserThread::FILE, FROM_HERE, thread); | |
186 } | |
187 } | |
188 | |
189 void ServerStartedOnUI(base::WeakPtr<DevToolsHttpHandler> handler, | |
190 base::Thread* thread, | |
191 ServerWrapper* server_wrapper, | |
192 content::DevToolsSocketFactory* socket_factory, | |
193 std::unique_ptr<net::IPEndPoint> ip_address) { | |
194 DCHECK_CURRENTLY_ON(BrowserThread::UI); | |
195 if (handler && thread && server_wrapper) { | |
196 handler->ServerStarted(thread, server_wrapper, socket_factory, | |
197 std::move(ip_address)); | |
198 } else { | |
199 TerminateOnUI(thread, server_wrapper, socket_factory); | |
200 } | |
201 } | |
202 | |
203 void StartServerOnHandlerThread( | |
204 base::WeakPtr<DevToolsHttpHandler> handler, | |
205 base::Thread* thread, | |
206 content::DevToolsSocketFactory* socket_factory, | |
207 const base::FilePath& output_directory, | |
208 const base::FilePath& frontend_dir, | |
209 bool bundles_resources) { | |
210 DCHECK(thread->task_runner()->BelongsToCurrentThread()); | |
211 ServerWrapper* server_wrapper = nullptr; | |
212 std::unique_ptr<net::ServerSocket> server_socket = | |
213 socket_factory->CreateForHttpServer(); | |
214 std::unique_ptr<net::IPEndPoint> ip_address(new net::IPEndPoint); | |
215 if (server_socket) { | |
216 server_wrapper = new ServerWrapper(handler, std::move(server_socket), | |
217 frontend_dir, bundles_resources); | |
218 if (!output_directory.empty()) | |
219 server_wrapper->WriteActivePortToUserProfile(output_directory); | |
220 | |
221 if (server_wrapper->GetLocalAddress(ip_address.get()) != net::OK) | |
222 ip_address.reset(); | |
223 } else { | |
224 ip_address.reset(); | |
225 LOG(ERROR) << "Cannot start http server for devtools. Stop devtools."; | |
226 } | |
227 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, | |
228 base::Bind(&ServerStartedOnUI, | |
229 handler, | |
230 thread, | |
231 server_wrapper, | |
232 socket_factory, | |
233 base::Passed(&ip_address))); | |
234 } | |
235 | |
236 void StartServerOnFile( | |
237 base::WeakPtr<DevToolsHttpHandler> handler, | |
238 content::DevToolsSocketFactory* socket_factory, | |
239 const base::FilePath& output_directory, | |
240 const base::FilePath& frontend_dir, | |
241 bool bundles_resources) { | |
242 DCHECK_CURRENTLY_ON(BrowserThread::FILE); | |
243 std::unique_ptr<base::Thread> thread( | |
244 new base::Thread(kDevToolsHandlerThreadName)); | |
245 base::Thread::Options options; | |
246 options.message_loop_type = base::MessageLoop::TYPE_IO; | |
247 if (thread->StartWithOptions(options)) { | |
248 base::MessageLoop* message_loop = thread->message_loop(); | |
249 message_loop->task_runner()->PostTask( | |
250 FROM_HERE, | |
251 base::Bind(&StartServerOnHandlerThread, handler, | |
252 base::Unretained(thread.release()), socket_factory, | |
253 output_directory, frontend_dir, bundles_resources)); | |
254 } | |
255 } | |
256 | |
257 // DevToolsAgentHostClientImpl ----------------------------------------------- | |
258 // An internal implementation of DevToolsAgentHostClient that delegates | |
259 // messages sent to a DebuggerShell instance. | |
260 class DevToolsAgentHostClientImpl : public DevToolsAgentHostClient { | |
261 public: | |
262 DevToolsAgentHostClientImpl(base::MessageLoop* message_loop, | |
263 ServerWrapper* server_wrapper, | |
264 int connection_id, | |
265 scoped_refptr<DevToolsAgentHost> agent_host) | |
266 : message_loop_(message_loop), | |
267 server_wrapper_(server_wrapper), | |
268 connection_id_(connection_id), | |
269 agent_host_(agent_host) { | |
270 DCHECK_CURRENTLY_ON(BrowserThread::UI); | |
271 agent_host_->AttachClient(this); | |
272 } | |
273 | |
274 ~DevToolsAgentHostClientImpl() override { | |
275 DCHECK_CURRENTLY_ON(BrowserThread::UI); | |
276 if (agent_host_.get()) | |
277 agent_host_->DetachClient(this); | |
278 } | |
279 | |
280 void AgentHostClosed(DevToolsAgentHost* agent_host, | |
281 bool replaced_with_another_client) override { | |
282 DCHECK_CURRENTLY_ON(BrowserThread::UI); | |
283 DCHECK(agent_host == agent_host_.get()); | |
284 | |
285 std::string message = base::StringPrintf( | |
286 "{ \"method\": \"Inspector.detached\", " | |
287 "\"params\": { \"reason\": \"%s\"} }", | |
288 replaced_with_another_client ? | |
289 "replaced_with_devtools" : "target_closed"); | |
290 DispatchProtocolMessage(agent_host, message); | |
291 | |
292 agent_host_ = nullptr; | |
293 message_loop_->task_runner()->PostTask( | |
294 FROM_HERE, | |
295 base::Bind(&ServerWrapper::Close, base::Unretained(server_wrapper_), | |
296 connection_id_)); | |
297 } | |
298 | |
299 void DispatchProtocolMessage(DevToolsAgentHost* agent_host, | |
300 const std::string& message) override { | |
301 DCHECK_CURRENTLY_ON(BrowserThread::UI); | |
302 DCHECK(agent_host == agent_host_.get()); | |
303 message_loop_->task_runner()->PostTask( | |
304 FROM_HERE, | |
305 base::Bind(&ServerWrapper::SendOverWebSocket, | |
306 base::Unretained(server_wrapper_), connection_id_, message)); | |
307 } | |
308 | |
309 void OnMessage(const std::string& message) { | |
310 DCHECK_CURRENTLY_ON(BrowserThread::UI); | |
311 if (agent_host_.get()) | |
312 agent_host_->DispatchProtocolMessage(this, message); | |
313 } | |
314 | |
315 private: | |
316 base::MessageLoop* const message_loop_; | |
317 ServerWrapper* const server_wrapper_; | |
318 const int connection_id_; | |
319 scoped_refptr<DevToolsAgentHost> agent_host_; | |
320 }; | |
321 | |
322 static bool TimeComparator(scoped_refptr<DevToolsAgentHost> host1, | |
323 scoped_refptr<DevToolsAgentHost> host2) { | |
324 return host1->GetLastActivityTime() > host2->GetLastActivityTime(); | |
325 } | |
326 | |
327 // DevToolsHttpHandler ------------------------------------------------------- | |
328 | |
329 DevToolsHttpHandler::~DevToolsHttpHandler() { | |
330 TerminateOnUI(thread_, server_wrapper_, socket_factory_); | |
331 } | |
332 | |
333 static std::string PathWithoutParams(const std::string& path) { | |
334 size_t query_position = path.find("?"); | |
335 if (query_position != std::string::npos) | |
336 return path.substr(0, query_position); | |
337 return path; | |
338 } | |
339 | |
340 static std::string GetMimeType(const std::string& filename) { | |
341 if (base::EndsWith(filename, ".html", base::CompareCase::INSENSITIVE_ASCII)) { | |
342 return "text/html"; | |
343 } else if (base::EndsWith(filename, ".css", | |
344 base::CompareCase::INSENSITIVE_ASCII)) { | |
345 return "text/css"; | |
346 } else if (base::EndsWith(filename, ".js", | |
347 base::CompareCase::INSENSITIVE_ASCII)) { | |
348 return "application/javascript"; | |
349 } else if (base::EndsWith(filename, ".png", | |
350 base::CompareCase::INSENSITIVE_ASCII)) { | |
351 return "image/png"; | |
352 } else if (base::EndsWith(filename, ".gif", | |
353 base::CompareCase::INSENSITIVE_ASCII)) { | |
354 return "image/gif"; | |
355 } else if (base::EndsWith(filename, ".json", | |
356 base::CompareCase::INSENSITIVE_ASCII)) { | |
357 return "application/json"; | |
358 } else if (base::EndsWith(filename, ".svg", | |
359 base::CompareCase::INSENSITIVE_ASCII)) { | |
360 return "image/svg+xml"; | |
361 } | |
362 LOG(ERROR) << "GetMimeType doesn't know mime type for: " | |
363 << filename | |
364 << " text/plain will be returned"; | |
365 NOTREACHED(); | |
366 return "text/plain"; | |
367 } | |
368 | |
369 void ServerWrapper::OnHttpRequest(int connection_id, | |
370 const net::HttpServerRequestInfo& info) { | |
371 server_->SetSendBufferSize(connection_id, kSendBufferSizeForDevTools); | |
372 | |
373 if (base::StartsWith(info.path, "/json", base::CompareCase::SENSITIVE)) { | |
374 BrowserThread::PostTask( | |
375 BrowserThread::UI, | |
376 FROM_HERE, | |
377 base::Bind(&DevToolsHttpHandler::OnJsonRequest, | |
378 handler_, | |
379 connection_id, | |
380 info)); | |
381 return; | |
382 } | |
383 | |
384 if (info.path.empty() || info.path == "/") { | |
385 // Discovery page request. | |
386 BrowserThread::PostTask( | |
387 BrowserThread::UI, | |
388 FROM_HERE, | |
389 base::Bind(&DevToolsHttpHandler::OnDiscoveryPageRequest, | |
390 handler_, | |
391 connection_id)); | |
392 return; | |
393 } | |
394 | |
395 if (!base::StartsWith(info.path, "/devtools/", | |
396 base::CompareCase::SENSITIVE)) { | |
397 server_->Send404(connection_id); | |
398 return; | |
399 } | |
400 | |
401 std::string filename = PathWithoutParams(info.path.substr(10)); | |
402 std::string mime_type = GetMimeType(filename); | |
403 | |
404 if (!frontend_dir_.empty()) { | |
405 base::FilePath path = frontend_dir_.AppendASCII(filename); | |
406 std::string data; | |
407 base::ReadFileToString(path, &data); | |
408 server_->Send200(connection_id, data, mime_type); | |
409 return; | |
410 } | |
411 | |
412 if (bundles_resources_) { | |
413 BrowserThread::PostTask( | |
414 BrowserThread::UI, | |
415 FROM_HERE, | |
416 base::Bind(&DevToolsHttpHandler::OnFrontendResourceRequest, | |
417 handler_, | |
418 connection_id, | |
419 filename)); | |
420 return; | |
421 } | |
422 server_->Send404(connection_id); | |
423 } | |
424 | |
425 void ServerWrapper::OnWebSocketRequest( | |
426 int connection_id, | |
427 const net::HttpServerRequestInfo& request) { | |
428 BrowserThread::PostTask( | |
429 BrowserThread::UI, | |
430 FROM_HERE, | |
431 base::Bind( | |
432 &DevToolsHttpHandler::OnWebSocketRequest, | |
433 handler_, | |
434 connection_id, | |
435 request)); | |
436 } | |
437 | |
438 void ServerWrapper::OnWebSocketMessage(int connection_id, | |
439 const std::string& data) { | |
440 BrowserThread::PostTask( | |
441 BrowserThread::UI, | |
442 FROM_HERE, | |
443 base::Bind( | |
444 &DevToolsHttpHandler::OnWebSocketMessage, | |
445 handler_, | |
446 connection_id, | |
447 data)); | |
448 } | |
449 | |
450 void ServerWrapper::OnClose(int connection_id) { | |
451 BrowserThread::PostTask( | |
452 BrowserThread::UI, | |
453 FROM_HERE, | |
454 base::Bind( | |
455 &DevToolsHttpHandler::OnClose, | |
456 handler_, | |
457 connection_id)); | |
458 } | |
459 | |
460 std::string DevToolsHttpHandler::GetFrontendURLInternal( | |
461 const std::string& id, | |
462 const std::string& host) { | |
463 return base::StringPrintf( | |
464 "%s%sws=%s%s%s", | |
465 frontend_url_.c_str(), | |
466 frontend_url_.find("?") == std::string::npos ? "?" : "&", | |
467 host.c_str(), | |
468 kPageUrlPrefix, | |
469 id.c_str()); | |
470 } | |
471 | |
472 static bool ParseJsonPath( | |
473 const std::string& path, | |
474 std::string* command, | |
475 std::string* target_id) { | |
476 | |
477 // Fall back to list in case of empty query. | |
478 if (path.empty()) { | |
479 *command = "list"; | |
480 return true; | |
481 } | |
482 | |
483 if (!base::StartsWith(path, "/", base::CompareCase::SENSITIVE)) { | |
484 // Malformed command. | |
485 return false; | |
486 } | |
487 *command = path.substr(1); | |
488 | |
489 size_t separator_pos = command->find("/"); | |
490 if (separator_pos != std::string::npos) { | |
491 *target_id = command->substr(separator_pos + 1); | |
492 *command = command->substr(0, separator_pos); | |
493 } | |
494 return true; | |
495 } | |
496 | |
497 void DevToolsHttpHandler::OnJsonRequest( | |
498 int connection_id, | |
499 const net::HttpServerRequestInfo& info) { | |
500 // Trim /json | |
501 std::string path = info.path.substr(5); | |
502 | |
503 // Trim fragment and query | |
504 std::string query; | |
505 size_t query_pos = path.find("?"); | |
506 if (query_pos != std::string::npos) { | |
507 query = path.substr(query_pos + 1); | |
508 path = path.substr(0, query_pos); | |
509 } | |
510 | |
511 size_t fragment_pos = path.find("#"); | |
512 if (fragment_pos != std::string::npos) | |
513 path = path.substr(0, fragment_pos); | |
514 | |
515 std::string command; | |
516 std::string target_id; | |
517 if (!ParseJsonPath(path, &command, &target_id)) { | |
518 SendJson(connection_id, | |
519 net::HTTP_NOT_FOUND, | |
520 NULL, | |
521 "Malformed query: " + info.path); | |
522 return; | |
523 } | |
524 | |
525 if (command == "version") { | |
526 base::DictionaryValue version; | |
527 version.SetString("Protocol-Version", | |
528 DevToolsAgentHost::GetProtocolVersion().c_str()); | |
529 version.SetString("WebKit-Version", content::GetWebKitVersion()); | |
530 version.SetString("Browser", product_name_); | |
531 version.SetString("User-Agent", user_agent_); | |
532 #if defined(OS_ANDROID) | |
533 version.SetString("Android-Package", | |
534 base::android::BuildInfo::GetInstance()->package_name()); | |
535 #endif | |
536 SendJson(connection_id, net::HTTP_OK, &version, std::string()); | |
537 return; | |
538 } | |
539 | |
540 if (command == "list") { | |
541 std::string host = info.headers["host"]; | |
542 DevToolsAgentHost::List agent_hosts = | |
543 content::DevToolsAgentHost::DiscoverAllHosts(); | |
544 std::sort(agent_hosts.begin(), agent_hosts.end(), TimeComparator); | |
545 agent_host_map_.clear(); | |
546 base::ListValue list_value; | |
547 for (auto& agent_host : agent_hosts) { | |
548 agent_host_map_[agent_host->GetId()] = agent_host; | |
549 list_value.Append(SerializeDescriptor(agent_host, host)); | |
550 } | |
551 SendJson(connection_id, net::HTTP_OK, &list_value, std::string()); | |
552 return; | |
553 } | |
554 | |
555 if (command == "new") { | |
556 GURL url(net::UnescapeURLComponent( | |
557 query, net::UnescapeRule::URL_SPECIAL_CHARS_EXCEPT_PATH_SEPARATORS | | |
558 net::UnescapeRule::PATH_SEPARATORS)); | |
559 if (!url.is_valid()) | |
560 url = GURL(url::kAboutBlankURL); | |
561 scoped_refptr<DevToolsAgentHost> agent_host = nullptr; | |
562 content::DevToolsManagerDelegate* delegate = | |
563 DevToolsAgentHost::GetDevToolsManagerDelegate(); | |
564 if (delegate) | |
565 agent_host = delegate->CreateNewTarget(url); | |
566 if (!agent_host) { | |
567 SendJson(connection_id, | |
568 net::HTTP_INTERNAL_SERVER_ERROR, | |
569 NULL, | |
570 "Could not create new page"); | |
571 return; | |
572 } | |
573 std::string host = info.headers["host"]; | |
574 std::unique_ptr<base::DictionaryValue> dictionary( | |
575 SerializeDescriptor(agent_host, host)); | |
576 SendJson(connection_id, net::HTTP_OK, dictionary.get(), std::string()); | |
577 const std::string target_id = agent_host->GetId(); | |
578 agent_host_map_[target_id] = agent_host; | |
579 return; | |
580 } | |
581 | |
582 if (command == "activate" || command == "close") { | |
583 scoped_refptr<DevToolsAgentHost> agent_host = GetAgentHost(target_id); | |
584 if (!agent_host) { | |
585 SendJson(connection_id, | |
586 net::HTTP_NOT_FOUND, | |
587 NULL, | |
588 "No such target id: " + target_id); | |
589 return; | |
590 } | |
591 | |
592 if (command == "activate") { | |
593 if (agent_host->Activate()) { | |
594 SendJson(connection_id, net::HTTP_OK, NULL, "Target activated"); | |
595 } else { | |
596 SendJson(connection_id, | |
597 net::HTTP_INTERNAL_SERVER_ERROR, | |
598 NULL, | |
599 "Could not activate target id: " + target_id); | |
600 } | |
601 return; | |
602 } | |
603 | |
604 if (command == "close") { | |
605 if (agent_host->Close()) { | |
606 SendJson(connection_id, net::HTTP_OK, NULL, "Target is closing"); | |
607 } else { | |
608 SendJson(connection_id, | |
609 net::HTTP_INTERNAL_SERVER_ERROR, | |
610 NULL, | |
611 "Could not close target id: " + target_id); | |
612 } | |
613 return; | |
614 } | |
615 } | |
616 SendJson(connection_id, | |
617 net::HTTP_NOT_FOUND, | |
618 NULL, | |
619 "Unknown command: " + command); | |
620 return; | |
621 } | |
622 | |
623 scoped_refptr<DevToolsAgentHost> DevToolsHttpHandler::GetAgentHost( | |
624 const std::string& target_id) { | |
625 DescriptorMap::const_iterator it = agent_host_map_.find(target_id); | |
626 return it != agent_host_map_.end() ? it->second : nullptr; | |
627 } | |
628 | |
629 void DevToolsHttpHandler::OnDiscoveryPageRequest(int connection_id) { | |
630 std::string response = delegate_->GetDiscoveryPageHTML(); | |
631 Send200(connection_id, response, "text/html; charset=UTF-8"); | |
632 } | |
633 | |
634 void DevToolsHttpHandler::OnFrontendResourceRequest( | |
635 int connection_id, const std::string& path) { | |
636 Send200(connection_id, | |
637 delegate_->GetFrontendResource(path), | |
638 GetMimeType(path)); | |
639 } | |
640 | |
641 void DevToolsHttpHandler::OnWebSocketRequest( | |
642 int connection_id, | |
643 const net::HttpServerRequestInfo& request) { | |
644 if (!thread_) | |
645 return; | |
646 | |
647 std::string browser_prefix = "/devtools/browser"; | |
648 if (base::StartsWith(request.path, browser_prefix, | |
649 base::CompareCase::SENSITIVE)) { | |
650 scoped_refptr<DevToolsAgentHost> browser_agent = | |
651 DevToolsAgentHost::CreateForBrowser( | |
652 thread_->task_runner(), | |
653 base::Bind(&content::DevToolsSocketFactory::CreateForTethering, | |
654 base::Unretained(socket_factory_))); | |
655 connection_to_client_[connection_id] = new DevToolsAgentHostClientImpl( | |
656 thread_->message_loop(), server_wrapper_, connection_id, browser_agent); | |
657 AcceptWebSocket(connection_id, request); | |
658 return; | |
659 } | |
660 | |
661 if (!base::StartsWith(request.path, kPageUrlPrefix, | |
662 base::CompareCase::SENSITIVE)) { | |
663 Send404(connection_id); | |
664 return; | |
665 } | |
666 | |
667 std::string target_id = request.path.substr(strlen(kPageUrlPrefix)); | |
668 scoped_refptr<DevToolsAgentHost> agent = GetAgentHost(target_id); | |
669 if (!agent) { | |
670 Send500(connection_id, "No such target id: " + target_id); | |
671 return; | |
672 } | |
673 | |
674 if (agent->IsAttached()) { | |
675 Send500(connection_id, | |
676 "Target with given id is being inspected: " + target_id); | |
677 return; | |
678 } | |
679 | |
680 DevToolsAgentHostClientImpl* client_host = new DevToolsAgentHostClientImpl( | |
681 thread_->message_loop(), server_wrapper_, connection_id, agent); | |
682 connection_to_client_[connection_id] = client_host; | |
683 | |
684 AcceptWebSocket(connection_id, request); | |
685 } | |
686 | |
687 void DevToolsHttpHandler::OnWebSocketMessage( | |
688 int connection_id, | |
689 const std::string& data) { | |
690 ConnectionToClientMap::iterator it = | |
691 connection_to_client_.find(connection_id); | |
692 if (it != connection_to_client_.end()) | |
693 it->second->OnMessage(data); | |
694 } | |
695 | |
696 void DevToolsHttpHandler::OnClose(int connection_id) { | |
697 ConnectionToClientMap::iterator it = | |
698 connection_to_client_.find(connection_id); | |
699 if (it != connection_to_client_.end()) { | |
700 delete it->second; | |
701 connection_to_client_.erase(connection_id); | |
702 } | |
703 } | |
704 | |
705 DevToolsHttpHandler::DevToolsHttpHandler( | |
706 std::unique_ptr<content::DevToolsSocketFactory> socket_factory, | |
707 const std::string& frontend_url, | |
708 DevToolsHttpHandlerDelegate* delegate, | |
709 const base::FilePath& output_directory, | |
710 const base::FilePath& debug_frontend_dir, | |
711 const std::string& product_name, | |
712 const std::string& user_agent) | |
713 : thread_(nullptr), | |
714 frontend_url_(frontend_url), | |
715 product_name_(product_name), | |
716 user_agent_(user_agent), | |
717 server_wrapper_(nullptr), | |
718 delegate_(delegate), | |
719 socket_factory_(nullptr), | |
720 weak_factory_(this) { | |
721 bool bundles_resources = frontend_url_.empty(); | |
722 if (frontend_url_.empty()) | |
723 frontend_url_ = "/devtools/inspector.html"; | |
724 | |
725 BrowserThread::PostTask( | |
726 BrowserThread::FILE, FROM_HERE, | |
727 base::Bind(&StartServerOnFile, | |
728 weak_factory_.GetWeakPtr(), | |
729 socket_factory.release(), | |
730 output_directory, | |
731 debug_frontend_dir, | |
732 bundles_resources)); | |
733 } | |
734 | |
735 void DevToolsHttpHandler::ServerStarted( | |
736 base::Thread* thread, | |
737 ServerWrapper* server_wrapper, | |
738 content::DevToolsSocketFactory* socket_factory, | |
739 std::unique_ptr<net::IPEndPoint> ip_address) { | |
740 thread_ = thread; | |
741 server_wrapper_ = server_wrapper; | |
742 socket_factory_ = socket_factory; | |
743 server_ip_address_.swap(ip_address); | |
744 } | |
745 | |
746 void ServerWrapper::WriteActivePortToUserProfile( | |
747 const base::FilePath& output_directory) { | |
748 DCHECK(!output_directory.empty()); | |
749 net::IPEndPoint endpoint; | |
750 int err; | |
751 if ((err = server_->GetLocalAddress(&endpoint)) != net::OK) { | |
752 LOG(ERROR) << "Error " << err << " getting local address"; | |
753 return; | |
754 } | |
755 | |
756 // Write this port to a well-known file in the profile directory | |
757 // so Telemetry can pick it up. | |
758 base::FilePath path = output_directory.Append(kDevToolsActivePortFileName); | |
759 std::string port_string = base::UintToString(endpoint.port()); | |
760 if (base::WriteFile(path, port_string.c_str(), | |
761 static_cast<int>(port_string.length())) < 0) { | |
762 LOG(ERROR) << "Error writing DevTools active port to file"; | |
763 } | |
764 } | |
765 | |
766 void DevToolsHttpHandler::SendJson(int connection_id, | |
767 net::HttpStatusCode status_code, | |
768 base::Value* value, | |
769 const std::string& message) { | |
770 if (!thread_) | |
771 return; | |
772 | |
773 // Serialize value and message. | |
774 std::string json_value; | |
775 if (value) { | |
776 base::JSONWriter::WriteWithOptions( | |
777 *value, base::JSONWriter::OPTIONS_PRETTY_PRINT, &json_value); | |
778 } | |
779 std::string json_message; | |
780 base::JSONWriter::Write(base::StringValue(message), &json_message); | |
781 | |
782 net::HttpServerResponseInfo response(status_code); | |
783 response.SetBody(json_value + message, "application/json; charset=UTF-8"); | |
784 | |
785 thread_->task_runner()->PostTask( | |
786 FROM_HERE, | |
787 base::Bind(&ServerWrapper::SendResponse, | |
788 base::Unretained(server_wrapper_), connection_id, response)); | |
789 } | |
790 | |
791 void DevToolsHttpHandler::Send200(int connection_id, | |
792 const std::string& data, | |
793 const std::string& mime_type) { | |
794 if (!thread_) | |
795 return; | |
796 thread_->task_runner()->PostTask( | |
797 FROM_HERE, | |
798 base::Bind(&ServerWrapper::Send200, base::Unretained(server_wrapper_), | |
799 connection_id, data, mime_type)); | |
800 } | |
801 | |
802 void DevToolsHttpHandler::Send404(int connection_id) { | |
803 if (!thread_) | |
804 return; | |
805 thread_->task_runner()->PostTask( | |
806 FROM_HERE, base::Bind(&ServerWrapper::Send404, | |
807 base::Unretained(server_wrapper_), connection_id)); | |
808 } | |
809 | |
810 void DevToolsHttpHandler::Send500(int connection_id, | |
811 const std::string& message) { | |
812 if (!thread_) | |
813 return; | |
814 thread_->task_runner()->PostTask( | |
815 FROM_HERE, | |
816 base::Bind(&ServerWrapper::Send500, base::Unretained(server_wrapper_), | |
817 connection_id, message)); | |
818 } | |
819 | |
820 void DevToolsHttpHandler::AcceptWebSocket( | |
821 int connection_id, | |
822 const net::HttpServerRequestInfo& request) { | |
823 if (!thread_) | |
824 return; | |
825 thread_->task_runner()->PostTask( | |
826 FROM_HERE, | |
827 base::Bind(&ServerWrapper::AcceptWebSocket, | |
828 base::Unretained(server_wrapper_), connection_id, request)); | |
829 } | |
830 | |
831 std::unique_ptr<base::DictionaryValue> DevToolsHttpHandler::SerializeDescriptor( | |
832 scoped_refptr<DevToolsAgentHost> agent_host, | |
833 const std::string& host) { | |
834 std::unique_ptr<base::DictionaryValue> dictionary(new base::DictionaryValue); | |
835 std::string id = agent_host->GetId(); | |
836 dictionary->SetString(kTargetIdField, id); | |
837 std::string parent_id = agent_host->GetParentId(); | |
838 if (!parent_id.empty()) | |
839 dictionary->SetString(kTargetParentIdField, parent_id); | |
840 dictionary->SetString(kTargetTypeField, agent_host->GetType()); | |
841 dictionary->SetString(kTargetTitleField, | |
842 net::EscapeForHTML(agent_host->GetTitle())); | |
843 dictionary->SetString(kTargetDescriptionField, agent_host->GetDescription()); | |
844 | |
845 GURL url = agent_host->GetURL(); | |
846 dictionary->SetString(kTargetUrlField, url.spec()); | |
847 | |
848 GURL favicon_url = agent_host->GetFaviconURL(); | |
849 if (favicon_url.is_valid()) | |
850 dictionary->SetString(kTargetFaviconUrlField, favicon_url.spec()); | |
851 | |
852 if (!agent_host->IsAttached()) { | |
853 dictionary->SetString(kTargetWebSocketDebuggerUrlField, | |
854 base::StringPrintf("ws://%s%s%s", | |
855 host.c_str(), | |
856 kPageUrlPrefix, | |
857 id.c_str())); | |
858 std::string devtools_frontend_url = GetFrontendURLInternal( | |
859 id.c_str(), | |
860 host); | |
861 dictionary->SetString( | |
862 kTargetDevtoolsFrontendUrlField, devtools_frontend_url); | |
863 } | |
864 | |
865 return dictionary; | |
866 } | |
867 | |
868 } // namespace devtools_http_handler | |
OLD | NEW |