Skip to content

Commit

Permalink
Single rule implementation refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
simoarpe committed Nov 20, 2024
1 parent a80f3bc commit 979a039
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 59 deletions.
46 changes: 10 additions & 36 deletions components/youtube_script_injector/browser/core/youtube_rule.cc
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,8 @@
namespace {

// youtube.json keys
// constexpr char kInclude[] = "include";
// constexpr char kExclude[] = "exclude";
constexpr char kVersion[] = "version";
constexpr char kPolicyScript[] = "policy_script";
constexpr char kFeatureScript[] = "feature_script";

constexpr char kYouTubeUrl[] = "https://youtube.com";

Expand Down Expand Up @@ -59,26 +57,20 @@ namespace youtube_script_injector {
YouTubeRule::YouTubeRule() = default;
YouTubeRule::~YouTubeRule() = default;
YouTubeRule::YouTubeRule(const YouTubeRule& other) {
// include_pattern_set_ = other.include_pattern_set_.Clone();
// exclude_pattern_set_ = other.exclude_pattern_set_.Clone();
policy_script_path_ = other.policy_script_path_;
feature_script_path_ = other.feature_script_path_;
version_ = other.version_;
}

// static
void YouTubeRule::RegisterJSONConverter(
base::JSONValueConverter<YouTubeRule>* converter) {
// converter->RegisterCustomValueField<extensions::URLPatternSet>(
// kInclude, &YouTubeRule::include_pattern_set_, GetURLPatternSetFromValue);
// converter->RegisterCustomValueField<extensions::URLPatternSet>(
// kExclude, &YouTubeRule::exclude_pattern_set_, GetURLPatternSetFromValue);
converter->RegisterCustomValueField<base::FilePath>(
kPolicyScript, &YouTubeRule::policy_script_path_, GetFilePathFromValue);
kFeatureScript, &YouTubeRule::feature_script_path_, GetFilePathFromValue);
converter->RegisterIntField(kVersion, &YouTubeRule::version_);
}

// static
std::optional<std::vector<YouTubeRule>> YouTubeRule::ParseRules(
std::optional<YouTubeRule> YouTubeRule::ParseRules(
const std::string& contents) {
if (contents.empty()) {
return std::nullopt;
Expand All @@ -88,17 +80,14 @@ std::optional<std::vector<YouTubeRule>> YouTubeRule::ParseRules(
VLOG(1) << "YouTubeRule::ParseRules: invalid JSON";
return std::nullopt;
}
std::vector<YouTubeRule> rules;

YouTubeRule rule = YouTubeRule();
base::JSONValueConverter<YouTubeRule> converter;
for (base::Value& it : root->GetList()) {
YouTubeRule rule = YouTubeRule();
if (!converter.Convert(it, &rule)) {
VLOG(1) << "YouTubeRule::ParseRules: invalid rule";
continue;
}
rules.emplace_back(rule);
if (!converter.Convert(*root, &rule)) {
VLOG(1) << "YouTubeRule::ParseRules: invalid rule";
return std::nullopt;
}
return rules;
return rule;
}

bool YouTubeRule::IsYouTubeDomain(const GURL& url) const {
Expand All @@ -111,19 +100,4 @@ bool YouTubeRule::IsYouTubeDomain(const GURL& url) const {
return false;
}

// bool YouTubeRule::ShouldInsertScript(const GURL& url) const {
// // If URL matches an explicitly excluded pattern, this rule does not
// // apply.
// if (exclude_pattern_set_.MatchesURL(url)) {
// return false;
// }
// // If URL does not match an explicitly included pattern, this rule does not
// // apply.
// if (!include_pattern_set_.MatchesURL(url)) {
// return false;
// }

// return true;
// }

} // namespace youtube_script_injector
Original file line number Diff line number Diff line change
Expand Up @@ -55,21 +55,21 @@ class YouTubeRule {
base::JSONValueConverter<YouTubeRule>* converter);

// Parse the youtube.json file contents into a vector of YouTubeRule.
static std::optional<std::vector<YouTubeRule>> ParseRules(
static std::optional<YouTubeRule> ParseRules(
const std::string& contents);
// Check if this rule matches the given URL.
// bool ShouldInsertScript(const GURL& url) const;
bool IsYouTubeDomain(const GURL& url) const;

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

private:
// extensions::URLPatternSet include_pattern_set_;
// extensions::URLPatternSet exclude_pattern_set_;
// This is a path (not content) relative to the component under scripts/.
base::FilePath policy_script_path_;
base::FilePath feature_script_path_;
// Used for checking if the last inserted script is the latest version.
int version_;
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,17 +68,13 @@ YouTubeRuleRegistry::~YouTubeRuleRegistry() = default;
void YouTubeRuleRegistry::CheckIfMatch(
const GURL& url,
base::OnceCallback<void(MatchedRule)> cb) const {
for (const YouTubeRule& rule : rules_) {
if (rule.IsYouTubeDomain(url)) {
base::ThreadPool::PostTaskAndReplyWithResult(
FROM_HERE, {base::MayBlock()},
base::BindOnce(&CreateMatchedRule, component_path_,
rule.GetPolicyScript(),
rule.GetVersion()),
std::move(cb));
// Only ever find one matching rule.
return;
}
if (rule_ && rule_->IsYouTubeDomain(url)) {
base::ThreadPool::PostTaskAndReplyWithResult(
FROM_HERE, {base::MayBlock()},
base::BindOnce(&CreateMatchedRule, component_path_,
rule_->GetFeatureScript(),
rule_->GetVersion()),
std::move(cb));
}
}

Expand All @@ -91,16 +87,12 @@ void YouTubeRuleRegistry::LoadRules(const base::FilePath& path) {
weak_factory_.GetWeakPtr()));
}

void YouTubeRuleRegistry::SetComponentPath(const base::FilePath& path) {
component_path_ = path;
void YouTubeRuleRegistry::OnLoadRules(const std::string& contents) {
rule_ = YouTubeRule::ParseRules(contents);
}

void YouTubeRuleRegistry::OnLoadRules(const std::string& contents) {
auto parsed_rules = YouTubeRule::ParseRules(contents);
if (!parsed_rules) {
return;
}
rules_ = std::move(parsed_rules.value());
void YouTubeRuleRegistry::SetComponentPath(const base::FilePath& path) {
component_path_ = path;
}

} // namespace youtube_script_injector
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ class COMPONENT_EXPORT(YOUTUBE_SCRIPT_INJECTOR_BROWSER_CORE) YouTubeRuleRegistry
void SetComponentPath(const base::FilePath& path);

base::FilePath component_path_;
std::vector<YouTubeRule> rules_;
std::optional<YouTubeRule> rule_;

base::WeakPtrFactory<YouTubeRuleRegistry> weak_factory_{this};

Expand Down

0 comments on commit 979a039

Please sign in to comment.