diff options
author | Allan Sandfeld Jensen <allan.jensen@qt.io> | 2017-01-04 14:17:57 +0100 |
---|---|---|
committer | Allan Sandfeld Jensen <allan.jensen@qt.io> | 2017-01-05 10:05:06 +0000 |
commit | 39d357e3248f80abea0159765ff39554affb40db (patch) | |
tree | aba0e6bfb76de0244bba0f5fdbd64b830dd6e621 /chromium/components/web_restrictions | |
parent | 87778abf5a1f89266f37d1321b92a21851d8244d (diff) | |
download | qtwebengine-chromium-39d357e3248f80abea0159765ff39554affb40db.tar.gz |
BASELINE: Update Chromium to 55.0.2883.105
And updates ninja to 1.7.2
Change-Id: I20d43c737f82764d857ada9a55586901b18b9243
Reviewed-by: Michael BrĂ¼ning <michael.bruning@qt.io>
Diffstat (limited to 'chromium/components/web_restrictions')
18 files changed, 415 insertions, 297 deletions
diff --git a/chromium/components/web_restrictions/BUILD.gn b/chromium/components/web_restrictions/BUILD.gn index f6850ef7629..89c8b33d7de 100644 --- a/chromium/components/web_restrictions/BUILD.gn +++ b/chromium/components/web_restrictions/BUILD.gn @@ -3,73 +3,71 @@ # found in the LICENSE file. import("//build/config/android/rules.gni") +import("//mojo/public/tools/bindings/mojom.gni") import("//testing/test.gni") if (is_android) { - # GYP: components.gyp:web_restrictions_java android_library("web_restrictions_java") { java_files = [ - "browser/java/src/org/chromium/components/webrestrictions/WebRestrictionsContentProvider.java", - "browser/java/src/org/chromium/components/webrestrictions/WebRestrictionsClient.java", + "browser/java/src/org/chromium/components/webrestrictions/browser/WebRestrictionsContentProvider.java", + "browser/java/src/org/chromium/components/webrestrictions/browser/WebRestrictionsClient.java", + "browser/java/src/org/chromium/components/webrestrictions/browser/WebRestrictionsClientResult.java", ] deps = [ "//base:base_java", ] } - # GYP: //components/components_test.gyp:components_junit_tests junit_binary("components_web_restrictions_junit_tests") { java_files = [ - "browser/junit/src/org/chromium/components/webrestrictions/WebRestrictionsContentProviderTest.java", - "browser/junit/src/org/chromium/components/webrestrictions/WebRestrictionsClientTest.java", + "browser/junit/src/org/chromium/components/webrestrictions/browser/WebRestrictionsContentProviderTest.java", + "browser/junit/src/org/chromium/components/webrestrictions/browser/WebRestrictionsClientTest.java", ] deps = [ ":web_restrictions_java", "//base:base_java", - "//third_party/junit:hamcrest", + "//third_party/hamcrest:hamcrest_java", ] } generate_jni("web_restrictions_jni_headers") { sources = [ - "browser/java/src/org/chromium/components/webrestrictions/WebRestrictionsClient.java", + "browser/java/src/org/chromium/components/webrestrictions/browser/WebRestrictionsClient.java", + "browser/java/src/org/chromium/components/webrestrictions/browser/WebRestrictionsClientResult.java", ] jni_package = "web_restrictions" } - # GYP: components.gyp:web_restrictions_browser static_library("browser") { sources = [ "browser/web_restrictions_client.cc", "browser/web_restrictions_client.h", + "browser/web_restrictions_client_result.cc", + "browser/web_restrictions_client_result.h", + "browser/web_restrictions_mojo_implementation.cc", + "browser/web_restrictions_mojo_implementation.h", "browser/web_restrictions_resource_throttle.cc", "browser/web_restrictions_resource_throttle.h", ] deps = [ + ":interfaces", ":web_restrictions_jni_headers", "//base", "//content/public/browser", - "//net", + "//mojo/public/cpp/bindings:bindings", + "//net:net", ] } - # GYP: components.gyp:web_restrictions_renderer - static_library("renderer") { + mojom("interfaces") { sources = [ - "renderer/web_restrictions_gin_wrapper.cc", - "renderer/web_restrictions_gin_wrapper.h", - ] - deps = [ - "//base", - "//content/public/renderer", - "//gin", - "//third_party/WebKit/public:blink_headers", + "interfaces/web_restrictions.mojom", ] } generate_jni("test_support_jni_headers") { sources = [ - "browser/javatest/src/org/chromium/components/webrestrictions/MockWebRestrictionsClient.java", + "browser/javatest/src/org/chromium/components/webrestrictions/browser/MockWebRestrictionsClient.java", ] jni_package = "web_restrictions" deps = [ @@ -78,7 +76,7 @@ if (is_android) { } android_library("test_support_java") { - java_files = [ "browser/javatest/src/org/chromium/components/webrestrictions/MockWebRestrictionsClient.java" ] + java_files = [ "browser/javatest/src/org/chromium/components/webrestrictions/browser/MockWebRestrictionsClient.java" ] deps = [ ":web_restrictions_java", "//base:base_java", diff --git a/chromium/components/web_restrictions/browser/DEPS b/chromium/components/web_restrictions/browser/DEPS index 9e20e2009ad..084960ff934 100644 --- a/chromium/components/web_restrictions/browser/DEPS +++ b/chromium/components/web_restrictions/browser/DEPS @@ -2,6 +2,7 @@ include_rules = [ "+content/public/browser", "+content/public/test", "+jni", + "+mojo/public/cpp/bindings", "+net", "+url", ] diff --git a/chromium/components/web_restrictions/browser/mock_web_restrictions_client.cc b/chromium/components/web_restrictions/browser/mock_web_restrictions_client.cc index acc849cc3e0..28edb45778d 100644 --- a/chromium/components/web_restrictions/browser/mock_web_restrictions_client.cc +++ b/chromium/components/web_restrictions/browser/mock_web_restrictions_client.cc @@ -18,9 +18,5 @@ MockWebRestrictionsClient::~MockWebRestrictionsClient() { base::android::AttachCurrentThread()); } -bool MockWebRestrictionsClient::Register(JNIEnv* env) { - return RegisterNativesImpl(env); -} - } // namespace web_restrictions diff --git a/chromium/components/web_restrictions/browser/mock_web_restrictions_client.h b/chromium/components/web_restrictions/browser/mock_web_restrictions_client.h index 45dd4eb1603..056f2cf4fc2 100644 --- a/chromium/components/web_restrictions/browser/mock_web_restrictions_client.h +++ b/chromium/components/web_restrictions/browser/mock_web_restrictions_client.h @@ -17,8 +17,6 @@ class MockWebRestrictionsClient { MockWebRestrictionsClient(); ~MockWebRestrictionsClient(); - - static bool Register(JNIEnv* env); }; } // namespace web_restrictions diff --git a/chromium/components/web_restrictions/browser/web_restrictions_client.cc b/chromium/components/web_restrictions/browser/web_restrictions_client.cc index bb1a99bbb2e..559821d61b1 100644 --- a/chromium/components/web_restrictions/browser/web_restrictions_client.cc +++ b/chromium/components/web_restrictions/browser/web_restrictions_client.cc @@ -7,6 +7,7 @@ #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" @@ -20,18 +21,18 @@ namespace { const size_t kMaxCacheSize = 100; bool RequestPermissionTask( - const GURL& url, + const std::string& url, const base::android::JavaRef<jobject>& java_provider) { JNIEnv* env = base::android::AttachCurrentThread(); return Java_WebRestrictionsClient_requestPermission( - env, java_provider.obj(), - base::android::ConvertUTF8ToJavaString(env, url.spec()).obj()); + 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.obj()); + return Java_WebRestrictionsClient_supportsRequest(env, java_provider); } } // namespace @@ -43,7 +44,6 @@ bool WebRestrictionsClient::Register(JNIEnv* env) { WebRestrictionsClient::WebRestrictionsClient() : initialized_(false), supports_request_(false) { - single_thread_task_runner_ = base::ThreadTaskRunnerHandle::Get(); base::SequencedWorkerPool* worker_pool = content::BrowserThread::GetBlockingPool(); background_task_runner_ = @@ -56,17 +56,27 @@ WebRestrictionsClient::~WebRestrictionsClient() { if (java_provider_.is_null()) return; JNIEnv* env = base::android::AttachCurrentThread(); - Java_WebRestrictionsClient_onDestroy(env, java_provider_.obj()); + Java_WebRestrictionsClient_onDestroy(env, java_provider_); java_provider_.Reset(); } void WebRestrictionsClient::SetAuthority( const std::string& content_provider_authority) { - DCHECK(single_thread_task_runner_->BelongsToCurrentThread()); + // 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_.obj()); + Java_WebRestrictionsClient_onDestroy(env, java_provider_); java_provider_.Reset(); } ClearCache(); @@ -78,8 +88,7 @@ void WebRestrictionsClient::SetAuthority( return; java_provider_.Reset(Java_WebRestrictionsClient_create( env, - base::android::ConvertUTF8ToJavaString(env, content_provider_authority) - .obj(), + base::android::ConvertUTF8ToJavaString(env, content_provider_authority), reinterpret_cast<jlong>(this))); supports_request_ = false; base::PostTaskAndReplyWithResult( @@ -91,18 +100,17 @@ void WebRestrictionsClient::SetAuthority( UrlAccess WebRestrictionsClient::ShouldProceed( bool is_main_frame, - const GURL& url, + const std::string& url, const base::Callback<void(bool)>& callback) { - DCHECK(single_thread_task_runner_->BelongsToCurrentThread()); + DCHECK_CURRENTLY_ON(content::BrowserThread::IO); if (!initialized_) return ALLOW; - auto iter = cache_.find(url); - if (iter != cache_.end()) { + + std::unique_ptr<const WebRestrictionsClientResult> result = + cache_.GetCacheEntry(url); + if (result) { RecordURLAccess(url); - JNIEnv* env = base::android::AttachCurrentThread(); - return Java_ShouldProceedResult_shouldProceed(env, iter->second.obj()) - ? ALLOW - : DISALLOW; + return result->ShouldProceed() ? ALLOW : DISALLOW; } base::PostTaskAndReplyWithResult( background_task_runner_.get(), FROM_HERE, @@ -118,62 +126,8 @@ bool WebRestrictionsClient::SupportsRequest() const { return initialized_ && supports_request_; } -int WebRestrictionsClient::GetResultColumnCount(const GURL& url) const { - DCHECK(single_thread_task_runner_->BelongsToCurrentThread()); - if (!initialized_) - return 0; - auto iter = cache_.find(url); - if (iter == cache_.end()) - return 0; - return Java_ShouldProceedResult_getColumnCount( - base::android::AttachCurrentThread(), iter->second.obj()); -} - -std::string WebRestrictionsClient::GetResultColumnName(const GURL& url, - int column) const { - DCHECK(single_thread_task_runner_->BelongsToCurrentThread()); - if (!initialized_) - return std::string(); - auto iter = cache_.find(url); - if (iter == cache_.end()) - return std::string(); - - JNIEnv* env = base::android::AttachCurrentThread(); - return base::android::ConvertJavaStringToUTF8( - env, - Java_ShouldProceedResult_getColumnName(env, iter->second.obj(), column) - .obj()); -} - -int WebRestrictionsClient::GetResultIntValue(const GURL& url, - int column) const { - DCHECK(single_thread_task_runner_->BelongsToCurrentThread()); - if (!initialized_) - return 0; - auto iter = cache_.find(url); - if (iter == cache_.end()) - return 0; - return Java_ShouldProceedResult_getInt(base::android::AttachCurrentThread(), - iter->second.obj(), column); -} - -std::string WebRestrictionsClient::GetResultStringValue(const GURL& url, - int column) const { - DCHECK(single_thread_task_runner_->BelongsToCurrentThread()); - if (!initialized_) - return std::string(); - auto iter = cache_.find(url); - if (iter == cache_.end()) - return std::string(); - - JNIEnv* env = base::android::AttachCurrentThread(); - return base::android::ConvertJavaStringToUTF8( - env, Java_ShouldProceedResult_getString(env, iter->second.obj(), column) - .obj()); -} - void WebRestrictionsClient::RequestPermission( - const GURL& url, + const std::string& url, const base::Callback<void(bool)>& request_success) { if (!initialized_) { request_success.Run(false); @@ -184,40 +138,43 @@ void WebRestrictionsClient::RequestPermission( base::Bind(&RequestPermissionTask, url, java_provider_), request_success); } -void WebRestrictionsClient::OnWebRestrictionsChanged() { - single_thread_task_runner_->PostTask( - FROM_HERE, +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 GURL& url) { - DCHECK(single_thread_task_runner_->BelongsToCurrentThread()); +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(std::string provider_authority, - GURL url, +void WebRestrictionsClient::UpdateCache(const std::string& provider_authority, + const std::string& url, ScopedJavaGlobalRef<jobject> result) { - DCHECK(single_thread_task_runner_->BelongsToCurrentThread()); + 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_.erase(recent_urls_.back()); + cache_.RemoveCacheEntry(recent_urls_.back()); recent_urls_.pop_back(); } - cache_[url] = result; + cache_.SetCacheEntry(url, WebRestrictionsClientResult(result)); } -void WebRestrictionsClient::RequestSupportKnown(std::string provider_authority, - bool supports_request) { +void WebRestrictionsClient::RequestSupportKnown( + const std::string& provider_authority, + bool supports_request) { // |supports_request_| is initialized to false. DCHECK(!supports_request_); - DCHECK(single_thread_task_runner_->BelongsToCurrentThread()); + 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_) @@ -227,40 +184,66 @@ void WebRestrictionsClient::RequestSupportKnown(std::string provider_authority, void WebRestrictionsClient::OnShouldProceedComplete( std::string provider_authority, - const GURL& url, + const std::string& url, const base::Callback<void(bool)>& callback, const ScopedJavaGlobalRef<jobject>& result) { UpdateCache(provider_authority, url, result); - JNIEnv* env = base::android::AttachCurrentThread(); - callback.Run(Java_ShouldProceedResult_shouldProceed(env, result.obj())); + callback.Run(cache_.GetCacheEntry(url)->ShouldProceed()); } void WebRestrictionsClient::ClearCache() { - DCHECK(single_thread_task_runner_->BelongsToCurrentThread()); - cache_.clear(); + 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 GURL& url, + 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.obj(), - base::android::ConvertUTF8ToJavaString(env, url.spec()).obj())); + env, java_provider, + base::android::ConvertUTF8ToJavaString(env, url))); return result; } -void NotifyWebRestrictionsChanged( - JNIEnv* env, - const base::android::JavaParamRef<jobject>& clazz, - jlong provider_ptr) { - WebRestrictionsClient* provider = - reinterpret_cast<WebRestrictionsClient*>(provider_ptr); - // TODO(knn): Also reload existing interstitials/error pages. - provider->OnWebRestrictionsChanged(); +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 diff --git a/chromium/components/web_restrictions/browser/web_restrictions_client.h b/chromium/components/web_restrictions/browser/web_restrictions_client.h index bad0b608ef0..3bd54d9342c 100644 --- a/chromium/components/web_restrictions/browser/web_restrictions_client.h +++ b/chromium/components/web_restrictions/browser/web_restrictions_client.h @@ -6,9 +6,10 @@ #define COMPONENTS_WEB_RESTRICTION_WEB_RESTRICTIONS_CLIENT_H_ #include <jni.h> + #include <list> -#include <map> #include <string> +#include <unordered_map> #include "base/android/jni_android.h" #include "base/android/scoped_java_ref.h" @@ -17,7 +18,8 @@ #include "base/memory/ref_counted.h" #include "base/sequenced_task_runner.h" #include "base/single_thread_task_runner.h" -#include "url/gurl.h" +#include "base/synchronization/lock.h" +#include "components/web_restrictions/browser/web_restrictions_client_result.h" namespace web_restrictions { @@ -42,44 +44,67 @@ class WebRestrictionsClient { // WebRestrictionsProvider: UrlAccess ShouldProceed(bool is_main_frame, - const GURL& url, + const std::string& url, const base::Callback<void(bool)>& callback); bool SupportsRequest() const; - int GetResultColumnCount(const GURL& url) const; - - std::string GetResultColumnName(const GURL& url, int column) const; - - int GetResultIntValue(const GURL& url, int column) const; - - std::string GetResultStringValue(const GURL& url, int column) const; - - void RequestPermission(const GURL& url, + void RequestPermission(const std::string& url, const base::Callback<void(bool)>& callback); - void OnWebRestrictionsChanged(); + void OnWebRestrictionsChanged( + JNIEnv* env, + const base::android::JavaParamRef<jobject>& obj); - private: - - void RecordURLAccess(const GURL& url); + // Get a cached WebRestrictionsResult synchronously, for use when building + // error pages etc.. May be called on any thread, and will return a fresh copy + // of the result (hence thread safe). + std::unique_ptr<WebRestrictionsClientResult> GetCachedWebRestrictionsResult( + const std::string& url); - void UpdateCache(std::string provider_authority, - GURL url, + private: + // Make the test classes friends, so that they can set the authority + // synchronously + friend class WebRestrictionsResourceThrottleTest; + friend class WebRestrictionsClientTest; + + class Cache { + public: + Cache(); + ~Cache(); + std::unique_ptr<WebRestrictionsClientResult> GetCacheEntry( + const std::string& url); + void SetCacheEntry(const std::string& url, + const WebRestrictionsClientResult& entry); + void RemoveCacheEntry(const std::string& url); + void Clear(); + + private: + base::Lock lock_; + std::unordered_map<std::string, WebRestrictionsClientResult> cache_data_; + DISALLOW_COPY_AND_ASSIGN(Cache); + }; + + void SetAuthorityTask(const std::string& content_provider_authority); + + void RecordURLAccess(const std::string& url); + + void UpdateCache(const std::string& provider_authority, + const std::string& url, base::android::ScopedJavaGlobalRef<jobject> result); - void RequestSupportKnown(std::string provider_authority, + void RequestSupportKnown(const std::string& provider_authority, bool supports_request); void ClearCache(); static base::android::ScopedJavaGlobalRef<jobject> ShouldProceedTask( - const GURL& url, + const std::string& url, const base::android::JavaRef<jobject>& java_provider); void OnShouldProceedComplete( std::string provider_authority, - const GURL& url, + const std::string& url, const base::Callback<void(bool)>& callback, const base::android::ScopedJavaGlobalRef<jobject>& result); @@ -88,12 +113,10 @@ class WebRestrictionsClient { bool supports_request_; base::android::ScopedJavaGlobalRef<jobject> java_provider_; std::string provider_authority_; + Cache cache_; scoped_refptr<base::SequencedTaskRunner> background_task_runner_; - scoped_refptr<base::SingleThreadTaskRunner> single_thread_task_runner_; - - std::map<GURL, base::android::ScopedJavaGlobalRef<jobject>> cache_; - std::list<GURL> recent_urls_; + std::list<std::string> recent_urls_; DISALLOW_COPY_AND_ASSIGN(WebRestrictionsClient); }; diff --git a/chromium/components/web_restrictions/browser/web_restrictions_client_result.cc b/chromium/components/web_restrictions/browser/web_restrictions_client_result.cc new file mode 100644 index 00000000000..6eab3d5beb9 --- /dev/null +++ b/chromium/components/web_restrictions/browser/web_restrictions_client_result.cc @@ -0,0 +1,54 @@ +// Copyright 2016 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 "base/android/jni_string.h" +#include "components/web_restrictions/browser/web_restrictions_client_result.h" +#include "jni/WebRestrictionsClientResult_jni.h" + +namespace web_restrictions { + +WebRestrictionsClientResult::WebRestrictionsClientResult( + base::android::ScopedJavaGlobalRef<jobject>& jresult) + : jresult_(jresult) {} + +WebRestrictionsClientResult::~WebRestrictionsClientResult() = default; + +WebRestrictionsClientResult::WebRestrictionsClientResult( + const WebRestrictionsClientResult& other) = default; + +int WebRestrictionsClientResult::GetInt(int column) const { + return Java_WebRestrictionsClientResult_getInt( + base::android::AttachCurrentThread(), jresult_.obj(), column); +} + +std::string WebRestrictionsClientResult::GetString(int column) const { + JNIEnv* env = base::android::AttachCurrentThread(); + return base::android::ConvertJavaStringToUTF8( + env, + Java_WebRestrictionsClientResult_getString(env, jresult_.obj(), column)); +} + +std::string WebRestrictionsClientResult::GetColumnName(int column) const { + JNIEnv* env = base::android::AttachCurrentThread(); + return base::android::ConvertJavaStringToUTF8( + env, Java_WebRestrictionsClientResult_getColumnName(env, jresult_.obj(), + column)); +} + +bool WebRestrictionsClientResult::ShouldProceed() const { + return Java_WebRestrictionsClientResult_shouldProceed( + base::android::AttachCurrentThread(), jresult_.obj()); +} + +int web_restrictions::WebRestrictionsClientResult::GetColumnCount() const { + return Java_WebRestrictionsClientResult_getColumnCount( + base::android::AttachCurrentThread(), jresult_.obj()); +} + +bool web_restrictions::WebRestrictionsClientResult::IsString(int column) const { + return Java_WebRestrictionsClientResult_isString( + base::android::AttachCurrentThread(), jresult_.obj(), column); +} + +} // namespace web_restrictions diff --git a/chromium/components/web_restrictions/browser/web_restrictions_client_result.h b/chromium/components/web_restrictions/browser/web_restrictions_client_result.h new file mode 100644 index 00000000000..aaf6e6c5732 --- /dev/null +++ b/chromium/components/web_restrictions/browser/web_restrictions_client_result.h @@ -0,0 +1,36 @@ +// Copyright 2016 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. + +#ifndef COMPONENTS_WEB_RESTRICTIONS_BROWSER_WEB_RESTRICTIONS_CLIENT_RESULT_H_ +#define COMPONENTS_WEB_RESTRICTIONS_BROWSER_WEB_RESTRICTIONS_CLIENT_RESULT_H_ + +#include <string> + +#include "base/android/jni_android.h" +#include "base/android/scoped_java_ref.h" + +namespace web_restrictions { + +// Wrapper for Java WebRestrictionsResult +class WebRestrictionsClientResult { + public: + WebRestrictionsClientResult( + base::android::ScopedJavaGlobalRef<jobject>& jresult); + WebRestrictionsClientResult(const WebRestrictionsClientResult& other); + ~WebRestrictionsClientResult(); + + bool ShouldProceed() const; + int GetColumnCount() const; + bool IsString(int column) const; + int GetInt(int column) const; + std::string GetString(int column) const; + std::string GetColumnName(int column) const; + + private: + base::android::ScopedJavaGlobalRef<jobject> jresult_; +}; + +} // namespace web_restrictions + +#endif // COMPONENTS_WEB_RESTRICTIONS_BROWSER_WEB_RESTRICTIONS_CLIENT_RESULT_H_ diff --git a/chromium/components/web_restrictions/browser/web_restrictions_client_unittest.cc b/chromium/components/web_restrictions/browser/web_restrictions_client_unittest.cc index df9782c42fe..d3dd7f2327f 100644 --- a/chromium/components/web_restrictions/browser/web_restrictions_client_unittest.cc +++ b/chromium/components/web_restrictions/browser/web_restrictions_client_unittest.cc @@ -2,6 +2,8 @@ // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. +#include <memory> + #include "base/bind.h" #include "base/run_loop.h" #include "components/web_restrictions/browser/mock_web_restrictions_client.h" @@ -10,6 +12,7 @@ #include "testing/gtest/include/gtest/gtest.h" using web_restrictions::WebRestrictionsClient; +using web_restrictions::WebRestrictionsClientResult; using web_restrictions::MockWebRestrictionsClient; namespace { @@ -23,8 +26,13 @@ void ResultCallback(const base::Closure& quit_closure, bool result) { } // namespace +namespace web_restrictions { + class WebRestrictionsClientTest : public testing::Test { protected: + void SetAuthority(std::string authority) { + client_.SetAuthorityTask(authority); + } // Mock the Java WebRestrictionsClient. The real version // would need a content provider to do anything. MockWebRestrictionsClient mock_; @@ -33,90 +41,90 @@ class WebRestrictionsClientTest : public testing::Test { }; TEST_F(WebRestrictionsClientTest, ShouldProceed) { - client_.SetAuthority("Good"); + SetAuthority("Good"); // First call should go to Web Restrictions Content Provider, and return a // delayed result. { g_returned_result = false; base::RunLoop run_loop; - EXPECT_EQ(web_restrictions::PENDING, + ASSERT_EQ(web_restrictions::PENDING, client_.ShouldProceed( - true, GURL("http://example.com"), + true, "http://example.com", base::Bind(&ResultCallback, run_loop.QuitClosure()))); run_loop.Run(); EXPECT_TRUE(g_returned_result); - EXPECT_EQ(1, client_.GetResultColumnCount(GURL("http://example.com"))); } // A repeated call should go to the cache and return a result immediately. { base::RunLoop run_loop; - EXPECT_EQ(web_restrictions::ALLOW, + ASSERT_EQ(web_restrictions::ALLOW, client_.ShouldProceed( - true, GURL("http://example.com"), + true, "http://example.com", base::Bind(&ResultCallback, run_loop.QuitClosure()))); } // However a different url should miss the cache { g_returned_result = false; base::RunLoop run_loop; - EXPECT_EQ(web_restrictions::PENDING, + ASSERT_EQ(web_restrictions::PENDING, client_.ShouldProceed( - true, GURL("http://example.com/2"), + true, "http://example.com/2", base::Bind(&ResultCallback, run_loop.QuitClosure()))); run_loop.Run(); EXPECT_TRUE(g_returned_result); } // Switching the authority should clear the cache. { - client_.SetAuthority("Good2"); + SetAuthority("Good2"); g_returned_result = false; base::RunLoop run_loop; - EXPECT_EQ(web_restrictions::PENDING, + ASSERT_EQ(web_restrictions::PENDING, client_.ShouldProceed( - true, GURL("http://example.com/2"), + true, "http://example.com/2", base::Bind(&ResultCallback, run_loop.QuitClosure()))); run_loop.Run(); EXPECT_TRUE(g_returned_result); } // Try getting a bad result { - client_.SetAuthority("Bad"); + SetAuthority("Bad"); g_returned_result = true; base::RunLoop run_loop; - EXPECT_EQ(web_restrictions::PENDING, + ASSERT_EQ(web_restrictions::PENDING, client_.ShouldProceed( - true, GURL("http://example.com/2"), + true, "http://example.com/2", base::Bind(&ResultCallback, run_loop.QuitClosure()))); run_loop.Run(); EXPECT_FALSE(g_returned_result); - EXPECT_EQ(3, client_.GetResultColumnCount(GURL("http://example.com/2"))); - EXPECT_EQ(42, client_.GetResultIntValue(GURL("http://example.com/2"), 1)); - EXPECT_EQ("Error string", - client_.GetResultColumnName(GURL("http://example.com/2"), 2)); - EXPECT_EQ("http://example.com/2", - client_.GetResultStringValue(GURL("http://example.com/2"), 2)); + std::unique_ptr<const WebRestrictionsClientResult> result = + client_.GetCachedWebRestrictionsResult("http://example.com/2"); + ASSERT_NE(nullptr, result.get()); + EXPECT_EQ(42, result->GetInt(1)); + EXPECT_EQ("http://example.com/2", result->GetString(2)); } } TEST_F(WebRestrictionsClientTest, RequestPermission) { - client_.SetAuthority("Good"); { + SetAuthority("Good"); base::RunLoop run_loop; g_returned_result = false; client_.RequestPermission( - GURL("http://example.com"), + "http://example.com", base::Bind(&ResultCallback, run_loop.QuitClosure())); run_loop.Run(); EXPECT_TRUE(g_returned_result); - client_.SetAuthority("Bad"); } { + SetAuthority("Bad"); base::RunLoop run_loop; g_returned_result = true; client_.RequestPermission( - GURL("http://example.com"), + "http://example.com", base::Bind(&ResultCallback, run_loop.QuitClosure())); run_loop.Run(); EXPECT_FALSE(g_returned_result); } } + +} // namespace web_restrictions diff --git a/chromium/components/web_restrictions/browser/web_restrictions_mojo_implementation.cc b/chromium/components/web_restrictions/browser/web_restrictions_mojo_implementation.cc new file mode 100644 index 00000000000..fe3b6809ae3 --- /dev/null +++ b/chromium/components/web_restrictions/browser/web_restrictions_mojo_implementation.cc @@ -0,0 +1,69 @@ +// Copyright 2016 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_mojo_implementation.h" + +#include <utility> + +#include "base/bind.h" +#include "components/web_restrictions/browser/web_restrictions_client.h" +#include "mojo/public/cpp/bindings/strong_binding.h" + +namespace web_restrictions { + +namespace { + +void ClientRequestPermissionCallback( + const mojom::WebRestrictions::RequestPermissionCallback& callback, + bool result) { + callback.Run(result); +} + +} // namespace + +WebRestrictionsMojoImplementation::WebRestrictionsMojoImplementation( + WebRestrictionsClient* client) + : web_restrictions_client_(client) {} + +WebRestrictionsMojoImplementation::~WebRestrictionsMojoImplementation() {} + +void WebRestrictionsMojoImplementation::Create( + WebRestrictionsClient* client, + mojo::InterfaceRequest<mojom::WebRestrictions> request) { + mojo::MakeStrongBinding( + base::MakeUnique<WebRestrictionsMojoImplementation>(client), + std::move(request)); +} + +void WebRestrictionsMojoImplementation::GetResult( + const std::string& url, + const GetResultCallback& callback) { + std::unique_ptr<const WebRestrictionsClientResult> web_restrictions_result( + web_restrictions_client_->GetCachedWebRestrictionsResult(url)); + mojom::ClientResultPtr result = mojom::ClientResult::New(); + if (!web_restrictions_result) { + callback.Run(std::move(result)); + return; + } + int columnCount = web_restrictions_result->GetColumnCount(); + for (int i = 0; i < columnCount; i++) { + if (web_restrictions_result->IsString(i)) { + result->stringParams[web_restrictions_result->GetColumnName(i)] = + web_restrictions_result->GetString(i); + } else { + result->intParams[web_restrictions_result->GetColumnName(i)] = + web_restrictions_result->GetInt(i); + } + } + callback.Run(std::move(result)); +} + +void WebRestrictionsMojoImplementation::RequestPermission( + const std::string& url, + const mojom::WebRestrictions::RequestPermissionCallback& callback) { + web_restrictions_client_->RequestPermission( + url, base::Bind(&ClientRequestPermissionCallback, callback)); +} + +} // namespace web_restrictions diff --git a/chromium/components/web_restrictions/browser/web_restrictions_mojo_implementation.h b/chromium/components/web_restrictions/browser/web_restrictions_mojo_implementation.h new file mode 100644 index 00000000000..c13c03a76f2 --- /dev/null +++ b/chromium/components/web_restrictions/browser/web_restrictions_mojo_implementation.h @@ -0,0 +1,35 @@ +// Copyright 2016 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. + +#ifndef COMPONENTS_WEB_RESTRICTIONS_BROWSER_WEB_RESTRICTIONS_MOJO_IMPLEMENTATION_H_ +#define COMPONENTS_WEB_RESTRICTIONS_BROWSER_WEB_RESTRICTIONS_MOJO_IMPLEMENTATION_H_ + +#include <string> + +#include "base/macros.h" +#include "components/web_restrictions/interfaces/web_restrictions.mojom.h" + +namespace web_restrictions { + +class WebRestrictionsClient; + +class WebRestrictionsMojoImplementation : public mojom::WebRestrictions { + public: + explicit WebRestrictionsMojoImplementation(WebRestrictionsClient* client); + ~WebRestrictionsMojoImplementation() override; + + static void Create(WebRestrictionsClient* client, + mojo::InterfaceRequest<mojom::WebRestrictions> request); + + private: + void GetResult(const std::string& url, + const GetResultCallback& callback) override; + void RequestPermission(const std::string& url, + const RequestPermissionCallback& callback) override; + + WebRestrictionsClient* web_restrictions_client_; +}; + +} // namespace web_restrictions +#endif // COMPONENTS_WEB_RESTRICTIONS_BROWSER_WEB_RESTRICTIONS_MOJO_IMPLEMENTATION_H_ diff --git a/chromium/components/web_restrictions/browser/web_restrictions_resource_throttle.cc b/chromium/components/web_restrictions/browser/web_restrictions_resource_throttle.cc index 378fa9216ce..6f83f08faee 100644 --- a/chromium/components/web_restrictions/browser/web_restrictions_resource_throttle.cc +++ b/chromium/components/web_restrictions/browser/web_restrictions_resource_throttle.cc @@ -43,7 +43,7 @@ bool WebRestrictionsResourceThrottle::ShouldDefer(const GURL& url) { if (provider_->SupportsRequest() && !is_main_frame_) return false; UrlAccess access = provider_->ShouldProceed( - is_main_frame_, url, + is_main_frame_, url.spec(), base::Bind(&WebRestrictionsResourceThrottle::OnCheckResult, weak_ptr_factory_.GetWeakPtr())); if (access == DISALLOW) diff --git a/chromium/components/web_restrictions/browser/web_restrictions_resource_throttle_unittest.cc b/chromium/components/web_restrictions/browser/web_restrictions_resource_throttle_unittest.cc index f3e7342f0f2..d3b54b882fb 100644 --- a/chromium/components/web_restrictions/browser/web_restrictions_resource_throttle_unittest.cc +++ b/chromium/components/web_restrictions/browser/web_restrictions_resource_throttle_unittest.cc @@ -60,8 +60,12 @@ class WebRestrictionsResourceThrottleTest : public testing::Test { throttle_.set_controller_for_testing(&controller_); } + void SetAuthority(std::string authority) { + provider_.SetAuthorityTask(authority); + } + void StartProvider() { - provider_.SetAuthority("Good"); + SetAuthority("Good"); bool defer; throttle_.WillStartRequest(&defer); run_loop_.Run(); @@ -89,7 +93,7 @@ TEST_F(WebRestrictionsResourceThrottleTest, WillStartRequest_NoAuthority) { TEST_F(WebRestrictionsResourceThrottleTest, WillStartRequest_DeferredAllow) { // Test deferring with a resource provider, and that the correct results // are received. - provider_.SetAuthority("Good"); + SetAuthority("Good"); bool defer; throttle_.WillStartRequest(&defer); EXPECT_TRUE(defer); @@ -99,7 +103,7 @@ TEST_F(WebRestrictionsResourceThrottleTest, WillStartRequest_DeferredAllow) { } TEST_F(WebRestrictionsResourceThrottleTest, WillStartRequest_DeferredForbid) { - provider_.SetAuthority("Bad"); + SetAuthority("Bad"); bool defer; throttle_.WillStartRequest(&defer); EXPECT_TRUE(defer); @@ -123,7 +127,7 @@ TEST_F(WebRestrictionsResourceThrottleTest, WillStartRequest_Subresource) { throttle.set_controller_for_testing(&test_controller); bool defer; throttle.WillStartRequest(&defer); - EXPECT_FALSE(defer); + ASSERT_FALSE(defer); } TEST_F(WebRestrictionsResourceThrottleTest, WillRedirectRequest_KnownUrl) { @@ -134,7 +138,7 @@ TEST_F(WebRestrictionsResourceThrottleTest, WillRedirectRequest_KnownUrl) { redirect.new_url = GURL("http://example.com"); bool defer; throttle_.WillRedirectRequest(redirect, &defer); - EXPECT_FALSE(defer); + ASSERT_FALSE(defer); } TEST_F(WebRestrictionsResourceThrottleTest, WillRedirectRequest_NewUrl) { @@ -148,7 +152,7 @@ TEST_F(WebRestrictionsResourceThrottleTest, WillRedirectRequest_NewUrl) { throttle_.set_controller_for_testing(&test_controller); bool defer; throttle_.WillRedirectRequest(redirect, &defer); - EXPECT_TRUE(defer); + ASSERT_TRUE(defer); // If we don't wait for the callback it may happen after the exit, which // results in accesses the redirect_url after the stack frame is freed. test_run_loop.Run(); diff --git a/chromium/components/web_restrictions/interfaces/OWNERS b/chromium/components/web_restrictions/interfaces/OWNERS new file mode 100644 index 00000000000..08850f42120 --- /dev/null +++ b/chromium/components/web_restrictions/interfaces/OWNERS @@ -0,0 +1,2 @@ +per-file *.mojom=set noparent +per-file *.mojom=file://ipc/SECURITY_OWNERS diff --git a/chromium/components/web_restrictions/interfaces/web_restrictions.mojom b/chromium/components/web_restrictions/interfaces/web_restrictions.mojom new file mode 100644 index 00000000000..b47e5b91472 --- /dev/null +++ b/chromium/components/web_restrictions/interfaces/web_restrictions.mojom @@ -0,0 +1,22 @@ +// Copyright 2016 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. + +module web_restrictions.mojom; + +struct ClientResult { + map<string, int32> intParams; + map<string, string> stringParams; +}; + +interface WebRestrictions { + // Get the cached result, if any, of requesting access to a URL; including + // key/value pairs for custom error ints and strings. This has to be + // synchronous since it is needed to show the error page, and delaying the + // error page can result in a race with the Webview API. + [Sync] + GetResult(string url) => (ClientResult reply); + + // Request + RequestPermission(string url) => (bool result); +}; diff --git a/chromium/components/web_restrictions/renderer/DEPS b/chromium/components/web_restrictions/renderer/DEPS deleted file mode 100644 index dae8c787f9b..00000000000 --- a/chromium/components/web_restrictions/renderer/DEPS +++ /dev/null @@ -1,5 +0,0 @@ -include_rules = [ - "+content/public/renderer", - "+gin", - "+third_party/WebKit", -] diff --git a/chromium/components/web_restrictions/renderer/web_restrictions_gin_wrapper.cc b/chromium/components/web_restrictions/renderer/web_restrictions_gin_wrapper.cc deleted file mode 100644 index d94358767df..00000000000 --- a/chromium/components/web_restrictions/renderer/web_restrictions_gin_wrapper.cc +++ /dev/null @@ -1,60 +0,0 @@ -// 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/renderer/web_restrictions_gin_wrapper.h" - -#include "content/public/renderer/render_frame.h" -#include "gin/handle.h" -#include "gin/object_template_builder.h" -#include "third_party/WebKit/public/web/WebKit.h" -#include "third_party/WebKit/public/web/WebLocalFrame.h" - -namespace web_restrictions { - -gin::WrapperInfo WebRestrictionsGinWrapper::kWrapperInfo = { - gin::kEmbedderNativeGin}; - -// static -void WebRestrictionsGinWrapper::Install(content::RenderFrame* render_frame) { - v8::Isolate* isolate = blink::mainThreadIsolate(); - v8::HandleScope handle_scope(isolate); - v8::Local<v8::Context> context = - render_frame->GetWebFrame()->mainWorldScriptContext(); - if (context.IsEmpty()) - return; - v8::Context::Scope context_scope(context); - gin::Handle<WebRestrictionsGinWrapper> controller = - gin::CreateHandle(isolate, new WebRestrictionsGinWrapper(render_frame)); - if (controller.IsEmpty()) - return; - v8::Local<v8::Object> global = context->Global(); - global->Set(gin::StringToV8(isolate, "webRestriction"), controller.ToV8()); -} - -WebRestrictionsGinWrapper::WebRestrictionsGinWrapper( - content::RenderFrame* render_frame) - : content::RenderFrameObserver(render_frame) {} - -WebRestrictionsGinWrapper::~WebRestrictionsGinWrapper() {} - -bool WebRestrictionsGinWrapper::RequestPermission() { - if (!render_frame()) - return false; - render_frame()->GetWebFrame()->reload(); - return true; -} - -void WebRestrictionsGinWrapper::OnDestruct() { - // Do nothing. Overrides version that deletes RenderFrameObserver. -} - -gin::ObjectTemplateBuilder WebRestrictionsGinWrapper::GetObjectTemplateBuilder( - v8::Isolate* isolate) { - return gin::Wrappable<WebRestrictionsGinWrapper>::GetObjectTemplateBuilder( - isolate) - .SetMethod("requestPermission", - &WebRestrictionsGinWrapper::RequestPermission); -} - -} // namespace web_restrictions diff --git a/chromium/components/web_restrictions/renderer/web_restrictions_gin_wrapper.h b/chromium/components/web_restrictions/renderer/web_restrictions_gin_wrapper.h deleted file mode 100644 index e7d3d3fc4f1..00000000000 --- a/chromium/components/web_restrictions/renderer/web_restrictions_gin_wrapper.h +++ /dev/null @@ -1,46 +0,0 @@ -// 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. - -#ifndef COMPONENTS_WEB_RESTRICTION_WEB_RESTRICTION_GIN_WRAPPER_H_ -#define COMPONENTS_WEB_RESTRICTION_WEB_RESTRICTION_GIN_WRAPPER_H_ - -#include "base/macros.h" -#include "content/public/renderer/render_frame_observer.h" -#include "gin/wrappable.h" - -namespace content { -class RenderFrame; -} - -namespace web_restrictions { - -class WebRestrictionsGinWrapper - : public gin::Wrappable<WebRestrictionsGinWrapper>, - public content::RenderFrameObserver { - public: - static gin::WrapperInfo kWrapperInfo; - - static void Install(content::RenderFrame* render_frame); - - private: - explicit WebRestrictionsGinWrapper(content::RenderFrame* render_frame); - ~WebRestrictionsGinWrapper() override; - - // Override OnDestruct to prevent the wrapper going away when the - // RenderFrame does - void OnDestruct() override; - - // Request permission to allow visiting the currently blocked site. - bool RequestPermission(); - - // gin::WrappableBase - gin::ObjectTemplateBuilder GetObjectTemplateBuilder( - v8::Isolate* isolate) override; - - DISALLOW_COPY_AND_ASSIGN(WebRestrictionsGinWrapper); -}; - -} // namespace web_restrictions. - -#endif // COMPONENTS_WEB_RESTRICTION_WEB_RESTRICTION_GIN_WRAPPER_H_ |