devtools_ui.cc 4.1 KB

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