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
3 // found in the LICENSE file.
5 #include "xwalk/runtime/browser/android/net/android_protocol_handler.h"
9 #include "base/android/jni_android.h"
10 #include "base/android/jni_helper.h"
11 #include "base/android/jni_string.h"
12 #include "base/strings/string_util.h"
13 #include "content/public/common/url_constants.h"
14 #include "jni/AndroidProtocolHandler_jni.h"
15 #include "net/base/io_buffer.h"
16 #include "net/base/mime_util.h"
17 #include "net/base/net_errors.h"
18 #include "net/base/net_util.h"
19 #include "net/http/http_util.h"
20 #include "net/url_request/protocol_intercept_job_factory.h"
21 #include "net/url_request/url_request.h"
23 #include "xwalk/runtime/browser/android/net/android_stream_reader_url_request_job.h"
24 #include "xwalk/runtime/browser/android/net/input_stream_impl.h"
25 #include "xwalk/runtime/browser/android/net/url_constants.h"
26 #include "xwalk/runtime/browser/runtime_context.h"
27 #include "xwalk/runtime/browser/xwalk_runner.h"
29 using base::android::AttachCurrentThread;
30 using base::android::ClearException;
31 using base::android::ConvertUTF8ToJavaString;
32 using base::android::ScopedJavaGlobalRef;
33 using base::android::ScopedJavaLocalRef;
34 using xwalk::InputStream;
35 using xwalk::InputStreamImpl;
39 // Override resource context for reading resource and asset files. Used for
41 JavaObjectWeakGlobalRef* g_resource_context = NULL;
43 void ResetResourceContext(JavaObjectWeakGlobalRef* ref) {
44 if (g_resource_context)
45 delete g_resource_context;
47 g_resource_context = ref;
50 void* kPreviouslyFailedKey = &kPreviouslyFailedKey;
52 void MarkRequestAsFailed(net::URLRequest* request) {
53 request->SetUserData(kPreviouslyFailedKey,
54 new base::SupportsUserData::Data());
57 bool HasRequestPreviouslyFailed(net::URLRequest* request) {
58 return request->GetUserData(kPreviouslyFailedKey) != NULL;
61 class AndroidStreamReaderURLRequestJobDelegateImpl
62 : public AndroidStreamReaderURLRequestJob::Delegate {
64 AndroidStreamReaderURLRequestJobDelegateImpl();
66 virtual scoped_ptr<InputStream> OpenInputStream(
68 const GURL& url) OVERRIDE;
70 virtual void OnInputStreamOpenFailed(net::URLRequest* request,
71 bool* restart) OVERRIDE;
73 virtual bool GetMimeType(JNIEnv* env,
74 net::URLRequest* request,
76 std::string* mime_type) OVERRIDE;
78 virtual bool GetCharset(JNIEnv* env,
79 net::URLRequest* request,
81 std::string* charset) OVERRIDE;
83 virtual bool GetPackageName(JNIEnv* env,
84 std::string* name) OVERRIDE;
86 virtual ~AndroidStreamReaderURLRequestJobDelegateImpl();
89 class AndroidProtocolHandlerBase
90 : public net::URLRequestJobFactory::ProtocolHandler {
92 virtual net::URLRequestJob* MaybeCreateJob(
93 net::URLRequest* request,
94 net::NetworkDelegate* network_delegate) const OVERRIDE;
96 virtual bool CanHandleRequest(const net::URLRequest* request) const = 0;
99 class AssetFileProtocolHandler : public AndroidProtocolHandlerBase {
101 AssetFileProtocolHandler();
103 virtual ~AssetFileProtocolHandler() OVERRIDE;
104 virtual bool CanHandleRequest(const net::URLRequest* request) const OVERRIDE;
107 // file:///android_asset/
108 const std::string asset_prefix_;
109 // file:///android_res/
110 const std::string resource_prefix_;
113 // Protocol handler for app:// scheme requests.
114 class AppSchemeProtocolHandler : public AndroidProtocolHandlerBase {
116 AppSchemeProtocolHandler();
117 virtual bool CanHandleRequest(const net::URLRequest* request) const OVERRIDE;
120 // Protocol handler for content:// scheme requests.
121 class ContentSchemeProtocolHandler : public AndroidProtocolHandlerBase {
123 ContentSchemeProtocolHandler();
124 virtual bool CanHandleRequest(const net::URLRequest* request) const OVERRIDE;
127 static ScopedJavaLocalRef<jobject> GetResourceContext(JNIEnv* env) {
128 if (g_resource_context)
129 return g_resource_context->get(env);
130 ScopedJavaLocalRef<jobject> context;
131 // We have to reset as GetApplicationContext() returns a jobject with a
132 // global ref. The constructor that takes a jobject would expect a local ref
134 context.Reset(env, base::android::GetApplicationContext());
138 // AndroidStreamReaderURLRequestJobDelegateImpl -------------------------------
140 AndroidStreamReaderURLRequestJobDelegateImpl::
141 AndroidStreamReaderURLRequestJobDelegateImpl() {}
143 AndroidStreamReaderURLRequestJobDelegateImpl::
144 ~AndroidStreamReaderURLRequestJobDelegateImpl() {
147 scoped_ptr<InputStream>
148 AndroidStreamReaderURLRequestJobDelegateImpl::OpenInputStream(
149 JNIEnv* env, const GURL& url) {
150 DCHECK(url.is_valid());
153 // Open the input stream.
154 ScopedJavaLocalRef<jstring> jurl =
155 ConvertUTF8ToJavaString(env, url.spec());
156 ScopedJavaLocalRef<jobject> stream =
157 xwalk::Java_AndroidProtocolHandler_open(
159 GetResourceContext(env).obj(),
162 // Check and clear pending exceptions.
163 if (ClearException(env) || stream.is_null()) {
164 DLOG(ERROR) << "Unable to open input stream for Android URL";
165 return scoped_ptr<InputStream>();
167 return make_scoped_ptr<InputStream>(new InputStreamImpl(stream));
170 void AndroidStreamReaderURLRequestJobDelegateImpl::OnInputStreamOpenFailed(
171 net::URLRequest* request,
173 DCHECK(!HasRequestPreviouslyFailed(request));
174 MarkRequestAsFailed(request);
178 bool AndroidStreamReaderURLRequestJobDelegateImpl::GetMimeType(
180 net::URLRequest* request,
181 xwalk::InputStream* stream,
182 std::string* mime_type) {
187 // Query the mime type from the Java side. It is possible for the query to
188 // fail, as the mime type cannot be determined for all supported schemes.
189 ScopedJavaLocalRef<jstring> url =
190 ConvertUTF8ToJavaString(env, request->url().spec());
191 const InputStreamImpl* stream_impl =
192 InputStreamImpl::FromInputStream(stream);
193 ScopedJavaLocalRef<jstring> returned_type =
194 xwalk::Java_AndroidProtocolHandler_getMimeType(
196 GetResourceContext(env).obj(),
197 stream_impl->jobj(), url.obj());
198 if (ClearException(env) || returned_type.is_null())
201 *mime_type = base::android::ConvertJavaStringToUTF8(returned_type);
205 bool AndroidStreamReaderURLRequestJobDelegateImpl::GetCharset(
207 net::URLRequest* request,
208 xwalk::InputStream* stream,
209 std::string* charset) {
210 // TODO(shouqun): We should probably be getting this from the managed side.
214 bool AndroidStreamReaderURLRequestJobDelegateImpl::GetPackageName(
217 ScopedJavaLocalRef<jstring> returned_name =
218 xwalk::Java_AndroidProtocolHandler_getPackageName(
220 GetResourceContext(env).obj());
222 if (ClearException(env) || returned_name.is_null())
225 *name = base::android::ConvertJavaStringToUTF8(returned_name);
229 // AndroidProtocolHandlerBase -------------------------------------------------
231 net::URLRequestJob* AndroidProtocolHandlerBase::MaybeCreateJob(
232 net::URLRequest* request,
233 net::NetworkDelegate* network_delegate) const {
234 if (!CanHandleRequest(request)) return NULL;
236 // For WebViewClassic compatibility this job can only accept URLs that can be
237 // opened. URLs that cannot be opened should be resolved by the next handler.
239 // If a request is initially handled here but the job fails due to it being
240 // unable to open the InputStream for that request the request is marked as
241 // previously failed and restarted.
242 // Restarting a request involves creating a new job for that request. This
243 // handler will ignore requests know to have previously failed to 1) prevent
244 // an infinite loop, 2) ensure that the next handler in line gets the
245 // opportunity to create a job for the request.
246 if (HasRequestPreviouslyFailed(request)) return NULL;
248 scoped_ptr<AndroidStreamReaderURLRequestJobDelegateImpl> reader_delegate(
249 new AndroidStreamReaderURLRequestJobDelegateImpl());
251 xwalk::RuntimeContext* runtime_context =
252 xwalk::XWalkRunner::GetInstance()->runtime_context();
253 std::string content_security_policy = runtime_context->GetCSPString();
255 return new AndroidStreamReaderURLRequestJob(
258 reader_delegate.PassAs<AndroidStreamReaderURLRequestJob::Delegate>(),
259 content_security_policy);
262 // AssetFileProtocolHandler ---------------------------------------------------
264 AssetFileProtocolHandler::AssetFileProtocolHandler()
265 : asset_prefix_(std::string(content::kFileScheme) +
266 std::string(content::kStandardSchemeSeparator) +
267 xwalk::kAndroidAssetPath),
268 resource_prefix_(std::string(content::kFileScheme) +
269 std::string(content::kStandardSchemeSeparator) +
270 xwalk::kAndroidResourcePath) {
273 AssetFileProtocolHandler::~AssetFileProtocolHandler() {
276 bool AssetFileProtocolHandler::CanHandleRequest(
277 const net::URLRequest* request) const {
278 if (!request->url().SchemeIsFile())
281 const std::string& url = request->url().spec();
282 if (!StartsWithASCII(url, asset_prefix_, /*case_sensitive=*/ true) &&
283 !StartsWithASCII(url, resource_prefix_, /*case_sensitive=*/ true)) {
290 // ContentSchemeProtocolHandler
291 ContentSchemeProtocolHandler::ContentSchemeProtocolHandler() {
294 bool ContentSchemeProtocolHandler::CanHandleRequest(
295 const net::URLRequest* request) const {
296 return request->url().SchemeIs(xwalk::kContentScheme);
299 // AppSchemeProtocolHandler
300 AppSchemeProtocolHandler::AppSchemeProtocolHandler() {
303 bool AppSchemeProtocolHandler::CanHandleRequest(
304 const net::URLRequest* request) const {
305 return request->url().SchemeIs(xwalk::kAppScheme);
312 bool RegisterAndroidProtocolHandler(JNIEnv* env) {
313 return RegisterNativesImpl(env);
317 scoped_ptr<net::URLRequestJobFactory::ProtocolHandler>
318 CreateContentSchemeProtocolHandler() {
319 return make_scoped_ptr<net::URLRequestJobFactory::ProtocolHandler>(
320 new ContentSchemeProtocolHandler());
324 scoped_ptr<net::URLRequestJobFactory::ProtocolHandler>
325 CreateAssetFileProtocolHandler() {
326 return make_scoped_ptr<net::URLRequestJobFactory::ProtocolHandler>(
327 new AssetFileProtocolHandler());
330 scoped_ptr<net::URLRequestJobFactory::ProtocolHandler>
331 CreateAppSchemeProtocolHandler() {
332 return make_scoped_ptr<net::URLRequestJobFactory::ProtocolHandler>(
333 new AppSchemeProtocolHandler());
337 // Set a context object to be used for resolving resource queries. This can
338 // be used to override the default application context and redirect all
339 // resource queries to a specific context object, e.g., for the purposes of
342 // |context| should be a android.content.Context instance or NULL to enable
343 // the use of the standard application context.
344 static void SetResourceContextForTesting(JNIEnv* env, jclass /*clazz*/,
347 ResetResourceContext(new JavaObjectWeakGlobalRef(env, context));
349 ResetResourceContext(NULL);
353 static jstring GetAndroidAssetPath(JNIEnv* env, jclass /*clazz*/) {
354 // OK to release, JNI binding.
355 return ConvertUTF8ToJavaString(
356 env, xwalk::kAndroidAssetPath).Release();
359 static jstring GetAndroidResourcePath(JNIEnv* env, jclass /*clazz*/) {
360 // OK to release, JNI binding.
361 return ConvertUTF8ToJavaString(
362 env, xwalk::kAndroidResourcePath).Release();