Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(533)

Side by Side Diff: android_webview/native/android_protocol_handler.cc

Issue 11363123: [android_webview] Don't block the IO thread when reading from an InputStream. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 8 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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 // URL request job for reading from resources and assets.
6
7 #include "android_webview/native/android_protocol_handler.h" 5 #include "android_webview/native/android_protocol_handler.h"
8 6
7 #include "android_webview/browser/net/android_stream_reader_url_request_job.h"
9 #include "android_webview/common/url_constants.h" 8 #include "android_webview/common/url_constants.h"
10 #include "android_webview/native/android_stream_reader_url_request_job.h" 9 #include "android_webview/native/input_stream_impl.h"
11 #include "base/android/jni_android.h" 10 #include "base/android/jni_android.h"
12 #include "base/android/jni_helper.h" 11 #include "base/android/jni_helper.h"
13 #include "base/android/jni_string.h" 12 #include "base/android/jni_string.h"
14 #include "base/string_util.h" 13 #include "base/string_util.h"
15 #include "content/public/common/url_constants.h" 14 #include "content/public/common/url_constants.h"
16 #include "googleurl/src/gurl.h" 15 #include "googleurl/src/gurl.h"
17 #include "jni/AndroidProtocolHandler_jni.h" 16 #include "jni/AndroidProtocolHandler_jni.h"
18 #include "net/base/io_buffer.h" 17 #include "net/base/io_buffer.h"
19 #include "net/base/mime_util.h" 18 #include "net/base/mime_util.h"
20 #include "net/base/net_errors.h" 19 #include "net/base/net_errors.h"
21 #include "net/base/net_util.h" 20 #include "net/base/net_util.h"
22 #include "net/http/http_util.h" 21 #include "net/http/http_util.h"
23 #include "net/url_request/url_request.h" 22 #include "net/url_request/url_request.h"
24 #include "net/url_request/url_request_job_factory.h" 23 #include "net/url_request/url_request_job_factory.h"
25 24
25 using android_webview::InputStream;
26 using android_webview::InputStreamImpl;
26 using base::android::AttachCurrentThread; 27 using base::android::AttachCurrentThread;
27 using base::android::ClearException; 28 using base::android::ClearException;
28 using base::android::ConvertUTF8ToJavaString; 29 using base::android::ConvertUTF8ToJavaString;
29 using base::android::ScopedJavaGlobalRef; 30 using base::android::ScopedJavaGlobalRef;
30 using base::android::ScopedJavaLocalRef; 31 using base::android::ScopedJavaLocalRef;
31 32
32 namespace { 33 namespace {
33 34
34 // Override resource context for reading resource and asset files. Used for 35 // Override resource context for reading resource and asset files. Used for
35 // testing. 36 // testing.
36 JavaObjectWeakGlobalRef* g_resource_context = NULL; 37 JavaObjectWeakGlobalRef* g_resource_context = NULL;
37 38
38 void ResetResourceContext(JavaObjectWeakGlobalRef* ref) { 39 void ResetResourceContext(JavaObjectWeakGlobalRef* ref) {
39 if (g_resource_context) 40 if (g_resource_context)
40 delete g_resource_context; 41 delete g_resource_context;
41 42
42 g_resource_context = ref; 43 g_resource_context = ref;
43 } 44 }
44 45
45 class AndroidStreamReaderURLRequestJobDelegateImpl 46 class AndroidStreamReaderURLRequestJobDelegateImpl
46 : public AndroidStreamReaderURLRequestJob::Delegate { 47 : public AndroidStreamReaderURLRequestJob::Delegate {
47 public: 48 public:
48 AndroidStreamReaderURLRequestJobDelegateImpl(); 49 AndroidStreamReaderURLRequestJobDelegateImpl();
49 50
50 virtual ScopedJavaLocalRef<jobject> OpenInputStream( 51 virtual scoped_ptr<InputStream> OpenInputStream(
51 JNIEnv* env, 52 JNIEnv* env,
52 net::URLRequest* request) OVERRIDE; 53 net::URLRequest* request) OVERRIDE;
53 54
54 virtual bool GetMimeType(JNIEnv* env, 55 virtual bool GetMimeType(JNIEnv* env,
55 net::URLRequest* request, 56 net::URLRequest* request,
56 jobject stream, 57 const InputStream& stream,
57 std::string* mime_type) OVERRIDE; 58 std::string* mime_type) OVERRIDE;
58 59
59 virtual bool GetCharset(JNIEnv* env, 60 virtual bool GetCharset(JNIEnv* env,
60 net::URLRequest* request, 61 net::URLRequest* request,
61 jobject stream, 62 const InputStream& stream,
62 std::string* charset) OVERRIDE; 63 std::string* charset) OVERRIDE;
63 64
64 virtual ~AndroidStreamReaderURLRequestJobDelegateImpl(); 65 virtual ~AndroidStreamReaderURLRequestJobDelegateImpl();
65 }; 66 };
66 67
67 class AssetFileProtocolInterceptor : 68 class AssetFileProtocolInterceptor :
68 public net::URLRequestJobFactory::Interceptor { 69 public net::URLRequestJobFactory::Interceptor {
69 public: 70 public:
70 AssetFileProtocolInterceptor(); 71 AssetFileProtocolInterceptor();
71 virtual ~AssetFileProtocolInterceptor() OVERRIDE; 72 virtual ~AssetFileProtocolInterceptor() OVERRIDE;
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after
117 } 118 }
118 119
119 AndroidStreamReaderURLRequestJobDelegateImpl:: 120 AndroidStreamReaderURLRequestJobDelegateImpl::
120 AndroidStreamReaderURLRequestJobDelegateImpl() { 121 AndroidStreamReaderURLRequestJobDelegateImpl() {
121 } 122 }
122 123
123 AndroidStreamReaderURLRequestJobDelegateImpl:: 124 AndroidStreamReaderURLRequestJobDelegateImpl::
124 ~AndroidStreamReaderURLRequestJobDelegateImpl() { 125 ~AndroidStreamReaderURLRequestJobDelegateImpl() {
125 } 126 }
126 127
127 ScopedJavaLocalRef<jobject> 128 scoped_ptr<InputStream>
128 AndroidStreamReaderURLRequestJobDelegateImpl::OpenInputStream( 129 AndroidStreamReaderURLRequestJobDelegateImpl::OpenInputStream(
129 JNIEnv* env, net::URLRequest* request) { 130 JNIEnv* env, net::URLRequest* request) {
130 DCHECK(request); 131 DCHECK(request);
131 DCHECK(env); 132 DCHECK(env);
132 133
133 // Open the input stream. 134 // Open the input stream.
134 ScopedJavaLocalRef<jstring> url = 135 ScopedJavaLocalRef<jstring> url =
135 ConvertUTF8ToJavaString(env, request->url().spec()); 136 ConvertUTF8ToJavaString(env, request->url().spec());
136 ScopedJavaLocalRef<jobject> stream = 137 ScopedJavaLocalRef<jobject> stream =
137 android_webview::Java_AndroidProtocolHandler_open( 138 android_webview::Java_AndroidProtocolHandler_open(
138 env, 139 env,
139 GetResourceContext(env).obj(), 140 GetResourceContext(env).obj(),
140 url.obj()); 141 url.obj());
141 142
142 // Check and clear pending exceptions. 143 // Check and clear pending exceptions.
143 if (ClearException(env) || stream.is_null()) { 144 if (ClearException(env) || stream.is_null()) {
144 DLOG(ERROR) << "Unable to open input stream for Android URL"; 145 DLOG(ERROR) << "Unable to open input stream for Android URL";
145 return ScopedJavaLocalRef<jobject>(env, NULL); 146 return scoped_ptr<InputStream>();
146 } 147 }
147 return stream; 148 return make_scoped_ptr<InputStream>(new InputStreamImpl(stream));
148 } 149 }
149 150
150 bool AndroidStreamReaderURLRequestJobDelegateImpl::GetMimeType( 151 bool AndroidStreamReaderURLRequestJobDelegateImpl::GetMimeType(
151 JNIEnv* env, 152 JNIEnv* env,
152 net::URLRequest* request, 153 net::URLRequest* request,
153 jobject stream, 154 const android_webview::InputStream& stream,
154 std::string* mime_type) { 155 std::string* mime_type) {
155 DCHECK(env); 156 DCHECK(env);
156 DCHECK(request); 157 DCHECK(request);
157 DCHECK(mime_type); 158 DCHECK(mime_type);
158 159
159 if (!stream)
160 return false;
161
162 // Query the mime type from the Java side. It is possible for the query to 160 // Query the mime type from the Java side. It is possible for the query to
163 // fail, as the mime type cannot be determined for all supported schemes. 161 // fail, as the mime type cannot be determined for all supported schemes.
164 ScopedJavaLocalRef<jstring> url = 162 ScopedJavaLocalRef<jstring> url =
165 ConvertUTF8ToJavaString(env, request->url().spec()); 163 ConvertUTF8ToJavaString(env, request->url().spec());
164 const InputStreamImpl* stream_impl =
165 InputStreamImpl::FromInputStream(&stream);
166 ScopedJavaLocalRef<jstring> returned_type = 166 ScopedJavaLocalRef<jstring> returned_type =
167 android_webview::Java_AndroidProtocolHandler_getMimeType( 167 android_webview::Java_AndroidProtocolHandler_getMimeType(
168 env, 168 env,
169 GetResourceContext(env).obj(), 169 GetResourceContext(env).obj(),
170 stream, url.obj()); 170 stream_impl->jobj(), url.obj());
171 if (ClearException(env) || returned_type.is_null()) 171 if (ClearException(env) || returned_type.is_null())
172 return false; 172 return false;
173 173
174 *mime_type = base::android::ConvertJavaStringToUTF8(returned_type); 174 *mime_type = base::android::ConvertJavaStringToUTF8(returned_type);
175 return true; 175 return true;
176 } 176 }
177 177
178 bool AndroidStreamReaderURLRequestJobDelegateImpl::GetCharset( 178 bool AndroidStreamReaderURLRequestJobDelegateImpl::GetCharset(
179 JNIEnv* env, 179 JNIEnv* env,
180 net::URLRequest* request, 180 net::URLRequest* request,
181 jobject stream, 181 const android_webview::InputStream& stream,
182 std::string* charset) { 182 std::string* charset) {
183 // TODO: We should probably be getting this from the managed side. 183 // TODO: We should probably be getting this from the managed side.
184 return false; 184 return false;
185 } 185 }
186 186
187 AssetFileProtocolInterceptor::AssetFileProtocolInterceptor() 187 AssetFileProtocolInterceptor::AssetFileProtocolInterceptor()
188 : asset_prefix_(std::string(chrome::kFileScheme) + 188 : asset_prefix_(std::string(chrome::kFileScheme) +
189 std::string(content::kStandardSchemeSeparator) + 189 std::string(content::kStandardSchemeSeparator) +
190 android_webview::kAndroidAssetPath), 190 android_webview::kAndroidAssetPath),
191 resource_prefix_(std::string(chrome::kFileScheme) + 191 resource_prefix_(std::string(chrome::kFileScheme) +
(...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after
271 env, android_webview::kAndroidAssetPath).Release(); 271 env, android_webview::kAndroidAssetPath).Release();
272 } 272 }
273 273
274 static jstring GetAndroidResourcePath(JNIEnv* env, jclass /*clazz*/) { 274 static jstring GetAndroidResourcePath(JNIEnv* env, jclass /*clazz*/) {
275 // OK to release, JNI binding. 275 // OK to release, JNI binding.
276 return ConvertUTF8ToJavaString( 276 return ConvertUTF8ToJavaString(
277 env, android_webview::kAndroidResourcePath).Release(); 277 env, android_webview::kAndroidResourcePath).Release();
278 } 278 }
279 279
280 } // namespace android_webview 280 } // namespace android_webview
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698