OLD | NEW |
---|---|
1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 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 CHROME_BROWSER_SAFE_BROWSING_PROTOCOL_MANAGER_H_ | 5 #ifndef CHROME_BROWSER_SAFE_BROWSING_PROTOCOL_MANAGER_H_ |
6 #define CHROME_BROWSER_SAFE_BROWSING_PROTOCOL_MANAGER_H_ | 6 #define CHROME_BROWSER_SAFE_BROWSING_PROTOCOL_MANAGER_H_ |
7 #pragma once | 7 #pragma once |
8 | 8 |
9 // A class that implements Chrome's interface with the SafeBrowsing protocol. | 9 // A class that implements Chrome's interface with the SafeBrowsing protocol. |
10 // The SafeBrowsingProtocolManager handles formatting and making requests of, | 10 // The SafeBrowsingProtocolManager handles formatting and making requests of, |
(...skipping 24 matching lines...) Expand all Loading... | |
35 namespace __gnu_cxx { | 35 namespace __gnu_cxx { |
36 template<> | 36 template<> |
37 struct hash<const URLFetcher*> { | 37 struct hash<const URLFetcher*> { |
38 size_t operator()(const URLFetcher* fetcher) const { | 38 size_t operator()(const URLFetcher* fetcher) const { |
39 return reinterpret_cast<size_t>(fetcher); | 39 return reinterpret_cast<size_t>(fetcher); |
40 } | 40 } |
41 }; | 41 }; |
42 } | 42 } |
43 #endif | 43 #endif |
44 | 44 |
45 class SafeBrowsingProtocolManager; | |
46 // Interface of a factory to create ProtocolManager. Useful for tests. | |
47 class ProtocolManagerFactory { | |
Scott Hess - ex-Googler
2010/12/07 21:22:57
This seems like a bit generic of a name to be in t
lzheng
2010/12/08 01:57:32
Done.
| |
48 public: | |
49 ProtocolManagerFactory() {} | |
50 virtual ~ProtocolManagerFactory() {} | |
51 virtual SafeBrowsingProtocolManager* CreateProtocolManager( | |
52 SafeBrowsingService* sb_service, | |
53 const std::string& client_name, | |
54 const std::string& client_key, | |
55 const std::string& wrapped_key, | |
56 URLRequestContextGetter* request_context_getter, | |
57 const std::string& info_url_prefix, | |
58 const std::string& mackey_url_prefix, | |
59 bool disable_auto_update) = 0; | |
60 private: | |
61 DISALLOW_COPY_AND_ASSIGN(ProtocolManagerFactory); | |
62 }; | |
63 | |
45 class SafeBrowsingProtocolManager : public URLFetcher::Delegate { | 64 class SafeBrowsingProtocolManager : public URLFetcher::Delegate { |
46 FRIEND_TEST_ALL_PREFIXES(SafeBrowsingProtocolManagerTest, TestBackOffTimes); | 65 FRIEND_TEST_ALL_PREFIXES(SafeBrowsingProtocolManagerTest, TestBackOffTimes); |
47 FRIEND_TEST_ALL_PREFIXES(SafeBrowsingProtocolManagerTest, TestChunkStrings); | 66 FRIEND_TEST_ALL_PREFIXES(SafeBrowsingProtocolManagerTest, TestChunkStrings); |
48 FRIEND_TEST_ALL_PREFIXES(SafeBrowsingProtocolManagerTest, TestGetHashUrl); | 67 FRIEND_TEST_ALL_PREFIXES(SafeBrowsingProtocolManagerTest, TestGetHashUrl); |
49 FRIEND_TEST_ALL_PREFIXES(SafeBrowsingProtocolManagerTest, | 68 FRIEND_TEST_ALL_PREFIXES(SafeBrowsingProtocolManagerTest, |
50 TestGetHashBackOffTimes); | 69 TestGetHashBackOffTimes); |
51 FRIEND_TEST_ALL_PREFIXES(SafeBrowsingProtocolManagerTest, TestMacKeyUrl); | 70 FRIEND_TEST_ALL_PREFIXES(SafeBrowsingProtocolManagerTest, TestMacKeyUrl); |
52 FRIEND_TEST_ALL_PREFIXES(SafeBrowsingProtocolManagerTest, | 71 FRIEND_TEST_ALL_PREFIXES(SafeBrowsingProtocolManagerTest, |
53 TestSafeBrowsingReportUrl); | 72 TestSafeBrowsingReportUrl); |
54 FRIEND_TEST_ALL_PREFIXES(SafeBrowsingProtocolManagerTest, TestNextChunkUrl); | 73 FRIEND_TEST_ALL_PREFIXES(SafeBrowsingProtocolManagerTest, TestNextChunkUrl); |
55 FRIEND_TEST_ALL_PREFIXES(SafeBrowsingProtocolManagerTest, TestUpdateUrl); | 74 FRIEND_TEST_ALL_PREFIXES(SafeBrowsingProtocolManagerTest, TestUpdateUrl); |
56 friend class SafeBrowsingServiceTest; | 75 friend class SafeBrowsingServiceTest; |
57 | 76 |
58 public: | 77 public: |
59 // Constructs a SafeBrowsingProtocolManager for |sb_service| that issues | |
60 // network requests using |request_context_getter|. When |disable_auto_update| | |
61 // is true, protocol manager won't schedule next update until | |
62 // ForceScheduleNextUpdate is called. | |
63 SafeBrowsingProtocolManager(SafeBrowsingService* sb_service, | |
64 const std::string& client_name, | |
65 const std::string& client_key, | |
66 const std::string& wrapped_key, | |
67 URLRequestContextGetter* request_context_getter, | |
68 const std::string& info_url_prefix, | |
69 const std::string& mackey_url_prefix, | |
70 bool disable_auto_update); | |
71 virtual ~SafeBrowsingProtocolManager(); | 78 virtual ~SafeBrowsingProtocolManager(); |
72 | 79 |
80 // Makes the passed |factory| the factory used to instanciate | |
Scott Hess - ex-Googler
2010/12/07 21:22:57
instantiate.
lzheng
2010/12/08 01:57:32
Done.
| |
81 // a SafeBrowsingService. Useful for tests. | |
82 static void RegisterFactory(ProtocolManagerFactory* factory) { | |
83 factory_ = factory; | |
84 } | |
85 | |
86 // Create an instance of the safe browsing service. | |
87 static SafeBrowsingProtocolManager* Create( | |
88 SafeBrowsingService* sb_service, | |
89 const std::string& client_name, | |
90 const std::string& client_key, | |
91 const std::string& wrapped_key, | |
92 URLRequestContextGetter* request_context_getter, | |
93 const std::string& info_url_prefix, | |
94 const std::string& mackey_url_prefix, | |
95 bool disable_auto_update); | |
96 | |
73 // Sets up the update schedule and internal state for making periodic requests | 97 // Sets up the update schedule and internal state for making periodic requests |
74 // of the SafeBrowsing service. | 98 // of the SafeBrowsing service. |
75 void Initialize(); | 99 virtual void Initialize(); |
76 | 100 |
77 // URLFetcher::Delegate interface. | 101 // URLFetcher::Delegate interface. |
78 virtual void OnURLFetchComplete(const URLFetcher* source, | 102 virtual void OnURLFetchComplete(const URLFetcher* source, |
79 const GURL& url, | 103 const GURL& url, |
80 const URLRequestStatus& status, | 104 const URLRequestStatus& status, |
81 int response_code, | 105 int response_code, |
82 const ResponseCookies& cookies, | 106 const ResponseCookies& cookies, |
83 const std::string& data); | 107 const std::string& data); |
84 | 108 |
85 // API used by the SafeBrowsingService for issuing queries. When the results | 109 // API used by the SafeBrowsingService for issuing queries. When the results |
86 // are available, SafeBrowsingService::HandleGetHashResults is called. | 110 // are available, SafeBrowsingService::HandleGetHashResults is called. |
87 void GetFullHash(SafeBrowsingService::SafeBrowsingCheck* check, | 111 virtual void GetFullHash(SafeBrowsingService::SafeBrowsingCheck* check, |
88 const std::vector<SBPrefix>& prefixes); | 112 const std::vector<SBPrefix>& prefixes); |
89 | 113 |
90 // Forces the start of next update after |next_update_msec| in msec. | 114 // Forces the start of next update after |next_update_msec| in msec. |
91 void ForceScheduleNextUpdate(int next_update_msec); | 115 void ForceScheduleNextUpdate(int next_update_msec); |
92 | 116 |
93 bool is_initial_request() const { return initial_request_; } | |
94 | |
95 // Scheduled update callback. | 117 // Scheduled update callback. |
96 void GetNextUpdate(); | 118 void GetNextUpdate(); |
97 | 119 |
98 // Called by the SafeBrowsingService when our request for a list of all chunks | 120 // Called by the SafeBrowsingService when our request for a list of all chunks |
99 // for each list is done. If database_error is true, that means the protocol | 121 // for each list is done. If database_error is true, that means the protocol |
100 // manager shouldn't fetch updates since they can't be written to disk. It | 122 // manager shouldn't fetch updates since they can't be written to disk. It |
101 // should try again later to open the database. | 123 // should try again later to open the database. |
102 void OnGetChunksComplete(const std::vector<SBListChunkRanges>& list, | 124 void OnGetChunksComplete(const std::vector<SBListChunkRanges>& list, |
103 bool database_error); | 125 bool database_error); |
104 | 126 |
105 // Called after the chunks that were parsed were inserted in the database. | 127 // Called after the chunks that were parsed were inserted in the database. |
106 void OnChunkInserted(); | 128 void OnChunkInserted(); |
107 | 129 |
108 // The last time we received an update. | |
109 base::Time last_update() const { return last_update_; } | |
110 | |
111 // For UMA users we report to Google when a SafeBrowsing interstitial is shown | 130 // For UMA users we report to Google when a SafeBrowsing interstitial is shown |
112 // to the user. We assume that the threat type is either URL_MALWARE or | 131 // to the user. We assume that the threat type is either URL_MALWARE or |
113 // URL_PHISHING. | 132 // URL_PHISHING. |
114 void ReportSafeBrowsingHit(const GURL& malicious_url, | 133 void ReportSafeBrowsingHit(const GURL& malicious_url, |
115 const GURL& page_url, | 134 const GURL& page_url, |
116 const GURL& referrer_url, | 135 const GURL& referrer_url, |
117 bool is_subresource, | 136 bool is_subresource, |
118 SafeBrowsingService::UrlCheckResult threat_type); | 137 SafeBrowsingService::UrlCheckResult threat_type); |
119 | 138 |
120 | 139 bool is_initial_request() const { return initial_request_; } |
140 // The last time we received an update. | |
141 base::Time last_update() const { return last_update_; } | |
121 // Setter for additional_query_. To make sure the additional_query_ won't | 142 // Setter for additional_query_. To make sure the additional_query_ won't |
122 // be changed in the middle of an update, caller (e.g.: SafeBrowsingService) | 143 // be changed in the middle of an update, caller (e.g.: SafeBrowsingService) |
123 // should call this after callbacks triggered in UpdateFinished() or before | 144 // should call this after callbacks triggered in UpdateFinished() or before |
124 // IssueUpdateRequest(). | 145 // IssueUpdateRequest(). |
125 void set_additional_query(const std::string& query) { | 146 void set_additional_query(const std::string& query) { |
126 additional_query_ = query; | 147 additional_query_ = query; |
127 } | 148 } |
128 const std::string& additional_query() const { | 149 const std::string& additional_query() const { |
129 return additional_query_; | 150 return additional_query_; |
130 } | 151 } |
131 | 152 |
153 protected: | |
154 // Constructs a SafeBrowsingProtocolManager for |sb_service| that issues | |
155 // network requests using |request_context_getter|. When |disable_auto_update| | |
156 // is true, protocol manager won't schedule next update until | |
157 // ForceScheduleNextUpdate is called. | |
158 SafeBrowsingProtocolManager(SafeBrowsingService* sb_service, | |
159 const std::string& client_name, | |
160 const std::string& client_key, | |
161 const std::string& wrapped_key, | |
162 URLRequestContextGetter* request_context_getter, | |
163 const std::string& info_url_prefix, | |
164 const std::string& mackey_url_prefix, | |
165 bool disable_auto_update); | |
132 private: | 166 private: |
167 friend class ProtocolManagerFactoryImpl; | |
168 | |
133 // Internal API for fetching information from the SafeBrowsing servers. The | 169 // Internal API for fetching information from the SafeBrowsing servers. The |
134 // GetHash requests are higher priority since they can block user requests | 170 // GetHash requests are higher priority since they can block user requests |
135 // so are handled separately. | 171 // so are handled separately. |
136 enum SafeBrowsingRequestType { | 172 enum SafeBrowsingRequestType { |
137 NO_REQUEST = 0, // No requests in progress | 173 NO_REQUEST = 0, // No requests in progress |
138 UPDATE_REQUEST, // Request for redirect URLs | 174 UPDATE_REQUEST, // Request for redirect URLs |
139 CHUNK_REQUEST, // Request for a specific chunk | 175 CHUNK_REQUEST, // Request for a specific chunk |
140 GETKEY_REQUEST // Update the client's MAC key | 176 GETKEY_REQUEST // Update the client's MAC key |
141 }; | 177 }; |
142 | 178 |
(...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
215 void HandleGetHashError(const base::Time& now); | 251 void HandleGetHashError(const base::Time& now); |
216 | 252 |
217 // Helper function for update completion. | 253 // Helper function for update completion. |
218 void UpdateFinished(bool success); | 254 void UpdateFinished(bool success); |
219 | 255 |
220 // A callback that runs if we timeout waiting for a response to an update | 256 // A callback that runs if we timeout waiting for a response to an update |
221 // request. We use this to properly set our update state. | 257 // request. We use this to properly set our update state. |
222 void UpdateResponseTimeout(); | 258 void UpdateResponseTimeout(); |
223 | 259 |
224 private: | 260 private: |
261 // The factory that controlls the creation of SafeBrowsingProtocolManager. | |
262 // This is used by tests. | |
263 static ProtocolManagerFactory* factory_; | |
Scott Hess - ex-Googler
2010/12/07 21:22:57
"controls", and a newline between this variable an
lzheng
2010/12/08 01:57:32
Done.
| |
225 // Main SafeBrowsing interface object. | 264 // Main SafeBrowsing interface object. |
226 SafeBrowsingService* sb_service_; | 265 SafeBrowsingService* sb_service_; |
227 | 266 |
228 // Current active request (in case we need to cancel) for updates or chunks | 267 // Current active request (in case we need to cancel) for updates or chunks |
229 // from the SafeBrowsing service. We can only have one of these outstanding | 268 // from the SafeBrowsing service. We can only have one of these outstanding |
230 // at any given time unlike GetHash requests, which are tracked separately. | 269 // at any given time unlike GetHash requests, which are tracked separately. |
231 scoped_ptr<URLFetcher> request_; | 270 scoped_ptr<URLFetcher> request_; |
232 | 271 |
233 // The kind of request that is currently in progress. | 272 // The kind of request that is currently in progress. |
234 SafeBrowsingRequestType request_type_; | 273 SafeBrowsingRequestType request_type_; |
(...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
317 std::string mackey_url_prefix_; | 356 std::string mackey_url_prefix_; |
318 | 357 |
319 // When true, protocol manager will not start an update unless | 358 // When true, protocol manager will not start an update unless |
320 // ForceScheduleNextUpdate() is called. This is set for testing purpose. | 359 // ForceScheduleNextUpdate() is called. This is set for testing purpose. |
321 bool disable_auto_update_; | 360 bool disable_auto_update_; |
322 | 361 |
323 DISALLOW_COPY_AND_ASSIGN(SafeBrowsingProtocolManager); | 362 DISALLOW_COPY_AND_ASSIGN(SafeBrowsingProtocolManager); |
324 }; | 363 }; |
325 | 364 |
326 #endif // CHROME_BROWSER_SAFE_BROWSING_PROTOCOL_MANAGER_H_ | 365 #endif // CHROME_BROWSER_SAFE_BROWSING_PROTOCOL_MANAGER_H_ |
OLD | NEW |