Commit 44db3ebd authored by Oliver Chang's avatar Oliver Chang Committed by Commit Bot

Add --file-url-path-alias.

--file-url-path-alias=/alias=/replacements will result in  all file://
URLs with root paths beginning with /alias to be replaced with
/replacement.

For IPC fuzzer builds, this is automatically populated with
/gen=BUILD_DIR/gen.

Bug: 839228
Change-Id: I935278d8b11369efc9b98281a5a8056cee8e579d
Reviewed-on: https://chromium-review.googlesource.com/1119727
Commit-Queue: Oliver Chang <ochang@chromium.org>
Reviewed-by: default avatarAlex Moshchuk <alexmos@chromium.org>
Reviewed-by: default avatarKen Rockot <rockot@chromium.org>
Cr-Commit-Position: refs/heads/master@{#572434}
parent 3bb6c29c
......@@ -27,6 +27,7 @@
#include "base/metrics/field_trial.h"
#include "base/metrics/histogram_macros.h"
#include "base/metrics/user_metrics.h"
#include "base/path_service.h"
#include "base/pending_task.h"
#include "base/power_monitor/power_monitor.h"
#include "base/power_monitor/power_monitor_device_source.h"
......@@ -233,6 +234,10 @@
#include "crypto/nss_util.h"
#endif
#if defined(ENABLE_IPC_FUZZER) && defined(OS_MACOSX)
#include "base/mac/foundation_util.h"
#endif
// One of the linux specific headers defines this as a macro.
#ifdef DestroyAll
#undef DestroyAll
......@@ -397,6 +402,40 @@ void InitDefaultJob() {
}
#endif // defined(OS_FUCHSIA)
#if defined(ENABLE_IPC_FUZZER)
bool GetBuildDirectory(base::FilePath* result) {
if (!base::PathService::Get(base::DIR_EXE, result))
return false;
#if defined(OS_MACOSX)
if (base::mac::AmIBundled()) {
// The bundled app executables (Chromium, TestShell, etc) live three
// levels down from the build directory, eg:
// Chromium.app/Contents/MacOS/Chromium
*result = result->DirName().DirName().DirName();
}
#endif
return true;
}
void SetFileUrlPathAliasForIpcFuzzer() {
if (base::CommandLine::ForCurrentProcess()->HasSwitch(
switches::kFileUrlPathAlias))
return;
base::FilePath build_directory;
if (!GetBuildDirectory(&build_directory)) {
LOG(ERROR) << "Failed to get build directory for /gen path alias.";
return;
}
const base::CommandLine::StringType alias_switch =
FILE_PATH_LITERAL("/gen=") + build_directory.AppendASCII("gen").value();
base::CommandLine::ForCurrentProcess()->AppendSwitchNative(
switches::kFileUrlPathAlias, alias_switch);
}
#endif
} // namespace
#if defined(USE_X11)
......@@ -1422,6 +1461,9 @@ int BrowserMainLoop::BrowserThreadsStarted() {
media::SetMediaDrmBridgeClient(GetContentClient()->GetMediaDrmBridgeClient());
#endif
#if defined(ENABLE_IPC_FUZZER)
SetFileUrlPathAliasForIpcFuzzer();
#endif
return result_code_;
}
......
......@@ -2781,6 +2781,7 @@ void RenderProcessHostImpl::PropagateBrowserCommandLineToRenderer(
switches::kEnableWebGLImageChromium,
switches::kEnableWebVR,
switches::kExplicitlyAllowedPorts,
switches::kFileUrlPathAlias,
switches::kFMPNetworkQuietTimeout,
switches::kForceColorProfile,
switches::kForceDeviceScaleFactor,
......
......@@ -476,6 +476,11 @@ const char kExplicitlyAllowedPorts[] = "explicitly-allowed-ports";
// shared memory segment as a string.
const char kFieldTrialHandle[] = "field-trial-handle";
// Define an alias root directory which is replaced with the replacement string
// in file URLs. The format is "/alias=/replacement", which would turn
// file:///alias/some/path.html into file:///replacement/some/path.html.
const char kFileUrlPathAlias[] = "file-url-path-alias";
// Always use the Skia GPU backend for drawing layer tiles. Only valid with GPU
// accelerated compositing + impl-side painting. Overrides the
// kEnableGpuRasterization flag.
......
......@@ -148,6 +148,7 @@ CONTENT_EXPORT extern const char kEnableWebVR[];
CONTENT_EXPORT extern const char kEnableZeroCopy[];
CONTENT_EXPORT extern const char kExplicitlyAllowedPorts[];
CONTENT_EXPORT extern const char kFieldTrialHandle[];
CONTENT_EXPORT extern const char kFileUrlPathAlias[];
CONTENT_EXPORT extern const char kForceDisplayList2dCanvas[];
CONTENT_EXPORT extern const char kForceGpuRasterization[];
CONTENT_EXPORT extern const char kDisableOopRasterization[];
......
......@@ -35,6 +35,8 @@
#include "base/stl_util.h"
#include "base/strings/string16.h"
#include "base/strings/string_piece.h"
#include "base/strings/string_split.h"
#include "base/strings/string_util.h"
#include "base/strings/utf_string_conversions.h"
#include "base/task_runner_util.h"
#include "base/task_scheduler/post_task.h"
......@@ -872,6 +874,41 @@ std::unique_ptr<DocumentState> BuildDocumentStateFromPending(
return document_state;
}
void ApplyFilePathAlias(blink::WebURLRequest* request) {
const base::CommandLine::StringType file_url_path_alias =
base::CommandLine::ForCurrentProcess()->GetSwitchValueNative(
switches::kFileUrlPathAlias);
if (file_url_path_alias.empty())
return;
const auto alias_mapping =
base::SplitString(file_url_path_alias, FILE_PATH_LITERAL("="),
base::TRIM_WHITESPACE, base::SPLIT_WANT_NONEMPTY);
if (alias_mapping.size() != 2) {
LOG(ERROR) << "Invalid file path alias format.";
return;
}
#if defined(OS_WIN)
base::string16 path = request->Url().GetString().Utf16();
const base::string16 file_prefix =
base::ASCIIToUTF16(url::kFileScheme) +
base::ASCIIToUTF16(url::kStandardSchemeSeparator);
#else
std::string path = request->Url().GetString().Utf8();
const std::string file_prefix =
std::string(url::kFileScheme) + url::kStandardSchemeSeparator;
#endif
if (!base::StartsWith(path, file_prefix + alias_mapping[0],
base::CompareCase::SENSITIVE)) {
return;
}
base::ReplaceFirstSubstringAfterOffset(&path, 0, alias_mapping[0],
alias_mapping[1]);
request->SetURL(blink::WebURL(GURL(path)));
}
} // namespace
class RenderFrameImpl::FrameURLLoaderFactory
......@@ -4727,6 +4764,7 @@ void RenderFrameImpl::WillSendRequest(blink::WebURLRequest& request) {
transition_type | ui::PAGE_TRANSITION_CLIENT_REDIRECT);
}
ApplyFilePathAlias(&request);
GURL new_url;
bool attach_same_site_cookies = false;
base::Optional<url::Origin> initiator_origin =
......
......@@ -19,6 +19,7 @@
#include "content/common/frame_owner_properties.h"
#include "content/common/renderer.mojom.h"
#include "content/common/view_messages.h"
#include "content/public/common/content_switches.h"
#include "content/public/common/previews_state.h"
#include "content/public/renderer/content_renderer_client.h"
#include "content/public/renderer/document_state.h"
......@@ -621,6 +622,29 @@ TEST_F(RenderFrameImplTest, AutoplayFlags_WrongOrigin) {
AutoplayFlagsForFrame(GetMainRenderFrame()));
}
TEST_F(RenderFrameImplTest, FileUrlPathAlias) {
const struct {
const char* original;
const char* transformed;
} kTestCases[] = {
{"file:///alias", "file:///replacement"},
{"file:///alias/path/to/file", "file:///replacement/path/to/file"},
{"file://alias/path/to/file", "file://alias/path/to/file"},
{"file:///notalias/path/to/file", "file:///notalias/path/to/file"},
{"file:///root/alias/path/to/file", "file:///root/alias/path/to/file"},
{"file:///", "file:///"},
};
base::CommandLine::ForCurrentProcess()->AppendSwitchASCII(
switches::kFileUrlPathAlias, "/alias=/replacement");
for (const auto& test_case : kTestCases) {
WebURLRequest request;
request.SetURL(GURL(test_case.original));
GetMainRenderFrame()->WillSendRequest(request);
EXPECT_EQ(test_case.transformed, request.Url().GetString().Utf8());
}
}
// RenderFrameRemoteInterfacesTest ------------------------------------
namespace {
......
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