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 class Cache { | |
67 public: | |
68 Cache() = default; | |
69 std::unique_ptr<WebRestrictionsClientResult> GetCacheEntry( | |
70 const std::string& url); | |
71 void SetCacheEntry(const std::string& url, | |
72 const WebRestrictionsClientResult& entry); | |
73 void RemoveCacheEntry(const std::string& url); | |
74 void Clear(); | |
64 | 75 |
65 void RecordURLAccess(const GURL& url); | 76 private: |
77 base::Lock lock_; | |
78 std::unordered_map<std::string, WebRestrictionsClientResult> cache_data_; | |
79 DISALLOW_COPY_AND_ASSIGN(Cache); | |
80 }; | |
66 | 81 |
67 void UpdateCache(std::string provider_authority, | 82 void SetAuthorityTask(const std::string& content_provider_authority); |
68 GURL url, | 83 |
84 void RecordURLAccess(const std::string& url); | |
85 | |
86 void UpdateCache(const std::string& provider_authority, | |
87 const 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(const 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 Cache 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 std::list<std::string> recent_urls_; |
94 | 114 |
95 std::map<GURL, base::android::ScopedJavaGlobalRef<jobject>> cache_; | 115 // Make the test classes friends, so that they can set the authority |
96 std::list<GURL> recent_urls_; | 116 // synchronously |
Bernhard Bauer
2016/05/19 10:00:15
Friend declarations come first in the private sect
aberent
2016/05/19 15:44:03
Done.
| |
117 friend class WebRestrictionsResourceThrottleTest; | |
118 friend class WebRestrictionsClientTest; | |
97 | 119 |
98 DISALLOW_COPY_AND_ASSIGN(WebRestrictionsClient); | 120 DISALLOW_COPY_AND_ASSIGN(WebRestrictionsClient); |
99 }; | 121 }; |
100 | 122 |
101 } // namespace web_restrictions | 123 } // namespace web_restrictions |
102 | 124 |
103 #endif // COMPONENTS_WEB_RESTRICTION_WEB_RESTRICTIONS_CLIENT_H_ | 125 #endif // COMPONENTS_WEB_RESTRICTION_WEB_RESTRICTIONS_CLIENT_H_ |
OLD | NEW |