OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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/client_side_detection_host.h" | 5 #include "chrome/browser/safe_browsing/client_side_detection_host.h" |
6 | 6 |
7 #include <vector> | 7 #include <vector> |
8 | 8 |
9 #include "base/logging.h" | 9 #include "base/logging.h" |
10 #include "base/memory/ref_counted.h" | 10 #include "base/memory/ref_counted.h" |
(...skipping 251 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
262 | 262 |
263 // static | 263 // static |
264 ClientSideDetectionHost* ClientSideDetectionHost::Create( | 264 ClientSideDetectionHost* ClientSideDetectionHost::Create( |
265 TabContents* tab) { | 265 TabContents* tab) { |
266 return new ClientSideDetectionHost(tab); | 266 return new ClientSideDetectionHost(tab); |
267 } | 267 } |
268 | 268 |
269 ClientSideDetectionHost::ClientSideDetectionHost(TabContents* tab) | 269 ClientSideDetectionHost::ClientSideDetectionHost(TabContents* tab) |
270 : TabContentsObserver(tab), | 270 : TabContentsObserver(tab), |
271 csd_service_(NULL), | 271 csd_service_(NULL), |
272 cb_factory_(ALLOW_THIS_IN_INITIALIZER_LIST(this)) { | 272 cb_factory_(ALLOW_THIS_IN_INITIALIZER_LIST(this)), |
| 273 unsafe_unique_page_id_(-1) { |
273 DCHECK(tab); | 274 DCHECK(tab); |
274 csd_service_ = g_browser_process->safe_browsing_detection_service(); | 275 csd_service_ = g_browser_process->safe_browsing_detection_service(); |
275 feature_extractor_.reset(new BrowserFeatureExtractor(tab, csd_service_)); | 276 feature_extractor_.reset(new BrowserFeatureExtractor(tab, csd_service_)); |
276 sb_service_ = g_browser_process->safe_browsing_service(); | 277 sb_service_ = g_browser_process->safe_browsing_service(); |
277 // Note: csd_service_ and sb_service_ will be NULL here in testing. | 278 // Note: csd_service_ and sb_service_ will be NULL here in testing. |
278 registrar_.Add(this, content::NOTIFICATION_RESOURCE_RESPONSE_STARTED, | 279 registrar_.Add(this, content::NOTIFICATION_RESOURCE_RESPONSE_STARTED, |
279 Source<RenderViewHostDelegate>(tab)); | 280 Source<RenderViewHostDelegate>(tab)); |
| 281 if (sb_service_) { |
| 282 sb_service_->AddObserver(this); |
| 283 } |
280 } | 284 } |
281 | 285 |
282 ClientSideDetectionHost::~ClientSideDetectionHost() {} | 286 ClientSideDetectionHost::~ClientSideDetectionHost() { |
| 287 if (sb_service_) { |
| 288 sb_service_->RemoveObserver(this); |
| 289 } |
| 290 } |
283 | 291 |
284 bool ClientSideDetectionHost::OnMessageReceived(const IPC::Message& message) { | 292 bool ClientSideDetectionHost::OnMessageReceived(const IPC::Message& message) { |
285 bool handled = true; | 293 bool handled = true; |
286 IPC_BEGIN_MESSAGE_MAP(ClientSideDetectionHost, message) | 294 IPC_BEGIN_MESSAGE_MAP(ClientSideDetectionHost, message) |
287 IPC_MESSAGE_HANDLER(SafeBrowsingHostMsg_DetectedPhishingSite, | 295 IPC_MESSAGE_HANDLER(SafeBrowsingHostMsg_PhishingDetectionDone, |
288 OnDetectedPhishingSite) | 296 OnPhishingDetectionDone) |
289 IPC_MESSAGE_UNHANDLED(handled = false) | 297 IPC_MESSAGE_UNHANDLED(handled = false) |
290 IPC_END_MESSAGE_MAP() | 298 IPC_END_MESSAGE_MAP() |
291 return handled; | 299 return handled; |
292 } | 300 } |
293 | 301 |
294 void ClientSideDetectionHost::DidNavigateMainFramePostCommit( | 302 void ClientSideDetectionHost::DidNavigateMainFramePostCommit( |
295 const content::LoadCommittedDetails& details, | 303 const content::LoadCommittedDetails& details, |
296 const ViewHostMsg_FrameNavigate_Params& params) { | 304 const ViewHostMsg_FrameNavigate_Params& params) { |
297 // TODO(noelutz): move this DCHECK to TabContents and fix all the unit tests | 305 // TODO(noelutz): move this DCHECK to TabContents and fix all the unit tests |
298 // that don't call this method on the UI thread. | 306 // that don't call this method on the UI thread. |
(...skipping 23 matching lines...) Expand all Loading... |
322 | 330 |
323 // Notify the renderer if it should classify this URL. | 331 // Notify the renderer if it should classify this URL. |
324 classification_request_ = new ShouldClassifyUrlRequest(params, | 332 classification_request_ = new ShouldClassifyUrlRequest(params, |
325 tab_contents(), | 333 tab_contents(), |
326 csd_service_, | 334 csd_service_, |
327 sb_service_, | 335 sb_service_, |
328 this); | 336 this); |
329 classification_request_->Start(); | 337 classification_request_->Start(); |
330 } | 338 } |
331 | 339 |
| 340 void ClientSideDetectionHost::OnSafeBrowsingHit( |
| 341 const SafeBrowsingService::UnsafeResource& resource) { |
| 342 // Check that this notification is really for us and that it corresponds to |
| 343 // either a malware or phishing hit. In this case we store the unique page |
| 344 // ID for later. |
| 345 if (tab_contents() && |
| 346 tab_contents()->GetRenderProcessHost()->id() == |
| 347 resource.render_process_host_id && |
| 348 tab_contents()->render_view_host()->routing_id() == |
| 349 resource.render_view_id && |
| 350 (resource.threat_type == SafeBrowsingService::URL_PHISHING || |
| 351 resource.threat_type == SafeBrowsingService::URL_MALWARE) && |
| 352 tab_contents()->controller().GetActiveEntry()) { |
| 353 unsafe_unique_page_id_ = |
| 354 tab_contents()->controller().GetActiveEntry()->unique_id(); |
| 355 } |
| 356 } |
| 357 |
332 void ClientSideDetectionHost::TabContentsDestroyed(TabContents* tab) { | 358 void ClientSideDetectionHost::TabContentsDestroyed(TabContents* tab) { |
333 DCHECK(tab); | 359 DCHECK(tab); |
334 // Tell any pending classification request that it is being canceled. | 360 // Tell any pending classification request that it is being canceled. |
335 if (classification_request_.get()) { | 361 if (classification_request_.get()) { |
336 classification_request_->Cancel(); | 362 classification_request_->Cancel(); |
337 } | 363 } |
338 // Cancel all pending feature extractions. | 364 // Cancel all pending feature extractions. |
339 feature_extractor_.reset(); | 365 feature_extractor_.reset(); |
340 } | 366 } |
341 | 367 |
342 void ClientSideDetectionHost::OnDetectedPhishingSite( | 368 void ClientSideDetectionHost::OnPhishingDetectionDone( |
343 const std::string& verdict_str) { | 369 const std::string& verdict_str) { |
344 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 370 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
345 // There is something seriously wrong if there is no service class but | 371 // There is something seriously wrong if there is no service class but |
346 // this method is called. The renderer should not start phishing detection | 372 // this method is called. The renderer should not start phishing detection |
347 // if there isn't any service class in the browser. | 373 // if there isn't any service class in the browser. |
348 DCHECK(csd_service_); | 374 DCHECK(csd_service_); |
349 // There shouldn't be any pending requests because we revoke them everytime | 375 // There shouldn't be any pending requests because we revoke them everytime |
350 // we navigate away. | 376 // we navigate away. |
351 DCHECK(!cb_factory_.HasPendingCallbacks()); | 377 DCHECK(!cb_factory_.HasPendingCallbacks()); |
352 DCHECK(browse_info_.get()); | 378 DCHECK(browse_info_.get()); |
353 | 379 |
354 // We parse the protocol buffer here. If we're unable to parse it we won't | 380 // We parse the protocol buffer here. If we're unable to parse it we won't |
355 // send the verdict further. | 381 // send the verdict further. |
356 scoped_ptr<ClientPhishingRequest> verdict(new ClientPhishingRequest); | 382 scoped_ptr<ClientPhishingRequest> verdict(new ClientPhishingRequest); |
357 if (csd_service_ && | 383 if (csd_service_ && |
358 !cb_factory_.HasPendingCallbacks() && | 384 !cb_factory_.HasPendingCallbacks() && |
359 browse_info_.get() && | 385 browse_info_.get() && |
360 verdict->ParseFromString(verdict_str) && | 386 verdict->ParseFromString(verdict_str) && |
361 verdict->IsInitialized()) { | 387 verdict->IsInitialized() && |
| 388 // We only send the verdict to the server if the verdict is phishing or if |
| 389 // a SafeBrowsing interstitial was already shown for this site. E.g., a |
| 390 // malware or phishing interstitial was shown but the user clicked |
| 391 // through. |
| 392 (verdict->is_phishing() || DidShowSBInterstitial())) { |
362 // Start browser-side feature extraction. Once we're done it will send | 393 // Start browser-side feature extraction. Once we're done it will send |
363 // the client verdict request. | 394 // the client verdict request. |
364 feature_extractor_->ExtractFeatures( | 395 feature_extractor_->ExtractFeatures( |
365 browse_info_.get(), | 396 browse_info_.get(), |
366 verdict.release(), | 397 verdict.release(), |
367 NewCallback(this, &ClientSideDetectionHost::FeatureExtractionDone)); | 398 NewCallback(this, &ClientSideDetectionHost::FeatureExtractionDone)); |
368 } | 399 } |
369 browse_info_.reset(); | 400 browse_info_.reset(); |
370 } | 401 } |
371 | 402 |
(...skipping 27 matching lines...) Expand all Loading... |
399 | 430 |
400 void ClientSideDetectionHost::FeatureExtractionDone( | 431 void ClientSideDetectionHost::FeatureExtractionDone( |
401 bool success, | 432 bool success, |
402 ClientPhishingRequest* request) { | 433 ClientPhishingRequest* request) { |
403 if (!request) { | 434 if (!request) { |
404 DLOG(FATAL) << "Invalid request object in FeatureExtractionDone"; | 435 DLOG(FATAL) << "Invalid request object in FeatureExtractionDone"; |
405 return; | 436 return; |
406 } | 437 } |
407 VLOG(2) << "Feature extraction done (success:" << success << ") for URL: " | 438 VLOG(2) << "Feature extraction done (success:" << success << ") for URL: " |
408 << request->url() << ". Start sending client phishing request."; | 439 << request->url() << ". Start sending client phishing request."; |
409 // Send ping no matter what - even if the browser feature extraction failed. | 440 ClientSideDetectionService::ClientReportPhishingRequestCallback* cb = NULL; |
| 441 // If the client-side verdict isn't phishing we don't care about the server |
| 442 // response because we aren't going to display a warning. |
| 443 if (request->is_phishing()) { |
| 444 cb = cb_factory_.NewCallback( |
| 445 &ClientSideDetectionHost::MaybeShowPhishingWarning); |
| 446 } |
| 447 // Send ping even if the browser feature extraction failed. |
410 csd_service_->SendClientReportPhishingRequest( | 448 csd_service_->SendClientReportPhishingRequest( |
411 request, // The service takes ownership of the request object. | 449 request, // The service takes ownership of the request object. |
412 cb_factory_.NewCallback( | 450 cb); |
413 &ClientSideDetectionHost::MaybeShowPhishingWarning)); | |
414 } | 451 } |
415 | 452 |
416 void ClientSideDetectionHost::Observe(int type, | 453 void ClientSideDetectionHost::Observe(int type, |
417 const NotificationSource& source, | 454 const NotificationSource& source, |
418 const NotificationDetails& details) { | 455 const NotificationDetails& details) { |
419 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 456 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
420 DCHECK_EQ(type, content::NOTIFICATION_RESOURCE_RESPONSE_STARTED); | 457 DCHECK_EQ(type, content::NOTIFICATION_RESOURCE_RESPONSE_STARTED); |
421 const ResourceRequestDetails* req = Details<ResourceRequestDetails>( | 458 const ResourceRequestDetails* req = Details<ResourceRequestDetails>( |
422 details).ptr(); | 459 details).ptr(); |
423 if (req && browse_info_.get()) { | 460 if (req && browse_info_.get()) { |
424 browse_info_->ips.insert(req->socket_address().host()); | 461 browse_info_->ips.insert(req->socket_address().host()); |
425 } | 462 } |
426 } | 463 } |
427 | 464 |
| 465 bool ClientSideDetectionHost::DidShowSBInterstitial() { |
| 466 if (unsafe_unique_page_id_ <= 0 || !tab_contents()) { |
| 467 return false; |
| 468 } |
| 469 const NavigationEntry* nav_entry = |
| 470 tab_contents()->controller().GetActiveEntry(); |
| 471 return (nav_entry && nav_entry->unique_id() == unsafe_unique_page_id_); |
| 472 } |
| 473 |
428 void ClientSideDetectionHost::set_client_side_detection_service( | 474 void ClientSideDetectionHost::set_client_side_detection_service( |
429 ClientSideDetectionService* service) { | 475 ClientSideDetectionService* service) { |
430 csd_service_ = service; | 476 csd_service_ = service; |
431 } | 477 } |
432 | 478 |
433 void ClientSideDetectionHost::set_safe_browsing_service( | 479 void ClientSideDetectionHost::set_safe_browsing_service( |
434 SafeBrowsingService* service) { | 480 SafeBrowsingService* service) { |
| 481 if (sb_service_) { |
| 482 sb_service_->RemoveObserver(this); |
| 483 } |
435 sb_service_ = service; | 484 sb_service_ = service; |
| 485 if (sb_service_) { |
| 486 sb_service_->AddObserver(this); |
| 487 } |
436 } | 488 } |
437 | 489 |
438 } // namespace safe_browsing | 490 } // namespace safe_browsing |
OLD | NEW |