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 COMPONENTS_SAFE_BROWSING_DB_V4_UPDATE_PROTOCOL_MANAGER_H_ | |
6 #define COMPONENTS_SAFE_BROWSING_DB_V4_UPDATE_PROTOCOL_MANAGER_H_ | |
7 | |
8 // A class that implements Chrome's interface with the SafeBrowsing V4 protocol. | |
Nathan Parker
2016/02/23 18:47:06
"...V4 update protocol"
vakh (use Gerrit instead)
2016/02/23 23:58:20
Done.
| |
9 // | |
10 // The V4UpdateProtocolManager handles formatting and making requests of, and | |
11 // handling responses from, Google's SafeBrowsing servers. The purpose of this | |
12 // class is to get full hash matches from the SB server for the given set of | |
Nathan Parker
2016/02/23 18:47:06
Is this right? (the full hash match line)
vakh (use Gerrit instead)
2016/02/23 23:58:20
Done.
| |
13 // hash prefixes. | |
14 | |
15 #include <string> | |
16 #include <vector> | |
17 | |
18 #include "base/gtest_prod_util.h" | |
19 #include "base/macros.h" | |
20 #include "base/memory/scoped_ptr.h" | |
21 #include "base/threading/non_thread_safe.h" | |
22 #include "base/time/time.h" | |
23 #include "base/timer/timer.h" | |
24 #include "components/safe_browsing_db/safebrowsing.pb.h" | |
25 #include "components/safe_browsing_db/util.h" | |
26 #include "components/safe_browsing_db/v4_protocol_manager_util.h" | |
27 #include "net/url_request/url_fetcher_delegate.h" | |
28 #include "url/gurl.h" | |
29 | |
30 namespace net { | |
31 class URLFetcher; | |
32 class URLRequestContextGetter; | |
33 } // namespace net | |
34 | |
35 namespace safe_browsing { | |
36 | |
37 class V4UpdateProtocolManagerFactory; | |
38 | |
39 class V4UpdateProtocolManager : public net::URLFetcherDelegate, | |
40 public base::NonThreadSafe { | |
41 public: | |
42 typedef FetchThreatListUpdatesRequest::ListUpdateRequest ListUpdateRequest; | |
43 typedef FetchThreatListUpdatesResponse::ListUpdateResponse ListUpdateResponse; | |
44 | |
45 // UpdateCallback is invoked when GetUpdateWithClientInfo completes. | |
46 // Parameters: | |
47 // - The vector of update response protobufs received from the server for | |
48 // each list type. | |
49 typedef base::Callback<void(const std::vector<ListUpdateResponse>&)> | |
50 UpdateCallback; | |
51 | |
52 ~V4UpdateProtocolManager() override; | |
53 | |
54 // Makes the passed |factory| the factory used to instantiate | |
55 // a V4UpdateProtocolManager. Useful for tests. | |
56 static void RegisterFactory(V4UpdateProtocolManagerFactory* factory) { | |
57 factory_ = factory; | |
58 } | |
59 | |
60 // Create an instance of the safe browsing v4 protocol manager. | |
61 static V4UpdateProtocolManager* Create( | |
62 net::URLRequestContextGetter* request_context_getter, | |
63 const V4ProtocolConfig& config); | |
64 | |
65 // net::URLFetcherDelegate interface. | |
66 void OnURLFetchComplete(const net::URLFetcher* source) override; | |
67 | |
68 // Retrieve the hash prefix update, and invoke the callback argument when the | |
69 // results are retrieved. The callback may be invoked synchronously. | |
70 virtual void GetUpdates( | |
71 const base::hash_set<UpdateListIdentifier>& lists_to_update, | |
72 UpdateCallback callback); | |
73 | |
74 // Record an update operation result. | |
75 static void RecordUpdateResult(OperationResultType result_type); | |
76 | |
77 protected: | |
78 // Constructs a V4UpdateProtocolManager that issues | |
79 // network requests using |request_context_getter|. | |
80 V4UpdateProtocolManager( | |
81 net::URLRequestContextGetter* request_context_getter, | |
82 const V4ProtocolConfig& config); | |
83 | |
84 private: | |
85 friend class V4UpdateProtocolManagerFactoryImpl; | |
86 | |
87 GURL GetUpdateUrl(const std::string& request_base64) const; | |
88 | |
89 // Fills a FetchThreatListUpdatesRequest protocol buffer for a request. | |
90 // Returns the serialized and base 64 encoded request as a string. | |
91 std::string GetUpdateRequest( | |
92 const base::hash_set<UpdateListIdentifier>& lists_to_update); | |
93 | |
94 // Parses the base64 encoded response received from the server as a | |
95 // FetchThreatListUpdatesResponse protobuf and returns each of the | |
96 // ListUpdateResponse protobufs contained in it as a vector. | |
97 // Returns true if parsing is successful, false otherwise. | |
98 bool ParseUpdateResponse( | |
99 const std::string& data_base64, | |
100 std::vector<ListUpdateResponse>* list_update_responses); | |
101 | |
102 // Resets the update error counter and multiplier. | |
103 void ResetUpdateErrors(); | |
104 | |
105 // Updates internal update and backoff state for each update response error, | |
106 // assuming that the current time is |now|. | |
107 void HandleUpdateError(const base::Time& now); | |
108 | |
109 private: | |
110 // Map of update requests to parameters which created it. | |
111 typedef base::hash_map<const net::URLFetcher*, UpdateCallback> UpdateRequests; | |
112 | |
113 // The factory that controls the creation of V4UpdateProtocolManager. | |
114 // This is used by tests. | |
115 static V4UpdateProtocolManagerFactory* factory_; | |
116 | |
117 // Current active request (in case we need to cancel) for updates or chunks | |
118 // from the SafeBrowsing service. | |
119 scoped_ptr<net::URLFetcher> request_; | |
Nathan Parker
2016/02/23 18:47:06
If there's only one, then we don't need a map abov
vakh (use Gerrit instead)
2016/02/23 23:58:20
Done.
| |
120 | |
121 // The number of HTTP response errors since the the last successful HTTP | |
122 // response, used for request backoff timing. | |
123 size_t update_error_count_; | |
124 | |
125 // Multiplier for the backoff error after the second. | |
126 size_t update_back_off_mult_; | |
127 | |
128 UpdateRequests update_requests_; | |
Nathan Parker
2016/02/23 18:47:06
Is this this the UpdateRequests for the outstandin
vakh (use Gerrit instead)
2016/02/23 23:58:20
Yes. Changed.
| |
129 | |
130 // For v4, the next update time is set to the backoff time is the last | |
Nathan Parker
2016/02/23 18:47:06
rm "For v4"
vakh (use Gerrit instead)
2016/02/23 23:58:20
Done.
| |
131 // response was an error, or the minimum wait time if the last response was | |
132 // successful. | |
133 base::Time next_update_time_; | |
134 | |
135 // The config of the client making Pver4 requests. | |
136 const V4ProtocolConfig config_; | |
137 | |
138 // The context we use to issue network requests. | |
139 scoped_refptr<net::URLRequestContextGetter> request_context_getter_; | |
140 | |
141 // ID for URLFetchers for testing. | |
142 int url_fetcher_id_; | |
143 | |
144 base::hash_map<UpdateListIdentifier, const std::string&> list_states_; | |
145 | |
146 DISALLOW_COPY_AND_ASSIGN(V4UpdateProtocolManager); | |
147 }; | |
148 | |
149 // Interface of a factory to create V4UpdateProtocolManager. Useful for tests. | |
150 class V4UpdateProtocolManagerFactory { | |
151 public: | |
152 V4UpdateProtocolManagerFactory() {} | |
153 virtual ~V4UpdateProtocolManagerFactory() {} | |
154 virtual V4UpdateProtocolManager* CreateProtocolManager( | |
155 net::URLRequestContextGetter* request_context_getter, | |
156 const V4ProtocolConfig& config) = 0; | |
157 | |
158 private: | |
159 DISALLOW_COPY_AND_ASSIGN(V4UpdateProtocolManagerFactory); | |
160 }; | |
161 | |
162 } // namespace safe_browsing | |
163 | |
164 #endif // COMPONENTS_SAFE_BROWSING_DB_V4_UPDATE_PROTOCOL_MANAGER_H_ | |
OLD | NEW |