devtools_ui.cc 4.2 KB

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