Skip to content

Commit

Permalink
Merge pull request #4653 from /issues/8244
Browse files Browse the repository at this point in the history
Copy cookie_rules_ because if anything uses PostTask it could be modi…
  • Loading branch information
bridiver authored Jun 16, 2020
2 parents a5ff39e + bdef295 commit bbd64f0
Showing 1 changed file with 30 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -57,22 +57,22 @@ Rule CloneRule(const Rule& rule, bool reverse_patterns = false) {

class BraveShieldsRuleIterator : public RuleIterator {
public:
BraveShieldsRuleIterator(std::vector<Rule>::const_iterator iterator,
std::vector<Rule>::const_iterator end)
: iterator_(iterator),
end_(end) {}
explicit BraveShieldsRuleIterator(std::vector<Rule> rules)
: rules_(std::move(rules)) {
iterator_ = rules_.begin();
}

bool HasNext() const override {
return iterator_ != end_;
return iterator_ != rules_.end();
}

Rule Next() override {
return CloneRule(*(iterator_++));
}

private:
std::vector<Rule> rules_;
std::vector<Rule>::const_iterator iterator_;
std::vector<Rule>::const_iterator end_;

DISALLOW_COPY_AND_ASSIGN(BraveShieldsRuleIterator);
};
Expand Down Expand Up @@ -278,9 +278,14 @@ std::unique_ptr<RuleIterator> BravePrefProvider::GetRuleIterator(
const ResourceIdentifier& resource_identifier,
bool incognito) const {
if (content_type == ContentSettingsType::COOKIES) {
return std::make_unique<BraveShieldsRuleIterator>(
cookie_rules_.at(incognito).begin(),
cookie_rules_.at(incognito).end());
std::vector<Rule> rules;
for (auto i = cookie_rules_.at(incognito).begin();
i != cookie_rules_.at(incognito).end();
++i) {
rules.emplace_back(CloneRule(*i));
}

return std::make_unique<BraveShieldsRuleIterator>(std::move(rules));
}

// Early return. We don't store flash plugin setting in preference.
Expand Down Expand Up @@ -309,9 +314,10 @@ void BravePrefProvider::UpdateCookieRules(ContentSettingsType content_type,
if (prefs_->GetBoolean(kGoogleLoginControlType)) {
auto rule = Rule(ContentSettingsPattern::FromString(kGoogleOAuthPattern),
ContentSettingsPattern::Wildcard(),
ContentSettingToValue(CONTENT_SETTING_ALLOW)->Clone());
rules.push_back(CloneRule(rule));
brave_cookie_rules_[incognito].push_back(CloneRule(rule));
base::Value::FromUniquePtrValue(
ContentSettingToValue(CONTENT_SETTING_ALLOW)));
rules.emplace_back(CloneRule(rule));
brave_cookie_rules_[incognito].emplace_back(CloneRule(rule));
}
// non-pref based exceptions should go in the cookie_settings_base.cc
// chromium_src override
Expand All @@ -322,7 +328,7 @@ void BravePrefProvider::UpdateCookieRules(ContentSettingsType content_type,
"",
incognito);
while (chromium_cookies_iterator && chromium_cookies_iterator->HasNext()) {
rules.push_back(CloneRule(chromium_cookies_iterator->Next()));
rules.emplace_back(CloneRule(chromium_cookies_iterator->Next()));
}
chromium_cookies_iterator.reset();

Expand All @@ -334,7 +340,7 @@ void BravePrefProvider::UpdateCookieRules(ContentSettingsType content_type,
// collect shield rules
std::vector<Rule> shield_rules;
while (brave_shields_iterator && brave_shields_iterator->HasNext()) {
shield_rules.push_back(CloneRule(brave_shields_iterator->Next()));
shield_rules.emplace_back(CloneRule(brave_shields_iterator->Next()));
}

brave_shields_iterator.reset();
Expand All @@ -349,8 +355,8 @@ void BravePrefProvider::UpdateCookieRules(ContentSettingsType content_type,
while (brave_cookies_iterator && brave_cookies_iterator->HasNext()) {
auto rule = brave_cookies_iterator->Next();
if (IsActive(rule, shield_rules)) {
rules.push_back(CloneRule(rule, true));
brave_cookie_rules_[incognito].push_back(CloneRule(rule, true));
rules.emplace_back(CloneRule(rule, true));
brave_cookie_rules_[incognito].emplace_back(CloneRule(rule, true));
}
}

Expand All @@ -362,14 +368,16 @@ void BravePrefProvider::UpdateCookieRules(ContentSettingsType content_type,

// Shields down.
if (ValueToContentSetting(&shield_rule.value) == CONTENT_SETTING_BLOCK) {
rules.push_back(
rules.emplace_back(
Rule(ContentSettingsPattern::Wildcard(),
shield_rule.primary_pattern,
ContentSettingToValue(CONTENT_SETTING_ALLOW)->Clone()));
brave_cookie_rules_[incognito].push_back(
base::Value::FromUniquePtrValue(
ContentSettingToValue(CONTENT_SETTING_ALLOW))));
brave_cookie_rules_[incognito].emplace_back(
Rule(ContentSettingsPattern::Wildcard(),
shield_rule.primary_pattern,
ContentSettingToValue(CONTENT_SETTING_ALLOW)->Clone()));
base::Value::FromUniquePtrValue(
ContentSettingToValue(CONTENT_SETTING_ALLOW))));
}
}

Expand All @@ -388,7 +396,7 @@ void BravePrefProvider::UpdateCookieRules(ContentSettingsType content_type,
ValueToContentSetting(&old_rule.value);
});
if (match == old_rules.end()) {
brave_cookie_updates.push_back(CloneRule(new_rule));
brave_cookie_updates.emplace_back(CloneRule(new_rule));
}
}

Expand All @@ -404,7 +412,7 @@ void BravePrefProvider::UpdateCookieRules(ContentSettingsType content_type,
new_rule.secondary_pattern == old_rule.secondary_pattern;
});
if (match == brave_cookie_rules_[incognito].end()) {
brave_cookie_updates.push_back(
brave_cookie_updates.emplace_back(
Rule(old_rule.primary_pattern,
old_rule.secondary_pattern,
base::Value()));
Expand Down

0 comments on commit bbd64f0

Please sign in to comment.