1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249
|
// Copyright 2015 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "components/web_restrictions/browser/web_restrictions_client.h"
#include "base/android/jni_string.h"
#include "base/bind.h"
#include "base/location.h"
#include "base/logging.h"
#include "base/threading/thread_task_runner_handle.h"
#include "content/public/browser/browser_thread.h"
#include "jni/WebRestrictionsClient_jni.h"
using base::android::ScopedJavaGlobalRef;
namespace web_restrictions {
namespace {
const size_t kMaxCacheSize = 100;
bool RequestPermissionTask(
const std::string& url,
const base::android::JavaRef<jobject>& java_provider) {
JNIEnv* env = base::android::AttachCurrentThread();
return Java_WebRestrictionsClient_requestPermission(
env, java_provider,
base::android::ConvertUTF8ToJavaString(env, url));
}
bool CheckSupportsRequestTask(
const base::android::JavaRef<jobject>& java_provider) {
JNIEnv* env = base::android::AttachCurrentThread();
return Java_WebRestrictionsClient_supportsRequest(env, java_provider);
}
} // namespace
// static
bool WebRestrictionsClient::Register(JNIEnv* env) {
return RegisterNativesImpl(env);
}
WebRestrictionsClient::WebRestrictionsClient()
: initialized_(false), supports_request_(false) {
base::SequencedWorkerPool* worker_pool =
content::BrowserThread::GetBlockingPool();
background_task_runner_ =
worker_pool->GetSequencedTaskRunnerWithShutdownBehavior(
worker_pool->GetSequenceToken(),
base::SequencedWorkerPool::SKIP_ON_SHUTDOWN);
}
WebRestrictionsClient::~WebRestrictionsClient() {
if (java_provider_.is_null())
return;
JNIEnv* env = base::android::AttachCurrentThread();
Java_WebRestrictionsClient_onDestroy(env, java_provider_);
java_provider_.Reset();
}
void WebRestrictionsClient::SetAuthority(
const std::string& content_provider_authority) {
// This is called from the UI thread, but class members should only be
// accessed from the IO thread.
content::BrowserThread::PostTask(
content::BrowserThread::IO, FROM_HERE,
base::Bind(&WebRestrictionsClient::SetAuthorityTask,
base::Unretained(this), content_provider_authority));
}
void WebRestrictionsClient::SetAuthorityTask(
const std::string& content_provider_authority) {
DCHECK_CURRENTLY_ON(content::BrowserThread::IO);
// Destroy any existing content resolver.
JNIEnv* env = base::android::AttachCurrentThread();
if (!java_provider_.is_null()) {
Java_WebRestrictionsClient_onDestroy(env, java_provider_);
java_provider_.Reset();
}
ClearCache();
provider_authority_ = content_provider_authority;
// Initialize the content resolver.
initialized_ = !content_provider_authority.empty();
if (!initialized_)
return;
java_provider_.Reset(Java_WebRestrictionsClient_create(
env,
base::android::ConvertUTF8ToJavaString(env, content_provider_authority),
reinterpret_cast<jlong>(this)));
supports_request_ = false;
base::PostTaskAndReplyWithResult(
content::BrowserThread::GetBlockingPool(), FROM_HERE,
base::Bind(&CheckSupportsRequestTask, java_provider_),
base::Bind(&WebRestrictionsClient::RequestSupportKnown,
base::Unretained(this), provider_authority_));
}
UrlAccess WebRestrictionsClient::ShouldProceed(
bool is_main_frame,
const std::string& url,
const base::Callback<void(bool)>& callback) {
DCHECK_CURRENTLY_ON(content::BrowserThread::IO);
if (!initialized_)
return ALLOW;
std::unique_ptr<const WebRestrictionsClientResult> result =
cache_.GetCacheEntry(url);
if (result) {
RecordURLAccess(url);
return result->ShouldProceed() ? ALLOW : DISALLOW;
}
base::PostTaskAndReplyWithResult(
background_task_runner_.get(), FROM_HERE,
base::Bind(&WebRestrictionsClient::ShouldProceedTask, url,
java_provider_),
base::Bind(&WebRestrictionsClient::OnShouldProceedComplete,
base::Unretained(this), provider_authority_, url, callback));
return PENDING;
}
bool WebRestrictionsClient::SupportsRequest() const {
return initialized_ && supports_request_;
}
void WebRestrictionsClient::RequestPermission(
const std::string& url,
const base::Callback<void(bool)>& request_success) {
if (!initialized_) {
request_success.Run(false);
return;
}
base::PostTaskAndReplyWithResult(
background_task_runner_.get(), FROM_HERE,
base::Bind(&RequestPermissionTask, url, java_provider_), request_success);
}
void WebRestrictionsClient::OnWebRestrictionsChanged(
JNIEnv* env,
const base::android::JavaParamRef<jobject>& obj) {
content::BrowserThread::PostTask(
content::BrowserThread::IO, FROM_HERE,
base::Bind(&WebRestrictionsClient::ClearCache, base::Unretained(this)));
}
void WebRestrictionsClient::RecordURLAccess(const std::string& url) {
DCHECK_CURRENTLY_ON(content::BrowserThread::IO);
// Move the URL to the front of the cache.
recent_urls_.remove(url);
recent_urls_.push_front(url);
}
void WebRestrictionsClient::UpdateCache(const std::string& provider_authority,
const std::string& url,
ScopedJavaGlobalRef<jobject> result) {
DCHECK_CURRENTLY_ON(content::BrowserThread::IO);
// If the webrestrictions provider changed when the old one was being queried,
// do not update the cache for the new provider.
if (provider_authority != provider_authority_)
return;
RecordURLAccess(url);
if (recent_urls_.size() >= kMaxCacheSize) {
cache_.RemoveCacheEntry(recent_urls_.back());
recent_urls_.pop_back();
}
cache_.SetCacheEntry(url, WebRestrictionsClientResult(result));
}
void WebRestrictionsClient::RequestSupportKnown(
const std::string& provider_authority,
bool supports_request) {
// |supports_request_| is initialized to false.
DCHECK(!supports_request_);
DCHECK_CURRENTLY_ON(content::BrowserThread::IO);
// If the webrestrictions provider changed when the old one was being queried,
// ignore the result.
if (provider_authority != provider_authority_)
return;
supports_request_ = supports_request;
}
void WebRestrictionsClient::OnShouldProceedComplete(
std::string provider_authority,
const std::string& url,
const base::Callback<void(bool)>& callback,
const ScopedJavaGlobalRef<jobject>& result) {
UpdateCache(provider_authority, url, result);
callback.Run(cache_.GetCacheEntry(url)->ShouldProceed());
}
void WebRestrictionsClient::ClearCache() {
DCHECK_CURRENTLY_ON(content::BrowserThread::IO);
cache_.Clear();
recent_urls_.clear();
}
std::unique_ptr<WebRestrictionsClientResult>
WebRestrictionsClient::GetCachedWebRestrictionsResult(const std::string& url) {
return cache_.GetCacheEntry(url);
}
// static
ScopedJavaGlobalRef<jobject> WebRestrictionsClient::ShouldProceedTask(
const std::string& url,
const base::android::JavaRef<jobject>& java_provider) {
JNIEnv* env = base::android::AttachCurrentThread();
base::android::ScopedJavaGlobalRef<jobject> result(
Java_WebRestrictionsClient_shouldProceed(
env, java_provider,
base::android::ConvertUTF8ToJavaString(env, url)));
return result;
}
WebRestrictionsClient::Cache::Cache() = default;
WebRestrictionsClient::Cache::~Cache() = default;
std::unique_ptr<WebRestrictionsClientResult>
WebRestrictionsClient::Cache::GetCacheEntry(const std::string& url) {
base::AutoLock lock(lock_);
auto iter = cache_data_.find(url);
if (iter == cache_data_.end())
return nullptr;
// This has to be thread-safe, so copy the data.
return std::unique_ptr<WebRestrictionsClientResult>(
new WebRestrictionsClientResult(iter->second));
}
void WebRestrictionsClient::Cache::SetCacheEntry(
const std::string& url,
const WebRestrictionsClientResult& entry) {
base::AutoLock lock(lock_);
cache_data_.emplace(url, entry);
}
void WebRestrictionsClient::Cache::RemoveCacheEntry(const std::string& url) {
base::AutoLock lock(lock_);
cache_data_.erase(url);
}
void WebRestrictionsClient::Cache::Clear() {
base::AutoLock lock(lock_);
cache_data_.clear();
}
} // namespace web_restrictions
|