OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "chrome/browser/permissions/permission_blacklist_client.h" |
| 6 |
| 7 #include <set> |
| 8 #include <string> |
| 9 |
| 10 #include "base/logging.h" |
| 11 #include "base/memory/ptr_util.h" |
| 12 #include "base/timer/timer.h" |
| 13 #include "content/public/browser/browser_thread.h" |
| 14 #include "content/public/browser/web_contents.h" |
| 15 #include "url/gurl.h" |
| 16 |
| 17 // static |
| 18 void PermissionBlacklistClient::CheckSafeBrowsingBlacklist( |
| 19 scoped_refptr<safe_browsing::SafeBrowsingDatabaseManager> db_manager, |
| 20 content::PermissionType permission_type, |
| 21 const GURL& request_origin, |
| 22 content::WebContents* web_contents, |
| 23 int timeout, |
| 24 base::Callback<void(bool)> callback) { |
| 25 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| 26 |
| 27 new PermissionBlacklistClient(db_manager, permission_type, request_origin, |
| 28 web_contents, timeout, callback); |
| 29 } |
| 30 |
| 31 PermissionBlacklistClient::PermissionBlacklistClient( |
| 32 scoped_refptr<safe_browsing::SafeBrowsingDatabaseManager> db_manager, |
| 33 content::PermissionType permission_type, |
| 34 const GURL& request_origin, |
| 35 content::WebContents* web_contents, |
| 36 int timeout, |
| 37 base::Callback<void(bool)> callback) |
| 38 : content::WebContentsObserver(web_contents), |
| 39 db_manager_(db_manager), |
| 40 permission_type_(permission_type), |
| 41 callback_(callback), |
| 42 timeout_(timeout), |
| 43 is_active_(true) { |
| 44 // Balanced by a call to Release() in OnCheckApiBlacklistUrlResult(). |
| 45 AddRef(); |
| 46 content::BrowserThread::PostTask( |
| 47 content::BrowserThread::IO, FROM_HERE, |
| 48 base::Bind(&PermissionBlacklistClient::StartCheck, this, request_origin)); |
| 49 } |
| 50 |
| 51 PermissionBlacklistClient::~PermissionBlacklistClient() {} |
| 52 |
| 53 void PermissionBlacklistClient::StartCheck(const GURL& request_origin) { |
| 54 DCHECK_CURRENTLY_ON(content::BrowserThread::IO); |
| 55 |
| 56 // Start the timer to interrupt into the client callback method with an |
| 57 // empty response if Safe Browsing times out. |
| 58 safe_browsing::ThreatMetadata empty_metadata; |
| 59 timer_ = base::MakeUnique<base::OneShotTimer>(); |
| 60 timer_->Start( |
| 61 FROM_HERE, base::TimeDelta::FromMilliseconds(timeout_), |
| 62 base::Bind(&PermissionBlacklistClient::OnCheckApiBlacklistUrlResult, this, |
| 63 request_origin, empty_metadata)); |
| 64 db_manager_->CheckApiBlacklistUrl(request_origin, this); |
| 65 } |
| 66 |
| 67 void PermissionBlacklistClient::OnCheckApiBlacklistUrlResult( |
| 68 const GURL& url, |
| 69 const safe_browsing::ThreatMetadata& metadata) { |
| 70 DCHECK_CURRENTLY_ON(content::BrowserThread::IO); |
| 71 |
| 72 if (timer_->IsRunning()) |
| 73 timer_->Stop(); |
| 74 else |
| 75 db_manager_->CancelApiCheck(this); |
| 76 timer_.reset(nullptr); |
| 77 |
| 78 // TODO(meredithl): Convert the strings returned from Safe Browsing to the |
| 79 // ones used by PermissionUtil for comparison. |
| 80 bool permission_blocked = |
| 81 metadata.api_permissions.find(PermissionUtil::GetPermissionString( |
| 82 permission_type_)) != metadata.api_permissions.end(); |
| 83 |
| 84 content::BrowserThread::PostTask( |
| 85 content::BrowserThread::UI, FROM_HERE, |
| 86 base::Bind(&PermissionBlacklistClient::EvaluateBlacklistResultOnUiThread, |
| 87 this, permission_blocked)); |
| 88 } |
| 89 |
| 90 void PermissionBlacklistClient::EvaluateBlacklistResultOnUiThread( |
| 91 bool permission_blocked) { |
| 92 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| 93 |
| 94 if (is_active_) |
| 95 callback_.Run(permission_blocked); |
| 96 Release(); |
| 97 } |
| 98 |
| 99 void PermissionBlacklistClient::WebContentsDestroyed() { |
| 100 is_active_ = false; |
| 101 Observe(nullptr); |
| 102 } |
OLD | NEW |