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 scoped_ptr
<InputStream
> OpenInputStream(JNIEnv
* env
,
64 const GURL
& url
) override
;
66 void OnInputStreamOpenFailed(net::URLRequest
* request
,
67 bool* restart
) override
;
69 bool GetMimeType(JNIEnv
* env
,
70 net::URLRequest
* request
,
72 std::string
* mime_type
) override
;
74 bool GetCharset(JNIEnv
* env
,
75 net::URLRequest
* request
,
77 std::string
* charset
) override
;
79 void AppendResponseHeaders(JNIEnv
* env
,
80 net::HttpResponseHeaders
* headers
) override
;
82 ~AndroidStreamReaderURLRequestJobDelegateImpl() override
;
85 class AndroidRequestInterceptorBase
: public net::URLRequestInterceptor
{
87 net::URLRequestJob
* MaybeInterceptRequest(
88 net::URLRequest
* request
,
89 net::NetworkDelegate
* network_delegate
) const override
;
91 virtual bool ShouldHandleRequest(const net::URLRequest
* request
) const = 0;
94 class AssetFileRequestInterceptor
: public AndroidRequestInterceptorBase
{
96 AssetFileRequestInterceptor();
98 ~AssetFileRequestInterceptor() override
;
99 bool ShouldHandleRequest(const net::URLRequest
* request
) const override
;
102 // file:///android_asset/
103 const std::string asset_prefix_
;
104 // file:///android_res/
105 const std::string resource_prefix_
;
108 // Protocol handler for content:// scheme requests.
109 class ContentSchemeRequestInterceptor
: public AndroidRequestInterceptorBase
{
111 ContentSchemeRequestInterceptor();
112 bool ShouldHandleRequest(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 void AndroidStreamReaderURLRequestJobDelegateImpl::AppendResponseHeaders(
203 net::HttpResponseHeaders
* headers
) {
207 // AndroidRequestInterceptorBase ----------------------------------------------
209 net::URLRequestJob
* AndroidRequestInterceptorBase::MaybeInterceptRequest(
210 net::URLRequest
* request
,
211 net::NetworkDelegate
* network_delegate
) const {
212 if (!ShouldHandleRequest(request
))
215 // For WebViewClassic compatibility this job can only accept URLs that can be
216 // opened. URLs that cannot be opened should be resolved by the next handler.
218 // If a request is initially handled here but the job fails due to it being
219 // unable to open the InputStream for that request the request is marked as
220 // previously failed and restarted.
221 // Restarting a request involves creating a new job for that request. This
222 // handler will ignore requests know to have previously failed to 1) prevent
223 // an infinite loop, 2) ensure that the next handler in line gets the
224 // opportunity to create a job for the request.
225 if (HasRequestPreviouslyFailed(request
))
228 scoped_ptr
<AndroidStreamReaderURLRequestJobDelegateImpl
> reader_delegate(
229 new AndroidStreamReaderURLRequestJobDelegateImpl());
231 return new AndroidStreamReaderURLRequestJob(
232 request
, network_delegate
, reader_delegate
.Pass());
235 // AssetFileRequestInterceptor ------------------------------------------------
237 AssetFileRequestInterceptor::AssetFileRequestInterceptor()
238 : asset_prefix_(std::string(url::kFileScheme
) +
239 std::string(url::kStandardSchemeSeparator
) +
240 android_webview::kAndroidAssetPath
),
241 resource_prefix_(std::string(url::kFileScheme
) +
242 std::string(url::kStandardSchemeSeparator
) +
243 android_webview::kAndroidResourcePath
) {
246 AssetFileRequestInterceptor::~AssetFileRequestInterceptor() {
249 bool AssetFileRequestInterceptor::ShouldHandleRequest(
250 const net::URLRequest
* request
) const {
251 if (!request
->url().SchemeIsFile())
254 const std::string
& url
= request
->url().spec();
255 if (!base::StartsWith(url
, asset_prefix_
, base::CompareCase::SENSITIVE
) &&
256 !base::StartsWith(url
, resource_prefix_
, base::CompareCase::SENSITIVE
)) {
263 // ContentSchemeRequestInterceptor --------------------------------------------
265 ContentSchemeRequestInterceptor::ContentSchemeRequestInterceptor() {
268 bool ContentSchemeRequestInterceptor::ShouldHandleRequest(
269 const net::URLRequest
* request
) const {
270 return request
->url().SchemeIs(android_webview::kContentScheme
);
275 namespace android_webview
{
277 bool RegisterAndroidProtocolHandler(JNIEnv
* env
) {
278 return RegisterNativesImpl(env
);
282 scoped_ptr
<net::URLRequestInterceptor
>
283 CreateContentSchemeRequestInterceptor() {
284 return make_scoped_ptr
<net::URLRequestInterceptor
>(
285 new ContentSchemeRequestInterceptor());
289 scoped_ptr
<net::URLRequestInterceptor
> CreateAssetFileRequestInterceptor() {
290 return scoped_ptr
<net::URLRequestInterceptor
>(
291 new AssetFileRequestInterceptor());
294 // Set a context object to be used for resolving resource queries. This can
295 // be used to override the default application context and redirect all
296 // resource queries to a specific context object, e.g., for the purposes of
299 // |context| should be a android.content.Context instance or NULL to enable
300 // the use of the standard application context.
301 static void SetResourceContextForTesting(JNIEnv
* env
, jclass
/*clazz*/,
304 ResetResourceContext(new JavaObjectWeakGlobalRef(env
, context
));
306 ResetResourceContext(NULL
);
310 static jstring
GetAndroidAssetPath(JNIEnv
* env
, jclass
/*clazz*/) {
311 // OK to release, JNI binding.
312 return ConvertUTF8ToJavaString(
313 env
, android_webview::kAndroidAssetPath
).Release();
316 static jstring
GetAndroidResourcePath(JNIEnv
* env
, jclass
/*clazz*/) {
317 // OK to release, JNI binding.
318 return ConvertUTF8ToJavaString(
319 env
, android_webview::kAndroidResourcePath
).Release();
322 } // namespace android_webview