Skip to content

Commit 9458c2c

Browse files
committed
Implement render process handling
1 parent 0775632 commit 9458c2c

File tree

6 files changed

+309
-0
lines changed

6 files changed

+309
-0
lines changed
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# Copyright (c) 2025 The Brave Authors. All rights reserved.
2+
# This Source Code Form is subject to the terms of the Mozilla Public
3+
# License, v. 2.0. If a copy of the MPL was not distributed with this file,
4+
# You can obtain one at https://mozilla.org/MPL/2.0/.
5+
6+
component("renderer") {
7+
output_name = "youtube_script_injector_renderer"
8+
9+
defines = [ "IS_YOUTUBE_SCRIPT_INJECTOR_RENDERER_IMPL" ]
10+
11+
sources = [
12+
"youtube_render_frame_observer.cc",
13+
"youtube_render_frame_observer.h",
14+
"youtube_injector_frame_js_handler.cc",
15+
"youtube_injector_frame_js_handler.h",
16+
]
17+
18+
deps = [
19+
"//base",
20+
"//brave/components/youtube_script_injector/common",
21+
"//brave/components/youtube_script_injector/common:mojom",
22+
"//brave/components/youtube_script_injector/browser/core",
23+
"//brave/components/youtube_script_injector/browser/content",
24+
"//content/public/renderer",
25+
"//gin",
26+
"//mojo/public/cpp/bindings",
27+
"//third_party/blink/public:blink",
28+
"//third_party/blink/public/common",
29+
"//third_party/blink/public/strings",
30+
"//v8",
31+
]
32+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
include_rules = [
2+
"+content/public/renderer",
3+
"+gin",
4+
"+third_party/blink/public",
5+
"+v8/include",
6+
]
Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
/* Copyright (c) 2025 The Brave Authors. All rights reserved.
2+
* This Source Code Form is subject to the terms of the Mozilla Public
3+
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
4+
* You can obtain one at http://mozilla.org/MPL/2.0/. */
5+
6+
#include "brave/components/youtube_script_injector/renderer/youtube_injector_frame_js_handler.h"
7+
8+
#include <tuple>
9+
#include <utility>
10+
11+
#include "base/no_destructor.h"
12+
#include "base/strings/utf_string_conversions.h"
13+
#include "content/public/renderer/render_frame.h"
14+
#include "gin/arguments.h"
15+
#include "gin/function_template.h"
16+
#include "third_party/blink/public/platform/browser_interface_broker_proxy.h"
17+
#include "third_party/blink/public/mojom/devtools/console_message.mojom.h"
18+
#include "third_party/blink/public/platform/scheduler/web_agent_group_scheduler.h"
19+
#include "third_party/blink/public/platform/web_string.h"
20+
#include "third_party/blink/public/web/blink.h"
21+
#include "third_party/blink/public/web/web_console_message.h"
22+
#include "third_party/blink/public/web/web_local_frame.h"
23+
#include "third_party/blink/public/web/web_script_source.h"
24+
25+
namespace youtube_script_injector {
26+
27+
YouTubeInjectorFrameJSHandler::YouTubeInjectorFrameJSHandler(
28+
content::RenderFrame* render_frame)
29+
: render_frame_(render_frame) {}
30+
31+
YouTubeInjectorFrameJSHandler::~YouTubeInjectorFrameJSHandler() = default;
32+
33+
bool YouTubeInjectorFrameJSHandler::EnsureConnected() {
34+
if (!youtube_injector_.is_bound()) {
35+
render_frame_->GetBrowserInterfaceBroker().GetInterface(
36+
youtube_injector_.BindNewPipeAndPassReceiver());
37+
}
38+
39+
return youtube_injector_.is_bound();
40+
}
41+
42+
void YouTubeInjectorFrameJSHandler::AddJavaScriptObjectToFrame(
43+
v8::Local<v8::Context> context) {
44+
CHECK(render_frame_);
45+
v8::Isolate* isolate =
46+
render_frame_->GetWebFrame()->GetAgentGroupScheduler()->Isolate();
47+
v8::HandleScope handle_scope(isolate);
48+
if (context.IsEmpty())
49+
return;
50+
51+
v8::Context::Scope context_scope(context);
52+
53+
BindFunctionsToObject(isolate, context);
54+
}
55+
56+
void YouTubeInjectorFrameJSHandler::ResetRemote(
57+
content::RenderFrame* render_frame) {
58+
render_frame_ = render_frame;
59+
youtube_injector_.reset();
60+
EnsureConnected();
61+
}
62+
63+
void YouTubeInjectorFrameJSHandler::BindFunctionsToObject(
64+
v8::Isolate* isolate,
65+
v8::Local<v8::Context> context) {
66+
v8::Local<v8::Object> global = context->Global();
67+
v8::Local<v8::Object> brave_obj;
68+
v8::Local<v8::Value> brave_value;
69+
if (!global->Get(context, gin::StringToV8(isolate, "brave"))
70+
.ToLocal(&brave_value) ||
71+
!brave_value->IsObject()) {
72+
brave_obj = v8::Object::New(isolate);
73+
global->Set(context, gin::StringToSymbol(isolate, "brave"), brave_obj)
74+
.Check();
75+
} else {
76+
brave_obj = brave_value->ToObject(context).ToLocalChecked();
77+
}
78+
BindFunctionToObject(
79+
isolate, brave_obj, "nativePipMode",
80+
base::BindRepeating(
81+
&YouTubeInjectorFrameJSHandler::NativePipMode, base::Unretained(this)));
82+
}
83+
84+
template <typename Sig>
85+
void YouTubeInjectorFrameJSHandler::BindFunctionToObject(
86+
v8::Isolate* isolate,
87+
v8::Local<v8::Object> javascript_object,
88+
const std::string& name,
89+
const base::RepeatingCallback<Sig>& callback) {
90+
LOG(ERROR) << "SIMONE - BindFunctionToObject";
91+
v8::Local<v8::Context> context = isolate->GetCurrentContext();
92+
javascript_object
93+
->Set(context, gin::StringToSymbol(isolate, name),
94+
gin::CreateFunctionTemplate(isolate, callback)
95+
->GetFunction(context)
96+
.ToLocalChecked())
97+
.Check();
98+
}
99+
100+
void YouTubeInjectorFrameJSHandler::NativePipMode() {
101+
if (!EnsureConnected()) {
102+
return;
103+
}
104+
// auto* web_frame = render_frame_->GetWebFrame();
105+
youtube_injector_->NativePipMode();
106+
}
107+
108+
} // namespace youtube_script_injector
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
// Copyright (c) 2025 The Brave Authors. All rights reserved.
2+
// This Source Code Form is subject to the terms of the Mozilla Public
3+
// License, v. 2.0. If a copy of the MPL was not distributed with this file,
4+
// you can obtain one at http://mozilla.org/MPL/2.0/.
5+
6+
#ifndef BRAVE_COMPONENTS_YOUTUBE_SCRIPT_INJECTOR_RENDERER_YOUTUBE_INJECTOR_FRAME_JS_HANDLER_H_
7+
#define BRAVE_COMPONENTS_YOUTUBE_SCRIPT_INJECTOR_RENDERER_YOUTUBE_INJECTOR_FRAME_JS_HANDLER_H_
8+
9+
#include <memory>
10+
#include <string>
11+
#include <vector>
12+
13+
#include "base/memory/raw_ptr.h"
14+
#include "brave/components/youtube_script_injector/common/youtube_injector.mojom.h"
15+
#include "content/public/renderer/render_frame.h"
16+
#include "content/public/renderer/render_frame_observer.h"
17+
#include "mojo/public/cpp/bindings/remote.h"
18+
#include "url/gurl.h"
19+
#include "v8/include/v8.h"
20+
21+
namespace youtube_script_injector {
22+
23+
class YouTubeInjectorFrameJSHandler {
24+
public:
25+
YouTubeInjectorFrameJSHandler(content::RenderFrame* render_frame);
26+
YouTubeInjectorFrameJSHandler(const YouTubeInjectorFrameJSHandler&) = delete;
27+
YouTubeInjectorFrameJSHandler& operator=(const YouTubeInjectorFrameJSHandler&) =
28+
delete;
29+
~YouTubeInjectorFrameJSHandler();
30+
31+
void AddJavaScriptObjectToFrame(v8::Local<v8::Context> context);
32+
void ResetRemote(content::RenderFrame* render_frame);
33+
34+
private:
35+
// Adds a function to the provided object.
36+
template <typename Sig>
37+
void BindFunctionToObject(v8::Isolate* isolate,
38+
v8::Local<v8::Object> javascript_object,
39+
const std::string& name,
40+
const base::RepeatingCallback<Sig>& callback);
41+
void BindFunctionsToObject(v8::Isolate* isolate,
42+
v8::Local<v8::Context> context);
43+
bool EnsureConnected();
44+
45+
// A function to be called from JS
46+
void NativePipMode();
47+
48+
raw_ptr<content::RenderFrame> render_frame_ = nullptr;
49+
mojo::Remote<youtube_script_injector::mojom::YouTubeInjector> youtube_injector_;
50+
};
51+
52+
} // namespace youtube_script_injector
53+
54+
#endif // BRAVE_COMPONENTS_YOUTUBE_SCRIPT_INJECTOR_RENDERER_YOUTUBE_INJECTOR_FRAME_JS_HANDLER_H_
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
/* Copyright (c) 2025 The Brave Authors. All rights reserved.
2+
* This Source Code Form is subject to the terms of the Mozilla Public
3+
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
4+
* You can obtain one at http://mozilla.org/MPL/2.0/. */
5+
6+
#include "brave/components/youtube_script_injector/renderer/youtube_render_frame_observer.h"
7+
#include "brave/components/youtube_script_injector/browser/core/youtube_registry.h"
8+
#include "brave/components/youtube_script_injector/browser/content/youtube_tab_helper.h"
9+
10+
#include "content/public/renderer/render_frame.h"
11+
#include "third_party/blink/public/platform/web_isolated_world_info.h"
12+
#include "third_party/blink/public/platform/web_url.h"
13+
#include "third_party/blink/public/web/web_local_frame.h"
14+
#include "v8/include/v8.h"
15+
16+
namespace youtube_script_injector {
17+
18+
YouTubeRenderFrameObserver::YouTubeRenderFrameObserver(
19+
content::RenderFrame* render_frame,
20+
int32_t world_id)
21+
: RenderFrameObserver(render_frame), world_id_(world_id) {
22+
}
23+
24+
YouTubeRenderFrameObserver::~YouTubeRenderFrameObserver() = default;
25+
26+
void YouTubeRenderFrameObserver::DidCreateScriptContext(
27+
v8::Local<v8::Context> context,
28+
int32_t world_id) {
29+
if (!render_frame()->IsMainFrame() || world_id_ != world_id) {
30+
return;
31+
}
32+
33+
if (!YouTubeRegistry::IsYouTubeDomain(url_)) {
34+
return;
35+
}
36+
37+
if (!native_javascript_handle_) {
38+
native_javascript_handle_ = std::make_unique<YouTubeInjectorFrameJSHandler>(
39+
render_frame());
40+
} else {
41+
native_javascript_handle_->ResetRemote(render_frame());
42+
}
43+
44+
native_javascript_handle_->AddJavaScriptObjectToFrame(context);
45+
}
46+
47+
void YouTubeRenderFrameObserver::DidStartNavigation(const GURL& url,
48+
std::optional<blink::WebNavigationType> navigation_type) {
49+
url_ = url;
50+
}
51+
52+
void YouTubeRenderFrameObserver::DidFinishLoad() {
53+
}
54+
55+
void YouTubeRenderFrameObserver::OnDestruct() {
56+
delete this;
57+
}
58+
59+
} // namespace youtube_script_injector
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/* Copyright (c) 2025 The Brave Authors. All rights reserved.
2+
* This Source Code Form is subject to the terms of the Mozilla Public
3+
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
4+
* You can obtain one at http://mozilla.org/MPL/2.0/. */
5+
6+
#ifndef BRAVE_COMPONENTS_YOUTUBE_SCRIPT_INJECTOR_RENDERER_YOUTUBE_RENDER_FRAME_OBSERVER_H_
7+
#define BRAVE_COMPONENTS_YOUTUBE_SCRIPT_INJECTOR_RENDERER_YOUTUBE_RENDER_FRAME_OBSERVER_H_
8+
9+
#include <memory>
10+
#include <optional>
11+
12+
#include "brave/components/youtube_script_injector/renderer/youtube_injector_frame_js_handler.h"
13+
#include "content/public/renderer/render_frame.h"
14+
#include "content/public/renderer/render_frame_observer.h"
15+
#include "third_party/blink/public/web/web_navigation_type.h"
16+
#include "url/gurl.h"
17+
#include "v8/include/v8.h"
18+
19+
namespace youtube_script_injector {
20+
21+
class COMPONENT_EXPORT(YOUTUBE_SCRIPT_INJECTOR_RENDERER) YouTubeRenderFrameObserver
22+
: public content::RenderFrameObserver {
23+
public:
24+
YouTubeRenderFrameObserver(content::RenderFrame* render_frame,
25+
int32_t world_id);
26+
~YouTubeRenderFrameObserver() override;
27+
YouTubeRenderFrameObserver(const YouTubeRenderFrameObserver&) =
28+
delete;
29+
YouTubeRenderFrameObserver& operator=(
30+
const YouTubeRenderFrameObserver&) = delete;
31+
32+
// RenderFrameObserver implementation.
33+
void DidStartNavigation(
34+
const GURL& url,
35+
std::optional<blink::WebNavigationType> navigation_type) override;
36+
void DidCreateScriptContext(v8::Local<v8::Context> context,
37+
int32_t world_id) override;
38+
void DidFinishLoad() override;
39+
40+
private:
41+
// RenderFrameObserver implementation.
42+
void OnDestruct() override;
43+
44+
std::unique_ptr<YouTubeInjectorFrameJSHandler> native_javascript_handle_;
45+
int32_t world_id_;
46+
GURL url_;
47+
};
48+
49+
} // namespace youtube_script_injector
50+
#endif // BRAVE_COMPONENTS_YOUTUBE_SCRIPT_INJECTOR_RENDERER_YOUTUBE_RENDER_FRAME_OBSERVER_H_

0 commit comments

Comments
 (0)