cc: Threaded GPU - fix thread binding.
[chromium-blink-merge.git] / android_webview / native / android_protocol_handler.cc
blobc80d9bd41ae0e47e05f67d2cd4420dc589f07e27
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"
24 #include "url/gurl.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();
98 ~AssetFileRequestInterceptor() override;
99 bool ShouldHandleRequest(const net::URLRequest* request) const override;
101 private:
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 {
110 public:
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
121 // and would assert.
122 context.Reset(env, base::android::GetApplicationContext());
123 return context;
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());
139 DCHECK(env);
141 // Open the input stream.
142 ScopedJavaLocalRef<jstring> jurl =
143 ConvertUTF8ToJavaString(env, url.spec());
144 ScopedJavaLocalRef<jobject> stream =
145 android_webview::Java_AndroidProtocolHandler_open(
146 env,
147 GetResourceContext(env).obj(),
148 jurl.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,
159 bool* restart) {
160 DCHECK(!HasRequestPreviouslyFailed(request));
161 MarkRequestAsFailed(request);
162 *restart = true;
165 bool AndroidStreamReaderURLRequestJobDelegateImpl::GetMimeType(
166 JNIEnv* env,
167 net::URLRequest* request,
168 android_webview::InputStream* stream,
169 std::string* mime_type) {
170 DCHECK(env);
171 DCHECK(request);
172 DCHECK(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(
182 env,
183 GetResourceContext(env).obj(),
184 stream_impl->jobj(), url.obj());
185 if (returned_type.is_null())
186 return false;
188 *mime_type = base::android::ConvertJavaStringToUTF8(returned_type);
189 return true;
192 bool AndroidStreamReaderURLRequestJobDelegateImpl::GetCharset(
193 JNIEnv* env,
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.
198 return false;
201 void AndroidStreamReaderURLRequestJobDelegateImpl::AppendResponseHeaders(
202 JNIEnv* env,
203 net::HttpResponseHeaders* headers) {
204 // no-op
207 // AndroidRequestInterceptorBase ----------------------------------------------
209 net::URLRequestJob* AndroidRequestInterceptorBase::MaybeInterceptRequest(
210 net::URLRequest* request,
211 net::NetworkDelegate* network_delegate) const {
212 if (!ShouldHandleRequest(request))
213 return NULL;
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))
226 return NULL;
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())
252 return false;
254 const std::string& url = request->url().spec();
255 if (!StartsWithASCII(url, asset_prefix_, /*case_sensitive=*/ true) &&
256 !StartsWithASCII(url, resource_prefix_, /*case_sensitive=*/ true)) {
257 return false;
260 return true;
263 // ContentSchemeRequestInterceptor --------------------------------------------
265 ContentSchemeRequestInterceptor::ContentSchemeRequestInterceptor() {
268 bool ContentSchemeRequestInterceptor::ShouldHandleRequest(
269 const net::URLRequest* request) const {
270 return request->url().SchemeIs(android_webview::kContentScheme);
273 } // namespace
275 namespace android_webview {
277 bool RegisterAndroidProtocolHandler(JNIEnv* env) {
278 return RegisterNativesImpl(env);
281 // static
282 scoped_ptr<net::URLRequestInterceptor>
283 CreateContentSchemeRequestInterceptor() {
284 return make_scoped_ptr<net::URLRequestInterceptor>(
285 new ContentSchemeRequestInterceptor());
288 // static
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
297 // testing.
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*/,
302 jobject context) {
303 if (context) {
304 ResetResourceContext(new JavaObjectWeakGlobalRef(env, context));
305 } else {
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