Skip to content

Commit

Permalink
Use embedded scripts
Browse files Browse the repository at this point in the history
  • Loading branch information
kaidokert committed Jan 16, 2025
1 parent fb78fdd commit 196445d
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 5 deletions.
23 changes: 23 additions & 0 deletions cobalt/android/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -234,13 +234,36 @@ template("content_shell_apk_tmpl") {
}
}

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

action("embed_polyfilled_javascript") {
script = "//cobalt/build/generate_data_header.py"

inputs = [
"embedded_resources/test.js",
"embedded_resources/test2.js",
]
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),
]
}

# Copied from target: libcontent_shell_content_view
# TODO(b/376867565) rename this library, NO shell
shared_library("libcobalt_content_shell_content_view") {
# TODO(b/375655377): remove testonly
testonly = true
deps = [
":content_shell_jni_headers",
":embed_polyfilled_javascript",
"//cobalt/renderer:renderer",
"//cobalt/user_agent",

Expand Down
1 change: 1 addition & 0 deletions cobalt/android/embedded_resources/test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
console.log("Hey i'm here - from embedded script");
1 change: 1 addition & 0 deletions cobalt/android/embedded_resources/test2.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
console.log("Another injected script");
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_
15 changes: 14 additions & 1 deletion cobalt/cobalt_web_contents_observer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,30 @@

#include "cobalt/cobalt_web_contents_observer.h"

#include "cobalt/android/embedded_resources/embedded_js.h"

#include "base/strings/utf_string_conversions.h"

namespace cobalt {

const char kEmbeddedJavascript[] = "test.js";

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

// Get the embedded header resource
GeneratedResourceMap resource_map;
CobaltJavaScriptPolyfill::GenerateMap(resource_map);
FileContents file_contents = resource_map[kEmbeddedJavascript];
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(u"console.log('Hello from JS injection');");
const std::u16string script(base::UTF8ToUTF16(js));
const std::vector<std::string> allowed_origins({"*"});
auto result = js_communication_host_->AddDocumentStartJavaScript(
script, allowed_origins);
Expand Down

0 comments on commit 196445d

Please sign in to comment.