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/browser/web_restrictions_client.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 "content/public/browser/browser_thread.h" | |
| 13 #include "jni/WebRestrictionsClient_jni.h" | |
| 14 | |
| 15 namespace web_restrictions { | |
| 16 | |
| 17 namespace { | |
| 18 | |
| 19 const size_t kMaxCacheSize = 100; | |
| 20 | |
| 21 bool RequestPermissionTask( | |
| 22 const GURL& url, | |
| 23 const base::android::JavaRef<jobject>& java_provider) { | |
| 24 JNIEnv* env = base::android::AttachCurrentThread(); | |
| 25 return Java_WebRestrictionsClient_requestPermission( | |
| 26 env, java_provider.obj(), | |
| 27 base::android::ConvertUTF8ToJavaString(env, url.spec()).obj()); | |
| 28 } | |
| 29 | |
| 30 bool CheckSupportsRequestTask( | |
| 31 const base::android::JavaRef<jobject>& java_provider) { | |
| 32 JNIEnv* env = base::android::AttachCurrentThread(); | |
| 33 return Java_WebRestrictionsClient_supportsRequest(env, java_provider.obj()); | |
| 34 } | |
| 35 | |
| 36 } // namespace | |
| 37 | |
| 38 // static | |
| 39 bool WebRestrictionsClient::Register(JNIEnv* env) { | |
| 40 return RegisterNativesImpl(env); | |
| 41 } | |
| 42 | |
| 43 WebRestrictionsClient::WebRestrictionsClient() | |
| 44 : initialized_(false), supports_request_(false) { | |
| 45 single_thread_task_runner_ = base::ThreadTaskRunnerHandle::Get(); | |
| 46 base::SequencedWorkerPool* worker_pool = | |
| 47 content::BrowserThread::GetBlockingPool(); | |
| 48 background_task_runner_ = | |
| 49 worker_pool->GetSequencedTaskRunnerWithShutdownBehavior( | |
| 50 worker_pool->GetSequenceToken(), | |
| 51 base::SequencedWorkerPool::SKIP_ON_SHUTDOWN); | |
| 52 } | |
| 53 | |
| 54 WebRestrictionsClient::~WebRestrictionsClient() { | |
| 55 if (java_provider_.is_null()) | |
| 56 return; | |
| 57 JNIEnv* env = base::android::AttachCurrentThread(); | |
| 58 Java_WebRestrictionsClient_onDestroy(env, java_provider_.obj()); | |
| 59 java_provider_.Reset(); | |
| 60 } | |
| 61 | |
| 62 void WebRestrictionsClient::SetAuthority( | |
| 63 const std::string& content_provider_authority) { | |
| 64 DCHECK(single_thread_task_runner_->BelongsToCurrentThread()); | |
| 65 // Destroy any existing content resolver. | |
| 66 JNIEnv* env = base::android::AttachCurrentThread(); | |
| 67 if (!java_provider_.is_null()) { | |
| 68 Java_WebRestrictionsClient_onDestroy(env, java_provider_.obj()); | |
| 69 java_provider_.Reset(); | |
| 70 } | |
| 71 ClearCache(); | |
| 72 provider_authority_ = content_provider_authority; | |
| 73 | |
| 74 // Initialize the content resolver. | |
| 75 initialized_ = !content_provider_authority.empty(); | |
| 76 if (!initialized_) | |
| 77 return; | |
| 78 java_provider_.Reset(Java_WebRestrictionsClient_create( | |
| 79 env, | |
| 80 base::android::ConvertUTF8ToJavaString(env, content_provider_authority) | |
| 81 .obj(), | |
| 82 reinterpret_cast<jlong>(this))); | |
| 83 supports_request_ = false; | |
| 84 base::PostTaskAndReplyWithResult( | |
| 85 content::BrowserThread::GetBlockingPool(), FROM_HERE, | |
| 86 base::Bind(&CheckSupportsRequestTask, java_provider_), | |
| 87 base::Bind(&WebRestrictionsClient::RequestSupportKnown, | |
| 88 base::Unretained(this), provider_authority_)); | |
| 89 } | |
| 90 | |
| 91 UrlAccess WebRestrictionsClient::ShouldProceed( | |
| 92 bool is_main_frame, | |
| 93 const GURL& url, | |
| 94 const base::Callback<void(bool)>& callback) { | |
| 95 DCHECK(single_thread_task_runner_->BelongsToCurrentThread()); | |
| 96 if (!initialized_) | |
| 97 return ALLOW; | |
| 98 auto iter = url_access_cache_.find(url); | |
| 99 if (iter != url_access_cache_.end()) { | |
| 100 RecordURLAccess(url); | |
| 101 return iter->second ? ALLOW : DISALLOW; | |
| 102 } | |
| 103 base::PostTaskAndReplyWithResult( | |
| 104 background_task_runner_.get(), FROM_HERE, | |
| 105 base::Bind(&WebRestrictionsClient::ShouldProceedTask, url, | |
| 106 java_provider_), | |
| 107 base::Bind(&WebRestrictionsClient::OnShouldProceedComplete, | |
| 108 base::Unretained(this), provider_authority_, url, callback)); | |
| 109 | |
| 110 return PENDING; | |
| 111 } | |
| 112 | |
| 113 bool WebRestrictionsClient::SupportsRequest() const { | |
| 114 return initialized_ && supports_request_; | |
| 115 } | |
| 116 | |
| 117 bool WebRestrictionsClient::GetErrorHtml(const GURL& url, | |
| 118 std::string* error_page) const { | |
| 119 DCHECK(single_thread_task_runner_->BelongsToCurrentThread()); | |
| 120 if (!initialized_) | |
| 121 return false; | |
| 122 auto iter = error_page_cache_.find(url); | |
| 123 if (iter == error_page_cache_.end()) | |
| 124 return false; | |
| 125 *error_page = iter->second; | |
| 126 return true; | |
| 127 } | |
| 128 | |
| 129 void WebRestrictionsClient::RequestPermission( | |
| 130 const GURL& url, | |
| 131 const base::Callback<void(bool)>& request_success) { | |
| 132 if (!initialized_) { | |
| 133 request_success.Run(false); | |
| 134 return; | |
| 135 } | |
| 136 base::PostTaskAndReplyWithResult( | |
| 137 background_task_runner_.get(), FROM_HERE, | |
| 138 base::Bind(&RequestPermissionTask, url, java_provider_), request_success); | |
| 139 } | |
| 140 | |
| 141 void WebRestrictionsClient::OnWebRestrictionsChanged() { | |
| 142 single_thread_task_runner_->PostTask( | |
| 143 FROM_HERE, | |
| 144 base::Bind(&WebRestrictionsClient::ClearCache, base::Unretained(this))); | |
| 145 } | |
| 146 | |
| 147 void WebRestrictionsClient::RecordURLAccess(const GURL& url) { | |
| 148 DCHECK(single_thread_task_runner_->BelongsToCurrentThread()); | |
| 149 // Move the URL to the front of the cache. | |
| 150 recent_urls_.remove(url); | |
| 151 recent_urls_.push_front(url); | |
| 152 } | |
| 153 | |
| 154 void WebRestrictionsClient::UpdateCache(std::string provider_authority, | |
| 155 GURL url, | |
| 156 bool should_proceed, | |
| 157 std::string error_page) { | |
| 158 DCHECK(single_thread_task_runner_->BelongsToCurrentThread()); | |
| 159 // If the webrestrictions provider changed when the old one was being queried, | |
| 160 // do not update the cache for the new provider. | |
| 161 if (provider_authority != provider_authority_) | |
| 162 return; | |
| 163 RecordURLAccess(url); | |
| 164 if (recent_urls_.size() >= kMaxCacheSize) { | |
| 165 url_access_cache_.erase(recent_urls_.back()); | |
| 166 error_page_cache_.erase(recent_urls_.back()); | |
| 167 recent_urls_.pop_back(); | |
| 168 } | |
| 169 url_access_cache_[url] = should_proceed; | |
| 170 if (!error_page.empty()) { | |
| 171 error_page_cache_[url] = error_page; | |
| 172 } else { | |
| 173 error_page_cache_.erase(url); | |
| 174 } | |
| 175 } | |
| 176 | |
| 177 void WebRestrictionsClient::RequestSupportKnown(std::string provider_authority, | |
| 178 bool supports_request) { | |
| 179 // |supports_request_| is initialized to false. | |
| 180 DCHECK(!supports_request_); | |
| 181 DCHECK(single_thread_task_runner_->BelongsToCurrentThread()); | |
| 182 // If the webrestrictions provider changed when the old one was being queried, | |
| 183 // ignore the result. | |
| 184 if (provider_authority != provider_authority_) | |
| 185 return; | |
| 186 supports_request_ = supports_request; | |
| 187 } | |
| 188 | |
| 189 void WebRestrictionsClient::OnShouldProceedComplete( | |
| 190 std::string provider_authority, | |
| 191 const GURL& url, | |
| 192 const base::Callback<void(bool)>& callback, | |
| 193 const ShouldProceedResult& result) { | |
| 194 UpdateCache(provider_authority, url, result.ok_to_proceed, result.error_page); | |
| 195 callback.Run(result.ok_to_proceed); | |
| 196 } | |
| 197 | |
| 198 void WebRestrictionsClient::ClearCache() { | |
| 199 DCHECK(single_thread_task_runner_->BelongsToCurrentThread()); | |
| 200 error_page_cache_.clear(); | |
| 201 url_access_cache_.clear(); | |
| 202 recent_urls_.clear(); | |
| 203 } | |
| 204 | |
| 205 // static | |
| 206 WebRestrictionsClient::ShouldProceedResult | |
| 207 WebRestrictionsClient::ShouldProceedTask( | |
|
Bernhard Bauer
2016/02/19 13:46:26
Does this need to be a private static method, or c
aberent
2016/02/19 19:40:08
Sadly it can't unless I make ShouldProceedResult p
| |
| 208 const GURL& url, | |
| 209 const base::android::JavaRef<jobject>& java_provider) { | |
| 210 WebRestrictionsClient::ShouldProceedResult result; | |
| 211 JNIEnv* env = base::android::AttachCurrentThread(); | |
| 212 base::android::ScopedJavaLocalRef<jobject> j_result = | |
| 213 Java_WebRestrictionsClient_shouldProceed( | |
| 214 env, java_provider.obj(), | |
| 215 base::android::ConvertUTF8ToJavaString(env, url.spec()).obj()); | |
| 216 result.ok_to_proceed = | |
| 217 Java_ShouldProceedResult_shouldProceed(env, j_result.obj()); | |
| 218 base::android::ScopedJavaLocalRef<jstring> j_error_page = | |
| 219 Java_ShouldProceedResult_getErrorPage(env, j_result.obj()); | |
| 220 if (j_error_page.obj() == nullptr) { | |
|
Bernhard Bauer
2016/02/19 13:46:26
You can use .is_null() for this.
aberent
2016/02/19 19:40:07
Done.
| |
| 221 result.error_page = nullptr; | |
|
Bernhard Bauer
2016/02/19 13:46:26
And this isn't really necessary, as the string wil
aberent
2016/02/19 19:40:08
Done.
| |
| 222 } else { | |
| 223 result.error_page = | |
|
Bernhard Bauer
2016/02/19 13:46:26
And finally, here you could just pass a pointer to
aberent
2016/02/19 19:40:08
This has to be passed to the OnShouldProceedComple
Bernhard Bauer
2016/02/22 12:39:16
Sorry, I meant using the outparam form of ConvertJ
aberent
2016/02/23 13:52:37
Done.
Bernhard Bauer
2016/02/23 14:29:13
Not really? 😃
aberent
2016/02/23 16:26:51
Done. Really, this time.
| |
| 224 base::android::ConvertJavaStringToUTF8(env, j_error_page.obj()); | |
| 225 } | |
| 226 return result; | |
| 227 } | |
| 228 | |
| 229 void NotifyWebRestrictionsChanged( | |
| 230 JNIEnv* env, | |
| 231 const base::android::JavaParamRef<jobject>& clazz, | |
| 232 jlong provider_ptr) { | |
| 233 WebRestrictionsClient* provider = | |
| 234 reinterpret_cast<WebRestrictionsClient*>(provider_ptr); | |
| 235 // TODO(knn): Also reload existing interstitials/error pages. | |
| 236 provider->OnWebRestrictionsChanged(); | |
| 237 } | |
| 238 | |
| 239 } // namespace web_restrictions | |
| OLD | NEW |