| 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 "android_webview/browser/net/android_stream_reader_url_request_job.h" | 7 #include "android_webview/browser/net/android_stream_reader_url_request_job.h" |
| 8 #include "android_webview/browser/net/aw_url_request_job_factory.h" | 8 #include "android_webview/browser/net/aw_url_request_job_factory.h" |
| 9 #include "android_webview/common/url_constants.h" | 9 #include "android_webview/common/url_constants.h" |
| 10 #include "android_webview/native/input_stream_impl.h" | 10 #include "android_webview/native/input_stream_impl.h" |
| 11 #include "base/android/jni_android.h" | 11 #include "base/android/jni_android.h" |
| 12 #include "base/android/jni_helper.h" | 12 #include "base/android/jni_helper.h" |
| 13 #include "base/android/jni_string.h" | 13 #include "base/android/jni_string.h" |
| 14 #include "base/string_util.h" | 14 #include "base/string_util.h" |
| 15 #include "content/public/common/url_constants.h" | 15 #include "content/public/common/url_constants.h" |
| 16 #include "googleurl/src/gurl.h" | 16 #include "googleurl/src/gurl.h" |
| 17 #include "jni/AndroidProtocolHandler_jni.h" | 17 #include "jni/AndroidProtocolHandler_jni.h" |
| 18 #include "net/base/io_buffer.h" | 18 #include "net/base/io_buffer.h" |
| 19 #include "net/base/mime_util.h" | 19 #include "net/base/mime_util.h" |
| 20 #include "net/base/net_errors.h" | 20 #include "net/base/net_errors.h" |
| 21 #include "net/base/net_util.h" | 21 #include "net/base/net_util.h" |
| 22 #include "net/http/http_util.h" | 22 #include "net/http/http_util.h" |
| 23 #include "net/url_request/protocol_intercept_job_factory.h" |
| 23 #include "net/url_request/url_request.h" | 24 #include "net/url_request/url_request.h" |
| 24 | 25 |
| 25 using android_webview::InputStream; | 26 using android_webview::InputStream; |
| 26 using android_webview::InputStreamImpl; | 27 using android_webview::InputStreamImpl; |
| 27 using base::android::AttachCurrentThread; | 28 using base::android::AttachCurrentThread; |
| 28 using base::android::ClearException; | 29 using base::android::ClearException; |
| 29 using base::android::ConvertUTF8ToJavaString; | 30 using base::android::ConvertUTF8ToJavaString; |
| 30 using base::android::ScopedJavaGlobalRef; | 31 using base::android::ScopedJavaGlobalRef; |
| 31 using base::android::ScopedJavaLocalRef; | 32 using base::android::ScopedJavaLocalRef; |
| 32 | 33 |
| (...skipping 26 matching lines...) Expand all Loading... |
| 59 | 60 |
| 60 virtual bool GetCharset(JNIEnv* env, | 61 virtual bool GetCharset(JNIEnv* env, |
| 61 net::URLRequest* request, | 62 net::URLRequest* request, |
| 62 InputStream* stream, | 63 InputStream* stream, |
| 63 std::string* charset) OVERRIDE; | 64 std::string* charset) OVERRIDE; |
| 64 | 65 |
| 65 virtual ~AndroidStreamReaderURLRequestJobDelegateImpl(); | 66 virtual ~AndroidStreamReaderURLRequestJobDelegateImpl(); |
| 66 }; | 67 }; |
| 67 | 68 |
| 68 class AssetFileProtocolInterceptor : | 69 class AssetFileProtocolInterceptor : |
| 69 public net::URLRequestJobFactory::Interceptor { | 70 public net::URLRequestJobFactory::ProtocolHandler { |
| 70 public: | 71 public: |
| 71 AssetFileProtocolInterceptor(); | |
| 72 virtual ~AssetFileProtocolInterceptor() OVERRIDE; | 72 virtual ~AssetFileProtocolInterceptor() OVERRIDE; |
| 73 virtual net::URLRequestJob* MaybeIntercept( | 73 static scoped_ptr<net::URLRequestJobFactory> CreateURLRequestJobFactory( |
| 74 net::URLRequest* request, | 74 scoped_ptr<net::URLRequestJobFactory> base_job_factory); |
| 75 net::NetworkDelegate* network_delegate) const OVERRIDE; | 75 virtual net::URLRequestJob* MaybeCreateJob( |
| 76 virtual net::URLRequestJob* MaybeInterceptRedirect( | |
| 77 const GURL& location, | |
| 78 net::URLRequest* request, | |
| 79 net::NetworkDelegate* network_delegate) const OVERRIDE; | |
| 80 virtual net::URLRequestJob* MaybeInterceptResponse( | |
| 81 net::URLRequest* request, | 76 net::URLRequest* request, |
| 82 net::NetworkDelegate* network_delegate) const OVERRIDE; | 77 net::NetworkDelegate* network_delegate) const OVERRIDE; |
| 83 | 78 |
| 84 private: | 79 private: |
| 80 AssetFileProtocolInterceptor(); |
| 81 |
| 85 // file:///android_asset/ | 82 // file:///android_asset/ |
| 86 const std::string asset_prefix_; | 83 const std::string asset_prefix_; |
| 87 // file:///android_res/ | 84 // file:///android_res/ |
| 88 const std::string resource_prefix_; | 85 const std::string resource_prefix_; |
| 89 }; | 86 }; |
| 90 | 87 |
| 91 // Protocol handler for content:// scheme requests. | 88 // Protocol handler for content:// scheme requests. |
| 92 class ContentSchemeProtocolHandler : | 89 class ContentSchemeProtocolHandler : |
| 93 public net::URLRequestJobFactory::ProtocolHandler { | 90 public net::URLRequestJobFactory::ProtocolHandler { |
| 94 public: | 91 public: |
| (...skipping 94 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 189 std::string(content::kStandardSchemeSeparator) + | 186 std::string(content::kStandardSchemeSeparator) + |
| 190 android_webview::kAndroidAssetPath), | 187 android_webview::kAndroidAssetPath), |
| 191 resource_prefix_(std::string(chrome::kFileScheme) + | 188 resource_prefix_(std::string(chrome::kFileScheme) + |
| 192 std::string(content::kStandardSchemeSeparator) + | 189 std::string(content::kStandardSchemeSeparator) + |
| 193 android_webview::kAndroidResourcePath) { | 190 android_webview::kAndroidResourcePath) { |
| 194 } | 191 } |
| 195 | 192 |
| 196 AssetFileProtocolInterceptor::~AssetFileProtocolInterceptor() { | 193 AssetFileProtocolInterceptor::~AssetFileProtocolInterceptor() { |
| 197 } | 194 } |
| 198 | 195 |
| 199 net::URLRequestJob* AssetFileProtocolInterceptor::MaybeIntercept( | 196 // static |
| 197 scoped_ptr<net::URLRequestJobFactory> |
| 198 AssetFileProtocolInterceptor::CreateURLRequestJobFactory( |
| 199 scoped_ptr<net::URLRequestJobFactory> base_job_factory) { |
| 200 scoped_ptr<net::URLRequestJobFactory> top_job_factory( |
| 201 new net::ProtocolInterceptJobFactory( |
| 202 base_job_factory.Pass(), |
| 203 scoped_ptr<net::URLRequestJobFactory::ProtocolHandler>( |
| 204 new AssetFileProtocolInterceptor()))); |
| 205 return top_job_factory.Pass(); |
| 206 } |
| 207 |
| 208 net::URLRequestJob* AssetFileProtocolInterceptor::MaybeCreateJob( |
| 200 net::URLRequest* request, net::NetworkDelegate* network_delegate) const { | 209 net::URLRequest* request, net::NetworkDelegate* network_delegate) const { |
| 201 if (!request->url().SchemeIsFile()) return NULL; | 210 if (!request->url().SchemeIsFile()) return NULL; |
| 202 | 211 |
| 203 const std::string& url = request->url().spec(); | 212 const std::string& url = request->url().spec(); |
| 204 if (!StartsWithASCII(url, asset_prefix_, /*case_sensitive=*/ true) && | 213 if (!StartsWithASCII(url, asset_prefix_, /*case_sensitive=*/ true) && |
| 205 !StartsWithASCII(url, resource_prefix_, /*case_sensitive=*/ true)) { | 214 !StartsWithASCII(url, resource_prefix_, /*case_sensitive=*/ true)) { |
| 206 return NULL; | 215 return NULL; |
| 207 } | 216 } |
| 208 | 217 |
| 209 return new AndroidStreamReaderURLRequestJob( | 218 return new AndroidStreamReaderURLRequestJob( |
| 210 request, | 219 request, |
| 211 network_delegate, | 220 network_delegate, |
| 212 scoped_ptr<AndroidStreamReaderURLRequestJob::Delegate>( | 221 scoped_ptr<AndroidStreamReaderURLRequestJob::Delegate>( |
| 213 new AndroidStreamReaderURLRequestJobDelegateImpl())); | 222 new AndroidStreamReaderURLRequestJobDelegateImpl())); |
| 214 } | 223 } |
| 215 | 224 |
| 216 net::URLRequestJob* AssetFileProtocolInterceptor::MaybeInterceptRedirect( | |
| 217 const GURL& location, | |
| 218 net::URLRequest* request, | |
| 219 net::NetworkDelegate* network_delegate) const { | |
| 220 return NULL; | |
| 221 } | |
| 222 | |
| 223 net::URLRequestJob* AssetFileProtocolInterceptor::MaybeInterceptResponse( | |
| 224 net::URLRequest* request, | |
| 225 net::NetworkDelegate* network_delegate) const { | |
| 226 return NULL; | |
| 227 } | |
| 228 | |
| 229 } // namespace | 225 } // namespace |
| 230 | 226 |
| 231 namespace android_webview { | 227 namespace android_webview { |
| 232 | 228 |
| 233 bool RegisterAndroidProtocolHandler(JNIEnv* env) { | 229 bool RegisterAndroidProtocolHandler(JNIEnv* env) { |
| 234 return RegisterNativesImpl(env); | 230 return RegisterNativesImpl(env); |
| 235 } | 231 } |
| 236 | 232 |
| 237 // static | 233 // static |
| 238 void RegisterAndroidProtocolsOnIOThread( | 234 scoped_ptr<net::URLRequestJobFactory> CreateAndroidRequestJobFactory( |
| 239 net::URLRequestContext* context, | 235 scoped_ptr<AwURLRequestJobFactory> job_factory) { |
| 240 AwURLRequestJobFactory* job_factory) { | |
| 241 // Register content://. Note that even though a scheme is | 236 // Register content://. Note that even though a scheme is |
| 242 // registered here, it cannot be used by child processes until access to it is | 237 // registered here, it cannot be used by child processes until access to it is |
| 243 // granted via ChildProcessSecurityPolicy::GrantScheme(). This is done in | 238 // granted via ChildProcessSecurityPolicy::GrantScheme(). This is done in |
| 244 // AwContentBrowserClient. | 239 // AwContentBrowserClient. |
| 245 // The job factory takes ownership of the handler. | 240 // The job factory takes ownership of the handler. |
| 246 job_factory->SetProtocolHandler(android_webview::kContentScheme, | 241 bool set_protocol = job_factory->SetProtocolHandler( |
| 247 new ContentSchemeProtocolHandler()); | 242 android_webview::kContentScheme, new ContentSchemeProtocolHandler()); |
| 248 // The job factory takes ownership of the interceptor. | 243 DCHECK(set_protocol); |
| 249 job_factory->AddInterceptor(new AssetFileProtocolInterceptor()); | 244 return AssetFileProtocolInterceptor::CreateURLRequestJobFactory( |
| 245 job_factory.PassAs<net::URLRequestJobFactory>()); |
| 250 } | 246 } |
| 251 | 247 |
| 252 // Set a context object to be used for resolving resource queries. This can | 248 // Set a context object to be used for resolving resource queries. This can |
| 253 // be used to override the default application context and redirect all | 249 // be used to override the default application context and redirect all |
| 254 // resource queries to a specific context object, e.g., for the purposes of | 250 // resource queries to a specific context object, e.g., for the purposes of |
| 255 // testing. | 251 // testing. |
| 256 // | 252 // |
| 257 // |context| should be a android.content.Context instance or NULL to enable | 253 // |context| should be a android.content.Context instance or NULL to enable |
| 258 // the use of the standard application context. | 254 // the use of the standard application context. |
| 259 static void SetResourceContextForTesting(JNIEnv* env, jclass /*clazz*/, | 255 static void SetResourceContextForTesting(JNIEnv* env, jclass /*clazz*/, |
| (...skipping 11 matching lines...) Expand all Loading... |
| 271 env, android_webview::kAndroidAssetPath).Release(); | 267 env, android_webview::kAndroidAssetPath).Release(); |
| 272 } | 268 } |
| 273 | 269 |
| 274 static jstring GetAndroidResourcePath(JNIEnv* env, jclass /*clazz*/) { | 270 static jstring GetAndroidResourcePath(JNIEnv* env, jclass /*clazz*/) { |
| 275 // OK to release, JNI binding. | 271 // OK to release, JNI binding. |
| 276 return ConvertUTF8ToJavaString( | 272 return ConvertUTF8ToJavaString( |
| 277 env, android_webview::kAndroidResourcePath).Release(); | 273 env, android_webview::kAndroidResourcePath).Release(); |
| 278 } | 274 } |
| 279 | 275 |
| 280 } // namespace android_webview | 276 } // namespace android_webview |
| OLD | NEW |