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 "content/public/browser/browser_thread.h" | |
| 15 #include "googleurl/src/gurl.h" | 16 #include "googleurl/src/gurl.h" |
| 16 #include "jni/AndroidProtocolAdapter_jni.h" | 17 #include "jni/AndroidProtocolAdapter_jni.h" |
| 17 #include "net/base/io_buffer.h" | 18 #include "net/base/io_buffer.h" |
| 18 #include "net/base/mime_util.h" | 19 #include "net/base/mime_util.h" |
| 19 #include "net/base/net_errors.h" | 20 #include "net/base/net_errors.h" |
| 20 #include "net/base/net_util.h" | 21 #include "net/base/net_util.h" |
| 21 #include "net/http/http_util.h" | 22 #include "net/http/http_util.h" |
| 22 #include "net/url_request/url_request_error_job.h" | 23 #include "net/url_request/url_request_context.h" |
| 23 #include "net/url_request/url_request_file_job.h" | 24 #include "net/url_request/url_request_context_getter.h" |
| 25 #include "net/url_request/url_request_job_factory.h" | |
| 24 #include "net/url_request/url_request_job_manager.h" | 26 #include "net/url_request/url_request_job_manager.h" |
| 25 | 27 |
| 26 using base::android::AttachCurrentThread; | 28 using base::android::AttachCurrentThread; |
| 27 using base::android::ClearException; | 29 using base::android::ClearException; |
| 28 using base::android::ConvertUTF8ToJavaString; | 30 using base::android::ConvertUTF8ToJavaString; |
| 29 using base::android::ScopedJavaGlobalRef; | 31 using base::android::ScopedJavaGlobalRef; |
| 30 using base::android::ScopedJavaLocalRef; | 32 using base::android::ScopedJavaLocalRef; |
| 31 | 33 |
| 32 namespace { | 34 namespace { |
| 33 | 35 |
| (...skipping 23 matching lines...) Expand all Loading... | |
| 57 std::string* mime_type) OVERRIDE; | 59 std::string* mime_type) OVERRIDE; |
| 58 | 60 |
| 59 virtual bool GetCharset(JNIEnv* env, | 61 virtual bool GetCharset(JNIEnv* env, |
| 60 net::URLRequest* request, | 62 net::URLRequest* request, |
| 61 jobject stream, | 63 jobject stream, |
| 62 std::string* charset) OVERRIDE; | 64 std::string* charset) OVERRIDE; |
| 63 | 65 |
| 64 virtual ~AndroidStreamReaderURLRequestJobDelegateImpl(); | 66 virtual ~AndroidStreamReaderURLRequestJobDelegateImpl(); |
| 65 }; | 67 }; |
| 66 | 68 |
| 69 class AssetFileProtocolInterceptor : | |
| 70 public net::URLRequestJobFactory::Interceptor { | |
| 71 public: | |
| 72 AssetFileProtocolInterceptor(); | |
| 73 virtual ~AssetFileProtocolInterceptor() OVERRIDE; | |
| 74 virtual net::URLRequestJob* MaybeIntercept(net::URLRequest* request) const OVE RRIDE; | |
| 75 virtual net::URLRequestJob* MaybeInterceptRedirect( | |
| 76 const GURL& location, | |
| 77 net::URLRequest* request) const OVERRIDE; | |
| 78 virtual net::URLRequestJob* MaybeInterceptResponse( | |
| 79 net::URLRequest* request) const OVERRIDE; | |
| 80 | |
| 81 private: | |
| 82 const std::string kAssetPrefix; | |
| 83 const std::string kResourcePrefix; | |
| 84 }; | |
| 67 | 85 |
| 68 } // namespace | 86 } // namespace |
| 69 | 87 |
| 70 static bool InitJNIBindings(JNIEnv* env) { | 88 static bool InitJNIBindings(JNIEnv* env) { |
| 71 return RegisterNativesImpl(env) && | 89 return RegisterNativesImpl(env) && |
| 72 AndroidStreamReaderURLRequestJob::InitJNIBindings(env); | 90 AndroidStreamReaderURLRequestJob::InitJNIBindings(env); |
| 73 } | 91 } |
| 74 | 92 |
| 75 // static | 93 // static |
| 76 net::URLRequestJob* AndroidProtocolAdapter::Factory( | 94 net::URLRequestJob* AndroidProtocolAdapter::Factory( |
| 77 net::URLRequest* request, | 95 net::URLRequest* request, const std::string& scheme) { |
| 78 net::NetworkDelegate* network_delegate, | 96 DCHECK(scheme == chrome::kContentScheme); |
| 79 const std::string& scheme) { | |
| 80 DCHECK(scheme == chrome::kFileScheme || | |
| 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( | 97 return new AndroidStreamReaderURLRequestJob( |
| 99 request, | 98 request, |
| 100 network_delegate, | 99 network_delegate, |
|
joth
2012/08/24 17:28:28
did you mean this? Looks like a compile error.
mnaganov (inactive)
2012/08/24 18:17:22
The upstream android_stream_reader_url_request_job
joth
2012/08/24 23:22:27
OK you're passing |network_delegate| as param 2, b
| |
| 101 scoped_ptr<AndroidStreamReaderURLRequestJob::Delegate>( | 100 scoped_ptr<AndroidStreamReaderURLRequestJob::Delegate>( |
| 102 new AndroidStreamReaderURLRequestJobDelegateImpl())); | 101 new AndroidStreamReaderURLRequestJobDelegateImpl())); |
| 103 } | 102 } |
| 104 | 103 |
| 104 static void AddFileSchemeInterceptorOnIOThread( | |
| 105 net::URLRequestContextGetter* context_getter) { | |
| 106 // The job factory takes ownership of the interceptor. | |
| 107 const_cast<net::URLRequestJobFactory*>( | |
| 108 context_getter->GetURLRequestContext()->job_factory())->AddInterceptor( | |
| 109 new AssetFileProtocolInterceptor()); | |
| 110 } | |
| 111 | |
| 105 // static | 112 // static |
| 106 bool AndroidProtocolAdapter::RegisterProtocols(JNIEnv* env) { | 113 void AndroidProtocolAdapter::RegisterProtocols( |
| 114 JNIEnv* env, net::URLRequestContextGetter* context) { | |
|
joth
2012/08/24 17:28:28
nit: context => context_getter.
mnaganov (inactive)
2012/08/24 18:17:22
Done.
| |
| 107 DCHECK(env); | 115 DCHECK(env); |
| 116 InitJNIBindings(env); | |
| 108 | 117 |
| 109 if (!InitJNIBindings(env)) | 118 // 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 | 119 // registered here, it cannot be used by child processes until access to it is |
| 114 // granted via ChildProcessSecurityPolicy::GrantScheme(). This is done in | 120 // granted via ChildProcessSecurityPolicy::GrantScheme(). This is done in |
| 115 // RenderViewHost. | 121 // RenderViewHost. |
| 122 // | |
| 123 // TODO(mnaganov): Convert into a ProtocolHandler. | |
| 116 net::URLRequestJobManager* job_manager = | 124 net::URLRequestJobManager* job_manager = |
| 117 net::URLRequestJobManager::GetInstance(); | 125 net::URLRequestJobManager::GetInstance(); |
| 118 job_manager->RegisterProtocolFactory(chrome::kContentScheme, | 126 job_manager->RegisterProtocolFactory(chrome::kContentScheme, |
| 119 &AndroidProtocolAdapter::Factory); | 127 &AndroidProtocolAdapter::Factory); |
| 120 job_manager->RegisterProtocolFactory( | |
| 121 chrome::kFileScheme, &AndroidProtocolAdapter::Factory); | |
| 122 | 128 |
| 123 return true; | 129 // Register a file: scheme interceptor for application assets. |
| 130 content::BrowserThread::PostTask( | |
|
joth
2012/08/24 17:28:28
I think the modern style for this is:
context_get
mnaganov (inactive)
2012/08/24 18:17:22
OK, changed.
| |
| 131 content::BrowserThread::IO, FROM_HERE, | |
| 132 base::Bind(&AddFileSchemeInterceptorOnIOThread, | |
| 133 base::Unretained(context))); | |
|
joth
2012/08/24 17:28:28
I believe the Unretained() shouldn't be needed as
mnaganov (inactive)
2012/08/24 18:17:22
It is ref-counted, but the profile returns a raw p
joth
2012/08/24 23:22:27
make_scoped_refptr(foo) seems no more onerous than
| |
| 134 // TODO(mnaganov): Add an interceptor for the incognito profile? | |
| 124 } | 135 } |
| 125 | 136 |
| 126 // Set a context object to be used for resolving resource queries. This can | 137 // 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 | 138 // 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 | 139 // resource queries to a specific context object, e.g., for the purposes of |
| 129 // testing. | 140 // testing. |
| 130 // | 141 // |
| 131 // |context| should be a android.content.Context instance or NULL to enable | 142 // |context| should be a android.content.Context instance or NULL to enable |
| 132 // the use of the standard application context. | 143 // the use of the standard application context. |
| 133 static void SetResourceContextForTesting(JNIEnv* env, jclass /*clazz*/, | 144 static void SetResourceContextForTesting(JNIEnv* env, jclass /*clazz*/, |
| (...skipping 84 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 218 } | 229 } |
| 219 | 230 |
| 220 bool AndroidStreamReaderURLRequestJobDelegateImpl::GetCharset( | 231 bool AndroidStreamReaderURLRequestJobDelegateImpl::GetCharset( |
| 221 JNIEnv* env, | 232 JNIEnv* env, |
| 222 net::URLRequest* request, | 233 net::URLRequest* request, |
| 223 jobject stream, | 234 jobject stream, |
| 224 std::string* charset) { | 235 std::string* charset) { |
| 225 // TODO: We should probably be getting this from the managed side. | 236 // TODO: We should probably be getting this from the managed side. |
| 226 return false; | 237 return false; |
| 227 } | 238 } |
| 239 | |
| 240 AssetFileProtocolInterceptor::AssetFileProtocolInterceptor() : | |
| 241 kAssetPrefix(std::string(chrome::kFileScheme) + | |
| 242 std::string(content::kStandardSchemeSeparator) + | |
| 243 chrome::kAndroidAssetPath), | |
| 244 kResourcePrefix(std::string(chrome::kFileScheme) + | |
| 245 std::string(content::kStandardSchemeSeparator) + | |
| 246 chrome::kAndroidResourcePath) { | |
| 247 } | |
| 248 | |
| 249 AssetFileProtocolInterceptor::~AssetFileProtocolInterceptor() { | |
| 250 } | |
| 251 | |
| 252 net::URLRequestJob* AssetFileProtocolInterceptor::MaybeIntercept( | |
| 253 net::URLRequest* request) const { | |
| 254 if (!request->url().SchemeIsFile()) return NULL; | |
| 255 | |
| 256 const std::string& url = request->url().spec(); | |
| 257 if (!StartsWithASCII(url, kAssetPrefix, /*case_sensitive=*/ true) && | |
| 258 !StartsWithASCII(url, kResourcePrefix, /*case_sensitive=*/ true)) { | |
| 259 return NULL; | |
| 260 } | |
| 261 | |
| 262 return new AndroidStreamReaderURLRequestJob( | |
| 263 request, | |
| 264 scoped_ptr<AndroidStreamReaderURLRequestJob::Delegate>( | |
| 265 new AndroidStreamReaderURLRequestJobDelegateImpl())); | |
| 266 } | |
| 267 | |
| 268 net::URLRequestJob* AssetFileProtocolInterceptor::MaybeInterceptRedirect( | |
| 269 const GURL& location, | |
| 270 net::URLRequest* request) const { | |
| 271 return NULL; | |
| 272 } | |
| 273 | |
| 274 net::URLRequestJob* AssetFileProtocolInterceptor::MaybeInterceptResponse( | |
| 275 net::URLRequest* request) const { | |
| 276 return NULL; | |
| 277 } | |
| OLD | NEW |