Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add hidden renderer for fetching backup search results #27285

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion android/android_browser_tests.gni
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
android_test_exception_sources = [
"//brave/browser/brave_content_browser_client_browsertest.cc",
"//brave/browser/brave_scheme_load_browsertest.cc",
"//brave/browser/brave_search/brave_search_browsertest.cc",
"//brave/browser/brave_shields/brave_shields_web_contents_observer_browsertest.cc",
"//brave/browser/brave_shields/cookie_expiry_browsertest.cc",
"//brave/browser/brave_shields/domain_block_page_browsertest.cc",
Expand Down
17 changes: 15 additions & 2 deletions browser/brave_content_browser_client.cc
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
#include "brave/browser/brave_browser_features.h"
#include "brave/browser/brave_browser_main_extra_parts.h"
#include "brave/browser/brave_browser_process.h"
#include "brave/browser/brave_search/backup_results_navigation_throttle.h"
#include "brave/browser/brave_search/backup_results_service_factory.h"
#include "brave/browser/brave_shields/brave_farbling_service_factory.h"
#include "brave/browser/brave_shields/brave_shields_web_contents_observer.h"
#include "brave/browser/brave_wallet/brave_wallet_context_utils.h"
Expand Down Expand Up @@ -56,6 +58,7 @@
#include "brave/components/body_sniffer/body_sniffer_throttle.h"
#include "brave/components/brave_federated/features.h"
#include "brave/components/brave_rewards/browser/rewards_protocol_navigation_throttle.h"
#include "brave/components/brave_search/browser/backup_results_service.h"
#include "brave/components/brave_search/browser/brave_search_default_host.h"
#include "brave/components/brave_search/browser/brave_search_default_host_private.h"
#include "brave/components/brave_search/browser/brave_search_fallback_host.h"
Expand Down Expand Up @@ -426,10 +429,14 @@ void BindBraveSearchFallbackHost(
}

content::BrowserContext* context = render_process_host->GetBrowserContext();
auto* backup_results_service =
brave_search::BackupResultsServiceFactory::GetForBrowserContext(context);
if (!backup_results_service) {
return;
}
mojo::MakeSelfOwnedReceiver(
std::make_unique<brave_search::BraveSearchFallbackHost>(
context->GetDefaultStoragePartition()
->GetURLLoaderFactoryForBrowserProcess()),
backup_results_service),
std::move(receiver));
}

Expand Down Expand Up @@ -1299,6 +1306,12 @@ BraveContentBrowserClient::CreateThrottlesForNavigation(
}
#endif

if (auto backup_results_throttle =
brave_search::BackupResultsNavigationThrottle::MaybeCreateThrottleFor(
handle)) {
throttles.push_back(std::move(backup_results_throttle));
}

return throttles;
}

Expand Down
52 changes: 52 additions & 0 deletions browser/brave_search/BUILD.gn
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
# Copyright (c) 2019 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 https://mozilla.org/MPL/2.0/.

source_set("brave_search") {
sources = [
"backup_results_navigation_throttle.cc",
"backup_results_navigation_throttle.h",
"backup_results_service_factory.cc",
"backup_results_service_factory.h",
"backup_results_service_impl.cc",
"backup_results_service_impl.h",
]

deps = [
"//brave/components/brave_search/browser",
"//brave/components/brave_search/common",
"//chrome/browser/content_extraction",
"//chrome/browser/profiles",
"//components/keyed_service/core",
"//content/public/browser",
"//services/network/public/cpp",
]
}

source_set("browser_tests") {
testonly = true

sources = [
"backup_results_service_browsertest.cc",
"brave_search_browsertest.cc",
]

deps = [
":brave_search",
"//base/test:test_support",
"//brave/components/brave_ads/core/public:headers",
"//brave/components/brave_rewards/common",
"//brave/components/brave_search/browser",
"//brave/components/brave_search/common",
"//chrome/browser/profiles:profile",
"//chrome/browser/search_engines",
"//chrome/browser/ui",
"//chrome/test:test_support",
"//chrome/test:test_support_ui",
"//content/test:test_support",
"//net:test_support",
]

defines = [ "HAS_OUT_OF_PROC_TEST_RUNNER" ]
}
53 changes: 53 additions & 0 deletions browser/brave_search/backup_results_navigation_throttle.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/* Copyright (c) 2025 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 https://mozilla.org/MPL/2.0/. */

#include "brave/browser/brave_search/backup_results_navigation_throttle.h"

#include "brave/browser/brave_search/backup_results_service_factory.h"
#include "brave/components/brave_search/browser/backup_results_service.h"
#include "chrome/browser/profiles/profile.h"
#include "content/public/browser/browser_context.h"
#include "content/public/browser/navigation_handle.h"
#include "content/public/browser/web_contents.h"

namespace brave_search {

std::unique_ptr<BackupResultsNavigationThrottle>
BackupResultsNavigationThrottle::MaybeCreateThrottleFor(
content::NavigationHandle* navigation_handle) {
auto* context = navigation_handle->GetWebContents()->GetBrowserContext();
auto* profile = Profile::FromBrowserContext(context);
if (!profile->IsOffTheRecord() ||
!profile->GetOTRProfileID().IsSearchBackupResults()) {
return nullptr;
}

return std::make_unique<BackupResultsNavigationThrottle>(navigation_handle);
}

BackupResultsNavigationThrottle::BackupResultsNavigationThrottle(
content::NavigationHandle* navigation_handle)
: NavigationThrottle(navigation_handle) {}
BackupResultsNavigationThrottle::~BackupResultsNavigationThrottle() = default;

content::NavigationThrottle::ThrottleCheckResult
BackupResultsNavigationThrottle::WillStartRequest() {
auto* web_contents = navigation_handle()->GetWebContents();
auto* backup_results_service =
BackupResultsServiceFactory::GetForBrowserContext(
web_contents->GetBrowserContext());
if (backup_results_service->HandleWebContentsStartRequest(
web_contents, navigation_handle()->GetURL())) {
return content::NavigationThrottle::PROCEED;
}

return content::NavigationThrottle::CANCEL;
}

const char* BackupResultsNavigationThrottle::GetNameForLogging() {
return "BackupResultsNavigationThrottle";
}

} // namespace brave_search
42 changes: 42 additions & 0 deletions browser/brave_search/backup_results_navigation_throttle.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/* Copyright (c) 2025 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 https://mozilla.org/MPL/2.0/. */

#ifndef BRAVE_BROWSER_BRAVE_SEARCH_BACKUP_RESULTS_NAVIGATION_THROTTLE_H_
#define BRAVE_BROWSER_BRAVE_SEARCH_BACKUP_RESULTS_NAVIGATION_THROTTLE_H_

#include <memory>

#include "content/public/browser/navigation_throttle.h"

namespace content {
class NavigationHandle;
} // namespace content

namespace brave_search {

class BackupResultsNavigationThrottle : public content::NavigationThrottle {
public:
explicit BackupResultsNavigationThrottle(
content::NavigationHandle* navigation_handle);
~BackupResultsNavigationThrottle() override;

BackupResultsNavigationThrottle(const BackupResultsNavigationThrottle&) =
delete;
BackupResultsNavigationThrottle& operator=(
const BackupResultsNavigationThrottle&) = delete;

static std::unique_ptr<BackupResultsNavigationThrottle>
MaybeCreateThrottleFor(content::NavigationHandle* navigation_handle);

private:
// content::NavigationThrottle overrides:
ThrottleCheckResult WillStartRequest() override;

const char* GetNameForLogging() override;
};

} // namespace brave_search

#endif // BRAVE_BROWSER_BRAVE_SEARCH_BACKUP_RESULTS_NAVIGATION_THROTTLE_H_
178 changes: 178 additions & 0 deletions browser/brave_search/backup_results_service_browsertest.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,178 @@
// Copyright (c) 2025 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 https://mozilla.org/MPL/2.0/.

#include "brave/components/brave_search/browser/backup_results_service.h"

#include "base/test/bind.h"
#include "base/test/scoped_feature_list.h"
#include "brave/browser/brave_search/backup_results_service_factory.h"
#include "brave/components/brave_search/common/features.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/ui/browser.h"
#include "chrome/test/base/in_process_browser_test.h"
#include "content/public/test/browser_test.h"
#include "content/public/test/browser_test_utils.h"
#include "content/public/test/content_mock_cert_verifier.h"
#include "net/dns/mock_host_resolver.h"
#include "net/test/embedded_test_server/embedded_test_server.h"
#include "net/test/embedded_test_server/http_request.h"
#include "net/test/embedded_test_server/http_response.h"

namespace brave_search {

namespace {

constexpr char kTestInitPath[] = "/test";
constexpr char kTestInitHtml[] = R"(
<!doctype html>
<html>
<body>
Test Content
<script>
document.cookie = "testcookie=value; path=/";
window.location.href = "/test2";
</script>
</body>
</html>
)";

constexpr char kTestFinalPath[] = "/test2";
constexpr char kTestFinalHtml[] =
"<!doctype html><html><body>Test Content</body></html>";

} // namespace

class BackupResultsServiceBrowserTest : public InProcessBrowserTest {
public:
void SetUpOnMainThread() override {
InProcessBrowserTest::SetUpOnMainThread();

mock_cert_verifier_.mock_cert_verifier()->set_default_result(net::OK);
host_resolver()->AddRule("*", "127.0.0.1");

https_server_ = std::make_unique<net::EmbeddedTestServer>(
net::test_server::EmbeddedTestServer::TYPE_HTTPS);
https_server_->RegisterRequestHandler(
base::BindRepeating(&BackupResultsServiceBrowserTest::HandleRequest,
base::Unretained(this)));

ASSERT_TRUE(https_server_->Start());
}

protected:
std::unique_ptr<net::test_server::HttpResponse> HandleRequest(
const net::test_server::HttpRequest& request) {
auto response = std::make_unique<net::test_server::BasicHttpResponse>();
response->set_code(net::HTTP_OK);
response->set_content_type("text/html");

auto path = request.GetURL().path();
if (path == kTestInitPath) {
response->set_content(kTestInitHtml);
} else if (path == kTestFinalPath) {
auto cookie_it = request.headers.find(net::HttpRequestHeaders::kCookie);
bool has_cookie =
cookie_it != request.headers.end() &&
cookie_it->second.find("testcookie=value") != std::string::npos;
EXPECT_TRUE(has_cookie);
if (has_cookie) {
response->set_content(kTestFinalHtml);
} else {
response->set_content(
"<html><body>Cookie validation failed</body></html>");
response->set_code(net::HTTP_BAD_REQUEST);
}
} else {
response->set_content("<html><body>Not Found</body></html>");
response->set_code(net::HTTP_NOT_FOUND);
}

return response;
}

base::test::ScopedFeatureList scoped_feature_list_;
content::ContentMockCertVerifier mock_cert_verifier_;
std::unique_ptr<net::EmbeddedTestServer> https_server_;
};

IN_PROC_BROWSER_TEST_F(BackupResultsServiceBrowserTest, BasicRenderAndLoad) {
auto* backup_results_service =
BackupResultsServiceFactory::GetForBrowserContext(browser()->profile());

base::RunLoop run_loop;
GURL url = https_server_->GetURL("a.com", kTestInitPath);

backup_results_service->FetchBackupResults(
url, std::nullopt,
base::BindLambdaForTesting(
[&](std::optional<BackupResultsService::BackupResults> result) {
EXPECT_TRUE(result.has_value());
if (result) {
EXPECT_EQ(kTestFinalHtml, result->html);
EXPECT_EQ(net::HTTP_OK, result->final_status_code);
}
run_loop.Quit();
}));

run_loop.Run();
}

IN_PROC_BROWSER_TEST_F(BackupResultsServiceBrowserTest, CookieHeader) {
auto* backup_results_service =
BackupResultsServiceFactory::GetForBrowserContext(browser()->profile());

base::RunLoop run_loop;
GURL url = https_server_->GetURL("a.com", kTestFinalPath);

net::HttpRequestHeaders headers;
headers.SetHeader(net::HttpRequestHeaders::kCookie, "testcookie=value");

backup_results_service->FetchBackupResults(
url, headers,
base::BindLambdaForTesting(
[&](std::optional<BackupResultsService::BackupResults> result) {
EXPECT_TRUE(result.has_value());
if (result) {
EXPECT_EQ(kTestFinalHtml, result->html);
EXPECT_EQ(net::HTTP_OK, result->final_status_code);
}
run_loop.Quit();
}));

run_loop.Run();
}

class BackupResultsServiceFullRenderBrowserTest
: public BackupResultsServiceBrowserTest {
public:
BackupResultsServiceFullRenderBrowserTest() {
scoped_feature_list_.InitAndEnableFeature(
features::kBackupResultsFullRender);
}
};

IN_PROC_BROWSER_TEST_F(BackupResultsServiceFullRenderBrowserTest, FullRender) {
auto* backup_results_service =
BackupResultsServiceFactory::GetForBrowserContext(browser()->profile());

base::RunLoop run_loop;
GURL url = https_server_->GetURL("a.com", kTestInitPath);

backup_results_service->FetchBackupResults(
url, std::nullopt,
base::BindLambdaForTesting(
[&](std::optional<BackupResultsService::BackupResults> result) {
EXPECT_TRUE(result.has_value());
if (result) {
EXPECT_EQ(kTestFinalHtml, result->html);
EXPECT_EQ(net::HTTP_OK, result->final_status_code);
}
run_loop.Quit();
}));

run_loop.Run();
}

} // namespace brave_search
Loading
Loading