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