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 void AppendResponseHeaders(
82 net::HttpResponseHeaders
* headers
) override
;
84 virtual ~AndroidStreamReaderURLRequestJobDelegateImpl();
87 class AndroidRequestInterceptorBase
: public net::URLRequestInterceptor
{
89 virtual net::URLRequestJob
* MaybeInterceptRequest(
90 net::URLRequest
* request
,
91 net::NetworkDelegate
* network_delegate
) const override
;
93 virtual bool ShouldHandleRequest(const net::URLRequest
* request
) const = 0;
96 class AssetFileRequestInterceptor
: public AndroidRequestInterceptorBase
{
98 AssetFileRequestInterceptor();
100 virtual ~AssetFileRequestInterceptor() override
;
101 virtual bool ShouldHandleRequest(
102 const net::URLRequest
* request
) const override
;
105 // file:///android_asset/
106 const std::string asset_prefix_
;
107 // file:///android_res/
108 const std::string resource_prefix_
;
111 // Protocol handler for content:// scheme requests.
112 class ContentSchemeRequestInterceptor
: public AndroidRequestInterceptorBase
{
114 ContentSchemeRequestInterceptor();
115 virtual bool ShouldHandleRequest(
116 const net::URLRequest
* request
) const override
;
119 static ScopedJavaLocalRef
<jobject
> GetResourceContext(JNIEnv
* env
) {
120 if (g_resource_context
)
121 return g_resource_context
->get(env
);
122 ScopedJavaLocalRef
<jobject
> context
;
123 // We have to reset as GetApplicationContext() returns a jobject with a
124 // global ref. The constructor that takes a jobject would expect a local ref
126 context
.Reset(env
, base::android::GetApplicationContext());
130 // AndroidStreamReaderURLRequestJobDelegateImpl -------------------------------
132 AndroidStreamReaderURLRequestJobDelegateImpl::
133 AndroidStreamReaderURLRequestJobDelegateImpl() {}
135 AndroidStreamReaderURLRequestJobDelegateImpl::
136 ~AndroidStreamReaderURLRequestJobDelegateImpl() {
139 scoped_ptr
<InputStream
>
140 AndroidStreamReaderURLRequestJobDelegateImpl::OpenInputStream(
141 JNIEnv
* env
, const GURL
& url
) {
142 DCHECK(url
.is_valid());
145 // Open the input stream.
146 ScopedJavaLocalRef
<jstring
> jurl
=
147 ConvertUTF8ToJavaString(env
, url
.spec());
148 ScopedJavaLocalRef
<jobject
> stream
=
149 android_webview::Java_AndroidProtocolHandler_open(
151 GetResourceContext(env
).obj(),
154 if (stream
.is_null()) {
155 DLOG(ERROR
) << "Unable to open input stream for Android URL";
156 return scoped_ptr
<InputStream
>();
158 return make_scoped_ptr
<InputStream
>(new InputStreamImpl(stream
));
161 void AndroidStreamReaderURLRequestJobDelegateImpl::OnInputStreamOpenFailed(
162 net::URLRequest
* request
,
164 DCHECK(!HasRequestPreviouslyFailed(request
));
165 MarkRequestAsFailed(request
);
169 bool AndroidStreamReaderURLRequestJobDelegateImpl::GetMimeType(
171 net::URLRequest
* request
,
172 android_webview::InputStream
* stream
,
173 std::string
* mime_type
) {
178 // Query the mime type from the Java side. It is possible for the query to
179 // fail, as the mime type cannot be determined for all supported schemes.
180 ScopedJavaLocalRef
<jstring
> url
=
181 ConvertUTF8ToJavaString(env
, request
->url().spec());
182 const InputStreamImpl
* stream_impl
=
183 InputStreamImpl::FromInputStream(stream
);
184 ScopedJavaLocalRef
<jstring
> returned_type
=
185 android_webview::Java_AndroidProtocolHandler_getMimeType(
187 GetResourceContext(env
).obj(),
188 stream_impl
->jobj(), url
.obj());
189 if (returned_type
.is_null())
192 *mime_type
= base::android::ConvertJavaStringToUTF8(returned_type
);
196 bool AndroidStreamReaderURLRequestJobDelegateImpl::GetCharset(
198 net::URLRequest
* request
,
199 android_webview::InputStream
* stream
,
200 std::string
* charset
) {
201 // TODO: We should probably be getting this from the managed side.
205 void AndroidStreamReaderURLRequestJobDelegateImpl::AppendResponseHeaders(
207 net::HttpResponseHeaders
* headers
) {
211 // AndroidRequestInterceptorBase ----------------------------------------------
213 net::URLRequestJob
* AndroidRequestInterceptorBase::MaybeInterceptRequest(
214 net::URLRequest
* request
,
215 net::NetworkDelegate
* network_delegate
) const {
216 if (!ShouldHandleRequest(request
))
219 // For WebViewClassic compatibility this job can only accept URLs that can be
220 // opened. URLs that cannot be opened should be resolved by the next handler.
222 // If a request is initially handled here but the job fails due to it being
223 // unable to open the InputStream for that request the request is marked as
224 // previously failed and restarted.
225 // Restarting a request involves creating a new job for that request. This
226 // handler will ignore requests know to have previously failed to 1) prevent
227 // an infinite loop, 2) ensure that the next handler in line gets the
228 // opportunity to create a job for the request.
229 if (HasRequestPreviouslyFailed(request
))
232 scoped_ptr
<AndroidStreamReaderURLRequestJobDelegateImpl
> reader_delegate(
233 new AndroidStreamReaderURLRequestJobDelegateImpl());
235 return new AndroidStreamReaderURLRequestJob(
236 request
, network_delegate
, reader_delegate
.Pass());
239 // AssetFileRequestInterceptor ------------------------------------------------
241 AssetFileRequestInterceptor::AssetFileRequestInterceptor()
242 : asset_prefix_(std::string(url::kFileScheme
) +
243 std::string(url::kStandardSchemeSeparator
) +
244 android_webview::kAndroidAssetPath
),
245 resource_prefix_(std::string(url::kFileScheme
) +
246 std::string(url::kStandardSchemeSeparator
) +
247 android_webview::kAndroidResourcePath
) {
250 AssetFileRequestInterceptor::~AssetFileRequestInterceptor() {
253 bool AssetFileRequestInterceptor::ShouldHandleRequest(
254 const net::URLRequest
* request
) const {
255 if (!request
->url().SchemeIsFile())
258 const std::string
& url
= request
->url().spec();
259 if (!StartsWithASCII(url
, asset_prefix_
, /*case_sensitive=*/ true) &&
260 !StartsWithASCII(url
, resource_prefix_
, /*case_sensitive=*/ true)) {
267 // ContentSchemeRequestInterceptor --------------------------------------------
269 ContentSchemeRequestInterceptor::ContentSchemeRequestInterceptor() {
272 bool ContentSchemeRequestInterceptor::ShouldHandleRequest(
273 const net::URLRequest
* request
) const {
274 return request
->url().SchemeIs(android_webview::kContentScheme
);
279 namespace android_webview
{
281 bool RegisterAndroidProtocolHandler(JNIEnv
* env
) {
282 return RegisterNativesImpl(env
);
286 scoped_ptr
<net::URLRequestInterceptor
>
287 CreateContentSchemeRequestInterceptor() {
288 return make_scoped_ptr
<net::URLRequestInterceptor
>(
289 new ContentSchemeRequestInterceptor());
293 scoped_ptr
<net::URLRequestInterceptor
> CreateAssetFileRequestInterceptor() {
294 return scoped_ptr
<net::URLRequestInterceptor
>(
295 new AssetFileRequestInterceptor());
298 // Set a context object to be used for resolving resource queries. This can
299 // be used to override the default application context and redirect all
300 // resource queries to a specific context object, e.g., for the purposes of
303 // |context| should be a android.content.Context instance or NULL to enable
304 // the use of the standard application context.
305 static void SetResourceContextForTesting(JNIEnv
* env
, jclass
/*clazz*/,
308 ResetResourceContext(new JavaObjectWeakGlobalRef(env
, context
));
310 ResetResourceContext(NULL
);
314 static jstring
GetAndroidAssetPath(JNIEnv
* env
, jclass
/*clazz*/) {
315 // OK to release, JNI binding.
316 return ConvertUTF8ToJavaString(
317 env
, android_webview::kAndroidAssetPath
).Release();
320 static jstring
GetAndroidResourcePath(JNIEnv
* env
, jclass
/*clazz*/) {
321 // OK to release, JNI binding.
322 return ConvertUTF8ToJavaString(
323 env
, android_webview::kAndroidResourcePath
).Release();
326 } // namespace android_webview