Chromium Code Reviews| Index: chrome/common/safe_browsing/file_type_policies.cc |
| diff --git a/chrome/common/safe_browsing/file_type_policies.cc b/chrome/common/safe_browsing/file_type_policies.cc |
| index 1741b5a88de3e1ab7ba2d6fc3879bbca939b9460..fb0c1a096560ddcafc85d33445880d8e7fd811fe 100644 |
| --- a/chrome/common/safe_browsing/file_type_policies.cc |
| +++ b/chrome/common/safe_browsing/file_type_policies.cc |
| @@ -22,19 +22,30 @@ struct FileTypePoliciesSingletonTrait |
| : public base::DefaultSingletonTraits<FileTypePolicies> { |
| static FileTypePolicies* New() { |
| FileTypePolicies* instance = new FileTypePolicies(); |
| + // If you change this, you may want to update FileTypePoliciesTestOverlay. |
| instance->PopulateFromResourceBundle(); |
| return instance; |
| } |
| }; |
| // --- FileTypePolicies methods --- |
| +// |
| + |
| +FileTypePolicies* FileTypePolicies::instance_for_tests_ = 0; |
|
asanka
2016/06/20 18:15:46
Nit: nullptr
Nathan Parker
2016/06/20 22:47:31
Done.
|
| // static |
| FileTypePolicies* FileTypePolicies::GetInstance() { |
| + if (instance_for_tests_) |
| + return instance_for_tests_; |
| + |
| return base::Singleton<FileTypePolicies, |
| FileTypePoliciesSingletonTrait>::get(); |
| } |
| +void FileTypePolicies::SetInstanceForTests(FileTypePolicies* instance) { |
| + instance_for_tests_ = instance; |
| +} |
| + |
| FileTypePolicies::FileTypePolicies() { |
| // Setup a file-type policy to use if the ResourceBundle is unreadable. |
| // This should normally never be used. |
| @@ -128,22 +139,36 @@ FileTypePolicies::UpdateResult FileTypePolicies::PopulateFromBinaryPb( |
| // Looks good. Update our internal list. |
| config_.reset(new_config.release()); |
| + BuildIndex(); |
| + return UpdateResult::SUCCESS; |
| +} |
| + |
| +void FileTypePolicies::BuildIndex() { |
| + lock_.AssertAcquired(); |
| // Build an index for faster lookup. |
| file_type_by_ext_.clear(); |
| for (const DownloadFileType& file_type : config_->file_types()) { |
| // If there are dups, first one wins. |
| file_type_by_ext_.insert(std::make_pair(file_type.extension(), &file_type)); |
| } |
| +} |
| - return UpdateResult::SUCCESS; |
| +// For testing. |
| +void FileTypePolicies::SetConfigForTesting( |
| + const DownloadFileTypeConfig& new_config) { |
| + AutoLock lock(lock_); |
| + config_->CopyFrom(new_config); |
| + BuildIndex(); |
| } |
| -float FileTypePolicies::SampledPingProbability() const { |
| +// For testing. |
| +DownloadFileTypeConfig FileTypePolicies::GetConfigForTesting() const { |
| AutoLock lock(lock_); |
| - return config_ ? config_->sampled_ping_probability() : 0.0; |
| + return *config_; |
| } |
| + |
| // static |
| base::FilePath::StringType FileTypePolicies::GetFileExtension( |
| const base::FilePath& file) { |
| @@ -166,6 +191,15 @@ std::string FileTypePolicies::CanonicalizedExtension( |
| return ascii_ext; |
| } |
| +// |
| +// Accessors |
| +// |
| + |
| +float FileTypePolicies::SampledPingProbability() const { |
| + AutoLock lock(lock_); |
| + return config_ ? config_->sampled_ping_probability() : 0.0; |
| +} |
| + |
| const DownloadFileType& FileTypePolicies::PolicyForExtension( |
| const std::string& ascii_ext) const { |
| lock_.AssertAcquired(); |
| @@ -208,11 +242,7 @@ bool FileTypePolicies::IsArchiveFile(const base::FilePath& file) const { |
| return PolicyForExtension(ext).is_archive(); |
| } |
| -bool FileTypePolicies::IsCheckedBinaryFile(const base::FilePath& file) const { |
| - const std::string ext = CanonicalizedExtension(file); |
| - AutoLock lock(lock_); |
| - return PolicyForExtension(ext).ping_setting() == DownloadFileType::FULL_PING; |
| -} |
| +// TODO(nparker): Add unit tests for these accessors. |
| bool FileTypePolicies::IsAllowedToOpenAutomatically( |
| const base::FilePath& file) const { |
| @@ -224,6 +254,17 @@ bool FileTypePolicies::IsAllowedToOpenAutomatically( |
| DownloadFileType::ALLOW_AUTO_OPEN; |
| } |
| +DownloadFileType::PingSetting FileTypePolicies::PingSettingForFile( |
| + const base::FilePath& file) const { |
| + const std::string ext = CanonicalizedExtension(file); |
| + AutoLock lock(lock_); |
| + return PolicyForExtension(ext).ping_setting(); |
| +} |
| + |
| +bool FileTypePolicies::IsCheckedBinaryFile(const base::FilePath& file) const { |
| + return PingSettingForFile(file) == DownloadFileType::FULL_PING; |
| +} |
| + |
| DownloadFileType::DangerLevel FileTypePolicies::GetFileDangerLevel( |
| const base::FilePath& file) const { |
| const std::string ext = CanonicalizedExtension(file); |