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