electron/brightray/browser/devtools_ui.cc

125 lines
3.8 KiB
C++
Raw Normal View History

// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE-CHROMIUM file.
#include "browser/devtools_ui.h"
#include <string>
#include "base/memory/ref_counted_memory.h"
#include "base/strings/string_util.h"
#include "base/strings/stringprintf.h"
#include "content/public/browser/devtools_frontend_host.h"
#include "content/public/browser/url_data_source.h"
#include "content/public/browser/web_contents.h"
#include "content/public/browser/web_ui.h"
using content::WebContents;
namespace brightray {
namespace {
2015-09-02 10:25:59 +00:00
const char kChromeUIDevToolsHost[] = "devtools";
const char kChromeUIDevToolsBundledPath[] = "bundled";
std::string PathWithoutParams(const std::string& path) {
return GURL(std::string("chrome-devtools://devtools/") + path)
.path().substr(1);
}
std::string GetMimeTypeForPath(const std::string& path) {
std::string filename = PathWithoutParams(path);
2015-09-02 07:16:34 +00:00
if (base::EndsWith(filename, ".html", false)) {
return "text/html";
2015-09-02 07:16:34 +00:00
} else if (base::EndsWith(filename, ".css", false)) {
return "text/css";
2015-09-02 07:16:34 +00:00
} else if (base::EndsWith(filename, ".js", false)) {
return "application/javascript";
2015-09-02 07:16:34 +00:00
} else if (base::EndsWith(filename, ".png", false)) {
return "image/png";
2015-09-02 07:16:34 +00:00
} else if (base::EndsWith(filename, ".gif", false)) {
return "image/gif";
2015-09-02 07:16:34 +00:00
} else if (base::EndsWith(filename, ".svg", false)) {
return "image/svg+xml";
2015-09-02 07:16:34 +00:00
} else if (base::EndsWith(filename, ".manifest", false)) {
return "text/cache-manifest";
}
return "text/html";
}
class BundledDataSource : public content::URLDataSource {
public:
2015-06-05 04:07:27 +00:00
BundledDataSource() {}
// content::URLDataSource implementation.
2015-06-05 04:07:27 +00:00
std::string GetSource() const override {
2015-09-02 10:25:59 +00:00
return kChromeUIDevToolsHost;
}
2015-06-05 04:07:27 +00:00
void StartDataRequest(const std::string& path,
int render_process_id,
2015-09-02 10:25:59 +00:00
int render_frame_id,
2015-06-05 04:07:27 +00:00
const GotDataCallback& callback) override {
2015-09-02 10:25:59 +00:00
// Serve request from local bundle.
std::string bundled_path_prefix(kChromeUIDevToolsBundledPath);
bundled_path_prefix += "/";
if (base::StartsWith(path, bundled_path_prefix,
base::CompareCase::INSENSITIVE_ASCII)) {
StartBundledDataRequest(path.substr(bundled_path_prefix.length()),
render_process_id, render_frame_id, callback);
return;
}
callback.Run(nullptr);
}
2015-06-05 04:07:27 +00:00
std::string GetMimeType(const std::string& path) const override {
return GetMimeTypeForPath(path);
}
2015-06-05 04:07:27 +00:00
bool ShouldAddContentSecurityPolicy() const override {
return false;
}
2015-06-05 04:07:27 +00:00
bool ShouldDenyXFrameOptions() const override {
return false;
}
2015-06-05 04:07:27 +00:00
bool ShouldServeMimeTypeAsContentTypeHeader() const override {
return true;
}
2015-09-02 10:25:59 +00:00
void StartBundledDataRequest(
const std::string& path,
int render_process_id,
int render_frame_id,
const content::URLDataSource::GotDataCallback& callback) {
std::string filename = PathWithoutParams(path);
base::StringPiece resource =
content::DevToolsFrontendHost::GetFrontendResource(filename);
DLOG_IF(WARNING, resource.empty())
<< "Unable to find dev tool resource: " << filename
<< ". If you compiled with debug_devtools=1, try running with "
"--debug-devtools.";
scoped_refptr<base::RefCountedStaticMemory> bytes(
new base::RefCountedStaticMemory(resource.data(), resource.length()));
callback.Run(bytes.get());
}
private:
virtual ~BundledDataSource() {}
DISALLOW_COPY_AND_ASSIGN(BundledDataSource);
};
2013-11-17 22:53:38 +00:00
} // namespace
DevToolsUI::DevToolsUI(content::BrowserContext* browser_context,
content::WebUI* web_ui)
2013-11-17 22:53:38 +00:00
: WebUIController(web_ui) {
web_ui->SetBindings(0);
content::URLDataSource::Add(browser_context, new BundledDataSource());
}
2013-11-17 22:53:38 +00:00
} // namespace brightray