Skip to content

Commit

Permalink
Update the JS injection demo
Browse files Browse the repository at this point in the history
  • Loading branch information
kaidokert committed Jan 16, 2025
1 parent 165ac0e commit e1b3191
Show file tree
Hide file tree
Showing 8 changed files with 41 additions and 4 deletions.
1 change: 1 addition & 0 deletions cobalt/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ if (!is_android) {
deps = [
"//cobalt/renderer:renderer",
"//cobalt/user_agent",
"//components/js_injection/browser:browser",
"//content/public/app",
"//content/shell:content_shell_app",
"//content/shell:content_shell_lib",
Expand Down
1 change: 1 addition & 0 deletions cobalt/android/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,7 @@ shared_library("libcobalt_content_shell_content_view") {

# TODO: what can be removed in the dependencies?
"//components/crash/content/browser",
"//components/js_injection/browser:browser",
"//content/shell:content_shell_app",
"//content/shell:content_shell_lib",
"//content/shell:pak",
Expand Down
2 changes: 1 addition & 1 deletion cobalt/cobalt_content_browser_client.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ class CobaltContentBrowserClient : public content::ShellContentBrowserClient {
blink::UserAgentMetadata GetUserAgentMetadata() override;
void OverrideWebkitPrefs(content::WebContents* web_contents,
blink::web_pref::WebPreferences* prefs) override;
void OnWebContentsCreated(content::WebContents* web_contents);
void OnWebContentsCreated(content::WebContents* web_contents) override;

private:
std::unique_ptr<CobaltWebContentsObserver> web_contents_observer_;
Expand Down
15 changes: 15 additions & 0 deletions cobalt/cobalt_web_contents_observer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,21 @@

namespace cobalt {

CobaltWebContentsObserver::CobaltWebContentsObserver(
content::WebContents* web_contents)
: content::WebContentsObserver(web_contents) {
// Create browser-side mojo service component
js_communication_host_ =
std::make_unique<js_injection::JsCommunicationHost>(web_contents);

// Inject a script at document start for all origins
const std::u16string script(u"console.log('Hello from JS injection');");
const std::vector<std::string> allowed_origins({"*"});
auto result = js_communication_host_->AddDocumentStartJavaScript(
script, allowed_origins);
CHECK(!result.error_message);
}

// Placeholder for a WebContentsObserver override
void CobaltWebContentsObserver::PrimaryMainDocumentElementAvailable() {
LOG(INFO) << "Cobalt::PrimaryMainDocumentElementAvailable";
Expand Down
8 changes: 6 additions & 2 deletions cobalt/cobalt_web_contents_observer.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,18 @@
#include "content/public/browser/web_contents_observer.h"
#include "content/shell/browser/shell_content_browser_client.h"

#include "components/js_injection/browser/js_communication_host.h"

namespace cobalt {

class CobaltWebContentsObserver : public content::WebContentsObserver {
public:
explicit CobaltWebContentsObserver(content::WebContents* web_contents)
: content::WebContentsObserver(web_contents) {}
explicit CobaltWebContentsObserver(content::WebContents* web_contents);

void PrimaryMainDocumentElementAvailable() override;

private:
std::unique_ptr<js_injection::JsCommunicationHost> js_communication_host_;
};

} // namespace cobalt
Expand Down
1 change: 1 addition & 0 deletions cobalt/renderer/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ source_set("renderer") {

deps = [
"//components/cdm/renderer",
"//components/js_injection/renderer:renderer",
"//components/network_hints/renderer",
"//components/web_cache/renderer",
"//content/public/common",
Expand Down
12 changes: 11 additions & 1 deletion cobalt/renderer/cobalt_content_renderer_client.cc
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,9 @@ void CobaltContentRendererClient::ExposeInterfacesToBrowser(
}

void CobaltContentRendererClient::RenderFrameCreated(
content::RenderFrame* render_frame) {}
content::RenderFrame* render_frame) {
new js_injection::JsCommunication(render_frame);
}

void CobaltContentRendererClient::PrepareErrorPage(
content::RenderFrame* render_frame,
Expand Down Expand Up @@ -285,6 +287,14 @@ bool CobaltContentRendererClient::IsSupportedVideoType(
}
#endif // BUILDFLAG(USE_STARBOARD_MEDIA)

void CobaltContentRendererClient::RunScriptsAtDocumentStart(
content::RenderFrame* render_frame) {
LOG(WARNING) << "Should run scripts here";
js_injection::JsCommunication* communication =
js_injection::JsCommunication::Get(render_frame);
communication->RunScriptsAtDocumentStart();
}

std::unique_ptr<blink::WebPrescientNetworking>
CobaltContentRendererClient::CreatePrescientNetworking(
content::RenderFrame* render_frame) {
Expand Down
5 changes: 5 additions & 0 deletions cobalt/renderer/cobalt_content_renderer_client.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
// For BUILDFLAG(USE_STARBOARD_MEDIA)
#include "build/build_config.h"

#include "components/js_injection/renderer/js_communication.h"

namespace blink {
class URLLoaderThrottleProvider;
enum class URLLoaderThrottleProviderType;
Expand Down Expand Up @@ -67,6 +69,9 @@ class CobaltContentRendererClient : public content::ContentRendererClient {
bool IsSupportedVideoType(const media::VideoType& type) override;
#endif // BUILDFLAG(USE_STARBOARD_MEDIA)

// JS Injection hook
void RunScriptsAtDocumentStart(content::RenderFrame* render_frame) override;

std::unique_ptr<blink::WebPrescientNetworking> CreatePrescientNetworking(
content::RenderFrame* render_frame) override;

Expand Down

0 comments on commit e1b3191

Please sign in to comment.