OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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 COMPONENTS_WEB_RESTRICTION_WEB_RESTRICTIONS_CLIENT_H_ | 5 #ifndef COMPONENTS_WEB_RESTRICTION_WEB_RESTRICTIONS_CLIENT_H_ |
6 #define COMPONENTS_WEB_RESTRICTION_WEB_RESTRICTIONS_CLIENT_H_ | 6 #define COMPONENTS_WEB_RESTRICTION_WEB_RESTRICTIONS_CLIENT_H_ |
7 | 7 |
8 #include <jni.h> | 8 #include <jni.h> |
| 9 |
9 #include <list> | 10 #include <list> |
10 #include <map> | |
11 #include <string> | 11 #include <string> |
| 12 #include <unordered_map> |
12 | 13 |
13 #include "base/android/jni_android.h" | 14 #include "base/android/jni_android.h" |
14 #include "base/android/scoped_java_ref.h" | 15 #include "base/android/scoped_java_ref.h" |
15 #include "base/callback.h" | 16 #include "base/callback.h" |
16 #include "base/macros.h" | 17 #include "base/macros.h" |
17 #include "base/memory/ref_counted.h" | 18 #include "base/memory/ref_counted.h" |
18 #include "base/sequenced_task_runner.h" | 19 #include "base/sequenced_task_runner.h" |
19 #include "base/single_thread_task_runner.h" | 20 #include "base/single_thread_task_runner.h" |
20 #include "url/gurl.h" | 21 #include "base/synchronization/lock.h" |
| 22 #include "components/web_restrictions/browser/web_restictions_client_result.h" |
| 23 |
| 24 namespace base { |
| 25 class Lock; |
| 26 } |
21 | 27 |
22 namespace web_restrictions { | 28 namespace web_restrictions { |
23 | 29 |
24 enum UrlAccess { ALLOW, DISALLOW, PENDING }; | 30 enum UrlAccess { ALLOW, DISALLOW, PENDING }; |
25 | 31 |
| 32 class WebRestrictionsCache { |
| 33 public: |
| 34 std::unique_ptr<const WebRestrictionsClientResult> GetCacheEntry( |
| 35 const std::string& url); |
| 36 void SetCacheEntry(const std::string& url, |
| 37 const WebRestrictionsClientResult& entry); |
| 38 void RemoveCacheEntry(const std::string& url); |
| 39 void Clear(); |
| 40 |
| 41 private: |
| 42 base::Lock lock_; |
| 43 std::unordered_map<std::string, WebRestrictionsClientResult> cache_data_; |
| 44 }; |
| 45 |
26 class WebRestrictionsClient { | 46 class WebRestrictionsClient { |
27 public: | 47 public: |
28 // An instance of this class is expected to live through the lifetime of a | 48 // An instance of this class is expected to live through the lifetime of a |
29 // browser and uses raw pointers in callbacks. | 49 // browser and uses raw pointers in callbacks. |
30 // Any changes to the class, enable/disable/change should be done through the | 50 // Any changes to the class, enable/disable/change should be done through the |
31 // SetAuthority(...) method. | 51 // SetAuthority(...) method. |
32 WebRestrictionsClient(); | 52 WebRestrictionsClient(); |
33 ~WebRestrictionsClient(); | 53 ~WebRestrictionsClient(); |
34 | 54 |
35 // Register JNI methods. | 55 // Register JNI methods. |
36 static bool Register(JNIEnv* env); | 56 static bool Register(JNIEnv* env); |
37 | 57 |
38 // Verify the content provider and query it for basic information like does it | 58 // Verify the content provider and query it for basic information like does it |
39 // support handling requests. This should be called everytime the provider | 59 // support handling requests. This should be called everytime the provider |
40 // changes. An empty authority can be used to disable this class. | 60 // changes. An empty authority can be used to disable this class. |
41 void SetAuthority(const std::string& content_provider_authority); | 61 void SetAuthority(const std::string& content_provider_authority); |
42 | 62 |
43 // WebRestrictionsProvider: | 63 // WebRestrictionsProvider: |
44 UrlAccess ShouldProceed(bool is_main_frame, | 64 UrlAccess ShouldProceed(bool is_main_frame, |
45 const GURL& url, | 65 const std::string& url, |
46 const base::Callback<void(bool)>& callback); | 66 const base::Callback<void(bool)>& callback); |
47 | 67 |
48 bool SupportsRequest() const; | 68 bool SupportsRequest() const; |
49 | 69 |
50 int GetResultColumnCount(const GURL& url) const; | 70 void RequestPermission(const std::string& url, |
51 | |
52 std::string GetResultColumnName(const GURL& url, int column) const; | |
53 | |
54 int GetResultIntValue(const GURL& url, int column) const; | |
55 | |
56 std::string GetResultStringValue(const GURL& url, int column) const; | |
57 | |
58 void RequestPermission(const GURL& url, | |
59 const base::Callback<void(bool)>& callback); | 71 const base::Callback<void(bool)>& callback); |
60 | 72 |
61 void OnWebRestrictionsChanged(); | 73 void OnWebRestrictionsChanged(); |
62 | 74 |
| 75 // Get a cached WebRestrictionsResult synchronously, for use when building |
| 76 // error pages etc.. May be called on any thread, and will return a fresh copy |
| 77 // of the result (hence thread safe). |
| 78 std::unique_ptr<const WebRestrictionsClientResult> |
| 79 GetCachedWebRestrictionsResult(const std::string& url); |
| 80 |
63 private: | 81 private: |
| 82 void SetAuthorityTask(const std::string& content_provider_authority); |
64 | 83 |
65 void RecordURLAccess(const GURL& url); | 84 void RecordURLAccess(const std::string& url); |
66 | 85 |
67 void UpdateCache(std::string provider_authority, | 86 void UpdateCache(std::string provider_authority, |
68 GURL url, | 87 std::string url, |
69 base::android::ScopedJavaGlobalRef<jobject> result); | 88 base::android::ScopedJavaGlobalRef<jobject> result); |
70 | 89 |
71 void RequestSupportKnown(std::string provider_authority, | 90 void RequestSupportKnown(std::string provider_authority, |
72 bool supports_request); | 91 bool supports_request); |
73 | 92 |
74 void ClearCache(); | 93 void ClearCache(); |
75 | 94 |
76 static base::android::ScopedJavaGlobalRef<jobject> ShouldProceedTask( | 95 static base::android::ScopedJavaGlobalRef<jobject> ShouldProceedTask( |
77 const GURL& url, | 96 const std::string& url, |
78 const base::android::JavaRef<jobject>& java_provider); | 97 const base::android::JavaRef<jobject>& java_provider); |
79 | 98 |
80 void OnShouldProceedComplete( | 99 void OnShouldProceedComplete( |
81 std::string provider_authority, | 100 std::string provider_authority, |
82 const GURL& url, | 101 const std::string& url, |
83 const base::Callback<void(bool)>& callback, | 102 const base::Callback<void(bool)>& callback, |
84 const base::android::ScopedJavaGlobalRef<jobject>& result); | 103 const base::android::ScopedJavaGlobalRef<jobject>& result); |
85 | 104 |
86 // Set up after SetAuthority(). | 105 // Set up after SetAuthority(). |
87 bool initialized_; | 106 bool initialized_; |
88 bool supports_request_; | 107 bool supports_request_; |
89 base::android::ScopedJavaGlobalRef<jobject> java_provider_; | 108 base::android::ScopedJavaGlobalRef<jobject> java_provider_; |
90 std::string provider_authority_; | 109 std::string provider_authority_; |
| 110 WebRestrictionsCache cache_; |
91 | 111 |
92 scoped_refptr<base::SequencedTaskRunner> background_task_runner_; | 112 scoped_refptr<base::SequencedTaskRunner> background_task_runner_; |
93 scoped_refptr<base::SingleThreadTaskRunner> single_thread_task_runner_; | 113 scoped_refptr<base::SingleThreadTaskRunner> io_thread_task_runner_; |
94 | 114 std::list<std::string> recent_urls_; |
95 std::map<GURL, base::android::ScopedJavaGlobalRef<jobject>> cache_; | |
96 std::list<GURL> recent_urls_; | |
97 | 115 |
98 DISALLOW_COPY_AND_ASSIGN(WebRestrictionsClient); | 116 DISALLOW_COPY_AND_ASSIGN(WebRestrictionsClient); |
99 }; | 117 }; |
100 | 118 |
101 } // namespace web_restrictions | 119 } // namespace web_restrictions |
102 | 120 |
103 #endif // COMPONENTS_WEB_RESTRICTION_WEB_RESTRICTIONS_CLIENT_H_ | 121 #endif // COMPONENTS_WEB_RESTRICTION_WEB_RESTRICTIONS_CLIENT_H_ |
OLD | NEW |