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 #ifndef CHROME_COMMON_SAFE_BROWSING_FILE_TYPE_POLICIES_H_ |
| 6 #define CHROME_COMMON_SAFE_BROWSING_FILE_TYPE_POLICIES_H_ |
| 7 |
| 8 #include <map> |
| 9 #include <memory> |
| 10 |
| 11 #include "base/files/file_path.h" |
| 12 #include "base/gtest_prod_util.h" |
| 13 #include "chrome/common/safe_browsing/download_file_types.pb.h" |
| 14 |
| 15 namespace safe_browsing { |
| 16 |
| 17 // This holds a list of file types (aka file extensions) that we know about, |
| 18 // with policies related to how Safe Browsing and the download UI should treat |
| 19 // them. |
| 20 // |
| 21 // The data to populate it is read from a ResourceBundle and then also |
| 22 // fetched periodically from Google to get the most up-to-date policies. |
| 23 // |
| 24 // It should be setup and accessed on IO thread. |
| 25 |
| 26 // TODO(nparker): Replace the following methods' contents with calls to |
| 27 // g_browser_process->safe_browsing_service()->file_type_policies()->***. |
| 28 // |
| 29 // bool IsSupportedBinaryFile(const base::FilePath& file); |
| 30 // bool IsArchiveFile(const base::FilePath& file); |
| 31 // ClientDownloadRequest::DownloadType GetDownloadType( |
| 32 // const base::FilePath& file); |
| 33 // int GetSBClientDownloadTypeValueForUMA(const base::FilePath& file); |
| 34 // bool IsAllowedToOpenAutomatically(const base::FilePath& path); |
| 35 // DownloadDangerLevel GetFileDangerLevel(const base::FilePath& path); |
| 36 |
| 37 class FileTypePolicies { |
| 38 public: |
| 39 // Creator must call one of Populate* before calling other methods. |
| 40 FileTypePolicies(); |
| 41 virtual ~FileTypePolicies(); |
| 42 |
| 43 // Read data from the main ResourceBundle. This updates the internal list |
| 44 // only if the data passes integrity checks. This is normally called once |
| 45 // after construction. |
| 46 void PopulateFromResourceBundle(); |
| 47 |
| 48 // Update the internal list from a binary proto fetched from the network. |
| 49 // Same integrity checks apply. This can be called multiple times with new |
| 50 // protos. |
| 51 void PopulateFromDynamicUpdate(const std::string& binary_pb); |
| 52 |
| 53 // Accessors |
| 54 const DownloadFileType& PolicyForFile(const base::FilePath& file); |
| 55 const DownloadFileType::PlatformSettings& SettingsForFile( |
| 56 const base::FilePath& file); |
| 57 int64_t UmaValueForFile(const base::FilePath& file); |
| 58 bool IsFileAnArchive(const base::FilePath& file); |
| 59 float SampledPingProbability() const; |
| 60 |
| 61 protected: |
| 62 // Used in metrics, do not reorder. |
| 63 enum class UpdateResult { |
| 64 SUCCESS = 1, |
| 65 FAILED_EMPTY = 2, |
| 66 FAILED_PROTO_PARSE = 3, |
| 67 FAILED_DELTA_CHECK = 4, |
| 68 FAILED_VERSION_CHECK = 5, |
| 69 FAILED_DEFAULT_SETTING_SET = 6, |
| 70 FAILED_WRONG_SETTINGS_COUNT = 7, |
| 71 }; |
| 72 |
| 73 // Read data from an serialized protobuf and update the internal list |
| 74 // only if it passes integrity checks. |
| 75 virtual UpdateResult PopulateFromBinaryPb(const std::string& binary_pb); |
| 76 |
| 77 // Fetch the blob from the main resource bundle. |
| 78 virtual void ReadResourceBundle(std::string* binary_pb); |
| 79 |
| 80 // Record the result of an update attempt. |
| 81 virtual void RecordUpdateMetrics(UpdateResult result, |
| 82 const std::string& src_name); |
| 83 |
| 84 // Return the ASCII lowercase extension w/o leading dot, or empty. |
| 85 static std::string CanonicalizedExtension(const base::FilePath& file); |
| 86 |
| 87 private: |
| 88 // The latest config we've committed. Starts out null. |
| 89 std::unique_ptr<DownloadFileTypeConfig> config_; |
| 90 |
| 91 // This references entries in config_. |
| 92 std::map<std::string, const DownloadFileType*> file_type_by_ext_; |
| 93 |
| 94 // Type used if we can't load from disk. |
| 95 DownloadFileType last_resort_default_; |
| 96 |
| 97 FRIEND_TEST_ALL_PREFIXES(FileTypePoliciesTest, UnpackResourceBundle); |
| 98 FRIEND_TEST_ALL_PREFIXES(FileTypePoliciesTest, BadProto); |
| 99 FRIEND_TEST_ALL_PREFIXES(FileTypePoliciesTest, BadUpdateFromExisting); |
| 100 }; |
| 101 |
| 102 } // namespace safe_browsing |
| 103 |
| 104 #endif // CHROME_COMMON_SAFE_BROWSING_FILE_TYPE_POLICIES_H_ |
OLD | NEW |