Skip to content

Commit

Permalink
Integrate js_injection and make java bridge polyfill script work (#4715)
Browse files Browse the repository at this point in the history
1. Incorporate Chromium's js_injection module into the Chrobalt codebase
to enable JavaScript injection capabilities.
2. Move the Kabuki polyfill scripts into cobalt/embedded_resources.
3. Utilize the embed_polyfilled_javascript GN rule to convert the
JavaScript files in cobalt/embedded_resources into embedded_js.h.
4. In the CobaltWebContentsObserver constructor, load the embedded
JavaScript from embedded_js.h into the GeneratedResourceMap. Use
AddDocumentStartJavaScript() to inject the polyfill code into web pages.
This ensures the polyfills are executed early in the page lifecycle.
5. Override the RunScriptsAtDocumentStart() method in
CobaltContentRendererClient to execute the injected polyfill JavaScript
code.

This PR is built on top of Kaido 's draft PR
#4704.

b/384742721

---------

Co-authored-by: Kaido Kert <[email protected]>
Co-authored-by: Colin Liang <[email protected]>
  • Loading branch information
3 people authored Jan 19, 2025
1 parent 454cedb commit 73018f4
Show file tree
Hide file tree
Showing 14 changed files with 96 additions and 33 deletions.
19 changes: 19 additions & 0 deletions cobalt/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,12 @@ if (!is_android) {
defines = []

deps = [
":embed_polyfilled_javascript",
"//cobalt/renderer:renderer",
"//cobalt/services/crash_annotator",
"//cobalt/services/crash_annotator/public/mojom",
"//cobalt/user_agent",
"//components/js_injection/browser:browser",
"//content/public/app",
"//content/shell:content_shell_app",
"//content/shell:content_shell_lib",
Expand Down Expand Up @@ -84,6 +86,23 @@ action("cobalt_build_info") {
]
}

config("embed_polyfilled_javascript_config") {
include_dirs = [ root_gen_dir ]
}

# TODO(b/390710539): migrate to GRIT
action("embed_polyfilled_javascript") {
script = "//cobalt/build/generate_data_header.py"
outputs = [ "$target_gen_dir/embedded_resources/embedded_js.h" ]
public_configs = [ ":embed_polyfilled_javascript_config" ]

args = [
"CobaltJavaScriptPolyfill",
rebase_path(outputs[0], root_build_dir),
rebase_path("embedded_resources", root_build_dir),
]
}

if (is_android) {
test("cobalt_unittests") {
testonly = true
Expand Down
2 changes: 2 additions & 0 deletions cobalt/android/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -241,13 +241,15 @@ shared_library("libcobalt_content_shell_content_view") {
testonly = true
deps = [
":content_shell_jni_headers",
"//cobalt:embed_polyfilled_javascript",
"//cobalt/renderer:renderer",
"//cobalt/services/crash_annotator",
"//cobalt/services/crash_annotator/public/mojom",
"//cobalt/user_agent",

# 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

This file was deleted.

This file was deleted.

This file was deleted.

8 changes: 4 additions & 4 deletions cobalt/base/generated_resources_types.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,16 @@

#include <string>

#include "base/containers/hash_tables.h"
#include <map>

struct FileContents {
FileContents() {}
FileContents(const unsigned char *data, int size) : data(data), size(size) {}
FileContents(const unsigned char* data, int size) : data(data), size(size) {}

const unsigned char *data;
const unsigned char* data;
int size;
};

typedef base::hash_map<std::string, FileContents> GeneratedResourceMap;
typedef std::map<std::string, FileContents> GeneratedResourceMap;

#endif // COBALT_BASE_GENERATED_RESOURCES_TYPES_H_
2 changes: 1 addition & 1 deletion cobalt/cobalt_content_browser_client.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,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;
void RegisterBrowserInterfaceBindersForFrame(
content::RenderFrameHost* render_frame_host,
mojo::BinderMapWithContext<content::RenderFrameHost*>* binder_map)
Expand Down
36 changes: 36 additions & 0 deletions cobalt/cobalt_web_contents_observer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,45 @@
// limitations under the License.

#include "cobalt/cobalt_web_contents_observer.h"
#include "base/strings/utf_string_conversions.h"
#include "cobalt/embedded_resources/embedded_js.h"

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);

RegisterInjectedJavaScript();
}

void CobaltWebContentsObserver::RegisterInjectedJavaScript() {
// Get the embedded header resource
GeneratedResourceMap resource_map;
CobaltJavaScriptPolyfill::GenerateMap(resource_map);

for (const auto& [file_name, file_contents] : resource_map) {
LOG(INFO) << "JS injection for filename: " << file_name;
std::string js(reinterpret_cast<const char*>(file_contents.data),
file_contents.size);

// Inject a script at document start for all origins
const std::u16string script(base::UTF8ToUTF16(js));
const std::vector<std::string> allowed_origins({"*"});
auto result = js_communication_host_->AddDocumentStartJavaScript(
script, allowed_origins);

if (result.error_message.has_value()) {
// error_message contains a value
LOG(WARNING) << "Failed to register JS injection for:" << file_name
<< ", error message: " << result.error_message.value();
}
}
}

// Placeholder for a WebContentsObserver override
void CobaltWebContentsObserver::PrimaryMainDocumentElementAvailable() {
LOG(INFO) << "Cobalt::PrimaryMainDocumentElementAvailable";
Expand Down
10 changes: 8 additions & 2 deletions cobalt/cobalt_web_contents_observer.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,20 @@
#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:
void RegisterInjectedJavaScript();

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

} // namespace cobalt
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ class PlatformServiceClient {
}
}

export function initializeH5vccPlatformService() {
function initializeH5vccPlatformService() {
if (typeof Android_H5vccPlatformService === 'undefined') {
return;
}
Expand Down Expand Up @@ -72,3 +72,5 @@ export function initializeH5vccPlatformService() {
},
}
}

initializeH5vccPlatformService();
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
/**
* @license
* Copyright The Cobalt Authors.
* SPDX-License-Identifier: Apache-2.0
*/
if (typeof HTMLMediaElementExtension !== 'undefined') {
HTMLMediaElement.prototype.canPlayType = HTMLMediaElementExtension.canPlayType;
}
1 change: 1 addition & 0 deletions cobalt/renderer/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ source_set("renderer") {
deps = [
"//cobalt/media/audio:webaudio",
"//components/cdm/renderer",
"//components/js_injection/renderer:renderer",
"//components/network_hints/renderer",
"//components/web_cache/renderer",
"//content/public/common",
Expand Down
11 changes: 10 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,13 @@ bool CobaltContentRendererClient::IsSupportedVideoType(
}
#endif // BUILDFLAG(USE_STARBOARD_MEDIA)

void CobaltContentRendererClient::RunScriptsAtDocumentStart(
content::RenderFrame* render_frame) {
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
4 changes: 4 additions & 0 deletions cobalt/renderer/cobalt_content_renderer_client.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

#include "build/build_config.h"
#include "cobalt/media/audio/cobalt_audio_device_factory.h"
#include "components/js_injection/renderer/js_communication.h"
#include "content/public/common/alternative_error_page_override_info.mojom-forward.h"
#include "content/public/renderer/content_renderer_client.h"
#include "media/mojo/buildflags.h"
Expand Down Expand Up @@ -65,6 +66,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 73018f4

Please sign in to comment.