OLD | NEW |
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
| 5 #include "chrome/common/safe_browsing/file_type_policies.h" |
| 6 |
5 #include "base/logging.h" | 7 #include "base/logging.h" |
| 8 #include "base/memory/singleton.h" |
6 #include "base/metrics/sparse_histogram.h" | 9 #include "base/metrics/sparse_histogram.h" |
7 #include "base/strings/string_util.h" | 10 #include "base/strings/string_util.h" |
8 #include "chrome/common/safe_browsing/download_protection_util.h" | |
9 #include "chrome/common/safe_browsing/file_type_policies.h" | |
10 #include "chrome/grit/browser_resources.h" | 11 #include "chrome/grit/browser_resources.h" |
11 #include "chrome/grit/generated_resources.h" | 12 #include "chrome/grit/generated_resources.h" |
12 #include "ui/base/resource/resource_bundle.h" | 13 #include "ui/base/resource/resource_bundle.h" |
13 | 14 |
14 namespace safe_browsing { | 15 namespace safe_browsing { |
15 | 16 |
| 17 using base::AutoLock; |
| 18 |
| 19 // Our Singleton needs to populate itself when first constructed. |
| 20 // This is left out of the constructor to make testing simpler. |
| 21 struct FileTypePoliciesSingletonTrait |
| 22 : public base::DefaultSingletonTraits<FileTypePolicies> { |
| 23 static FileTypePolicies* New() { |
| 24 FileTypePolicies* instance = new FileTypePolicies(); |
| 25 instance->PopulateFromResourceBundle(); |
| 26 return instance; |
| 27 } |
| 28 }; |
| 29 |
| 30 // --- FileTypePolicies methods --- |
| 31 |
| 32 // static |
| 33 FileTypePolicies* FileTypePolicies::GetInstance() { |
| 34 return base::Singleton<FileTypePolicies, |
| 35 FileTypePoliciesSingletonTrait>::get(); |
| 36 } |
| 37 |
16 FileTypePolicies::FileTypePolicies() { | 38 FileTypePolicies::FileTypePolicies() { |
17 // Setup a file-type policy to use if the ResourceBundle is unreadable. | 39 // Setup a file-type policy to use if the ResourceBundle is unreadable. |
18 // This should normally never be used. | 40 // This should normally never be used. |
19 last_resort_default_.set_uma_value(-1l); | 41 last_resort_default_.set_uma_value(-1l); |
20 last_resort_default_.set_ping_setting(DownloadFileType::NO_PING); | 42 last_resort_default_.set_ping_setting(DownloadFileType::NO_PING); |
21 auto settings = last_resort_default_.add_platform_settings(); | 43 auto settings = last_resort_default_.add_platform_settings(); |
22 settings->set_danger_level(DownloadFileType::ALLOW_ON_USER_GESTURE); | 44 settings->set_danger_level(DownloadFileType::ALLOW_ON_USER_GESTURE); |
23 settings->set_auto_open_hint(DownloadFileType::DISALLOW_AUTO_OPEN); | 45 settings->set_auto_open_hint(DownloadFileType::DISALLOW_AUTO_OPEN); |
24 } | 46 } |
25 | 47 |
26 FileTypePolicies::~FileTypePolicies() {} | 48 FileTypePolicies::~FileTypePolicies() { |
| 49 AutoLock lock(lock_); // DCHECK fail if the lock is held. |
| 50 } |
27 | 51 |
28 void FileTypePolicies::ReadResourceBundle(std::string* binary_pb) { | 52 void FileTypePolicies::ReadResourceBundle(std::string* binary_pb) { |
29 ui::ResourceBundle& bundle = ui::ResourceBundle::GetSharedInstance(); | 53 ui::ResourceBundle& bundle = ui::ResourceBundle::GetSharedInstance(); |
30 bundle.GetRawDataResource(IDR_DOWNLOAD_FILE_TYPES_PB).CopyToString(binary_pb); | 54 bundle.GetRawDataResource(IDR_DOWNLOAD_FILE_TYPES_PB).CopyToString(binary_pb); |
31 } | 55 } |
32 | 56 |
33 void FileTypePolicies::RecordUpdateMetrics(UpdateResult result, | 57 void FileTypePolicies::RecordUpdateMetrics(UpdateResult result, |
34 const std::string& src_name) { | 58 const std::string& src_name) { |
| 59 lock_.AssertAcquired(); |
35 // src_name should be "ResourceBundle" or "DynamicUpdate". | 60 // src_name should be "ResourceBundle" or "DynamicUpdate". |
36 UMA_HISTOGRAM_SPARSE_SLOWLY( | 61 UMA_HISTOGRAM_SPARSE_SLOWLY( |
37 "SafeBrowsing.FileTypeUpdate." + src_name + "Result", | 62 "SafeBrowsing.FileTypeUpdate." + src_name + "Result", |
38 static_cast<unsigned int>(result)); | 63 static_cast<unsigned int>(result)); |
39 | 64 |
40 if (result == UpdateResult::SUCCESS) { | 65 if (result == UpdateResult::SUCCESS) { |
41 UMA_HISTOGRAM_SPARSE_SLOWLY( | 66 UMA_HISTOGRAM_SPARSE_SLOWLY( |
42 "SafeBrowsing.FileTypeUpdate." + src_name + "Version", | 67 "SafeBrowsing.FileTypeUpdate." + src_name + "Version", |
43 config_->version_id()); | 68 config_->version_id()); |
44 UMA_HISTOGRAM_SPARSE_SLOWLY( | 69 UMA_HISTOGRAM_SPARSE_SLOWLY( |
45 "SafeBrowsing.FileTypeUpdate." + src_name + "TypeCount", | 70 "SafeBrowsing.FileTypeUpdate." + src_name + "TypeCount", |
46 config_->file_types().size()); | 71 config_->file_types().size()); |
47 } | 72 } |
48 } | 73 } |
49 | 74 |
50 void FileTypePolicies::PopulateFromResourceBundle() { | 75 void FileTypePolicies::PopulateFromResourceBundle() { |
| 76 AutoLock lock(lock_); |
51 std::string binary_pb; | 77 std::string binary_pb; |
52 ReadResourceBundle(&binary_pb); | 78 ReadResourceBundle(&binary_pb); |
53 UpdateResult result = PopulateFromBinaryPb(binary_pb); | 79 UpdateResult result = PopulateFromBinaryPb(binary_pb); |
54 RecordUpdateMetrics(result, "ResourceBundle"); | 80 RecordUpdateMetrics(result, "ResourceBundle"); |
55 } | 81 } |
56 | 82 |
57 void FileTypePolicies::PopulateFromDynamicUpdate(const std::string& binary_pb) { | 83 void FileTypePolicies::PopulateFromDynamicUpdate(const std::string& binary_pb) { |
| 84 AutoLock lock(lock_); |
58 UpdateResult result = PopulateFromBinaryPb(binary_pb); | 85 UpdateResult result = PopulateFromBinaryPb(binary_pb); |
59 RecordUpdateMetrics(result, "DynamicUpdate"); | 86 RecordUpdateMetrics(result, "DynamicUpdate"); |
60 } | 87 } |
61 | 88 |
62 FileTypePolicies::UpdateResult FileTypePolicies::PopulateFromBinaryPb( | 89 FileTypePolicies::UpdateResult FileTypePolicies::PopulateFromBinaryPb( |
63 const std::string& binary_pb) { | 90 const std::string& binary_pb) { |
| 91 lock_.AssertAcquired(); |
| 92 |
64 // Parse the proto and do some validation on it. | 93 // Parse the proto and do some validation on it. |
65 if (binary_pb.empty()) | 94 if (binary_pb.empty()) |
66 return UpdateResult::FAILED_EMPTY; | 95 return UpdateResult::FAILED_EMPTY; |
67 | 96 |
68 std::unique_ptr<DownloadFileTypeConfig> new_config( | 97 std::unique_ptr<DownloadFileTypeConfig> new_config( |
69 new DownloadFileTypeConfig); | 98 new DownloadFileTypeConfig); |
70 if (!new_config->ParseFromString(binary_pb)) | 99 if (!new_config->ParseFromString(binary_pb)) |
71 return UpdateResult::FAILED_PROTO_PARSE; | 100 return UpdateResult::FAILED_PROTO_PARSE; |
72 | 101 |
73 // Need at least a default setting. | 102 // Need at least a default setting. |
(...skipping 25 matching lines...) Expand all Loading... |
99 file_type_by_ext_.clear(); | 128 file_type_by_ext_.clear(); |
100 for (const DownloadFileType& file_type : config_->file_types()) { | 129 for (const DownloadFileType& file_type : config_->file_types()) { |
101 // If there are dups, first one wins. | 130 // If there are dups, first one wins. |
102 file_type_by_ext_.insert(std::make_pair(file_type.extension(), &file_type)); | 131 file_type_by_ext_.insert(std::make_pair(file_type.extension(), &file_type)); |
103 } | 132 } |
104 | 133 |
105 return UpdateResult::SUCCESS; | 134 return UpdateResult::SUCCESS; |
106 } | 135 } |
107 | 136 |
108 float FileTypePolicies::SampledPingProbability() const { | 137 float FileTypePolicies::SampledPingProbability() const { |
| 138 AutoLock lock(lock_); |
109 return config_ ? config_->sampled_ping_probability() : 0.0; | 139 return config_ ? config_->sampled_ping_probability() : 0.0; |
110 } | 140 } |
111 | 141 |
112 // static | 142 // static |
| 143 base::FilePath::StringType FileTypePolicies::GetFileExtension( |
| 144 const base::FilePath& file) { |
| 145 // Remove trailing space and period characters from the extension. |
| 146 base::FilePath::StringType file_basename = file.BaseName().value(); |
| 147 base::FilePath::StringPieceType trimmed_filename = base::TrimString( |
| 148 file_basename, FILE_PATH_LITERAL(". "), base::TRIM_TRAILING); |
| 149 return base::FilePath(trimmed_filename).FinalExtension(); |
| 150 } |
| 151 |
| 152 // static |
113 std::string FileTypePolicies::CanonicalizedExtension( | 153 std::string FileTypePolicies::CanonicalizedExtension( |
114 const base::FilePath& file) { | 154 const base::FilePath& file) { |
115 // The policy list is all ASCII, so a non-ASCII extension won't be in it. | 155 // The policy list is all ASCII, so a non-ASCII extension won't be in it. |
116 const base::FilePath::StringType ext = | 156 const base::FilePath::StringType ext = GetFileExtension(file); |
117 download_protection_util::GetFileExtension(file); | |
118 std::string ascii_ext = | 157 std::string ascii_ext = |
119 base::ToLowerASCII(base::FilePath(ext).MaybeAsASCII()); | 158 base::ToLowerASCII(base::FilePath(ext).MaybeAsASCII()); |
120 if (ascii_ext[0] == '.') | 159 if (ascii_ext[0] == '.') |
121 ascii_ext.erase(0, 1); | 160 ascii_ext.erase(0, 1); |
122 return ascii_ext; | 161 return ascii_ext; |
123 } | 162 } |
124 | 163 |
125 const DownloadFileType& FileTypePolicies::PolicyForFile( | 164 const DownloadFileType& FileTypePolicies::PolicyForExtension( |
126 const base::FilePath& file) { | 165 const std::string& ascii_ext) const { |
| 166 lock_.AssertAcquired(); |
127 // This could happen if the ResourceBundle is corrupted. | 167 // This could happen if the ResourceBundle is corrupted. |
128 if (!config_) { | 168 if (!config_) { |
129 DCHECK(false); | 169 DCHECK(false); |
130 return last_resort_default_; | 170 return last_resort_default_; |
131 } | 171 } |
132 | |
133 std::string ascii_ext = CanonicalizedExtension(file); | |
134 auto itr = file_type_by_ext_.find(ascii_ext); | 172 auto itr = file_type_by_ext_.find(ascii_ext); |
135 if (itr != file_type_by_ext_.end()) | 173 if (itr != file_type_by_ext_.end()) |
136 return *itr->second; | 174 return *itr->second; |
137 else | 175 else |
138 return config_->default_file_type(); | 176 return config_->default_file_type(); |
139 } | 177 } |
140 | 178 |
141 const DownloadFileType::PlatformSettings& FileTypePolicies::SettingsForFile( | 179 DownloadFileType FileTypePolicies::PolicyForFile( |
142 const base::FilePath& file) { | 180 const base::FilePath& file) const { |
143 DCHECK_EQ(1, PolicyForFile(file).platform_settings().size()); | 181 const std::string ext = CanonicalizedExtension(file); |
144 return PolicyForFile(file).platform_settings(0); | 182 AutoLock lock(lock_); |
| 183 return PolicyForExtension(ext); |
145 } | 184 } |
146 | 185 |
147 int64_t FileTypePolicies::UmaValueForFile(const base::FilePath& file) { | 186 DownloadFileType::PlatformSettings FileTypePolicies::SettingsForFile( |
148 return PolicyForFile(file).uma_value(); | 187 const base::FilePath& file) const { |
| 188 const std::string ext = CanonicalizedExtension(file); |
| 189 AutoLock lock(lock_); |
| 190 DCHECK_EQ(1, PolicyForExtension(ext).platform_settings().size()); |
| 191 return PolicyForExtension(ext).platform_settings(0); |
149 } | 192 } |
150 | 193 |
151 bool FileTypePolicies::IsFileAnArchive(const base::FilePath& file) { | 194 int64_t FileTypePolicies::UmaValueForFile(const base::FilePath& file) const { |
152 return PolicyForFile(file).is_archive(); | 195 const std::string ext = CanonicalizedExtension(file); |
| 196 AutoLock lock(lock_); |
| 197 return PolicyForExtension(ext).uma_value(); |
| 198 } |
| 199 |
| 200 bool FileTypePolicies::IsArchiveFile(const base::FilePath& file) const { |
| 201 const std::string ext = CanonicalizedExtension(file); |
| 202 AutoLock lock(lock_); |
| 203 return PolicyForExtension(ext).is_archive(); |
| 204 } |
| 205 |
| 206 bool FileTypePolicies::IsCheckedBinaryFile(const base::FilePath& file) const { |
| 207 const std::string ext = CanonicalizedExtension(file); |
| 208 AutoLock lock(lock_); |
| 209 return PolicyForExtension(ext).ping_setting() == DownloadFileType::FULL_PING; |
153 } | 210 } |
154 | 211 |
155 } // namespace safe_browsing | 212 } // namespace safe_browsing |
OLD | NEW |