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 "android_webview/native/android_protocol_handler.h"
7 #include "android_webview/browser/net/android_stream_reader_url_request_job.h"
8 #include "android_webview/browser/net/aw_url_request_job_factory.h"
9 #include "android_webview/common/url_constants.h"
10 #include "android_webview/native/input_stream_impl.h"
11 #include "base/android/jni_android.h"
12 #include "base/android/jni_string.h"
13 #include "base/android/jni_weak_ref.h"
14 #include "base/strings/string_util.h"
15 #include "content/public/common/url_constants.h"
16 #include "jni/AndroidProtocolHandler_jni.h"
17 #include "net/base/io_buffer.h"
18 #include "net/base/mime_util.h"
19 #include "net/base/net_errors.h"
20 #include "net/base/net_util.h"
21 #include "net/http/http_util.h"
22 #include "net/url_request/url_request.h"
23 #include "net/url_request/url_request_interceptor.h"
26 using android_webview::InputStream
;
27 using android_webview::InputStreamImpl
;
28 using base::android::AttachCurrentThread
;
29 using base::android::ClearException
;
30 using base::android::ConvertUTF8ToJavaString
;
31 using base::android::ScopedJavaGlobalRef
;
32 using base::android::ScopedJavaLocalRef
;
36 // Override resource context for reading resource and asset files. Used for
38 JavaObjectWeakGlobalRef
* g_resource_context
= NULL
;
40 void ResetResourceContext(JavaObjectWeakGlobalRef
* ref
) {
41 if (g_resource_context
)
42 delete g_resource_context
;
44 g_resource_context
= ref
;
47 void* kPreviouslyFailedKey
= &kPreviouslyFailedKey
;
49 void MarkRequestAsFailed(net::URLRequest
* request
) {
50 request
->SetUserData(kPreviouslyFailedKey
,
51 new base::SupportsUserData::Data());
54 bool HasRequestPreviouslyFailed(net::URLRequest
* request
) {
55 return request
->GetUserData(kPreviouslyFailedKey
) != NULL
;
58 class AndroidStreamReaderURLRequestJobDelegateImpl
59 : public AndroidStreamReaderURLRequestJob::Delegate
{
61 AndroidStreamReaderURLRequestJobDelegateImpl();
63 virtual scoped_ptr
<InputStream
> OpenInputStream(
65 const GURL
& url
) OVERRIDE
;
67 virtual void OnInputStreamOpenFailed(net::URLRequest
* request
,
68 bool* restart
) OVERRIDE
;
70 virtual bool GetMimeType(JNIEnv
* env
,
71 net::URLRequest
* request
,
73 std::string
* mime_type
) OVERRIDE
;
75 virtual bool GetCharset(JNIEnv
* env
,
76 net::URLRequest
* request
,
78 std::string
* charset
) OVERRIDE
;
80 virtual ~AndroidStreamReaderURLRequestJobDelegateImpl();
83 class AndroidRequestInterceptorBase
: public net::URLRequestInterceptor
{
85 virtual net::URLRequestJob
* MaybeInterceptRequest(
86 net::URLRequest
* request
,
87 net::NetworkDelegate
* network_delegate
) const OVERRIDE
;
89 virtual bool ShouldHandleRequest(const net::URLRequest
* request
) const = 0;
92 class AssetFileRequestInterceptor
: public AndroidRequestInterceptorBase
{
94 AssetFileRequestInterceptor();
96 virtual ~AssetFileRequestInterceptor() OVERRIDE
;
97 virtual bool ShouldHandleRequest(
98 const net::URLRequest
* request
) const OVERRIDE
;
101 // file:///android_asset/
102 const std::string asset_prefix_
;
103 // file:///android_res/
104 const std::string resource_prefix_
;
107 // Protocol handler for content:// scheme requests.
108 class ContentSchemeRequestInterceptor
: public AndroidRequestInterceptorBase
{
110 ContentSchemeRequestInterceptor();
111 virtual bool ShouldHandleRequest(
112 const net::URLRequest
* request
) const OVERRIDE
;
115 static ScopedJavaLocalRef
<jobject
> GetResourceContext(JNIEnv
* env
) {
116 if (g_resource_context
)
117 return g_resource_context
->get(env
);
118 ScopedJavaLocalRef
<jobject
> context
;
119 // We have to reset as GetApplicationContext() returns a jobject with a
120 // global ref. The constructor that takes a jobject would expect a local ref
122 context
.Reset(env
, base::android::GetApplicationContext());
126 // AndroidStreamReaderURLRequestJobDelegateImpl -------------------------------
128 AndroidStreamReaderURLRequestJobDelegateImpl::
129 AndroidStreamReaderURLRequestJobDelegateImpl() {}
131 AndroidStreamReaderURLRequestJobDelegateImpl::
132 ~AndroidStreamReaderURLRequestJobDelegateImpl() {
135 scoped_ptr
<InputStream
>
136 AndroidStreamReaderURLRequestJobDelegateImpl::OpenInputStream(
137 JNIEnv
* env
, const GURL
& url
) {
138 DCHECK(url
.is_valid());
141 // Open the input stream.
142 ScopedJavaLocalRef
<jstring
> jurl
=
143 ConvertUTF8ToJavaString(env
, url
.spec());
144 ScopedJavaLocalRef
<jobject
> stream
=
145 android_webview::Java_AndroidProtocolHandler_open(
147 GetResourceContext(env
).obj(),
150 if (stream
.is_null()) {
151 DLOG(ERROR
) << "Unable to open input stream for Android URL";
152 return scoped_ptr
<InputStream
>();
154 return make_scoped_ptr
<InputStream
>(new InputStreamImpl(stream
));
157 void AndroidStreamReaderURLRequestJobDelegateImpl::OnInputStreamOpenFailed(
158 net::URLRequest
* request
,
160 DCHECK(!HasRequestPreviouslyFailed(request
));
161 MarkRequestAsFailed(request
);
165 bool AndroidStreamReaderURLRequestJobDelegateImpl::GetMimeType(
167 net::URLRequest
* request
,
168 android_webview::InputStream
* stream
,
169 std::string
* mime_type
) {
174 // Query the mime type from the Java side. It is possible for the query to
175 // fail, as the mime type cannot be determined for all supported schemes.
176 ScopedJavaLocalRef
<jstring
> url
=
177 ConvertUTF8ToJavaString(env
, request
->url().spec());
178 const InputStreamImpl
* stream_impl
=
179 InputStreamImpl::FromInputStream(stream
);
180 ScopedJavaLocalRef
<jstring
> returned_type
=
181 android_webview::Java_AndroidProtocolHandler_getMimeType(
183 GetResourceContext(env
).obj(),
184 stream_impl
->jobj(), url
.obj());
185 if (returned_type
.is_null())
188 *mime_type
= base::android::ConvertJavaStringToUTF8(returned_type
);
192 bool AndroidStreamReaderURLRequestJobDelegateImpl::GetCharset(
194 net::URLRequest
* request
,
195 android_webview::InputStream
* stream
,
196 std::string
* charset
) {
197 // TODO: We should probably be getting this from the managed side.
201 // AndroidRequestInterceptorBase ----------------------------------------------
203 net::URLRequestJob
* AndroidRequestInterceptorBase::MaybeInterceptRequest(
204 net::URLRequest
* request
,
205 net::NetworkDelegate
* network_delegate
) const {
206 if (!ShouldHandleRequest(request
))
209 // For WebViewClassic compatibility this job can only accept URLs that can be
210 // opened. URLs that cannot be opened should be resolved by the next handler.
212 // If a request is initially handled here but the job fails due to it being
213 // unable to open the InputStream for that request the request is marked as
214 // previously failed and restarted.
215 // Restarting a request involves creating a new job for that request. This
216 // handler will ignore requests know to have previously failed to 1) prevent
217 // an infinite loop, 2) ensure that the next handler in line gets the
218 // opportunity to create a job for the request.
219 if (HasRequestPreviouslyFailed(request
))
222 scoped_ptr
<AndroidStreamReaderURLRequestJobDelegateImpl
> reader_delegate(
223 new AndroidStreamReaderURLRequestJobDelegateImpl());
225 return new AndroidStreamReaderURLRequestJob(
228 reader_delegate
.PassAs
<AndroidStreamReaderURLRequestJob::Delegate
>());
231 // AssetFileRequestInterceptor ------------------------------------------------
233 AssetFileRequestInterceptor::AssetFileRequestInterceptor()
234 : asset_prefix_(std::string(url::kFileScheme
) +
235 std::string(url::kStandardSchemeSeparator
) +
236 android_webview::kAndroidAssetPath
),
237 resource_prefix_(std::string(url::kFileScheme
) +
238 std::string(url::kStandardSchemeSeparator
) +
239 android_webview::kAndroidResourcePath
) {
242 AssetFileRequestInterceptor::~AssetFileRequestInterceptor() {
245 bool AssetFileRequestInterceptor::ShouldHandleRequest(
246 const net::URLRequest
* request
) const {
247 if (!request
->url().SchemeIsFile())
250 const std::string
& url
= request
->url().spec();
251 if (!StartsWithASCII(url
, asset_prefix_
, /*case_sensitive=*/ true) &&
252 !StartsWithASCII(url
, resource_prefix_
, /*case_sensitive=*/ true)) {
259 // ContentSchemeRequestInterceptor --------------------------------------------
261 ContentSchemeRequestInterceptor::ContentSchemeRequestInterceptor() {
264 bool ContentSchemeRequestInterceptor::ShouldHandleRequest(
265 const net::URLRequest
* request
) const {
266 return request
->url().SchemeIs(android_webview::kContentScheme
);
271 namespace android_webview
{
273 bool RegisterAndroidProtocolHandler(JNIEnv
* env
) {
274 return RegisterNativesImpl(env
);
278 scoped_ptr
<net::URLRequestInterceptor
>
279 CreateContentSchemeRequestInterceptor() {
280 return make_scoped_ptr
<net::URLRequestInterceptor
>(
281 new ContentSchemeRequestInterceptor());
285 scoped_ptr
<net::URLRequestInterceptor
> CreateAssetFileRequestInterceptor() {
286 return scoped_ptr
<net::URLRequestInterceptor
>(
287 new AssetFileRequestInterceptor());
290 // Set a context object to be used for resolving resource queries. This can
291 // be used to override the default application context and redirect all
292 // resource queries to a specific context object, e.g., for the purposes of
295 // |context| should be a android.content.Context instance or NULL to enable
296 // the use of the standard application context.
297 static void SetResourceContextForTesting(JNIEnv
* env
, jclass
/*clazz*/,
300 ResetResourceContext(new JavaObjectWeakGlobalRef(env
, context
));
302 ResetResourceContext(NULL
);
306 static jstring
GetAndroidAssetPath(JNIEnv
* env
, jclass
/*clazz*/) {
307 // OK to release, JNI binding.
308 return ConvertUTF8ToJavaString(
309 env
, android_webview::kAndroidAssetPath
).Release();
312 static jstring
GetAndroidResourcePath(JNIEnv
* env
, jclass
/*clazz*/) {
313 // OK to release, JNI binding.
314 return ConvertUTF8ToJavaString(
315 env
, android_webview::kAndroidResourcePath
).Release();
318 } // namespace android_webview