| 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/android_protocol_handler.h" | 5 #include "android_webview/native/android_protocol_handler.h" |
| 6 | 6 |
| 7 #include <utility> | 7 #include <utility> |
| 8 | 8 |
| 9 #include "android_webview/browser/net/android_stream_reader_url_request_job.h" | 9 #include "android_webview/browser/net/android_stream_reader_url_request_job.h" |
| 10 #include "android_webview/browser/net/aw_url_request_job_factory.h" | 10 #include "android_webview/browser/net/aw_url_request_job_factory.h" |
| 11 #include "android_webview/common/url_constants.h" | 11 #include "android_webview/common/url_constants.h" |
| 12 #include "android_webview/native/input_stream_impl.h" | 12 #include "android_webview/native/input_stream_impl.h" |
| 13 #include "base/android/context_utils.h" |
| 13 #include "base/android/jni_android.h" | 14 #include "base/android/jni_android.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/memory/ptr_util.h" | 17 #include "base/memory/ptr_util.h" |
| 17 #include "content/public/common/url_constants.h" | 18 #include "content/public/common/url_constants.h" |
| 18 #include "jni/AndroidProtocolHandler_jni.h" | 19 #include "jni/AndroidProtocolHandler_jni.h" |
| 19 #include "net/base/io_buffer.h" | 20 #include "net/base/io_buffer.h" |
| 20 #include "net/base/mime_util.h" | 21 #include "net/base/mime_util.h" |
| 21 #include "net/base/net_errors.h" | 22 #include "net/base/net_errors.h" |
| 22 #include "net/http/http_util.h" | 23 #include "net/http/http_util.h" |
| 23 #include "net/url_request/url_request.h" | 24 #include "net/url_request/url_request.h" |
| 24 #include "net/url_request/url_request_interceptor.h" | 25 #include "net/url_request/url_request_interceptor.h" |
| 25 #include "url/gurl.h" | 26 #include "url/gurl.h" |
| 26 #include "url/url_constants.h" | 27 #include "url/url_constants.h" |
| 27 | 28 |
| 28 using android_webview::AndroidStreamReaderURLRequestJob; | 29 using android_webview::AndroidStreamReaderURLRequestJob; |
| 29 using android_webview::InputStream; | 30 using android_webview::InputStream; |
| 30 using android_webview::InputStreamImpl; | 31 using android_webview::InputStreamImpl; |
| 31 using base::android::AttachCurrentThread; | 32 using base::android::AttachCurrentThread; |
| 32 using base::android::ClearException; | 33 using base::android::ClearException; |
| 33 using base::android::ConvertUTF8ToJavaString; | 34 using base::android::ConvertUTF8ToJavaString; |
| 34 using base::android::JavaParamRef; | 35 using base::android::JavaParamRef; |
| 35 using base::android::ScopedJavaGlobalRef; | 36 using base::android::ScopedJavaGlobalRef; |
| 36 using base::android::ScopedJavaLocalRef; | 37 using base::android::ScopedJavaLocalRef; |
| 37 | 38 |
| 38 namespace { | 39 namespace { |
| 39 | 40 |
| 41 // Override resource context for reading resource and asset files. Used for |
| 42 // testing. |
| 43 JavaObjectWeakGlobalRef* g_resource_context = NULL; |
| 44 |
| 45 void ResetResourceContext(JavaObjectWeakGlobalRef* ref) { |
| 46 if (g_resource_context) |
| 47 delete g_resource_context; |
| 48 |
| 49 g_resource_context = ref; |
| 50 } |
| 51 |
| 40 void* kPreviouslyFailedKey = &kPreviouslyFailedKey; | 52 void* kPreviouslyFailedKey = &kPreviouslyFailedKey; |
| 41 | 53 |
| 42 void MarkRequestAsFailed(net::URLRequest* request) { | 54 void MarkRequestAsFailed(net::URLRequest* request) { |
| 43 request->SetUserData(kPreviouslyFailedKey, | 55 request->SetUserData(kPreviouslyFailedKey, |
| 44 new base::SupportsUserData::Data()); | 56 new base::SupportsUserData::Data()); |
| 45 } | 57 } |
| 46 | 58 |
| 47 bool HasRequestPreviouslyFailed(net::URLRequest* request) { | 59 bool HasRequestPreviouslyFailed(net::URLRequest* request) { |
| 48 return request->GetUserData(kPreviouslyFailedKey) != NULL; | 60 return request->GetUserData(kPreviouslyFailedKey) != NULL; |
| 49 } | 61 } |
| (...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 90 bool ShouldHandleRequest(const net::URLRequest* request) const override; | 102 bool ShouldHandleRequest(const net::URLRequest* request) const override; |
| 91 }; | 103 }; |
| 92 | 104 |
| 93 // Protocol handler for content:// scheme requests. | 105 // Protocol handler for content:// scheme requests. |
| 94 class ContentSchemeRequestInterceptor : public AndroidRequestInterceptorBase { | 106 class ContentSchemeRequestInterceptor : public AndroidRequestInterceptorBase { |
| 95 public: | 107 public: |
| 96 ContentSchemeRequestInterceptor(); | 108 ContentSchemeRequestInterceptor(); |
| 97 bool ShouldHandleRequest(const net::URLRequest* request) const override; | 109 bool ShouldHandleRequest(const net::URLRequest* request) const override; |
| 98 }; | 110 }; |
| 99 | 111 |
| 112 static ScopedJavaLocalRef<jobject> GetResourceContext(JNIEnv* env) { |
| 113 if (g_resource_context) |
| 114 return g_resource_context->get(env); |
| 115 return ScopedJavaLocalRef<jobject>(base::android::GetApplicationContext()); |
| 116 } |
| 117 |
| 100 // AndroidStreamReaderURLRequestJobDelegateImpl ------------------------------- | 118 // AndroidStreamReaderURLRequestJobDelegateImpl ------------------------------- |
| 101 | 119 |
| 102 AndroidStreamReaderURLRequestJobDelegateImpl:: | 120 AndroidStreamReaderURLRequestJobDelegateImpl:: |
| 103 AndroidStreamReaderURLRequestJobDelegateImpl() {} | 121 AndroidStreamReaderURLRequestJobDelegateImpl() {} |
| 104 | 122 |
| 105 AndroidStreamReaderURLRequestJobDelegateImpl:: | 123 AndroidStreamReaderURLRequestJobDelegateImpl:: |
| 106 ~AndroidStreamReaderURLRequestJobDelegateImpl() { | 124 ~AndroidStreamReaderURLRequestJobDelegateImpl() { |
| 107 } | 125 } |
| 108 | 126 |
| 109 std::unique_ptr<InputStream> | 127 std::unique_ptr<InputStream> |
| 110 AndroidStreamReaderURLRequestJobDelegateImpl::OpenInputStream(JNIEnv* env, | 128 AndroidStreamReaderURLRequestJobDelegateImpl::OpenInputStream(JNIEnv* env, |
| 111 const GURL& url) { | 129 const GURL& url) { |
| 112 DCHECK(url.is_valid()); | 130 DCHECK(url.is_valid()); |
| 113 DCHECK(env); | 131 DCHECK(env); |
| 114 | 132 |
| 115 // Open the input stream. | 133 // Open the input stream. |
| 116 ScopedJavaLocalRef<jstring> jurl = | 134 ScopedJavaLocalRef<jstring> jurl = |
| 117 ConvertUTF8ToJavaString(env, url.spec()); | 135 ConvertUTF8ToJavaString(env, url.spec()); |
| 118 ScopedJavaLocalRef<jobject> stream = | 136 ScopedJavaLocalRef<jobject> stream = |
| 119 android_webview::Java_AndroidProtocolHandler_open(env, jurl); | 137 android_webview::Java_AndroidProtocolHandler_open( |
| 138 env, GetResourceContext(env), jurl); |
| 120 | 139 |
| 121 if (stream.is_null()) { | 140 if (stream.is_null()) { |
| 122 DLOG(ERROR) << "Unable to open input stream for Android URL"; | 141 DLOG(ERROR) << "Unable to open input stream for Android URL"; |
| 123 return std::unique_ptr<InputStream>(); | 142 return std::unique_ptr<InputStream>(); |
| 124 } | 143 } |
| 125 return base::MakeUnique<InputStreamImpl>(stream); | 144 return base::MakeUnique<InputStreamImpl>(stream); |
| 126 } | 145 } |
| 127 | 146 |
| 128 void AndroidStreamReaderURLRequestJobDelegateImpl::OnInputStreamOpenFailed( | 147 void AndroidStreamReaderURLRequestJobDelegateImpl::OnInputStreamOpenFailed( |
| 129 net::URLRequest* request, | 148 net::URLRequest* request, |
| (...skipping 13 matching lines...) Expand all Loading... |
| 143 DCHECK(mime_type); | 162 DCHECK(mime_type); |
| 144 | 163 |
| 145 // Query the mime type from the Java side. It is possible for the query to | 164 // Query the mime type from the Java side. It is possible for the query to |
| 146 // fail, as the mime type cannot be determined for all supported schemes. | 165 // fail, as the mime type cannot be determined for all supported schemes. |
| 147 ScopedJavaLocalRef<jstring> url = | 166 ScopedJavaLocalRef<jstring> url = |
| 148 ConvertUTF8ToJavaString(env, request->url().spec()); | 167 ConvertUTF8ToJavaString(env, request->url().spec()); |
| 149 const InputStreamImpl* stream_impl = | 168 const InputStreamImpl* stream_impl = |
| 150 InputStreamImpl::FromInputStream(stream); | 169 InputStreamImpl::FromInputStream(stream); |
| 151 ScopedJavaLocalRef<jstring> returned_type = | 170 ScopedJavaLocalRef<jstring> returned_type = |
| 152 android_webview::Java_AndroidProtocolHandler_getMimeType( | 171 android_webview::Java_AndroidProtocolHandler_getMimeType( |
| 153 env, stream_impl->jobj(), url); | 172 env, GetResourceContext(env), stream_impl->jobj(), url); |
| 154 if (returned_type.is_null()) | 173 if (returned_type.is_null()) |
| 155 return false; | 174 return false; |
| 156 | 175 |
| 157 *mime_type = base::android::ConvertJavaStringToUTF8(returned_type); | 176 *mime_type = base::android::ConvertJavaStringToUTF8(returned_type); |
| 158 return true; | 177 return true; |
| 159 } | 178 } |
| 160 | 179 |
| 161 bool AndroidStreamReaderURLRequestJobDelegateImpl::GetCharset( | 180 bool AndroidStreamReaderURLRequestJobDelegateImpl::GetCharset( |
| 162 JNIEnv* env, | 181 JNIEnv* env, |
| 163 net::URLRequest* request, | 182 net::URLRequest* request, |
| (...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 235 return base::MakeUnique<ContentSchemeRequestInterceptor>(); | 254 return base::MakeUnique<ContentSchemeRequestInterceptor>(); |
| 236 } | 255 } |
| 237 | 256 |
| 238 // static | 257 // static |
| 239 std::unique_ptr<net::URLRequestInterceptor> | 258 std::unique_ptr<net::URLRequestInterceptor> |
| 240 CreateAssetFileRequestInterceptor() { | 259 CreateAssetFileRequestInterceptor() { |
| 241 return std::unique_ptr<net::URLRequestInterceptor>( | 260 return std::unique_ptr<net::URLRequestInterceptor>( |
| 242 new AssetFileRequestInterceptor()); | 261 new AssetFileRequestInterceptor()); |
| 243 } | 262 } |
| 244 | 263 |
| 264 // Set a context object to be used for resolving resource queries. This can |
| 265 // be used to override the default application context and redirect all |
| 266 // resource queries to a specific context object, e.g., for the purposes of |
| 267 // testing. |
| 268 // |
| 269 // |context| should be a android.content.Context instance or NULL to enable |
| 270 // the use of the standard application context. |
| 271 static void SetResourceContextForTesting(JNIEnv* env, |
| 272 const JavaParamRef<jclass>& /*clazz*/, |
| 273 const JavaParamRef<jobject>& context) { |
| 274 if (context) { |
| 275 ResetResourceContext(new JavaObjectWeakGlobalRef(env, context)); |
| 276 } else { |
| 277 ResetResourceContext(NULL); |
| 278 } |
| 279 } |
| 280 |
| 245 static ScopedJavaLocalRef<jstring> GetAndroidAssetPath( | 281 static ScopedJavaLocalRef<jstring> GetAndroidAssetPath( |
| 246 JNIEnv* env, | 282 JNIEnv* env, |
| 247 const JavaParamRef<jclass>& /*clazz*/) { | 283 const JavaParamRef<jclass>& /*clazz*/) { |
| 248 return ConvertUTF8ToJavaString(env, android_webview::kAndroidAssetPath); | 284 return ConvertUTF8ToJavaString(env, android_webview::kAndroidAssetPath); |
| 249 } | 285 } |
| 250 | 286 |
| 251 static ScopedJavaLocalRef<jstring> GetAndroidResourcePath( | 287 static ScopedJavaLocalRef<jstring> GetAndroidResourcePath( |
| 252 JNIEnv* env, | 288 JNIEnv* env, |
| 253 const JavaParamRef<jclass>& /*clazz*/) { | 289 const JavaParamRef<jclass>& /*clazz*/) { |
| 254 return ConvertUTF8ToJavaString(env, android_webview::kAndroidResourcePath); | 290 return ConvertUTF8ToJavaString(env, android_webview::kAndroidResourcePath); |
| 255 } | 291 } |
| 256 | 292 |
| 257 } // namespace android_webview | 293 } // namespace android_webview |
| OLD | NEW |