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 <stddef.h> | 5 #include <stddef.h> |
6 #include <stdint.h> | 6 #include <stdint.h> |
7 | 7 |
8 #include <algorithm> | 8 #include <algorithm> |
9 #include <utility> | 9 #include <utility> |
10 | 10 |
11 #include "base/bind.h" | 11 #include "base/bind.h" |
12 #include "base/compiler_specific.h" | 12 #include "base/compiler_specific.h" |
13 #include "base/files/file_util.h" | 13 #include "base/files/file_util.h" |
14 #include "base/json/json_writer.h" | 14 #include "base/json/json_writer.h" |
15 #include "base/location.h" | 15 #include "base/location.h" |
16 #include "base/logging.h" | 16 #include "base/logging.h" |
| 17 #include "base/memory/ref_counted_memory.h" |
17 #include "base/single_thread_task_runner.h" | 18 #include "base/single_thread_task_runner.h" |
18 #include "base/strings/string_number_conversions.h" | 19 #include "base/strings/string_number_conversions.h" |
19 #include "base/strings/string_util.h" | 20 #include "base/strings/string_util.h" |
20 #include "base/strings/stringprintf.h" | 21 #include "base/strings/stringprintf.h" |
21 #include "base/threading/thread.h" | 22 #include "base/threading/thread.h" |
22 #include "base/values.h" | 23 #include "base/values.h" |
23 #include "build/build_config.h" | 24 #include "build/build_config.h" |
24 #include "content/browser/devtools/devtools_http_handler.h" | 25 #include "content/browser/devtools/devtools_http_handler.h" |
25 #include "content/browser/devtools/devtools_manager.h" | 26 #include "content/browser/devtools/devtools_manager.h" |
| 27 #include "content/browser/devtools/grit/devtools_resources.h" |
26 #include "content/public/browser/browser_thread.h" | 28 #include "content/public/browser/browser_thread.h" |
27 #include "content/public/browser/devtools_external_agent_proxy_delegate.h" | 29 #include "content/public/browser/devtools_external_agent_proxy_delegate.h" |
28 #include "content/public/browser/devtools_manager_delegate.h" | 30 #include "content/public/browser/devtools_manager_delegate.h" |
29 #include "content/public/browser/devtools_socket_factory.h" | 31 #include "content/public/browser/devtools_socket_factory.h" |
| 32 #include "content/public/common/content_client.h" |
30 #include "content/public/common/url_constants.h" | 33 #include "content/public/common/url_constants.h" |
31 #include "content/public/common/user_agent.h" | 34 #include "content/public/common/user_agent.h" |
32 #include "net/base/escape.h" | 35 #include "net/base/escape.h" |
33 #include "net/base/io_buffer.h" | 36 #include "net/base/io_buffer.h" |
34 #include "net/base/ip_endpoint.h" | 37 #include "net/base/ip_endpoint.h" |
35 #include "net/base/net_errors.h" | 38 #include "net/base/net_errors.h" |
36 #include "net/server/http_server.h" | 39 #include "net/server/http_server.h" |
37 #include "net/server/http_server_request_info.h" | 40 #include "net/server/http_server_request_info.h" |
38 #include "net/server/http_server_response_info.h" | 41 #include "net/server/http_server_response_info.h" |
39 #include "net/socket/server_socket.h" | 42 #include "net/socket/server_socket.h" |
| 43 #include "third_party/brotli/include/brotli/decode.h" |
40 #include "v8/include/v8-version-string.h" | 44 #include "v8/include/v8-version-string.h" |
41 | 45 |
42 #if defined(OS_ANDROID) | 46 #if defined(OS_ANDROID) |
43 #include "base/android/build_info.h" | 47 #include "base/android/build_info.h" |
44 #endif | 48 #endif |
45 | 49 |
46 namespace content { | 50 namespace content { |
47 | 51 |
48 namespace { | 52 namespace { |
49 | 53 |
(...skipping 477 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
527 version.SetString("User-Agent", user_agent_); | 531 version.SetString("User-Agent", user_agent_); |
528 version.SetString("V8-Version", V8_VERSION_STRING); | 532 version.SetString("V8-Version", V8_VERSION_STRING); |
529 #if defined(OS_ANDROID) | 533 #if defined(OS_ANDROID) |
530 version.SetString("Android-Package", | 534 version.SetString("Android-Package", |
531 base::android::BuildInfo::GetInstance()->package_name()); | 535 base::android::BuildInfo::GetInstance()->package_name()); |
532 #endif | 536 #endif |
533 SendJson(connection_id, net::HTTP_OK, &version, std::string()); | 537 SendJson(connection_id, net::HTTP_OK, &version, std::string()); |
534 return; | 538 return; |
535 } | 539 } |
536 | 540 |
| 541 if (command == "protocol") { |
| 542 DecompressAndSendJsonProtocol(connection_id); |
| 543 return; |
| 544 } |
| 545 |
537 if (command == "list") { | 546 if (command == "list") { |
538 DevToolsManager* manager = DevToolsManager::GetInstance(); | 547 DevToolsManager* manager = DevToolsManager::GetInstance(); |
539 DevToolsAgentHost::List list = | 548 DevToolsAgentHost::List list = |
540 manager->delegate() ? manager->delegate()->RemoteDebuggingTargets() | 549 manager->delegate() ? manager->delegate()->RemoteDebuggingTargets() |
541 : DevToolsAgentHost::GetOrCreateAll(); | 550 : DevToolsAgentHost::GetOrCreateAll(); |
542 RespondToJsonList(connection_id, info.headers["host"], std::move(list)); | 551 RespondToJsonList(connection_id, info.headers["host"], std::move(list)); |
543 return; | 552 return; |
544 } | 553 } |
545 | 554 |
546 if (command == "new") { | 555 if (command == "new") { |
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
600 return; | 609 return; |
601 } | 610 } |
602 } | 611 } |
603 SendJson(connection_id, | 612 SendJson(connection_id, |
604 net::HTTP_NOT_FOUND, | 613 net::HTTP_NOT_FOUND, |
605 NULL, | 614 NULL, |
606 "Unknown command: " + command); | 615 "Unknown command: " + command); |
607 return; | 616 return; |
608 } | 617 } |
609 | 618 |
| 619 void DevToolsHttpHandler::DecompressAndSendJsonProtocol(int connection_id) { |
| 620 scoped_refptr<base::RefCountedMemory> raw_bytes = |
| 621 GetContentClient()->GetDataResourceBytes(COMPRESSED_PROTOCOL_JSON); |
| 622 const uint8_t* next_encoded_byte = raw_bytes->front(); |
| 623 size_t input_size_remaining = raw_bytes->size(); |
| 624 BrotliDecoderState* decoder = BrotliDecoderCreateInstance( |
| 625 nullptr /* no custom allocator */, nullptr /* no custom deallocator */, |
| 626 nullptr /* no custom memory handle */); |
| 627 CHECK(!!decoder); |
| 628 std::vector<std::string> decoded_parts; |
| 629 size_t decompressed_size = 0; |
| 630 while (!BrotliDecoderIsFinished(decoder)) { |
| 631 size_t output_size_remaining = 0; |
| 632 CHECK(BrotliDecoderDecompressStream( |
| 633 decoder, &input_size_remaining, &next_encoded_byte, |
| 634 &output_size_remaining, nullptr, |
| 635 nullptr) != BROTLI_DECODER_RESULT_ERROR); |
| 636 const uint8_t* output_buffer = |
| 637 BrotliDecoderTakeOutput(decoder, &output_size_remaining); |
| 638 decoded_parts.emplace_back(reinterpret_cast<const char*>(output_buffer), |
| 639 output_size_remaining); |
| 640 decompressed_size += output_size_remaining; |
| 641 } |
| 642 BrotliDecoderDestroyInstance(decoder); |
| 643 |
| 644 // Ideally we'd use a StringBuilder here but there isn't one in base/. |
| 645 std::string json_protocol; |
| 646 json_protocol.reserve(decompressed_size); |
| 647 for (const std::string& part : decoded_parts) { |
| 648 json_protocol.append(part); |
| 649 } |
| 650 |
| 651 net::HttpServerResponseInfo response(net::HTTP_OK); |
| 652 response.SetBody(json_protocol, "application/json; charset=UTF-8"); |
| 653 |
| 654 thread_->task_runner()->PostTask( |
| 655 FROM_HERE, |
| 656 base::Bind(&ServerWrapper::SendResponse, |
| 657 base::Unretained(server_wrapper_), connection_id, response)); |
| 658 } |
| 659 |
610 void DevToolsHttpHandler::RespondToJsonList( | 660 void DevToolsHttpHandler::RespondToJsonList( |
611 int connection_id, | 661 int connection_id, |
612 const std::string& host, | 662 const std::string& host, |
613 DevToolsAgentHost::List hosts) { | 663 DevToolsAgentHost::List hosts) { |
614 DevToolsAgentHost::List agent_hosts = std::move(hosts); | 664 DevToolsAgentHost::List agent_hosts = std::move(hosts); |
615 std::sort(agent_hosts.begin(), agent_hosts.end(), TimeComparator); | 665 std::sort(agent_hosts.begin(), agent_hosts.end(), TimeComparator); |
616 base::ListValue list_value; | 666 base::ListValue list_value; |
617 for (auto& agent_host : agent_hosts) | 667 for (auto& agent_host : agent_hosts) |
618 list_value.Append(SerializeDescriptor(agent_host, host)); | 668 list_value.Append(SerializeDescriptor(agent_host, host)); |
619 SendJson(connection_id, net::HTTP_OK, &list_value, std::string()); | 669 SendJson(connection_id, net::HTTP_OK, &list_value, std::string()); |
(...skipping 226 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
846 id.c_str())); | 896 id.c_str())); |
847 std::string devtools_frontend_url = GetFrontendURLInternal(id, host); | 897 std::string devtools_frontend_url = GetFrontendURLInternal(id, host); |
848 dictionary->SetString( | 898 dictionary->SetString( |
849 kTargetDevtoolsFrontendUrlField, devtools_frontend_url); | 899 kTargetDevtoolsFrontendUrlField, devtools_frontend_url); |
850 } | 900 } |
851 | 901 |
852 return dictionary; | 902 return dictionary; |
853 } | 903 } |
854 | 904 |
855 } // namespace content | 905 } // namespace content |
OLD | NEW |