devtools_ui.cc 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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 "brightray/browser/devtools_ui.h"
  5. #include <string>
  6. #include "base/memory/ref_counted_memory.h"
  7. #include "base/strings/string_util.h"
  8. #include "base/strings/stringprintf.h"
  9. #include "content/public/browser/devtools_frontend_host.h"
  10. #include "content/public/browser/url_data_source.h"
  11. #include "content/public/browser/web_contents.h"
  12. #include "content/public/browser/web_ui.h"
  13. namespace brightray {
  14. namespace {
  15. const char kChromeUIDevToolsHost[] = "devtools";
  16. const char kChromeUIDevToolsBundledPath[] = "bundled";
  17. std::string PathWithoutParams(const std::string& path) {
  18. return GURL(std::string("chrome-devtools://devtools/") + path)
  19. .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. // content::URLDataSource implementation.
  50. std::string GetSource() const override {
  51. return kChromeUIDevToolsHost;
  52. }
  53. void StartDataRequest(
  54. const std::string& path,
  55. const content::ResourceRequestInfo::WebContentsGetter& wc_getter,
  56. const GotDataCallback& callback) override {
  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. callback);
  64. return;
  65. }
  66. // We do not handle remote and custom requests.
  67. callback.Run(nullptr);
  68. }
  69. std::string GetMimeType(const std::string& path) const override {
  70. return GetMimeTypeForPath(path);
  71. }
  72. bool ShouldAddContentSecurityPolicy() const override {
  73. return false;
  74. }
  75. bool ShouldDenyXFrameOptions() const override {
  76. return false;
  77. }
  78. bool ShouldServeMimeTypeAsContentTypeHeader() const override {
  79. return true;
  80. }
  81. void StartBundledDataRequest(const std::string& path,
  82. const GotDataCallback& callback) {
  83. std::string filename = PathWithoutParams(path);
  84. base::StringPiece resource =
  85. content::DevToolsFrontendHost::GetFrontendResource(filename);
  86. DLOG_IF(WARNING, resource.empty())
  87. << "Unable to find dev tool resource: " << filename
  88. << ". If you compiled with debug_devtools=1, try running with "
  89. "--debug-devtools.";
  90. scoped_refptr<base::RefCountedStaticMemory> bytes(
  91. new base::RefCountedStaticMemory(resource.data(), resource.length()));
  92. callback.Run(bytes.get());
  93. }
  94. private:
  95. ~BundledDataSource() override {}
  96. DISALLOW_COPY_AND_ASSIGN(BundledDataSource);
  97. };
  98. } // namespace
  99. DevToolsUI::DevToolsUI(content::BrowserContext* browser_context,
  100. content::WebUI* web_ui)
  101. : WebUIController(web_ui) {
  102. web_ui->SetBindings(0);
  103. content::URLDataSource::Add(browser_context, new BundledDataSource());
  104. }
  105. } // namespace brightray