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 #ifndef CHROME_BROWSER_PERMISSIONS_PERMISSION_BLACKLIST_CLIENT_H_ | 5 #ifndef CHROME_BROWSER_PERMISSIONS_PERMISSION_BLACKLIST_CLIENT_H_ |
6 #define CHROME_BROWSER_PERMISSIONS_PERMISSION_BLACKLIST_CLIENT_H_ | 6 #define CHROME_BROWSER_PERMISSIONS_PERMISSION_BLACKLIST_CLIENT_H_ |
7 | 7 |
8 #include "base/callback.h" | 8 #include "base/callback.h" |
9 #include "base/memory/ref_counted.h" | 9 #include "base/memory/ref_counted.h" |
10 #include "chrome/browser/permissions/permission_util.h" | 10 #include "chrome/browser/permissions/permission_util.h" |
11 #include "components/safe_browsing_db/database_manager.h" | 11 #include "components/safe_browsing_db/database_manager.h" |
12 #include "content/public/browser/permission_type.h" | |
13 #include "content/public/browser/web_contents_observer.h" | 12 #include "content/public/browser/web_contents_observer.h" |
14 | 13 |
15 class GURL; | 14 class GURL; |
16 | 15 |
17 namespace content { | 16 namespace content { |
18 class WebContents; | 17 class WebContents; |
19 } | 18 } |
20 | 19 |
21 namespace base { | 20 namespace base { |
22 class OneShotTimer; | 21 class OneShotTimer; |
23 } | 22 } |
24 | 23 |
25 // The client used when checking whether a permission has been blacklisted by | 24 // The client used when checking whether a permission has been blacklisted by |
26 // Safe Browsing. The check is done asynchronously as no state can be stored in | 25 // Safe Browsing. The check is done asynchronously as no state can be stored in |
27 // PermissionContextBase (since additional permission requests may be made). | 26 // PermissionContextBase (since additional permission requests may be made). |
28 // This class must be created and destroyed on the UI thread. | 27 // This class must be created and destroyed on the UI thread. |
29 class PermissionBlacklistClient | 28 class PermissionBlacklistClient |
30 : public safe_browsing::SafeBrowsingDatabaseManager::Client, | 29 : public safe_browsing::SafeBrowsingDatabaseManager::Client, |
31 public base::RefCountedThreadSafe<PermissionBlacklistClient>, | 30 public base::RefCountedThreadSafe<PermissionBlacklistClient>, |
32 public content::WebContentsObserver { | 31 public content::WebContentsObserver { |
33 public: | 32 public: |
34 // |callback| will not be called if |web_contents| is destroyed. Thus if the | 33 // |callback| will not be called if |web_contents| is destroyed. Thus if the |
35 // callback is run, the profile associated with |web_contents| is guaranteed | 34 // callback is run, the profile associated with |web_contents| is guaranteed |
36 // to be alive. | 35 // to be alive. |
37 static void CheckSafeBrowsingBlacklist( | 36 static void CheckSafeBrowsingBlacklist( |
38 scoped_refptr<safe_browsing::SafeBrowsingDatabaseManager> db_manager, | 37 scoped_refptr<safe_browsing::SafeBrowsingDatabaseManager> db_manager, |
39 content::PermissionType permission_type, | 38 ContentSettingsType content_settings_type, |
40 const GURL& request_origin, | 39 const GURL& request_origin, |
41 content::WebContents* web_contents, | 40 content::WebContents* web_contents, |
42 int timeout, | 41 int timeout, |
43 base::Callback<void(bool)> callback); | 42 base::Callback<void(bool)> callback); |
44 | 43 |
45 private: | 44 private: |
46 friend class base::RefCountedThreadSafe<PermissionBlacklistClient>; | 45 friend class base::RefCountedThreadSafe<PermissionBlacklistClient>; |
47 | 46 |
48 PermissionBlacklistClient( | 47 PermissionBlacklistClient( |
49 scoped_refptr<safe_browsing::SafeBrowsingDatabaseManager> db_manager, | 48 scoped_refptr<safe_browsing::SafeBrowsingDatabaseManager> db_manager, |
50 content::PermissionType permission_type, | 49 ContentSettingsType content_settings_type, |
51 const GURL& request_origin, | 50 const GURL& request_origin, |
52 content::WebContents* web_contents, | 51 content::WebContents* web_contents, |
53 int timeout, | 52 int timeout, |
54 base::Callback<void(bool)> callback); | 53 base::Callback<void(bool)> callback); |
55 | 54 |
56 ~PermissionBlacklistClient() override; | 55 ~PermissionBlacklistClient() override; |
57 | 56 |
58 void StartCheck(const GURL& request_origin); | 57 void StartCheck(const GURL& request_origin); |
59 | 58 |
60 // SafeBrowsingDatabaseManager::Client implementation. | 59 // SafeBrowsingDatabaseManager::Client implementation. |
61 void OnCheckApiBlacklistUrlResult( | 60 void OnCheckApiBlacklistUrlResult( |
62 const GURL& url, | 61 const GURL& url, |
63 const safe_browsing::ThreatMetadata& metadata) override; | 62 const safe_browsing::ThreatMetadata& metadata) override; |
64 | 63 |
65 void EvaluateBlacklistResultOnUiThread(bool permission_blocked); | 64 void EvaluateBlacklistResultOnUiThread(bool permission_blocked); |
66 | 65 |
67 // WebContentsObserver implementation. Sets a flag so that when the database | 66 // WebContentsObserver implementation. Sets a flag so that when the database |
68 // manager returns with a result, it won't attempt to run the callback with a | 67 // manager returns with a result, it won't attempt to run the callback with a |
69 // deleted WebContents. | 68 // deleted WebContents. |
70 void WebContentsDestroyed() override; | 69 void WebContentsDestroyed() override; |
71 | 70 |
72 scoped_refptr<safe_browsing::SafeBrowsingDatabaseManager> db_manager_; | 71 scoped_refptr<safe_browsing::SafeBrowsingDatabaseManager> db_manager_; |
73 content::PermissionType permission_type_; | 72 ContentSettingsType content_settings_type_; |
74 | 73 |
75 // PermissionContextBase callback to run on the UI thread. | 74 // PermissionContextBase callback to run on the UI thread. |
76 base::Callback<void(bool)> callback_; | 75 base::Callback<void(bool)> callback_; |
77 | 76 |
78 // Timer to abort the Safe Browsing check if it takes too long. Created and | 77 // Timer to abort the Safe Browsing check if it takes too long. Created and |
79 // used on the IO Thread. | 78 // used on the IO Thread. |
80 std::unique_ptr<base::OneShotTimer> timer_; | 79 std::unique_ptr<base::OneShotTimer> timer_; |
81 int timeout_; | 80 int timeout_; |
82 | 81 |
83 // True if |callback_| should be invoked, if web_contents() is destroyed, this | 82 // True if |callback_| should be invoked, if web_contents() is destroyed, this |
84 // is set to false. | 83 // is set to false. |
85 bool is_active_; | 84 bool is_active_; |
86 | 85 |
87 DISALLOW_COPY_AND_ASSIGN(PermissionBlacklistClient); | 86 DISALLOW_COPY_AND_ASSIGN(PermissionBlacklistClient); |
88 }; | 87 }; |
89 | 88 |
90 #endif // CHROME_BROWSER_PERMISSIONS_PERMISSION_BLACKLIST_CLIENT_H_ | 89 #endif // CHROME_BROWSER_PERMISSIONS_PERMISSION_BLACKLIST_CLIENT_H_ |
OLD | NEW |