| OLD | NEW |
| 1 // Copyright 2017 The Chromium Authors. All rights reserved. | 1 // Copyright 2017 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/permissions/permission_blacklist_client.h" | 5 #include "chrome/browser/permissions/permission_blacklist_client.h" |
| 6 | 6 |
| 7 #include <set> | 7 #include <set> |
| 8 #include <string> | 8 #include <string> |
| 9 | 9 |
| 10 #include "base/logging.h" | 10 #include "base/logging.h" |
| 11 #include "base/memory/ptr_util.h" | 11 #include "base/memory/ptr_util.h" |
| 12 #include "base/timer/elapsed_timer.h" |
| 12 #include "base/timer/timer.h" | 13 #include "base/timer/timer.h" |
| 14 #include "chrome/browser/permissions/permission_uma_util.h" |
| 15 #include "chrome/browser/permissions/permission_util.h" |
| 13 #include "content/public/browser/browser_thread.h" | 16 #include "content/public/browser/browser_thread.h" |
| 14 #include "content/public/browser/web_contents.h" | 17 #include "content/public/browser/web_contents.h" |
| 15 #include "url/gurl.h" | 18 #include "url/gurl.h" |
| 16 | 19 |
| 17 // static | 20 // static |
| 18 void PermissionBlacklistClient::CheckSafeBrowsingBlacklist( | 21 void PermissionBlacklistClient::CheckSafeBrowsingBlacklist( |
| 19 scoped_refptr<safe_browsing::SafeBrowsingDatabaseManager> db_manager, | 22 scoped_refptr<safe_browsing::SafeBrowsingDatabaseManager> db_manager, |
| 20 content::PermissionType permission_type, | 23 content::PermissionType permission_type, |
| 21 const GURL& request_origin, | 24 const GURL& request_origin, |
| 22 content::WebContents* web_contents, | 25 content::WebContents* web_contents, |
| (...skipping 27 matching lines...) Expand all Loading... |
| 50 | 53 |
| 51 PermissionBlacklistClient::~PermissionBlacklistClient() {} | 54 PermissionBlacklistClient::~PermissionBlacklistClient() {} |
| 52 | 55 |
| 53 void PermissionBlacklistClient::StartCheck(const GURL& request_origin) { | 56 void PermissionBlacklistClient::StartCheck(const GURL& request_origin) { |
| 54 DCHECK_CURRENTLY_ON(content::BrowserThread::IO); | 57 DCHECK_CURRENTLY_ON(content::BrowserThread::IO); |
| 55 | 58 |
| 56 // Start the timer to interrupt into the client callback method with an | 59 // Start the timer to interrupt into the client callback method with an |
| 57 // empty response if Safe Browsing times out. | 60 // empty response if Safe Browsing times out. |
| 58 safe_browsing::ThreatMetadata empty_metadata; | 61 safe_browsing::ThreatMetadata empty_metadata; |
| 59 timer_ = base::MakeUnique<base::OneShotTimer>(); | 62 timer_ = base::MakeUnique<base::OneShotTimer>(); |
| 63 elapsed_timer_.reset(new base::ElapsedTimer()); |
| 60 timer_->Start( | 64 timer_->Start( |
| 61 FROM_HERE, base::TimeDelta::FromMilliseconds(timeout_), | 65 FROM_HERE, base::TimeDelta::FromMilliseconds(timeout_), |
| 62 base::Bind(&PermissionBlacklistClient::OnCheckApiBlacklistUrlResult, this, | 66 base::Bind(&PermissionBlacklistClient::OnCheckApiBlacklistUrlResult, this, |
| 63 request_origin, empty_metadata)); | 67 request_origin, empty_metadata)); |
| 64 // If CheckApiBlacklistUrl returns true, no asynchronous call to |this| will | 68 // If CheckApiBlacklistUrl returns true, no asynchronous call to |this| will |
| 65 // be made, so just directly call through to OnCheckApiBlacklistUrlResult. | 69 // be made, so just directly call through to OnCheckApiBlacklistUrlResult. |
| 66 if (db_manager_->CheckApiBlacklistUrl(request_origin, this)) | 70 if (db_manager_->CheckApiBlacklistUrl(request_origin, this)) |
| 67 OnCheckApiBlacklistUrlResult(request_origin, empty_metadata); | 71 OnCheckApiBlacklistUrlResult(request_origin, empty_metadata); |
| 68 } | 72 } |
| 69 | 73 |
| 70 void PermissionBlacklistClient::OnCheckApiBlacklistUrlResult( | 74 void PermissionBlacklistClient::OnCheckApiBlacklistUrlResult( |
| 71 const GURL& url, | 75 const GURL& url, |
| 72 const safe_browsing::ThreatMetadata& metadata) { | 76 const safe_browsing::ThreatMetadata& metadata) { |
| 73 DCHECK_CURRENTLY_ON(content::BrowserThread::IO); | 77 DCHECK_CURRENTLY_ON(content::BrowserThread::IO); |
| 74 if (timer_->IsRunning()) | 78 |
| 79 base::TimeDelta response_time = elapsed_timer_->Elapsed(); |
| 80 SafeBrowsingResponse response = SafeBrowsingResponse::NOT_BLACKLISTED; |
| 81 |
| 82 if (timer_->IsRunning()) { |
| 75 timer_->Stop(); | 83 timer_->Stop(); |
| 76 else | 84 } else { |
| 77 db_manager_->CancelApiCheck(this); | 85 db_manager_->CancelApiCheck(this); |
| 86 response = SafeBrowsingResponse::TIMEOUT; |
| 87 } |
| 88 |
| 78 timer_.reset(nullptr); | 89 timer_.reset(nullptr); |
| 79 | |
| 80 bool permission_blocked = | 90 bool permission_blocked = |
| 81 metadata.api_permissions.find( | 91 metadata.api_permissions.find( |
| 82 PermissionUtil::ConvertPermissionTypeToSafeBrowsingName( | 92 PermissionUtil::ConvertPermissionTypeToSafeBrowsingName( |
| 83 permission_type_)) != metadata.api_permissions.end(); | 93 permission_type_)) != metadata.api_permissions.end(); |
| 94 if (permission_blocked) |
| 95 response = SafeBrowsingResponse::BLACKLISTED; |
| 84 | 96 |
| 97 PermissionUmaUtil::RecordSafeBrowsingResponse(response_time, response); |
| 85 content::BrowserThread::PostTask( | 98 content::BrowserThread::PostTask( |
| 86 content::BrowserThread::UI, FROM_HERE, | 99 content::BrowserThread::UI, FROM_HERE, |
| 87 base::Bind(&PermissionBlacklistClient::EvaluateBlacklistResultOnUiThread, | 100 base::Bind(&PermissionBlacklistClient::EvaluateBlacklistResultOnUiThread, |
| 88 this, permission_blocked)); | 101 this, permission_blocked)); |
| 89 } | 102 } |
| 90 | 103 |
| 91 void PermissionBlacklistClient::EvaluateBlacklistResultOnUiThread( | 104 void PermissionBlacklistClient::EvaluateBlacklistResultOnUiThread( |
| 92 bool permission_blocked) { | 105 bool response) { |
| 93 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | 106 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| 94 | 107 |
| 95 if (is_active_) | 108 if (is_active_) |
| 96 callback_.Run(permission_blocked); | 109 callback_.Run(response); |
| 97 Release(); | 110 Release(); |
| 98 } | 111 } |
| 99 | 112 |
| 100 void PermissionBlacklistClient::WebContentsDestroyed() { | 113 void PermissionBlacklistClient::WebContentsDestroyed() { |
| 101 is_active_ = false; | 114 is_active_ = false; |
| 102 Observe(nullptr); | 115 Observe(nullptr); |
| 103 } | 116 } |
| OLD | NEW |