| 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 // URL request job for reading from resources and assets. | 5 // URL request job for reading from resources and assets. |
| 6 | 6 |
| 7 #include "android_webview/native/android_protocol_handler.h" | 7 #include "android_webview/native/android_protocol_handler.h" |
| 8 #include "android_webview/native/input_stream.h" |
| 8 | 9 |
| 9 #include "android_webview/common/url_constants.h" | 10 #include "android_webview/common/url_constants.h" |
| 10 #include "android_webview/native/android_stream_reader_url_request_job.h" | 11 #include "android_webview/native/android_stream_reader_url_request_job.h" |
| 11 #include "base/android/jni_android.h" | 12 #include "base/android/jni_android.h" |
| 12 #include "base/android/jni_helper.h" | 13 #include "base/android/jni_helper.h" |
| 13 #include "base/android/jni_string.h" | 14 #include "base/android/jni_string.h" |
| 14 #include "base/string_util.h" | 15 #include "base/string_util.h" |
| 15 #include "content/public/common/url_constants.h" | 16 #include "content/public/common/url_constants.h" |
| 16 #include "googleurl/src/gurl.h" | 17 #include "googleurl/src/gurl.h" |
| 17 #include "jni/AndroidProtocolHandler_jni.h" | 18 #include "jni/AndroidProtocolHandler_jni.h" |
| (...skipping 22 matching lines...) Expand all Loading... |
| 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<android_webview::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 android_webview::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 android_webview::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 Loading... |
| 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<android_webview::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<android_webview::InputStream>(); |
| 146 } | 147 } |
| 147 return stream; | 148 return make_scoped_ptr(new android_webview::InputStream(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()); |
| 166 ScopedJavaLocalRef<jstring> returned_type = | 164 ScopedJavaLocalRef<jstring> returned_type = |
| 167 android_webview::Java_AndroidProtocolHandler_getMimeType( | 165 android_webview::Java_AndroidProtocolHandler_getMimeType( |
| 168 env, | 166 env, |
| 169 GetResourceContext(env).obj(), | 167 GetResourceContext(env).obj(), |
| 170 stream, url.obj()); | 168 stream.jobj(), url.obj()); |
| 171 if (ClearException(env) || returned_type.is_null()) | 169 if (ClearException(env) || returned_type.is_null()) |
| 172 return false; | 170 return false; |
| 173 | 171 |
| 174 *mime_type = base::android::ConvertJavaStringToUTF8(returned_type); | 172 *mime_type = base::android::ConvertJavaStringToUTF8(returned_type); |
| 175 return true; | 173 return true; |
| 176 } | 174 } |
| 177 | 175 |
| 178 bool AndroidStreamReaderURLRequestJobDelegateImpl::GetCharset( | 176 bool AndroidStreamReaderURLRequestJobDelegateImpl::GetCharset( |
| 179 JNIEnv* env, | 177 JNIEnv* env, |
| 180 net::URLRequest* request, | 178 net::URLRequest* request, |
| 181 jobject stream, | 179 const android_webview::InputStream& stream, |
| 182 std::string* charset) { | 180 std::string* charset) { |
| 183 // TODO: We should probably be getting this from the managed side. | 181 // TODO: We should probably be getting this from the managed side. |
| 184 return false; | 182 return false; |
| 185 } | 183 } |
| 186 | 184 |
| 187 AssetFileProtocolInterceptor::AssetFileProtocolInterceptor() | 185 AssetFileProtocolInterceptor::AssetFileProtocolInterceptor() |
| 188 : asset_prefix_(std::string(chrome::kFileScheme) + | 186 : asset_prefix_(std::string(chrome::kFileScheme) + |
| 189 std::string(content::kStandardSchemeSeparator) + | 187 std::string(content::kStandardSchemeSeparator) + |
| 190 android_webview::kAndroidAssetPath), | 188 android_webview::kAndroidAssetPath), |
| 191 resource_prefix_(std::string(chrome::kFileScheme) + | 189 resource_prefix_(std::string(chrome::kFileScheme) + |
| (...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 271 env, android_webview::kAndroidAssetPath).Release(); | 269 env, android_webview::kAndroidAssetPath).Release(); |
| 272 } | 270 } |
| 273 | 271 |
| 274 static jstring GetAndroidResourcePath(JNIEnv* env, jclass /*clazz*/) { | 272 static jstring GetAndroidResourcePath(JNIEnv* env, jclass /*clazz*/) { |
| 275 // OK to release, JNI binding. | 273 // OK to release, JNI binding. |
| 276 return ConvertUTF8ToJavaString( | 274 return ConvertUTF8ToJavaString( |
| 277 env, android_webview::kAndroidResourcePath).Release(); | 275 env, android_webview::kAndroidResourcePath).Release(); |
| 278 } | 276 } |
| 279 | 277 |
| 280 } // namespace android_webview | 278 } // namespace android_webview |
| OLD | NEW |