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 #ifndef COMPONENTS_SAFE_BROWSING_DB_V4_GET_HASH_PROTOCOL_MANAGER_H_ | 5 #ifndef COMPONENTS_SAFE_BROWSING_DB_V4_GET_HASH_PROTOCOL_MANAGER_H_ |
6 #define COMPONENTS_SAFE_BROWSING_DB_V4_GET_HASH_PROTOCOL_MANAGER_H_ | 6 #define COMPONENTS_SAFE_BROWSING_DB_V4_GET_HASH_PROTOCOL_MANAGER_H_ |
7 | 7 |
8 // A class that implements Chrome's interface with the SafeBrowsing V4 protocol. | 8 // A class that implements Chrome's interface with the SafeBrowsing V4 protocol. |
9 // | 9 // |
10 // The V4GetHashProtocolManager handles formatting and making requests of, and | 10 // The V4GetHashProtocolManager handles formatting and making requests of, and |
(...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
76 | 76 |
77 // Retrieve the full hash and API metadata for a set of prefixes, and invoke | 77 // Retrieve the full hash and API metadata for a set of prefixes, and invoke |
78 // the callback argument when the results are retrieved. The callback may be | 78 // the callback argument when the results are retrieved. The callback may be |
79 // invoked synchronously. | 79 // invoked synchronously. |
80 virtual void GetFullHashesWithApis(const std::vector<SBPrefix>& prefixes, | 80 virtual void GetFullHashesWithApis(const std::vector<SBPrefix>& prefixes, |
81 FullHashCallback callback); | 81 FullHashCallback callback); |
82 | 82 |
83 // Overrides the clock used to check the time. | 83 // Overrides the clock used to check the time. |
84 void SetClockForTests(std::unique_ptr<base::Clock> clock); | 84 void SetClockForTests(std::unique_ptr<base::Clock> clock); |
85 | 85 |
86 // Looks up the cached results for |threat_type|. Fills |prefixes| with the | |
87 // prefixes that need a request. Fills |cached_results| with the cached | |
88 // results. | |
89 void GetFullHashCachedResults(const SBThreatType& threat_type, | |
90 const std::vector<SBFullHash>& full_hashes, | |
91 base::Time now, | |
92 std::vector<SBPrefix>* prefixes, | |
93 std::vector<SBFullHashResult>* cached_results); | |
94 | |
86 protected: | 95 protected: |
87 // Constructs a V4GetHashProtocolManager that issues | 96 // Constructs a V4GetHashProtocolManager that issues |
88 // network requests using |request_context_getter|. | 97 // network requests using |request_context_getter|. |
89 V4GetHashProtocolManager(net::URLRequestContextGetter* request_context_getter, | 98 V4GetHashProtocolManager(net::URLRequestContextGetter* request_context_getter, |
90 const V4ProtocolConfig& config); | 99 const V4ProtocolConfig& config); |
91 | 100 |
92 private: | 101 private: |
93 FRIEND_TEST_ALL_PREFIXES(SafeBrowsingV4GetHashProtocolManagerTest, | 102 FRIEND_TEST_ALL_PREFIXES(V4GetHashProtocolManagerTest, |
94 TestGetHashRequest); | 103 TestGetHashRequest); |
95 FRIEND_TEST_ALL_PREFIXES(SafeBrowsingV4GetHashProtocolManagerTest, | 104 FRIEND_TEST_ALL_PREFIXES(V4GetHashProtocolManagerTest, |
96 TestParseHashResponse); | 105 TestParseHashResponse); |
97 FRIEND_TEST_ALL_PREFIXES(SafeBrowsingV4GetHashProtocolManagerTest, | 106 FRIEND_TEST_ALL_PREFIXES(V4GetHashProtocolManagerTest, |
98 TestParseHashResponseWrongThreatEntryType); | 107 TestParseHashResponseWrongThreatEntryType); |
99 FRIEND_TEST_ALL_PREFIXES(SafeBrowsingV4GetHashProtocolManagerTest, | 108 FRIEND_TEST_ALL_PREFIXES(V4GetHashProtocolManagerTest, |
100 TestParseHashThreatPatternType); | 109 TestParseHashThreatPatternType); |
101 FRIEND_TEST_ALL_PREFIXES(SafeBrowsingV4GetHashProtocolManagerTest, | 110 FRIEND_TEST_ALL_PREFIXES(V4GetHashProtocolManagerTest, |
102 TestParseHashResponseNonPermissionMetadata); | 111 TestParseHashResponseNonPermissionMetadata); |
103 FRIEND_TEST_ALL_PREFIXES(SafeBrowsingV4GetHashProtocolManagerTest, | 112 FRIEND_TEST_ALL_PREFIXES(V4GetHashProtocolManagerTest, |
104 TestParseHashResponseInconsistentThreatTypes); | 113 TestParseHashResponseInconsistentThreatTypes); |
105 FRIEND_TEST_ALL_PREFIXES(SafeBrowsingV4GetHashProtocolManagerTest, | 114 FRIEND_TEST_ALL_PREFIXES(V4GetHashProtocolManagerTest, |
106 TestGetHashErrorHandlingOK); | 115 TestGetHashErrorHandlingOK); |
107 FRIEND_TEST_ALL_PREFIXES(SafeBrowsingV4GetHashProtocolManagerTest, | 116 FRIEND_TEST_ALL_PREFIXES(V4GetHashProtocolManagerTest, |
108 TestGetHashErrorHandlingNetwork); | 117 TestGetHashErrorHandlingNetwork); |
109 FRIEND_TEST_ALL_PREFIXES(SafeBrowsingV4GetHashProtocolManagerTest, | 118 FRIEND_TEST_ALL_PREFIXES(V4GetHashProtocolManagerTest, |
110 TestGetHashErrorHandlingResponseCode); | 119 TestGetHashErrorHandlingResponseCode); |
120 FRIEND_TEST_ALL_PREFIXES(V4GetHashProtocolManagerTest, GetCachedResults); | |
111 friend class V4GetHashProtocolManagerFactoryImpl; | 121 friend class V4GetHashProtocolManagerFactoryImpl; |
112 | 122 |
113 void GetHashUrlAndHeaders(const std::string& request_base64, | 123 void GetHashUrlAndHeaders(const std::string& request_base64, |
114 GURL* gurl, | 124 GURL* gurl, |
115 net::HttpRequestHeaders* headers) const; | 125 net::HttpRequestHeaders* headers) const; |
116 | 126 |
117 // Fills a FindFullHashesRequest protocol buffer for a request. | 127 // Fills a FindFullHashesRequest protocol buffer for a request. |
118 // Returns the serialized and base 64 encoded request as a string. | 128 // Returns the serialized and base 64 encoded request as a string. |
119 std::string GetHashRequest(const std::vector<SBPrefix>& prefixes, | 129 std::string GetHashRequest(const std::vector<SBPrefix>& prefixes, |
120 const std::vector<PlatformType>& platforms, | 130 const std::vector<PlatformType>& platforms, |
121 ThreatType threat_type); | 131 ThreatType threat_type); |
122 | 132 |
123 // Parses a FindFullHashesResponse protocol buffer and fills the results in | 133 // Parses a FindFullHashesResponse protocol buffer and fills the results in |
124 // |full_hashes| and |negative_cache_expire|. |data| is a serialized | 134 // |full_hashes| and |negative_cache_expire|. |data| is a serialized |
125 // FindFullHashes protocol buffer. |negative_cache_expire| is the cache expiry | 135 // FindFullHashes protocol buffer. |negative_cache_expire| is the cache expiry |
126 // time of the response for entities that did not match the threat list. | 136 // time of the response for entities that did not match the threat list. |
127 // Returns true if parsing is successful, false otherwise. | 137 // Returns true if parsing is successful, false otherwise. |
128 bool ParseHashResponse(const std::string& data_base64, | 138 bool ParseHashResponse(const std::string& data_base64, |
129 std::vector<SBFullHashResult>* full_hashes, | 139 std::vector<SBFullHashResult>* full_hashes, |
130 base::Time* negative_cache_expire); | 140 base::Time* negative_cache_expire); |
131 | 141 |
132 // Resets the gethash error counter and multiplier. | 142 // Resets the gethash error counter and multiplier. |
133 void ResetGetHashErrors(); | 143 void ResetGetHashErrors(); |
134 | 144 |
135 // Updates internal state for each GetHash response error, assuming that | 145 // Updates internal state for each GetHash response error, assuming that |
136 // the current time is |now|. | 146 // the current time is |now|. |
137 void HandleGetHashError(const base::Time& now); | 147 void HandleGetHashError(const base::Time& now); |
138 | 148 |
149 // TODO(vakh): Use HashPrefix and FullHash instead. | |
150 // TODO(vakh): Use base::hash_map instead of std::map | |
151 typedef std::map<SBPrefix, SBCachedFullHashResult> PrefixToFullHashResultsMap; | |
152 | |
153 typedef std::map<SBThreatType, PrefixToFullHashResultsMap> | |
154 ThreatTypeToResultsMap; | |
155 | |
156 ThreatTypeToResultsMap* v4_full_hash_cache(){ | |
Nathan Parker
2016/08/15 19:54:47
Does this accessor add value? You could just make
| |
157 return &v4_full_hash_cache_; | |
158 } | |
159 | |
139 private: | 160 private: |
140 // Map of GetHash requests to parameters which created it. | 161 // Map of GetHash requests to parameters which created it. |
141 typedef base::hash_map<const net::URLFetcher*, FullHashCallback> HashRequests; | 162 typedef base::hash_map<const net::URLFetcher*, std::pair<std::vector<SBPrefix> , FullHashCallback>> PendingHashRequests; |
Nathan Parker
2016/08/15 19:54:47
>80 char line.
| |
142 | 163 |
143 // The factory that controls the creation of V4GetHashProtocolManager. | 164 // The factory that controls the creation of V4GetHashProtocolManager. |
144 // This is used by tests. | 165 // This is used by tests. |
145 static V4GetHashProtocolManagerFactory* factory_; | 166 static V4GetHashProtocolManagerFactory* factory_; |
146 | 167 |
147 // Current active request (in case we need to cancel) for updates or chunks | 168 // Current active request (in case we need to cancel) for updates or chunks |
148 // from the SafeBrowsing service. We can only have one of these outstanding | 169 // from the SafeBrowsing service. We can only have one of these outstanding |
149 // at any given time unlike GetHash requests, which are tracked separately. | 170 // at any given time unlike GetHash requests, which are tracked separately. |
150 std::unique_ptr<net::URLFetcher> request_; | 171 std::unique_ptr<net::URLFetcher> request_; |
151 | 172 |
152 // The number of HTTP response errors since the the last successful HTTP | 173 // The number of HTTP response errors since the the last successful HTTP |
153 // response, used for request backoff timing. | 174 // response, used for request backoff timing. |
154 size_t gethash_error_count_; | 175 size_t gethash_error_count_; |
155 | 176 |
156 // Multiplier for the backoff error after the second. | 177 // Multiplier for the backoff error after the second. |
157 size_t gethash_back_off_mult_; | 178 size_t gethash_back_off_mult_; |
158 | 179 |
159 HashRequests hash_requests_; | 180 PendingHashRequests pending_hash_requests_; |
160 | 181 |
161 // For v4, the next gethash time is set to the backoff time is the last | 182 // For v4, the next gethash time is set to the backoff time is the last |
162 // response was an error, or the minimum wait time if the last response was | 183 // response was an error, or the minimum wait time if the last response was |
163 // successful. | 184 // successful. |
164 base::Time next_gethash_time_; | 185 base::Time next_gethash_time_; |
165 | 186 |
166 // The config of the client making Pver4 requests. | 187 // The config of the client making Pver4 requests. |
167 const V4ProtocolConfig config_; | 188 const V4ProtocolConfig config_; |
168 | 189 |
169 // The context we use to issue network requests. | 190 // The context we use to issue network requests. |
170 scoped_refptr<net::URLRequestContextGetter> request_context_getter_; | 191 scoped_refptr<net::URLRequestContextGetter> request_context_getter_; |
171 | 192 |
172 // ID for URLFetchers for testing. | 193 // ID for URLFetchers for testing. |
173 int url_fetcher_id_; | 194 int url_fetcher_id_; |
174 | 195 |
175 // The clock used to vend times. | 196 // The clock used to vend times. |
176 std::unique_ptr<base::Clock> clock_; | 197 std::unique_ptr<base::Clock> clock_; |
177 | 198 |
199 // A cache of V4 full hash results for api checks. | |
Nathan Parker
2016/08/15 19:54:47
Is this just for API requests?
| |
200 ThreatTypeToResultsMap v4_full_hash_cache_; | |
201 | |
178 DISALLOW_COPY_AND_ASSIGN(V4GetHashProtocolManager); | 202 DISALLOW_COPY_AND_ASSIGN(V4GetHashProtocolManager); |
179 }; | 203 }; |
180 | 204 |
181 // Interface of a factory to create V4GetHashProtocolManager. Useful for tests. | 205 // Interface of a factory to create V4GetHashProtocolManager. Useful for tests. |
182 class V4GetHashProtocolManagerFactory { | 206 class V4GetHashProtocolManagerFactory { |
183 public: | 207 public: |
184 V4GetHashProtocolManagerFactory() {} | 208 V4GetHashProtocolManagerFactory() {} |
185 virtual ~V4GetHashProtocolManagerFactory() {} | 209 virtual ~V4GetHashProtocolManagerFactory() {} |
186 virtual V4GetHashProtocolManager* CreateProtocolManager( | 210 virtual V4GetHashProtocolManager* CreateProtocolManager( |
187 net::URLRequestContextGetter* request_context_getter, | 211 net::URLRequestContextGetter* request_context_getter, |
188 const V4ProtocolConfig& config) = 0; | 212 const V4ProtocolConfig& config) = 0; |
189 | 213 |
190 private: | 214 private: |
191 DISALLOW_COPY_AND_ASSIGN(V4GetHashProtocolManagerFactory); | 215 DISALLOW_COPY_AND_ASSIGN(V4GetHashProtocolManagerFactory); |
192 }; | 216 }; |
193 | 217 |
194 } // namespace safe_browsing | 218 } // namespace safe_browsing |
195 | 219 |
196 #endif // COMPONENTS_SAFE_BROWSING_DB_V4_GET_HASH_PROTOCOL_MANAGER_H_ | 220 #endif // COMPONENTS_SAFE_BROWSING_DB_V4_GET_HASH_PROTOCOL_MANAGER_H_ |
OLD | NEW |