Roll src/third_party/WebKit 06cb9e9:a978ee5 (svn 202558:202559)
[chromium-blink-merge.git] / android_webview / native / android_protocol_handler.cc
bloba2a9f6d38d7be8cb121861ab4dabeb201017183f
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 "content/public/common/url_constants.h"
15 #include "jni/AndroidProtocolHandler_jni.h"
16 #include "net/base/io_buffer.h"
17 #include "net/base/mime_util.h"
18 #include "net/base/net_errors.h"
19 #include "net/base/net_util.h"
20 #include "net/http/http_util.h"
21 #include "net/url_request/url_request.h"
22 #include "net/url_request/url_request_interceptor.h"
23 #include "url/gurl.h"
24 #include "url/url_constants.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;
34 namespace {
36 // Override resource context for reading resource and asset files. Used for
37 // testing.
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 {
60 public:
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,
71 InputStream* stream,
72 std::string* mime_type) override;
74 bool GetCharset(JNIEnv* env,
75 net::URLRequest* request,
76 InputStream* stream,
77 std::string* charset) override;
79 void AppendResponseHeaders(JNIEnv* env,
80 net::HttpResponseHeaders* headers) override;
82 ~AndroidStreamReaderURLRequestJobDelegateImpl() override;
85 class AndroidRequestInterceptorBase : public net::URLRequestInterceptor {
86 public:
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 {
95 public:
96 AssetFileRequestInterceptor();
97 bool ShouldHandleRequest(const net::URLRequest* request) const override;
100 // Protocol handler for content:// scheme requests.
101 class ContentSchemeRequestInterceptor : public AndroidRequestInterceptorBase {
102 public:
103 ContentSchemeRequestInterceptor();
104 bool ShouldHandleRequest(const net::URLRequest* request) const override;
107 static ScopedJavaLocalRef<jobject> GetResourceContext(JNIEnv* env) {
108 if (g_resource_context)
109 return g_resource_context->get(env);
110 ScopedJavaLocalRef<jobject> context;
111 // We have to reset as GetApplicationContext() returns a jobject with a
112 // global ref. The constructor that takes a jobject would expect a local ref
113 // and would assert.
114 context.Reset(env, base::android::GetApplicationContext());
115 return context;
118 // AndroidStreamReaderURLRequestJobDelegateImpl -------------------------------
120 AndroidStreamReaderURLRequestJobDelegateImpl::
121 AndroidStreamReaderURLRequestJobDelegateImpl() {}
123 AndroidStreamReaderURLRequestJobDelegateImpl::
124 ~AndroidStreamReaderURLRequestJobDelegateImpl() {
127 scoped_ptr<InputStream>
128 AndroidStreamReaderURLRequestJobDelegateImpl::OpenInputStream(
129 JNIEnv* env, const GURL& url) {
130 DCHECK(url.is_valid());
131 DCHECK(env);
133 // Open the input stream.
134 ScopedJavaLocalRef<jstring> jurl =
135 ConvertUTF8ToJavaString(env, url.spec());
136 ScopedJavaLocalRef<jobject> stream =
137 android_webview::Java_AndroidProtocolHandler_open(
138 env,
139 GetResourceContext(env).obj(),
140 jurl.obj());
142 if (stream.is_null()) {
143 DLOG(ERROR) << "Unable to open input stream for Android URL";
144 return scoped_ptr<InputStream>();
146 return make_scoped_ptr<InputStream>(new InputStreamImpl(stream));
149 void AndroidStreamReaderURLRequestJobDelegateImpl::OnInputStreamOpenFailed(
150 net::URLRequest* request,
151 bool* restart) {
152 DCHECK(!HasRequestPreviouslyFailed(request));
153 MarkRequestAsFailed(request);
154 *restart = true;
157 bool AndroidStreamReaderURLRequestJobDelegateImpl::GetMimeType(
158 JNIEnv* env,
159 net::URLRequest* request,
160 android_webview::InputStream* stream,
161 std::string* mime_type) {
162 DCHECK(env);
163 DCHECK(request);
164 DCHECK(mime_type);
166 // Query the mime type from the Java side. It is possible for the query to
167 // fail, as the mime type cannot be determined for all supported schemes.
168 ScopedJavaLocalRef<jstring> url =
169 ConvertUTF8ToJavaString(env, request->url().spec());
170 const InputStreamImpl* stream_impl =
171 InputStreamImpl::FromInputStream(stream);
172 ScopedJavaLocalRef<jstring> returned_type =
173 android_webview::Java_AndroidProtocolHandler_getMimeType(
174 env,
175 GetResourceContext(env).obj(),
176 stream_impl->jobj(), url.obj());
177 if (returned_type.is_null())
178 return false;
180 *mime_type = base::android::ConvertJavaStringToUTF8(returned_type);
181 return true;
184 bool AndroidStreamReaderURLRequestJobDelegateImpl::GetCharset(
185 JNIEnv* env,
186 net::URLRequest* request,
187 android_webview::InputStream* stream,
188 std::string* charset) {
189 // TODO: We should probably be getting this from the managed side.
190 return false;
193 void AndroidStreamReaderURLRequestJobDelegateImpl::AppendResponseHeaders(
194 JNIEnv* env,
195 net::HttpResponseHeaders* headers) {
196 // no-op
199 // AndroidRequestInterceptorBase ----------------------------------------------
201 net::URLRequestJob* AndroidRequestInterceptorBase::MaybeInterceptRequest(
202 net::URLRequest* request,
203 net::NetworkDelegate* network_delegate) const {
204 if (!ShouldHandleRequest(request))
205 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))
218 return NULL;
220 scoped_ptr<AndroidStreamReaderURLRequestJobDelegateImpl> reader_delegate(
221 new AndroidStreamReaderURLRequestJobDelegateImpl());
223 return new AndroidStreamReaderURLRequestJob(
224 request, network_delegate, reader_delegate.Pass());
227 // AssetFileRequestInterceptor ------------------------------------------------
229 AssetFileRequestInterceptor::AssetFileRequestInterceptor() {
232 bool AssetFileRequestInterceptor::ShouldHandleRequest(
233 const net::URLRequest* request) const {
234 return android_webview::IsAndroidSpecialFileUrl(request->url());
237 // ContentSchemeRequestInterceptor --------------------------------------------
239 ContentSchemeRequestInterceptor::ContentSchemeRequestInterceptor() {
242 bool ContentSchemeRequestInterceptor::ShouldHandleRequest(
243 const net::URLRequest* request) const {
244 return request->url().SchemeIs(url::kContentScheme);
247 } // namespace
249 namespace android_webview {
251 bool RegisterAndroidProtocolHandler(JNIEnv* env) {
252 return RegisterNativesImpl(env);
255 // static
256 scoped_ptr<net::URLRequestInterceptor>
257 CreateContentSchemeRequestInterceptor() {
258 return make_scoped_ptr<net::URLRequestInterceptor>(
259 new ContentSchemeRequestInterceptor());
262 // static
263 scoped_ptr<net::URLRequestInterceptor> CreateAssetFileRequestInterceptor() {
264 return scoped_ptr<net::URLRequestInterceptor>(
265 new AssetFileRequestInterceptor());
268 // Set a context object to be used for resolving resource queries. This can
269 // be used to override the default application context and redirect all
270 // resource queries to a specific context object, e.g., for the purposes of
271 // testing.
273 // |context| should be a android.content.Context instance or NULL to enable
274 // the use of the standard application context.
275 static void SetResourceContextForTesting(JNIEnv* env,
276 const JavaParamRef<jclass>& /*clazz*/,
277 const JavaParamRef<jobject>& context) {
278 if (context) {
279 ResetResourceContext(new JavaObjectWeakGlobalRef(env, context));
280 } else {
281 ResetResourceContext(NULL);
285 static ScopedJavaLocalRef<jstring> GetAndroidAssetPath(
286 JNIEnv* env,
287 const JavaParamRef<jclass>& /*clazz*/) {
288 return ConvertUTF8ToJavaString(env, android_webview::kAndroidAssetPath);
291 static ScopedJavaLocalRef<jstring> GetAndroidResourcePath(
292 JNIEnv* env,
293 const JavaParamRef<jclass>& /*clazz*/) {
294 return ConvertUTF8ToJavaString(env, android_webview::kAndroidResourcePath);
297 } // namespace android_webview