Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(389)

Side by Side Diff: chrome/browser/android/android_protocol_adapter.cc

Issue 10880025: [Android] Fix handling of asset files loading. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Comments addressed Created 8 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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
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(net::URLRequest* request) const OVE RRIDE;
Sami 2012/08/28 11:10:50 Line length?
mnaganov (inactive) 2012/08/28 13:08:00 Done.
74 virtual net::URLRequestJob* MaybeInterceptRedirect(
75 const GURL& location,
76 net::URLRequest* request) const OVERRIDE;
77 virtual net::URLRequestJob* MaybeInterceptResponse(
78 net::URLRequest* request) const OVERRIDE;
79
80 private:
81 const std::string kAssetPrefix;
82 const std::string kResourcePrefix;
83 };
67 84
68 } // namespace 85 } // namespace
69 86
70 static bool InitJNIBindings(JNIEnv* env) { 87 static bool InitJNIBindings(JNIEnv* env) {
71 return RegisterNativesImpl(env) && 88 return RegisterNativesImpl(env) &&
72 AndroidStreamReaderURLRequestJob::InitJNIBindings(env); 89 AndroidStreamReaderURLRequestJob::InitJNIBindings(env);
73 } 90 }
74 91
75 // static 92 // static
76 net::URLRequestJob* AndroidProtocolAdapter::Factory( 93 net::URLRequestJob* AndroidProtocolAdapter::Factory(
77 net::URLRequest* request, 94 net::URLRequest* request, const std::string& scheme) {
78 net::NetworkDelegate* network_delegate, 95 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( 96 return new AndroidStreamReaderURLRequestJob(
99 request, 97 request,
100 network_delegate, 98 network_delegate,
101 scoped_ptr<AndroidStreamReaderURLRequestJob::Delegate>( 99 scoped_ptr<AndroidStreamReaderURLRequestJob::Delegate>(
102 new AndroidStreamReaderURLRequestJobDelegateImpl())); 100 new AndroidStreamReaderURLRequestJobDelegateImpl()));
103 } 101 }
104 102
103 static void AddFileSchemeInterceptorOnIOThread(
104 net::URLRequestContextGetter* context_getter) {
105 // The job factory takes ownership of the interceptor.
106 const_cast<net::URLRequestJobFactory*>(
107 context_getter->GetURLRequestContext()->job_factory())->AddInterceptor(
108 new AssetFileProtocolInterceptor());
109 }
110
105 // static 111 // static
106 bool AndroidProtocolAdapter::RegisterProtocols(JNIEnv* env) { 112 void AndroidProtocolAdapter::RegisterProtocols(
113 JNIEnv* env, net::URLRequestContextGetter* context_getter) {
107 DCHECK(env); 114 DCHECK(env);
115 InitJNIBindings(env);
Sami 2012/08/28 11:10:50 Maybe add a DCHECK for the result here? Otherwise
mnaganov (inactive) 2012/08/28 13:08:00 Good idea. But wrapping the in a DCHECK will mean
108 116
109 if (!InitJNIBindings(env)) 117 // 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 118 // registered here, it cannot be used by child processes until access to it is
114 // granted via ChildProcessSecurityPolicy::GrantScheme(). This is done in 119 // granted via ChildProcessSecurityPolicy::GrantScheme(). This is done in
115 // RenderViewHost. 120 // RenderViewHost.
121 //
122 // TODO(mnaganov): Convert into a ProtocolHandler.
116 net::URLRequestJobManager* job_manager = 123 net::URLRequestJobManager* job_manager =
117 net::URLRequestJobManager::GetInstance(); 124 net::URLRequestJobManager::GetInstance();
118 job_manager->RegisterProtocolFactory(chrome::kContentScheme, 125 job_manager->RegisterProtocolFactory(chrome::kContentScheme,
119 &AndroidProtocolAdapter::Factory); 126 &AndroidProtocolAdapter::Factory);
120 job_manager->RegisterProtocolFactory(
121 chrome::kFileScheme, &AndroidProtocolAdapter::Factory);
122 127
123 return true; 128 // Register a file: scheme interceptor for application assets.
129 context_getter->GetNetworkTaskRunner()->PostTask(
130 FROM_HERE,
131 base::Bind(&AddFileSchemeInterceptorOnIOThread,
132 base::Unretained(context_getter)));
133 // TODO(mnaganov): Add an interceptor for the incognito profile?
124 } 134 }
125 135
126 // Set a context object to be used for resolving resource queries. This can 136 // 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 137 // 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 138 // resource queries to a specific context object, e.g., for the purposes of
129 // testing. 139 // testing.
130 // 140 //
131 // |context| should be a android.content.Context instance or NULL to enable 141 // |context| should be a android.content.Context instance or NULL to enable
132 // the use of the standard application context. 142 // the use of the standard application context.
133 static void SetResourceContextForTesting(JNIEnv* env, jclass /*clazz*/, 143 static void SetResourceContextForTesting(JNIEnv* env, jclass /*clazz*/,
(...skipping 84 matching lines...) Expand 10 before | Expand all | Expand 10 after
218 } 228 }
219 229
220 bool AndroidStreamReaderURLRequestJobDelegateImpl::GetCharset( 230 bool AndroidStreamReaderURLRequestJobDelegateImpl::GetCharset(
221 JNIEnv* env, 231 JNIEnv* env,
222 net::URLRequest* request, 232 net::URLRequest* request,
223 jobject stream, 233 jobject stream,
224 std::string* charset) { 234 std::string* charset) {
225 // TODO: We should probably be getting this from the managed side. 235 // TODO: We should probably be getting this from the managed side.
226 return false; 236 return false;
227 } 237 }
238
239 AssetFileProtocolInterceptor::AssetFileProtocolInterceptor() :
240 kAssetPrefix(std::string(chrome::kFileScheme) +
241 std::string(content::kStandardSchemeSeparator) +
242 chrome::kAndroidAssetPath),
243 kResourcePrefix(std::string(chrome::kFileScheme) +
244 std::string(content::kStandardSchemeSeparator) +
245 chrome::kAndroidResourcePath) {
246 }
247
248 AssetFileProtocolInterceptor::~AssetFileProtocolInterceptor() {
249 }
250
251 net::URLRequestJob* AssetFileProtocolInterceptor::MaybeIntercept(
252 net::URLRequest* request) const {
253 if (!request->url().SchemeIsFile()) return NULL;
254
255 const std::string& url = request->url().spec();
256 if (!StartsWithASCII(url, kAssetPrefix, /*case_sensitive=*/ true) &&
257 !StartsWithASCII(url, kResourcePrefix, /*case_sensitive=*/ true)) {
258 return NULL;
259 }
260
261 return new AndroidStreamReaderURLRequestJob(
262 request,
263 scoped_ptr<AndroidStreamReaderURLRequestJob::Delegate>(
264 new AndroidStreamReaderURLRequestJobDelegateImpl()));
265 }
266
267 net::URLRequestJob* AssetFileProtocolInterceptor::MaybeInterceptRedirect(
268 const GURL& location,
269 net::URLRequest* request) const {
270 return NULL;
271 }
272
273 net::URLRequestJob* AssetFileProtocolInterceptor::MaybeInterceptResponse(
274 net::URLRequest* request) const {
275 return NULL;
276 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698