| 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 <utility> | 8 #include <utility> |
| 9 | 9 |
| 10 #include "android_webview/common/devtools_instrumentation.h" | 10 #include "android_webview/common/devtools_instrumentation.h" |
| 11 #include "android_webview/native/intercepted_request_data_impl.h" | 11 #include "android_webview/native/aw_web_resource_response_impl.h" |
| 12 #include "base/android/jni_array.h" |
| 12 #include "base/android/jni_string.h" | 13 #include "base/android/jni_string.h" |
| 13 #include "base/android/jni_weak_ref.h" | 14 #include "base/android/jni_weak_ref.h" |
| 14 #include "base/lazy_instance.h" | 15 #include "base/lazy_instance.h" |
| 15 #include "base/memory/linked_ptr.h" | 16 #include "base/memory/linked_ptr.h" |
| 16 #include "base/memory/scoped_ptr.h" | 17 #include "base/memory/scoped_ptr.h" |
| 17 #include "base/synchronization/lock.h" | 18 #include "base/synchronization/lock.h" |
| 18 #include "content/public/browser/browser_thread.h" | 19 #include "content/public/browser/browser_thread.h" |
| 19 #include "content/public/browser/render_frame_host.h" | 20 #include "content/public/browser/render_frame_host.h" |
| 20 #include "content/public/browser/render_process_host.h" | 21 #include "content/public/browser/render_process_host.h" |
| 21 #include "content/public/browser/render_view_host.h" | 22 #include "content/public/browser/render_view_host.h" |
| 22 #include "content/public/browser/resource_request_info.h" | 23 #include "content/public/browser/resource_request_info.h" |
| 23 #include "content/public/browser/web_contents.h" | 24 #include "content/public/browser/web_contents.h" |
| 24 #include "content/public/browser/web_contents_observer.h" | 25 #include "content/public/browser/web_contents_observer.h" |
| 25 #include "jni/AwContentsIoThreadClient_jni.h" | 26 #include "jni/AwContentsIoThreadClient_jni.h" |
| 27 #include "net/http/http_request_headers.h" |
| 26 #include "net/url_request/url_request.h" | 28 #include "net/url_request/url_request.h" |
| 27 #include "url/gurl.h" | 29 #include "url/gurl.h" |
| 28 | 30 |
| 29 using base::android::AttachCurrentThread; | 31 using base::android::AttachCurrentThread; |
| 30 using base::android::ConvertUTF8ToJavaString; | 32 using base::android::ConvertUTF8ToJavaString; |
| 31 using base::android::JavaRef; | 33 using base::android::JavaRef; |
| 32 using base::android::ScopedJavaLocalRef; | 34 using base::android::ScopedJavaLocalRef; |
| 35 using base::android::ToJavaArrayOfStrings; |
| 33 using base::LazyInstance; | 36 using base::LazyInstance; |
| 34 using content::BrowserThread; | 37 using content::BrowserThread; |
| 35 using content::RenderFrameHost; | 38 using content::RenderFrameHost; |
| 36 using content::WebContents; | 39 using content::WebContents; |
| 37 using std::map; | 40 using std::map; |
| 38 using std::pair; | 41 using std::pair; |
| 42 using std::string; |
| 43 using std::vector; |
| 39 | 44 |
| 40 namespace android_webview { | 45 namespace android_webview { |
| 41 | 46 |
| 42 namespace { | 47 namespace { |
| 43 | 48 |
| 44 struct IoThreadClientData { | 49 struct IoThreadClientData { |
| 45 bool pending_association; | 50 bool pending_association; |
| 46 JavaObjectWeakGlobalRef io_thread_client; | 51 JavaObjectWeakGlobalRef io_thread_client; |
| 47 | 52 |
| 48 IoThreadClientData(); | 53 IoThreadClientData(); |
| (...skipping 171 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 220 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 225 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| 221 if (java_object_.is_null()) | 226 if (java_object_.is_null()) |
| 222 return AwContentsIoThreadClient::LOAD_DEFAULT; | 227 return AwContentsIoThreadClient::LOAD_DEFAULT; |
| 223 | 228 |
| 224 JNIEnv* env = AttachCurrentThread(); | 229 JNIEnv* env = AttachCurrentThread(); |
| 225 return static_cast<AwContentsIoThreadClient::CacheMode>( | 230 return static_cast<AwContentsIoThreadClient::CacheMode>( |
| 226 Java_AwContentsIoThreadClient_getCacheMode( | 231 Java_AwContentsIoThreadClient_getCacheMode( |
| 227 env, java_object_.obj())); | 232 env, java_object_.obj())); |
| 228 } | 233 } |
| 229 | 234 |
| 230 scoped_ptr<InterceptedRequestData> | 235 scoped_ptr<AwWebResourceResponse> |
| 231 AwContentsIoThreadClientImpl::ShouldInterceptRequest( | 236 AwContentsIoThreadClientImpl::ShouldInterceptRequest( |
| 232 const GURL& location, | 237 const GURL& location, |
| 233 const net::URLRequest* request) { | 238 const net::URLRequest* request) { |
| 234 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 239 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| 235 if (java_object_.is_null()) | 240 if (java_object_.is_null()) |
| 236 return scoped_ptr<InterceptedRequestData>(); | 241 return scoped_ptr<AwWebResourceResponse>(); |
| 237 const content::ResourceRequestInfo* info = | 242 const content::ResourceRequestInfo* info = |
| 238 content::ResourceRequestInfo::ForRequest(request); | 243 content::ResourceRequestInfo::ForRequest(request); |
| 239 bool is_main_frame = info && | 244 bool is_main_frame = info && |
| 240 info->GetResourceType() == ResourceType::MAIN_FRAME; | 245 info->GetResourceType() == ResourceType::MAIN_FRAME; |
| 246 bool has_user_gesture = info && info->HasUserGesture(); |
| 247 |
| 248 vector<string> headers_names; |
| 249 vector<string> headers_values; |
| 250 { |
| 251 net::HttpRequestHeaders headers; |
| 252 if (!request->GetFullRequestHeaders(&headers)) |
| 253 headers = request->extra_request_headers(); |
| 254 net::HttpRequestHeaders::Iterator headers_iterator(headers); |
| 255 while (headers_iterator.GetNext()) { |
| 256 headers_names.push_back(headers_iterator.name()); |
| 257 headers_values.push_back(headers_iterator.value()); |
| 258 } |
| 259 } |
| 241 | 260 |
| 242 JNIEnv* env = AttachCurrentThread(); | 261 JNIEnv* env = AttachCurrentThread(); |
| 243 ScopedJavaLocalRef<jstring> jstring_url = | 262 ScopedJavaLocalRef<jstring> jstring_url = |
| 244 ConvertUTF8ToJavaString(env, location.spec()); | 263 ConvertUTF8ToJavaString(env, location.spec()); |
| 264 ScopedJavaLocalRef<jstring> jstring_method = |
| 265 ConvertUTF8ToJavaString(env, request->method()); |
| 266 ScopedJavaLocalRef<jobjectArray> jstringArray_headers_names = |
| 267 ToJavaArrayOfStrings(env, headers_names); |
| 268 ScopedJavaLocalRef<jobjectArray> jstringArray_headers_values = |
| 269 ToJavaArrayOfStrings(env, headers_values); |
| 245 devtools_instrumentation::ScopedEmbedderCallbackTask embedder_callback( | 270 devtools_instrumentation::ScopedEmbedderCallbackTask embedder_callback( |
| 246 "shouldInterceptRequest"); | 271 "shouldInterceptRequest"); |
| 247 ScopedJavaLocalRef<jobject> ret = | 272 ScopedJavaLocalRef<jobject> ret = |
| 248 Java_AwContentsIoThreadClient_shouldInterceptRequest( | 273 Java_AwContentsIoThreadClient_shouldInterceptRequest( |
| 249 env, java_object_.obj(), jstring_url.obj(), is_main_frame); | 274 env, |
| 275 java_object_.obj(), |
| 276 jstring_url.obj(), |
| 277 is_main_frame, |
| 278 has_user_gesture, |
| 279 jstring_method.obj(), |
| 280 jstringArray_headers_names.obj(), |
| 281 jstringArray_headers_values.obj()); |
| 250 if (ret.is_null()) | 282 if (ret.is_null()) |
| 251 return scoped_ptr<InterceptedRequestData>(); | 283 return scoped_ptr<AwWebResourceResponse>(); |
| 252 return scoped_ptr<InterceptedRequestData>( | 284 return scoped_ptr<AwWebResourceResponse>( |
| 253 new InterceptedRequestDataImpl(ret)); | 285 new AwWebResourceResponseImpl(ret)); |
| 254 } | 286 } |
| 255 | 287 |
| 256 bool AwContentsIoThreadClientImpl::ShouldBlockContentUrls() const { | 288 bool AwContentsIoThreadClientImpl::ShouldBlockContentUrls() const { |
| 257 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 289 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| 258 if (java_object_.is_null()) | 290 if (java_object_.is_null()) |
| 259 return false; | 291 return false; |
| 260 | 292 |
| 261 JNIEnv* env = AttachCurrentThread(); | 293 JNIEnv* env = AttachCurrentThread(); |
| 262 return Java_AwContentsIoThreadClient_shouldBlockContentUrls( | 294 return Java_AwContentsIoThreadClient_shouldBlockContentUrls( |
| 263 env, java_object_.obj()); | 295 env, java_object_.obj()); |
| (...skipping 24 matching lines...) Expand all Loading... |
| 288 if (java_object_.is_null()) | 320 if (java_object_.is_null()) |
| 289 return false; | 321 return false; |
| 290 | 322 |
| 291 JNIEnv* env = AttachCurrentThread(); | 323 JNIEnv* env = AttachCurrentThread(); |
| 292 return Java_AwContentsIoThreadClient_shouldBlockNetworkLoads( | 324 return Java_AwContentsIoThreadClient_shouldBlockNetworkLoads( |
| 293 env, java_object_.obj()); | 325 env, java_object_.obj()); |
| 294 } | 326 } |
| 295 | 327 |
| 296 void AwContentsIoThreadClientImpl::NewDownload( | 328 void AwContentsIoThreadClientImpl::NewDownload( |
| 297 const GURL& url, | 329 const GURL& url, |
| 298 const std::string& user_agent, | 330 const string& user_agent, |
| 299 const std::string& content_disposition, | 331 const string& content_disposition, |
| 300 const std::string& mime_type, | 332 const string& mime_type, |
| 301 int64 content_length) { | 333 int64 content_length) { |
| 302 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 334 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| 303 if (java_object_.is_null()) | 335 if (java_object_.is_null()) |
| 304 return; | 336 return; |
| 305 | 337 |
| 306 JNIEnv* env = AttachCurrentThread(); | 338 JNIEnv* env = AttachCurrentThread(); |
| 307 ScopedJavaLocalRef<jstring> jstring_url = | 339 ScopedJavaLocalRef<jstring> jstring_url = |
| 308 ConvertUTF8ToJavaString(env, url.spec()); | 340 ConvertUTF8ToJavaString(env, url.spec()); |
| 309 ScopedJavaLocalRef<jstring> jstring_user_agent = | 341 ScopedJavaLocalRef<jstring> jstring_user_agent = |
| 310 ConvertUTF8ToJavaString(env, user_agent); | 342 ConvertUTF8ToJavaString(env, user_agent); |
| 311 ScopedJavaLocalRef<jstring> jstring_content_disposition = | 343 ScopedJavaLocalRef<jstring> jstring_content_disposition = |
| 312 ConvertUTF8ToJavaString(env, content_disposition); | 344 ConvertUTF8ToJavaString(env, content_disposition); |
| 313 ScopedJavaLocalRef<jstring> jstring_mime_type = | 345 ScopedJavaLocalRef<jstring> jstring_mime_type = |
| 314 ConvertUTF8ToJavaString(env, mime_type); | 346 ConvertUTF8ToJavaString(env, mime_type); |
| 315 | 347 |
| 316 Java_AwContentsIoThreadClient_onDownloadStart( | 348 Java_AwContentsIoThreadClient_onDownloadStart( |
| 317 env, | 349 env, |
| 318 java_object_.obj(), | 350 java_object_.obj(), |
| 319 jstring_url.obj(), | 351 jstring_url.obj(), |
| 320 jstring_user_agent.obj(), | 352 jstring_user_agent.obj(), |
| 321 jstring_content_disposition.obj(), | 353 jstring_content_disposition.obj(), |
| 322 jstring_mime_type.obj(), | 354 jstring_mime_type.obj(), |
| 323 content_length); | 355 content_length); |
| 324 } | 356 } |
| 325 | 357 |
| 326 void AwContentsIoThreadClientImpl::NewLoginRequest(const std::string& realm, | 358 void AwContentsIoThreadClientImpl::NewLoginRequest(const string& realm, |
| 327 const std::string& account, | 359 const string& account, |
| 328 const std::string& args) { | 360 const string& args) { |
| 329 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 361 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| 330 if (java_object_.is_null()) | 362 if (java_object_.is_null()) |
| 331 return; | 363 return; |
| 332 | 364 |
| 333 JNIEnv* env = AttachCurrentThread(); | 365 JNIEnv* env = AttachCurrentThread(); |
| 334 ScopedJavaLocalRef<jstring> jrealm = ConvertUTF8ToJavaString(env, realm); | 366 ScopedJavaLocalRef<jstring> jrealm = ConvertUTF8ToJavaString(env, realm); |
| 335 ScopedJavaLocalRef<jstring> jargs = ConvertUTF8ToJavaString(env, args); | 367 ScopedJavaLocalRef<jstring> jargs = ConvertUTF8ToJavaString(env, args); |
| 336 | 368 |
| 337 ScopedJavaLocalRef<jstring> jaccount; | 369 ScopedJavaLocalRef<jstring> jaccount; |
| 338 if (!account.empty()) | 370 if (!account.empty()) |
| 339 jaccount = ConvertUTF8ToJavaString(env, account); | 371 jaccount = ConvertUTF8ToJavaString(env, account); |
| 340 | 372 |
| 341 Java_AwContentsIoThreadClient_newLoginRequest( | 373 Java_AwContentsIoThreadClient_newLoginRequest( |
| 342 env, java_object_.obj(), jrealm.obj(), jaccount.obj(), jargs.obj()); | 374 env, java_object_.obj(), jrealm.obj(), jaccount.obj(), jargs.obj()); |
| 343 } | 375 } |
| 344 | 376 |
| 345 bool RegisterAwContentsIoThreadClientImpl(JNIEnv* env) { | 377 bool RegisterAwContentsIoThreadClientImpl(JNIEnv* env) { |
| 346 return RegisterNativesImpl(env); | 378 return RegisterNativesImpl(env); |
| 347 } | 379 } |
| 348 | 380 |
| 349 } // namespace android_webview | 381 } // namespace android_webview |
| OLD | NEW |