devtools_ui.cc 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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()
  20. .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() {}
  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. ~BundledDataSource() override {}
  89. DISALLOW_COPY_AND_ASSIGN(BundledDataSource);
  90. };
  91. } // namespace
  92. DevToolsUI::DevToolsUI(content::BrowserContext* browser_context,
  93. content::WebUI* web_ui)
  94. : WebUIController(web_ui) {
  95. web_ui->SetBindings(0);
  96. content::URLDataSource::Add(browser_context, new BundledDataSource());
  97. }
  98. } // namespace brightray