| 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/aw_contents_background_thread_client.h" |
| 11 #include "android_webview/native/aw_web_resource_response_impl.h" | 12 #include "android_webview/native/aw_web_resource_response_impl.h" |
| 12 #include "base/android/jni_array.h" | 13 #include "base/android/jni_array.h" |
| 13 #include "base/android/jni_string.h" | 14 #include "base/android/jni_string.h" |
| 14 #include "base/android/jni_weak_ref.h" | 15 #include "base/android/jni_weak_ref.h" |
| 15 #include "base/lazy_instance.h" | 16 #include "base/lazy_instance.h" |
| 16 #include "base/memory/linked_ptr.h" | 17 #include "base/memory/linked_ptr.h" |
| 17 #include "base/memory/scoped_ptr.h" | 18 #include "base/memory/scoped_ptr.h" |
| 18 #include "base/synchronization/lock.h" | 19 #include "base/synchronization/lock.h" |
| 19 #include "content/public/browser/browser_thread.h" | 20 #include "content/public/browser/browser_thread.h" |
| 20 #include "content/public/browser/render_frame_host.h" | 21 #include "content/public/browser/render_frame_host.h" |
| (...skipping 127 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 148 | 149 |
| 149 void ClientMapEntryUpdater::RenderFrameDeleted(RenderFrameHost* rfh) { | 150 void ClientMapEntryUpdater::RenderFrameDeleted(RenderFrameHost* rfh) { |
| 150 RfhToIoThreadClientMap::GetInstance()->Erase(GetRenderFrameHostIdPair(rfh)); | 151 RfhToIoThreadClientMap::GetInstance()->Erase(GetRenderFrameHostIdPair(rfh)); |
| 151 } | 152 } |
| 152 | 153 |
| 153 void ClientMapEntryUpdater::WebContentsDestroyed() { | 154 void ClientMapEntryUpdater::WebContentsDestroyed() { |
| 154 delete this; | 155 delete this; |
| 155 } | 156 } |
| 156 | 157 |
| 157 struct WebResourceRequest { | 158 struct WebResourceRequest { |
| 158 ScopedJavaLocalRef<jstring> jstring_url; | 159 std::string url; |
| 160 std::string method; |
| 159 bool is_main_frame; | 161 bool is_main_frame; |
| 160 bool has_user_gesture; | 162 bool has_user_gesture; |
| 163 vector<string> header_names; |
| 164 vector<string> header_values; |
| 165 |
| 166 ScopedJavaLocalRef<jstring> jstring_url; |
| 161 ScopedJavaLocalRef<jstring> jstring_method; | 167 ScopedJavaLocalRef<jstring> jstring_method; |
| 162 ScopedJavaLocalRef<jobjectArray> jstringArray_header_names; | 168 ScopedJavaLocalRef<jobjectArray> jstringArray_header_names; |
| 163 ScopedJavaLocalRef<jobjectArray> jstringArray_header_values; | 169 ScopedJavaLocalRef<jobjectArray> jstringArray_header_values; |
| 164 | 170 |
| 165 WebResourceRequest(JNIEnv* env, const net::URLRequest* request) | 171 WebResourceRequest(const net::URLRequest* request) |
| 166 : jstring_url(ConvertUTF8ToJavaString(env, request->url().spec())), | 172 : url(request->url().spec()), |
| 167 jstring_method(ConvertUTF8ToJavaString(env, request->method())) { | 173 method(request->method()) { |
| 168 const content::ResourceRequestInfo* info = | 174 const content::ResourceRequestInfo* info = |
| 169 content::ResourceRequestInfo::ForRequest(request); | 175 content::ResourceRequestInfo::ForRequest(request); |
| 170 is_main_frame = | 176 is_main_frame = |
| 171 info && info->GetResourceType() == content::RESOURCE_TYPE_MAIN_FRAME; | 177 info && info->GetResourceType() == content::RESOURCE_TYPE_MAIN_FRAME; |
| 172 has_user_gesture = info && info->HasUserGesture(); | 178 has_user_gesture = info && info->HasUserGesture(); |
| 173 | 179 |
| 174 vector<string> header_names; | |
| 175 vector<string> header_values; | |
| 176 net::HttpRequestHeaders headers; | 180 net::HttpRequestHeaders headers; |
| 177 if (!request->GetFullRequestHeaders(&headers)) | 181 if (!request->GetFullRequestHeaders(&headers)) |
| 178 headers = request->extra_request_headers(); | 182 headers = request->extra_request_headers(); |
| 179 net::HttpRequestHeaders::Iterator headers_iterator(headers); | 183 net::HttpRequestHeaders::Iterator headers_iterator(headers); |
| 180 while (headers_iterator.GetNext()) { | 184 while (headers_iterator.GetNext()) { |
| 181 header_names.push_back(headers_iterator.name()); | 185 header_names.push_back(headers_iterator.name()); |
| 182 header_values.push_back(headers_iterator.value()); | 186 header_values.push_back(headers_iterator.value()); |
| 183 } | 187 } |
| 188 } |
| 189 |
| 190 WebResourceRequest(JNIEnv* env, const net::URLRequest* request) |
| 191 : WebResourceRequest(request) { |
| 192 ConvertToJava(env); |
| 193 } |
| 194 |
| 195 void ConvertToJava(JNIEnv* env) { |
| 196 jstring_url = ConvertUTF8ToJavaString(env, url); |
| 197 jstring_method = ConvertUTF8ToJavaString(env, method); |
| 184 jstringArray_header_names = ToJavaArrayOfStrings(env, header_names); | 198 jstringArray_header_names = ToJavaArrayOfStrings(env, header_names); |
| 185 jstringArray_header_values = ToJavaArrayOfStrings(env, header_values); | 199 jstringArray_header_values = ToJavaArrayOfStrings(env, header_values); |
| 186 } | 200 } |
| 187 }; | 201 }; |
| 188 | 202 |
| 189 } // namespace | 203 } // namespace |
| 190 | 204 |
| 191 // AwContentsIoThreadClientImpl ----------------------------------------------- | 205 // AwContentsIoThreadClientImpl ----------------------------------------------- |
| 192 | 206 |
| 193 // static | 207 // static |
| (...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 260 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 274 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 261 if (java_object_.is_null()) | 275 if (java_object_.is_null()) |
| 262 return AwContentsIoThreadClient::LOAD_DEFAULT; | 276 return AwContentsIoThreadClient::LOAD_DEFAULT; |
| 263 | 277 |
| 264 JNIEnv* env = AttachCurrentThread(); | 278 JNIEnv* env = AttachCurrentThread(); |
| 265 return static_cast<AwContentsIoThreadClient::CacheMode>( | 279 return static_cast<AwContentsIoThreadClient::CacheMode>( |
| 266 Java_AwContentsIoThreadClient_getCacheMode( | 280 Java_AwContentsIoThreadClient_getCacheMode( |
| 267 env, java_object_.obj())); | 281 env, java_object_.obj())); |
| 268 } | 282 } |
| 269 | 283 |
| 270 scoped_ptr<AwWebResourceResponse> | |
| 271 AwContentsIoThreadClientImpl::ShouldInterceptRequest( | |
| 272 const net::URLRequest* request) { | |
| 273 DCHECK_CURRENTLY_ON(BrowserThread::IO); | |
| 274 if (java_object_.is_null()) | |
| 275 return scoped_ptr<AwWebResourceResponse>(); | |
| 276 | 284 |
| 285 namespace { |
| 286 |
| 287 scoped_ptr<AwWebResourceResponse> RunShouldInterceptRequest( |
| 288 WebResourceRequest web_request, |
| 289 JavaObjectWeakGlobalRef ref) { |
| 290 DCHECK_CURRENTLY_ON(BrowserThread::FILE); |
| 277 JNIEnv* env = AttachCurrentThread(); | 291 JNIEnv* env = AttachCurrentThread(); |
| 278 WebResourceRequest web_request(env, request); | 292 base::android::ScopedJavaLocalRef<jobject> obj = ref.get(env); |
| 293 if (obj.is_null()) |
| 294 return nullptr; |
| 295 |
| 296 web_request.ConvertToJava(env); |
| 279 | 297 |
| 280 devtools_instrumentation::ScopedEmbedderCallbackTask embedder_callback( | 298 devtools_instrumentation::ScopedEmbedderCallbackTask embedder_callback( |
| 281 "shouldInterceptRequest"); | 299 "shouldInterceptRequest"); |
| 282 ScopedJavaLocalRef<jobject> ret = | 300 ScopedJavaLocalRef<jobject> ret = |
| 283 Java_AwContentsIoThreadClient_shouldInterceptRequest( | 301 AwContentsBackgroundThreadClient::shouldInterceptRequest( |
| 284 env, | 302 env, |
| 285 java_object_.obj(), | 303 obj.obj(), |
| 286 web_request.jstring_url.obj(), | 304 web_request.jstring_url.obj(), |
| 287 web_request.is_main_frame, | 305 web_request.is_main_frame, |
| 288 web_request.has_user_gesture, | 306 web_request.has_user_gesture, |
| 289 web_request.jstring_method.obj(), | 307 web_request.jstring_method.obj(), |
| 290 web_request.jstringArray_header_names.obj(), | 308 web_request.jstringArray_header_names.obj(), |
| 291 web_request.jstringArray_header_values.obj()); | 309 web_request.jstringArray_header_values.obj()); |
| 292 if (ret.is_null()) | |
| 293 return scoped_ptr<AwWebResourceResponse>(); | |
| 294 return scoped_ptr<AwWebResourceResponse>( | 310 return scoped_ptr<AwWebResourceResponse>( |
| 295 new AwWebResourceResponseImpl(ret)); | 311 ret.is_null() ? nullptr : new AwWebResourceResponseImpl(ret)).Pass(); |
| 312 } |
| 313 |
| 314 scoped_ptr<AwWebResourceResponse> ReturnNull() { |
| 315 return scoped_ptr<AwWebResourceResponse>(); |
| 316 } |
| 317 |
| 318 } // namespace |
| 319 |
| 320 void AwContentsIoThreadClientImpl::ShouldInterceptRequestAsync( |
| 321 const net::URLRequest* request, |
| 322 const ShouldInterceptRequestResultCallback callback) { |
| 323 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 324 base::Callback<scoped_ptr<AwWebResourceResponse>()> get_response = |
| 325 base::Bind(&ReturnNull); |
| 326 JNIEnv* env = AttachCurrentThread(); |
| 327 if (bg_thread_client_object_.is_null() && !java_object_.is_null()) { |
| 328 bg_thread_client_object_.Reset( |
| 329 Java_AwContentsIoThreadClient_getBackgroundThreadClient( |
| 330 env, java_object_.obj())); |
| 331 } |
| 332 if (!bg_thread_client_object_.is_null()) { |
| 333 get_response = base::Bind( |
| 334 &RunShouldInterceptRequest, WebResourceRequest(request), |
| 335 JavaObjectWeakGlobalRef(env, bg_thread_client_object_.obj())); |
| 336 } |
| 337 BrowserThread::PostTaskAndReplyWithResult(BrowserThread::FILE, FROM_HERE, |
| 338 get_response, callback); |
| 296 } | 339 } |
| 297 | 340 |
| 298 bool AwContentsIoThreadClientImpl::ShouldBlockContentUrls() const { | 341 bool AwContentsIoThreadClientImpl::ShouldBlockContentUrls() const { |
| 299 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 342 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 300 if (java_object_.is_null()) | 343 if (java_object_.is_null()) |
| 301 return false; | 344 return false; |
| 302 | 345 |
| 303 JNIEnv* env = AttachCurrentThread(); | 346 JNIEnv* env = AttachCurrentThread(); |
| 304 return Java_AwContentsIoThreadClient_shouldBlockContentUrls( | 347 return Java_AwContentsIoThreadClient_shouldBlockContentUrls( |
| 305 env, java_object_.obj()); | 348 env, java_object_.obj()); |
| (...skipping 155 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 461 jstring_reason.obj(), | 504 jstring_reason.obj(), |
| 462 jstringArray_response_header_names.obj(), | 505 jstringArray_response_header_names.obj(), |
| 463 jstringArray_response_header_values.obj()); | 506 jstringArray_response_header_values.obj()); |
| 464 } | 507 } |
| 465 | 508 |
| 466 bool RegisterAwContentsIoThreadClientImpl(JNIEnv* env) { | 509 bool RegisterAwContentsIoThreadClientImpl(JNIEnv* env) { |
| 467 return RegisterNativesImpl(env); | 510 return RegisterNativesImpl(env); |
| 468 } | 511 } |
| 469 | 512 |
| 470 } // namespace android_webview | 513 } // namespace android_webview |
| OLD | NEW |