| 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() = default; |
| 74 std::unique_ptr<WebRestrictionsClientResult> GetCacheEntry( |
| 75 const std::string& url); |
| 76 void SetCacheEntry(const std::string& url, |
| 77 const WebRestrictionsClientResult& entry); |
| 78 void RemoveCacheEntry(const std::string& url); |
| 79 void Clear(); |
| 66 | 80 |
| 67 void UpdateCache(std::string provider_authority, | 81 private: |
| 68 GURL url, | 82 base::Lock lock_; |
| 83 std::unordered_map<std::string, WebRestrictionsClientResult> cache_data_; |
| 84 DISALLOW_COPY_AND_ASSIGN(Cache); |
| 85 }; |
| 86 |
| 87 void SetAuthorityTask(const std::string& content_provider_authority); |
| 88 |
| 89 void RecordURLAccess(const std::string& url); |
| 90 |
| 91 void UpdateCache(const std::string& provider_authority, |
| 92 const std::string& url, |
| 69 base::android::ScopedJavaGlobalRef<jobject> result); | 93 base::android::ScopedJavaGlobalRef<jobject> result); |
| 70 | 94 |
| 71 void RequestSupportKnown(std::string provider_authority, | 95 void RequestSupportKnown(const std::string& provider_authority, |
| 72 bool supports_request); | 96 bool supports_request); |
| 73 | 97 |
| 74 void ClearCache(); | 98 void ClearCache(); |
| 75 | 99 |
| 76 static base::android::ScopedJavaGlobalRef<jobject> ShouldProceedTask( | 100 static base::android::ScopedJavaGlobalRef<jobject> ShouldProceedTask( |
| 77 const GURL& url, | 101 const std::string& url, |
| 78 const base::android::JavaRef<jobject>& java_provider); | 102 const base::android::JavaRef<jobject>& java_provider); |
| 79 | 103 |
| 80 void OnShouldProceedComplete( | 104 void OnShouldProceedComplete( |
| 81 std::string provider_authority, | 105 std::string provider_authority, |
| 82 const GURL& url, | 106 const std::string& url, |
| 83 const base::Callback<void(bool)>& callback, | 107 const base::Callback<void(bool)>& callback, |
| 84 const base::android::ScopedJavaGlobalRef<jobject>& result); | 108 const base::android::ScopedJavaGlobalRef<jobject>& result); |
| 85 | 109 |
| 86 // Set up after SetAuthority(). | 110 // Set up after SetAuthority(). |
| 87 bool initialized_; | 111 bool initialized_; |
| 88 bool supports_request_; | 112 bool supports_request_; |
| 89 base::android::ScopedJavaGlobalRef<jobject> java_provider_; | 113 base::android::ScopedJavaGlobalRef<jobject> java_provider_; |
| 90 std::string provider_authority_; | 114 std::string provider_authority_; |
| 115 Cache cache_; |
| 91 | 116 |
| 92 scoped_refptr<base::SequencedTaskRunner> background_task_runner_; | 117 scoped_refptr<base::SequencedTaskRunner> background_task_runner_; |
| 93 scoped_refptr<base::SingleThreadTaskRunner> single_thread_task_runner_; | 118 std::list<std::string> recent_urls_; |
| 94 | |
| 95 std::map<GURL, base::android::ScopedJavaGlobalRef<jobject>> cache_; | |
| 96 std::list<GURL> recent_urls_; | |
| 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 |