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

Side by Side Diff: chrome/browser/safe_browsing/browser_feature_extractor.cc

Issue 42553002: Mostly integrate new malware IP blacklist with the csd client. When (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 7 years, 1 month 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 | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 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 #include "chrome/browser/safe_browsing/browser_feature_extractor.h" 5 #include "chrome/browser/safe_browsing/browser_feature_extractor.h"
6 6
7 #include <map> 7 #include <map>
8 #include <utility> 8 #include <utility>
9 9
10 #include "base/bind.h" 10 #include "base/bind.h"
11 #include "base/bind_helpers.h" 11 #include "base/bind_helpers.h"
12 #include "base/format_macros.h" 12 #include "base/format_macros.h"
13 #include "base/stl_util.h" 13 #include "base/stl_util.h"
14 #include "base/strings/stringprintf.h" 14 #include "base/strings/stringprintf.h"
15 #include "base/time/time.h" 15 #include "base/time/time.h"
16 #include "chrome/browser/common/cancelable_request.h" 16 #include "chrome/browser/common/cancelable_request.h"
17 #include "chrome/browser/history/history_service.h" 17 #include "chrome/browser/history/history_service.h"
18 #include "chrome/browser/history/history_service_factory.h" 18 #include "chrome/browser/history/history_service_factory.h"
19 #include "chrome/browser/history/history_types.h" 19 #include "chrome/browser/history/history_types.h"
20 #include "chrome/browser/profiles/profile.h" 20 #include "chrome/browser/profiles/profile.h"
21 #include "chrome/browser/safe_browsing/browser_features.h" 21 #include "chrome/browser/safe_browsing/browser_features.h"
22 #include "chrome/browser/safe_browsing/client_side_detection_service.h" 22 #include "chrome/browser/safe_browsing/client_side_detection_host.h"
23 #include "chrome/common/safe_browsing/csd.pb.h" 23 #include "chrome/common/safe_browsing/csd.pb.h"
24 #include "content/public/browser/browser_thread.h" 24 #include "content/public/browser/browser_thread.h"
25 #include "content/public/browser/navigation_controller.h" 25 #include "content/public/browser/navigation_controller.h"
26 #include "content/public/browser/navigation_entry.h" 26 #include "content/public/browser/navigation_entry.h"
27 #include "content/public/browser/web_contents.h" 27 #include "content/public/browser/web_contents.h"
28 #include "content/public/common/page_transition_types.h" 28 #include "content/public/common/page_transition_types.h"
29 #include "url/gurl.h" 29 #include "url/gurl.h"
30 30
31 using content::BrowserThread; 31 using content::BrowserThread;
32 using content::NavigationController; 32 using content::NavigationController;
(...skipping 92 matching lines...) Expand 10 before | Expand all | Expand 10 after
125 features::kRedirect, 125 features::kRedirect,
126 i, 126 i,
127 printable_redirect.c_str()), 127 printable_redirect.c_str()),
128 1.0, 128 1.0,
129 request); 129 request);
130 } 130 }
131 } 131 }
132 132
133 BrowserFeatureExtractor::BrowserFeatureExtractor( 133 BrowserFeatureExtractor::BrowserFeatureExtractor(
134 WebContents* tab, 134 WebContents* tab,
135 ClientSideDetectionService* service) 135 ClientSideDetectionHost* host)
136 : tab_(tab), 136 : tab_(tab),
137 service_(service), 137 host_(host),
138 weak_factory_(this) { 138 weak_factory_(this) {
139 DCHECK(tab); 139 DCHECK(tab);
140 } 140 }
141 141
142 BrowserFeatureExtractor::~BrowserFeatureExtractor() { 142 BrowserFeatureExtractor::~BrowserFeatureExtractor() {
143 weak_factory_.InvalidateWeakPtrs(); 143 weak_factory_.InvalidateWeakPtrs();
144 // Delete all the pending extractions (delete callback and request objects). 144 // Delete all the pending extractions (delete callback and request objects).
145 STLDeleteContainerPairFirstPointers(pending_extractions_.begin(), 145 STLDeleteContainerPairFirstPointers(pending_extractions_.begin(),
146 pending_extractions_.end()); 146 pending_extractions_.end());
147 147
(...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after
223 223
224 ExtractBrowseInfoFeatures(*info, request); 224 ExtractBrowseInfoFeatures(*info, request);
225 pending_extractions_[request] = callback; 225 pending_extractions_[request] = callback;
226 base::MessageLoop::current()->PostTask( 226 base::MessageLoop::current()->PostTask(
227 FROM_HERE, 227 FROM_HERE,
228 base::Bind(&BrowserFeatureExtractor::StartExtractFeatures, 228 base::Bind(&BrowserFeatureExtractor::StartExtractFeatures,
229 weak_factory_.GetWeakPtr(), request, callback)); 229 weak_factory_.GetWeakPtr(), request, callback));
230 } 230 }
231 231
232 void BrowserFeatureExtractor::ExtractMalwareFeatures( 232 void BrowserFeatureExtractor::ExtractMalwareFeatures(
233 const BrowseInfo* info, 233 const BrowseInfo& info,
234 ClientMalwareRequest* request,
235 const MalwareDoneCallback& callback) {
236 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
237 DCHECK_EQ(0U, request->url().find("http:"));
238 DCHECK(!callback.is_null());
239 if (callback.is_null()) {
240 DLOG(ERROR) << "ExtractMalwareFeatures called without a callback object";
241 return;
mattm 2013/10/25 06:28:12 I think remove this, the DCHECK should be fine. Ch
noé 2013/10/28 23:39:26 Done.
mattm 2013/10/29 01:11:47 It looks like you removed the one from the other m
noé 2013/10/31 02:41:12 Removed it in both cases now :).
242 }
243 // Copy the IPs because they might go away before we're done
244 // checking them against the IP blacklist on the IO thread.
245 scoped_ptr<IPUrlMap> ips(new IPUrlMap(info.ips.begin(),
246 info.ips.end()));
mattm 2013/10/25 06:28:12 If the ips map isn't used by any other functions,
noé 2013/10/28 23:39:26 Done.
247
248 // IP blacklist lookups have to happen on the IO thread.
249 BrowserThread::PostTask(
250 BrowserThread::IO,
251 FROM_HERE,
252 base::Bind(&BrowserFeatureExtractor::StartExtractMalwareFeatures,
253 weak_factory_.GetWeakPtr(),
mattm 2013/10/25 06:28:12 weakptrs shouldn't be de-referenced on a different
noé 2013/10/28 23:39:26 Thank you very much for providing that example. T
254 base::Passed(&ips), request, callback));
255 }
256
257 static void CallMalwareCallback(
mattm 2013/10/25 06:28:12 Chromium style is to use anonymous namespaces rath
noé 2013/10/28 23:39:26 Done.
258 const BrowserFeatureExtractor::MalwareDoneCallback& callback,
259 bool success,
234 ClientMalwareRequest* request) { 260 ClientMalwareRequest* request) {
235 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 261 callback.Run(success, request);
236 DCHECK(request); 262 }
237 DCHECK(info); 263
238 DCHECK_EQ(0U, request->url().find("http:")); 264 void BrowserFeatureExtractor::StartExtractMalwareFeatures(
mattm 2013/10/25 06:28:12 Maybe name ExtractMalwareFeaturesOnIOThread?
noé 2013/10/28 23:39:26 Done.
265 scoped_ptr<IPUrlMap> ips,
266 ClientMalwareRequest* request,
267 const MalwareDoneCallback& callback) {
239 // get the IPs and urls that match the malware blacklisted IP list. 268 // get the IPs and urls that match the malware blacklisted IP list.
240 if (service_) { 269 if (!host_) {
241 int matched_bad_ips = 0; 270 callback.Run(false, request);
242 for (IPUrlMap::const_iterator it = info->ips.begin(); 271 return;
243 it != info->ips.end(); ++it) { 272 }
244 if (service_->IsBadIpAddress(it->first)) { 273 int matched_bad_ips = 0;
245 AddMalwareFeature(features::kBadIpFetch + it->first, 274 for (IPUrlMap::const_iterator it = ips->begin();
246 it->second, 1.0, request); 275 it != ips->end(); ++it) {
247 ++matched_bad_ips; 276 if (host_->IsBadIpAddress(it->first)) {
248 // Limit the number of matched bad IPs in one request to control 277 AddMalwareFeature(features::kBadIpFetch + it->first,
249 // the request's size 278 it->second, 1.0, request);
mattm 2013/10/25 06:28:12 I don't think AddMalwareFeature is threadsafe?
noé 2013/10/28 23:39:26 Can you elaborate why you think it is not thread s
mattm 2013/10/29 01:11:47 Oh, you're right. I missed that the request object
noé 2013/10/31 02:41:12 Done.
250 if (matched_bad_ips >= kMaxMalwareIPPerRequest) { 279 ++matched_bad_ips;
251 return; 280 // Limit the number of matched bad IPs in one request to control
252 } 281 // the request's size
282 if (matched_bad_ips >= kMaxMalwareIPPerRequest) {
283 break;
253 } 284 }
254 } 285 }
255 } 286 }
287 // We guanteed to the client of this API that the callback was going to
288 // be called on the UI thread.
289 BrowserThread::PostTask(
290 BrowserThread::UI,
291 FROM_HERE,
292 base::Bind(&CallMalwareCallback, callback, true, request));
mattm 2013/10/25 06:28:12 You can just do base::Bind(callback, true, request
noé 2013/10/28 23:39:26 Neat!
256 } 293 }
257 294
258 void BrowserFeatureExtractor::ExtractBrowseInfoFeatures( 295 void BrowserFeatureExtractor::ExtractBrowseInfoFeatures(
259 const BrowseInfo& info, 296 const BrowseInfo& info,
260 ClientPhishingRequest* request) { 297 ClientPhishingRequest* request) {
261 if (service_) {
262 for (IPUrlMap::const_iterator it = info.ips.begin();
263 it != info.ips.end(); ++it) {
264 if (service_->IsBadIpAddress(it->first)) {
265 AddFeature(features::kBadIpFetch + it->first, 1.0, request);
mattm 2013/10/25 06:28:12 This doesn't exist in the new version, intentional
noé 2013/10/28 23:39:26 Good catch :-). Yes, this is intentional. We don
266 }
267 }
268 }
269 if (info.unsafe_resource.get()) { 298 if (info.unsafe_resource.get()) {
270 // A SafeBrowsing interstitial was shown for the current URL. 299 // A SafeBrowsing interstitial was shown for the current URL.
271 AddFeature(features::kSafeBrowsingMaliciousUrl + 300 AddFeature(features::kSafeBrowsingMaliciousUrl +
272 info.unsafe_resource->url.spec(), 301 info.unsafe_resource->url.spec(),
273 1.0, 302 1.0,
274 request); 303 request);
275 AddFeature(features::kSafeBrowsingOriginalUrl + 304 AddFeature(features::kSafeBrowsingOriginalUrl +
276 info.unsafe_resource->original_url.spec(), 305 info.unsafe_resource->original_url.spec(),
277 1.0, 306 1.0,
278 request); 307 request);
(...skipping 210 matching lines...) Expand 10 before | Expand all | Expand 10 after
489 Profile::EXPLICIT_ACCESS); 518 Profile::EXPLICIT_ACCESS);
490 if (*history) { 519 if (*history) {
491 return true; 520 return true;
492 } 521 }
493 } 522 }
494 VLOG(2) << "Unable to query history. No history service available."; 523 VLOG(2) << "Unable to query history. No history service available.";
495 return false; 524 return false;
496 } 525 }
497 526
498 } // namespace safe_browsing 527 } // namespace safe_browsing
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698