Commit 441f9a8d authored by Andrey Kosyakov's avatar Andrey Kosyakov Committed by Commit Bot

Fix debug_devtools builds with network service

This removes DebugDevToolsInterceptor and supports DEBUG_DEVTOOLS
layout as part of regular DevToolsDataSource.

Change-Id: I06d5a805ff8b6b324127b131bb87801d3595311f
Reviewed-on: https://chromium-review.googlesource.com/1229198Reviewed-by: default avatarMatt Menke <mmenke@chromium.org>
Reviewed-by: default avatarDmitry Gozman <dgozman@chromium.org>
Commit-Queue: Andrey Kosyakov <caseq@chromium.org>
Cr-Commit-Position: refs/heads/master@{#592091}
parent 5660a902
......@@ -18,7 +18,6 @@
#include "base/files/file_util.h"
#include "base/logging.h"
#include "base/macros.h"
#include "base/path_service.h"
#include "base/single_thread_task_runner.h"
#include "base/stl_util.h"
#include "base/strings/string_number_conversions.h"
......@@ -47,7 +46,6 @@
#include "chrome/common/buildflags.h"
#include "chrome/common/chrome_constants.h"
#include "chrome/common/chrome_features.h"
#include "chrome/common/chrome_paths.h"
#include "chrome/common/chrome_switches.h"
#include "chrome/common/pref_names.h"
#include "chrome/common/url_constants.h"
......@@ -210,78 +208,6 @@ class WrappedCertVerifierForProfileIODataTesting : public net::CertVerifier {
}
};
#if BUILDFLAG(DEBUG_DEVTOOLS)
bool IsSupportedDevToolsURL(const GURL& url, base::FilePath* path) {
std::string bundled_path_prefix(chrome::kChromeUIDevToolsBundledPath);
bundled_path_prefix = "/" + bundled_path_prefix + "/";
if (!url.SchemeIs(content::kChromeDevToolsScheme) ||
url.host_piece() != chrome::kChromeUIDevToolsHost ||
!base::StartsWith(url.path_piece(), bundled_path_prefix,
base::CompareCase::INSENSITIVE_ASCII)) {
return false;
}
if (!url.is_valid()) {
NOTREACHED();
return false;
}
// Remove Query and Ref from URL.
GURL stripped_url;
GURL::Replacements replacements;
replacements.ClearQuery();
replacements.ClearRef();
stripped_url = url.ReplaceComponents(replacements);
std::string relative_path;
const std::string& spec = stripped_url.possibly_invalid_spec();
const url::Parsed& parsed = stripped_url.parsed_for_possibly_invalid_spec();
int offset = parsed.CountCharactersBefore(url::Parsed::PATH, false);
if (offset < static_cast<int>(spec.size()))
relative_path.assign(spec.substr(offset + bundled_path_prefix.length()));
// Check that |relative_path| is not an absolute path (otherwise
// AppendASCII() will DCHECK). The awkward use of StringType is because on
// some systems FilePath expects a std::string, but on others a std::wstring.
base::FilePath p(
base::FilePath::StringType(relative_path.begin(), relative_path.end()));
if (p.IsAbsolute())
return false;
base::FilePath inspector_debug_dir;
if (!base::PathService::Get(chrome::DIR_INSPECTOR_DEBUG,
&inspector_debug_dir))
return false;
DCHECK(!inspector_debug_dir.empty());
*path = inspector_debug_dir.AppendASCII(relative_path);
return true;
}
class DebugDevToolsInterceptor : public net::URLRequestInterceptor {
public:
// net::URLRequestInterceptor implementation.
net::URLRequestJob* MaybeInterceptRequest(
net::URLRequest* request,
net::NetworkDelegate* network_delegate) const override {
base::FilePath path;
if (IsSupportedDevToolsURL(request->url(), &path)) {
net::URLRequestFileJob* job = new net::URLRequestFileJob(
request, network_delegate, path,
base::CreateTaskRunnerWithTraits(
{base::MayBlock(), base::TaskPriority::USER_VISIBLE,
base::TaskShutdownBehavior::SKIP_ON_SHUTDOWN}));
job->ShouldServeMimeTypeAsContentTypeHeader();
return job;
}
return NULL;
}
};
#endif // BUILDFLAG(DEBUG_DEVTOOLS)
#if defined(OS_CHROMEOS)
// The following four functions are responsible for initializing NSS for each
// profile on ChromeOS, which has a separate NSS database and TPM slot
......@@ -1257,10 +1183,6 @@ ProfileIOData::SetUpJobFactoryDefaults(
url::kFtpScheme, net::FtpProtocolHandler::Create(host_resolver));
#endif // !BUILDFLAG(DISABLE_FTP_SUPPORT)
#if BUILDFLAG(DEBUG_DEVTOOLS)
request_interceptors.push_back(std::make_unique<DebugDevToolsInterceptor>());
#endif
// Set up interceptors in the reverse order.
std::unique_ptr<net::URLRequestJobFactory> top_job_factory =
std::move(job_factory);
......@@ -1314,10 +1236,6 @@ void ProfileIOData::SetUpJobFactoryDefaultsForBuilder(
url::kAboutScheme,
std::make_unique<about_handler::AboutProtocolHandler>());
#if BUILDFLAG(DEBUG_DEVTOOLS)
request_interceptors.push_back(std::make_unique<DebugDevToolsInterceptor>());
#endif
builder->SetInterceptors(std::move(request_interceptors));
if (protocol_handler_interceptor) {
......
......@@ -10,9 +10,12 @@
#include "base/command_line.h"
#include "base/macros.h"
#include "base/memory/ref_counted_memory.h"
#include "base/path_service.h"
#include "base/strings/string_util.h"
#include "base/strings/stringprintf.h"
#include "base/task/post_task.h"
#include "chrome/browser/devtools/url_constants.h"
#include "chrome/common/chrome_paths.h"
#include "chrome/common/chrome_switches.h"
#include "chrome/common/url_constants.h"
#include "content/public/browser/browser_context.h"
......@@ -127,6 +130,11 @@ class DevToolsDataSource : public content::URLDataSource {
int load_flags,
const GotDataCallback& callback);
#if BUILDFLAG(DEBUG_DEVTOOLS)
void StartFileRequestForDebugDevtools(const std::string& path,
const GotDataCallback& callback);
#endif
struct PendingRequest {
PendingRequest() = default;
PendingRequest(PendingRequest&& other) = default;
......@@ -162,8 +170,17 @@ void DevToolsDataSource::StartDataRequest(
bundled_path_prefix += "/";
if (base::StartsWith(path, bundled_path_prefix,
base::CompareCase::INSENSITIVE_ASCII)) {
StartBundledDataRequest(path.substr(bundled_path_prefix.length()),
callback);
std::string path_without_params = PathWithoutParams(path);
DCHECK(base::StartsWith(path_without_params, bundled_path_prefix,
base::CompareCase::INSENSITIVE_ASCII));
std::string path_under_bundled =
path_without_params.substr(bundled_path_prefix.length());
#if BUILDFLAG(DEBUG_DEVTOOLS)
StartFileRequestForDebugDevtools(path_under_bundled, callback);
#else
StartBundledDataRequest(path_under_bundled, callback);
#endif
return;
}
......@@ -236,12 +253,11 @@ bool DevToolsDataSource::ShouldServeMimeTypeAsContentTypeHeader() const {
void DevToolsDataSource::StartBundledDataRequest(
const std::string& path,
const content::URLDataSource::GotDataCallback& callback) {
std::string filename = PathWithoutParams(path);
base::StringPiece resource =
content::DevToolsFrontendHost::GetFrontendResource(filename);
content::DevToolsFrontendHost::GetFrontendResource(path);
DLOG_IF(WARNING, resource.empty())
<< "Unable to find dev tool resource: " << filename
<< "Unable to find dev tool resource: " << path
<< ". If you compiled with debug_devtools=1, try running with "
"--debug-devtools.";
scoped_refptr<base::RefCountedStaticMemory> bytes(
......@@ -339,6 +355,42 @@ void DevToolsDataSource::StartNetworkRequest(
base::Unretained(this), request_iter));
}
#if BUILDFLAG(DEBUG_DEVTOOLS)
scoped_refptr<base::RefCountedMemory> ReadFile(const base::FilePath& path) {
std::string buffer;
if (!base::ReadFileToString(path, &buffer)) {
LOG(ERROR) << "Failed to read " << path;
return CreateNotFoundResponse();
}
return base::RefCountedString::TakeString(&buffer);
}
void DevToolsDataSource::StartFileRequestForDebugDevtools(
const std::string& path,
const GotDataCallback& callback) {
base::FilePath inspector_debug_dir;
if (!base::PathService::Get(chrome::DIR_INSPECTOR_DEBUG,
&inspector_debug_dir)) {
callback.Run(CreateNotFoundResponse());
return;
}
DCHECK(!inspector_debug_dir.empty());
base::FilePath full_path = inspector_debug_dir.AppendASCII(path);
base::PostTaskWithTraitsAndReplyWithResult(
FROM_HERE,
{base::MayBlock(), base::TaskShutdownBehavior::SKIP_ON_SHUTDOWN,
base::TaskPriority::USER_VISIBLE},
// The usage of BindRepeating below is only because the type of
// task callback needs to match that of response callback, which
// is currently a repeating callback.
base::BindRepeating(ReadFile, std::move(full_path)), callback);
}
#endif // BUILDFLAG(DEBUG_DEVTOOLS)
void DevToolsDataSource::OnLoadComplete(
std::list<PendingRequest>::iterator request_iter,
std::unique_ptr<std::string> response_body) {
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment