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 // URL request job for reading from resources and assets. | 5 // URL request job for reading from resources and assets. |
| 6 | 6 |
| 7 #include "chrome/browser/android/android_protocol_adapter.h" | 7 #include "chrome/browser/android/android_protocol_adapter.h" |
| 8 | 8 |
| 9 #include "base/android/jni_android.h" | 9 #include "base/android/jni_android.h" |
| 10 #include "base/android/jni_helper.h" | 10 #include "base/android/jni_helper.h" |
| 11 #include "base/android/jni_string.h" | 11 #include "base/android/jni_string.h" |
| 12 #include "base/string_util.h" | 12 #include "base/string_util.h" |
| 13 #include "chrome/browser/android/android_stream_reader_url_request_job.h" | 13 #include "chrome/browser/android/android_stream_reader_url_request_job.h" |
| 14 #include "chrome/common/url_constants.h" | 14 #include "chrome/common/url_constants.h" |
| 15 #include "googleurl/src/gurl.h" | 15 #include "googleurl/src/gurl.h" |
| 16 #include "jni/AndroidProtocolAdapter_jni.h" | 16 #include "jni/AndroidProtocolAdapter_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/url_request_error_job.h" | 22 #include "net/url_request/url_request_context.h" |
| 23 #include "net/url_request/url_request_file_job.h" | 23 #include "net/url_request/url_request_context_getter.h" |
| 24 #include "net/url_request/url_request_job_factory.h" | |
| 24 #include "net/url_request/url_request_job_manager.h" | 25 #include "net/url_request/url_request_job_manager.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 { |
| 33 | 34 |
| (...skipping 23 matching lines...) Expand all Loading... | |
| 57 std::string* mime_type) OVERRIDE; | 58 std::string* mime_type) OVERRIDE; |
| 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 |
| 68 class AssetFileProtocolInterceptor : | |
| 69 public net::URLRequestJobFactory::Interceptor { | |
| 70 public: | |
| 71 AssetFileProtocolInterceptor(); | |
| 72 virtual ~AssetFileProtocolInterceptor() OVERRIDE; | |
| 73 virtual net::URLRequestJob* MaybeIntercept( | |
| 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, | |
| 82 net::NetworkDelegate* network_delegate) const OVERRIDE; | |
| 83 | |
| 84 private: | |
| 85 const std::string kAssetPrefix; | |
| 86 const std::string kResourcePrefix; | |
| 87 }; | |
| 67 | 88 |
| 68 } // namespace | 89 } // namespace |
| 69 | 90 |
| 70 static bool InitJNIBindings(JNIEnv* env) { | 91 static bool InitJNIBindings(JNIEnv* env) { |
| 71 return RegisterNativesImpl(env) && | 92 return RegisterNativesImpl(env) && |
| 72 AndroidStreamReaderURLRequestJob::InitJNIBindings(env); | 93 AndroidStreamReaderURLRequestJob::InitJNIBindings(env); |
| 73 } | 94 } |
| 74 | 95 |
| 75 // static | 96 // static |
| 76 net::URLRequestJob* AndroidProtocolAdapter::Factory( | 97 net::URLRequestJob* AndroidProtocolAdapter::Factory( |
| 77 net::URLRequest* request, | 98 net::URLRequest* request, |
| 78 net::NetworkDelegate* network_delegate, | 99 net::NetworkDelegate* network_delegate, |
| 79 const std::string& scheme) { | 100 const std::string& scheme) { |
| 80 DCHECK(scheme == chrome::kFileScheme || | 101 DCHECK(scheme == chrome::kContentScheme); |
| 81 scheme == chrome::kContentScheme); | |
| 82 JNIEnv* env = AttachCurrentThread(); | |
| 83 DCHECK(env); | |
| 84 // If this is a file:// URL we cannot handle, fall back to the default | |
| 85 // handler. | |
| 86 const std::string& url = request->url().spec(); | |
| 87 std::string assetPrefix = std::string(chrome::kFileScheme) + "://" + | |
| 88 chrome::kAndroidAssetPath; | |
| 89 std::string resourcePrefix = std::string(chrome::kFileScheme) + "://" + | |
| 90 chrome::kAndroidResourcePath; | |
| 91 | |
| 92 if (scheme == chrome::kFileScheme && | |
| 93 !StartsWithASCII(url, assetPrefix, /*case_sensitive=*/ true) && | |
| 94 !StartsWithASCII(url, resourcePrefix, /*case_sensitive=*/ true)) { | |
| 95 return net::URLRequestFileJob::Factory(request, network_delegate, scheme); | |
| 96 } | |
| 97 | |
| 98 return new AndroidStreamReaderURLRequestJob( | 102 return new AndroidStreamReaderURLRequestJob( |
| 99 request, | 103 request, |
| 100 network_delegate, | 104 network_delegate, |
| 101 scoped_ptr<AndroidStreamReaderURLRequestJob::Delegate>( | 105 scoped_ptr<AndroidStreamReaderURLRequestJob::Delegate>( |
| 102 new AndroidStreamReaderURLRequestJobDelegateImpl())); | 106 new AndroidStreamReaderURLRequestJobDelegateImpl())); |
| 103 } | 107 } |
| 104 | 108 |
| 109 static void AddFileSchemeInterceptorOnIOThread( | |
| 110 net::URLRequestContextGetter* context_getter) { | |
| 111 // The job factory takes ownership of the interceptor. | |
| 112 const_cast<net::URLRequestJobFactory*>( | |
| 113 context_getter->GetURLRequestContext()->job_factory())->AddInterceptor( | |
| 114 new AssetFileProtocolInterceptor()); | |
| 115 } | |
| 116 | |
| 105 // static | 117 // static |
| 106 bool AndroidProtocolAdapter::RegisterProtocols(JNIEnv* env) { | 118 void AndroidProtocolAdapter::RegisterProtocols( |
| 119 JNIEnv* env, net::URLRequestContextGetter* context_getter) { | |
| 107 DCHECK(env); | 120 DCHECK(env); |
| 121 if (!InitJNIBindings(env)) { | |
| 122 // Must not fail. | |
| 123 NOTREACHED(); | |
| 124 } | |
| 108 | 125 |
| 109 if (!InitJNIBindings(env)) | 126 // Register content://. Note that even though a scheme is |
| 110 return false; | |
| 111 | |
| 112 // Register content:// and file://. Note that even though a scheme is | |
| 113 // registered here, it cannot be used by child processes until access to it is | 127 // registered here, it cannot be used by child processes until access to it is |
| 114 // granted via ChildProcessSecurityPolicy::GrantScheme(). This is done in | 128 // granted via ChildProcessSecurityPolicy::GrantScheme(). This is done in |
| 115 // RenderViewHost. | 129 // RenderViewHost. |
| 130 // | |
| 131 // TODO(mnaganov): Convert into a ProtocolHandler. | |
| 116 net::URLRequestJobManager* job_manager = | 132 net::URLRequestJobManager* job_manager = |
| 117 net::URLRequestJobManager::GetInstance(); | 133 net::URLRequestJobManager::GetInstance(); |
| 118 job_manager->RegisterProtocolFactory(chrome::kContentScheme, | 134 job_manager->RegisterProtocolFactory(chrome::kContentScheme, |
| 119 &AndroidProtocolAdapter::Factory); | 135 &AndroidProtocolAdapter::Factory); |
| 120 job_manager->RegisterProtocolFactory( | |
| 121 chrome::kFileScheme, &AndroidProtocolAdapter::Factory); | |
| 122 | 136 |
| 123 return true; | 137 // Register a file: scheme interceptor for application assets. |
| 138 context_getter->GetNetworkTaskRunner()->PostTask( | |
| 139 FROM_HERE, | |
| 140 base::Bind(&AddFileSchemeInterceptorOnIOThread, | |
| 141 make_scoped_refptr(context_getter))); | |
| 142 // TODO(mnaganov): Add an interceptor for the incognito profile? | |
| 124 } | 143 } |
| 125 | 144 |
| 126 // Set a context object to be used for resolving resource queries. This can | 145 // Set a context object to be used for resolving resource queries. This can |
| 127 // be used to override the default application context and redirect all | 146 // be used to override the default application context and redirect all |
| 128 // resource queries to a specific context object, e.g., for the purposes of | 147 // resource queries to a specific context object, e.g., for the purposes of |
| 129 // testing. | 148 // testing. |
| 130 // | 149 // |
| 131 // |context| should be a android.content.Context instance or NULL to enable | 150 // |context| should be a android.content.Context instance or NULL to enable |
| 132 // the use of the standard application context. | 151 // the use of the standard application context. |
| 133 static void SetResourceContextForTesting(JNIEnv* env, jclass /*clazz*/, | 152 static void SetResourceContextForTesting(JNIEnv* env, jclass /*clazz*/, |
| (...skipping 84 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 218 } | 237 } |
| 219 | 238 |
| 220 bool AndroidStreamReaderURLRequestJobDelegateImpl::GetCharset( | 239 bool AndroidStreamReaderURLRequestJobDelegateImpl::GetCharset( |
| 221 JNIEnv* env, | 240 JNIEnv* env, |
| 222 net::URLRequest* request, | 241 net::URLRequest* request, |
| 223 jobject stream, | 242 jobject stream, |
| 224 std::string* charset) { | 243 std::string* charset) { |
| 225 // TODO: We should probably be getting this from the managed side. | 244 // TODO: We should probably be getting this from the managed side. |
| 226 return false; | 245 return false; |
| 227 } | 246 } |
| 247 | |
| 248 AssetFileProtocolInterceptor::AssetFileProtocolInterceptor() : | |
| 249 kAssetPrefix(std::string(chrome::kFileScheme) + | |
|
joth
2012/08/29 19:49:33
nit: initializer colon should be on this line
mnaganov (inactive)
2012/08/30 09:24:38
Done.
| |
| 250 std::string(content::kStandardSchemeSeparator) + | |
| 251 chrome::kAndroidAssetPath), | |
| 252 kResourcePrefix(std::string(chrome::kFileScheme) + | |
|
joth
2012/08/29 19:49:33
nit: I think technically kConst is only for things
mnaganov (inactive)
2012/08/30 09:24:38
Agree. Changed to asset_prefix_ and resource_prefi
joth
2012/08/30 18:10:40
Ah I wasn't meaning to suggest removing constants,
| |
| 253 std::string(content::kStandardSchemeSeparator) + | |
| 254 chrome::kAndroidResourcePath) { | |
| 255 } | |
| 256 | |
| 257 AssetFileProtocolInterceptor::~AssetFileProtocolInterceptor() { | |
| 258 } | |
| 259 | |
| 260 net::URLRequestJob* AssetFileProtocolInterceptor::MaybeIntercept( | |
| 261 net::URLRequest* request, net::NetworkDelegate* network_delegate) const { | |
| 262 if (!request->url().SchemeIsFile()) return NULL; | |
| 263 | |
| 264 const std::string& url = request->url().spec(); | |
| 265 if (!StartsWithASCII(url, kAssetPrefix, /*case_sensitive=*/ true) && | |
| 266 !StartsWithASCII(url, kResourcePrefix, /*case_sensitive=*/ true)) { | |
| 267 return NULL; | |
| 268 } | |
| 269 | |
| 270 return new AndroidStreamReaderURLRequestJob( | |
| 271 request, | |
| 272 network_delegate, | |
| 273 scoped_ptr<AndroidStreamReaderURLRequestJob::Delegate>( | |
| 274 new AndroidStreamReaderURLRequestJobDelegateImpl())); | |
| 275 } | |
| 276 | |
| 277 net::URLRequestJob* AssetFileProtocolInterceptor::MaybeInterceptRedirect( | |
| 278 const GURL& location, | |
| 279 net::URLRequest* request, | |
| 280 net::NetworkDelegate* network_delegate) const { | |
| 281 return NULL; | |
| 282 } | |
| 283 | |
| 284 net::URLRequestJob* AssetFileProtocolInterceptor::MaybeInterceptResponse( | |
| 285 net::URLRequest* request, | |
| 286 net::NetworkDelegate* network_delegate) const { | |
| 287 return NULL; | |
| 288 } | |
| OLD | NEW |