Skip to content

Commit

Permalink
Extract helper functions out of brave_content_settings_pref_provider.cc
Browse files Browse the repository at this point in the history
The helper functions to retrieve a timestamp and a SessionModel value
out of a DictionaryValue will also be useful from the unit tests we
will include, so let's extract this into brave_content_settings_utils.
  • Loading branch information
mariospr committed Dec 22, 2020
1 parent 73f8f88 commit 1456884
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 51 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -45,52 +45,6 @@ const char kSessionModelPath[] = "model";
const char kSettingPath[] = "setting";
const char kPerResourcePath[] = "per_resource";

std::string GetShieldsPreferenceName(const std::string& name) {
return std::string("profile.content_settings.exceptions.").append(name);
}

// Extract a timestamp from |dictionary[kLastModifiedPath]|.
// Will return base::Time() if no timestamp exists.
base::Time GetTimeStamp(const base::DictionaryValue* dictionary) {
std::string timestamp_str;
dictionary->GetStringWithoutPathExpansion(kLastModifiedPath, &timestamp_str);
int64_t timestamp = 0;
base::StringToInt64(timestamp_str, &timestamp);
base::Time last_modified = base::Time::FromDeltaSinceWindowsEpoch(
base::TimeDelta::FromMicroseconds(timestamp));
return last_modified;
}

// Extract a timestamp from |dictionary[kExpirationPath]|. Will return
// base::Time() if no timestamp exists.
base::Time GetExpiration(const base::DictionaryValue* dictionary) {
std::string expiration_timestamp_str;
dictionary->GetStringWithoutPathExpansion(kExpirationPath,
&expiration_timestamp_str);
int64_t expiration_timestamp = 0;
base::StringToInt64(expiration_timestamp_str, &expiration_timestamp);
base::Time expiration = base::Time::FromDeltaSinceWindowsEpoch(
base::TimeDelta::FromMicroseconds(expiration_timestamp));
return expiration;
}

// Extract a SessionModel from |dictionary[kSessionModelPath]|. Will return
// SessionModel::Durable if no model exists.
content_settings::SessionModel GetSessionModel(
const base::DictionaryValue* dictionary) {
int model_int = 0;
dictionary->GetIntegerWithoutPathExpansion(kSessionModelPath, &model_int);
if ((model_int >
static_cast<int>(content_settings::SessionModel::kMaxValue)) ||
(model_int < 0)) {
model_int = 0;
}

content_settings::SessionModel session_model =
static_cast<content_settings::SessionModel>(model_int);
return session_model;
}

Rule CloneRule(const Rule& rule, bool reverse_patterns = false) {
// brave plugin rules incorrectly use first party url as primary
auto primary_pattern = reverse_patterns ? rule.secondary_pattern
Expand Down Expand Up @@ -236,13 +190,16 @@ void BravePrefProvider::MigrateShieldsSettingsFromResourceIds() {
bool is_dictionary = i.value().GetAsDictionary(&settings_dictionary);
DCHECK(is_dictionary);

base::Time expiration = GetExpiration(settings_dictionary);
SessionModel session_model = GetSessionModel(settings_dictionary);
base::Time expiration = GetTimeStampFromDictionary(settings_dictionary,
kExpirationPath);
SessionModel session_model = GetSessionModelFromDictionary(
settings_dictionary, kSessionModelPath);

const base::DictionaryValue* resource_dictionary = nullptr;
if (settings_dictionary->GetDictionary(kPerResourcePath,
&resource_dictionary)) {
base::Time last_modified = GetTimeStamp(settings_dictionary);
base::Time last_modified = GetTimeStampFromDictionary(settings_dictionary,
kLastModifiedPath);
for (base::DictionaryValue::Iterator j(*resource_dictionary);
!j.IsAtEnd(); j.Advance()) {
const std::string& resource_identifier(j.key());
Expand All @@ -262,7 +219,7 @@ void BravePrefProvider::MigrateShieldsSettingsFromResourceIds() {
actual_name = resource_identifier;

MigrateShieldsSettingsFromResourceIdsForOneType(
GetShieldsPreferenceName(actual_name), patterns_string,
GetShieldsSettingUserPrefsPath(actual_name), patterns_string,
expiration, last_modified, session_model, setting);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@
#include <algorithm>

#include "base/notreached.h"
#include "base/strings/string_number_conversions.h"
#include "base/optional.h"
#include "base/values.h"
#include "brave/components/brave_shields/common/brave_shield_constants.h"
#include "url/gurl.h"

Expand Down Expand Up @@ -111,4 +113,40 @@ base::Optional<ContentSettingsPattern> ConvertPatternToWildcardSchemeAndPort(
return new_pattern;
}

// Returns the full path in the user preferences store to the Brave Shields
// setting identified by it's name (i.e. |name|).
std::string GetShieldsSettingUserPrefsPath(const std::string& name) {
return std::string("profile.content_settings.exceptions.").append(name);
}

// Extract a timestamp from |dictionary[key]|. Will return base::Time() if no
// timestamp exists.
base::Time GetTimeStampFromDictionary(
const base::DictionaryValue* dictionary, const char* key) {
std::string timestamp_str;
dictionary->GetStringWithoutPathExpansion(key, &timestamp_str);
int64_t timestamp = 0;
base::StringToInt64(timestamp_str, &timestamp);
base::Time last_modified = base::Time::FromDeltaSinceWindowsEpoch(
base::TimeDelta::FromMicroseconds(timestamp));
return last_modified;
}

// Extract a SessionModel from |dictionary[key]|. Will return
// SessionModel::Durable if no model exists.
content_settings::SessionModel GetSessionModelFromDictionary(
const base::DictionaryValue* dictionary, const char* key) {
int model_int = 0;
dictionary->GetIntegerWithoutPathExpansion(key, &model_int);
if ((model_int >
static_cast<int>(content_settings::SessionModel::kMaxValue)) ||
(model_int < 0)) {
model_int = 0;
}

content_settings::SessionModel session_model =
static_cast<content_settings::SessionModel>(model_int);
return session_model;
}

} // namespace content_settings
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,10 @@
#include <string>
#include <vector>

#include "components/content_settings/core/common/content_settings.h"
#include "base/time/time.h"
#include "components/content_settings/core/common/content_settings_constraints.h"
#include "components/content_settings/core/common/content_settings_types.h"
#include "components/content_settings/core/common/content_settings.h"

namespace content_settings {

Expand All @@ -23,6 +25,14 @@ bool IsShieldsContentSettingsType(const ContentSettingsType& content_type);
base::Optional<ContentSettingsPattern> ConvertPatternToWildcardSchemeAndPort(
const ContentSettingsPattern& pattern);

std::string GetShieldsSettingUserPrefsPath(const std::string& name);

base::Time GetTimeStampFromDictionary(
const base::DictionaryValue* dictionary, const char* key);

content_settings::SessionModel GetSessionModelFromDictionary(
const base::DictionaryValue* dictionary, const char* key);

} // namespace content_settings

#endif // BRAVE_COMPONENTS_CONTENT_SETTINGS_CORE_BROWSER_BRAVE_CONTENT_SETTINGS_UTILS_H_

0 comments on commit 1456884

Please sign in to comment.