devtools_ui.cc 4.7 KB

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