OLD | NEW |
| (Empty) |
1 // Copyright 2015 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 | |
5 #include "chrome/browser/android/download/chrome_download_manager_overwrite_info
bar_delegate.h" | |
6 | |
7 #include <memory> | |
8 | |
9 #include "base/android/jni_android.h" | |
10 #include "base/android/jni_string.h" | |
11 #include "base/android/path_utils.h" | |
12 #include "base/files/file_util.h" | |
13 #include "base/memory/ptr_util.h" | |
14 #include "base/strings/stringprintf.h" | |
15 #include "chrome/browser/android/download/download_controller.h" | |
16 #include "chrome/browser/android/download/download_manager_service.h" | |
17 #include "chrome/browser/infobars/infobar_service.h" | |
18 #include "chrome/browser/ui/android/infobars/download_overwrite_infobar.h" | |
19 #include "components/infobars/core/infobar.h" | |
20 #include "content/public/browser/browser_thread.h" | |
21 #include "content/public/browser/web_contents.h" | |
22 | |
23 namespace { | |
24 | |
25 void DeleteExistingDownloadFileDone( | |
26 const base::FilePath& download_path, | |
27 const DownloadTargetDeterminerDelegate::FileSelectedCallback& callback) { | |
28 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | |
29 DownloadManagerService::GetInstance()->RemoveDownloadsForPath( | |
30 download_path); | |
31 callback.Run(download_path); | |
32 } | |
33 | |
34 void DeleteExistingDownloadFile( | |
35 const base::FilePath& download_path, | |
36 const DownloadTargetDeterminerDelegate::FileSelectedCallback& callback) { | |
37 DCHECK_CURRENTLY_ON(content::BrowserThread::FILE); | |
38 base::File::Info info; | |
39 if (GetFileInfo(download_path, &info) && !info.is_directory) | |
40 base::DeleteFile(download_path, false); | |
41 | |
42 content::BrowserThread::PostTask( | |
43 content::BrowserThread::UI, FROM_HERE, | |
44 base::Bind(&DeleteExistingDownloadFileDone, download_path, callback)); | |
45 } | |
46 | |
47 void CreateNewFileDone( | |
48 const DownloadTargetDeterminerDelegate::FileSelectedCallback& callback, | |
49 const base::FilePath& target_path, bool verified) { | |
50 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | |
51 if (verified) | |
52 callback.Run(target_path); | |
53 else | |
54 callback.Run(base::FilePath()); | |
55 } | |
56 | |
57 } // namespace | |
58 | |
59 namespace chrome { | |
60 namespace android { | |
61 | |
62 ChromeDownloadManagerOverwriteInfoBarDelegate:: | |
63 ~ChromeDownloadManagerOverwriteInfoBarDelegate() { | |
64 if (download_item_) | |
65 download_item_->RemoveObserver(this); | |
66 } | |
67 | |
68 // static | |
69 void ChromeDownloadManagerOverwriteInfoBarDelegate::Create( | |
70 InfoBarService* infobar_service, | |
71 content::DownloadItem* download_item, | |
72 const base::FilePath& suggested_path, | |
73 const DownloadTargetDeterminerDelegate::FileSelectedCallback& callback) { | |
74 infobar_service->AddInfoBar(DownloadOverwriteInfoBar::CreateInfoBar( | |
75 base::WrapUnique(new ChromeDownloadManagerOverwriteInfoBarDelegate( | |
76 download_item, suggested_path, callback)))); | |
77 } | |
78 | |
79 void ChromeDownloadManagerOverwriteInfoBarDelegate::OnDownloadDestroyed( | |
80 content::DownloadItem* download_item) { | |
81 DCHECK_EQ(download_item, download_item_); | |
82 download_item_ = nullptr; | |
83 } | |
84 | |
85 ChromeDownloadManagerOverwriteInfoBarDelegate:: | |
86 ChromeDownloadManagerOverwriteInfoBarDelegate( | |
87 content::DownloadItem* download_item, | |
88 const base::FilePath& suggested_download_path, | |
89 const DownloadTargetDeterminerDelegate::FileSelectedCallback& | |
90 file_selected_callback) | |
91 : download_item_(download_item), | |
92 suggested_download_path_(suggested_download_path), | |
93 file_selected_callback_(file_selected_callback) { | |
94 download_item_->AddObserver(this); | |
95 } | |
96 | |
97 infobars::InfoBarDelegate::InfoBarIdentifier | |
98 ChromeDownloadManagerOverwriteInfoBarDelegate::GetIdentifier() const { | |
99 return CHROME_DOWNLOAD_MANAGER_OVERWRITE_INFOBAR_DELEGATE; | |
100 } | |
101 | |
102 bool ChromeDownloadManagerOverwriteInfoBarDelegate::OverwriteExistingFile() { | |
103 if (!download_item_) | |
104 return true; | |
105 | |
106 content::BrowserThread::PostTask( | |
107 content::BrowserThread::FILE, FROM_HERE, | |
108 base::Bind(&DeleteExistingDownloadFile, suggested_download_path_, | |
109 file_selected_callback_)); | |
110 return true; | |
111 } | |
112 | |
113 bool ChromeDownloadManagerOverwriteInfoBarDelegate::CreateNewFile() { | |
114 if (!download_item_) | |
115 return true; | |
116 | |
117 base::FilePath download_dir; | |
118 if (!base::android::GetDownloadsDirectory(&download_dir)) | |
119 return true; | |
120 | |
121 DownloadPathReservationTracker::GetReservedPath( | |
122 download_item_, | |
123 suggested_download_path_, | |
124 download_dir, | |
125 true, | |
126 DownloadPathReservationTracker::UNIQUIFY, | |
127 base::Bind(&CreateNewFileDone, file_selected_callback_)); | |
128 return true; | |
129 } | |
130 | |
131 std::string ChromeDownloadManagerOverwriteInfoBarDelegate::GetFileName() const { | |
132 return suggested_download_path_.BaseName().value(); | |
133 } | |
134 | |
135 std::string ChromeDownloadManagerOverwriteInfoBarDelegate::GetDirName() const { | |
136 return suggested_download_path_.DirName().BaseName().value(); | |
137 } | |
138 | |
139 std::string ChromeDownloadManagerOverwriteInfoBarDelegate::GetDirFullPath() | |
140 const { | |
141 return suggested_download_path_.DirName().value(); | |
142 } | |
143 | |
144 void ChromeDownloadManagerOverwriteInfoBarDelegate::InfoBarDismissed() { | |
145 file_selected_callback_.Run(base::FilePath()); | |
146 DownloadController::RecordDownloadCancelReason( | |
147 DownloadController::CANCEL_REASON_OVERWRITE_INFOBAR_DISMISSED); | |
148 } | |
149 | |
150 } // namespace android | |
151 } // namespace chrome | |
OLD | NEW |