Skip to content

Commit

Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Clean up youtube script injector component
Browse files Browse the repository at this point in the history
Perform refactoring and remove unneeded test script parameters
simoarpe committed Nov 20, 2024
1 parent d37152f commit 048ea6e
Showing 6 changed files with 12 additions and 53 deletions.
Original file line number Diff line number Diff line change
@@ -47,28 +47,10 @@ YouTubeTabHelper::YouTubeTabHelper(content::WebContents* web_contents,

YouTubeTabHelper::~YouTubeTabHelper() = default;

void YouTubeTabHelper::OnTestScriptResult(
const std::string& policy_script,
const content::GlobalRenderFrameHostId& render_frame_host_id,
base::Value value) {
if (value.GetIfBool().value_or(false)) {
InsertScriptInPage(render_frame_host_id, policy_script, base::DoNothing());
}
}

void YouTubeTabHelper::InsertTestScript(
const content::GlobalRenderFrameHostId& render_frame_host_id,
MatchedRule rule) {
InsertScriptInPage(render_frame_host_id, rule.test_script,
base::BindOnce(&YouTubeTabHelper::OnTestScriptResult,
weak_factory_.GetWeakPtr(),
rule.policy_script, render_frame_host_id));
}

void YouTubeTabHelper::InsertScriptInPage(
const content::GlobalRenderFrameHostId& render_frame_host_id,
const std::string& script,
content::RenderFrameHost::JavaScriptResultCallback cb) {
MatchedRule rule) {
// InsertScriptInPage(render_frame_host_id, rule.policy_script);
content::RenderFrameHost* render_frame_host =
content::RenderFrameHost::FromID(render_frame_host_id);

@@ -78,9 +60,9 @@ void YouTubeTabHelper::InsertScriptInPage(
web_contents()->GetPrimaryMainFrame()->GetGlobalId()) {
GetRemote(render_frame_host)
->RequestAsyncExecuteScript(
world_id_, base::UTF8ToUTF16(script),
world_id_, base::UTF8ToUTF16(rule.policy_script),
blink::mojom::UserActivationOption::kDoNotActivate,
blink::mojom::PromiseResultOption::kAwait, std::move(cb));
blink::mojom::PromiseResultOption::kAwait, base::DoNothing());
} else {
VLOG(2) << "render_frame_host is invalid.";
return;
@@ -110,7 +92,7 @@ void YouTubeTabHelper::DidFinishNavigation(
web_contents()->GetPrimaryMainFrame()->GetGlobalId();

youtube_rule_registry_->CheckIfMatch(
url, base::BindOnce(&YouTubeTabHelper::InsertTestScript,
url, base::BindOnce(&YouTubeTabHelper::InsertScriptInPage,
weak_factory_.GetWeakPtr(), render_frame_host_id));
}

Original file line number Diff line number Diff line change
@@ -37,21 +37,10 @@ class COMPONENT_EXPORT(YOUTUBE_SCRIPT_INJECTOR_BROWSER_CONTENT) YouTubeTabHelper

private:
YouTubeTabHelper(content::WebContents*, const int32_t world_id);
// Called to insert both test script and policy script.
// Called to insert the YouTube script into the page.
void InsertScriptInPage(
const content::GlobalRenderFrameHostId& render_frame_host_id,
const std::string& script,
content::RenderFrameHost::JavaScriptResultCallback cb);
// Used to insert a YouTube test script into the page.
// The result is used to determine whether to insert the policy
// script in |OnTestScriptResult|.
void InsertTestScript(
const content::GlobalRenderFrameHostId& render_frame_host_id,
MatchedRule rule);
void OnTestScriptResult(
const std::string& policy_script,
const content::GlobalRenderFrameHostId& render_frame_host_id,
base::Value value);
mojo::AssociatedRemote<script_injector::mojom::ScriptInjector>& GetRemote(
content::RenderFrameHost* rfh);
friend class content::WebContentsUserData<YouTubeTabHelper>;
Original file line number Diff line number Diff line change
@@ -31,11 +31,9 @@ namespace {
// |_ manifest.json
// |_ youtube.json
// |_ scripts/
// |_ twitter/
// |_ test.js
// |_ keep-playing-audio/
// |_ policy.js
// |_ linkedin/
// |_ test.js
// |_ set-fullscreen/
// |_ policy.js
// See youtube_rule.cc for the format of youtube.json.

Original file line number Diff line number Diff line change
@@ -25,7 +25,6 @@ namespace {
constexpr char kInclude[] = "include";
constexpr char kExclude[] = "exclude";
constexpr char kVersion[] = "version";
constexpr char kTestScript[] = "test_script";
constexpr char kPolicyScript[] = "policy_script";

bool GetURLPatternSetFromValue(const base::Value* value,
@@ -60,7 +59,6 @@ YouTubeRule::~YouTubeRule() = default;
YouTubeRule::YouTubeRule(const YouTubeRule& other) {
include_pattern_set_ = other.include_pattern_set_.Clone();
exclude_pattern_set_ = other.exclude_pattern_set_.Clone();
test_script_path_ = other.test_script_path_;
policy_script_path_ = other.policy_script_path_;
version_ = other.version_;
}
@@ -72,8 +70,6 @@ void YouTubeRule::RegisterJSONConverter(
kInclude, &YouTubeRule::include_pattern_set_, GetURLPatternSetFromValue);
converter->RegisterCustomValueField<extensions::URLPatternSet>(
kExclude, &YouTubeRule::exclude_pattern_set_, GetURLPatternSetFromValue);
converter->RegisterCustomValueField<base::FilePath>(
kTestScript, &YouTubeRule::test_script_path_, GetFilePathFromValue);
converter->RegisterCustomValueField<base::FilePath>(
kPolicyScript, &YouTubeRule::policy_script_path_, GetFilePathFromValue);
converter->RegisterIntField(kVersion, &YouTubeRule::version_);
Original file line number Diff line number Diff line change
@@ -24,7 +24,6 @@ namespace youtube_script_injector {

// Holds the loaded script text when a rule is matched.
struct MatchedRule {
std::string test_script;
std::string policy_script;
int version;
};
@@ -38,11 +37,10 @@ struct MatchedRule {
// "exclude": [
// ],
// "version": 1,
// "test_script": "twitter/test.js",
// "policy_script": "twitter/policy.js"
// }, ...
// ]
// Note that "test_script" and "policy_script" give paths
// Note that "policy_script" gives a path
// relative to the component under scripts/
// This class describes a single rule in the youtube.json file.
class YouTubeRule {
@@ -64,15 +62,13 @@ class YouTubeRule {

// Getters.
const base::FilePath& GetPolicyScript() const { return policy_script_path_; }
const base::FilePath& GetTestScript() const { return test_script_path_; }
int GetVersion() const { return version_; }

private:
extensions::URLPatternSet include_pattern_set_;
extensions::URLPatternSet exclude_pattern_set_;
// These are paths (not contents!) relative to the component under scripts/.
// This is a path (not content) relative to the component under scripts/.
base::FilePath policy_script_path_;
base::FilePath test_script_path_;
// Used for checking if the last inserted script is the latest version.
int version_;
};
Original file line number Diff line number Diff line change
@@ -42,14 +42,12 @@ std::string ReadFile(const base::FilePath& file_path) {
}

MatchedRule CreateMatchedRule(const base::FilePath& component_path,
const base::FilePath& test_script_path,
const base::FilePath& policy_script_path,
const int version) {
auto prefix = base::FilePath(component_path).Append(kScriptsDir);
auto test_script = ReadFile(base::FilePath(prefix).Append(test_script_path));
auto policy_script =
ReadFile(base::FilePath(prefix).Append(policy_script_path));
return {test_script, policy_script, version};
return {policy_script, version};
}

} // namespace
@@ -75,7 +73,7 @@ void YouTubeRuleRegistry::CheckIfMatch(
base::ThreadPool::PostTaskAndReplyWithResult(
FROM_HERE, {base::MayBlock()},
base::BindOnce(&CreateMatchedRule, component_path_,
rule.GetTestScript(), rule.GetPolicyScript(),
rule.GetPolicyScript(),
rule.GetVersion()),
std::move(cb));
// Only ever find one matching rule.

0 comments on commit 048ea6e

Please sign in to comment.