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