Skip to content

Commit

Permalink
Moves backup results function to service workers
Browse files Browse the repository at this point in the history
  • Loading branch information
SergeyZhukovsky committed Apr 9, 2021
1 parent 49cec3f commit e257f91
Show file tree
Hide file tree
Showing 22 changed files with 342 additions and 188 deletions.
21 changes: 8 additions & 13 deletions browser/brave_content_browser_client.cc
Original file line number Diff line number Diff line change
Expand Up @@ -265,16 +265,6 @@ void MaybeBindBraveWalletProvider(
std::move(receiver));
}
#endif

void BindBraveSearchHost(
content::RenderFrameHost* const frame_host,
mojo::PendingReceiver<brave_search::mojom::BraveSearchFallback> receiver) {
mojo::MakeSelfOwnedReceiver(
std::make_unique<brave_search::BraveSearchHost>(
g_brave_browser_process->shared_url_loader_factory()),
std::move(receiver));
}

} // namespace

BraveContentBrowserClient::BraveContentBrowserClient()
Expand Down Expand Up @@ -327,6 +317,14 @@ BraveContentBrowserClient::AllowWebBluetooth(
return ContentBrowserClient::AllowWebBluetoothResult::BLOCK_GLOBALLY_DISABLED;
}

void BraveContentBrowserClient::BindBraveSearchHost(
mojo::PendingReceiver<brave_search::mojom::BraveSearchFallback> receiver) {
mojo::MakeSelfOwnedReceiver(
std::make_unique<brave_search::BraveSearchHost>(
g_brave_browser_process->shared_url_loader_factory()),
std::move(receiver));
}

void BraveContentBrowserClient::RegisterBrowserInterfaceBindersForFrame(
content::RenderFrameHost* render_frame_host,
mojo::BinderMapWithContext<content::RenderFrameHost*>* map) {
Expand All @@ -341,9 +339,6 @@ void BraveContentBrowserClient::RegisterBrowserInterfaceBindersForFrame(
base::BindRepeating(&MaybeBindBraveWalletProvider));
}
#endif

map->Add<brave_search::mojom::BraveSearchFallback>(
base::BindRepeating(&BindBraveSearchHost));
}

bool BraveContentBrowserClient::HandleExternalProtocol(
Expand Down
3 changes: 3 additions & 0 deletions browser/brave_content_browser_client.h
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,9 @@ class BraveContentBrowserClient : public ChromeContentBrowserClient {

std::string GetEffectiveUserAgent(content::BrowserContext* browser_context,
const GURL& url) override;
void BindBraveSearchHost(
mojo::PendingReceiver<brave_search::mojom::BraveSearchFallback> receiver)
override;

private:
friend class ::BraveNavigatorUserAgentFarblingBrowserTest;
Expand Down
6 changes: 3 additions & 3 deletions browser/brave_search/brave_search_browsertest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ class BraveSearchTest : public InProcessBrowserTest {
std::unique_ptr<net::EmbeddedTestServer> https_server_;
};

IN_PROC_BROWSER_TEST_F(BraveSearchTest, CheckForAFunction) {
IN_PROC_BROWSER_TEST_F(BraveSearchTest, DISABLED_CheckForAFunction) {
GURL url = https_server()->GetURL(kAllowedDomain, "/simple.html");
ui_test_utils::NavigateToURL(browser(), url);
content::WebContents* contents =
Expand All @@ -109,7 +109,7 @@ IN_PROC_BROWSER_TEST_F(BraveSearchTest, CheckForAFunction) {
EXPECT_EQ(base::Value(true), result_first.value);
}

IN_PROC_BROWSER_TEST_F(BraveSearchTest, CheckForAFunctionDev) {
IN_PROC_BROWSER_TEST_F(BraveSearchTest, DISABLED_CheckForAFunctionDev) {
GURL url = https_server()->GetURL(kAllowedDomainDev, "/simple.html");
ui_test_utils::NavigateToURL(browser(), url);
content::WebContents* contents =
Expand All @@ -121,7 +121,7 @@ IN_PROC_BROWSER_TEST_F(BraveSearchTest, CheckForAFunctionDev) {
EXPECT_EQ(base::Value(true), result_first.value);
}

IN_PROC_BROWSER_TEST_F(BraveSearchTest, CheckForAnUndefinedFunction) {
IN_PROC_BROWSER_TEST_F(BraveSearchTest, DISABLED_CheckForAnUndefinedFunction) {
GURL url = https_server()->GetURL(kNotAllowedDomain, "/simple.html");
ui_test_utils::NavigateToURL(browser(), url);
content::WebContents* contents =
Expand Down
1 change: 1 addition & 0 deletions build/config/brave_build.gni
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import("//brave/components/omnibox/browser/sources.gni")
import("//brave/components/search_engines/sources.gni")
import("//brave/components/sync/driver/sources.gni")
import("//brave/components/sync_device_info/sources.gni")
import("//brave/content/sources.gni")
import("//brave/installer/linux/sources.gni")
import("//brave/net/dns/sources.gni")
import("//brave/net/sources.gni")
Expand Down
28 changes: 28 additions & 0 deletions chromium_src/content/browser/browser_interface_binders.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/* Copyright 2021 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/. */

#include "brave/components/brave_search/common/brave_search.mojom.h"
#include "content/browser/process_internals/process_internals.mojom.h"
#include "content/browser/service_worker/service_worker_host.h"
#include "content/public/common/content_client.h"

namespace content {
namespace internal {

void BindBraveSearchHost(
mojo::PendingReceiver<brave_search::mojom::BraveSearchFallback> receiver) {
GetContentClient()->browser()->BindBraveSearchHost(std::move(receiver));
}

void PopulateServiceWorkerBindersBrave(ServiceWorkerHost* host,
mojo::BinderMap* map) {
map->Add<brave_search::mojom::BraveSearchFallback>(
base::BindRepeating(&BindBraveSearchHost));
}

} // namespace internal
} // namespace content

#include "../../../../content/browser/browser_interface_binders.cc"
7 changes: 6 additions & 1 deletion chromium_src/content/public/browser/content_browser_client.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
#ifndef BRAVE_CHROMIUM_SRC_CONTENT_PUBLIC_BROWSER_CONTENT_BROWSER_CLIENT_H_
#define BRAVE_CHROMIUM_SRC_CONTENT_PUBLIC_BROWSER_CONTENT_BROWSER_CLIENT_H_

#include "brave/components/brave_search/common/brave_search.mojom.h"
#include "content/browser/process_internals/process_internals.mojom.h"
#include "third_party/blink/public/mojom/loader/referrer.mojom.h"

// Brave-specific: allows the embedder to modify the referrer string
Expand All @@ -15,7 +17,10 @@
BrowserContext* browser_context, const GURL& request_url, \
const GURL& document_url, blink::mojom::ReferrerPtr* referrer) {} \
virtual std::string GetEffectiveUserAgent(BrowserContext* browser_context, \
const GURL& url);
const GURL& url); \
virtual void BindBraveSearchHost( \
mojo::PendingReceiver<brave_search::mojom::BraveSearchFallback> \
receiver) {}

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

Expand Down
5 changes: 3 additions & 2 deletions components/brave_search/renderer/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ source_set("renderer") {
sources = [
"brave_search_js_handler.cc",
"brave_search_js_handler.h",
"brave_search_render_frame_observer.cc",
"brave_search_render_frame_observer.h",
"brave_search_sw_holder.cc",
"brave_search_sw_holder.h",
]

deps = [
Expand All @@ -14,6 +14,7 @@ source_set("renderer") {
"//mojo/public/cpp/bindings",
"//third_party/blink/public:blink",
"//third_party/blink/public/common",
"//third_party/blink/renderer/core:core",
"//v8",
]
}
1 change: 1 addition & 0 deletions components/brave_search/renderer/DEPS
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,6 @@ include_rules = [
"+content/public/renderer",
"+gin",
"+third_party/blink/public",
"+third_party/blink/renderer/core",
"+v8/include",
]
76 changes: 42 additions & 34 deletions components/brave_search/renderer/brave_search_js_handler.cc
Original file line number Diff line number Diff line change
Expand Up @@ -16,69 +16,82 @@
#include "third_party/blink/public/web/blink.h"
#include "third_party/blink/public/web/web_local_frame.h"
#include "third_party/blink/public/web/web_script_source.h"
#include "third_party/blink/renderer/core/execution_context/execution_context.h"

namespace brave_search {

BraveSearchJSHandler::BraveSearchJSHandler(content::RenderFrame* render_frame)
: render_frame_(render_frame) {}
BraveSearchJSHandler::BraveSearchJSHandler() {}

BraveSearchJSHandler::~BraveSearchJSHandler() = default;

bool BraveSearchJSHandler::EnsureConnected() {
if (!brave_search_fallback_.is_bound()) {
render_frame_->GetBrowserInterfaceBroker()->GetInterface(
ExecutionContext* context = ExecutionContext::From(Context());
context->GetBrowserInterfaceBroker().GetInterface(
brave_search_fallback_.BindNewPipeAndPassReceiver());
}

return brave_search_fallback_.is_bound();
}

void BraveSearchJSHandler::AddJavaScriptObjectToFrame(
v8::Local<v8::Context> context) {
v8::Isolate* isolate = blink::MainThreadIsolate();
v8::Local<v8::Context> BraveSearchJSHandler::Context() {
return v8::Local<v8::Context>::New(isolate_, *context_);
}

v8::Isolate* BraveSearchJSHandler::GetIsolate() {
return isolate_;
}

void BraveSearchJSHandler::AddJavaScriptObject(v8::Local<v8::Context> context) {
v8::Isolate* isolate = context->GetIsolate();
context_ = std::make_unique<v8::Global<v8::Context>>(isolate, context);
isolate_ = isolate;
v8::HandleScope handle_scope(isolate);
if (context.IsEmpty())
return;

v8::Context::Scope context_scope(context);

BindFunctionsToObject(isolate, context);
BindFunctionsToObject(context);
}

void BraveSearchJSHandler::BindFunctionsToObject(
v8::Isolate* isolate,
v8::Local<v8::Context> context) {
v8::Local<v8::Object> global = context->Global();
v8::Local<v8::Value> chrome_value;
if (global->Get(context, gin::StringToV8(isolate, "chrome"))
.ToLocal(&chrome_value) &&
chrome_value->IsObject()) {
BindFunctionToObject(
isolate, chrome_value->ToObject(context).ToLocalChecked(),
"fetchBackupResults",
base::BindRepeating(&BraveSearchJSHandler::FetchBackupResults,
base::Unretained(this), isolate));
v8::Local<v8::Object> brave_obj;
v8::Local<v8::Value> brave_value;
if (!global->Get(context, gin::StringToV8(isolate_, "brave"))
.ToLocal(&brave_value) ||
!brave_value->IsObject()) {
brave_obj = v8::Object::New(isolate_);
global->Set(context, gin::StringToSymbol(isolate_, "brave"), brave_obj)
.Check();
} else {
brave_obj = brave_value->ToObject(context).ToLocalChecked();
}

BindFunctionToObject(
brave_obj, "fetchBackupResults",
base::BindRepeating(&BraveSearchJSHandler::FetchBackupResults,
base::Unretained(this)));
}

template <typename Sig>
void BraveSearchJSHandler::BindFunctionToObject(
v8::Isolate* isolate,
v8::Local<v8::Object> javascript_object,
const std::string& name,
const base::RepeatingCallback<Sig>& callback) {
v8::Local<v8::Context> context = isolate->GetCurrentContext();
v8::Local<v8::Context> context = isolate_->GetCurrentContext();
// Get the isolate associated with this object.
javascript_object
->Set(context, gin::StringToSymbol(isolate, name),
gin::CreateFunctionTemplate(isolate, callback)
->Set(context, gin::StringToSymbol(isolate_, name),
gin::CreateFunctionTemplate(isolate_, callback)
->GetFunction(context)
.ToLocalChecked())
.Check();
}

v8::Local<v8::Promise> BraveSearchJSHandler::FetchBackupResults(
v8::Isolate* isolate,
const std::string& query_string,
const std::string& lang,
const std::string& country,
Expand All @@ -87,18 +100,15 @@ v8::Local<v8::Promise> BraveSearchJSHandler::FetchBackupResults(
return v8::Local<v8::Promise>();

v8::MaybeLocal<v8::Promise::Resolver> resolver =
v8::Promise::Resolver::New(isolate->GetCurrentContext());
v8::Promise::Resolver::New(isolate_->GetCurrentContext());
if (!resolver.IsEmpty()) {
auto promise_resolver =
std::make_unique<v8::Global<v8::Promise::Resolver>>();
promise_resolver->Reset(isolate, resolver.ToLocalChecked());
auto context_old = std::make_unique<v8::Global<v8::Context>>(
isolate, isolate->GetCurrentContext());
promise_resolver->Reset(isolate_, resolver.ToLocalChecked());
brave_search_fallback_->FetchBackupResults(
query_string, lang, country, geo,
base::BindOnce(&BraveSearchJSHandler::OnFetchBackupResults,
base::Unretained(this), std::move(promise_resolver),
isolate, std::move(context_old)));
base::Unretained(this), std::move(promise_resolver)));

return resolver.ToLocalChecked()->GetPromise();
}
Expand All @@ -108,16 +118,14 @@ v8::Local<v8::Promise> BraveSearchJSHandler::FetchBackupResults(

void BraveSearchJSHandler::OnFetchBackupResults(
std::unique_ptr<v8::Global<v8::Promise::Resolver>> promise_resolver,
v8::Isolate* isolate,
std::unique_ptr<v8::Global<v8::Context>> context_old,
const std::string& response) {
v8::HandleScope handle_scope(isolate);
v8::Local<v8::Context> context = context_old->Get(isolate);
v8::HandleScope handle_scope(isolate_);
v8::Local<v8::Context> context = context_->Get(isolate_);
v8::Context::Scope context_scope(context);

v8::Local<v8::Promise::Resolver> resolver = promise_resolver->Get(isolate);
v8::Local<v8::Promise::Resolver> resolver = promise_resolver->Get(isolate_);
v8::Local<v8::String> result;
result = v8::String::NewFromUtf8(isolate, response.c_str()).ToLocalChecked();
result = v8::String::NewFromUtf8(isolate_, response.c_str()).ToLocalChecked();

ALLOW_UNUSED_LOCAL(resolver->Resolve(context, result));
}
Expand Down
22 changes: 9 additions & 13 deletions components/brave_search/renderer/brave_search_js_handler.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@
#include <vector>

#include "brave/components/brave_search/common/brave_search.mojom.h"
#include "content/public/renderer/render_frame.h"
#include "content/public/renderer/render_frame_observer.h"
#include "mojo/public/cpp/bindings/remote.h"
#include "url/gurl.h"
#include "v8/include/v8.h"
Expand All @@ -21,38 +19,36 @@ namespace brave_search {

class BraveSearchJSHandler {
public:
explicit BraveSearchJSHandler(content::RenderFrame* render_frame);
BraveSearchJSHandler();
BraveSearchJSHandler(const BraveSearchJSHandler&) = delete;
BraveSearchJSHandler& operator=(const BraveSearchJSHandler&) = delete;
~BraveSearchJSHandler();

void AddJavaScriptObjectToFrame(v8::Local<v8::Context> context);
void AddJavaScriptObject(v8::Local<v8::Context> context);
v8::Local<v8::Context> Context();
v8::Isolate* GetIsolate();

private:
// Adds a function to the provided object.
template <typename Sig>
void BindFunctionToObject(v8::Isolate* isolate,
v8::Local<v8::Object> javascript_object,
void BindFunctionToObject(v8::Local<v8::Object> javascript_object,
const std::string& name,
const base::RepeatingCallback<Sig>& callback);
void BindFunctionsToObject(v8::Isolate* isolate,
v8::Local<v8::Context> context);
void BindFunctionsToObject(v8::Local<v8::Context> context);
bool EnsureConnected();

// A function to be called from JS
v8::Local<v8::Promise> FetchBackupResults(v8::Isolate* isolate,
const std::string& query_string,
v8::Local<v8::Promise> FetchBackupResults(const std::string& query_string,
const std::string& lang,
const std::string& country,
const std::string& geo);
void OnFetchBackupResults(
std::unique_ptr<v8::Global<v8::Promise::Resolver>> promise_resolver,
v8::Isolate* isolate,
std::unique_ptr<v8::Global<v8::Context>> context_old,
const std::string& response);

content::RenderFrame* render_frame_;
mojo::Remote<brave_search::mojom::BraveSearchFallback> brave_search_fallback_;
std::unique_ptr<v8::Global<v8::Context>> context_;
v8::Isolate* isolate_;
};

} // namespace brave_search
Expand Down
Loading

0 comments on commit e257f91

Please sign in to comment.