Chromium Code Reviews| 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/common/url_constants.h" | 8 #include "android_webview/common/url_constants.h" |
| 9 #include "android_webview/native/input_stream_impl.h" | 9 #include "android_webview/native/input_stream_impl.h" |
| 10 #include "base/android/jni_android.h" | 10 #include "base/android/jni_android.h" |
| 11 #include "base/android/jni_helper.h" | 11 #include "base/android/jni_helper.h" |
| 12 #include "base/android/jni_string.h" | 12 #include "base/android/jni_string.h" |
| 13 #include "base/string_util.h" | 13 #include "base/string_util.h" |
| 14 #include "content/public/common/url_constants.h" | 14 #include "content/public/common/url_constants.h" |
| 15 #include "googleurl/src/gurl.h" | 15 #include "googleurl/src/gurl.h" |
| 16 #include "jni/AndroidProtocolHandler_jni.h" | 16 #include "jni/AndroidProtocolHandler_jni.h" |
| 17 #include "net/base/io_buffer.h" | 17 #include "net/base/io_buffer.h" |
| 18 #include "net/base/mime_util.h" | 18 #include "net/base/mime_util.h" |
| 19 #include "net/base/net_errors.h" | 19 #include "net/base/net_errors.h" |
| 20 #include "net/base/net_util.h" | 20 #include "net/base/net_util.h" |
| 21 #include "net/http/http_util.h" | 21 #include "net/http/http_util.h" |
| 22 #include "net/url_request/protocol_intercept_job_factory.h" | |
| 22 #include "net/url_request/url_request.h" | 23 #include "net/url_request/url_request.h" |
| 23 #include "net/url_request/url_request_job_factory.h" | 24 #include "net/url_request/url_request_job_factory.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; |
| (...skipping 27 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 static scoped_ptr<net::URLRequestJobFactory> CreateURLRequestJobFactory( |
| 73 scoped_ptr<net::URLRequestJobFactory> base_job_factory); | |
| 72 virtual ~AssetFileProtocolInterceptor() OVERRIDE; | 74 virtual ~AssetFileProtocolInterceptor() OVERRIDE; |
| 73 virtual net::URLRequestJob* MaybeIntercept( | 75 virtual net::URLRequestJob* MaybeCreateJob( |
| 74 net::URLRequest* request, | |
| 75 net::NetworkDelegate* network_delegate) const OVERRIDE; | |
| 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(); | |
| 85 // file:///android_asset/ | 81 // file:///android_asset/ |
| 86 const std::string asset_prefix_; | 82 const std::string asset_prefix_; |
| 87 // file:///android_res/ | 83 // file:///android_res/ |
| 88 const std::string resource_prefix_; | 84 const std::string resource_prefix_; |
| 89 }; | 85 }; |
| 90 | 86 |
| 91 // Protocol handler for content:// scheme requests. | 87 // Protocol handler for content:// scheme requests. |
| 92 class ContentSchemeProtocolHandler : | 88 class ContentSchemeProtocolHandler : |
| 93 public net::URLRequestJobFactory::ProtocolHandler { | 89 public net::URLRequestJobFactory::ProtocolHandler { |
| 94 public: | 90 public: |
| (...skipping 94 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 new AssetFileProtocolInterceptor())); | |
| 202 return top_job_factory.Pass(); | |
| 203 } | |
| 204 | |
| 205 net::URLRequestJob* AssetFileProtocolInterceptor::MaybeCreateJob( | |
| 200 net::URLRequest* request, net::NetworkDelegate* network_delegate) const { | 206 net::URLRequest* request, net::NetworkDelegate* network_delegate) const { |
| 201 if (!request->url().SchemeIsFile()) return NULL; | 207 if (!request->url().SchemeIsFile()) return NULL; |
| 202 | 208 |
| 203 const std::string& url = request->url().spec(); | 209 const std::string& url = request->url().spec(); |
| 204 if (!StartsWithASCII(url, asset_prefix_, /*case_sensitive=*/ true) && | 210 if (!StartsWithASCII(url, asset_prefix_, /*case_sensitive=*/ true) && |
| 205 !StartsWithASCII(url, resource_prefix_, /*case_sensitive=*/ true)) { | 211 !StartsWithASCII(url, resource_prefix_, /*case_sensitive=*/ true)) { |
| 206 return NULL; | 212 return NULL; |
| 207 } | 213 } |
| 208 | 214 |
| 209 return new AndroidStreamReaderURLRequestJob( | 215 return new AndroidStreamReaderURLRequestJob( |
| 210 request, | 216 request, |
| 211 network_delegate, | 217 network_delegate, |
| 212 scoped_ptr<AndroidStreamReaderURLRequestJob::Delegate>( | 218 scoped_ptr<AndroidStreamReaderURLRequestJob::Delegate>( |
| 213 new AndroidStreamReaderURLRequestJobDelegateImpl())); | 219 new AndroidStreamReaderURLRequestJobDelegateImpl())); |
| 214 } | 220 } |
| 215 | 221 |
| 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 | 222 } // namespace |
| 230 | 223 |
| 231 namespace android_webview { | 224 namespace android_webview { |
| 232 | 225 |
| 233 bool RegisterAndroidProtocolHandler(JNIEnv* env) { | 226 bool RegisterAndroidProtocolHandler(JNIEnv* env) { |
| 234 return RegisterNativesImpl(env); | 227 return RegisterNativesImpl(env); |
| 235 } | 228 } |
| 236 | 229 |
| 237 // static | 230 // static |
| 238 void RegisterAndroidProtocolsOnIOThread( | 231 scoped_ptr<net::URLRequestJobFactory> RegisterAndroidProtocolsOnIOThread( |
|
mmenke
2012/12/11 17:22:38
Since we're creating a new factory, I think this f
| |
| 239 net::URLRequestContext* context, | 232 net::URLRequestContext* context, |
|
mmenke
2012/12/11 17:22:38
While we're here, looks like we don't need |contex
| |
| 240 net::URLRequestJobFactory* job_factory) { | 233 scoped_ptr<net::URLRequestJobFactory> job_factory) { |
| 241 // Register content://. Note that even though a scheme is | 234 // Register content://. Note that even though a scheme is |
| 242 // registered here, it cannot be used by child processes until access to it is | 235 // registered here, it cannot be used by child processes until access to it is |
| 243 // granted via ChildProcessSecurityPolicy::GrantScheme(). This is done in | 236 // granted via ChildProcessSecurityPolicy::GrantScheme(). This is done in |
| 244 // AwContentBrowserClient. | 237 // AwContentBrowserClient. |
| 245 // The job factory takes ownership of the handler. | 238 // The job factory takes ownership of the handler. |
| 246 job_factory->SetProtocolHandler(android_webview::kContentScheme, | 239 job_factory->SetProtocolHandler(android_webview::kContentScheme, |
| 247 new ContentSchemeProtocolHandler()); | 240 new ContentSchemeProtocolHandler()); |
| 248 // The job factory takes ownership of the interceptor. | 241 return AssetFileProtocolInterceptor::CreateURLRequestJobFactory( |
| 249 job_factory->AddInterceptor(new AssetFileProtocolInterceptor()); | 242 job_factory.Pass()); |
| 250 } | 243 } |
| 251 | 244 |
| 252 // Set a context object to be used for resolving resource queries. This can | 245 // 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 | 246 // 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 | 247 // resource queries to a specific context object, e.g., for the purposes of |
| 255 // testing. | 248 // testing. |
| 256 // | 249 // |
| 257 // |context| should be a android.content.Context instance or NULL to enable | 250 // |context| should be a android.content.Context instance or NULL to enable |
| 258 // the use of the standard application context. | 251 // the use of the standard application context. |
| 259 static void SetResourceContextForTesting(JNIEnv* env, jclass /*clazz*/, | 252 static void SetResourceContextForTesting(JNIEnv* env, jclass /*clazz*/, |
| (...skipping 11 matching lines...) Expand all Loading... | |
| 271 env, android_webview::kAndroidAssetPath).Release(); | 264 env, android_webview::kAndroidAssetPath).Release(); |
| 272 } | 265 } |
| 273 | 266 |
| 274 static jstring GetAndroidResourcePath(JNIEnv* env, jclass /*clazz*/) { | 267 static jstring GetAndroidResourcePath(JNIEnv* env, jclass /*clazz*/) { |
| 275 // OK to release, JNI binding. | 268 // OK to release, JNI binding. |
| 276 return ConvertUTF8ToJavaString( | 269 return ConvertUTF8ToJavaString( |
| 277 env, android_webview::kAndroidResourcePath).Release(); | 270 env, android_webview::kAndroidResourcePath).Release(); |
| 278 } | 271 } |
| 279 | 272 |
| 280 } // namespace android_webview | 273 } // namespace android_webview |
| OLD | NEW |