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" | |
raymes
2017/02/08 02:08:00
nit: is this needed?
meredithl
2017/02/08 02:40:32
It is used to convert between the PermissionType t
| |
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 db_manager_->CheckApiBlacklistUrl(request_origin, this); | 68 db_manager_->CheckApiBlacklistUrl(request_origin, this); |
65 } | 69 } |
66 | 70 |
67 void PermissionBlacklistClient::OnCheckApiBlacklistUrlResult( | 71 void PermissionBlacklistClient::OnCheckApiBlacklistUrlResult( |
68 const GURL& url, | 72 const GURL& url, |
69 const safe_browsing::ThreatMetadata& metadata) { | 73 const safe_browsing::ThreatMetadata& metadata) { |
70 DCHECK_CURRENTLY_ON(content::BrowserThread::IO); | 74 DCHECK_CURRENTLY_ON(content::BrowserThread::IO); |
71 | 75 |
72 if (timer_->IsRunning()) | 76 base::TimeDelta response_time = elapsed_timer_->Elapsed(); |
77 SafeBrowsingResponse response = SafeBrowsingResponse::NOT_BLACKLISTED; | |
78 | |
79 if (timer_->IsRunning()) { | |
73 timer_->Stop(); | 80 timer_->Stop(); |
74 else | 81 } else { |
75 db_manager_->CancelApiCheck(this); | 82 db_manager_->CancelApiCheck(this); |
83 response = SafeBrowsingResponse::TIMEOUT; | |
84 } | |
85 | |
76 timer_.reset(nullptr); | 86 timer_.reset(nullptr); |
77 | |
78 bool permission_blocked = | 87 bool permission_blocked = |
79 metadata.api_permissions.find( | 88 metadata.api_permissions.find( |
80 PermissionUtil::ConvertPermissionTypeToSafeBrowsingName( | 89 PermissionUtil::ConvertPermissionTypeToSafeBrowsingName( |
81 permission_type_)) != metadata.api_permissions.end(); | 90 permission_type_)) != metadata.api_permissions.end(); |
91 if (permission_blocked) | |
92 response = SafeBrowsingResponse::BLACKLISTED; | |
82 | 93 |
94 PermissionUmaUtil::RecordSafeBrowsingResponse(response_time, response); | |
83 content::BrowserThread::PostTask( | 95 content::BrowserThread::PostTask( |
84 content::BrowserThread::UI, FROM_HERE, | 96 content::BrowserThread::UI, FROM_HERE, |
85 base::Bind(&PermissionBlacklistClient::EvaluateBlacklistResultOnUiThread, | 97 base::Bind(&PermissionBlacklistClient::EvaluateBlacklistResultOnUiThread, |
86 this, permission_blocked)); | 98 this, permission_blocked)); |
87 } | 99 } |
88 | 100 |
89 void PermissionBlacklistClient::EvaluateBlacklistResultOnUiThread( | 101 void PermissionBlacklistClient::EvaluateBlacklistResultOnUiThread( |
90 bool permission_blocked) { | 102 bool response) { |
91 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | 103 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
92 | 104 |
93 if (is_active_) | 105 if (is_active_) |
94 callback_.Run(permission_blocked); | 106 callback_.Run(response); |
95 Release(); | 107 Release(); |
96 } | 108 } |
97 | 109 |
98 void PermissionBlacklistClient::WebContentsDestroyed() { | 110 void PermissionBlacklistClient::WebContentsDestroyed() { |
99 is_active_ = false; | 111 is_active_ = false; |
100 Observe(nullptr); | 112 Observe(nullptr); |
101 } | 113 } |
OLD | NEW |