forked from chromium/chromium
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchrome_duplicate_download_infobar_delegate.cc
133 lines (111 loc) · 4.29 KB
/
chrome_duplicate_download_infobar_delegate.cc
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
// Copyright 2016 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/chrome_duplicate_download_infobar_delegate.h"
#include <memory>
#include "base/android/path_utils.h"
#include "base/memory/ptr_util.h"
#include "base/metrics/histogram_macros.h"
#include "chrome/browser/android/download/download_controller.h"
#include "chrome/browser/download/download_path_reservation_tracker.h"
#include "chrome/browser/infobars/infobar_service.h"
#include "chrome/browser/ui/android/infobars/duplicate_download_infobar.h"
#include "components/infobars/core/infobar.h"
#include "content/public/browser/browser_context.h"
#include "content/public/browser/browser_thread.h"
namespace {
void CreateNewFileDone(
const DownloadTargetDeterminerDelegate::ConfirmationCallback& callback,
PathValidationResult result,
const base::FilePath& target_path) {
DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
if (result == PathValidationResult::SUCCESS)
callback.Run(DownloadConfirmationResult::CONFIRMED, target_path);
else
callback.Run(DownloadConfirmationResult::FAILED, base::FilePath());
}
} // namespace
namespace chrome {
namespace android {
ChromeDuplicateDownloadInfoBarDelegate::
~ChromeDuplicateDownloadInfoBarDelegate() {
if (download_item_)
download_item_->RemoveObserver(this);
}
// static
void ChromeDuplicateDownloadInfoBarDelegate::Create(
InfoBarService* infobar_service,
content::DownloadItem* download_item,
const base::FilePath& file_path,
const DownloadTargetDeterminerDelegate::ConfirmationCallback& callback) {
infobar_service->AddInfoBar(DuplicateDownloadInfoBar::CreateInfoBar(
base::WrapUnique(new ChromeDuplicateDownloadInfoBarDelegate(
download_item, file_path, callback))));
}
void ChromeDuplicateDownloadInfoBarDelegate::OnDownloadDestroyed(
content::DownloadItem* download_item) {
DCHECK_EQ(download_item, download_item_);
download_item_ = nullptr;
}
ChromeDuplicateDownloadInfoBarDelegate::ChromeDuplicateDownloadInfoBarDelegate(
content::DownloadItem* download_item,
const base::FilePath& file_path,
const DownloadTargetDeterminerDelegate::ConfirmationCallback&
file_selected_callback)
: download_item_(download_item),
file_path_(file_path),
is_off_the_record_(download_item->GetBrowserContext()->IsOffTheRecord()),
file_selected_callback_(file_selected_callback) {
download_item_->AddObserver(this);
RecordDuplicateInfobarType(INFOBAR_SHOWN);
}
infobars::InfoBarDelegate::InfoBarIdentifier
ChromeDuplicateDownloadInfoBarDelegate::GetIdentifier() const {
return CHROME_DUPLICATE_DOWNLOAD_INFOBAR_DELEGATE;
}
bool ChromeDuplicateDownloadInfoBarDelegate::Accept() {
if (!download_item_) {
RecordDuplicateInfobarType(INFOBAR_DOWNLOAD_CANCELED);
return true;
}
base::FilePath download_dir;
if (!base::android::GetDownloadsDirectory(&download_dir)) {
RecordDuplicateInfobarType(INFOBAR_NO_DOWNLOAD_DIR);
return true;
}
DownloadPathReservationTracker::GetReservedPath(
download_item_,
file_path_,
download_dir,
true,
DownloadPathReservationTracker::UNIQUIFY,
base::Bind(&CreateNewFileDone, file_selected_callback_));
RecordDuplicateInfobarType(INFOBAR_CREATE_NEW_FILE);
return true;
}
bool ChromeDuplicateDownloadInfoBarDelegate::Cancel() {
if (!download_item_)
return true;
file_selected_callback_.Run(DownloadConfirmationResult::CANCELED,
base::FilePath());
// TODO(qinmin): rename this histogram enum.
DownloadController::RecordDownloadCancelReason(
DownloadController::CANCEL_REASON_OVERWRITE_INFOBAR_DISMISSED);
return true;
}
std::string ChromeDuplicateDownloadInfoBarDelegate::GetFilePath() const {
return file_path_.value();
}
void ChromeDuplicateDownloadInfoBarDelegate::InfoBarDismissed() {
Cancel();
}
bool ChromeDuplicateDownloadInfoBarDelegate::IsOffTheRecord() const {
return is_off_the_record_;
}
void ChromeDuplicateDownloadInfoBarDelegate::RecordDuplicateInfobarType(
DuplicateInfobarType type) {
UMA_HISTOGRAM_ENUMERATION("MobileDownload.DuplicateInfobar", type,
INFOBAR_MAX);
}
} // namespace android
} // namespace chrome