| 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" |
| 11 #include "base/memory/scoped_ptr.h" | 11 #include "base/memory/scoped_ptr.h" |
| 12 #include "base/metrics/histogram.h" | 12 #include "base/metrics/histogram.h" |
| 13 #include "base/task.h" | 13 #include "base/task.h" |
| 14 #include "chrome/browser/browser_process.h" | 14 #include "chrome/browser/browser_process.h" |
| 15 #include "chrome/browser/profiles/profile.h" | 15 #include "chrome/browser/profiles/profile.h" |
| 16 #include "chrome/browser/safe_browsing/browser_feature_extractor.h" |
| 16 #include "chrome/browser/safe_browsing/client_side_detection_service.h" | 17 #include "chrome/browser/safe_browsing/client_side_detection_service.h" |
| 17 #include "chrome/browser/safe_browsing/safe_browsing_service.h" | 18 #include "chrome/browser/safe_browsing/safe_browsing_service.h" |
| 18 #include "chrome/common/chrome_switches.h" | 19 #include "chrome/common/chrome_switches.h" |
| 19 #include "chrome/common/safe_browsing/csd.pb.h" | 20 #include "chrome/common/safe_browsing/csd.pb.h" |
| 20 #include "chrome/common/safe_browsing/safebrowsing_messages.h" | 21 #include "chrome/common/safe_browsing/safebrowsing_messages.h" |
| 21 #include "content/browser/browser_thread.h" | 22 #include "content/browser/browser_thread.h" |
| 22 #include "content/browser/renderer_host/render_process_host.h" | 23 #include "content/browser/renderer_host/render_process_host.h" |
| 23 #include "content/browser/renderer_host/render_view_host.h" | 24 #include "content/browser/renderer_host/render_view_host.h" |
| 24 #include "content/browser/renderer_host/resource_dispatcher_host.h" | 25 #include "content/browser/renderer_host/resource_dispatcher_host.h" |
| 25 #include "content/browser/tab_contents/navigation_details.h" | 26 #include "content/browser/tab_contents/navigation_details.h" |
| (...skipping 231 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 257 | 258 |
| 258 // static | 259 // static |
| 259 ClientSideDetectionHost* ClientSideDetectionHost::Create( | 260 ClientSideDetectionHost* ClientSideDetectionHost::Create( |
| 260 TabContents* tab) { | 261 TabContents* tab) { |
| 261 return new ClientSideDetectionHost(tab); | 262 return new ClientSideDetectionHost(tab); |
| 262 } | 263 } |
| 263 | 264 |
| 264 ClientSideDetectionHost::ClientSideDetectionHost(TabContents* tab) | 265 ClientSideDetectionHost::ClientSideDetectionHost(TabContents* tab) |
| 265 : TabContentsObserver(tab), | 266 : TabContentsObserver(tab), |
| 266 csd_service_(g_browser_process->safe_browsing_detection_service()), | 267 csd_service_(g_browser_process->safe_browsing_detection_service()), |
| 268 feature_extractor_(new BrowserFeatureExtractor(tab)), |
| 267 cb_factory_(ALLOW_THIS_IN_INITIALIZER_LIST(this)) { | 269 cb_factory_(ALLOW_THIS_IN_INITIALIZER_LIST(this)) { |
| 268 DCHECK(tab); | 270 DCHECK(tab); |
| 269 // Note: csd_service_ and sb_service_ might be NULL. | 271 // Note: csd_service_ and sb_service_ might be NULL. |
| 270 sb_service_ = g_browser_process->safe_browsing_service(); | 272 sb_service_ = g_browser_process->safe_browsing_service(); |
| 271 } | 273 } |
| 272 | 274 |
| 273 ClientSideDetectionHost::~ClientSideDetectionHost() { | 275 ClientSideDetectionHost::~ClientSideDetectionHost() {} |
| 274 // Tell any pending classification request that it is being canceled. | |
| 275 if (classification_request_.get()) { | |
| 276 classification_request_->Cancel(); | |
| 277 } | |
| 278 } | |
| 279 | 276 |
| 280 bool ClientSideDetectionHost::OnMessageReceived(const IPC::Message& message) { | 277 bool ClientSideDetectionHost::OnMessageReceived(const IPC::Message& message) { |
| 281 bool handled = true; | 278 bool handled = true; |
| 282 IPC_BEGIN_MESSAGE_MAP(ClientSideDetectionHost, message) | 279 IPC_BEGIN_MESSAGE_MAP(ClientSideDetectionHost, message) |
| 283 IPC_MESSAGE_HANDLER(SafeBrowsingHostMsg_DetectedPhishingSite, | 280 IPC_MESSAGE_HANDLER(SafeBrowsingHostMsg_DetectedPhishingSite, |
| 284 OnDetectedPhishingSite) | 281 OnDetectedPhishingSite) |
| 285 IPC_MESSAGE_UNHANDLED(handled = false) | 282 IPC_MESSAGE_UNHANDLED(handled = false) |
| 286 IPC_END_MESSAGE_MAP() | 283 IPC_END_MESSAGE_MAP() |
| 287 return handled; | 284 return handled; |
| 288 } | 285 } |
| (...skipping 28 matching lines...) Expand all Loading... |
| 317 // Notify the renderer if it should classify this URL. | 314 // Notify the renderer if it should classify this URL. |
| 318 classification_request_ = new ShouldClassifyUrlRequest(params, | 315 classification_request_ = new ShouldClassifyUrlRequest(params, |
| 319 tab_contents(), | 316 tab_contents(), |
| 320 csd_service_, | 317 csd_service_, |
| 321 sb_service_, | 318 sb_service_, |
| 322 this); | 319 this); |
| 323 classification_request_->Start(); | 320 classification_request_->Start(); |
| 324 } | 321 } |
| 325 } | 322 } |
| 326 | 323 |
| 324 void ClientSideDetectionHost::TabContentsDestroyed(TabContents* tab) { |
| 325 DCHECK(tab); |
| 326 // Tell any pending classification request that it is being canceled. |
| 327 if (classification_request_.get()) { |
| 328 classification_request_->Cancel(); |
| 329 } |
| 330 // Cancel all pending feature extractions. |
| 331 feature_extractor_.reset(); |
| 332 } |
| 333 |
| 327 void ClientSideDetectionHost::OnDetectedPhishingSite( | 334 void ClientSideDetectionHost::OnDetectedPhishingSite( |
| 328 const std::string& verdict_str) { | 335 const std::string& verdict_str) { |
| 329 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 336 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 330 // There is something seriously wrong if there is no service class but | 337 // There is something seriously wrong if there is no service class but |
| 331 // this method is called. The renderer should not start phishing detection | 338 // this method is called. The renderer should not start phishing detection |
| 332 // if there isn't any service class in the browser. | 339 // if there isn't any service class in the browser. |
| 333 DCHECK(csd_service_); | 340 DCHECK(csd_service_); |
| 334 // We parse the protocol buffer here. If we're unable to parse it we won't | 341 // We parse the protocol buffer here. If we're unable to parse it we won't |
| 335 // send the verdict further. | 342 // send the verdict further. |
| 336 scoped_ptr<ClientPhishingRequest> verdict(new ClientPhishingRequest); | 343 scoped_ptr<ClientPhishingRequest> verdict(new ClientPhishingRequest); |
| 337 if (csd_service_ && | 344 if (csd_service_ && |
| 338 verdict->ParseFromString(verdict_str) && | 345 verdict->ParseFromString(verdict_str) && |
| 339 verdict->IsInitialized()) { | 346 verdict->IsInitialized()) { |
| 340 // There shouldn't be any pending requests because we revoke them everytime | 347 // There shouldn't be any pending requests because we revoke them everytime |
| 341 // we navigate away. | 348 // we navigate away. |
| 342 DCHECK(!cb_factory_.HasPendingCallbacks()); | 349 DCHECK(!cb_factory_.HasPendingCallbacks()); |
| 343 VLOG(2) << "Start sending client phishing request for URL: " | 350 |
| 344 << verdict->url(); | 351 // Start browser-side feature extraction. Once we're done it will send |
| 345 csd_service_->SendClientReportPhishingRequest( | 352 // the client verdict request. |
| 346 verdict.release(), // The service takes ownership of the verdict. | 353 feature_extractor_->ExtractFeatures( |
| 347 cb_factory_.NewCallback( | 354 verdict.release(), |
| 348 &ClientSideDetectionHost::MaybeShowPhishingWarning)); | 355 NewCallback(this, &ClientSideDetectionHost::FeatureExtractionDone)); |
| 349 } | 356 } |
| 350 } | 357 } |
| 351 | 358 |
| 352 void ClientSideDetectionHost::MaybeShowPhishingWarning(GURL phishing_url, | 359 void ClientSideDetectionHost::MaybeShowPhishingWarning(GURL phishing_url, |
| 353 bool is_phishing) { | 360 bool is_phishing) { |
| 354 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 361 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 355 VLOG(2) << "Received server phishing verdict for URL:" << phishing_url | 362 VLOG(2) << "Received server phishing verdict for URL:" << phishing_url |
| 356 << " is_phishing:" << is_phishing; | 363 << " is_phishing:" << is_phishing; |
| 357 if (is_phishing) { | 364 if (is_phishing) { |
| 358 DCHECK(tab_contents()); | 365 DCHECK(tab_contents()); |
| (...skipping 11 matching lines...) Expand all Loading... |
| 370 // We need to stop any pending navigations, otherwise the interstital | 377 // We need to stop any pending navigations, otherwise the interstital |
| 371 // might not get created properly. | 378 // might not get created properly. |
| 372 tab_contents()->controller().DiscardNonCommittedEntries(); | 379 tab_contents()->controller().DiscardNonCommittedEntries(); |
| 373 resource.client = new CsdClient(); // Will delete itself | 380 resource.client = new CsdClient(); // Will delete itself |
| 374 sb_service_->DoDisplayBlockingPage(resource); | 381 sb_service_->DoDisplayBlockingPage(resource); |
| 375 } | 382 } |
| 376 } | 383 } |
| 377 } | 384 } |
| 378 } | 385 } |
| 379 | 386 |
| 387 void ClientSideDetectionHost::FeatureExtractionDone( |
| 388 bool success, |
| 389 ClientPhishingRequest* request) { |
| 390 if (!request) { |
| 391 DLOG(FATAL) << "Invalid request object in FeatureExtractionDone"; |
| 392 return; |
| 393 } |
| 394 VLOG(2) << "Feature extraction done (success:" << success << ") for URL: " |
| 395 << request->url() << ". Start sending client phishing request."; |
| 396 // Send ping no matter what - even if the browser feature extraction failed. |
| 397 csd_service_->SendClientReportPhishingRequest( |
| 398 request, // The service takes ownership of the request object. |
| 399 cb_factory_.NewCallback( |
| 400 &ClientSideDetectionHost::MaybeShowPhishingWarning)); |
| 401 } |
| 402 |
| 380 void ClientSideDetectionHost::set_client_side_detection_service( | 403 void ClientSideDetectionHost::set_client_side_detection_service( |
| 381 ClientSideDetectionService* service) { | 404 ClientSideDetectionService* service) { |
| 382 csd_service_ = service; | 405 csd_service_ = service; |
| 383 } | 406 } |
| 384 | 407 |
| 385 void ClientSideDetectionHost::set_safe_browsing_service( | 408 void ClientSideDetectionHost::set_safe_browsing_service( |
| 386 SafeBrowsingService* service) { | 409 SafeBrowsingService* service) { |
| 387 sb_service_ = service; | 410 sb_service_ = service; |
| 388 } | 411 } |
| 389 | 412 |
| 390 } // namespace safe_browsing | 413 } // namespace safe_browsing |
| OLD | NEW |