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 "googleurl/src/gurl.h"
17 #include "jni/AndroidProtocolHandler_jni.h"
18 #include "net/base/io_buffer.h"
19 #include "net/base/mime_util.h"
20 #include "net/base/net_errors.h"
21 #include "net/base/net_util.h"
22 #include "net/http/http_util.h"
23 #include "net/url_request/protocol_intercept_job_factory.h"
24 #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 // Check and clear pending exceptions.
150 if (ClearException(env
) || 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 (ClearException(env
) || 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 // AndroidProtocolHandlerBase -------------------------------------------------
203 net::URLRequestJob
* AndroidProtocolHandlerBase::MaybeCreateJob(
204 net::URLRequest
* request
,
205 net::NetworkDelegate
* network_delegate
) const {
206 if (!CanHandleRequest(request
)) return NULL
;
208 // For WebViewClassic compatibility this job can only accept URLs that can be
209 // opened. URLs that cannot be opened should be resolved by the next handler.
211 // If a request is initially handled here but the job fails due to it being
212 // unable to open the InputStream for that request the request is marked as
213 // previously failed and restarted.
214 // Restarting a request involves creating a new job for that request. This
215 // handler will ignore requests know to have previously failed to 1) prevent
216 // an infinite loop, 2) ensure that the next handler in line gets the
217 // opportunity to create a job for the request.
218 if (HasRequestPreviouslyFailed(request
)) return NULL
;
220 scoped_ptr
<AndroidStreamReaderURLRequestJobDelegateImpl
> reader_delegate(
221 new AndroidStreamReaderURLRequestJobDelegateImpl());
223 return new AndroidStreamReaderURLRequestJob(
226 reader_delegate
.PassAs
<AndroidStreamReaderURLRequestJob::Delegate
>());
229 // AssetFileProtocolHandler ---------------------------------------------------
231 AssetFileProtocolHandler::AssetFileProtocolHandler()
232 : asset_prefix_(std::string(chrome::kFileScheme
) +
233 std::string(content::kStandardSchemeSeparator
) +
234 android_webview::kAndroidAssetPath
),
235 resource_prefix_(std::string(chrome::kFileScheme
) +
236 std::string(content::kStandardSchemeSeparator
) +
237 android_webview::kAndroidResourcePath
) {
240 AssetFileProtocolHandler::~AssetFileProtocolHandler() {
243 bool AssetFileProtocolHandler::CanHandleRequest(
244 const net::URLRequest
* request
) const {
245 if (!request
->url().SchemeIsFile())
248 const std::string
& url
= request
->url().spec();
249 if (!StartsWithASCII(url
, asset_prefix_
, /*case_sensitive=*/ true) &&
250 !StartsWithASCII(url
, resource_prefix_
, /*case_sensitive=*/ true)) {
257 // ContentSchemeProtocolHandler -----------------------------------------------
259 ContentSchemeProtocolHandler::ContentSchemeProtocolHandler() {
262 bool ContentSchemeProtocolHandler::CanHandleRequest(
263 const net::URLRequest
* request
) const {
264 return request
->url().SchemeIs(android_webview::kContentScheme
);
269 namespace android_webview
{
271 bool RegisterAndroidProtocolHandler(JNIEnv
* env
) {
272 return RegisterNativesImpl(env
);
276 scoped_ptr
<net::URLRequestJobFactory::ProtocolHandler
>
277 CreateContentSchemeProtocolHandler() {
278 return make_scoped_ptr
<net::URLRequestJobFactory::ProtocolHandler
>(
279 new ContentSchemeProtocolHandler());
283 scoped_ptr
<net::URLRequestJobFactory::ProtocolHandler
>
284 CreateAssetFileProtocolHandler() {
285 return make_scoped_ptr
<net::URLRequestJobFactory::ProtocolHandler
>(
286 new AssetFileProtocolHandler());
289 // Set a context object to be used for resolving resource queries. This can
290 // be used to override the default application context and redirect all
291 // resource queries to a specific context object, e.g., for the purposes of
294 // |context| should be a android.content.Context instance or NULL to enable
295 // the use of the standard application context.
296 static void SetResourceContextForTesting(JNIEnv
* env
, jclass
/*clazz*/,
299 ResetResourceContext(new JavaObjectWeakGlobalRef(env
, context
));
301 ResetResourceContext(NULL
);
305 static jstring
GetAndroidAssetPath(JNIEnv
* env
, jclass
/*clazz*/) {
306 // OK to release, JNI binding.
307 return ConvertUTF8ToJavaString(
308 env
, android_webview::kAndroidAssetPath
).Release();
311 static jstring
GetAndroidResourcePath(JNIEnv
* env
, jclass
/*clazz*/) {
312 // OK to release, JNI binding.
313 return ConvertUTF8ToJavaString(
314 env
, android_webview::kAndroidResourcePath
).Release();
317 } // namespace android_webview