| 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_browser_target.h" | 20 #include "content/browser/devtools/devtools_browser_target.h" |
| 21 #include "content/browser/devtools/devtools_manager.h" |
| 21 #include "content/browser/devtools/devtools_protocol.h" | 22 #include "content/browser/devtools/devtools_protocol.h" |
| 22 #include "content/browser/devtools/devtools_protocol_constants.h" | 23 #include "content/browser/devtools/devtools_protocol_constants.h" |
| 23 #include "content/browser/devtools/devtools_system_info_handler.h" | 24 #include "content/browser/devtools/devtools_system_info_handler.h" |
| 24 #include "content/browser/devtools/devtools_tracing_handler.h" | 25 #include "content/browser/devtools/devtools_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" |
| (...skipping 484 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 515 version.SetString("Android-Package", | 516 version.SetString("Android-Package", |
| 516 base::android::BuildInfo::GetInstance()->package_name()); | 517 base::android::BuildInfo::GetInstance()->package_name()); |
| 517 #endif | 518 #endif |
| 518 SendJson(connection_id, net::HTTP_OK, &version, std::string()); | 519 SendJson(connection_id, net::HTTP_OK, &version, std::string()); |
| 519 return; | 520 return; |
| 520 } | 521 } |
| 521 | 522 |
| 522 if (command == "list") { | 523 if (command == "list") { |
| 523 std::string host = info.headers["host"]; | 524 std::string host = info.headers["host"]; |
| 524 AddRef(); // Balanced in OnTargetListReceived. | 525 AddRef(); // Balanced in OnTargetListReceived. |
| 525 delegate_->EnumerateTargets( | 526 DevToolsManagerDelegate* manager_delegate = |
| 526 base::Bind(&DevToolsHttpHandlerImpl::OnTargetListReceived, | 527 DevToolsManager::GetInstance()->delegate(); |
| 527 this, connection_id, host)); | 528 if (manager_delegate) { |
| 529 manager_delegate->EnumerateTargets( |
| 530 base::Bind(&DevToolsHttpHandlerImpl::OnTargetListReceived, |
| 531 this, connection_id, host)); |
| 532 } else { |
| 533 DevToolsManagerDelegate::TargetList empty_list; |
| 534 OnTargetListReceived(connection_id, host, empty_list); |
| 535 } |
| 528 return; | 536 return; |
| 529 } | 537 } |
| 530 | 538 |
| 531 if (command == "new") { | 539 if (command == "new") { |
| 532 GURL url(net::UnescapeURLComponent( | 540 GURL url(net::UnescapeURLComponent( |
| 533 query, net::UnescapeRule::URL_SPECIAL_CHARS)); | 541 query, net::UnescapeRule::URL_SPECIAL_CHARS)); |
| 534 if (!url.is_valid()) | 542 if (!url.is_valid()) |
| 535 url = GURL(url::kAboutBlankURL); | 543 url = GURL(url::kAboutBlankURL); |
| 536 scoped_ptr<DevToolsTarget> target(delegate_->CreateNewTarget(url)); | 544 scoped_ptr<DevToolsTarget> target; |
| 545 DevToolsManagerDelegate* manager_delegate = |
| 546 DevToolsManager::GetInstance()->delegate(); |
| 547 if (manager_delegate) |
| 548 target = manager_delegate->CreateNewTarget(url); |
| 537 if (!target) { | 549 if (!target) { |
| 538 SendJson(connection_id, | 550 SendJson(connection_id, |
| 539 net::HTTP_INTERNAL_SERVER_ERROR, | 551 net::HTTP_INTERNAL_SERVER_ERROR, |
| 540 NULL, | 552 NULL, |
| 541 "Could not create new page"); | 553 "Could not create new page"); |
| 542 return; | 554 return; |
| 543 } | 555 } |
| 544 std::string host = info.headers["host"]; | 556 std::string host = info.headers["host"]; |
| 545 scoped_ptr<base::DictionaryValue> dictionary( | 557 scoped_ptr<base::DictionaryValue> dictionary( |
| 546 SerializeTarget(*target.get(), host)); | 558 SerializeTarget(*target.get(), host)); |
| (...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 587 SendJson(connection_id, | 599 SendJson(connection_id, |
| 588 net::HTTP_NOT_FOUND, | 600 net::HTTP_NOT_FOUND, |
| 589 NULL, | 601 NULL, |
| 590 "Unknown command: " + command); | 602 "Unknown command: " + command); |
| 591 return; | 603 return; |
| 592 } | 604 } |
| 593 | 605 |
| 594 void DevToolsHttpHandlerImpl::OnTargetListReceived( | 606 void DevToolsHttpHandlerImpl::OnTargetListReceived( |
| 595 int connection_id, | 607 int connection_id, |
| 596 const std::string& host, | 608 const std::string& host, |
| 597 const DevToolsHttpHandlerDelegate::TargetList& targets) { | 609 const DevToolsManagerDelegate::TargetList& targets) { |
| 598 DevToolsHttpHandlerDelegate::TargetList sorted_targets = targets; | 610 DevToolsManagerDelegate::TargetList sorted_targets = targets; |
| 599 std::sort(sorted_targets.begin(), sorted_targets.end(), TimeComparator); | 611 std::sort(sorted_targets.begin(), sorted_targets.end(), TimeComparator); |
| 600 | 612 |
| 601 STLDeleteValues(&target_map_); | 613 STLDeleteValues(&target_map_); |
| 602 base::ListValue list_value; | 614 base::ListValue list_value; |
| 603 for (DevToolsHttpHandlerDelegate::TargetList::const_iterator it = | 615 for (DevToolsManagerDelegate::TargetList::const_iterator it = |
| 604 sorted_targets.begin(); it != sorted_targets.end(); ++it) { | 616 sorted_targets.begin(); it != sorted_targets.end(); ++it) { |
| 605 DevToolsTarget* target = *it; | 617 DevToolsTarget* target = *it; |
| 606 target_map_[target->GetId()] = target; | 618 target_map_[target->GetId()] = target; |
| 607 list_value.Append(SerializeTarget(*target, host)); | 619 list_value.Append(SerializeTarget(*target, host)); |
| 608 } | 620 } |
| 609 SendJson(connection_id, net::HTTP_OK, &list_value, std::string()); | 621 SendJson(connection_id, net::HTTP_OK, &list_value, std::string()); |
| 610 Release(); // Balanced in OnJsonRequestUI. | 622 Release(); // Balanced in OnJsonRequestUI. |
| 611 } | 623 } |
| 612 | 624 |
| 613 DevToolsTarget* DevToolsHttpHandlerImpl::GetTarget(const std::string& id) { | 625 DevToolsTarget* DevToolsHttpHandlerImpl::GetTarget(const std::string& id) { |
| 614 TargetMap::const_iterator it = target_map_.find(id); | 626 TargetMap::const_iterator it = target_map_.find(id); |
| 615 if (it == target_map_.end()) | 627 if (it == target_map_.end()) |
| 616 return NULL; | 628 return NULL; |
| 617 return it->second; | 629 return it->second; |
| 618 } | 630 } |
| 619 | 631 |
| 620 void DevToolsHttpHandlerImpl::OnThumbnailRequestUI( | 632 void DevToolsHttpHandlerImpl::OnThumbnailRequestUI( |
| 621 int connection_id, const GURL& page_url) { | 633 int connection_id, const GURL& page_url) { |
| 622 std::string data = delegate_->GetPageThumbnailData(page_url); | 634 DevToolsManagerDelegate* manager_delegate = |
| 635 DevToolsManager::GetInstance()->delegate(); |
| 636 std::string data = |
| 637 manager_delegate ? manager_delegate->GetPageThumbnailData(page_url) : ""; |
| 623 if (!data.empty()) | 638 if (!data.empty()) |
| 624 Send200(connection_id, data, "image/png"); | 639 Send200(connection_id, data, "image/png"); |
| 625 else | 640 else |
| 626 Send404(connection_id); | 641 Send404(connection_id); |
| 627 } | 642 } |
| 628 | 643 |
| 629 void DevToolsHttpHandlerImpl::OnDiscoveryPageRequestUI(int connection_id) { | 644 void DevToolsHttpHandlerImpl::OnDiscoveryPageRequestUI(int connection_id) { |
| 630 std::string response = delegate_->GetDiscoveryPageHTML(); | 645 std::string response = delegate_->GetDiscoveryPageHTML(); |
| 631 Send200(connection_id, response, "text/html; charset=UTF-8"); | 646 Send200(connection_id, response, "text/html; charset=UTF-8"); |
| 632 } | 647 } |
| (...skipping 224 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 857 net::EscapeForHTML(target.GetTitle())); | 872 net::EscapeForHTML(target.GetTitle())); |
| 858 dictionary->SetString(kTargetDescriptionField, target.GetDescription()); | 873 dictionary->SetString(kTargetDescriptionField, target.GetDescription()); |
| 859 | 874 |
| 860 GURL url = target.GetURL(); | 875 GURL url = target.GetURL(); |
| 861 dictionary->SetString(kTargetUrlField, url.spec()); | 876 dictionary->SetString(kTargetUrlField, url.spec()); |
| 862 | 877 |
| 863 GURL favicon_url = target.GetFaviconURL(); | 878 GURL favicon_url = target.GetFaviconURL(); |
| 864 if (favicon_url.is_valid()) | 879 if (favicon_url.is_valid()) |
| 865 dictionary->SetString(kTargetFaviconUrlField, favicon_url.spec()); | 880 dictionary->SetString(kTargetFaviconUrlField, favicon_url.spec()); |
| 866 | 881 |
| 867 if (!delegate_->GetPageThumbnailData(url).empty()) { | 882 DevToolsManagerDelegate* manager_delegate = |
| 883 DevToolsManager::GetInstance()->delegate(); |
| 884 if (manager_delegate && |
| 885 !manager_delegate->GetPageThumbnailData(url).empty()) { |
| 868 dictionary->SetString(kTargetThumbnailUrlField, | 886 dictionary->SetString(kTargetThumbnailUrlField, |
| 869 std::string(kThumbUrlPrefix) + id); | 887 std::string(kThumbUrlPrefix) + id); |
| 870 } | 888 } |
| 871 | 889 |
| 872 if (!target.IsAttached()) { | 890 if (!target.IsAttached()) { |
| 873 dictionary->SetString(kTargetWebSocketDebuggerUrlField, | 891 dictionary->SetString(kTargetWebSocketDebuggerUrlField, |
| 874 base::StringPrintf("ws://%s%s%s", | 892 base::StringPrintf("ws://%s%s%s", |
| 875 host.c_str(), | 893 host.c_str(), |
| 876 kPageUrlPrefix, | 894 kPageUrlPrefix, |
| 877 id.c_str())); | 895 id.c_str())); |
| 878 std::string devtools_frontend_url = GetFrontendURLInternal( | 896 std::string devtools_frontend_url = GetFrontendURLInternal( |
| 879 id.c_str(), | 897 id.c_str(), |
| 880 host); | 898 host); |
| 881 dictionary->SetString( | 899 dictionary->SetString( |
| 882 kTargetDevtoolsFrontendUrlField, devtools_frontend_url); | 900 kTargetDevtoolsFrontendUrlField, devtools_frontend_url); |
| 883 } | 901 } |
| 884 | 902 |
| 885 return dictionary; | 903 return dictionary; |
| 886 } | 904 } |
| 887 | 905 |
| 888 } // namespace content | 906 } // namespace content |
| OLD | NEW |