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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
|
// Copyright 2015 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 "chrome/browser/android/download/download_manager_service.h"
#include "base/android/jni_string.h"
#include "base/message_loop/message_loop.h"
#include "base/time/time.h"
#include "chrome/browser/profiles/profile_manager.h"
#include "content/public/browser/android/download_controller_android.h"
#include "content/public/browser/browser_context.h"
#include "content/public/browser/download_item.h"
#include "jni/DownloadManagerService_jni.h"
using base::android::JavaParamRef;
using base::android::ConvertJavaStringToUTF8;
using base::android::ConvertUTF8ToJavaString;
namespace {
// The retry interval when resuming/canceling a download. This is needed because
// when the browser process is launched, we have to wait until the download
// history get loaded before a download can be resumed/cancelled. However,
// we don't want to retry after a long period of time as the same download Id
// can be reused later.
const int kRetryIntervalInMilliseconds = 3000;
}
// static
bool DownloadManagerService::RegisterDownloadManagerService(JNIEnv* env) {
return RegisterNativesImpl(env);
}
static jlong Init(JNIEnv* env, const JavaParamRef<jobject>& jobj) {
Profile* profile = ProfileManager::GetActiveUserProfile();
content::DownloadManager* manager =
content::BrowserContext::GetDownloadManager(profile);
DownloadManagerService* service =
new DownloadManagerService(env, jobj, manager);
return reinterpret_cast<intptr_t>(service);
}
DownloadManagerService::DownloadManagerService(
JNIEnv* env,
jobject obj,
content::DownloadManager* manager)
: java_ref_(env, obj), manager_(manager) {
manager_->AddObserver(this);
}
DownloadManagerService::~DownloadManagerService() {
if (manager_)
manager_->RemoveObserver(this);
}
void DownloadManagerService::ResumeDownload(JNIEnv* env,
jobject obj,
uint32_t download_id,
jstring fileName) {
ResumeDownloadInternal(download_id, ConvertJavaStringToUTF8(env, fileName),
true);
}
void DownloadManagerService::CancelDownload(JNIEnv* env,
jobject obj,
uint32_t download_id) {
CancelDownloadInternal(download_id, true);
}
void DownloadManagerService::ManagerGoingDown(
content::DownloadManager* manager) {
manager_ = nullptr;
}
void DownloadManagerService::ResumeDownloadItem(content::DownloadItem* item,
const std::string& fileName) {
if (!item->CanResume()) {
OnResumptionFailed(item->GetId(), fileName);
return;
}
item->AddObserver(content::DownloadControllerAndroid::Get());
item->Resume();
if (!resume_callback_for_testing_.is_null())
resume_callback_for_testing_.Run(true);
}
void DownloadManagerService::ResumeDownloadInternal(uint32_t download_id,
const std::string& fileName,
bool retry) {
if (!manager_) {
OnResumptionFailed(download_id, fileName);
return;
}
content::DownloadItem* item = manager_->GetDownload(download_id);
if (item) {
ResumeDownloadItem(item, fileName);
return;
}
if (!retry) {
OnResumptionFailed(download_id, fileName);
return;
}
// Post a delayed task to wait for the download history to load the download
// item. If the download item is not loaded when the delayed task runs, show
// an download failed notification. Alternatively, we can have the
// DownloadManager inform us when a download item is created. However, there
// is no guarantee when the download item will be created, since the newly
// created item might not be loaded from download history. So user might wait
// indefinitely to see the failed notification. See http://crbug.com/577893.
base::MessageLoop::current()->PostDelayedTask(
FROM_HERE,
base::Bind(&DownloadManagerService::ResumeDownloadInternal,
base::Unretained(this), download_id, fileName, false),
base::TimeDelta::FromMilliseconds(kRetryIntervalInMilliseconds));
}
void DownloadManagerService::CancelDownloadInternal(uint32_t download_id,
bool retry) {
if (!manager_)
return;
content::DownloadItem* item = manager_->GetDownload(download_id);
if (item) {
item->Cancel(true);
return;
}
if (retry) {
base::MessageLoop::current()->PostDelayedTask(
FROM_HERE, base::Bind(&DownloadManagerService::CancelDownloadInternal,
base::Unretained(this), download_id, false),
base::TimeDelta::FromMilliseconds(kRetryIntervalInMilliseconds));
}
}
void DownloadManagerService::OnResumptionFailed(uint32_t download_id,
const std::string& fileName) {
if (!java_ref_.is_null()) {
JNIEnv* env = base::android::AttachCurrentThread();
Java_DownloadManagerService_onResumptionFailed(
env, java_ref_.obj(), download_id,
ConvertUTF8ToJavaString(env, fileName).obj());
}
if (!resume_callback_for_testing_.is_null())
resume_callback_for_testing_.Run(false);
}
|