download_callback_proxy.cc 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. // Copyright 2019 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 "weblayer/browser/download_callback_proxy.h"
  5. #include "base/android/jni_string.h"
  6. #include "base/trace_event/trace_event.h"
  7. #include "url/android/gurl_android.h"
  8. #include "url/gurl.h"
  9. #include "weblayer/browser/download_impl.h"
  10. #include "weblayer/browser/java/jni/DownloadCallbackProxy_jni.h"
  11. #include "weblayer/browser/profile_impl.h"
  12. #include "weblayer/browser/tab_impl.h"
  13. using base::android::AttachCurrentThread;
  14. using base::android::ConvertUTF8ToJavaString;
  15. using base::android::ScopedJavaLocalRef;
  16. namespace weblayer {
  17. DownloadCallbackProxy::DownloadCallbackProxy(JNIEnv* env,
  18. jobject obj,
  19. Profile* profile)
  20. : profile_(profile), java_delegate_(env, obj) {
  21. profile_->SetDownloadDelegate(this);
  22. }
  23. DownloadCallbackProxy::~DownloadCallbackProxy() {
  24. profile_->SetDownloadDelegate(nullptr);
  25. }
  26. bool DownloadCallbackProxy::InterceptDownload(
  27. const GURL& url,
  28. const std::string& user_agent,
  29. const std::string& content_disposition,
  30. const std::string& mime_type,
  31. int64_t content_length) {
  32. JNIEnv* env = AttachCurrentThread();
  33. ScopedJavaLocalRef<jstring> jstring_url(
  34. ConvertUTF8ToJavaString(env, url.spec()));
  35. ScopedJavaLocalRef<jstring> jstring_user_agent(
  36. ConvertUTF8ToJavaString(env, user_agent));
  37. ScopedJavaLocalRef<jstring> jstring_content_disposition(
  38. ConvertUTF8ToJavaString(env, content_disposition));
  39. ScopedJavaLocalRef<jstring> jstring_mime_type(
  40. ConvertUTF8ToJavaString(env, mime_type));
  41. TRACE_EVENT0("weblayer", "Java_DownloadCallbackProxy_interceptDownload");
  42. return Java_DownloadCallbackProxy_interceptDownload(
  43. env, java_delegate_, jstring_url, jstring_user_agent,
  44. jstring_content_disposition, jstring_mime_type, content_length);
  45. }
  46. void DownloadCallbackProxy::AllowDownload(
  47. Tab* tab,
  48. const GURL& url,
  49. const std::string& request_method,
  50. absl::optional<url::Origin> request_initiator,
  51. AllowDownloadCallback callback) {
  52. JNIEnv* env = AttachCurrentThread();
  53. ScopedJavaLocalRef<jstring> jstring_url(
  54. ConvertUTF8ToJavaString(env, url.spec()));
  55. ScopedJavaLocalRef<jstring> jstring_method(
  56. ConvertUTF8ToJavaString(env, request_method));
  57. ScopedJavaLocalRef<jstring> jstring_request_initator;
  58. if (request_initiator)
  59. jstring_request_initator =
  60. ConvertUTF8ToJavaString(env, request_initiator->Serialize());
  61. // Make copy on the heap so we can pass the pointer through JNI. This will be
  62. // deleted when it's run.
  63. intptr_t callback_id = reinterpret_cast<intptr_t>(
  64. new AllowDownloadCallback(std::move(callback)));
  65. Java_DownloadCallbackProxy_allowDownload(
  66. env, java_delegate_, static_cast<TabImpl*>(tab)->GetJavaTab(),
  67. jstring_url, jstring_method, jstring_request_initator, callback_id);
  68. }
  69. void DownloadCallbackProxy::DownloadStarted(Download* download) {
  70. DownloadImpl* download_impl = static_cast<DownloadImpl*>(download);
  71. JNIEnv* env = AttachCurrentThread();
  72. Java_DownloadCallbackProxy_createDownload(
  73. env, java_delegate_, reinterpret_cast<jlong>(download_impl),
  74. download_impl->GetNotificationId(), download_impl->IsTransient(),
  75. url::GURLAndroid::FromNativeGURL(env, download_impl->GetSourceUrl()));
  76. Java_DownloadCallbackProxy_downloadStarted(env, java_delegate_,
  77. download_impl->java_download());
  78. }
  79. void DownloadCallbackProxy::DownloadProgressChanged(Download* download) {
  80. DownloadImpl* download_impl = static_cast<DownloadImpl*>(download);
  81. Java_DownloadCallbackProxy_downloadProgressChanged(
  82. AttachCurrentThread(), java_delegate_, download_impl->java_download());
  83. }
  84. void DownloadCallbackProxy::DownloadCompleted(Download* download) {
  85. DownloadImpl* download_impl = static_cast<DownloadImpl*>(download);
  86. Java_DownloadCallbackProxy_downloadCompleted(
  87. AttachCurrentThread(), java_delegate_, download_impl->java_download());
  88. }
  89. void DownloadCallbackProxy::DownloadFailed(Download* download) {
  90. DownloadImpl* download_impl = static_cast<DownloadImpl*>(download);
  91. Java_DownloadCallbackProxy_downloadFailed(
  92. AttachCurrentThread(), java_delegate_, download_impl->java_download());
  93. }
  94. static jlong JNI_DownloadCallbackProxy_CreateDownloadCallbackProxy(
  95. JNIEnv* env,
  96. const base::android::JavaParamRef<jobject>& proxy,
  97. jlong profile) {
  98. return reinterpret_cast<jlong>(new DownloadCallbackProxy(
  99. env, proxy, reinterpret_cast<ProfileImpl*>(profile)));
  100. }
  101. static void JNI_DownloadCallbackProxy_DeleteDownloadCallbackProxy(JNIEnv* env,
  102. jlong proxy) {
  103. delete reinterpret_cast<DownloadCallbackProxy*>(proxy);
  104. }
  105. static void JNI_DownloadCallbackProxy_AllowDownload(JNIEnv* env,
  106. jlong callback_id,
  107. jboolean allow) {
  108. std::unique_ptr<AllowDownloadCallback> cb(
  109. reinterpret_cast<AllowDownloadCallback*>(callback_id));
  110. std::move(*cb).Run(allow);
  111. }
  112. } // namespace weblayer