devtools_ui.cc 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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-CHROMIUM file.
  4. #include "shell/browser/ui/devtools_ui.h"
  5. #include <memory>
  6. #include <string>
  7. #include <utility>
  8. #include "base/memory/ref_counted_memory.h"
  9. #include "base/strings/strcat.h"
  10. #include "base/strings/string_util.h"
  11. #include "chrome/common/webui_url_constants.h"
  12. #include "content/public/browser/devtools_frontend_host.h"
  13. #include "content/public/browser/url_data_source.h"
  14. #include "content/public/browser/web_contents.h"
  15. #include "content/public/browser/web_ui.h"
  16. namespace electron {
  17. namespace {
  18. std::string PathWithoutParams(const std::string& path) {
  19. return GURL(base::StrCat({content::kChromeDevToolsScheme,
  20. url::kStandardSchemeSeparator,
  21. chrome::kChromeUIDevToolsHost}))
  22. .Resolve(path)
  23. .path()
  24. .substr(1);
  25. }
  26. std::string GetMimeTypeForUrl(const GURL& url) {
  27. std::string filename = url.ExtractFileName();
  28. if (base::EndsWith(filename, ".html", base::CompareCase::INSENSITIVE_ASCII)) {
  29. return "text/html";
  30. } else if (base::EndsWith(filename, ".css",
  31. base::CompareCase::INSENSITIVE_ASCII)) {
  32. return "text/css";
  33. } else if (base::EndsWith(filename, ".js",
  34. base::CompareCase::INSENSITIVE_ASCII) ||
  35. base::EndsWith(filename, ".mjs",
  36. base::CompareCase::INSENSITIVE_ASCII)) {
  37. return "application/javascript";
  38. } else if (base::EndsWith(filename, ".png",
  39. base::CompareCase::INSENSITIVE_ASCII)) {
  40. return "image/png";
  41. } else if (base::EndsWith(filename, ".map",
  42. base::CompareCase::INSENSITIVE_ASCII)) {
  43. return "application/json";
  44. } else if (base::EndsWith(filename, ".ts",
  45. base::CompareCase::INSENSITIVE_ASCII)) {
  46. return "application/x-typescript";
  47. } else if (base::EndsWith(filename, ".gif",
  48. base::CompareCase::INSENSITIVE_ASCII)) {
  49. return "image/gif";
  50. } else if (base::EndsWith(filename, ".svg",
  51. base::CompareCase::INSENSITIVE_ASCII)) {
  52. return "image/svg+xml";
  53. } else if (base::EndsWith(filename, ".manifest",
  54. base::CompareCase::INSENSITIVE_ASCII)) {
  55. return "text/cache-manifest";
  56. }
  57. return "text/html";
  58. }
  59. class BundledDataSource : public content::URLDataSource {
  60. public:
  61. BundledDataSource() = default;
  62. ~BundledDataSource() override = default;
  63. // disable copy
  64. BundledDataSource(const BundledDataSource&) = delete;
  65. BundledDataSource& operator=(const BundledDataSource&) = delete;
  66. // content::URLDataSource implementation.
  67. std::string GetSource() override { return chrome::kChromeUIDevToolsHost; }
  68. void StartDataRequest(const GURL& url,
  69. const content::WebContents::Getter& wc_getter,
  70. GotDataCallback callback) override {
  71. const std::string path = content::URLDataSource::URLToRequestPath(url);
  72. // Serve request from local bundle.
  73. std::string bundled_path_prefix(chrome::kChromeUIDevToolsBundledPath);
  74. bundled_path_prefix += "/";
  75. if (base::StartsWith(path, bundled_path_prefix,
  76. base::CompareCase::INSENSITIVE_ASCII)) {
  77. StartBundledDataRequest(path.substr(bundled_path_prefix.length()),
  78. std::move(callback));
  79. return;
  80. }
  81. // We do not handle remote and custom requests.
  82. std::move(callback).Run(nullptr);
  83. }
  84. std::string GetMimeType(const GURL& url) override {
  85. return GetMimeTypeForUrl(url);
  86. }
  87. bool ShouldAddContentSecurityPolicy() override { return false; }
  88. bool ShouldDenyXFrameOptions() override { return false; }
  89. bool ShouldServeMimeTypeAsContentTypeHeader() override { return true; }
  90. void StartBundledDataRequest(const std::string& path,
  91. GotDataCallback callback) {
  92. std::string filename = PathWithoutParams(path);
  93. scoped_refptr<base::RefCountedMemory> bytes =
  94. content::DevToolsFrontendHost::GetFrontendResourceBytes(filename);
  95. DLOG_IF(WARNING, !bytes)
  96. << "Unable to find dev tool resource: " << filename
  97. << ". If you compiled with debug_devtools=1, try running with "
  98. "--debug-devtools.";
  99. std::move(callback).Run(bytes);
  100. }
  101. };
  102. } // namespace
  103. DevToolsUI::DevToolsUI(content::BrowserContext* browser_context,
  104. content::WebUI* web_ui)
  105. : WebUIController(web_ui) {
  106. web_ui->SetBindings(0);
  107. content::URLDataSource::Add(browser_context,
  108. std::make_unique<BundledDataSource>());
  109. }
  110. } // namespace electron