OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "android_webview/native/android_protocol_handler.h" | 5 #include "android_webview/native/android_protocol_handler.h" |
6 | 6 |
7 #include <utility> | 7 #include <utility> |
8 | 8 |
9 #include "android_webview/browser/net/android_stream_reader_url_request_job.h" | 9 #include "android_webview/browser/net/android_stream_reader_url_request_job.h" |
10 #include "android_webview/browser/net/aw_url_request_job_factory.h" | 10 #include "android_webview/browser/net/aw_url_request_job_factory.h" |
11 #include "android_webview/common/url_constants.h" | 11 #include "android_webview/common/url_constants.h" |
12 #include "android_webview/native/input_stream_impl.h" | 12 #include "android_webview/native/input_stream_impl.h" |
13 #include "base/android/context_utils.h" | |
14 #include "base/android/jni_android.h" | 13 #include "base/android/jni_android.h" |
15 #include "base/android/jni_string.h" | 14 #include "base/android/jni_string.h" |
16 #include "base/android/jni_weak_ref.h" | 15 #include "base/android/jni_weak_ref.h" |
17 #include "base/memory/ptr_util.h" | 16 #include "base/memory/ptr_util.h" |
18 #include "content/public/common/url_constants.h" | 17 #include "content/public/common/url_constants.h" |
19 #include "jni/AndroidProtocolHandler_jni.h" | 18 #include "jni/AndroidProtocolHandler_jni.h" |
20 #include "net/base/io_buffer.h" | 19 #include "net/base/io_buffer.h" |
21 #include "net/base/mime_util.h" | 20 #include "net/base/mime_util.h" |
22 #include "net/base/net_errors.h" | 21 #include "net/base/net_errors.h" |
23 #include "net/http/http_util.h" | 22 #include "net/http/http_util.h" |
24 #include "net/url_request/url_request.h" | 23 #include "net/url_request/url_request.h" |
25 #include "net/url_request/url_request_interceptor.h" | 24 #include "net/url_request/url_request_interceptor.h" |
26 #include "url/gurl.h" | 25 #include "url/gurl.h" |
27 #include "url/url_constants.h" | 26 #include "url/url_constants.h" |
28 | 27 |
29 using android_webview::AndroidStreamReaderURLRequestJob; | 28 using android_webview::AndroidStreamReaderURLRequestJob; |
30 using android_webview::InputStream; | 29 using android_webview::InputStream; |
31 using android_webview::InputStreamImpl; | 30 using android_webview::InputStreamImpl; |
32 using base::android::AttachCurrentThread; | 31 using base::android::AttachCurrentThread; |
33 using base::android::ClearException; | 32 using base::android::ClearException; |
34 using base::android::ConvertUTF8ToJavaString; | 33 using base::android::ConvertUTF8ToJavaString; |
35 using base::android::JavaParamRef; | 34 using base::android::JavaParamRef; |
36 using base::android::ScopedJavaGlobalRef; | 35 using base::android::ScopedJavaGlobalRef; |
37 using base::android::ScopedJavaLocalRef; | 36 using base::android::ScopedJavaLocalRef; |
38 | 37 |
39 namespace { | 38 namespace { |
40 | 39 |
41 // Override resource context for reading resource and asset files. Used for | |
42 // testing. | |
43 JavaObjectWeakGlobalRef* g_resource_context = NULL; | |
44 | |
45 void ResetResourceContext(JavaObjectWeakGlobalRef* ref) { | |
46 if (g_resource_context) | |
47 delete g_resource_context; | |
48 | |
49 g_resource_context = ref; | |
50 } | |
51 | |
52 void* kPreviouslyFailedKey = &kPreviouslyFailedKey; | 40 void* kPreviouslyFailedKey = &kPreviouslyFailedKey; |
53 | 41 |
54 void MarkRequestAsFailed(net::URLRequest* request) { | 42 void MarkRequestAsFailed(net::URLRequest* request) { |
55 request->SetUserData(kPreviouslyFailedKey, | 43 request->SetUserData(kPreviouslyFailedKey, |
56 new base::SupportsUserData::Data()); | 44 new base::SupportsUserData::Data()); |
57 } | 45 } |
58 | 46 |
59 bool HasRequestPreviouslyFailed(net::URLRequest* request) { | 47 bool HasRequestPreviouslyFailed(net::URLRequest* request) { |
60 return request->GetUserData(kPreviouslyFailedKey) != NULL; | 48 return request->GetUserData(kPreviouslyFailedKey) != NULL; |
61 } | 49 } |
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
102 bool ShouldHandleRequest(const net::URLRequest* request) const override; | 90 bool ShouldHandleRequest(const net::URLRequest* request) const override; |
103 }; | 91 }; |
104 | 92 |
105 // Protocol handler for content:// scheme requests. | 93 // Protocol handler for content:// scheme requests. |
106 class ContentSchemeRequestInterceptor : public AndroidRequestInterceptorBase { | 94 class ContentSchemeRequestInterceptor : public AndroidRequestInterceptorBase { |
107 public: | 95 public: |
108 ContentSchemeRequestInterceptor(); | 96 ContentSchemeRequestInterceptor(); |
109 bool ShouldHandleRequest(const net::URLRequest* request) const override; | 97 bool ShouldHandleRequest(const net::URLRequest* request) const override; |
110 }; | 98 }; |
111 | 99 |
112 static ScopedJavaLocalRef<jobject> GetResourceContext(JNIEnv* env) { | |
113 if (g_resource_context) | |
114 return g_resource_context->get(env); | |
115 return ScopedJavaLocalRef<jobject>(base::android::GetApplicationContext()); | |
116 } | |
117 | |
118 // AndroidStreamReaderURLRequestJobDelegateImpl ------------------------------- | 100 // AndroidStreamReaderURLRequestJobDelegateImpl ------------------------------- |
119 | 101 |
120 AndroidStreamReaderURLRequestJobDelegateImpl:: | 102 AndroidStreamReaderURLRequestJobDelegateImpl:: |
121 AndroidStreamReaderURLRequestJobDelegateImpl() {} | 103 AndroidStreamReaderURLRequestJobDelegateImpl() {} |
122 | 104 |
123 AndroidStreamReaderURLRequestJobDelegateImpl:: | 105 AndroidStreamReaderURLRequestJobDelegateImpl:: |
124 ~AndroidStreamReaderURLRequestJobDelegateImpl() { | 106 ~AndroidStreamReaderURLRequestJobDelegateImpl() { |
125 } | 107 } |
126 | 108 |
127 std::unique_ptr<InputStream> | 109 std::unique_ptr<InputStream> |
128 AndroidStreamReaderURLRequestJobDelegateImpl::OpenInputStream(JNIEnv* env, | 110 AndroidStreamReaderURLRequestJobDelegateImpl::OpenInputStream(JNIEnv* env, |
129 const GURL& url) { | 111 const GURL& url) { |
130 DCHECK(url.is_valid()); | 112 DCHECK(url.is_valid()); |
131 DCHECK(env); | 113 DCHECK(env); |
132 | 114 |
133 // Open the input stream. | 115 // Open the input stream. |
134 ScopedJavaLocalRef<jstring> jurl = | 116 ScopedJavaLocalRef<jstring> jurl = |
135 ConvertUTF8ToJavaString(env, url.spec()); | 117 ConvertUTF8ToJavaString(env, url.spec()); |
136 ScopedJavaLocalRef<jobject> stream = | 118 ScopedJavaLocalRef<jobject> stream = |
137 android_webview::Java_AndroidProtocolHandler_open( | 119 android_webview::Java_AndroidProtocolHandler_open(env, jurl); |
138 env, GetResourceContext(env), jurl); | |
139 | 120 |
140 if (stream.is_null()) { | 121 if (stream.is_null()) { |
141 DLOG(ERROR) << "Unable to open input stream for Android URL"; | 122 DLOG(ERROR) << "Unable to open input stream for Android URL"; |
142 return std::unique_ptr<InputStream>(); | 123 return std::unique_ptr<InputStream>(); |
143 } | 124 } |
144 return base::MakeUnique<InputStreamImpl>(stream); | 125 return base::MakeUnique<InputStreamImpl>(stream); |
145 } | 126 } |
146 | 127 |
147 void AndroidStreamReaderURLRequestJobDelegateImpl::OnInputStreamOpenFailed( | 128 void AndroidStreamReaderURLRequestJobDelegateImpl::OnInputStreamOpenFailed( |
148 net::URLRequest* request, | 129 net::URLRequest* request, |
(...skipping 13 matching lines...) Expand all Loading... |
162 DCHECK(mime_type); | 143 DCHECK(mime_type); |
163 | 144 |
164 // Query the mime type from the Java side. It is possible for the query to | 145 // Query the mime type from the Java side. It is possible for the query to |
165 // fail, as the mime type cannot be determined for all supported schemes. | 146 // fail, as the mime type cannot be determined for all supported schemes. |
166 ScopedJavaLocalRef<jstring> url = | 147 ScopedJavaLocalRef<jstring> url = |
167 ConvertUTF8ToJavaString(env, request->url().spec()); | 148 ConvertUTF8ToJavaString(env, request->url().spec()); |
168 const InputStreamImpl* stream_impl = | 149 const InputStreamImpl* stream_impl = |
169 InputStreamImpl::FromInputStream(stream); | 150 InputStreamImpl::FromInputStream(stream); |
170 ScopedJavaLocalRef<jstring> returned_type = | 151 ScopedJavaLocalRef<jstring> returned_type = |
171 android_webview::Java_AndroidProtocolHandler_getMimeType( | 152 android_webview::Java_AndroidProtocolHandler_getMimeType( |
172 env, GetResourceContext(env), stream_impl->jobj(), url); | 153 env, stream_impl->jobj(), url); |
173 if (returned_type.is_null()) | 154 if (returned_type.is_null()) |
174 return false; | 155 return false; |
175 | 156 |
176 *mime_type = base::android::ConvertJavaStringToUTF8(returned_type); | 157 *mime_type = base::android::ConvertJavaStringToUTF8(returned_type); |
177 return true; | 158 return true; |
178 } | 159 } |
179 | 160 |
180 bool AndroidStreamReaderURLRequestJobDelegateImpl::GetCharset( | 161 bool AndroidStreamReaderURLRequestJobDelegateImpl::GetCharset( |
181 JNIEnv* env, | 162 JNIEnv* env, |
182 net::URLRequest* request, | 163 net::URLRequest* request, |
(...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
254 return base::MakeUnique<ContentSchemeRequestInterceptor>(); | 235 return base::MakeUnique<ContentSchemeRequestInterceptor>(); |
255 } | 236 } |
256 | 237 |
257 // static | 238 // static |
258 std::unique_ptr<net::URLRequestInterceptor> | 239 std::unique_ptr<net::URLRequestInterceptor> |
259 CreateAssetFileRequestInterceptor() { | 240 CreateAssetFileRequestInterceptor() { |
260 return std::unique_ptr<net::URLRequestInterceptor>( | 241 return std::unique_ptr<net::URLRequestInterceptor>( |
261 new AssetFileRequestInterceptor()); | 242 new AssetFileRequestInterceptor()); |
262 } | 243 } |
263 | 244 |
264 // Set a context object to be used for resolving resource queries. This can | |
265 // be used to override the default application context and redirect all | |
266 // resource queries to a specific context object, e.g., for the purposes of | |
267 // testing. | |
268 // | |
269 // |context| should be a android.content.Context instance or NULL to enable | |
270 // the use of the standard application context. | |
271 static void SetResourceContextForTesting(JNIEnv* env, | |
272 const JavaParamRef<jclass>& /*clazz*/, | |
273 const JavaParamRef<jobject>& context) { | |
274 if (context) { | |
275 ResetResourceContext(new JavaObjectWeakGlobalRef(env, context)); | |
276 } else { | |
277 ResetResourceContext(NULL); | |
278 } | |
279 } | |
280 | |
281 static ScopedJavaLocalRef<jstring> GetAndroidAssetPath( | 245 static ScopedJavaLocalRef<jstring> GetAndroidAssetPath( |
282 JNIEnv* env, | 246 JNIEnv* env, |
283 const JavaParamRef<jclass>& /*clazz*/) { | 247 const JavaParamRef<jclass>& /*clazz*/) { |
284 return ConvertUTF8ToJavaString(env, android_webview::kAndroidAssetPath); | 248 return ConvertUTF8ToJavaString(env, android_webview::kAndroidAssetPath); |
285 } | 249 } |
286 | 250 |
287 static ScopedJavaLocalRef<jstring> GetAndroidResourcePath( | 251 static ScopedJavaLocalRef<jstring> GetAndroidResourcePath( |
288 JNIEnv* env, | 252 JNIEnv* env, |
289 const JavaParamRef<jclass>& /*clazz*/) { | 253 const JavaParamRef<jclass>& /*clazz*/) { |
290 return ConvertUTF8ToJavaString(env, android_webview::kAndroidResourcePath); | 254 return ConvertUTF8ToJavaString(env, android_webview::kAndroidResourcePath); |
291 } | 255 } |
292 | 256 |
293 } // namespace android_webview | 257 } // namespace android_webview |
OLD | NEW |