| 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/context_utils.h" |
| 14 #include "base/android/jni_android.h" | 14 #include "base/android/jni_android.h" |
| 15 #include "base/android/jni_string.h" | 15 #include "base/android/jni_string.h" |
| 16 #include "base/android/jni_weak_ref.h" | 16 #include "base/android/jni_weak_ref.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" |
| (...skipping 29 matching lines...) Expand all Loading... |
| 56 | 57 |
| 57 bool HasRequestPreviouslyFailed(net::URLRequest* request) { | 58 bool HasRequestPreviouslyFailed(net::URLRequest* request) { |
| 58 return request->GetUserData(kPreviouslyFailedKey) != NULL; | 59 return request->GetUserData(kPreviouslyFailedKey) != NULL; |
| 59 } | 60 } |
| 60 | 61 |
| 61 class AndroidStreamReaderURLRequestJobDelegateImpl | 62 class AndroidStreamReaderURLRequestJobDelegateImpl |
| 62 : public AndroidStreamReaderURLRequestJob::Delegate { | 63 : public AndroidStreamReaderURLRequestJob::Delegate { |
| 63 public: | 64 public: |
| 64 AndroidStreamReaderURLRequestJobDelegateImpl(); | 65 AndroidStreamReaderURLRequestJobDelegateImpl(); |
| 65 | 66 |
| 66 scoped_ptr<InputStream> OpenInputStream(JNIEnv* env, | 67 std::unique_ptr<InputStream> OpenInputStream(JNIEnv* env, |
| 67 const GURL& url) override; | 68 const GURL& url) override; |
| 68 | 69 |
| 69 void OnInputStreamOpenFailed(net::URLRequest* request, | 70 void OnInputStreamOpenFailed(net::URLRequest* request, |
| 70 bool* restart) override; | 71 bool* restart) override; |
| 71 | 72 |
| 72 bool GetMimeType(JNIEnv* env, | 73 bool GetMimeType(JNIEnv* env, |
| 73 net::URLRequest* request, | 74 net::URLRequest* request, |
| 74 InputStream* stream, | 75 InputStream* stream, |
| 75 std::string* mime_type) override; | 76 std::string* mime_type) override; |
| 76 | 77 |
| 77 bool GetCharset(JNIEnv* env, | 78 bool GetCharset(JNIEnv* env, |
| (...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 120 | 121 |
| 121 // AndroidStreamReaderURLRequestJobDelegateImpl ------------------------------- | 122 // AndroidStreamReaderURLRequestJobDelegateImpl ------------------------------- |
| 122 | 123 |
| 123 AndroidStreamReaderURLRequestJobDelegateImpl:: | 124 AndroidStreamReaderURLRequestJobDelegateImpl:: |
| 124 AndroidStreamReaderURLRequestJobDelegateImpl() {} | 125 AndroidStreamReaderURLRequestJobDelegateImpl() {} |
| 125 | 126 |
| 126 AndroidStreamReaderURLRequestJobDelegateImpl:: | 127 AndroidStreamReaderURLRequestJobDelegateImpl:: |
| 127 ~AndroidStreamReaderURLRequestJobDelegateImpl() { | 128 ~AndroidStreamReaderURLRequestJobDelegateImpl() { |
| 128 } | 129 } |
| 129 | 130 |
| 130 scoped_ptr<InputStream> | 131 std::unique_ptr<InputStream> |
| 131 AndroidStreamReaderURLRequestJobDelegateImpl::OpenInputStream( | 132 AndroidStreamReaderURLRequestJobDelegateImpl::OpenInputStream(JNIEnv* env, |
| 132 JNIEnv* env, const GURL& url) { | 133 const GURL& url) { |
| 133 DCHECK(url.is_valid()); | 134 DCHECK(url.is_valid()); |
| 134 DCHECK(env); | 135 DCHECK(env); |
| 135 | 136 |
| 136 // Open the input stream. | 137 // Open the input stream. |
| 137 ScopedJavaLocalRef<jstring> jurl = | 138 ScopedJavaLocalRef<jstring> jurl = |
| 138 ConvertUTF8ToJavaString(env, url.spec()); | 139 ConvertUTF8ToJavaString(env, url.spec()); |
| 139 ScopedJavaLocalRef<jobject> stream = | 140 ScopedJavaLocalRef<jobject> stream = |
| 140 android_webview::Java_AndroidProtocolHandler_open( | 141 android_webview::Java_AndroidProtocolHandler_open( |
| 141 env, | 142 env, |
| 142 GetResourceContext(env).obj(), | 143 GetResourceContext(env).obj(), |
| 143 jurl.obj()); | 144 jurl.obj()); |
| 144 | 145 |
| 145 if (stream.is_null()) { | 146 if (stream.is_null()) { |
| 146 DLOG(ERROR) << "Unable to open input stream for Android URL"; | 147 DLOG(ERROR) << "Unable to open input stream for Android URL"; |
| 147 return scoped_ptr<InputStream>(); | 148 return std::unique_ptr<InputStream>(); |
| 148 } | 149 } |
| 149 return make_scoped_ptr<InputStream>(new InputStreamImpl(stream)); | 150 return base::WrapUnique(new InputStreamImpl(stream)); |
| 150 } | 151 } |
| 151 | 152 |
| 152 void AndroidStreamReaderURLRequestJobDelegateImpl::OnInputStreamOpenFailed( | 153 void AndroidStreamReaderURLRequestJobDelegateImpl::OnInputStreamOpenFailed( |
| 153 net::URLRequest* request, | 154 net::URLRequest* request, |
| 154 bool* restart) { | 155 bool* restart) { |
| 155 DCHECK(!HasRequestPreviouslyFailed(request)); | 156 DCHECK(!HasRequestPreviouslyFailed(request)); |
| 156 MarkRequestAsFailed(request); | 157 MarkRequestAsFailed(request); |
| 157 *restart = true; | 158 *restart = true; |
| 158 } | 159 } |
| 159 | 160 |
| (...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 213 // If a request is initially handled here but the job fails due to it being | 214 // If a request is initially handled here but the job fails due to it being |
| 214 // unable to open the InputStream for that request the request is marked as | 215 // unable to open the InputStream for that request the request is marked as |
| 215 // previously failed and restarted. | 216 // previously failed and restarted. |
| 216 // Restarting a request involves creating a new job for that request. This | 217 // Restarting a request involves creating a new job for that request. This |
| 217 // handler will ignore requests know to have previously failed to 1) prevent | 218 // handler will ignore requests know to have previously failed to 1) prevent |
| 218 // an infinite loop, 2) ensure that the next handler in line gets the | 219 // an infinite loop, 2) ensure that the next handler in line gets the |
| 219 // opportunity to create a job for the request. | 220 // opportunity to create a job for the request. |
| 220 if (HasRequestPreviouslyFailed(request)) | 221 if (HasRequestPreviouslyFailed(request)) |
| 221 return NULL; | 222 return NULL; |
| 222 | 223 |
| 223 scoped_ptr<AndroidStreamReaderURLRequestJobDelegateImpl> reader_delegate( | 224 std::unique_ptr<AndroidStreamReaderURLRequestJobDelegateImpl> reader_delegate( |
| 224 new AndroidStreamReaderURLRequestJobDelegateImpl()); | 225 new AndroidStreamReaderURLRequestJobDelegateImpl()); |
| 225 | 226 |
| 226 return new AndroidStreamReaderURLRequestJob(request, network_delegate, | 227 return new AndroidStreamReaderURLRequestJob(request, network_delegate, |
| 227 std::move(reader_delegate)); | 228 std::move(reader_delegate)); |
| 228 } | 229 } |
| 229 | 230 |
| 230 // AssetFileRequestInterceptor ------------------------------------------------ | 231 // AssetFileRequestInterceptor ------------------------------------------------ |
| 231 | 232 |
| 232 AssetFileRequestInterceptor::AssetFileRequestInterceptor() { | 233 AssetFileRequestInterceptor::AssetFileRequestInterceptor() { |
| 233 } | 234 } |
| (...skipping 15 matching lines...) Expand all Loading... |
| 249 | 250 |
| 250 } // namespace | 251 } // namespace |
| 251 | 252 |
| 252 namespace android_webview { | 253 namespace android_webview { |
| 253 | 254 |
| 254 bool RegisterAndroidProtocolHandler(JNIEnv* env) { | 255 bool RegisterAndroidProtocolHandler(JNIEnv* env) { |
| 255 return RegisterNativesImpl(env); | 256 return RegisterNativesImpl(env); |
| 256 } | 257 } |
| 257 | 258 |
| 258 // static | 259 // static |
| 259 scoped_ptr<net::URLRequestInterceptor> | 260 std::unique_ptr<net::URLRequestInterceptor> |
| 260 CreateContentSchemeRequestInterceptor() { | 261 CreateContentSchemeRequestInterceptor() { |
| 261 return make_scoped_ptr<net::URLRequestInterceptor>( | 262 return base::WrapUnique(new ContentSchemeRequestInterceptor()); |
| 262 new ContentSchemeRequestInterceptor()); | |
| 263 } | 263 } |
| 264 | 264 |
| 265 // static | 265 // static |
| 266 scoped_ptr<net::URLRequestInterceptor> CreateAssetFileRequestInterceptor() { | 266 std::unique_ptr<net::URLRequestInterceptor> |
| 267 return scoped_ptr<net::URLRequestInterceptor>( | 267 CreateAssetFileRequestInterceptor() { |
| 268 return std::unique_ptr<net::URLRequestInterceptor>( |
| 268 new AssetFileRequestInterceptor()); | 269 new AssetFileRequestInterceptor()); |
| 269 } | 270 } |
| 270 | 271 |
| 271 // Set a context object to be used for resolving resource queries. This can | 272 // Set a context object to be used for resolving resource queries. This can |
| 272 // be used to override the default application context and redirect all | 273 // be used to override the default application context and redirect all |
| 273 // resource queries to a specific context object, e.g., for the purposes of | 274 // resource queries to a specific context object, e.g., for the purposes of |
| 274 // testing. | 275 // testing. |
| 275 // | 276 // |
| 276 // |context| should be a android.content.Context instance or NULL to enable | 277 // |context| should be a android.content.Context instance or NULL to enable |
| 277 // the use of the standard application context. | 278 // the use of the standard application context. |
| (...skipping 13 matching lines...) Expand all Loading... |
| 291 return ConvertUTF8ToJavaString(env, android_webview::kAndroidAssetPath); | 292 return ConvertUTF8ToJavaString(env, android_webview::kAndroidAssetPath); |
| 292 } | 293 } |
| 293 | 294 |
| 294 static ScopedJavaLocalRef<jstring> GetAndroidResourcePath( | 295 static ScopedJavaLocalRef<jstring> GetAndroidResourcePath( |
| 295 JNIEnv* env, | 296 JNIEnv* env, |
| 296 const JavaParamRef<jclass>& /*clazz*/) { | 297 const JavaParamRef<jclass>& /*clazz*/) { |
| 297 return ConvertUTF8ToJavaString(env, android_webview::kAndroidResourcePath); | 298 return ConvertUTF8ToJavaString(env, android_webview::kAndroidResourcePath); |
| 298 } | 299 } |
| 299 | 300 |
| 300 } // namespace android_webview | 301 } // namespace android_webview |
| OLD | NEW |