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