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

Side by Side Diff: components/web_restrictions/browser/web_restrictions_client.cc

Issue 1847523002: Avoid HTML in WebRestrictionsContentProvider interface (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Client changes, and fix findbugs issues. Created 4 years, 8 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
1 // Copyright 2015 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "components/web_restrictions/browser/web_restrictions_client.h" 5 #include "components/web_restrictions/browser/web_restrictions_client.h"
6 6
7 #include "base/android/jni_string.h" 7 #include "base/android/jni_string.h"
8 #include "base/android/scoped_java_ref.h"
9 #include "base/bind.h" 8 #include "base/bind.h"
10 #include "base/location.h" 9 #include "base/location.h"
11 #include "base/thread_task_runner_handle.h" 10 #include "base/thread_task_runner_handle.h"
12 #include "content/public/browser/browser_thread.h" 11 #include "content/public/browser/browser_thread.h"
13 #include "jni/WebRestrictionsClient_jni.h" 12 #include "jni/WebRestrictionsClient_jni.h"
14 13
14 using base::android::ScopedJavaGlobalRef;
15
15 namespace web_restrictions { 16 namespace web_restrictions {
16 17
17 namespace { 18 namespace {
18 19
19 const size_t kMaxCacheSize = 100; 20 const size_t kMaxCacheSize = 100;
20 21
21 bool RequestPermissionTask( 22 bool RequestPermissionTask(
22 const GURL& url, 23 const GURL& url,
23 const base::android::JavaRef<jobject>& java_provider) { 24 const base::android::JavaRef<jobject>& java_provider) {
24 JNIEnv* env = base::android::AttachCurrentThread(); 25 JNIEnv* env = base::android::AttachCurrentThread();
(...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after
88 base::Unretained(this), provider_authority_)); 89 base::Unretained(this), provider_authority_));
89 } 90 }
90 91
91 UrlAccess WebRestrictionsClient::ShouldProceed( 92 UrlAccess WebRestrictionsClient::ShouldProceed(
92 bool is_main_frame, 93 bool is_main_frame,
93 const GURL& url, 94 const GURL& url,
94 const base::Callback<void(bool)>& callback) { 95 const base::Callback<void(bool)>& callback) {
95 DCHECK(single_thread_task_runner_->BelongsToCurrentThread()); 96 DCHECK(single_thread_task_runner_->BelongsToCurrentThread());
96 if (!initialized_) 97 if (!initialized_)
97 return ALLOW; 98 return ALLOW;
98 auto iter = url_access_cache_.find(url); 99 auto iter = cache_.find(url);
99 if (iter != url_access_cache_.end()) { 100 if (iter != cache_.end()) {
100 RecordURLAccess(url); 101 RecordURLAccess(url);
101 return iter->second ? ALLOW : DISALLOW; 102 JNIEnv* env = base::android::AttachCurrentThread();
103 return Java_ShouldProceedResult_shouldProceed(env, iter->second.obj())
104 ? ALLOW
105 : DISALLOW;
102 } 106 }
103 base::PostTaskAndReplyWithResult( 107 base::PostTaskAndReplyWithResult(
104 background_task_runner_.get(), FROM_HERE, 108 background_task_runner_.get(), FROM_HERE,
105 base::Bind(&WebRestrictionsClient::ShouldProceedTask, url, 109 base::Bind(&WebRestrictionsClient::ShouldProceedTask, url,
106 java_provider_), 110 java_provider_),
107 base::Bind(&WebRestrictionsClient::OnShouldProceedComplete, 111 base::Bind(&WebRestrictionsClient::OnShouldProceedComplete,
108 base::Unretained(this), provider_authority_, url, callback)); 112 base::Unretained(this), provider_authority_, url, callback));
109 113
110 return PENDING; 114 return PENDING;
111 } 115 }
112 116
113 bool WebRestrictionsClient::SupportsRequest() const { 117 bool WebRestrictionsClient::SupportsRequest() const {
114 return initialized_ && supports_request_; 118 return initialized_ && supports_request_;
115 } 119 }
116 120
117 bool WebRestrictionsClient::GetErrorHtml(const GURL& url, 121 int WebRestrictionsClient::GetResultColumnCount(const GURL& url) const {
118 std::string* error_page) const {
119 DCHECK(single_thread_task_runner_->BelongsToCurrentThread()); 122 DCHECK(single_thread_task_runner_->BelongsToCurrentThread());
120 if (!initialized_) 123 if (!initialized_)
121 return false; 124 return 0;
122 auto iter = error_page_cache_.find(url); 125 auto iter = cache_.find(url);
123 if (iter == error_page_cache_.end()) 126 if (iter == cache_.end())
124 return false; 127 return 0;
125 *error_page = iter->second; 128 return Java_ShouldProceedResult_getColumnCount(
126 return true; 129 base::android::AttachCurrentThread(), iter->second.obj());
130 }
131
132 std::string WebRestrictionsClient::GetResultColumnName(const GURL& url,
133 int column) const {
134 DCHECK(single_thread_task_runner_->BelongsToCurrentThread());
135 if (!initialized_)
136 return 0;
Bernhard Bauer 2016/03/31 10:09:07 How can you return an int in a method that is supp
aberent 2016/03/31 11:53:58 Done.
137 auto iter = cache_.find(url);
138 if (iter == cache_.end())
139 return 0;
140
141 JNIEnv* env = base::android::AttachCurrentThread();
142 return base::android::ConvertJavaStringToUTF8(
143 env,
144 Java_ShouldProceedResult_getColumnName(env, iter->second.obj(), column)
145 .obj());
146 }
147
148 int WebRestrictionsClient::GetResultIntValue(const GURL& url,
149 int column) const {
150 DCHECK(single_thread_task_runner_->BelongsToCurrentThread());
151 if (!initialized_)
152 return 0;
153 auto iter = cache_.find(url);
154 if (iter == cache_.end())
155 return 0;
156 return Java_ShouldProceedResult_getInt(base::android::AttachCurrentThread(),
157 iter->second.obj(), column);
158 }
159
160 std::string WebRestrictionsClient::GetResultStringValue(const GURL& url,
161 int column) const {
162 DCHECK(single_thread_task_runner_->BelongsToCurrentThread());
163 if (!initialized_)
164 return 0;
165 auto iter = cache_.find(url);
166 if (iter == cache_.end())
167 return 0;
168
169 JNIEnv* env = base::android::AttachCurrentThread();
170 return base::android::ConvertJavaStringToUTF8(
171 env, Java_ShouldProceedResult_getString(env, iter->second.obj(), column)
172 .obj());
127 } 173 }
128 174
129 void WebRestrictionsClient::RequestPermission( 175 void WebRestrictionsClient::RequestPermission(
130 const GURL& url, 176 const GURL& url,
131 const base::Callback<void(bool)>& request_success) { 177 const base::Callback<void(bool)>& request_success) {
132 if (!initialized_) { 178 if (!initialized_) {
133 request_success.Run(false); 179 request_success.Run(false);
134 return; 180 return;
135 } 181 }
136 base::PostTaskAndReplyWithResult( 182 base::PostTaskAndReplyWithResult(
137 background_task_runner_.get(), FROM_HERE, 183 background_task_runner_.get(), FROM_HERE,
138 base::Bind(&RequestPermissionTask, url, java_provider_), request_success); 184 base::Bind(&RequestPermissionTask, url, java_provider_), request_success);
139 } 185 }
140 186
141 void WebRestrictionsClient::OnWebRestrictionsChanged() { 187 void WebRestrictionsClient::OnWebRestrictionsChanged() {
142 single_thread_task_runner_->PostTask( 188 single_thread_task_runner_->PostTask(
143 FROM_HERE, 189 FROM_HERE,
144 base::Bind(&WebRestrictionsClient::ClearCache, base::Unretained(this))); 190 base::Bind(&WebRestrictionsClient::ClearCache, base::Unretained(this)));
145 } 191 }
146 192
147 void WebRestrictionsClient::RecordURLAccess(const GURL& url) { 193 void WebRestrictionsClient::RecordURLAccess(const GURL& url) {
148 DCHECK(single_thread_task_runner_->BelongsToCurrentThread()); 194 DCHECK(single_thread_task_runner_->BelongsToCurrentThread());
149 // Move the URL to the front of the cache. 195 // Move the URL to the front of the cache.
150 recent_urls_.remove(url); 196 recent_urls_.remove(url);
151 recent_urls_.push_front(url); 197 recent_urls_.push_front(url);
152 } 198 }
153 199
154 void WebRestrictionsClient::UpdateCache(std::string provider_authority, 200 void WebRestrictionsClient::UpdateCache(std::string provider_authority,
155 GURL url, 201 GURL url,
156 bool should_proceed, 202 ScopedJavaGlobalRef<jobject> result) {
157 std::string error_page) {
158 DCHECK(single_thread_task_runner_->BelongsToCurrentThread()); 203 DCHECK(single_thread_task_runner_->BelongsToCurrentThread());
159 // If the webrestrictions provider changed when the old one was being queried, 204 // If the webrestrictions provider changed when the old one was being queried,
160 // do not update the cache for the new provider. 205 // do not update the cache for the new provider.
161 if (provider_authority != provider_authority_) 206 if (provider_authority != provider_authority_)
162 return; 207 return;
163 RecordURLAccess(url); 208 RecordURLAccess(url);
164 if (recent_urls_.size() >= kMaxCacheSize) { 209 if (recent_urls_.size() >= kMaxCacheSize) {
165 url_access_cache_.erase(recent_urls_.back()); 210 cache_.erase(recent_urls_.back());
166 error_page_cache_.erase(recent_urls_.back());
167 recent_urls_.pop_back(); 211 recent_urls_.pop_back();
168 } 212 }
169 url_access_cache_[url] = should_proceed; 213 cache_[url] = result;
170 if (!error_page.empty()) {
171 error_page_cache_[url] = error_page;
172 } else {
173 error_page_cache_.erase(url);
174 }
175 } 214 }
176 215
177 void WebRestrictionsClient::RequestSupportKnown(std::string provider_authority, 216 void WebRestrictionsClient::RequestSupportKnown(std::string provider_authority,
178 bool supports_request) { 217 bool supports_request) {
179 // |supports_request_| is initialized to false. 218 // |supports_request_| is initialized to false.
180 DCHECK(!supports_request_); 219 DCHECK(!supports_request_);
181 DCHECK(single_thread_task_runner_->BelongsToCurrentThread()); 220 DCHECK(single_thread_task_runner_->BelongsToCurrentThread());
182 // If the webrestrictions provider changed when the old one was being queried, 221 // If the webrestrictions provider changed when the old one was being queried,
183 // ignore the result. 222 // ignore the result.
184 if (provider_authority != provider_authority_) 223 if (provider_authority != provider_authority_)
185 return; 224 return;
186 supports_request_ = supports_request; 225 supports_request_ = supports_request;
187 } 226 }
188 227
189 void WebRestrictionsClient::OnShouldProceedComplete( 228 void WebRestrictionsClient::OnShouldProceedComplete(
190 std::string provider_authority, 229 std::string provider_authority,
191 const GURL& url, 230 const GURL& url,
192 const base::Callback<void(bool)>& callback, 231 const base::Callback<void(bool)>& callback,
193 const ShouldProceedResult& result) { 232 const ScopedJavaGlobalRef<jobject>& result) {
194 UpdateCache(provider_authority, url, result.ok_to_proceed, result.error_page); 233 UpdateCache(provider_authority, url, result);
195 callback.Run(result.ok_to_proceed); 234 JNIEnv* env = base::android::AttachCurrentThread();
235 callback.Run(Java_ShouldProceedResult_shouldProceed(env, result.obj()));
196 } 236 }
197 237
198 void WebRestrictionsClient::ClearCache() { 238 void WebRestrictionsClient::ClearCache() {
199 DCHECK(single_thread_task_runner_->BelongsToCurrentThread()); 239 DCHECK(single_thread_task_runner_->BelongsToCurrentThread());
200 error_page_cache_.clear(); 240 cache_.clear();
201 url_access_cache_.clear();
202 recent_urls_.clear(); 241 recent_urls_.clear();
203 } 242 }
204 243
205 // static 244 // static
206 WebRestrictionsClient::ShouldProceedResult 245 ScopedJavaGlobalRef<jobject> WebRestrictionsClient::ShouldProceedTask(
207 WebRestrictionsClient::ShouldProceedTask(
208 const GURL& url, 246 const GURL& url,
209 const base::android::JavaRef<jobject>& java_provider) { 247 const base::android::JavaRef<jobject>& java_provider) {
210 WebRestrictionsClient::ShouldProceedResult result;
211 JNIEnv* env = base::android::AttachCurrentThread(); 248 JNIEnv* env = base::android::AttachCurrentThread();
212 base::android::ScopedJavaLocalRef<jobject> j_result = 249 base::android::ScopedJavaGlobalRef<jobject> result(
213 Java_WebRestrictionsClient_shouldProceed( 250 Java_WebRestrictionsClient_shouldProceed(
214 env, java_provider.obj(), 251 env, java_provider.obj(),
215 base::android::ConvertUTF8ToJavaString(env, url.spec()).obj()); 252 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.is_null()) {
221 base::android::ConvertJavaStringToUTF8(env, j_error_page.obj(),
222 &result.error_page);
223 }
224 return result; 253 return result;
225 } 254 }
226 255
227 void NotifyWebRestrictionsChanged( 256 void NotifyWebRestrictionsChanged(
228 JNIEnv* env, 257 JNIEnv* env,
229 const base::android::JavaParamRef<jobject>& clazz, 258 const base::android::JavaParamRef<jobject>& clazz,
230 jlong provider_ptr) { 259 jlong provider_ptr) {
231 WebRestrictionsClient* provider = 260 WebRestrictionsClient* provider =
232 reinterpret_cast<WebRestrictionsClient*>(provider_ptr); 261 reinterpret_cast<WebRestrictionsClient*>(provider_ptr);
233 // TODO(knn): Also reload existing interstitials/error pages. 262 // TODO(knn): Also reload existing interstitials/error pages.
234 provider->OnWebRestrictionsChanged(); 263 provider->OnWebRestrictionsChanged();
235 } 264 }
236 265
237 } // namespace web_restrictions 266 } // namespace web_restrictions
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698