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_helper.h"
13 #include "base/android/jni_string.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/protocol_intercept_job_factory.h"
23 #include "net/url_request/url_request.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 AndroidProtocolHandlerBase
:
84 public net::URLRequestJobFactory::ProtocolHandler
{
86 virtual net::URLRequestJob
* MaybeCreateJob(
87 net::URLRequest
* request
,
88 net::NetworkDelegate
* network_delegate
) const OVERRIDE
;
90 virtual bool CanHandleRequest(const net::URLRequest
* request
) const = 0;
93 class AssetFileProtocolHandler
: public AndroidProtocolHandlerBase
{
95 AssetFileProtocolHandler();
97 virtual ~AssetFileProtocolHandler() OVERRIDE
;
98 virtual bool CanHandleRequest(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 ContentSchemeProtocolHandler
: public AndroidProtocolHandlerBase
{
110 ContentSchemeProtocolHandler();
111 virtual bool CanHandleRequest(const net::URLRequest
* request
) const OVERRIDE
;
114 static ScopedJavaLocalRef
<jobject
> GetResourceContext(JNIEnv
* env
) {
115 if (g_resource_context
)
116 return g_resource_context
->get(env
);
117 ScopedJavaLocalRef
<jobject
> context
;
118 // We have to reset as GetApplicationContext() returns a jobject with a
119 // global ref. The constructor that takes a jobject would expect a local ref
121 context
.Reset(env
, base::android::GetApplicationContext());
125 // AndroidStreamReaderURLRequestJobDelegateImpl -------------------------------
127 AndroidStreamReaderURLRequestJobDelegateImpl::
128 AndroidStreamReaderURLRequestJobDelegateImpl() {}
130 AndroidStreamReaderURLRequestJobDelegateImpl::
131 ~AndroidStreamReaderURLRequestJobDelegateImpl() {
134 scoped_ptr
<InputStream
>
135 AndroidStreamReaderURLRequestJobDelegateImpl::OpenInputStream(
136 JNIEnv
* env
, const GURL
& url
) {
137 DCHECK(url
.is_valid());
140 // Open the input stream.
141 ScopedJavaLocalRef
<jstring
> jurl
=
142 ConvertUTF8ToJavaString(env
, url
.spec());
143 ScopedJavaLocalRef
<jobject
> stream
=
144 android_webview::Java_AndroidProtocolHandler_open(
146 GetResourceContext(env
).obj(),
149 if (stream
.is_null()) {
150 DLOG(ERROR
) << "Unable to open input stream for Android URL";
151 return scoped_ptr
<InputStream
>();
153 return make_scoped_ptr
<InputStream
>(new InputStreamImpl(stream
));
156 void AndroidStreamReaderURLRequestJobDelegateImpl::OnInputStreamOpenFailed(
157 net::URLRequest
* request
,
159 DCHECK(!HasRequestPreviouslyFailed(request
));
160 MarkRequestAsFailed(request
);
164 bool AndroidStreamReaderURLRequestJobDelegateImpl::GetMimeType(
166 net::URLRequest
* request
,
167 android_webview::InputStream
* stream
,
168 std::string
* mime_type
) {
173 // Query the mime type from the Java side. It is possible for the query to
174 // fail, as the mime type cannot be determined for all supported schemes.
175 ScopedJavaLocalRef
<jstring
> url
=
176 ConvertUTF8ToJavaString(env
, request
->url().spec());
177 const InputStreamImpl
* stream_impl
=
178 InputStreamImpl::FromInputStream(stream
);
179 ScopedJavaLocalRef
<jstring
> returned_type
=
180 android_webview::Java_AndroidProtocolHandler_getMimeType(
182 GetResourceContext(env
).obj(),
183 stream_impl
->jobj(), url
.obj());
184 if (returned_type
.is_null())
187 *mime_type
= base::android::ConvertJavaStringToUTF8(returned_type
);
191 bool AndroidStreamReaderURLRequestJobDelegateImpl::GetCharset(
193 net::URLRequest
* request
,
194 android_webview::InputStream
* stream
,
195 std::string
* charset
) {
196 // TODO: We should probably be getting this from the managed side.
200 // AndroidProtocolHandlerBase -------------------------------------------------
202 net::URLRequestJob
* AndroidProtocolHandlerBase::MaybeCreateJob(
203 net::URLRequest
* request
,
204 net::NetworkDelegate
* network_delegate
) const {
205 if (!CanHandleRequest(request
)) return NULL
;
207 // For WebViewClassic compatibility this job can only accept URLs that can be
208 // opened. URLs that cannot be opened should be resolved by the next handler.
210 // If a request is initially handled here but the job fails due to it being
211 // unable to open the InputStream for that request the request is marked as
212 // previously failed and restarted.
213 // Restarting a request involves creating a new job for that request. This
214 // handler will ignore requests know to have previously failed to 1) prevent
215 // an infinite loop, 2) ensure that the next handler in line gets the
216 // opportunity to create a job for the request.
217 if (HasRequestPreviouslyFailed(request
)) return NULL
;
219 scoped_ptr
<AndroidStreamReaderURLRequestJobDelegateImpl
> reader_delegate(
220 new AndroidStreamReaderURLRequestJobDelegateImpl());
222 return new AndroidStreamReaderURLRequestJob(
225 reader_delegate
.PassAs
<AndroidStreamReaderURLRequestJob::Delegate
>());
228 // AssetFileProtocolHandler ---------------------------------------------------
230 AssetFileProtocolHandler::AssetFileProtocolHandler()
231 : asset_prefix_(std::string(content::kFileScheme
) +
232 std::string(content::kStandardSchemeSeparator
) +
233 android_webview::kAndroidAssetPath
),
234 resource_prefix_(std::string(content::kFileScheme
) +
235 std::string(content::kStandardSchemeSeparator
) +
236 android_webview::kAndroidResourcePath
) {
239 AssetFileProtocolHandler::~AssetFileProtocolHandler() {
242 bool AssetFileProtocolHandler::CanHandleRequest(
243 const net::URLRequest
* request
) const {
244 if (!request
->url().SchemeIsFile())
247 const std::string
& url
= request
->url().spec();
248 if (!StartsWithASCII(url
, asset_prefix_
, /*case_sensitive=*/ true) &&
249 !StartsWithASCII(url
, resource_prefix_
, /*case_sensitive=*/ true)) {
256 // ContentSchemeProtocolHandler -----------------------------------------------
258 ContentSchemeProtocolHandler::ContentSchemeProtocolHandler() {
261 bool ContentSchemeProtocolHandler::CanHandleRequest(
262 const net::URLRequest
* request
) const {
263 return request
->url().SchemeIs(android_webview::kContentScheme
);
268 namespace android_webview
{
270 bool RegisterAndroidProtocolHandler(JNIEnv
* env
) {
271 return RegisterNativesImpl(env
);
275 scoped_ptr
<net::URLRequestJobFactory::ProtocolHandler
>
276 CreateContentSchemeProtocolHandler() {
277 return make_scoped_ptr
<net::URLRequestJobFactory::ProtocolHandler
>(
278 new ContentSchemeProtocolHandler());
282 scoped_ptr
<net::URLRequestJobFactory::ProtocolHandler
>
283 CreateAssetFileProtocolHandler() {
284 return make_scoped_ptr
<net::URLRequestJobFactory::ProtocolHandler
>(
285 new AssetFileProtocolHandler());
288 // Set a context object to be used for resolving resource queries. This can
289 // be used to override the default application context and redirect all
290 // resource queries to a specific context object, e.g., for the purposes of
293 // |context| should be a android.content.Context instance or NULL to enable
294 // the use of the standard application context.
295 static void SetResourceContextForTesting(JNIEnv
* env
, jclass
/*clazz*/,
298 ResetResourceContext(new JavaObjectWeakGlobalRef(env
, context
));
300 ResetResourceContext(NULL
);
304 static jstring
GetAndroidAssetPath(JNIEnv
* env
, jclass
/*clazz*/) {
305 // OK to release, JNI binding.
306 return ConvertUTF8ToJavaString(
307 env
, android_webview::kAndroidAssetPath
).Release();
310 static jstring
GetAndroidResourcePath(JNIEnv
* env
, jclass
/*clazz*/) {
311 // OK to release, JNI binding.
312 return ConvertUTF8ToJavaString(
313 env
, android_webview::kAndroidResourcePath
).Release();
316 } // namespace android_webview