| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 // Utilities for the SafeBrowsing code. | 5 // Utilities for the SafeBrowsing code. |
| 6 | 6 |
| 7 #ifndef CHROME_BROWSER_SAFE_BROWSING_SAFE_BROWSING_UTIL_H_ | 7 #ifndef CHROME_BROWSER_SAFE_BROWSING_SAFE_BROWSING_UTIL_H_ |
| 8 #define CHROME_BROWSER_SAFE_BROWSING_SAFE_BROWSING_UTIL_H_ | 8 #define CHROME_BROWSER_SAFE_BROWSING_SAFE_BROWSING_UTIL_H_ |
| 9 | 9 |
| 10 #include <cstring> | 10 #include <cstring> |
| 11 #include <string> | 11 #include <string> |
| 12 #include <vector> | 12 #include <vector> |
| 13 | 13 |
| 14 #include "base/basictypes.h" | 14 #include "base/basictypes.h" |
| 15 #include "base/memory/scoped_ptr.h" | 15 #include "base/memory/scoped_ptr.h" |
| 16 #include "base/strings/string_piece.h" | 16 #include "base/strings/string_piece.h" |
| 17 #include "base/time/time.h" | 17 #include "base/time/time.h" |
| 18 #include "chrome/browser/safe_browsing/chunk_range.h" | 18 #include "chrome/browser/safe_browsing/chunk_range.h" |
| 19 #include "components/safe_browsing_db/safe_browsing_db_util.h" | 19 #include "components/safe_browsing_db/safe_browsing_db_util.h" |
| 20 | 20 |
| 21 namespace safe_browsing { | 21 namespace safe_browsing { |
| 22 class ChunkData; | 22 class ChunkData; |
| 23 }; | 23 }; |
| 24 | 24 |
| 25 class GURL; | |
| 26 | |
| 27 // Container for holding a chunk URL and the list it belongs to. | 25 // Container for holding a chunk URL and the list it belongs to. |
| 28 struct ChunkUrl { | 26 struct ChunkUrl { |
| 29 std::string url; | 27 std::string url; |
| 30 std::string list_name; | 28 std::string list_name; |
| 31 }; | 29 }; |
| 32 | 30 |
| 33 // Data for an individual chunk sent from the server. | 31 // Data for an individual chunk sent from the server. |
| 34 class SBChunkData { | 32 class SBChunkData { |
| 35 public: | 33 public: |
| 36 SBChunkData(); | 34 SBChunkData(); |
| (...skipping 21 matching lines...) Expand all Loading... |
| 58 size_t FullHashCount() const; | 56 size_t FullHashCount() const; |
| 59 SBFullHash FullHashAt(size_t i) const; | 57 SBFullHash FullHashAt(size_t i) const; |
| 60 | 58 |
| 61 private: | 59 private: |
| 62 // Protocol buffer sent from server. | 60 // Protocol buffer sent from server. |
| 63 scoped_ptr<safe_browsing::ChunkData> chunk_data_; | 61 scoped_ptr<safe_browsing::ChunkData> chunk_data_; |
| 64 | 62 |
| 65 DISALLOW_COPY_AND_ASSIGN(SBChunkData); | 63 DISALLOW_COPY_AND_ASSIGN(SBChunkData); |
| 66 }; | 64 }; |
| 67 | 65 |
| 68 // Used when we get a gethash response. | |
| 69 struct SBFullHashResult { | |
| 70 SBFullHash hash; | |
| 71 // TODO(shess): Refactor to allow ListType here. | |
| 72 int list_id; | |
| 73 std::string metadata; | |
| 74 }; | |
| 75 | |
| 76 // Caches individual response from GETHASH request. | |
| 77 struct SBCachedFullHashResult { | |
| 78 SBCachedFullHashResult(); | |
| 79 explicit SBCachedFullHashResult(const base::Time& in_expire_after); | |
| 80 ~SBCachedFullHashResult(); | |
| 81 | |
| 82 base::Time expire_after; | |
| 83 std::vector<SBFullHashResult> full_hashes; | |
| 84 }; | |
| 85 | |
| 86 // Contains information about a list in the database. | 66 // Contains information about a list in the database. |
| 87 struct SBListChunkRanges { | 67 struct SBListChunkRanges { |
| 88 explicit SBListChunkRanges(const std::string& n); | 68 explicit SBListChunkRanges(const std::string& n); |
| 89 | 69 |
| 90 std::string name; // The list name. | 70 std::string name; // The list name. |
| 91 std::string adds; // The ranges for add chunks. | 71 std::string adds; // The ranges for add chunks. |
| 92 std::string subs; // The ranges for sub chunks. | 72 std::string subs; // The ranges for sub chunks. |
| 93 }; | 73 }; |
| 94 | 74 |
| 95 // Container for deleting chunks from the database. | 75 // Container for deleting chunks from the database. |
| 96 struct SBChunkDelete { | 76 struct SBChunkDelete { |
| 97 SBChunkDelete(); | 77 SBChunkDelete(); |
| 98 ~SBChunkDelete(); | 78 ~SBChunkDelete(); |
| 99 | 79 |
| 100 std::string list_name; | 80 std::string list_name; |
| 101 bool is_sub_del; | 81 bool is_sub_del; |
| 102 std::vector<ChunkRange> chunk_del; | 82 std::vector<ChunkRange> chunk_del; |
| 103 }; | 83 }; |
| 104 | 84 |
| 105 // Utility functions ----------------------------------------------------------- | |
| 106 | |
| 107 namespace safe_browsing_util { | |
| 108 | |
| 109 // SafeBrowsing list names. | |
| 110 extern const char kMalwareList[]; | |
| 111 extern const char kPhishingList[]; | |
| 112 // Binary Download list name. | |
| 113 extern const char kBinUrlList[]; | |
| 114 // SafeBrowsing client-side detection whitelist list name. | |
| 115 extern const char kCsdWhiteList[]; | |
| 116 // SafeBrowsing download whitelist list name. | |
| 117 extern const char kDownloadWhiteList[]; | |
| 118 // SafeBrowsing extension list name. | |
| 119 extern const char kExtensionBlacklist[]; | |
| 120 // SafeBrowsing csd malware IP blacklist name. | |
| 121 extern const char kIPBlacklist[]; | |
| 122 // SafeBrowsing unwanted URL list. | |
| 123 extern const char kUnwantedUrlList[]; | |
| 124 // SafeBrowsing off-domain inclusion whitelist list name. | |
| 125 extern const char kInclusionWhitelist[]; | |
| 126 | |
| 127 // This array must contain all Safe Browsing lists. | |
| 128 extern const char* kAllLists[9]; | |
| 129 | |
| 130 enum ListType { | |
| 131 INVALID = -1, | |
| 132 MALWARE = 0, | |
| 133 PHISH = 1, | |
| 134 BINURL = 2, | |
| 135 // Obsolete BINHASH = 3, | |
| 136 CSDWHITELIST = 4, | |
| 137 // SafeBrowsing lists are stored in pairs. Keep ListType 5 | |
| 138 // available for a potential second list that we would store in the | |
| 139 // csd-whitelist store file. | |
| 140 DOWNLOADWHITELIST = 6, | |
| 141 // See above comment. Leave 7 available. | |
| 142 EXTENSIONBLACKLIST = 8, | |
| 143 // See above comment. Leave 9 available. | |
| 144 // Obsolete SIDEEFFECTFREEWHITELIST = 10, | |
| 145 // See above comment. Leave 11 available. | |
| 146 IPBLACKLIST = 12, | |
| 147 // See above comment. Leave 13 available. | |
| 148 UNWANTEDURL = 14, | |
| 149 // See above comment. Leave 15 available. | |
| 150 INCLUSIONWHITELIST = 16, | |
| 151 // See above comment. Leave 17 available. | |
| 152 }; | |
| 153 | |
| 154 // Maps a list name to ListType. | |
| 155 ListType GetListId(const base::StringPiece& name); | |
| 156 | |
| 157 // Maps a ListId to list name. Return false if fails. | |
| 158 bool GetListName(ListType list_id, std::string* list); | |
| 159 | |
| 160 // Canonicalizes url as per Google Safe Browsing Specification. | |
| 161 // See section 6.1 in | |
| 162 // http://code.google.com/p/google-safe-browsing/wiki/Protocolv2Spec. | |
| 163 void CanonicalizeUrl(const GURL& url, std::string* canonicalized_hostname, | |
| 164 std::string* canonicalized_path, | |
| 165 std::string* canonicalized_query); | |
| 166 | |
| 167 // Given a URL, returns all the hosts we need to check. They are returned | |
| 168 // in order of size (i.e. b.c is first, then a.b.c). | |
| 169 void GenerateHostsToCheck(const GURL& url, std::vector<std::string>* hosts); | |
| 170 | |
| 171 // Given a URL, returns all the paths we need to check. | |
| 172 void GeneratePathsToCheck(const GURL& url, std::vector<std::string>* paths); | |
| 173 | |
| 174 // Given a URL, returns all the patterns we need to check. | |
| 175 void GeneratePatternsToCheck(const GURL& url, std::vector<std::string>* urls); | |
| 176 | |
| 177 SBFullHash StringToSBFullHash(const std::string& hash_in); | |
| 178 std::string SBFullHashToString(const SBFullHash& hash_out); | |
| 179 | |
| 180 } // namespace safe_browsing_util | |
| 181 | |
| 182 #endif // CHROME_BROWSER_SAFE_BROWSING_SAFE_BROWSING_UTIL_H_ | 85 #endif // CHROME_BROWSER_SAFE_BROWSING_SAFE_BROWSING_UTIL_H_ |
| OLD | NEW |