Chromium Code Reviews| 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 #include "components/web_restrictions/content_resolver_web_restrictions_provider .h" | |
| 6 | |
| 7 #include "base/android/jni_string.h" | |
| 8 #include "base/android/scoped_java_ref.h" | |
| 9 #include "base/bind.h" | |
| 10 #include "base/location.h" | |
| 11 #include "base/thread_task_runner_handle.h" | |
| 12 #include "jni/ContentResolverWebRestrictionProvider_jni.h" | |
| 13 | |
| 14 namespace { | |
| 15 const size_t kMaxCacheSize = 100; | |
| 16 } // namespace | |
| 17 | |
| 18 namespace web_restrictions { | |
| 19 namespace android { | |
| 20 | |
| 21 SelfDeletingCallback::SelfDeletingCallback( | |
| 22 const GURL& url, | |
| 23 const base::Callback<void(bool)>& callback, | |
| 24 const scoped_refptr<base::TaskRunner>& callback_runner, | |
| 25 ContentResolverWebRestrictionProvider* provider) | |
| 26 : url_(url), | |
| 27 callback_(callback), | |
| 28 callback_runner_(callback_runner), | |
| 29 provider_(provider) {} | |
| 30 | |
| 31 SelfDeletingCallback::~SelfDeletingCallback() {} | |
| 32 | |
| 33 void SelfDeletingCallback::ShouldProceed( | |
| 34 JNIEnv* env, | |
| 35 const base::android::JavaParamRef<jobject>& obj, | |
| 36 jboolean should_proceed, | |
| 37 const base::android::JavaParamRef<jstring>& j_error_page) { | |
| 38 auto& url_access_cache = provider_->url_access_cache_; | |
| 39 auto& error_page_cache = provider_->error_page_cache_; | |
| 40 auto& recent_urls = provider_->recent_urls_; | |
| 41 url_access_cache.erase(url_); | |
| 42 error_page_cache.erase(url_); | |
| 43 recent_urls.remove(url_); | |
| 44 if (recent_urls.size() >= kMaxCacheSize) { | |
| 45 url_access_cache.erase(recent_urls.back()); | |
| 46 error_page_cache.erase(recent_urls.back()); | |
| 47 recent_urls.pop_back(); | |
| 48 } | |
| 49 recent_urls.push_front(url_); | |
| 50 url_access_cache[url_] = should_proceed; | |
|
aberent
2016/01/27 14:16:42
What happens when the parent grants permission? Do
knn
2016/01/28 11:43:25
Fixed this.
| |
| 51 if (!j_error_page.is_null()) { | |
| 52 error_page_cache[url_] = | |
| 53 base::android::ConvertJavaStringToUTF8(j_error_page); | |
| 54 } | |
| 55 callback_runner_->PostTask(FROM_HERE, base::Bind(callback_, should_proceed)); | |
| 56 delete this; | |
| 57 } | |
| 58 | |
| 59 void SelfDeletingCallback::RequestSuccess( | |
| 60 JNIEnv* env, | |
| 61 const base::android::JavaParamRef<jobject>& obj, | |
| 62 jboolean request_success) { | |
| 63 callback_runner_->PostTask(FROM_HERE, base::Bind(callback_, request_success)); | |
| 64 delete this; | |
| 65 } | |
| 66 | |
| 67 } // namespace android | |
| 68 | |
| 69 // static | |
| 70 bool ContentResolverWebRestrictionProvider::Register(JNIEnv* env) { | |
| 71 return android::RegisterNativesImpl(env); | |
| 72 } | |
| 73 | |
| 74 ContentResolverWebRestrictionProvider::ContentResolverWebRestrictionProvider() | |
| 75 : initialized_(false) {} | |
| 76 | |
| 77 ContentResolverWebRestrictionProvider:: | |
| 78 ~ContentResolverWebRestrictionProvider() {} | |
| 79 | |
| 80 void ContentResolverWebRestrictionProvider::Initialize( | |
| 81 const std::string& content_provider_authority) { | |
| 82 DCHECK(!content_provider_authority.empty()); | |
| 83 JNIEnv* env = base::android::AttachCurrentThread(); | |
| 84 java_provider_.Reset( | |
| 85 android::Java_ContentResolverWebRestrictionProvider_create( | |
| 86 env, base::android::ConvertUTF8ToJavaString( | |
| 87 env, content_provider_authority) | |
| 88 .obj())); | |
| 89 supports_request_ = | |
| 90 android::Java_ContentResolverWebRestrictionProvider_supportsRequest( | |
| 91 env, java_provider_.obj()); | |
| 92 error_page_cache_.clear(); | |
| 93 url_access_cache_.clear(); | |
| 94 recent_urls_.clear(); | |
| 95 initialized_ = true; | |
| 96 } | |
| 97 | |
| 98 UrlAccess ContentResolverWebRestrictionProvider::ShouldProceed( | |
| 99 bool is_main_frame, | |
| 100 const GURL& url, | |
| 101 const base::Callback<void(bool)>& callback) { | |
| 102 if (!initialized_) | |
| 103 return ALLOW; | |
| 104 auto iter = url_access_cache_.find(url); | |
| 105 if (iter != url_access_cache_.end()) { | |
| 106 // Move the url to the front of the cache. | |
| 107 recent_urls_.remove(url); | |
| 108 recent_urls_.push_front(url); | |
| 109 return iter->second ? ALLOW : DISALLOW; | |
| 110 } | |
| 111 scoped_refptr<base::SingleThreadTaskRunner> callback_runner = | |
| 112 base::ThreadTaskRunnerHandle::Get(); | |
| 113 android::SelfDeletingCallback* wrapped_callback = | |
| 114 new android::SelfDeletingCallback(url, callback, callback_runner, this); | |
| 115 JNIEnv* env = base::android::AttachCurrentThread(); | |
| 116 android::Java_ContentResolverWebRestrictionProvider_shouldProceed( | |
| 117 env, java_provider_.obj(), reinterpret_cast<jlong>(wrapped_callback), | |
| 118 base::android::ConvertUTF8ToJavaString(env, url.spec()).obj()); | |
| 119 return PENDING; | |
| 120 } | |
| 121 | |
| 122 bool ContentResolverWebRestrictionProvider::SupportsRequest() const { | |
| 123 return initialized_ && supports_request_; | |
| 124 } | |
| 125 | |
| 126 bool ContentResolverWebRestrictionProvider::GetErrorHtml( | |
| 127 const GURL& url, | |
| 128 std::string* error_page) const { | |
| 129 if (!initialized_) | |
| 130 return false; | |
| 131 auto iter = error_page_cache_.find(url); | |
| 132 if (iter == error_page_cache_.end()) | |
| 133 return false; | |
| 134 *error_page = iter->second; | |
| 135 return true; | |
| 136 } | |
| 137 | |
| 138 void ContentResolverWebRestrictionProvider::RequestPermission( | |
| 139 const GURL& url, | |
| 140 const base::Callback<void(bool)>& request_success) { | |
| 141 if (!initialized_) { | |
| 142 request_success.Run(false); | |
| 143 return; | |
| 144 } | |
| 145 scoped_refptr<base::SingleThreadTaskRunner> callback_runner = | |
| 146 base::ThreadTaskRunnerHandle::Get(); | |
| 147 android::SelfDeletingCallback* wrapped_callback = | |
| 148 new android::SelfDeletingCallback(url, request_success, callback_runner, | |
| 149 this); | |
| 150 JNIEnv* env = base::android::AttachCurrentThread(); | |
| 151 android::Java_ContentResolverWebRestrictionProvider_requestPermission( | |
| 152 env, java_provider_.obj(), reinterpret_cast<jlong>(wrapped_callback), | |
| 153 base::android::ConvertUTF8ToJavaString(env, url.spec()).obj()); | |
| 154 } | |
| 155 | |
| 156 } // namespace web_restrictions | |
| OLD | NEW |