OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #ifndef COMPONENTS_WEB_RESTRICTION_CONTENT_RESOLVER_WEB_RESTRICTION_PROVIDER_H_ |
| 6 #define COMPONENTS_WEB_RESTRICTION_CONTENT_RESOLVER_WEB_RESTRICTION_PROVIDER_H_ |
| 7 |
| 8 #include <jni.h> |
| 9 #include <list> |
| 10 #include <map> |
| 11 |
| 12 #include "base/android/jni_android.h" |
| 13 #include "base/callback.h" |
| 14 #include "base/macros.h" |
| 15 #include "base/memory/ref_counted.h" |
| 16 #include "base/task_runner.h" |
| 17 #include "components/web_restriction/web_restriction_provider.h" |
| 18 |
| 19 namespace web_restriction { |
| 20 |
| 21 class ContentResolverWebRestrictionProvider; |
| 22 |
| 23 namespace android { |
| 24 |
| 25 // A wrapper to the callback class to facilitate getting a callback from java |
| 26 // into C++. Objects of this class delete itself only when they are called back |
| 27 // so we must ensure that happens even for error cases. |
| 28 class SelfDeletingCallback { |
| 29 public: |
| 30 SelfDeletingCallback(const GURL& url, |
| 31 const base::Callback<void(bool)>& callback, |
| 32 const scoped_refptr<base::TaskRunner>& callback_runner, |
| 33 ContentResolverWebRestrictionProvider* provider); |
| 34 |
| 35 void RequestSuccess(JNIEnv* env, |
| 36 const base::android::JavaParamRef<jobject>& obj, |
| 37 jboolean request_success); |
| 38 void ShouldProceed(JNIEnv* env, |
| 39 const base::android::JavaParamRef<jobject>& obj, |
| 40 jboolean should_proceed, |
| 41 const base::android::JavaParamRef<jstring>& error_page); |
| 42 |
| 43 private: |
| 44 GURL url_; |
| 45 base::Callback<void(bool)> callback_; |
| 46 scoped_refptr<base::TaskRunner> callback_runner_; |
| 47 ContentResolverWebRestrictionProvider* provider_; |
| 48 |
| 49 // Only the callback can delete itself. We must ensure it is indeed |
| 50 // called back. |
| 51 ~SelfDeletingCallback(); |
| 52 DISALLOW_COPY_AND_ASSIGN(SelfDeletingCallback); |
| 53 }; |
| 54 |
| 55 } // namespace android |
| 56 |
| 57 class ContentResolverWebRestrictionProvider : public WebRestrictionProvider { |
| 58 public: |
| 59 ContentResolverWebRestrictionProvider(); |
| 60 ~ContentResolverWebRestrictionProvider() override; |
| 61 |
| 62 // Register JNI methods. |
| 63 static bool Register(JNIEnv* env); |
| 64 |
| 65 // Verify the content provider and Setup. |
| 66 void Initialize(const std::string& content_provider_authority); |
| 67 |
| 68 // WebRestrictionProvider: |
| 69 UrlAccess ShouldProceed(bool is_main_frame, |
| 70 const GURL& url, |
| 71 const base::Callback<void(bool)>& callback) override; |
| 72 |
| 73 bool SupportsRequest() const override; |
| 74 |
| 75 bool GetErrorHtml(const GURL& url, std::string* error_html) const override; |
| 76 |
| 77 void RequestPermission(const GURL& url, |
| 78 const base::Callback<void(bool)>& callback) override; |
| 79 |
| 80 private: |
| 81 friend class android::SelfDeletingCallback; // For updating the cache. |
| 82 |
| 83 bool initialized_; |
| 84 // Setup during Initialize. |
| 85 bool supports_request_; |
| 86 base::android::ScopedJavaGlobalRef<jobject> java_provider_; |
| 87 |
| 88 std::map<GURL, std::string> error_page_cache_; |
| 89 std::map<GURL, bool> url_access_cache_; |
| 90 std::list<GURL> recent_urls_; |
| 91 |
| 92 DISALLOW_COPY_AND_ASSIGN(ContentResolverWebRestrictionProvider); |
| 93 }; |
| 94 |
| 95 } // namespace web_restriction |
| 96 |
| 97 #endif // COMPONENTS_WEB_RESTRICTION_CONTENT_RESOLVER_WEB_RESTRICTION_PROVIDER_
H_ |
OLD | NEW |