Skip to content

Commit

Permalink
Merge pull request #5142 from brave/speedreader-rust
Browse files Browse the repository at this point in the history
Speedreader repository moved into brave core
  • Loading branch information
bridiver committed May 5, 2020
2 parents 3ab6551 + c901fe1 commit 3fa625c
Show file tree
Hide file tree
Showing 69 changed files with 52,898 additions and 262 deletions.
1 change: 0 additions & 1 deletion DEPS
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ deps = {
"vendor/python-patch": "https://github.com/brave/python-patch@d8880110be6554686bc08261766538c2926d4e82",
"vendor/omaha": "https://github.com/brave/omaha.git@de118d8511e4754a673d44a9f1e92d80442069e7",
"vendor/sparkle": "https://github.com/brave/Sparkle.git@198b1f7930d3fb036eaae7a0eae34ec63ef4f479",
"vendor/speedreader_rust_ffi": "https://github.com/brave-experiments/speedreader-rust-ffi.git@9237e8ac4e6fd06548177a7065681a4efabcc239",
"vendor/bat-native-rapidjson": "https://github.com/brave-intl/bat-native-rapidjson.git@86aafe2ef89835ae71c9ed7c2527e3bb3000930e",
"vendor/bip39wally-core-native": "https://github.com/brave-intl/bip39wally-core-native.git@13bb40a215248cfbdd87d0a6b425c8397402e9e6",
"vendor/bat-native-anonize": "https://github.com/brave-intl/bat-native-anonize.git@e3742ba3e8942eea9e4755d91532491871bd3116",
Expand Down
1 change: 1 addition & 0 deletions browser/brave_content_browser_client.cc
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,7 @@ BraveContentBrowserClient::CreateURLLoaderThrottles(
&& request.resource_type
== static_cast<int>(content::ResourceType::kMainFrame)) {
result.push_back(std::make_unique<speedreader::SpeedReaderThrottle>(
g_brave_browser_process->speedreader_whitelist(),
base::ThreadTaskRunnerHandle::Get()));
}
#endif // ENABLE_SPEEDREADER
Expand Down
31 changes: 20 additions & 11 deletions browser/speedreader/speedreader_browsertest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,23 @@
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
* You can obtain one at http://mozilla.org/MPL/2.0/. */

#include "brave/components/speedreader/speedreader_switches.h"

#include "base/bind.h"
#include "base/path_service.h"
#include "brave/app/brave_command_ids.h"
#include "brave/common/brave_paths.h"
#include "brave/components/speedreader/speedreader_switches.h"
#include "chrome/browser/ui/browser.h"
#include "chrome/browser/ui/browser_commands.h"
#include "chrome/test/base/in_process_browser_test.h"
#include "chrome/test/base/ui_test_utils.h"
#include "components/network_session_configurator/common/network_switches.h"
#include "content/public/test/browser_test_utils.h"
#include "net/dns/mock_host_resolver.h"
#include "net/test/embedded_test_server/embedded_test_server.h"

const char kTestHost[] = "theguardian.com";
const char kTestPage[] = "/guardian.html";
const char kTestWhitelist[] = "speedreader_whitelist.json";

class SpeedReaderBrowserTest : public InProcessBrowserTest {
public:
Expand All @@ -26,7 +28,7 @@ class SpeedReaderBrowserTest : public InProcessBrowserTest {
brave::RegisterPathProvider();
base::FilePath test_data_dir;
base::PathService::Get(brave::DIR_TEST_DATA, &test_data_dir);

https_server_.SetSSLConfig(net::EmbeddedTestServer::CERT_OK);
https_server_.ServeFilesFromDirectory(test_data_dir);
EXPECT_TRUE(https_server_.Start());
}
Expand All @@ -38,6 +40,14 @@ class SpeedReaderBrowserTest : public InProcessBrowserTest {

void SetUpCommandLine(base::CommandLine* command_line) override {
command_line->AppendSwitch(speedreader::kEnableSpeedreader);
base::FilePath test_data_dir;
base::PathService::Get(brave::DIR_TEST_DATA, &test_data_dir);
base::FilePath whitelist_path = test_data_dir.Append(kTestWhitelist);
command_line->AppendSwitchPath(speedreader::kSpeedreaderWhitelistPath,
whitelist_path);
// HTTPS server only serves a valid cert for localhost, so this is needed
// to load pages from other hosts without an error
command_line->AppendSwitch(switches::kIgnoreCertificateErrors);
}

void SetUpOnMainThread() override {
Expand All @@ -50,25 +60,24 @@ class SpeedReaderBrowserTest : public InProcessBrowserTest {

IN_PROC_BROWSER_TEST_F(SpeedReaderBrowserTest, SmokeTest) {
chrome::ExecuteCommand(browser(), IDC_TOGGLE_SPEEDREADER);
const GURL url = https_server_.GetURL(kTestPage);
const GURL url = https_server_.GetURL(kTestHost, kTestPage);
ui_test_utils::NavigateToURL(browser(), url);
content::WebContents* contents =
browser()->tab_strip_model()->GetActiveWebContents();
content::RenderFrameHost* rfh = contents->GetMainFrame();

const char kGetStyle[] =
"document.getElementById(\"brave_speedreader_style\").innerHTML";
const char kGetStyleLength[] =
"document.getElementById(\"brave_speedreader_style\").innerHTML.length";

const char kGetContent[] = "document.body.innerHTML";
const char kGetContentLength[] = "document.body.innerHTML.length";

// Check that the document became much smaller and that non-empty speedreader
// style is injected.
EXPECT_LT(0ull, content::EvalJs(rfh, kGetStyle).ExtractString().size());
EXPECT_GT(4096ull, content::EvalJs(rfh, kGetContent).ExtractString().size());
EXPECT_LT(0, content::EvalJs(rfh, kGetStyleLength));
EXPECT_GT(17750 + 1, content::EvalJs(rfh, kGetContentLength));

// Check that disabled speedreader doesn't affect the page.
chrome::ExecuteCommand(browser(), IDC_TOGGLE_SPEEDREADER);
ui_test_utils::NavigateToURL(browser(), url);
EXPECT_LT(106000ull,
content::EvalJs(rfh, kGetContent).ExtractString().size());
EXPECT_LT(106000, content::EvalJs(rfh, kGetContentLength));
}
1 change: 1 addition & 0 deletions browser/speedreader/speedreader_tab_helper.cc
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ void SpeedreaderTabHelper::UpdateActiveState(
auto* whitelist = g_brave_browser_process->speedreader_whitelist();
if (speedreader::IsWhitelistedForTest(handle->GetURL()) ||
whitelist->IsWhitelisted(handle->GetURL())) {
VLOG(2) << __func__ << " SpeedReader active for " << handle->GetURL();
active_ = true;
return;
}
Expand Down
47 changes: 44 additions & 3 deletions build/cargo.gni
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,16 @@
# License, v. 2.0. If a copy of the MPL was not distributed with this file,
# You can obtain one at http://mozilla.org/MPL/2.0/.

import("//brave/build/rust/config.gni")

if (is_mac) {
import("//build/config/mac/mac_sdk.gni")
}

declare_args() {
rustup_path = rebase_path("//brave/build/rustup/", root_build_dir)
}

# Runs a script to compile a local Cargo package and all of its dependencies
template("cargo_build") {
action(target_name) {
Expand All @@ -16,22 +22,57 @@ template("cargo_build") {

outputs = [output]

if (!defined(target)) {
target = rustc_target # defined by //brave/build/rust/config.gni,
}

args = [
"--rustup_path=" + rebase_path("//brave/build/rustup/", root_build_dir),
"--cargo_path=" + rebase_path("//brave/build/rustup/", root_build_dir),
"--rustup_path=" + rustup_path,
"--cargo_path=" + rustup_path,
"--manifest_path=" + rebase_path(manifest_path, root_build_dir),
"--build_path=" + rebase_path(build_path, root_build_dir),
"--target=" + target,
"--toolchain=" + rustc_toolchain, # defined by //brave/build/rust/config.gni,
"--is_debug=$is_debug"
]

if (is_mac) {
args += [ "--mac_deployment_target=" + mac_deployment_target ]
}

if (defined(invoker.rust_flags)) {
if (defined(ldflags)) {
rust_flags += " -C link-args=${ldflags} "
}

if (defined(rust_flags)) {
args += [ "--rust_flags=$rust_flags" ]
}

}
}

# Runs a script to generate C bindings for rust library
template("cargo_cbindgen") {
action(target_name) {
script = "//brave/script/cargo_cbindgen.py"

forward_variables_from(invoker, ["config", "sources", "output"])

assert(defined(config), "Must specify cbindgen.toml config to use")
assert(defined(sources), "Must specify sources to generate bindings for")
assert(defined(output), "Must specify output header file")

sources += [
config,
]
outputs = [ output ]

args = [
"--rustup_path=" + rustup_path,
"--cargo_path=" + rustup_path,
"--config=" + rebase_path(config, root_build_dir),
"--path=" + rebase_path(get_path_info(config, "dir"), root_build_dir),
"--output=" + rebase_path(output, root_build_dir),
]
}
}
85 changes: 85 additions & 0 deletions build/rust/config.gni
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
# Copyright 2020 The Brave Authors. All rights reserved.
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this file,
# You can obtain one at http://mozilla.org/MPL/2.0/. */

import("//build/config/sysroot.gni")

declare_args() {
rust_flags = ""
rustc_target = ""
rustc_toolchain = ""
cargo_lib_prefix = ""
cargo_lib_suffix = ""
rust_shared_lib = is_mac
}

# See https://forge.rust-lang.org/release/platform-support.html for possible targets
if (is_win) {
if (current_cpu == "x86") {
rust_flags += " --codegen target-feature=+crt-static "
rustc_target = "i686-pc-windows-msvc"
} else if (current_cpu == "x64") {
rustc_target = "x86_64-pc-windows-msvc"
}
} else if (is_mac) {
if (current_cpu == "x64") {
rustc_target = "x86_64-apple-darwin"
}
} else if (is_linux) {
if (current_cpu == "x64") {
rustc_target = "x86_64-unknown-linux-gnu"
if (use_sysroot) {
rust_flags += " -C link-arg=--sysroot=$target_sysroot "
rust_flags += " -C link-arg=-Wl,-rpath=\$ORIGIN "
}
}
} else if (is_android) {
if (current_cpu == "arm") {
rustc_target = "arm-linux-androideabi"
rustc_toolchain = "arm"
} else if (current_cpu == "arm64") {
rustc_target = "aarch64-linux-android"
rustc_toolchain = "arm64"
} else if (current_cpu == "x86") {
rustc_target = "i686-linux-android"
rustc_toolchain = "x86"
} else if (current_cpu == "x64") {
rustc_target = "x86_64-linux-android"
rustc_toolchain = "x86_64"
}
} else if (is_ios) {
if (current_cpu == "arm64") {
rustc_target = "aarch64-apple-ios"
} else if (current_cpu == "x64") {
rustc_target = "x86_64-apple-ios"
}
}

cargo_lib_prefix = ""

if (rustc_target != "") {
cargo_lib_prefix += rustc_target
}

if (is_debug) {
cargo_lib_prefix += "/debug"
} else {
cargo_lib_prefix += "/release"
}

cargo_lib_suffix = ""
if (is_win) {
cargo_lib_prefix += "/"
cargo_lib_suffix = ".lib"
} else if (is_linux || is_android || is_ios) {
cargo_lib_prefix += "/lib"
cargo_lib_suffix = ".a"
} else if (is_mac) {
cargo_lib_prefix += "/lib"
if (rust_shared_lib) {
cargo_lib_suffix = ".dylib"
} else {
cargo_lib_suffix = ".a"
}
}
34 changes: 8 additions & 26 deletions components/speedreader/BUILD.gn
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import("//build/buildflag_header.gni")
import("//brave/components/speedreader/buildflags.gni")
import("//brave/vendor/speedreader_rust_ffi/config.gni")

buildflag_header("buildflags") {
header = "buildflags.h"
Expand All @@ -10,9 +9,12 @@ buildflag_header("buildflags") {
}

source_set("speedreader") {
public_deps = [
":buildflags",
"rust/ffi:speedreader_ffi_lib",
]

sources = [
"speedreader_loader.cc",
"speedreader_loader.h",
"speedreader_pref_names.h",
"speedreader_service.cc",
"speedreader_service.h",
Expand All @@ -21,39 +23,19 @@ source_set("speedreader") {
"speedreader_test_whitelist.h",
"speedreader_throttle.cc",
"speedreader_throttle.h",
"speedreader_url_loader.cc",
"speedreader_url_loader.h",
"speedreader_whitelist.cc",
"speedreader_whitelist.h",
]

deps = [
"//brave/components/brave_component_updater/browser",
"//brave/components/resources",
"//brave/components/speedreader:buildflags",
"//brave/vendor/adblock_rust_ffi:adblock_ffi",
"//brave/vendor/speedreader_rust_ffi:speedreadder_ffi",
"//services/network/public/cpp",
"//services/network/public/mojom",
"//ui/base", # For ResourceBundle, consider getting rid of this?
"//url",
"rust/ffi:speedreader_ffi",
]

public_deps = [
":speedreader_libs"
]
}

if (is_mac) {
bundle_data("speedreader_libs") {
sources = [
speedreader_lib_path,
]
outputs = [
"{{bundle_contents_dir}}/Libraries/{{source_file_part}}",
]
public_deps = [
"//brave/vendor/speedreader_rust_ffi:speedreader",
]
}
} else {
group("speedreader_libs") {}
}
Loading

0 comments on commit 3fa625c

Please sign in to comment.