Injects window.chrome.fetchBackupResults

This commit is contained in:
Serg
2021-03-30 22:19:41 -04:00
parent 7113399ea0
commit 9e42e85322
16 changed files with 464 additions and 0 deletions
+2
View File
@@ -134,6 +134,8 @@ source_set("browser_process") {
"//brave/components/brave_component_updater/browser",
"//brave/components/brave_drm",
"//brave/components/brave_referrals/buildflags",
"//brave/components/brave_search/browser",
"//brave/components/brave_search/common:mojom",
"//brave/components/brave_shields/browser",
"//brave/components/brave_shields/common",
"//brave/components/brave_sync",
+13
View File
@@ -25,6 +25,8 @@
#include "brave/common/webui_url_constants.h"
#include "brave/components/binance/browser/buildflags/buildflags.h"
#include "brave/components/brave_rewards/browser/buildflags/buildflags.h"
#include "brave/components/brave_search/browser/brave_search_fallback.h"
#include "brave/components/brave_search/common/brave_search.mojom.h"
#include "brave/components/brave_shields/browser/brave_shields_util.h"
#include "brave/components/brave_shields/browser/brave_shields_web_contents_observer.h"
#include "brave/components/brave_shields/browser/domain_block_navigation_throttle.h"
@@ -262,6 +264,14 @@ void MaybeBindBraveWalletProvider(
}
#endif
void BindBraveSearchFallback(
content::RenderFrameHost* const frame_host,
mojo::PendingReceiver<brave_search::mojom::BraveSearchFallback> receiver) {
mojo::MakeSelfOwnedReceiver(
std::make_unique<brave_search::BraveSearchFallback>(),
std::move(receiver));
}
} // namespace
BraveContentBrowserClient::BraveContentBrowserClient()
@@ -328,6 +338,9 @@ void BraveContentBrowserClient::RegisterBrowserInterfaceBindersForFrame(
base::BindRepeating(&MaybeBindBraveWalletProvider));
}
#endif
map->Add<brave_search::mojom::BraveSearchFallback>(
base::BindRepeating(&BindBraveSearchFallback));
}
bool BraveContentBrowserClient::HandleExternalProtocol(
+13
View File
@@ -0,0 +1,13 @@
import("//build/config/features.gni")
source_set("browser") {
sources = [
"brave_search_fallback.cc",
"brave_search_fallback.h",
]
deps = [
"//base",
"//brave/components/brave_search/common:mojom",
]
}
@@ -0,0 +1,32 @@
/* Copyright (c) 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/browser/brave_search_fallback.h"
#include <utility>
namespace brave_search {
BraveSearchFallback::BraveSearchFallback() : weak_factory_(this) {}
BraveSearchFallback::~BraveSearchFallback() {}
void BraveSearchFallback::FetchBackupResults(
const std::string& query_string,
const std::string& lang,
const std::string& country,
const std::string& geo,
FetchBackupResultsCallback callback) {
// TODO(sergz): make a call to fetch a response and remove that callback call
std::move(callback).Run("response");
}
void BraveSearchFallback::OnFetchBackupResults(
FetchBackupResultsCallback callback,
const std::string& response) {
std::move(callback).Run(response);
}
} // namespace brave_search
@@ -0,0 +1,39 @@
/* Copyright (c) 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/. */
#ifndef BRAVE_COMPONENTS_BRAVE_SEARCH_BROWSER_BRAVE_SEARCH_FALLBACK_H_
#define BRAVE_COMPONENTS_BRAVE_SEARCH_BROWSER_BRAVE_SEARCH_FALLBACK_H_
#include <memory>
#include <string>
#include "base/memory/weak_ptr.h"
#include "brave/components/brave_search/common/brave_search.mojom.h"
namespace brave_search {
class BraveSearchFallback final
: public brave_search::mojom::BraveSearchFallback {
public:
BraveSearchFallback(const BraveSearchFallback&) = delete;
BraveSearchFallback& operator=(const BraveSearchFallback&) = delete;
BraveSearchFallback();
~BraveSearchFallback() override;
void FetchBackupResults(const std::string& query_string,
const std::string& lang,
const std::string& country,
const std::string& geo,
FetchBackupResultsCallback callback) override;
void OnFetchBackupResults(FetchBackupResultsCallback callback,
const std::string& response);
private:
base::WeakPtrFactory<BraveSearchFallback> weak_factory_;
};
} // namespace brave_search
#endif // BRAVE_COMPONENTS_BRAVE_SEARCH_BROWSER_BRAVE_SEARCH_FALLBACK_H_
+7
View File
@@ -0,0 +1,7 @@
import("//mojo/public/tools/bindings/mojom.gni")
mojom("mojom") {
sources = [ "brave_search.mojom" ]
deps = [ "//mojo/public/mojom/base" ]
}
@@ -0,0 +1,10 @@
module brave_search.mojom;
import "mojo/public/mojom/base/values.mojom";
interface BraveSearchFallback {
FetchBackupResults(string query_string,
string lang,
string country,
string geo) => (string response);
};
+25
View File
@@ -0,0 +1,25 @@
source_set("renderer") {
visibility = [
"//brave:child_dependencies",
"//brave/renderer/*",
"//chrome/renderer/*",
]
sources = [
"brave_search_js_handler.cc",
"brave_search_js_handler.h",
"brave_search_render_frame_observer.cc",
"brave_search_render_frame_observer.h",
]
deps = [
"//base",
"//brave/components/brave_search/common:mojom",
"//content/public/renderer",
"//gin",
"//mojo/public/cpp/bindings",
"//third_party/blink/public:blink",
"//third_party/blink/public/common",
"//v8",
]
}
+6
View File
@@ -0,0 +1,6 @@
include_rules = [
"+content/public/renderer",
"+gin",
"+third_party/blink/public",
"+v8/include",
]
@@ -0,0 +1,132 @@
/* Copyright (c) 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/renderer/brave_search_js_handler.h"
#include <utility>
#include "base/no_destructor.h"
#include "base/strings/utf_string_conversions.h"
#include "content/public/renderer/render_frame.h"
#include "gin/arguments.h"
#include "gin/function_template.h"
#include "third_party/blink/public/common/browser_interface_broker_proxy.h"
#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"
namespace brave_search {
BraveSearchJSHandler::BraveSearchJSHandler(content::RenderFrame* render_frame)
: render_frame_(render_frame) {}
BraveSearchJSHandler::~BraveSearchJSHandler() = default;
bool BraveSearchJSHandler::EnsureConnected() {
if (!brave_search_fallback_.is_bound()) {
render_frame_->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::HandleScope handle_scope(isolate);
if (context.IsEmpty())
return;
v8::Context::Scope context_scope(context);
CreateAFallbackObject(isolate, context);
}
void BraveSearchJSHandler::CreateAFallbackObject(
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()) {
BindFunctionsToObject(isolate, context,
chrome_value->ToObject(context).ToLocalChecked());
}
}
void BraveSearchJSHandler::BindFunctionsToObject(
v8::Isolate* isolate,
v8::Local<v8::Context> context,
v8::Local<v8::Object> javascript_object) {
BindFunctionToObject(
isolate, javascript_object, "fetchBackupResults",
base::BindRepeating(&BraveSearchJSHandler::FetchBackupResults,
base::Unretained(this), isolate));
}
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();
// Get the isolate associated with this object.
javascript_object
->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,
const std::string& geo) {
if (!EnsureConnected())
return v8::Local<v8::Promise>();
v8::MaybeLocal<v8::Promise::Resolver> resolver =
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());
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)));
return resolver.ToLocalChecked()->GetPromise();
}
return v8::Local<v8::Promise>();
}
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::Context::Scope context_scope(context);
v8::Local<v8::Promise::Resolver> resolver = promise_resolver->Get(isolate);
v8::Local<v8::String> result;
result = v8::String::NewFromUtf8(isolate, response.c_str()).ToLocalChecked();
ALLOW_UNUSED_LOCAL(resolver->Resolve(context, result));
}
} // namespace brave_search
@@ -0,0 +1,64 @@
/* Copyright (c) 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/. */
#ifndef BRAVE_COMPONENTS_BRAVE_SEARCH_RENDERER_BRAVE_SEARCH_JS_HANDLER_H_
#define BRAVE_COMPONENTS_BRAVE_SEARCH_RENDERER_BRAVE_SEARCH_JS_HANDLER_H_
#include <memory>
#include <string>
#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"
namespace brave_search {
class BraveSearchJSHandler {
public:
explicit BraveSearchJSHandler(content::RenderFrame* render_frame);
BraveSearchJSHandler(const BraveSearchJSHandler&) = delete;
BraveSearchJSHandler& operator=(const BraveSearchJSHandler&) = delete;
~BraveSearchJSHandler();
void AddJavaScriptObjectToFrame(v8::Local<v8::Context> context);
private:
void BindFunctionsToObject(v8::Isolate* isolate,
v8::Local<v8::Context> context,
v8::Local<v8::Object> javascript_object);
// Adds a function to the provided object.
template <typename Sig>
void BindFunctionToObject(v8::Isolate* isolate,
v8::Local<v8::Object> javascript_object,
const std::string& name,
const base::RepeatingCallback<Sig>& callback);
void CreateAFallbackObject(v8::Isolate* isolate,
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,
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_;
};
} // namespace brave_search
#endif // BRAVE_COMPONENTS_BRAVE_SEARCH_RENDERER_BRAVE_SEARCH_JS_HANDLER_H_
@@ -0,0 +1,68 @@
/* Copyright (c) 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/renderer/brave_search_render_frame_observer.h"
#include <string>
#include <vector>
#include "base/no_destructor.h"
#include "content/public/renderer/render_frame.h"
#include "third_party/blink/public/web/web_local_frame.h"
namespace {
static base::NoDestructor<std::vector<std::string>> g_vetted_hosts(
{"search.brave.com", "search-dev.brave.com"});
bool IsVettedHost(const GURL& url) {
std::string host = url.host();
for (size_t i = 0; i < g_vetted_hosts->size(); i++) {
if ((*g_vetted_hosts)[i] == host)
return true;
}
return false;
}
} // namespace
namespace brave_search {
BraveSearchRenderFrameObserver::BraveSearchRenderFrameObserver(
content::RenderFrame* render_frame)
: RenderFrameObserver(render_frame) {
native_javascript_handle_.reset(new BraveSearchJSHandler(render_frame));
}
BraveSearchRenderFrameObserver::~BraveSearchRenderFrameObserver() {}
void BraveSearchRenderFrameObserver::DidStartNavigation(
const GURL& url,
base::Optional<blink::WebNavigationType> navigation_type) {
url_ = url;
}
void BraveSearchRenderFrameObserver::DidCreateScriptContext(
v8::Local<v8::Context> context,
int32_t world_id) {
// There could be empty, invalid and "about:blank" URLs,
// they should fallback to the main frame rules
if (url_.is_empty() || !url_.is_valid() || url_.spec() == "about:blank")
url_ = url::Origin(render_frame()->GetWebFrame()->GetSecurityOrigin())
.GetURL();
if (!url_.SchemeIsHTTPOrHTTPS() || !native_javascript_handle_ ||
!IsVettedHost(url_))
return;
native_javascript_handle_->AddJavaScriptObjectToFrame(context);
}
void BraveSearchRenderFrameObserver::OnDestruct() {
delete this;
}
} // namespace brave_search
@@ -0,0 +1,48 @@
/* Copyright (c) 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/. */
#ifndef BRAVE_COMPONENTS_BRAVE_SEARCH_RENDERER_BRAVE_SEARCH_RENDER_FRAME_OBSERVER_H_
#define BRAVE_COMPONENTS_BRAVE_SEARCH_RENDERER_BRAVE_SEARCH_RENDER_FRAME_OBSERVER_H_
#include <memory>
#include "brave/components/brave_search/renderer/brave_search_js_handler.h"
#include "content/public/renderer/render_frame.h"
#include "content/public/renderer/render_frame_observer.h"
#include "third_party/blink/public/web/web_navigation_type.h"
#include "url/gurl.h"
#include "v8/include/v8.h"
namespace brave_search {
class BraveSearchRenderFrameObserver : public content::RenderFrameObserver {
public:
explicit BraveSearchRenderFrameObserver(content::RenderFrame* render_frame);
BraveSearchRenderFrameObserver(const BraveSearchRenderFrameObserver&) =
delete;
BraveSearchRenderFrameObserver& operator=(
const BraveSearchRenderFrameObserver&) = delete;
~BraveSearchRenderFrameObserver() override;
// RenderFrameObserver implementation.
void DidStartNavigation(
const GURL& url,
base::Optional<blink::WebNavigationType> navigation_type) override;
void DidCreateScriptContext(v8::Local<v8::Context> context,
int32_t world_id) override;
private:
// RenderFrameObserver implementation.
void OnDestruct() override;
// Handle to "handler" JavaScript object functionality.
std::unique_ptr<BraveSearchJSHandler> native_javascript_handle_;
GURL url_;
};
} // namespace brave_search
#endif // BRAVE_COMPONENTS_BRAVE_SEARCH_RENDERER_BRAVE_SEARCH_RENDER_FRAME_OBSERVER_H_
+1
View File
@@ -12,6 +12,7 @@ source_set("renderer") {
public_deps = [ "//chrome/renderer" ]
deps = [
"//brave/components/brave_search/renderer",
"//brave/components/brave_shields/common",
"//brave/components/brave_wallet/common/buildflags",
"//brave/components/cosmetic_filters/renderer",
@@ -6,6 +6,7 @@
#include "brave/renderer/brave_content_renderer_client.h"
#include "base/feature_list.h"
#include "brave/components/brave_search/renderer/brave_search_render_frame_observer.h"
#include "brave/components/brave_shields/common/features.h"
#include "brave/components/brave_wallet/common/buildflags/buildflags.h"
#include "brave/components/cosmetic_filters/renderer/cosmetic_filters_js_render_frame_observer.h"
@@ -65,6 +66,8 @@ void BraveContentRendererClient::RenderFrameCreated(
render_frame, BraveRenderThreadObserver::GetDynamicParams());
}
#endif
new brave_search::BraveSearchRenderFrameObserver(render_frame);
}
void BraveContentRendererClient::RunScriptsAtDocumentStart(
+1
View File
@@ -13,6 +13,7 @@ brave_chrome_renderer_sources = [
]
brave_chrome_renderer_public_deps = [
"//brave/components/brave_search/renderer",
"//brave/components/brave_wallet/common/buildflags",
"//brave/components/content_settings/renderer",
"//brave/components/cosmetic_filters/renderer",