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 "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 GetMimeTypeForPath(const std::string& path) {
  28. std::string filename = PathWithoutParams(path);
  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. // content::URLDataSource implementation.
  65. std::string GetSource() override { return chrome::kChromeUIDevToolsHost; }
  66. void StartDataRequest(const GURL& url,
  67. const content::WebContents::Getter& wc_getter,
  68. GotDataCallback callback) override {
  69. const std::string path = content::URLDataSource::URLToRequestPath(url);
  70. // Serve request from local bundle.
  71. std::string bundled_path_prefix(chrome::kChromeUIDevToolsBundledPath);
  72. bundled_path_prefix += "/";
  73. if (base::StartsWith(path, bundled_path_prefix,
  74. base::CompareCase::INSENSITIVE_ASCII)) {
  75. StartBundledDataRequest(path.substr(bundled_path_prefix.length()),
  76. std::move(callback));
  77. return;
  78. }
  79. // We do not handle remote and custom requests.
  80. std::move(callback).Run(nullptr);
  81. }
  82. std::string GetMimeType(const std::string& path) override {
  83. return GetMimeTypeForPath(path);
  84. }
  85. bool ShouldAddContentSecurityPolicy() override { return false; }
  86. bool ShouldDenyXFrameOptions() override { return false; }
  87. bool ShouldServeMimeTypeAsContentTypeHeader() override { return true; }
  88. void StartBundledDataRequest(const std::string& path,
  89. GotDataCallback callback) {
  90. std::string filename = PathWithoutParams(path);
  91. scoped_refptr<base::RefCountedMemory> bytes =
  92. content::DevToolsFrontendHost::GetFrontendResourceBytes(filename);
  93. DLOG_IF(WARNING, !bytes)
  94. << "Unable to find dev tool resource: " << filename
  95. << ". If you compiled with debug_devtools=1, try running with "
  96. "--debug-devtools.";
  97. std::move(callback).Run(bytes);
  98. }
  99. private:
  100. DISALLOW_COPY_AND_ASSIGN(BundledDataSource);
  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