summaryrefslogtreecommitdiff
path: root/chromium/weblayer/browser/download_callback_proxy.cc
blob: 0d35bd16630f1d6246892a25a74ee9375c53da60 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
// Copyright 2019 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "weblayer/browser/download_callback_proxy.h"

#include "base/android/jni_string.h"
#include "url/gurl.h"
#include "weblayer/browser/download_impl.h"
#include "weblayer/browser/java/jni/DownloadCallbackProxy_jni.h"
#include "weblayer/browser/profile_impl.h"
#include "weblayer/browser/tab_impl.h"

using base::android::AttachCurrentThread;
using base::android::ConvertUTF8ToJavaString;
using base::android::ScopedJavaLocalRef;

namespace weblayer {

DownloadCallbackProxy::DownloadCallbackProxy(JNIEnv* env,
                                             jobject obj,
                                             Profile* profile)
    : profile_(profile), java_delegate_(env, obj) {
  profile_->SetDownloadDelegate(this);
}

DownloadCallbackProxy::~DownloadCallbackProxy() {
  profile_->SetDownloadDelegate(nullptr);
}

bool DownloadCallbackProxy::InterceptDownload(
    const GURL& url,
    const std::string& user_agent,
    const std::string& content_disposition,
    const std::string& mime_type,
    int64_t content_length) {
  JNIEnv* env = AttachCurrentThread();
  ScopedJavaLocalRef<jstring> jstring_url(
      ConvertUTF8ToJavaString(env, url.spec()));
  ScopedJavaLocalRef<jstring> jstring_user_agent(
      ConvertUTF8ToJavaString(env, user_agent));
  ScopedJavaLocalRef<jstring> jstring_content_disposition(
      ConvertUTF8ToJavaString(env, content_disposition));
  ScopedJavaLocalRef<jstring> jstring_mime_type(
      ConvertUTF8ToJavaString(env, mime_type));
  TRACE_EVENT0("weblayer", "Java_DownloadCallbackProxy_interceptDownload");
  return Java_DownloadCallbackProxy_interceptDownload(
      env, java_delegate_, jstring_url, jstring_user_agent,
      jstring_content_disposition, jstring_mime_type, content_length);
}

void DownloadCallbackProxy::AllowDownload(
    Tab* tab,
    const GURL& url,
    const std::string& request_method,
    base::Optional<url::Origin> request_initiator,
    AllowDownloadCallback callback) {
  JNIEnv* env = AttachCurrentThread();
  ScopedJavaLocalRef<jstring> jstring_url(
      ConvertUTF8ToJavaString(env, url.spec()));
  ScopedJavaLocalRef<jstring> jstring_method(
      ConvertUTF8ToJavaString(env, request_method));
  ScopedJavaLocalRef<jstring> jstring_request_initator;
  if (request_initiator)
    jstring_request_initator =
        ConvertUTF8ToJavaString(env, request_initiator->Serialize());
  // Make copy on the heap so we can pass the pointer through JNI. This will be
  // deleted when it's run.
  intptr_t callback_id = reinterpret_cast<intptr_t>(
      new AllowDownloadCallback(std::move(callback)));
  Java_DownloadCallbackProxy_allowDownload(
      env, java_delegate_, static_cast<TabImpl*>(tab)->GetJavaTab(),
      jstring_url, jstring_method, jstring_request_initator, callback_id);
}

void DownloadCallbackProxy::DownloadStarted(Download* download) {
  DownloadImpl* download_impl = static_cast<DownloadImpl*>(download);
  JNIEnv* env = AttachCurrentThread();
  Java_DownloadCallbackProxy_createDownload(
      env, java_delegate_, reinterpret_cast<jlong>(download_impl),
      download_impl->GetId());
  Java_DownloadCallbackProxy_downloadStarted(env, java_delegate_,
                                             download_impl->java_download());
}

void DownloadCallbackProxy::DownloadProgressChanged(Download* download) {
  DownloadImpl* download_impl = static_cast<DownloadImpl*>(download);
  Java_DownloadCallbackProxy_downloadProgressChanged(
      AttachCurrentThread(), java_delegate_, download_impl->java_download());
}

void DownloadCallbackProxy::DownloadCompleted(Download* download) {
  DownloadImpl* download_impl = static_cast<DownloadImpl*>(download);
  Java_DownloadCallbackProxy_downloadCompleted(
      AttachCurrentThread(), java_delegate_, download_impl->java_download());
}

void DownloadCallbackProxy::DownloadFailed(Download* download) {
  DownloadImpl* download_impl = static_cast<DownloadImpl*>(download);
  Java_DownloadCallbackProxy_downloadFailed(
      AttachCurrentThread(), java_delegate_, download_impl->java_download());
}

static jlong JNI_DownloadCallbackProxy_CreateDownloadCallbackProxy(
    JNIEnv* env,
    const base::android::JavaParamRef<jobject>& proxy,
    jlong profile) {
  return reinterpret_cast<jlong>(new DownloadCallbackProxy(
      env, proxy, reinterpret_cast<ProfileImpl*>(profile)));
}

static void JNI_DownloadCallbackProxy_DeleteDownloadCallbackProxy(JNIEnv* env,
                                                                  jlong proxy) {
  delete reinterpret_cast<DownloadCallbackProxy*>(proxy);
}

static void JNI_DownloadCallbackProxy_AllowDownload(JNIEnv* env,
                                                    jlong callback_id,
                                                    jboolean allow) {
  std::unique_ptr<AllowDownloadCallback> cb(
      reinterpret_cast<AllowDownloadCallback*>(callback_id));
  std::move(*cb).Run(allow);
}

}  // namespace weblayer