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/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 static scoped_ptr<net::URLRequestJobFactory> CreateURLRequestJobFactory( |
| 73 scoped_ptr<net::URLRequestJobFactory> base_job_factory); | |
| 72 virtual ~AssetFileProtocolInterceptor() OVERRIDE; | 74 virtual ~AssetFileProtocolInterceptor() OVERRIDE; |
|
mmenke
2012/12/18 20:32:15
nit: Destructor should go before other methods.
| |
| 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(); | |
|
mmenke
2012/12/18 20:32:15
nit: Blank line between methods and data.
| |
| 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( | |
| 201 base_job_factory.Pass(), | |
| 202 scoped_ptr<net::URLRequestJobFactory::ProtocolHandler>( | |
| 203 new AssetFileProtocolInterceptor()))); | |
| 204 return top_job_factory.Pass(); | |
| 205 } | |
| 206 | |
| 207 net::URLRequestJob* AssetFileProtocolInterceptor::MaybeCreateJob( | |
| 200 net::URLRequest* request, net::NetworkDelegate* network_delegate) const { | 208 net::URLRequest* request, net::NetworkDelegate* network_delegate) const { |
| 201 if (!request->url().SchemeIsFile()) return NULL; | 209 if (!request->url().SchemeIsFile()) return NULL; |
| 202 | 210 |
| 203 const std::string& url = request->url().spec(); | 211 const std::string& url = request->url().spec(); |
| 204 if (!StartsWithASCII(url, asset_prefix_, /*case_sensitive=*/ true) && | 212 if (!StartsWithASCII(url, asset_prefix_, /*case_sensitive=*/ true) && |
| 205 !StartsWithASCII(url, resource_prefix_, /*case_sensitive=*/ true)) { | 213 !StartsWithASCII(url, resource_prefix_, /*case_sensitive=*/ true)) { |
| 206 return NULL; | 214 return NULL; |
| 207 } | 215 } |
| 208 | 216 |
| 209 return new AndroidStreamReaderURLRequestJob( | 217 return new AndroidStreamReaderURLRequestJob( |
| 210 request, | 218 request, |
| 211 network_delegate, | 219 network_delegate, |
| 212 scoped_ptr<AndroidStreamReaderURLRequestJob::Delegate>( | 220 scoped_ptr<AndroidStreamReaderURLRequestJob::Delegate>( |
| 213 new AndroidStreamReaderURLRequestJobDelegateImpl())); | 221 new AndroidStreamReaderURLRequestJobDelegateImpl())); |
| 214 } | 222 } |
| 215 | 223 |
| 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 | 224 } // namespace |
| 230 | 225 |
| 231 namespace android_webview { | 226 namespace android_webview { |
| 232 | 227 |
| 233 bool RegisterAndroidProtocolHandler(JNIEnv* env) { | 228 bool RegisterAndroidProtocolHandler(JNIEnv* env) { |
| 234 return RegisterNativesImpl(env); | 229 return RegisterNativesImpl(env); |
| 235 } | 230 } |
| 236 | 231 |
| 237 // static | 232 // static |
| 238 void RegisterAndroidProtocolsOnIOThread( | 233 scoped_ptr<net::URLRequestJobFactory> CreateAndroidRequestJobFactory( |
| 239 net::URLRequestContext* context, | 234 scoped_ptr<AwURLRequestJobFactory> job_factory) { |
| 240 AwURLRequestJobFactory* 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 bool set_protocol = job_factory->SetProtocolHandler( |
| 247 new ContentSchemeProtocolHandler()); | 241 android_webview::kContentScheme, new ContentSchemeProtocolHandler()); |
| 248 // The job factory takes ownership of the interceptor. | 242 DCHECK(set_protocol); |
| 249 job_factory->AddInterceptor(new AssetFileProtocolInterceptor()); | 243 return AssetFileProtocolInterceptor::CreateURLRequestJobFactory( |
| 244 job_factory.PassAs<net::URLRequestJobFactory>()); | |
| 250 } | 245 } |
| 251 | 246 |
| 252 // Set a context object to be used for resolving resource queries. This can | 247 // 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 | 248 // 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 | 249 // resource queries to a specific context object, e.g., for the purposes of |
| 255 // testing. | 250 // testing. |
| 256 // | 251 // |
| 257 // |context| should be a android.content.Context instance or NULL to enable | 252 // |context| should be a android.content.Context instance or NULL to enable |
| 258 // the use of the standard application context. | 253 // the use of the standard application context. |
| 259 static void SetResourceContextForTesting(JNIEnv* env, jclass /*clazz*/, | 254 static void SetResourceContextForTesting(JNIEnv* env, jclass /*clazz*/, |
| (...skipping 11 matching lines...) Expand all Loading... | |
| 271 env, android_webview::kAndroidAssetPath).Release(); | 266 env, android_webview::kAndroidAssetPath).Release(); |
| 272 } | 267 } |
| 273 | 268 |
| 274 static jstring GetAndroidResourcePath(JNIEnv* env, jclass /*clazz*/) { | 269 static jstring GetAndroidResourcePath(JNIEnv* env, jclass /*clazz*/) { |
| 275 // OK to release, JNI binding. | 270 // OK to release, JNI binding. |
| 276 return ConvertUTF8ToJavaString( | 271 return ConvertUTF8ToJavaString( |
| 277 env, android_webview::kAndroidResourcePath).Release(); | 272 env, android_webview::kAndroidResourcePath).Release(); |
| 278 } | 273 } |
| 279 | 274 |
| 280 } // namespace android_webview | 275 } // namespace android_webview |
| OLD | NEW |