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

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: 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/browser/profiles/profile_manager.h"
14 #include "chrome/common/url_constants.h" 15 #include "chrome/common/url_constants.h"
joth 2012/08/23 18:23:28 prefer to be removing chrome/ deps from here rathe
mnaganov (inactive) 2012/08/24 12:53:05 Done.
16 #include "content/public/browser/browser_thread.h"
15 #include "googleurl/src/gurl.h" 17 #include "googleurl/src/gurl.h"
16 #include "jni/AndroidProtocolAdapter_jni.h" 18 #include "jni/AndroidProtocolAdapter_jni.h"
17 #include "net/base/io_buffer.h" 19 #include "net/base/io_buffer.h"
18 #include "net/base/mime_util.h" 20 #include "net/base/mime_util.h"
19 #include "net/base/net_errors.h" 21 #include "net/base/net_errors.h"
20 #include "net/base/net_util.h" 22 #include "net/base/net_util.h"
21 #include "net/http/http_util.h" 23 #include "net/http/http_util.h"
22 #include "net/url_request/url_request_error_job.h" 24 #include "net/url_request/url_request_context.h"
23 #include "net/url_request/url_request_file_job.h" 25 #include "net/url_request/url_request_context_getter.h"
26 #include "net/url_request/url_request_job_factory.h"
24 #include "net/url_request/url_request_job_manager.h" 27 #include "net/url_request/url_request_job_manager.h"
25 28
26 using base::android::AttachCurrentThread; 29 using base::android::AttachCurrentThread;
27 using base::android::ClearException; 30 using base::android::ClearException;
28 using base::android::ConvertUTF8ToJavaString; 31 using base::android::ConvertUTF8ToJavaString;
29 using base::android::ScopedJavaGlobalRef; 32 using base::android::ScopedJavaGlobalRef;
30 using base::android::ScopedJavaLocalRef; 33 using base::android::ScopedJavaLocalRef;
31 34
32 namespace { 35 namespace {
33 36
(...skipping 23 matching lines...) Expand all
57 std::string* mime_type) OVERRIDE; 60 std::string* mime_type) OVERRIDE;
58 61
59 virtual bool GetCharset(JNIEnv* env, 62 virtual bool GetCharset(JNIEnv* env,
60 net::URLRequest* request, 63 net::URLRequest* request,
61 jobject stream, 64 jobject stream,
62 std::string* charset) OVERRIDE; 65 std::string* charset) OVERRIDE;
63 66
64 virtual ~AndroidStreamReaderURLRequestJobDelegateImpl(); 67 virtual ~AndroidStreamReaderURLRequestJobDelegateImpl();
65 }; 68 };
66 69
70 class AssetFileProtocolInterceptor :
71 public net::URLRequestJobFactory::Interceptor {
72 public:
73 AssetFileProtocolInterceptor();
74 virtual ~AssetFileProtocolInterceptor() OVERRIDE;
75 virtual net::URLRequestJob* MaybeIntercept(net::URLRequest* request) const OVE RRIDE;
76 virtual net::URLRequestJob* MaybeInterceptRedirect(
77 const GURL& location,
78 net::URLRequest* request) const OVERRIDE;
79 virtual net::URLRequestJob* MaybeInterceptResponse(
80 net::URLRequest* request) const OVERRIDE;
81
82 private:
83 const std::string kAssetPrefix;
84 const std::string kResourcePrefix;
85 };
67 86
68 } // namespace 87 } // namespace
69 88
70 static bool InitJNIBindings(JNIEnv* env) { 89 static bool InitJNIBindings(JNIEnv* env) {
71 return RegisterNativesImpl(env) && 90 return RegisterNativesImpl(env) &&
72 AndroidStreamReaderURLRequestJob::InitJNIBindings(env); 91 AndroidStreamReaderURLRequestJob::InitJNIBindings(env);
73 } 92 }
74 93
75 // static 94 // static
76 net::URLRequestJob* AndroidProtocolAdapter::Factory( 95 net::URLRequestJob* AndroidProtocolAdapter::Factory(
77 net::URLRequest* request, const std::string& scheme) { 96 net::URLRequest* request, const std::string& scheme) {
78 DCHECK(scheme == chrome::kFileScheme || 97 DCHECK(scheme == chrome::kContentScheme);
79 scheme == chrome::kContentScheme);
80 JNIEnv* env = AttachCurrentThread();
81 DCHECK(env);
82 // If this is a file:// URL we cannot handle, fall back to the default
83 // handler.
84 const std::string& url = request->url().spec();
85 std::string assetPrefix = std::string(chrome::kFileScheme) + "://" +
86 chrome::kAndroidAssetPath;
87 std::string resourcePrefix = std::string(chrome::kFileScheme) + "://" +
88 chrome::kAndroidResourcePath;
89
90 if (scheme == chrome::kFileScheme &&
91 !StartsWithASCII(url, assetPrefix, /*case_sensitive=*/ true) &&
92 !StartsWithASCII(url, resourcePrefix, /*case_sensitive=*/ true)) {
93 return net::URLRequestFileJob::Factory(request, scheme);
94 }
95
96 return new AndroidStreamReaderURLRequestJob( 98 return new AndroidStreamReaderURLRequestJob(
97 request, 99 request,
98 scoped_ptr<AndroidStreamReaderURLRequestJob::Delegate>( 100 scoped_ptr<AndroidStreamReaderURLRequestJob::Delegate>(
99 new AndroidStreamReaderURLRequestJobDelegateImpl())); 101 new AndroidStreamReaderURLRequestJobDelegateImpl()));
100 } 102 }
101 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
102 // static 112 // static
103 bool AndroidProtocolAdapter::RegisterProtocols(JNIEnv* env) { 113 bool AndroidProtocolAdapter::RegisterProtocols(JNIEnv* env) {
104 DCHECK(env); 114 DCHECK(env);
105 115
106 if (!InitJNIBindings(env)) 116 if (!InitJNIBindings(env))
107 return false; 117 return false;
joth 2012/08/23 18:23:28 out of scope for this CL, but this should never fa
mnaganov (inactive) 2012/08/24 12:53:05 Removed.
108 118
109 // Register content:// and file://. Note that even though a scheme is 119 // Register content://. Note that even though a scheme is
110 // registered here, it cannot be used by child processes until access to it is 120 // registered here, it cannot be used by child processes until access to it is
111 // granted via ChildProcessSecurityPolicy::GrantScheme(). This is done in 121 // granted via ChildProcessSecurityPolicy::GrantScheme(). This is done in
112 // RenderViewHost. 122 // RenderViewHost.
123 //
124 // TODO(mnaganov): Convert into a ProtocolHandler.
113 net::URLRequestJobManager* job_manager = 125 net::URLRequestJobManager* job_manager =
114 net::URLRequestJobManager::GetInstance(); 126 net::URLRequestJobManager::GetInstance();
115 job_manager->RegisterProtocolFactory(chrome::kContentScheme, 127 job_manager->RegisterProtocolFactory(chrome::kContentScheme,
116 &AndroidProtocolAdapter::Factory); 128 &AndroidProtocolAdapter::Factory);
117 job_manager->RegisterProtocolFactory( 129
118 chrome::kFileScheme, &AndroidProtocolAdapter::Factory); 130 // Register a file: scheme interceptor for application assets.
131 net::URLRequestContextGetter* context_getter =
132 ProfileManager::GetDefaultProfile()->GetRequestContext();
joth 2012/08/23 18:23:28 could you have the context_getter passed in as a p
mnaganov (inactive) 2012/08/24 12:53:05 Done.
133 content::BrowserThread::PostTask(
134 content::BrowserThread::IO, FROM_HERE,
135 base::Bind(&AddFileSchemeInterceptorOnIOThread,
136 base::Unretained(context_getter)));
137 // TODO(mnaganov): Add an interceptor for the incognito profile?
119 138
120 return true; 139 return true;
121 } 140 }
122 141
123 // Set a context object to be used for resolving resource queries. This can 142 // Set a context object to be used for resolving resource queries. This can
124 // be used to override the default application context and redirect all 143 // be used to override the default application context and redirect all
125 // resource queries to a specific context object, e.g., for the purposes of 144 // resource queries to a specific context object, e.g., for the purposes of
126 // testing. 145 // testing.
127 // 146 //
128 // |context| should be a android.content.Context instance or NULL to enable 147 // |context| should be a android.content.Context instance or NULL to enable
(...skipping 86 matching lines...) Expand 10 before | Expand all | Expand 10 after
215 } 234 }
216 235
217 bool AndroidStreamReaderURLRequestJobDelegateImpl::GetCharset( 236 bool AndroidStreamReaderURLRequestJobDelegateImpl::GetCharset(
218 JNIEnv* env, 237 JNIEnv* env,
219 net::URLRequest* request, 238 net::URLRequest* request,
220 jobject stream, 239 jobject stream,
221 std::string* charset) { 240 std::string* charset) {
222 // TODO: We should probably be getting this from the managed side. 241 // TODO: We should probably be getting this from the managed side.
223 return false; 242 return false;
224 } 243 }
244
245 AssetFileProtocolInterceptor::AssetFileProtocolInterceptor() :
246 kAssetPrefix(std::string(chrome::kFileScheme) +
247 std::string(content::kStandardSchemeSeparator) +
248 chrome::kAndroidAssetPath),
249 kResourcePrefix(std::string(chrome::kFileScheme) +
250 std::string(content::kStandardSchemeSeparator) +
251 chrome::kAndroidResourcePath) {
252 }
253
254 AssetFileProtocolInterceptor::~AssetFileProtocolInterceptor() {
255 }
256
257 net::URLRequestJob* AssetFileProtocolInterceptor::MaybeIntercept(
258 net::URLRequest* request) const {
259 if (!request->url().SchemeIsFile()) return NULL;
260
261 const std::string& url = request->url().spec();
262 if (!StartsWithASCII(url, kAssetPrefix, /*case_sensitive=*/ true) &&
263 !StartsWithASCII(url, kResourcePrefix, /*case_sensitive=*/ true)) {
264 return NULL;
265 }
266
267 return new AndroidStreamReaderURLRequestJob(
268 request,
269 scoped_ptr<AndroidStreamReaderURLRequestJob::Delegate>(
270 new AndroidStreamReaderURLRequestJobDelegateImpl()));
271 }
272
273 net::URLRequestJob* AssetFileProtocolInterceptor::MaybeInterceptRedirect(
274 const GURL& location,
275 net::URLRequest* request) const {
276 return NULL;
277 }
278
279 net::URLRequestJob* AssetFileProtocolInterceptor::MaybeInterceptResponse(
280 net::URLRequest* request) const {
281 return NULL;
282 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698