OLD | NEW |
1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/browser/download/download_manager.h" | 5 #include "chrome/browser/download/download_manager.h" |
6 | 6 |
7 #include "app/l10n_util.h" | 7 #include "app/l10n_util.h" |
8 #include "app/resource_bundle.h" | 8 #include "app/resource_bundle.h" |
9 #include "base/callback.h" | 9 #include "base/callback.h" |
10 #include "base/file_util.h" | 10 #include "base/file_util.h" |
(...skipping 61 matching lines...) Loading... |
72 | 72 |
73 // Update frequency (milliseconds). | 73 // Update frequency (milliseconds). |
74 const int kUpdateTimeMs = 1000; | 74 const int kUpdateTimeMs = 1000; |
75 | 75 |
76 // Our download table ID starts at 1, so we use 0 to represent a download that | 76 // Our download table ID starts at 1, so we use 0 to represent a download that |
77 // has started, but has not yet had its data persisted in the table. We use fake | 77 // has started, but has not yet had its data persisted in the table. We use fake |
78 // database handles in incognito mode starting at -1 and progressively getting | 78 // database handles in incognito mode starting at -1 and progressively getting |
79 // more negative. | 79 // more negative. |
80 const int kUninitializedHandle = 0; | 80 const int kUninitializedHandle = 0; |
81 | 81 |
82 // Appends the passed the number between parenthesis the path before the | |
83 // extension. | |
84 void AppendNumberToPath(FilePath* path, int number) { | |
85 file_util::InsertBeforeExtension(path, | |
86 StringPrintf(FILE_PATH_LITERAL(" (%d)"), number)); | |
87 } | |
88 | |
89 // Attempts to find a number that can be appended to that path to make it | |
90 // unique. If |path| does not exist, 0 is returned. If it fails to find such | |
91 // a number, -1 is returned. | |
92 int GetUniquePathNumber(const FilePath& path) { | |
93 const int kMaxAttempts = 100; | |
94 | |
95 if (!file_util::PathExists(path)) | |
96 return 0; | |
97 | |
98 FilePath new_path; | |
99 for (int count = 1; count <= kMaxAttempts; ++count) { | |
100 new_path = FilePath(path); | |
101 AppendNumberToPath(&new_path, count); | |
102 | |
103 if (!file_util::PathExists(new_path)) | |
104 return count; | |
105 } | |
106 | |
107 return -1; | |
108 } | |
109 | |
110 // Used to sort download items based on descending start time. | 82 // Used to sort download items based on descending start time. |
111 bool CompareStartTime(DownloadItem* first, DownloadItem* second) { | 83 bool CompareStartTime(DownloadItem* first, DownloadItem* second) { |
112 return first->start_time() > second->start_time(); | 84 return first->start_time() > second->start_time(); |
113 } | 85 } |
114 | 86 |
115 } // namespace | 87 } // namespace |
116 | 88 |
117 // DownloadItem implementation ------------------------------------------------- | 89 // DownloadItem implementation ------------------------------------------------- |
118 | 90 |
119 // Constructor for reading from the history service. | 91 // Constructor for reading from the history service. |
(...skipping 197 matching lines...) Loading... |
317 manager_->PauseDownload(id_, !is_paused_); | 289 manager_->PauseDownload(id_, !is_paused_); |
318 is_paused_ = !is_paused_; | 290 is_paused_ = !is_paused_; |
319 UpdateObservers(); | 291 UpdateObservers(); |
320 } | 292 } |
321 | 293 |
322 FilePath DownloadItem::GetFileName() const { | 294 FilePath DownloadItem::GetFileName() const { |
323 if (safety_state_ == DownloadItem::SAFE) | 295 if (safety_state_ == DownloadItem::SAFE) |
324 return file_name_; | 296 return file_name_; |
325 if (path_uniquifier_ > 0) { | 297 if (path_uniquifier_ > 0) { |
326 FilePath name(original_name_); | 298 FilePath name(original_name_); |
327 AppendNumberToPath(&name, path_uniquifier_); | 299 download_util::AppendNumberToPath(&name, path_uniquifier_); |
328 return name; | 300 return name; |
329 } | 301 } |
330 return original_name_; | 302 return original_name_; |
331 } | 303 } |
332 | 304 |
333 // DownloadManager implementation ---------------------------------------------- | 305 // DownloadManager implementation ---------------------------------------------- |
334 | 306 |
335 // static | 307 // static |
336 void DownloadManager::RegisterUserPrefs(PrefService* prefs) { | 308 void DownloadManager::RegisterUserPrefs(PrefService* prefs) { |
337 prefs->RegisterBooleanPref(prefs::kPromptForDownload, false); | 309 prefs->RegisterBooleanPref(prefs::kPromptForDownload, false); |
(...skipping 354 matching lines...) Loading... |
692 FilePath dir = info->suggested_path.DirName(); | 664 FilePath dir = info->suggested_path.DirName(); |
693 FilePath filename = info->suggested_path.BaseName(); | 665 FilePath filename = info->suggested_path.BaseName(); |
694 if (!file_util::PathIsWritable(dir)) { | 666 if (!file_util::PathIsWritable(dir)) { |
695 info->save_as = true; | 667 info->save_as = true; |
696 PathService::Get(chrome::DIR_USER_DOCUMENTS, &info->suggested_path); | 668 PathService::Get(chrome::DIR_USER_DOCUMENTS, &info->suggested_path); |
697 info->suggested_path = info->suggested_path.Append(filename); | 669 info->suggested_path = info->suggested_path.Append(filename); |
698 } | 670 } |
699 | 671 |
700 // Do not add the path uniquifier if we are saving to a specific path as in | 672 // Do not add the path uniquifier if we are saving to a specific path as in |
701 // the drag-out case. | 673 // the drag-out case. |
702 if (info->save_info.file_path.empty()) | 674 if (info->save_info.file_path.empty()) { |
703 info->path_uniquifier = GetUniquePathNumber(info->suggested_path); | 675 info->path_uniquifier = download_util::GetUniquePathNumber( |
| 676 info->suggested_path); |
| 677 } |
704 | 678 |
705 // If the download is deemed dangerous, we'll use a temporary name for it. | 679 // If the download is deemed dangerous, we'll use a temporary name for it. |
706 if (info->is_dangerous) { | 680 if (info->is_dangerous) { |
707 info->original_name = FilePath(info->suggested_path).BaseName(); | 681 info->original_name = FilePath(info->suggested_path).BaseName(); |
708 // Create a temporary file to hold the file until the user approves its | 682 // Create a temporary file to hold the file until the user approves its |
709 // download. | 683 // download. |
710 FilePath::StringType file_name; | 684 FilePath::StringType file_name; |
711 FilePath path; | 685 FilePath path; |
712 while (path.empty()) { | 686 while (path.empty()) { |
713 SStringPrintf(&file_name, FILE_PATH_LITERAL("unconfirmed %d.download"), | 687 SStringPrintf(&file_name, FILE_PATH_LITERAL("unconfirmed %d.download"), |
714 base::RandInt(0, 100000)); | 688 base::RandInt(0, 100000)); |
715 path = dir.Append(file_name); | 689 path = dir.Append(file_name); |
716 if (file_util::PathExists(path)) | 690 if (file_util::PathExists(path)) |
717 path = FilePath(); | 691 path = FilePath(); |
718 } | 692 } |
719 info->suggested_path = path; | 693 info->suggested_path = path; |
720 } else { | 694 } else { |
721 // We know the final path, build it if necessary. | 695 // We know the final path, build it if necessary. |
722 if (info->path_uniquifier > 0) { | 696 if (info->path_uniquifier > 0) { |
723 AppendNumberToPath(&(info->suggested_path), info->path_uniquifier); | 697 download_util::AppendNumberToPath(&(info->suggested_path), |
| 698 info->path_uniquifier); |
724 // Setting path_uniquifier to 0 to make sure we don't try to unique it | 699 // Setting path_uniquifier to 0 to make sure we don't try to unique it |
725 // later on. | 700 // later on. |
726 info->path_uniquifier = 0; | 701 info->path_uniquifier = 0; |
727 } else if (info->path_uniquifier == -1) { | 702 } else if (info->path_uniquifier == -1) { |
728 // We failed to find a unique path. We have to prompt the user. | 703 // We failed to find a unique path. We have to prompt the user. |
729 info->save_as = true; | 704 info->save_as = true; |
730 } | 705 } |
731 } | 706 } |
732 | 707 |
733 if (!info->save_as && info->save_info.file_path.empty()) { | 708 if (!info->save_as && info->save_info.file_path.empty()) { |
(...skipping 275 matching lines...) Loading... |
1009 bool success = false; | 984 bool success = false; |
1010 FilePath new_path; | 985 FilePath new_path; |
1011 int uniquifier = 0; | 986 int uniquifier = 0; |
1012 if (file_util::PathExists(path)) { | 987 if (file_util::PathExists(path)) { |
1013 new_path = path.DirName().Append(original_name); | 988 new_path = path.DirName().Append(original_name); |
1014 // Make our name unique at this point, as if a dangerous file is downloading | 989 // Make our name unique at this point, as if a dangerous file is downloading |
1015 // and a 2nd download is started for a file with the same name, they would | 990 // and a 2nd download is started for a file with the same name, they would |
1016 // have the same path. This is because we uniquify the name on download | 991 // have the same path. This is because we uniquify the name on download |
1017 // start, and at that time the first file does not exists yet, so the second | 992 // start, and at that time the first file does not exists yet, so the second |
1018 // file gets the same name. | 993 // file gets the same name. |
1019 uniquifier = GetUniquePathNumber(new_path); | 994 uniquifier = download_util::GetUniquePathNumber(new_path); |
1020 if (uniquifier > 0) | 995 if (uniquifier > 0) |
1021 AppendNumberToPath(&new_path, uniquifier); | 996 download_util::AppendNumberToPath(&new_path, uniquifier); |
1022 success = file_util::Move(path, new_path); | 997 success = file_util::Move(path, new_path); |
1023 } else { | 998 } else { |
1024 NOTREACHED(); | 999 NOTREACHED(); |
1025 } | 1000 } |
1026 | 1001 |
1027 ChromeThread::PostTask( | 1002 ChromeThread::PostTask( |
1028 ChromeThread::UI, FROM_HERE, | 1003 ChromeThread::UI, FROM_HERE, |
1029 NewRunnableMethod(this, &DownloadManager::DangerousDownloadRenamed, | 1004 NewRunnableMethod(this, &DownloadManager::DangerousDownloadRenamed, |
1030 download_handle, success, new_path, uniquifier)); | 1005 download_handle, success, new_path, uniquifier)); |
1031 } | 1006 } |
(...skipping 775 matching lines...) Loading... |
1807 observing_download_manager_->NotifyModelChanged(); | 1782 observing_download_manager_->NotifyModelChanged(); |
1808 } | 1783 } |
1809 | 1784 |
1810 void DownloadManager::OtherDownloadManagerObserver::SetDownloads( | 1785 void DownloadManager::OtherDownloadManagerObserver::SetDownloads( |
1811 std::vector<DownloadItem*>& downloads) { | 1786 std::vector<DownloadItem*>& downloads) { |
1812 } | 1787 } |
1813 | 1788 |
1814 void DownloadManager::OtherDownloadManagerObserver::ManagerGoingDown() { | 1789 void DownloadManager::OtherDownloadManagerObserver::ManagerGoingDown() { |
1815 observed_download_manager_ = NULL; | 1790 observed_download_manager_ = NULL; |
1816 } | 1791 } |
OLD | NEW |