devtools_ui.cc 4.1 KB

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