Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(41)

Side by Side Diff: components/web_restriction/content_resolver_web_restriction_provider.cc

Issue 1423713015: [WIP] WebRestrictions (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: continue review in split cl Created 4 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(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_restriction/content_resolver_web_restriction_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_restriction {
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;
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 void SelfDeletingCallback::RequestSuccess(
59 JNIEnv* env,
60 const base::android::JavaParamRef<jobject>& obj,
61 jboolean request_success) {
62 callback_runner_->PostTask(FROM_HERE, base::Bind(callback_, request_success));
63 delete this;
64 }
65
66 } // namespace android
67
68 // static
69 bool ContentResolverWebRestrictionProvider::Register(JNIEnv* env) {
70 return android::RegisterNativesImpl(env);
71 }
72
73 ContentResolverWebRestrictionProvider::ContentResolverWebRestrictionProvider() {
74 initialized_ = false;
75 }
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_restriction
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698