Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "base/logging.h" | |
| 6 #include "base/metrics/sparse_histogram.h" | |
| 7 #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/generated_resources.h" | |
| 12 #include "ui/base/resource/resource_bundle.h" | |
| 13 | |
| 14 namespace safe_browsing { | |
| 15 | |
| 16 FileTypePolicies::FileTypePolicies() { | |
| 17 // Setup a file-type policy to use if the ResourceBundle is unreadable. | |
| 18 // This should normally never be used. | |
| 19 last_resort_default_.set_uma_value(99999); // TODO: Add this to xml. | |
| 20 auto settings = last_resort_default_.add_platform_settings(); | |
| 21 settings->set_danger_level(DownloadFileType::ALLOW_ON_USER_GESTURE); | |
| 22 settings->set_auto_open_hint(DownloadFileType::DISALLOW_AUTO_OPEN); | |
| 23 settings->set_ping_setting(DownloadFileType::NO_PING); | |
| 24 } | |
| 25 | |
| 26 FileTypePolicies::~FileTypePolicies() {} | |
| 27 | |
| 28 void FileTypePolicies::ReadResourceBundle(std::string* binary_pb) { | |
| 29 ui::ResourceBundle& bundle = ui::ResourceBundle::GetSharedInstance(); | |
| 30 bundle.GetRawDataResource(IDR_DOWNLOAD_FILE_TYPES_PB).CopyToString(binary_pb); | |
| 31 } | |
| 32 | |
| 33 void FileTypePolicies::RecordUpdateMetrics(UpdateResult result, | |
| 34 const std::string& src_name) { | |
| 35 // src_name should be "ResourceBundle" or "DynamicUpdate". | |
| 36 UMA_HISTOGRAM_SPARSE_SLOWLY( | |
| 37 "SafeBrowsing.FileTypeUpdate." + src_name + "Result", | |
| 38 static_cast<unsigned int>(result)); | |
| 39 | |
| 40 if (result == UpdateResult::SUCCESS) { | |
| 41 UMA_HISTOGRAM_SPARSE_SLOWLY( | |
| 42 "SafeBrowsing.FileTypeUpdate." + src_name + "Version", | |
| 43 config_->version_id()); | |
| 44 UMA_HISTOGRAM_SPARSE_SLOWLY( | |
| 45 "SafeBrowsing.FileTypeUpdate." + src_name + "TypeCount", | |
| 46 config_->file_types().size()); | |
| 47 } | |
| 48 } | |
| 49 | |
| 50 void FileTypePolicies::PopulateFromResourceBundle() { | |
| 51 std::string binary_pb; | |
| 52 ReadResourceBundle(&binary_pb); | |
| 53 UpdateResult result = PopulateFromBinaryPb(binary_pb); | |
| 54 RecordUpdateMetrics(result, "ResourceBundle"); | |
| 55 } | |
| 56 | |
| 57 void FileTypePolicies::PopulateFromDynamicUpdate(const std::string& binary_pb) { | |
| 58 UpdateResult result = PopulateFromBinaryPb(binary_pb); | |
| 59 RecordUpdateMetrics(result, "DynamicUpdate"); | |
| 60 } | |
| 61 | |
| 62 FileTypePolicies::UpdateResult FileTypePolicies::PopulateFromBinaryPb( | |
| 63 const std::string& binary_pb) { | |
| 64 // Parse the proto and do some validation on it. | |
| 65 if (binary_pb.empty()) | |
| 66 return UpdateResult::FAILED_EMPTY; | |
| 67 | |
| 68 std::unique_ptr<DownloadFileTypeConfig> new_config( | |
| 69 new DownloadFileTypeConfig); | |
| 70 if (!new_config->ParseFromString(binary_pb)) | |
| 71 return UpdateResult::FAILED_PROTO_PARSE; | |
| 72 | |
| 73 // Need at least a default setting. | |
| 74 if (new_config->default_file_type().platform_settings().size() == 0) | |
| 75 return UpdateResult::FAILED_DEFAULT_SETTING_SET; | |
| 76 | |
| 77 // Every file type should have exactly one setting, pre-filtered for this | |
| 78 // platform. | |
| 79 for (const auto& file_type : new_config->file_types()) { | |
| 80 if (file_type.platform_settings().size() != 1) | |
| 81 return UpdateResult::FAILED_WRONG_SETTINGS_COUNT; | |
| 82 } | |
| 83 | |
| 84 // Compare against existing config, if we have one. | |
| 85 if (config_) { | |
| 86 // Check that version number increases | |
| 87 if (new_config->version_id() <= config_->version_id()) | |
| 88 return UpdateResult::FAILED_VERSION_CHECK; | |
| 89 | |
| 90 // Check that we haven't dropped more than 1/2 the list. | |
|
Jialiu Lin
2016/04/22 21:36:41
Just curious, why 1/2 ? Any rationale behind check
Nathan Parker
2016/04/25 22:39:20
I picked 1/2 out of the air.
If someone screws
| |
| 91 if (new_config->file_types().size() * 2 < config_->file_types().size()) | |
| 92 return UpdateResult::FAILED_DELTA_CHECK; | |
| 93 } | |
| 94 | |
| 95 // Looks good. Update our internal list. | |
| 96 config_.reset(new_config.release()); | |
| 97 | |
| 98 // Build an index for faster lookup. | |
| 99 file_type_by_ext_.clear(); | |
| 100 for (const DownloadFileType& file_type : config_->file_types()) { | |
| 101 // If there are dups, first one wins. | |
| 102 file_type_by_ext_.insert(std::make_pair(file_type.extension(), &file_type)); | |
| 103 } | |
| 104 | |
| 105 return UpdateResult::SUCCESS; | |
| 106 } | |
| 107 | |
| 108 float FileTypePolicies::SampledPingProbability() const { | |
| 109 return config_ ? config_->sampled_ping_probability() : 0.0; | |
| 110 } | |
| 111 | |
| 112 // static | |
| 113 std::string FileTypePolicies::CanonicalizedExtension( | |
| 114 const base::FilePath& file) { | |
| 115 // The policy list is all ASCII, so a non-ASCII extension won't be in it. | |
| 116 const base::FilePath::StringType ext = | |
| 117 download_protection_util::GetFileExtension(file); | |
| 118 std::string ascii_ext = | |
| 119 base::ToLowerASCII(base::FilePath(ext).MaybeAsASCII()); | |
| 120 if (ascii_ext[0] == '.') | |
| 121 ascii_ext.erase(0, 1); | |
| 122 return ascii_ext; | |
| 123 } | |
| 124 | |
| 125 const DownloadFileType& FileTypePolicies::PolicyForFile( | |
| 126 const base::FilePath& file) { | |
| 127 // This could happen if the ResourceBundle is corrupted. | |
| 128 if (!config_) { | |
| 129 DCHECK(false); | |
| 130 return last_resort_default_; | |
| 131 } | |
| 132 | |
| 133 std::string ascii_ext = CanonicalizedExtension(file); | |
| 134 auto itr = file_type_by_ext_.find(ascii_ext); | |
| 135 if (itr != file_type_by_ext_.end()) | |
| 136 return *itr->second; | |
| 137 else | |
| 138 return config_->default_file_type(); | |
| 139 } | |
| 140 | |
| 141 const DownloadFileType::PlatformSettings& FileTypePolicies::SettingsForFile( | |
| 142 const base::FilePath& file) { | |
| 143 return PolicyForFile(file).platform_settings(0); | |
|
Jialiu Lin
2016/04/22 21:36:41
Maybe do a safety check on platform_settings() in
Nathan Parker
2016/04/25 22:39:20
It should have exactly one entry, per line 80 abov
Jialiu Lin
2016/04/25 23:26:47
Ah, you're totally right. line 80 already did the
| |
| 144 } | |
| 145 | |
| 146 int64_t FileTypePolicies::UmaValueForFile(const base::FilePath& file) { | |
| 147 return PolicyForFile(file).uma_value(); | |
| 148 } | |
| 149 | |
| 150 bool FileTypePolicies::IsFileAnArchive(const base::FilePath& file) { | |
| 151 return PolicyForFile(file).is_archive(); | |
| 152 } | |
| 153 | |
| 154 } // namespace safe_browsing | |
| OLD | NEW |