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 SBProtocolManagerFactory { |
| 48 public: |
| 49 SBProtocolManagerFactory() {} |
| 50 virtual ~SBProtocolManagerFactory() {} |
| 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(SBProtocolManagerFactory); |
| 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 TestSafeBrowsingHitUrl); | 72 TestSafeBrowsingHitUrl); |
54 FRIEND_TEST_ALL_PREFIXES(SafeBrowsingProtocolManagerTest, | 73 FRIEND_TEST_ALL_PREFIXES(SafeBrowsingProtocolManagerTest, |
55 TestMalwareDetailsUrl); | 74 TestMalwareDetailsUrl); |
56 FRIEND_TEST_ALL_PREFIXES(SafeBrowsingProtocolManagerTest, TestNextChunkUrl); | 75 FRIEND_TEST_ALL_PREFIXES(SafeBrowsingProtocolManagerTest, TestNextChunkUrl); |
57 FRIEND_TEST_ALL_PREFIXES(SafeBrowsingProtocolManagerTest, TestUpdateUrl); | 76 FRIEND_TEST_ALL_PREFIXES(SafeBrowsingProtocolManagerTest, TestUpdateUrl); |
58 friend class SafeBrowsingServiceTest; | 77 friend class SafeBrowsingServiceTest; |
59 | 78 |
60 public: | 79 public: |
61 // Constructs a SafeBrowsingProtocolManager for |sb_service| that issues | |
62 // network requests using |request_context_getter|. When |disable_auto_update| | |
63 // is true, protocol manager won't schedule next update until | |
64 // ForceScheduleNextUpdate is called. | |
65 SafeBrowsingProtocolManager(SafeBrowsingService* sb_service, | |
66 const std::string& client_name, | |
67 const std::string& client_key, | |
68 const std::string& wrapped_key, | |
69 URLRequestContextGetter* request_context_getter, | |
70 const std::string& http_url_prefix, | |
71 const std::string& https_url_prefix, | |
72 bool disable_auto_update); | |
73 virtual ~SafeBrowsingProtocolManager(); | 80 virtual ~SafeBrowsingProtocolManager(); |
74 | 81 |
| 82 // Makes the passed |factory| the factory used to instantiate |
| 83 // a SafeBrowsingService. Useful for tests. |
| 84 static void RegisterFactory(SBProtocolManagerFactory* factory) { |
| 85 factory_ = factory; |
| 86 } |
| 87 |
| 88 // Create an instance of the safe browsing service. |
| 89 static SafeBrowsingProtocolManager* Create( |
| 90 SafeBrowsingService* sb_service, |
| 91 const std::string& client_name, |
| 92 const std::string& client_key, |
| 93 const std::string& wrapped_key, |
| 94 URLRequestContextGetter* request_context_getter, |
| 95 const std::string& info_url_prefix, |
| 96 const std::string& mackey_url_prefix, |
| 97 bool disable_auto_update); |
| 98 |
75 // Sets up the update schedule and internal state for making periodic requests | 99 // Sets up the update schedule and internal state for making periodic requests |
76 // of the SafeBrowsing service. | 100 // of the SafeBrowsing service. |
77 void Initialize(); | 101 virtual void Initialize(); |
78 | 102 |
79 // URLFetcher::Delegate interface. | 103 // URLFetcher::Delegate interface. |
80 virtual void OnURLFetchComplete(const URLFetcher* source, | 104 virtual void OnURLFetchComplete(const URLFetcher* source, |
81 const GURL& url, | 105 const GURL& url, |
82 const URLRequestStatus& status, | 106 const URLRequestStatus& status, |
83 int response_code, | 107 int response_code, |
84 const ResponseCookies& cookies, | 108 const ResponseCookies& cookies, |
85 const std::string& data); | 109 const std::string& data); |
86 | 110 |
87 // API used by the SafeBrowsingService for issuing queries. When the results | 111 // API used by the SafeBrowsingService for issuing queries. When the results |
88 // are available, SafeBrowsingService::HandleGetHashResults is called. | 112 // are available, SafeBrowsingService::HandleGetHashResults is called. |
89 void GetFullHash(SafeBrowsingService::SafeBrowsingCheck* check, | 113 virtual void GetFullHash(SafeBrowsingService::SafeBrowsingCheck* check, |
90 const std::vector<SBPrefix>& prefixes); | 114 const std::vector<SBPrefix>& prefixes); |
91 | 115 |
92 // Forces the start of next update after |next_update_msec| in msec. | 116 // Forces the start of next update after |next_update_msec| in msec. |
93 void ForceScheduleNextUpdate(int next_update_msec); | 117 void ForceScheduleNextUpdate(int next_update_msec); |
94 | 118 |
95 bool is_initial_request() const { return initial_request_; } | |
96 | |
97 // Scheduled update callback. | 119 // Scheduled update callback. |
98 void GetNextUpdate(); | 120 void GetNextUpdate(); |
99 | 121 |
100 // Called by the SafeBrowsingService when our request for a list of all chunks | 122 // Called by the SafeBrowsingService when our request for a list of all chunks |
101 // for each list is done. If database_error is true, that means the protocol | 123 // for each list is done. If database_error is true, that means the protocol |
102 // manager shouldn't fetch updates since they can't be written to disk. It | 124 // manager shouldn't fetch updates since they can't be written to disk. It |
103 // should try again later to open the database. | 125 // should try again later to open the database. |
104 void OnGetChunksComplete(const std::vector<SBListChunkRanges>& list, | 126 void OnGetChunksComplete(const std::vector<SBListChunkRanges>& list, |
105 bool database_error); | 127 bool database_error); |
106 | 128 |
107 // Called after the chunks that were parsed were inserted in the database. | 129 // Called after the chunks that were parsed were inserted in the database. |
108 void OnChunkInserted(); | 130 void OnChunkInserted(); |
109 | 131 |
110 // The last time we received an update. | |
111 base::Time last_update() const { return last_update_; } | |
112 | |
113 // For UMA users we report to Google when a SafeBrowsing interstitial is shown | 132 // For UMA users we report to Google when a SafeBrowsing interstitial is shown |
114 // to the user. We assume that the threat type is either URL_MALWARE or | 133 // to the user. We assume that the threat type is either URL_MALWARE or |
115 // URL_PHISHING. | 134 // URL_PHISHING. |
116 void ReportSafeBrowsingHit(const GURL& malicious_url, | 135 void ReportSafeBrowsingHit(const GURL& malicious_url, |
117 const GURL& page_url, | 136 const GURL& page_url, |
118 const GURL& referrer_url, | 137 const GURL& referrer_url, |
119 bool is_subresource, | 138 bool is_subresource, |
120 SafeBrowsingService::UrlCheckResult threat_type); | 139 SafeBrowsingService::UrlCheckResult threat_type); |
121 | 140 |
122 // Users can opt-in on the SafeBrowsing interstitial to send detailed | 141 // Users can opt-in on the SafeBrowsing interstitial to send detailed |
123 // malware reports. |report| is the serialized report. | 142 // malware reports. |report| is the serialized report. |
124 void ReportMalwareDetails(const std::string& report); | 143 void ReportMalwareDetails(const std::string& report); |
125 | 144 |
| 145 bool is_initial_request() const { return initial_request_; } |
| 146 |
| 147 // The last time we received an update. |
| 148 base::Time last_update() const { return last_update_; } |
| 149 |
126 // Setter for additional_query_. To make sure the additional_query_ won't | 150 // Setter for additional_query_. To make sure the additional_query_ won't |
127 // be changed in the middle of an update, caller (e.g.: SafeBrowsingService) | 151 // be changed in the middle of an update, caller (e.g.: SafeBrowsingService) |
128 // should call this after callbacks triggered in UpdateFinished() or before | 152 // should call this after callbacks triggered in UpdateFinished() or before |
129 // IssueUpdateRequest(). | 153 // IssueUpdateRequest(). |
130 void set_additional_query(const std::string& query) { | 154 void set_additional_query(const std::string& query) { |
131 additional_query_ = query; | 155 additional_query_ = query; |
132 } | 156 } |
133 const std::string& additional_query() const { | 157 const std::string& additional_query() const { |
134 return additional_query_; | 158 return additional_query_; |
135 } | 159 } |
136 | 160 |
| 161 protected: |
| 162 // Constructs a SafeBrowsingProtocolManager for |sb_service| that issues |
| 163 // network requests using |request_context_getter|. When |disable_auto_update| |
| 164 // is true, protocol manager won't schedule next update until |
| 165 // ForceScheduleNextUpdate is called. |
| 166 SafeBrowsingProtocolManager(SafeBrowsingService* sb_service, |
| 167 const std::string& client_name, |
| 168 const std::string& client_key, |
| 169 const std::string& wrapped_key, |
| 170 URLRequestContextGetter* request_context_getter, |
| 171 const std::string& http_url_prefix, |
| 172 const std::string& https_url_prefix, |
| 173 bool disable_auto_update); |
137 private: | 174 private: |
| 175 friend class SBProtocolManagerFactoryImpl; |
| 176 |
138 // Internal API for fetching information from the SafeBrowsing servers. The | 177 // Internal API for fetching information from the SafeBrowsing servers. The |
139 // GetHash requests are higher priority since they can block user requests | 178 // GetHash requests are higher priority since they can block user requests |
140 // so are handled separately. | 179 // so are handled separately. |
141 enum SafeBrowsingRequestType { | 180 enum SafeBrowsingRequestType { |
142 NO_REQUEST = 0, // No requests in progress | 181 NO_REQUEST = 0, // No requests in progress |
143 UPDATE_REQUEST, // Request for redirect URLs | 182 UPDATE_REQUEST, // Request for redirect URLs |
144 CHUNK_REQUEST, // Request for a specific chunk | 183 CHUNK_REQUEST, // Request for a specific chunk |
145 GETKEY_REQUEST // Update the client's MAC key | 184 GETKEY_REQUEST // Update the client's MAC key |
146 }; | 185 }; |
147 | 186 |
(...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
223 void HandleGetHashError(const base::Time& now); | 262 void HandleGetHashError(const base::Time& now); |
224 | 263 |
225 // Helper function for update completion. | 264 // Helper function for update completion. |
226 void UpdateFinished(bool success); | 265 void UpdateFinished(bool success); |
227 | 266 |
228 // A callback that runs if we timeout waiting for a response to an update | 267 // A callback that runs if we timeout waiting for a response to an update |
229 // request. We use this to properly set our update state. | 268 // request. We use this to properly set our update state. |
230 void UpdateResponseTimeout(); | 269 void UpdateResponseTimeout(); |
231 | 270 |
232 private: | 271 private: |
| 272 // The factory that controls the creation of SafeBrowsingProtocolManager. |
| 273 // This is used by tests. |
| 274 static SBProtocolManagerFactory* factory_; |
| 275 |
233 // Main SafeBrowsing interface object. | 276 // Main SafeBrowsing interface object. |
234 SafeBrowsingService* sb_service_; | 277 SafeBrowsingService* sb_service_; |
235 | 278 |
236 // Current active request (in case we need to cancel) for updates or chunks | 279 // Current active request (in case we need to cancel) for updates or chunks |
237 // from the SafeBrowsing service. We can only have one of these outstanding | 280 // from the SafeBrowsing service. We can only have one of these outstanding |
238 // at any given time unlike GetHash requests, which are tracked separately. | 281 // at any given time unlike GetHash requests, which are tracked separately. |
239 scoped_ptr<URLFetcher> request_; | 282 scoped_ptr<URLFetcher> request_; |
240 | 283 |
241 // The kind of request that is currently in progress. | 284 // The kind of request that is currently in progress. |
242 SafeBrowsingRequestType request_type_; | 285 SafeBrowsingRequestType request_type_; |
(...skipping 84 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
327 std::string https_url_prefix_; | 370 std::string https_url_prefix_; |
328 | 371 |
329 // When true, protocol manager will not start an update unless | 372 // When true, protocol manager will not start an update unless |
330 // ForceScheduleNextUpdate() is called. This is set for testing purpose. | 373 // ForceScheduleNextUpdate() is called. This is set for testing purpose. |
331 bool disable_auto_update_; | 374 bool disable_auto_update_; |
332 | 375 |
333 DISALLOW_COPY_AND_ASSIGN(SafeBrowsingProtocolManager); | 376 DISALLOW_COPY_AND_ASSIGN(SafeBrowsingProtocolManager); |
334 }; | 377 }; |
335 | 378 |
336 #endif // CHROME_BROWSER_SAFE_BROWSING_PROTOCOL_MANAGER_H_ | 379 #endif // CHROME_BROWSER_SAFE_BROWSING_PROTOCOL_MANAGER_H_ |
OLD | NEW |