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

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

Issue 1719883003: Ignore: v4_update_protocol_manager: Basic implementation with TODOs (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@v4_01_prot_mgr
Patch Set: rebase again (and more branching funkiness) Created 4 years, 9 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 typedef V4ProtocolManagerUtil::OperationResultType OperationResultType;
54
55 ~V4UpdateProtocolManager() override;
56
57 // Makes the passed |factory| the factory used to instantiate
58 // a V4UpdateProtocolManager. Useful for tests.
59 static void RegisterFactory(V4UpdateProtocolManagerFactory* factory) {
60 factory_ = factory;
61 }
62
63 // Create an instance of the safe browsing v4 protocol manager.
64 static V4UpdateProtocolManager* Create(
65 net::URLRequestContextGetter* request_context_getter,
66 const V4ProtocolConfig& config);
67
68 // net::URLFetcherDelegate interface.
69 void OnURLFetchComplete(const net::URLFetcher* source) override;
70
71 // Retrieve the hash prefix update, and invoke the callback argument when the
72 // results are retrieved. The callback may be invoked synchronously.
73 // Parameters:
74 // - The set of list to fetch the updates for.
75 // - The last known state for each of the known lists.
76 // It is valud to have one or more lists in lists_to_update set that have no
77 // corresponding value in the current_list_states map. This corresponds to the
78 // initial state for those lists.
79 virtual void GetUpdates(
80 const base::hash_set<const UpdateListIdentifier*>& lists_to_update,
81 const base::hash_map<const UpdateListIdentifier*, const std::string&>&
82 current_list_states,
83 UpdateCallback callback);
84
85 // Record an update operation result.
86 static void RecordUpdateResult(OperationResultType result_type);
87
88 protected:
89 // Constructs a V4UpdateProtocolManager that issues network requests using
90 // |request_context_getter|.
91 V4UpdateProtocolManager(net::URLRequestContextGetter* request_context_getter,
92 const V4ProtocolConfig& config);
93
94 private:
95 FRIEND_TEST_ALL_PREFIXES(SafeBrowsingV4UpdateProtocolManagerTest,
96 TestGetUpdatesErrorHandlingNetwork);
97 FRIEND_TEST_ALL_PREFIXES(SafeBrowsingV4UpdateProtocolManagerTest,
98 TestGetUpdatesErrorHandlingResponseCode);
99 FRIEND_TEST_ALL_PREFIXES(SafeBrowsingV4UpdateProtocolManagerTest,
100 TestGetUpdatesNoError);
101 friend class V4UpdateProtocolManagerFactoryImpl;
102
103 GURL GetUpdateUrl(const std::string& request_base64) const;
104
105 // Fills a FetchThreatListUpdatesRequest protocol buffer for a request.
106 // Returns the serialized and base 64 encoded request as a string.
107 std::string GetUpdateRequest(
108 const base::hash_set<const UpdateListIdentifier*>& lists_to_update,
109 const base::hash_map<const UpdateListIdentifier*, const std::string&>&
110 current_list_states);
111
112 // Parses the base64 encoded response received from the server as a
113 // FetchThreatListUpdatesResponse protobuf and returns each of the
114 // ListUpdateResponse protobufs contained in it as a vector.
115 // Returns true if parsing is successful, false otherwise.
116 bool ParseUpdateResponse(
117 const std::string& data_base64,
118 std::vector<ListUpdateResponse>* list_update_responses);
119
120 // Resets the update error counter and multiplier.
121 void ResetUpdateErrors();
122
123 // Updates internal update and backoff state for each update response error,
124 // assuming that the current time is |now|.
125 void HandleUpdateError(const base::Time& now);
126
127 // The factory that controls the creation of V4UpdateProtocolManager.
128 // This is used by tests.
129 static V4UpdateProtocolManagerFactory* factory_;
130
131 // The number of HTTP response errors since the the last successful HTTP
132 // response, used for request backoff timing.
133 size_t update_error_count_;
134
135 // Multiplier for the backoff error after the second.
136 size_t update_back_off_mult_;
137
138 // The next update time is set to the backoff time is the last response was an
139 // error, or 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<const 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
« no previous file with comments | « components/safe_browsing_db/v4_protocol_manager_util.h ('k') | components/safe_browsing_db/v4_update_protocol_manager.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698