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/native/aw_contents_io_thread_client_impl.h" | 5 #include "android_webview/native/aw_contents_io_thread_client_impl.h" |
6 | 6 |
7 #include <map> | 7 #include <map> |
| 8 #include <memory> |
8 #include <utility> | 9 #include <utility> |
9 | 10 |
10 #include "android_webview/common/devtools_instrumentation.h" | 11 #include "android_webview/common/devtools_instrumentation.h" |
11 #include "android_webview/native/aw_contents_background_thread_client.h" | 12 #include "android_webview/native/aw_contents_background_thread_client.h" |
12 #include "android_webview/native/aw_web_resource_response_impl.h" | 13 #include "android_webview/native/aw_web_resource_response_impl.h" |
13 #include "base/android/jni_array.h" | 14 #include "base/android/jni_array.h" |
14 #include "base/android/jni_string.h" | 15 #include "base/android/jni_string.h" |
15 #include "base/android/jni_weak_ref.h" | 16 #include "base/android/jni_weak_ref.h" |
16 #include "base/lazy_instance.h" | 17 #include "base/lazy_instance.h" |
17 #include "base/memory/scoped_ptr.h" | |
18 #include "base/synchronization/lock.h" | 18 #include "base/synchronization/lock.h" |
19 #include "content/public/browser/browser_thread.h" | 19 #include "content/public/browser/browser_thread.h" |
20 #include "content/public/browser/render_frame_host.h" | 20 #include "content/public/browser/render_frame_host.h" |
21 #include "content/public/browser/render_process_host.h" | 21 #include "content/public/browser/render_process_host.h" |
22 #include "content/public/browser/render_view_host.h" | 22 #include "content/public/browser/render_view_host.h" |
23 #include "content/public/browser/resource_request_info.h" | 23 #include "content/public/browser/resource_request_info.h" |
24 #include "content/public/browser/web_contents.h" | 24 #include "content/public/browser/web_contents.h" |
25 #include "content/public/browser/web_contents_observer.h" | 25 #include "content/public/browser/web_contents_observer.h" |
26 #include "jni/AwContentsIoThreadClient_jni.h" | 26 #include "jni/AwContentsIoThreadClient_jni.h" |
27 #include "net/base/net_errors.h" | 27 #include "net/base/net_errors.h" |
(...skipping 173 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
201 jstringArray_header_names = ToJavaArrayOfStrings(env, header_names); | 201 jstringArray_header_names = ToJavaArrayOfStrings(env, header_names); |
202 jstringArray_header_values = ToJavaArrayOfStrings(env, header_values); | 202 jstringArray_header_values = ToJavaArrayOfStrings(env, header_values); |
203 } | 203 } |
204 }; | 204 }; |
205 | 205 |
206 } // namespace | 206 } // namespace |
207 | 207 |
208 // AwContentsIoThreadClientImpl ----------------------------------------------- | 208 // AwContentsIoThreadClientImpl ----------------------------------------------- |
209 | 209 |
210 // static | 210 // static |
211 scoped_ptr<AwContentsIoThreadClient> | 211 std::unique_ptr<AwContentsIoThreadClient> AwContentsIoThreadClient::FromID( |
212 AwContentsIoThreadClient::FromID(int render_process_id, int render_frame_id) { | 212 int render_process_id, |
| 213 int render_frame_id) { |
213 pair<int, int> rfh_id(render_process_id, render_frame_id); | 214 pair<int, int> rfh_id(render_process_id, render_frame_id); |
214 IoThreadClientData client_data; | 215 IoThreadClientData client_data; |
215 if (!RfhToIoThreadClientMap::GetInstance()->Get(rfh_id, &client_data)) | 216 if (!RfhToIoThreadClientMap::GetInstance()->Get(rfh_id, &client_data)) |
216 return scoped_ptr<AwContentsIoThreadClient>(); | 217 return std::unique_ptr<AwContentsIoThreadClient>(); |
217 | 218 |
218 JNIEnv* env = AttachCurrentThread(); | 219 JNIEnv* env = AttachCurrentThread(); |
219 ScopedJavaLocalRef<jobject> java_delegate = | 220 ScopedJavaLocalRef<jobject> java_delegate = |
220 client_data.io_thread_client.get(env); | 221 client_data.io_thread_client.get(env); |
221 DCHECK(!client_data.pending_association || java_delegate.is_null()); | 222 DCHECK(!client_data.pending_association || java_delegate.is_null()); |
222 return scoped_ptr<AwContentsIoThreadClient>(new AwContentsIoThreadClientImpl( | 223 return std::unique_ptr<AwContentsIoThreadClient>( |
223 client_data.pending_association, java_delegate)); | 224 new AwContentsIoThreadClientImpl(client_data.pending_association, |
| 225 java_delegate)); |
224 } | 226 } |
225 | 227 |
226 // static | 228 // static |
227 void AwContentsIoThreadClient::SubFrameCreated(int render_process_id, | 229 void AwContentsIoThreadClient::SubFrameCreated(int render_process_id, |
228 int parent_render_frame_id, | 230 int parent_render_frame_id, |
229 int child_render_frame_id) { | 231 int child_render_frame_id) { |
230 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); |
231 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); |
232 IoThreadClientData client_data; | 234 IoThreadClientData client_data; |
233 if (!RfhToIoThreadClientMap::GetInstance()->Get(parent_rfh_id, | 235 if (!RfhToIoThreadClientMap::GetInstance()->Get(parent_rfh_id, |
(...skipping 28 matching lines...) Expand all Loading... |
262 const base::android::JavaRef<jobject>& jclient, | 264 const base::android::JavaRef<jobject>& jclient, |
263 const base::android::JavaRef<jobject>& browser_context) { | 265 const base::android::JavaRef<jobject>& browser_context) { |
264 // TODO: currently there is only one browser context so it is ok to | 266 // TODO: currently there is only one browser context so it is ok to |
265 // store in a global variable, in the future use browser_context to | 267 // store in a global variable, in the future use browser_context to |
266 // obtain the correct instance. | 268 // obtain the correct instance. |
267 JavaObjectWeakGlobalRef temp(AttachCurrentThread(), jclient.obj()); | 269 JavaObjectWeakGlobalRef temp(AttachCurrentThread(), jclient.obj()); |
268 g_sw_instance_.Get() = temp; | 270 g_sw_instance_.Get() = temp; |
269 } | 271 } |
270 | 272 |
271 // static | 273 // static |
272 scoped_ptr<AwContentsIoThreadClient> | 274 std::unique_ptr<AwContentsIoThreadClient> |
273 AwContentsIoThreadClient::GetServiceWorkerIoThreadClient() { | 275 AwContentsIoThreadClient::GetServiceWorkerIoThreadClient() { |
274 if (g_sw_instance_.Get().is_empty()) | 276 if (g_sw_instance_.Get().is_empty()) |
275 return scoped_ptr<AwContentsIoThreadClient>(); | 277 return std::unique_ptr<AwContentsIoThreadClient>(); |
276 | 278 |
277 JNIEnv* env = AttachCurrentThread(); | 279 JNIEnv* env = AttachCurrentThread(); |
278 ScopedJavaLocalRef<jobject> java_delegate = g_sw_instance_.Get().get(env); | 280 ScopedJavaLocalRef<jobject> java_delegate = g_sw_instance_.Get().get(env); |
279 | 281 |
280 DCHECK(!java_delegate.is_null()); | 282 DCHECK(!java_delegate.is_null()); |
281 return scoped_ptr<AwContentsIoThreadClient>(new AwContentsIoThreadClientImpl( | 283 return std::unique_ptr<AwContentsIoThreadClient>( |
282 false, java_delegate)); | 284 new AwContentsIoThreadClientImpl(false, java_delegate)); |
283 } | 285 } |
284 | 286 |
285 AwContentsIoThreadClientImpl::AwContentsIoThreadClientImpl( | 287 AwContentsIoThreadClientImpl::AwContentsIoThreadClientImpl( |
286 bool pending_association, | 288 bool pending_association, |
287 const JavaRef<jobject>& obj) | 289 const JavaRef<jobject>& obj) |
288 : pending_association_(pending_association), | 290 : pending_association_(pending_association), |
289 java_object_(obj) { | 291 java_object_(obj) { |
290 } | 292 } |
291 | 293 |
292 AwContentsIoThreadClientImpl::~AwContentsIoThreadClientImpl() { | 294 AwContentsIoThreadClientImpl::~AwContentsIoThreadClientImpl() { |
(...skipping 12 matching lines...) Expand all Loading... |
305 | 307 |
306 JNIEnv* env = AttachCurrentThread(); | 308 JNIEnv* env = AttachCurrentThread(); |
307 return static_cast<AwContentsIoThreadClient::CacheMode>( | 309 return static_cast<AwContentsIoThreadClient::CacheMode>( |
308 Java_AwContentsIoThreadClient_getCacheMode( | 310 Java_AwContentsIoThreadClient_getCacheMode( |
309 env, java_object_.obj())); | 311 env, java_object_.obj())); |
310 } | 312 } |
311 | 313 |
312 | 314 |
313 namespace { | 315 namespace { |
314 | 316 |
315 scoped_ptr<AwWebResourceResponse> RunShouldInterceptRequest( | 317 std::unique_ptr<AwWebResourceResponse> RunShouldInterceptRequest( |
316 WebResourceRequest web_request, | 318 WebResourceRequest web_request, |
317 JavaObjectWeakGlobalRef ref) { | 319 JavaObjectWeakGlobalRef ref) { |
318 DCHECK_CURRENTLY_ON(BrowserThread::FILE); | 320 DCHECK_CURRENTLY_ON(BrowserThread::FILE); |
319 JNIEnv* env = AttachCurrentThread(); | 321 JNIEnv* env = AttachCurrentThread(); |
320 base::android::ScopedJavaLocalRef<jobject> obj = ref.get(env); | 322 base::android::ScopedJavaLocalRef<jobject> obj = ref.get(env); |
321 if (obj.is_null()) | 323 if (obj.is_null()) |
322 return nullptr; | 324 return nullptr; |
323 | 325 |
324 web_request.ConvertToJava(env); | 326 web_request.ConvertToJava(env); |
325 | 327 |
326 devtools_instrumentation::ScopedEmbedderCallbackTask embedder_callback( | 328 devtools_instrumentation::ScopedEmbedderCallbackTask embedder_callback( |
327 "shouldInterceptRequest"); | 329 "shouldInterceptRequest"); |
328 ScopedJavaLocalRef<jobject> ret = | 330 ScopedJavaLocalRef<jobject> ret = |
329 AwContentsBackgroundThreadClient::shouldInterceptRequest( | 331 AwContentsBackgroundThreadClient::shouldInterceptRequest( |
330 env, | 332 env, |
331 obj.obj(), | 333 obj.obj(), |
332 web_request.jstring_url.obj(), | 334 web_request.jstring_url.obj(), |
333 web_request.is_main_frame, | 335 web_request.is_main_frame, |
334 web_request.has_user_gesture, | 336 web_request.has_user_gesture, |
335 web_request.jstring_method.obj(), | 337 web_request.jstring_method.obj(), |
336 web_request.jstringArray_header_names.obj(), | 338 web_request.jstringArray_header_names.obj(), |
337 web_request.jstringArray_header_values.obj()); | 339 web_request.jstringArray_header_values.obj()); |
338 return scoped_ptr<AwWebResourceResponse>( | 340 return std::unique_ptr<AwWebResourceResponse>( |
339 ret.is_null() ? nullptr : new AwWebResourceResponseImpl(ret)); | 341 ret.is_null() ? nullptr : new AwWebResourceResponseImpl(ret)); |
340 } | 342 } |
341 | 343 |
342 scoped_ptr<AwWebResourceResponse> ReturnNull() { | 344 std::unique_ptr<AwWebResourceResponse> ReturnNull() { |
343 return scoped_ptr<AwWebResourceResponse>(); | 345 return std::unique_ptr<AwWebResourceResponse>(); |
344 } | 346 } |
345 | 347 |
346 } // namespace | 348 } // namespace |
347 | 349 |
348 void AwContentsIoThreadClientImpl::ShouldInterceptRequestAsync( | 350 void AwContentsIoThreadClientImpl::ShouldInterceptRequestAsync( |
349 const net::URLRequest* request, | 351 const net::URLRequest* request, |
350 const ShouldInterceptRequestResultCallback callback) { | 352 const ShouldInterceptRequestResultCallback callback) { |
351 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 353 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
352 base::Callback<scoped_ptr<AwWebResourceResponse>()> get_response = | 354 base::Callback<std::unique_ptr<AwWebResourceResponse>()> get_response = |
353 base::Bind(&ReturnNull); | 355 base::Bind(&ReturnNull); |
354 JNIEnv* env = AttachCurrentThread(); | 356 JNIEnv* env = AttachCurrentThread(); |
355 if (bg_thread_client_object_.is_null() && !java_object_.is_null()) { | 357 if (bg_thread_client_object_.is_null() && !java_object_.is_null()) { |
356 bg_thread_client_object_.Reset( | 358 bg_thread_client_object_.Reset( |
357 Java_AwContentsIoThreadClient_getBackgroundThreadClient( | 359 Java_AwContentsIoThreadClient_getBackgroundThreadClient( |
358 env, java_object_.obj())); | 360 env, java_object_.obj())); |
359 } | 361 } |
360 if (!bg_thread_client_object_.is_null()) { | 362 if (!bg_thread_client_object_.is_null()) { |
361 get_response = base::Bind( | 363 get_response = base::Bind( |
362 &RunShouldInterceptRequest, WebResourceRequest(request), | 364 &RunShouldInterceptRequest, WebResourceRequest(request), |
(...skipping 169 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
532 jstring_reason.obj(), | 534 jstring_reason.obj(), |
533 jstringArray_response_header_names.obj(), | 535 jstringArray_response_header_names.obj(), |
534 jstringArray_response_header_values.obj()); | 536 jstringArray_response_header_values.obj()); |
535 } | 537 } |
536 | 538 |
537 bool RegisterAwContentsIoThreadClientImpl(JNIEnv* env) { | 539 bool RegisterAwContentsIoThreadClientImpl(JNIEnv* env) { |
538 return RegisterNativesImpl(env); | 540 return RegisterNativesImpl(env); |
539 } | 541 } |
540 | 542 |
541 } // namespace android_webview | 543 } // namespace android_webview |
OLD | NEW |