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

Demo javascript injection with js_injection component #4588

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
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
4 changes: 4 additions & 0 deletions content/shell/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -289,6 +289,10 @@ static_library("content_shell_lib") {
"//components/variations",
"//components/variations/service",
"//components/web_cache/renderer",

"//components/js_injection/browser:browser",
"//components/js_injection/renderer:renderer",

"//content:content_resources",
"//content:dev_ui_content_resources",
"//content/common:main_frame_counter",
Expand Down
18 changes: 18 additions & 0 deletions content/shell/browser/shell.cc
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,19 @@ Shell::Shell(std::unique_ptr<WebContents> web_contents,

windows_.push_back(this);

// Quick hack to demo injecting scripts
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This code doesn't have to be in content/shell, can be moved to /cobalt


// Create browser-side mojo service component
js_communication_host_ =
std::make_unique<js_injection::JsCommunicationHost>(web_contents_.get());

// 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);
// End inject

if (shell_created_callback_)
std::move(shell_created_callback_).Run(this);
}
Expand Down Expand Up @@ -766,4 +779,9 @@ void Shell::TitleWasSet(NavigationEntry* entry) {
g_platform->SetTitle(this, entry->GetTitle());
}

void Shell::PrimaryMainDocumentElementAvailable() {
LOG(WARNING) << "Primary doc element created";
}


} // namespace content
6 changes: 6 additions & 0 deletions content/shell/browser/shell.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
#include "ui/gfx/geometry/size.h"
#include "ui/gfx/native_widget_types.h"

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

class GURL;

namespace content {
Expand Down Expand Up @@ -226,6 +228,8 @@ class Shell : public WebContentsDelegate, public WebContentsObserver {
void PrimaryPageChanged(Page& page) override;
#endif

void PrimaryMainDocumentElementAvailable() override;

std::unique_ptr<JavaScriptDialogManager> dialog_manager_;

std::unique_ptr<WebContents> web_contents_;
Expand All @@ -243,6 +247,8 @@ class Shell : public WebContentsDelegate, public WebContentsObserver {
static std::vector<Shell*> windows_;

static base::OnceCallback<void(Shell*)> shell_created_callback_;

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

} // namespace content
Expand Down
10 changes: 10 additions & 0 deletions content/shell/renderer/shell_content_renderer_client.cc
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@
#include "components/cdm/renderer/widevine_key_system_info.h"
#endif // BUILDFLAG(USE_STARBOARD_MEDIA)

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

#if BUILDFLAG(ENABLE_PLUGINS)
#include "ppapi/shared_impl/ppapi_switches.h" // nogncheck
#endif
Expand Down Expand Up @@ -222,6 +224,7 @@ void ShellContentRendererClient::RenderFrameCreated(RenderFrame* render_frame) {
// browser tests. If we only create that for browser tests then the override
// of this method in WebTestContentRendererClient would not be needed.
new ShellRenderFrameObserver(render_frame);
new js_injection::JsCommunication(render_frame);
}

void ShellContentRendererClient::PrepareErrorPage(
Expand Down Expand Up @@ -414,4 +417,11 @@ ShellContentRendererClient::CreatePrescientNetworking(
render_frame);
}

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

} // namespace content
3 changes: 3 additions & 0 deletions content/shell/renderer/shell_content_renderer_client.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ class ShellContentRendererClient : public ContentRendererClient {
ShellContentRendererClient();
~ShellContentRendererClient() override;

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

// ContentRendererClient implementation.
void RenderThreadStarted() override;
void ExposeInterfacesToBrowser(mojo::BinderMap* binders) override;
Expand Down
Loading