OLD | NEW |
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/bind.h" | 8 #include "base/bind.h" |
9 #include "base/location.h" | 9 #include "base/location.h" |
10 #include "base/threading/thread_task_runner_handle.h" | 10 #include "base/threading/thread_task_runner_handle.h" |
11 #include "content/public/browser/browser_thread.h" | 11 #include "content/public/browser/browser_thread.h" |
12 #include "jni/WebRestrictionsClient_jni.h" | 12 #include "jni/WebRestrictionsClient_jni.h" |
13 | 13 |
14 using base::android::ScopedJavaGlobalRef; | 14 using base::android::ScopedJavaGlobalRef; |
15 | 15 |
16 namespace web_restrictions { | 16 namespace web_restrictions { |
17 | 17 |
18 namespace { | 18 namespace { |
19 | 19 |
20 const size_t kMaxCacheSize = 100; | 20 const size_t kMaxCacheSize = 100; |
21 | 21 |
22 bool RequestPermissionTask( | 22 bool RequestPermissionTask( |
23 const GURL& url, | 23 const GURL& url, |
24 const base::android::JavaRef<jobject>& java_provider) { | 24 const base::android::JavaRef<jobject>& java_provider) { |
25 JNIEnv* env = base::android::AttachCurrentThread(); | 25 JNIEnv* env = base::android::AttachCurrentThread(); |
26 return Java_WebRestrictionsClient_requestPermission( | 26 return Java_WebRestrictionsClient_requestPermission( |
27 env, java_provider.obj(), | 27 env, java_provider, |
28 base::android::ConvertUTF8ToJavaString(env, url.spec()).obj()); | 28 base::android::ConvertUTF8ToJavaString(env, url.spec())); |
29 } | 29 } |
30 | 30 |
31 bool CheckSupportsRequestTask( | 31 bool CheckSupportsRequestTask( |
32 const base::android::JavaRef<jobject>& java_provider) { | 32 const base::android::JavaRef<jobject>& java_provider) { |
33 JNIEnv* env = base::android::AttachCurrentThread(); | 33 JNIEnv* env = base::android::AttachCurrentThread(); |
34 return Java_WebRestrictionsClient_supportsRequest(env, java_provider.obj()); | 34 return Java_WebRestrictionsClient_supportsRequest(env, java_provider); |
35 } | 35 } |
36 | 36 |
37 } // namespace | 37 } // namespace |
38 | 38 |
39 // static | 39 // static |
40 bool WebRestrictionsClient::Register(JNIEnv* env) { | 40 bool WebRestrictionsClient::Register(JNIEnv* env) { |
41 return RegisterNativesImpl(env); | 41 return RegisterNativesImpl(env); |
42 } | 42 } |
43 | 43 |
44 WebRestrictionsClient::WebRestrictionsClient() | 44 WebRestrictionsClient::WebRestrictionsClient() |
45 : initialized_(false), supports_request_(false) { | 45 : initialized_(false), supports_request_(false) { |
46 single_thread_task_runner_ = base::ThreadTaskRunnerHandle::Get(); | 46 single_thread_task_runner_ = base::ThreadTaskRunnerHandle::Get(); |
47 base::SequencedWorkerPool* worker_pool = | 47 base::SequencedWorkerPool* worker_pool = |
48 content::BrowserThread::GetBlockingPool(); | 48 content::BrowserThread::GetBlockingPool(); |
49 background_task_runner_ = | 49 background_task_runner_ = |
50 worker_pool->GetSequencedTaskRunnerWithShutdownBehavior( | 50 worker_pool->GetSequencedTaskRunnerWithShutdownBehavior( |
51 worker_pool->GetSequenceToken(), | 51 worker_pool->GetSequenceToken(), |
52 base::SequencedWorkerPool::SKIP_ON_SHUTDOWN); | 52 base::SequencedWorkerPool::SKIP_ON_SHUTDOWN); |
53 } | 53 } |
54 | 54 |
55 WebRestrictionsClient::~WebRestrictionsClient() { | 55 WebRestrictionsClient::~WebRestrictionsClient() { |
56 if (java_provider_.is_null()) | 56 if (java_provider_.is_null()) |
57 return; | 57 return; |
58 JNIEnv* env = base::android::AttachCurrentThread(); | 58 JNIEnv* env = base::android::AttachCurrentThread(); |
59 Java_WebRestrictionsClient_onDestroy(env, java_provider_.obj()); | 59 Java_WebRestrictionsClient_onDestroy(env, java_provider_); |
60 java_provider_.Reset(); | 60 java_provider_.Reset(); |
61 } | 61 } |
62 | 62 |
63 void WebRestrictionsClient::SetAuthority( | 63 void WebRestrictionsClient::SetAuthority( |
64 const std::string& content_provider_authority) { | 64 const std::string& content_provider_authority) { |
65 DCHECK(single_thread_task_runner_->BelongsToCurrentThread()); | 65 DCHECK(single_thread_task_runner_->BelongsToCurrentThread()); |
66 // Destroy any existing content resolver. | 66 // Destroy any existing content resolver. |
67 JNIEnv* env = base::android::AttachCurrentThread(); | 67 JNIEnv* env = base::android::AttachCurrentThread(); |
68 if (!java_provider_.is_null()) { | 68 if (!java_provider_.is_null()) { |
69 Java_WebRestrictionsClient_onDestroy(env, java_provider_.obj()); | 69 Java_WebRestrictionsClient_onDestroy(env, java_provider_); |
70 java_provider_.Reset(); | 70 java_provider_.Reset(); |
71 } | 71 } |
72 ClearCache(); | 72 ClearCache(); |
73 provider_authority_ = content_provider_authority; | 73 provider_authority_ = content_provider_authority; |
74 | 74 |
75 // Initialize the content resolver. | 75 // Initialize the content resolver. |
76 initialized_ = !content_provider_authority.empty(); | 76 initialized_ = !content_provider_authority.empty(); |
77 if (!initialized_) | 77 if (!initialized_) |
78 return; | 78 return; |
79 java_provider_.Reset(Java_WebRestrictionsClient_create( | 79 java_provider_.Reset(Java_WebRestrictionsClient_create( |
80 env, | 80 env, |
81 base::android::ConvertUTF8ToJavaString(env, content_provider_authority) | 81 base::android::ConvertUTF8ToJavaString(env, content_provider_authority), |
82 .obj(), | |
83 reinterpret_cast<jlong>(this))); | 82 reinterpret_cast<jlong>(this))); |
84 supports_request_ = false; | 83 supports_request_ = false; |
85 base::PostTaskAndReplyWithResult( | 84 base::PostTaskAndReplyWithResult( |
86 content::BrowserThread::GetBlockingPool(), FROM_HERE, | 85 content::BrowserThread::GetBlockingPool(), FROM_HERE, |
87 base::Bind(&CheckSupportsRequestTask, java_provider_), | 86 base::Bind(&CheckSupportsRequestTask, java_provider_), |
88 base::Bind(&WebRestrictionsClient::RequestSupportKnown, | 87 base::Bind(&WebRestrictionsClient::RequestSupportKnown, |
89 base::Unretained(this), provider_authority_)); | 88 base::Unretained(this), provider_authority_)); |
90 } | 89 } |
91 | 90 |
92 UrlAccess WebRestrictionsClient::ShouldProceed( | 91 UrlAccess WebRestrictionsClient::ShouldProceed( |
93 bool is_main_frame, | 92 bool is_main_frame, |
94 const GURL& url, | 93 const GURL& url, |
95 const base::Callback<void(bool)>& callback) { | 94 const base::Callback<void(bool)>& callback) { |
96 DCHECK(single_thread_task_runner_->BelongsToCurrentThread()); | 95 DCHECK(single_thread_task_runner_->BelongsToCurrentThread()); |
97 if (!initialized_) | 96 if (!initialized_) |
98 return ALLOW; | 97 return ALLOW; |
99 auto iter = cache_.find(url); | 98 auto iter = cache_.find(url); |
100 if (iter != cache_.end()) { | 99 if (iter != cache_.end()) { |
101 RecordURLAccess(url); | 100 RecordURLAccess(url); |
102 JNIEnv* env = base::android::AttachCurrentThread(); | 101 JNIEnv* env = base::android::AttachCurrentThread(); |
103 return Java_ShouldProceedResult_shouldProceed(env, iter->second.obj()) | 102 return Java_ShouldProceedResult_shouldProceed(env, iter->second) ? ALLOW |
104 ? ALLOW | 103 : DISALLOW; |
105 : DISALLOW; | |
106 } | 104 } |
107 base::PostTaskAndReplyWithResult( | 105 base::PostTaskAndReplyWithResult( |
108 background_task_runner_.get(), FROM_HERE, | 106 background_task_runner_.get(), FROM_HERE, |
109 base::Bind(&WebRestrictionsClient::ShouldProceedTask, url, | 107 base::Bind(&WebRestrictionsClient::ShouldProceedTask, url, |
110 java_provider_), | 108 java_provider_), |
111 base::Bind(&WebRestrictionsClient::OnShouldProceedComplete, | 109 base::Bind(&WebRestrictionsClient::OnShouldProceedComplete, |
112 base::Unretained(this), provider_authority_, url, callback)); | 110 base::Unretained(this), provider_authority_, url, callback)); |
113 | 111 |
114 return PENDING; | 112 return PENDING; |
115 } | 113 } |
116 | 114 |
117 bool WebRestrictionsClient::SupportsRequest() const { | 115 bool WebRestrictionsClient::SupportsRequest() const { |
118 return initialized_ && supports_request_; | 116 return initialized_ && supports_request_; |
119 } | 117 } |
120 | 118 |
121 int WebRestrictionsClient::GetResultColumnCount(const GURL& url) const { | 119 int WebRestrictionsClient::GetResultColumnCount(const GURL& url) const { |
122 DCHECK(single_thread_task_runner_->BelongsToCurrentThread()); | 120 DCHECK(single_thread_task_runner_->BelongsToCurrentThread()); |
123 if (!initialized_) | 121 if (!initialized_) |
124 return 0; | 122 return 0; |
125 auto iter = cache_.find(url); | 123 auto iter = cache_.find(url); |
126 if (iter == cache_.end()) | 124 if (iter == cache_.end()) |
127 return 0; | 125 return 0; |
128 return Java_ShouldProceedResult_getColumnCount( | 126 return Java_ShouldProceedResult_getColumnCount( |
129 base::android::AttachCurrentThread(), iter->second.obj()); | 127 base::android::AttachCurrentThread(), iter->second); |
130 } | 128 } |
131 | 129 |
132 std::string WebRestrictionsClient::GetResultColumnName(const GURL& url, | 130 std::string WebRestrictionsClient::GetResultColumnName(const GURL& url, |
133 int column) const { | 131 int column) const { |
134 DCHECK(single_thread_task_runner_->BelongsToCurrentThread()); | 132 DCHECK(single_thread_task_runner_->BelongsToCurrentThread()); |
135 if (!initialized_) | 133 if (!initialized_) |
136 return std::string(); | 134 return std::string(); |
137 auto iter = cache_.find(url); | 135 auto iter = cache_.find(url); |
138 if (iter == cache_.end()) | 136 if (iter == cache_.end()) |
139 return std::string(); | 137 return std::string(); |
140 | 138 |
141 JNIEnv* env = base::android::AttachCurrentThread(); | 139 JNIEnv* env = base::android::AttachCurrentThread(); |
142 return base::android::ConvertJavaStringToUTF8( | 140 return base::android::ConvertJavaStringToUTF8( |
143 env, | 141 env, |
144 Java_ShouldProceedResult_getColumnName(env, iter->second.obj(), column) | 142 Java_ShouldProceedResult_getColumnName(env, iter->second, column).obj()); |
145 .obj()); | |
146 } | 143 } |
147 | 144 |
148 int WebRestrictionsClient::GetResultIntValue(const GURL& url, | 145 int WebRestrictionsClient::GetResultIntValue(const GURL& url, |
149 int column) const { | 146 int column) const { |
150 DCHECK(single_thread_task_runner_->BelongsToCurrentThread()); | 147 DCHECK(single_thread_task_runner_->BelongsToCurrentThread()); |
151 if (!initialized_) | 148 if (!initialized_) |
152 return 0; | 149 return 0; |
153 auto iter = cache_.find(url); | 150 auto iter = cache_.find(url); |
154 if (iter == cache_.end()) | 151 if (iter == cache_.end()) |
155 return 0; | 152 return 0; |
156 return Java_ShouldProceedResult_getInt(base::android::AttachCurrentThread(), | 153 return Java_ShouldProceedResult_getInt(base::android::AttachCurrentThread(), |
157 iter->second.obj(), column); | 154 iter->second, column); |
158 } | 155 } |
159 | 156 |
160 std::string WebRestrictionsClient::GetResultStringValue(const GURL& url, | 157 std::string WebRestrictionsClient::GetResultStringValue(const GURL& url, |
161 int column) const { | 158 int column) const { |
162 DCHECK(single_thread_task_runner_->BelongsToCurrentThread()); | 159 DCHECK(single_thread_task_runner_->BelongsToCurrentThread()); |
163 if (!initialized_) | 160 if (!initialized_) |
164 return std::string(); | 161 return std::string(); |
165 auto iter = cache_.find(url); | 162 auto iter = cache_.find(url); |
166 if (iter == cache_.end()) | 163 if (iter == cache_.end()) |
167 return std::string(); | 164 return std::string(); |
168 | 165 |
169 JNIEnv* env = base::android::AttachCurrentThread(); | 166 JNIEnv* env = base::android::AttachCurrentThread(); |
170 return base::android::ConvertJavaStringToUTF8( | 167 return base::android::ConvertJavaStringToUTF8( |
171 env, Java_ShouldProceedResult_getString(env, iter->second.obj(), column) | 168 env, Java_ShouldProceedResult_getString(env, iter->second, column).obj()); |
172 .obj()); | |
173 } | 169 } |
174 | 170 |
175 void WebRestrictionsClient::RequestPermission( | 171 void WebRestrictionsClient::RequestPermission( |
176 const GURL& url, | 172 const GURL& url, |
177 const base::Callback<void(bool)>& request_success) { | 173 const base::Callback<void(bool)>& request_success) { |
178 if (!initialized_) { | 174 if (!initialized_) { |
179 request_success.Run(false); | 175 request_success.Run(false); |
180 return; | 176 return; |
181 } | 177 } |
182 base::PostTaskAndReplyWithResult( | 178 base::PostTaskAndReplyWithResult( |
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
225 supports_request_ = supports_request; | 221 supports_request_ = supports_request; |
226 } | 222 } |
227 | 223 |
228 void WebRestrictionsClient::OnShouldProceedComplete( | 224 void WebRestrictionsClient::OnShouldProceedComplete( |
229 std::string provider_authority, | 225 std::string provider_authority, |
230 const GURL& url, | 226 const GURL& url, |
231 const base::Callback<void(bool)>& callback, | 227 const base::Callback<void(bool)>& callback, |
232 const ScopedJavaGlobalRef<jobject>& result) { | 228 const ScopedJavaGlobalRef<jobject>& result) { |
233 UpdateCache(provider_authority, url, result); | 229 UpdateCache(provider_authority, url, result); |
234 JNIEnv* env = base::android::AttachCurrentThread(); | 230 JNIEnv* env = base::android::AttachCurrentThread(); |
235 callback.Run(Java_ShouldProceedResult_shouldProceed(env, result.obj())); | 231 callback.Run(Java_ShouldProceedResult_shouldProceed(env, result)); |
236 } | 232 } |
237 | 233 |
238 void WebRestrictionsClient::ClearCache() { | 234 void WebRestrictionsClient::ClearCache() { |
239 DCHECK(single_thread_task_runner_->BelongsToCurrentThread()); | 235 DCHECK(single_thread_task_runner_->BelongsToCurrentThread()); |
240 cache_.clear(); | 236 cache_.clear(); |
241 recent_urls_.clear(); | 237 recent_urls_.clear(); |
242 } | 238 } |
243 | 239 |
244 // static | 240 // static |
245 ScopedJavaGlobalRef<jobject> WebRestrictionsClient::ShouldProceedTask( | 241 ScopedJavaGlobalRef<jobject> WebRestrictionsClient::ShouldProceedTask( |
246 const GURL& url, | 242 const GURL& url, |
247 const base::android::JavaRef<jobject>& java_provider) { | 243 const base::android::JavaRef<jobject>& java_provider) { |
248 JNIEnv* env = base::android::AttachCurrentThread(); | 244 JNIEnv* env = base::android::AttachCurrentThread(); |
249 base::android::ScopedJavaGlobalRef<jobject> result( | 245 base::android::ScopedJavaGlobalRef<jobject> result( |
250 Java_WebRestrictionsClient_shouldProceed( | 246 Java_WebRestrictionsClient_shouldProceed( |
251 env, java_provider.obj(), | 247 env, java_provider, |
252 base::android::ConvertUTF8ToJavaString(env, url.spec()).obj())); | 248 base::android::ConvertUTF8ToJavaString(env, url.spec()))); |
253 return result; | 249 return result; |
254 } | 250 } |
255 | 251 |
256 void NotifyWebRestrictionsChanged( | 252 void NotifyWebRestrictionsChanged( |
257 JNIEnv* env, | 253 JNIEnv* env, |
258 const base::android::JavaParamRef<jobject>& clazz, | 254 const base::android::JavaParamRef<jobject>& clazz, |
259 jlong provider_ptr) { | 255 jlong provider_ptr) { |
260 WebRestrictionsClient* provider = | 256 WebRestrictionsClient* provider = |
261 reinterpret_cast<WebRestrictionsClient*>(provider_ptr); | 257 reinterpret_cast<WebRestrictionsClient*>(provider_ptr); |
262 // TODO(knn): Also reload existing interstitials/error pages. | 258 // TODO(knn): Also reload existing interstitials/error pages. |
263 provider->OnWebRestrictionsChanged(); | 259 provider->OnWebRestrictionsChanged(); |
264 } | 260 } |
265 | 261 |
266 } // namespace web_restrictions | 262 } // namespace web_restrictions |
OLD | NEW |