Skip to content

Commit

Permalink
Temp commit
Browse files Browse the repository at this point in the history
  • Loading branch information
simoarpe committed Nov 20, 2024
1 parent 048ea6e commit a80f3bc
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 36 deletions.
76 changes: 44 additions & 32 deletions components/youtube_script_injector/browser/core/youtube_rule.cc
Original file line number Diff line number Diff line change
Expand Up @@ -22,24 +22,26 @@
namespace {

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

bool GetURLPatternSetFromValue(const base::Value* value,
extensions::URLPatternSet* result) {
if (!value->is_list()) {
return false;
}
std::string error;
bool valid = result->Populate(value->GetList(), URLPattern::SCHEME_HTTPS,
false, &error);
if (!valid) {
DVLOG(1) << error;
}
return valid;
}
constexpr char kYouTubeUrl[] = "https://youtube.com";

// bool GetURLPatternSetFromValue(const base::Value* value,
// extensions::URLPatternSet* result) {
// if (!value->is_list()) {
// return false;
// }
// std::string error;
// bool valid = result->Populate(value->GetList(), URLPattern::SCHEME_HTTPS,
// false, &error);
// if (!valid) {
// DVLOG(1) << error;
// }
// return valid;
// }

bool GetFilePathFromValue(const base::Value* value, base::FilePath* result) {
if (!value->is_string()) {
Expand All @@ -57,19 +59,19 @@ 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();
// include_pattern_set_ = other.include_pattern_set_.Clone();
// exclude_pattern_set_ = other.exclude_pattern_set_.Clone();
policy_script_path_ = other.policy_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<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);
converter->RegisterIntField(kVersion, &YouTubeRule::version_);
Expand Down Expand Up @@ -99,19 +101,29 @@ std::optional<std::vector<YouTubeRule>> YouTubeRule::ParseRules(
return rules;
}

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;
bool YouTubeRule::IsYouTubeDomain(const GURL& url) const {
if (net::registry_controlled_domains::SameDomainOrHost(
url, GURL(kYouTubeUrl),
net::registry_controlled_domains::INCLUDE_PRIVATE_REGISTRIES)) {
return true;
}

return true;
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 @@ -58,15 +58,16 @@ class YouTubeRule {
static std::optional<std::vector<YouTubeRule>> ParseRules(
const std::string& contents);
// Check if this rule matches the given URL.
bool ShouldInsertScript(const GURL& url) const;
// bool ShouldInsertScript(const GURL& url) const;
bool IsYouTubeDomain(const GURL& url) const;

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

private:
extensions::URLPatternSet include_pattern_set_;
extensions::URLPatternSet exclude_pattern_set_;
// 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_;
// Used for checking if the last inserted script is the latest version.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ void YouTubeRuleRegistry::CheckIfMatch(
const GURL& url,
base::OnceCallback<void(MatchedRule)> cb) const {
for (const YouTubeRule& rule : rules_) {
if (rule.ShouldInsertScript(url)) {
if (rule.IsYouTubeDomain(url)) {
base::ThreadPool::PostTaskAndReplyWithResult(
FROM_HERE, {base::MayBlock()},
base::BindOnce(&CreateMatchedRule, component_path_,
Expand Down

0 comments on commit a80f3bc

Please sign in to comment.