Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(150)

Side by Side Diff: components/safe_browsing_db/v4_update_protocol_manager.h

Issue 1727033003: v4_update_protocol_manager: Basic implementation with TODOs (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Use struct objects in unordered_map and unordered_set Created 4 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(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 update
9 // protocol.
10 //
11 // The V4UpdateProtocolManager handles formatting and making requests of, and
12 // handling responses from, Google's SafeBrowsing servers. The purpose of this
13 // class is to get hash prefixes from the SB server for the given set of lists.
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 GetUpdates completes.
46 // Parameters:
47 // - The vector of update response protobufs received from the server for
48 // each list type.
49 // The caller can then use this vector to re-build the current_list_states.
50 typedef base::Callback<void(const std::vector<ListUpdateResponse>&)>
51 UpdateCallback;
52
53 ~V4UpdateProtocolManager() override;
54
55 // Makes the passed |factory| the factory used to instantiate
56 // a V4UpdateProtocolManager. Useful for tests.
57 static void RegisterFactory(V4UpdateProtocolManagerFactory* factory) {
58 factory_ = factory;
59 }
60
61 // Create an instance of the safe browsing v4 protocol manager.
62 static V4UpdateProtocolManager* Create(
63 net::URLRequestContextGetter* request_context_getter,
64 const V4ProtocolConfig& config);
65
66 // net::URLFetcherDelegate interface.
67 void OnURLFetchComplete(const net::URLFetcher* source) override;
68
69 // Retrieve the hash prefix update, and invoke the callback argument when the
70 // results are retrieved. The callback may be invoked synchronously.
71 // Parameters:
72 // - The set of lists to fetch the updates for.
73 // - The last known state for each of the known lists.
74 // It is valid to have one or more lists in lists_to_update set that have no
75 // corresponding value in the current_list_states map. This corresponds to the
76 // initial state for those lists.
77 virtual void GetUpdates(
78 const base::hash_set<UpdateListIdentifier>& lists_to_update,
79 const base::hash_map<UpdateListIdentifier, const std::string&>&
80 current_list_states,
81 UpdateCallback callback);
82
83 protected:
84 // Constructs a V4UpdateProtocolManager that issues network requests using
85 // |request_context_getter|.
86 V4UpdateProtocolManager(net::URLRequestContextGetter* request_context_getter,
87 const V4ProtocolConfig& config);
88
89 private:
90 FRIEND_TEST_ALL_PREFIXES(SafeBrowsingV4UpdateProtocolManagerTest,
Nathan Parker 2016/03/25 23:51:01 readability nit: Do these tests need to start with
vakh (use Gerrit instead) 2016/03/26 00:06:56 Done.
91 TestGetUpdatesErrorHandlingNetwork);
92 FRIEND_TEST_ALL_PREFIXES(SafeBrowsingV4UpdateProtocolManagerTest,
93 TestGetUpdatesErrorHandlingResponseCode);
94 FRIEND_TEST_ALL_PREFIXES(SafeBrowsingV4UpdateProtocolManagerTest,
95 TestGetUpdatesNoError);
96 friend class V4UpdateProtocolManagerFactoryImpl;
97
98 // The method to generate the URL for the request to be sent to the server.
99 // |request_base64| is the base64 encoded form of an instance of the protobuf
100 // FetchThreatListUpdatesRequest.
101 GURL GetUpdateUrl(const std::string& request_base64) const;
102
103 // Fills a FetchThreatListUpdatesRequest protocol buffer for a request.
104 // Returns the serialized and base 64 encoded request as a string.
105 std::string GetUpdateRequest(
106 const base::hash_set<UpdateListIdentifier>& lists_to_update,
107 const base::hash_map<UpdateListIdentifier, const std::string&>&
108 current_list_states);
109
110 // Parses the base64 encoded response received from the server as a
111 // FetchThreatListUpdatesResponse protobuf and returns each of the
112 // ListUpdateResponse protobufs contained in it as a vector.
113 // Returns true if parsing is successful, false otherwise.
114 bool ParseUpdateResponse(
115 const std::string& data_base64,
116 std::vector<ListUpdateResponse>* list_update_responses);
117
118 // Resets the update error counter and multiplier.
119 void ResetUpdateErrors();
120
121 // Updates internal update and backoff state for each update response error,
122 // assuming that the current time is |now|.
123 void HandleUpdateError(const base::Time& now);
124
125 // The factory that controls the creation of V4UpdateProtocolManager.
126 // This is used by tests.
127 static V4UpdateProtocolManagerFactory* factory_;
128
129 // The number of HTTP response errors since the the last successful HTTP
130 // response, used for request backoff timing.
131 size_t update_error_count_;
132
133 // Multiplier for the backoff error after the second.
134 size_t update_back_off_mult_;
135
136 // The time before which the next update request may not be sent.
137 // It is set to:
138 // the backoff time, if the last response was an error, or
139 // the minimum wait time, if the last response was successful.
140 base::Time next_update_time_;
141
142 // The config of the client making Pver4 requests.
143 const V4ProtocolConfig config_;
144
145 // The context we use to issue network requests.
146 scoped_refptr<net::URLRequestContextGetter> request_context_getter_;
147
148 // ID for URLFetchers for testing.
149 int url_fetcher_id_;
150
151 // True if there's a request pending.
152 bool update_request_pending_;
153
154 // The callback that's called when GetUpdates completes.
155 UpdateCallback callback_;
156
157 // The pending update request. The request must be canceled when the object is
158 // destroyed.
159 scoped_ptr<net::URLFetcher> request_;
160
161 DISALLOW_COPY_AND_ASSIGN(V4UpdateProtocolManager);
162 };
163
164 // Interface of a factory to create V4UpdateProtocolManager. Useful for tests.
165 class V4UpdateProtocolManagerFactory {
166 public:
167 V4UpdateProtocolManagerFactory() {}
168 virtual ~V4UpdateProtocolManagerFactory() {}
169 virtual V4UpdateProtocolManager* CreateProtocolManager(
170 net::URLRequestContextGetter* request_context_getter,
171 const V4ProtocolConfig& config) = 0;
172
173 private:
174 DISALLOW_COPY_AND_ASSIGN(V4UpdateProtocolManagerFactory);
175 };
176
177 } // namespace safe_browsing
178
179 #endif // COMPONENTS_SAFE_BROWSING_DB_V4_UPDATE_PROTOCOL_MANAGER_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698