OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 "android_webview/browser/aw_contents_io_thread_client_impl.h" | 5 #include "android_webview/browser/aw_contents_io_thread_client.h" |
6 | 6 |
7 #include <map> | 7 #include <map> |
8 #include <memory> | 8 #include <memory> |
9 #include <utility> | 9 #include <utility> |
10 | 10 |
11 #include "android_webview/browser/aw_contents_background_thread_client.h" | 11 #include "android_webview/browser/aw_contents_background_thread_client.h" |
12 #include "android_webview/browser/aw_web_resource_response_impl.h" | |
13 #include "android_webview/browser/net/aw_web_resource_request.h" | 12 #include "android_webview/browser/net/aw_web_resource_request.h" |
| 13 #include "android_webview/browser/net/aw_web_resource_response.h" |
14 #include "android_webview/common/devtools_instrumentation.h" | 14 #include "android_webview/common/devtools_instrumentation.h" |
15 #include "base/android/jni_array.h" | 15 #include "base/android/jni_array.h" |
16 #include "base/android/jni_string.h" | 16 #include "base/android/jni_string.h" |
17 #include "base/android/jni_weak_ref.h" | 17 #include "base/android/jni_weak_ref.h" |
18 #include "base/lazy_instance.h" | 18 #include "base/lazy_instance.h" |
19 #include "base/synchronization/lock.h" | 19 #include "base/synchronization/lock.h" |
20 #include "content/public/browser/browser_thread.h" | 20 #include "content/public/browser/browser_thread.h" |
21 #include "content/public/browser/render_frame_host.h" | 21 #include "content/public/browser/render_frame_host.h" |
22 #include "content/public/browser/render_process_host.h" | 22 #include "content/public/browser/render_process_host.h" |
23 #include "content/public/browser/render_view_host.h" | 23 #include "content/public/browser/render_view_host.h" |
(...skipping 160 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
184 RfhToIoThreadClientMap::GetInstance()->Erase(GetRenderFrameHostIdPair(rfh)); | 184 RfhToIoThreadClientMap::GetInstance()->Erase(GetRenderFrameHostIdPair(rfh)); |
185 RfhToIoThreadClientMap::GetInstance()->Erase(rfh->GetFrameTreeNodeId()); | 185 RfhToIoThreadClientMap::GetInstance()->Erase(rfh->GetFrameTreeNodeId()); |
186 } | 186 } |
187 | 187 |
188 void ClientMapEntryUpdater::WebContentsDestroyed() { | 188 void ClientMapEntryUpdater::WebContentsDestroyed() { |
189 delete this; | 189 delete this; |
190 } | 190 } |
191 | 191 |
192 } // namespace | 192 } // namespace |
193 | 193 |
194 // AwContentsIoThreadClientImpl ----------------------------------------------- | 194 // AwContentsIoThreadClient ----------------------------------------------- |
195 | 195 |
196 // static | 196 // static |
197 std::unique_ptr<AwContentsIoThreadClient> AwContentsIoThreadClient::FromID( | 197 std::unique_ptr<AwContentsIoThreadClient> AwContentsIoThreadClient::FromID( |
198 int render_process_id, | 198 int render_process_id, |
199 int render_frame_id) { | 199 int render_frame_id) { |
200 pair<int, int> rfh_id(render_process_id, render_frame_id); | 200 pair<int, int> rfh_id(render_process_id, render_frame_id); |
201 IoThreadClientData client_data; | 201 IoThreadClientData client_data; |
202 if (!RfhToIoThreadClientMap::GetInstance()->Get(rfh_id, &client_data)) | 202 if (!RfhToIoThreadClientMap::GetInstance()->Get(rfh_id, &client_data)) |
203 return std::unique_ptr<AwContentsIoThreadClient>(); | 203 return std::unique_ptr<AwContentsIoThreadClient>(); |
204 | 204 |
205 JNIEnv* env = AttachCurrentThread(); | 205 JNIEnv* env = AttachCurrentThread(); |
206 ScopedJavaLocalRef<jobject> java_delegate = | 206 ScopedJavaLocalRef<jobject> java_delegate = |
207 client_data.io_thread_client.get(env); | 207 client_data.io_thread_client.get(env); |
208 DCHECK(!client_data.pending_association || java_delegate.is_null()); | 208 DCHECK(!client_data.pending_association || java_delegate.is_null()); |
209 return std::unique_ptr<AwContentsIoThreadClient>( | 209 return std::unique_ptr<AwContentsIoThreadClient>(new AwContentsIoThreadClient( |
210 new AwContentsIoThreadClientImpl(client_data.pending_association, | 210 client_data.pending_association, java_delegate)); |
211 java_delegate)); | |
212 } | 211 } |
213 | 212 |
214 std::unique_ptr<AwContentsIoThreadClient> AwContentsIoThreadClient::FromID( | 213 std::unique_ptr<AwContentsIoThreadClient> AwContentsIoThreadClient::FromID( |
215 int frame_tree_node_id) { | 214 int frame_tree_node_id) { |
216 IoThreadClientData client_data; | 215 IoThreadClientData client_data; |
217 if (!RfhToIoThreadClientMap::GetInstance()->Get(frame_tree_node_id, | 216 if (!RfhToIoThreadClientMap::GetInstance()->Get(frame_tree_node_id, |
218 &client_data)) | 217 &client_data)) |
219 return std::unique_ptr<AwContentsIoThreadClient>(); | 218 return std::unique_ptr<AwContentsIoThreadClient>(); |
220 | 219 |
221 JNIEnv* env = AttachCurrentThread(); | 220 JNIEnv* env = AttachCurrentThread(); |
222 ScopedJavaLocalRef<jobject> java_delegate = | 221 ScopedJavaLocalRef<jobject> java_delegate = |
223 client_data.io_thread_client.get(env); | 222 client_data.io_thread_client.get(env); |
224 DCHECK(!client_data.pending_association || java_delegate.is_null()); | 223 DCHECK(!client_data.pending_association || java_delegate.is_null()); |
225 return std::unique_ptr<AwContentsIoThreadClient>( | 224 return std::unique_ptr<AwContentsIoThreadClient>(new AwContentsIoThreadClient( |
226 new AwContentsIoThreadClientImpl(client_data.pending_association, | 225 client_data.pending_association, java_delegate)); |
227 java_delegate)); | |
228 } | 226 } |
229 | 227 |
230 // static | 228 // static |
231 void AwContentsIoThreadClient::SubFrameCreated(int render_process_id, | 229 void AwContentsIoThreadClient::SubFrameCreated(int render_process_id, |
232 int parent_render_frame_id, | 230 int parent_render_frame_id, |
233 int child_render_frame_id) { | 231 int child_render_frame_id) { |
234 pair<int, int> parent_rfh_id(render_process_id, parent_render_frame_id); | 232 pair<int, int> parent_rfh_id(render_process_id, parent_render_frame_id); |
235 pair<int, int> child_rfh_id(render_process_id, child_render_frame_id); | 233 pair<int, int> child_rfh_id(render_process_id, child_render_frame_id); |
236 IoThreadClientData client_data; | 234 IoThreadClientData client_data; |
237 if (!RfhToIoThreadClientMap::GetInstance()->Get(parent_rfh_id, | 235 if (!RfhToIoThreadClientMap::GetInstance()->Get(parent_rfh_id, |
238 &client_data)) { | 236 &client_data)) { |
239 NOTREACHED(); | 237 NOTREACHED(); |
240 return; | 238 return; |
241 } | 239 } |
242 | 240 |
243 RfhToIoThreadClientMap::GetInstance()->Set(child_rfh_id, client_data); | 241 RfhToIoThreadClientMap::GetInstance()->Set(child_rfh_id, client_data); |
244 } | 242 } |
245 | 243 |
246 // static | 244 // static |
247 void AwContentsIoThreadClientImpl::RegisterPendingContents( | 245 void AwContentsIoThreadClient::RegisterPendingContents( |
248 WebContents* web_contents) { | 246 WebContents* web_contents) { |
249 IoThreadClientData client_data; | 247 IoThreadClientData client_data; |
250 client_data.pending_association = true; | 248 client_data.pending_association = true; |
251 RfhToIoThreadClientMap::GetInstance()->Set( | 249 RfhToIoThreadClientMap::GetInstance()->Set( |
252 GetRenderFrameHostIdPair(web_contents->GetMainFrame()), client_data); | 250 GetRenderFrameHostIdPair(web_contents->GetMainFrame()), client_data); |
253 } | 251 } |
254 | 252 |
255 // static | 253 // static |
256 void AwContentsIoThreadClientImpl::Associate(WebContents* web_contents, | 254 void AwContentsIoThreadClient::Associate(WebContents* web_contents, |
257 const JavaRef<jobject>& jclient) { | 255 const JavaRef<jobject>& jclient) { |
258 JNIEnv* env = AttachCurrentThread(); | 256 JNIEnv* env = AttachCurrentThread(); |
259 // The ClientMapEntryUpdater lifespan is tied to the WebContents. | 257 // The ClientMapEntryUpdater lifespan is tied to the WebContents. |
260 new ClientMapEntryUpdater(env, web_contents, jclient.obj()); | 258 new ClientMapEntryUpdater(env, web_contents, jclient.obj()); |
261 } | 259 } |
262 | 260 |
263 // static | 261 // static |
264 void AwContentsIoThreadClientImpl::SetServiceWorkerIoThreadClient( | 262 void AwContentsIoThreadClient::SetServiceWorkerIoThreadClient( |
265 const base::android::JavaRef<jobject>& jclient, | 263 const base::android::JavaRef<jobject>& jclient, |
266 const base::android::JavaRef<jobject>& browser_context) { | 264 const base::android::JavaRef<jobject>& browser_context) { |
267 // TODO: currently there is only one browser context so it is ok to | 265 // TODO: currently there is only one browser context so it is ok to |
268 // store in a global variable, in the future use browser_context to | 266 // store in a global variable, in the future use browser_context to |
269 // obtain the correct instance. | 267 // obtain the correct instance. |
270 JavaObjectWeakGlobalRef temp(AttachCurrentThread(), jclient.obj()); | 268 JavaObjectWeakGlobalRef temp(AttachCurrentThread(), jclient.obj()); |
271 g_sw_instance_.Get() = temp; | 269 g_sw_instance_.Get() = temp; |
272 } | 270 } |
273 | 271 |
274 // static | 272 // static |
275 std::unique_ptr<AwContentsIoThreadClient> | 273 std::unique_ptr<AwContentsIoThreadClient> |
276 AwContentsIoThreadClient::GetServiceWorkerIoThreadClient() { | 274 AwContentsIoThreadClient::GetServiceWorkerIoThreadClient() { |
277 JNIEnv* env = AttachCurrentThread(); | 275 JNIEnv* env = AttachCurrentThread(); |
278 ScopedJavaLocalRef<jobject> java_delegate = g_sw_instance_.Get().get(env); | 276 ScopedJavaLocalRef<jobject> java_delegate = g_sw_instance_.Get().get(env); |
279 | 277 |
280 if (java_delegate.is_null()) | 278 if (java_delegate.is_null()) |
281 return std::unique_ptr<AwContentsIoThreadClient>(); | 279 return std::unique_ptr<AwContentsIoThreadClient>(); |
282 | 280 |
283 return std::unique_ptr<AwContentsIoThreadClient>( | 281 return std::unique_ptr<AwContentsIoThreadClient>( |
284 new AwContentsIoThreadClientImpl(false, java_delegate)); | 282 new AwContentsIoThreadClient(false, java_delegate)); |
285 } | 283 } |
286 | 284 |
287 AwContentsIoThreadClientImpl::AwContentsIoThreadClientImpl( | 285 AwContentsIoThreadClient::AwContentsIoThreadClient(bool pending_association, |
288 bool pending_association, | 286 const JavaRef<jobject>& obj) |
289 const JavaRef<jobject>& obj) | |
290 : pending_association_(pending_association), java_object_(obj) {} | 287 : pending_association_(pending_association), java_object_(obj) {} |
291 | 288 |
292 AwContentsIoThreadClientImpl::~AwContentsIoThreadClientImpl() { | 289 AwContentsIoThreadClient::~AwContentsIoThreadClient() { |
293 // explict, out-of-line destructor. | 290 // explict, out-of-line destructor. |
294 } | 291 } |
295 | 292 |
296 bool AwContentsIoThreadClientImpl::PendingAssociation() const { | 293 bool AwContentsIoThreadClient::PendingAssociation() const { |
297 return pending_association_; | 294 return pending_association_; |
298 } | 295 } |
299 | 296 |
300 AwContentsIoThreadClient::CacheMode AwContentsIoThreadClientImpl::GetCacheMode() | 297 AwContentsIoThreadClient::CacheMode AwContentsIoThreadClient::GetCacheMode() |
301 const { | 298 const { |
302 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 299 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
303 if (java_object_.is_null()) | 300 if (java_object_.is_null()) |
304 return AwContentsIoThreadClient::LOAD_DEFAULT; | 301 return AwContentsIoThreadClient::LOAD_DEFAULT; |
305 | 302 |
306 JNIEnv* env = AttachCurrentThread(); | 303 JNIEnv* env = AttachCurrentThread(); |
307 return static_cast<AwContentsIoThreadClient::CacheMode>( | 304 return static_cast<AwContentsIoThreadClient::CacheMode>( |
308 Java_AwContentsIoThreadClient_getCacheMode(env, java_object_)); | 305 Java_AwContentsIoThreadClient_getCacheMode(env, java_object_)); |
309 } | 306 } |
310 | 307 |
(...skipping 13 matching lines...) Expand all Loading... |
324 | 321 |
325 devtools_instrumentation::ScopedEmbedderCallbackTask embedder_callback( | 322 devtools_instrumentation::ScopedEmbedderCallbackTask embedder_callback( |
326 "shouldInterceptRequest"); | 323 "shouldInterceptRequest"); |
327 ScopedJavaLocalRef<jobject> ret = | 324 ScopedJavaLocalRef<jobject> ret = |
328 AwContentsBackgroundThreadClient::shouldInterceptRequest( | 325 AwContentsBackgroundThreadClient::shouldInterceptRequest( |
329 env, obj, java_web_resource_request.jurl, request.is_main_frame, | 326 env, obj, java_web_resource_request.jurl, request.is_main_frame, |
330 request.has_user_gesture, java_web_resource_request.jmethod, | 327 request.has_user_gesture, java_web_resource_request.jmethod, |
331 java_web_resource_request.jheader_names, | 328 java_web_resource_request.jheader_names, |
332 java_web_resource_request.jheader_values); | 329 java_web_resource_request.jheader_values); |
333 return std::unique_ptr<AwWebResourceResponse>( | 330 return std::unique_ptr<AwWebResourceResponse>( |
334 ret.is_null() ? nullptr : new AwWebResourceResponseImpl(ret)); | 331 ret.is_null() ? nullptr : new AwWebResourceResponse(ret)); |
335 } | 332 } |
336 | 333 |
337 std::unique_ptr<AwWebResourceResponse> ReturnNull() { | 334 std::unique_ptr<AwWebResourceResponse> ReturnNull() { |
338 return std::unique_ptr<AwWebResourceResponse>(); | 335 return std::unique_ptr<AwWebResourceResponse>(); |
339 } | 336 } |
340 | 337 |
341 } // namespace | 338 } // namespace |
342 | 339 |
343 void AwContentsIoThreadClientImpl::ShouldInterceptRequestAsync( | 340 void AwContentsIoThreadClient::ShouldInterceptRequestAsync( |
344 const net::URLRequest* request, | 341 const net::URLRequest* request, |
345 const ShouldInterceptRequestResultCallback callback) { | 342 const ShouldInterceptRequestResultCallback callback) { |
346 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 343 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
347 base::Callback<std::unique_ptr<AwWebResourceResponse>()> get_response = | 344 base::Callback<std::unique_ptr<AwWebResourceResponse>()> get_response = |
348 base::Bind(&ReturnNull); | 345 base::Bind(&ReturnNull); |
349 JNIEnv* env = AttachCurrentThread(); | 346 JNIEnv* env = AttachCurrentThread(); |
350 if (bg_thread_client_object_.is_null() && !java_object_.is_null()) { | 347 if (bg_thread_client_object_.is_null() && !java_object_.is_null()) { |
351 bg_thread_client_object_.Reset( | 348 bg_thread_client_object_.Reset( |
352 Java_AwContentsIoThreadClient_getBackgroundThreadClient(env, | 349 Java_AwContentsIoThreadClient_getBackgroundThreadClient(env, |
353 java_object_)); | 350 java_object_)); |
354 } | 351 } |
355 if (!bg_thread_client_object_.is_null()) { | 352 if (!bg_thread_client_object_.is_null()) { |
356 get_response = base::Bind( | 353 get_response = base::Bind( |
357 &RunShouldInterceptRequest, AwWebResourceRequest(*request), | 354 &RunShouldInterceptRequest, AwWebResourceRequest(*request), |
358 JavaObjectWeakGlobalRef(env, bg_thread_client_object_.obj())); | 355 JavaObjectWeakGlobalRef(env, bg_thread_client_object_.obj())); |
359 } | 356 } |
360 BrowserThread::PostTaskAndReplyWithResult(BrowserThread::FILE, FROM_HERE, | 357 BrowserThread::PostTaskAndReplyWithResult(BrowserThread::FILE, FROM_HERE, |
361 get_response, callback); | 358 get_response, callback); |
362 } | 359 } |
363 | 360 |
364 bool AwContentsIoThreadClientImpl::ShouldBlockContentUrls() const { | 361 bool AwContentsIoThreadClient::ShouldBlockContentUrls() const { |
365 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 362 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
366 if (java_object_.is_null()) | 363 if (java_object_.is_null()) |
367 return false; | 364 return false; |
368 | 365 |
369 JNIEnv* env = AttachCurrentThread(); | 366 JNIEnv* env = AttachCurrentThread(); |
370 return Java_AwContentsIoThreadClient_shouldBlockContentUrls(env, | 367 return Java_AwContentsIoThreadClient_shouldBlockContentUrls(env, |
371 java_object_); | 368 java_object_); |
372 } | 369 } |
373 | 370 |
374 bool AwContentsIoThreadClientImpl::ShouldBlockFileUrls() const { | 371 bool AwContentsIoThreadClient::ShouldBlockFileUrls() const { |
375 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 372 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
376 if (java_object_.is_null()) | 373 if (java_object_.is_null()) |
377 return false; | 374 return false; |
378 | 375 |
379 JNIEnv* env = AttachCurrentThread(); | 376 JNIEnv* env = AttachCurrentThread(); |
380 return Java_AwContentsIoThreadClient_shouldBlockFileUrls(env, java_object_); | 377 return Java_AwContentsIoThreadClient_shouldBlockFileUrls(env, java_object_); |
381 } | 378 } |
382 | 379 |
383 bool AwContentsIoThreadClientImpl::ShouldAcceptThirdPartyCookies() const { | 380 bool AwContentsIoThreadClient::ShouldAcceptThirdPartyCookies() const { |
384 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 381 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
385 if (java_object_.is_null()) | 382 if (java_object_.is_null()) |
386 return false; | 383 return false; |
387 | 384 |
388 JNIEnv* env = AttachCurrentThread(); | 385 JNIEnv* env = AttachCurrentThread(); |
389 return Java_AwContentsIoThreadClient_shouldAcceptThirdPartyCookies( | 386 return Java_AwContentsIoThreadClient_shouldAcceptThirdPartyCookies( |
390 env, java_object_); | 387 env, java_object_); |
391 } | 388 } |
392 | 389 |
393 bool AwContentsIoThreadClientImpl::GetSafeBrowsingEnabled() const { | 390 bool AwContentsIoThreadClient::GetSafeBrowsingEnabled() const { |
394 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 391 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
395 if (java_object_.is_null()) | 392 if (java_object_.is_null()) |
396 return false; | 393 return false; |
397 | 394 |
398 JNIEnv* env = AttachCurrentThread(); | 395 JNIEnv* env = AttachCurrentThread(); |
399 return Java_AwContentsIoThreadClient_getSafeBrowsingEnabled(env, | 396 return Java_AwContentsIoThreadClient_getSafeBrowsingEnabled(env, |
400 java_object_); | 397 java_object_); |
401 } | 398 } |
402 | 399 |
403 bool AwContentsIoThreadClientImpl::ShouldBlockNetworkLoads() const { | 400 bool AwContentsIoThreadClient::ShouldBlockNetworkLoads() const { |
404 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 401 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
405 if (java_object_.is_null()) | 402 if (java_object_.is_null()) |
406 return false; | 403 return false; |
407 | 404 |
408 JNIEnv* env = AttachCurrentThread(); | 405 JNIEnv* env = AttachCurrentThread(); |
409 return Java_AwContentsIoThreadClient_shouldBlockNetworkLoads(env, | 406 return Java_AwContentsIoThreadClient_shouldBlockNetworkLoads(env, |
410 java_object_); | 407 java_object_); |
411 } | 408 } |
412 | 409 |
413 } // namespace android_webview | 410 } // namespace android_webview |
OLD | NEW |