android_protocol_handler.cc 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  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.
  4. #include "android_webview/browser/android_protocol_handler.h"
  5. #include <memory>
  6. #include <utility>
  7. #include "android_webview/browser_jni_headers/AndroidProtocolHandler_jni.h"
  8. #include "android_webview/common/url_constants.h"
  9. #include "base/android/jni_android.h"
  10. #include "base/android/jni_string.h"
  11. #include "base/android/jni_weak_ref.h"
  12. #include "components/embedder_support/android/util/input_stream.h"
  13. #include "content/public/common/url_constants.h"
  14. #include "net/base/io_buffer.h"
  15. #include "net/base/mime_util.h"
  16. #include "net/base/net_errors.h"
  17. #include "net/http/http_util.h"
  18. #include "url/android/gurl_android.h"
  19. #include "url/gurl.h"
  20. #include "url/url_constants.h"
  21. using base::android::AttachCurrentThread;
  22. using base::android::ClearException;
  23. using base::android::ConvertUTF8ToJavaString;
  24. using base::android::JavaParamRef;
  25. using base::android::ScopedJavaGlobalRef;
  26. using base::android::ScopedJavaLocalRef;
  27. using embedder_support::InputStream;
  28. namespace android_webview {
  29. // static
  30. std::unique_ptr<InputStream> CreateInputStream(JNIEnv* env, const GURL& url) {
  31. DCHECK(url.is_valid());
  32. DCHECK(env);
  33. // Open the input stream.
  34. ScopedJavaLocalRef<jobject> stream =
  35. android_webview::Java_AndroidProtocolHandler_open(
  36. env, url::GURLAndroid::FromNativeGURL(env, url));
  37. if (!stream) {
  38. DLOG(ERROR) << "Unable to open input stream for Android URL";
  39. return nullptr;
  40. }
  41. return std::make_unique<InputStream>(stream);
  42. }
  43. bool GetInputStreamMimeType(JNIEnv* env,
  44. const GURL& url,
  45. embedder_support::InputStream* stream,
  46. std::string* mime_type) {
  47. // Query the mime type from the Java side. It is possible for the query to
  48. // fail, as the mime type cannot be determined for all supported schemes.
  49. ScopedJavaLocalRef<jstring> returned_type =
  50. android_webview::Java_AndroidProtocolHandler_getMimeType(
  51. env, stream->jobj(), url::GURLAndroid::FromNativeGURL(env, url));
  52. if (!returned_type)
  53. return false;
  54. *mime_type = base::android::ConvertJavaStringToUTF8(returned_type);
  55. return true;
  56. }
  57. static ScopedJavaLocalRef<jstring>
  58. JNI_AndroidProtocolHandler_GetAndroidAssetPath(JNIEnv* env) {
  59. return ConvertUTF8ToJavaString(env, android_webview::kAndroidAssetPath);
  60. }
  61. static ScopedJavaLocalRef<jstring>
  62. JNI_AndroidProtocolHandler_GetAndroidResourcePath(JNIEnv* env) {
  63. return ConvertUTF8ToJavaString(env, android_webview::kAndroidResourcePath);
  64. }
  65. } // namespace android_webview