| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 #ifndef CHROME_BROWSER_DOWNLOAD_DOWNLOAD_PATH_RESERVATION_TRACKER_H_ | 5 #ifndef CHROME_BROWSER_DOWNLOAD_DOWNLOAD_PATH_RESERVATION_TRACKER_H_ |
| 6 #define CHROME_BROWSER_DOWNLOAD_DOWNLOAD_PATH_RESERVATION_TRACKER_H_ | 6 #define CHROME_BROWSER_DOWNLOAD_DOWNLOAD_PATH_RESERVATION_TRACKER_H_ |
| 7 | 7 |
| 8 #include "base/callback_forward.h" | 8 #include "base/callback_forward.h" |
| 9 | 9 |
| 10 namespace base { | 10 namespace base { |
| 11 class FilePath; | 11 class FilePath; |
| 12 } | 12 } |
| 13 | 13 |
| 14 namespace content { | 14 namespace content { |
| 15 class DownloadItem; | 15 class DownloadItem; |
| 16 } | 16 } |
| 17 | 17 |
| 18 enum class PathValidationResult { |
| 19 SUCCESS, |
| 20 PATH_NOT_WRITABLE, |
| 21 NAME_TOO_LONG, |
| 22 CONFLICT |
| 23 }; |
| 24 |
| 18 // Chrome attempts to uniquify filenames that are assigned to downloads in order | 25 // Chrome attempts to uniquify filenames that are assigned to downloads in order |
| 19 // to avoid overwriting files that already exist on the file system. Downloads | 26 // to avoid overwriting files that already exist on the file system. Downloads |
| 20 // that are considered potentially dangerous use random intermediate filenames. | 27 // that are considered potentially dangerous use random intermediate filenames. |
| 21 // Therefore only considering files that exist on the filesystem is | 28 // Therefore only considering files that exist on the filesystem is |
| 22 // insufficient. This class tracks files that are assigned to active downloads | 29 // insufficient. This class tracks files that are assigned to active downloads |
| 23 // so that uniquification can take those into account as well. | 30 // so that uniquification can take those into account as well. |
| 24 class DownloadPathReservationTracker { | 31 class DownloadPathReservationTracker { |
| 25 public: | 32 public: |
| 26 // Callback used with |GetReservedPath|. |target_path| specifies the target | 33 // Callback used with |GetReservedPath|. |target_path| specifies the target |
| 27 // path for the download. |target_path_verified| is true if all of the | 34 // path for the download. If |result| is SUCCESS then: |
| 28 // following is true: | |
| 29 // - |requested_target_path| (passed into GetReservedPath()) was writeable. | 35 // - |requested_target_path| (passed into GetReservedPath()) was writeable. |
| 30 // - |target_path| was verified as being unique if uniqueness was | 36 // - |target_path| was verified as being unique if uniqueness was |
| 31 // required. | 37 // required. |
| 32 // | 38 // |
| 33 // If |requested_target_path| was not writeable, then the parent directory of | 39 // If |requested_target_path| was not writeable, then the parent directory of |
| 34 // |target_path| may be different from that of |requested_target_path|. | 40 // |target_path| may be different from that of |requested_target_path|. |
| 35 typedef base::Callback<void(const base::FilePath& target_path, | 41 using ReservedPathCallback = |
| 36 bool target_path_verified)> ReservedPathCallback; | 42 base::Callback<void(PathValidationResult result, |
| 43 const base::FilePath& target_path)>; |
| 37 | 44 |
| 38 // The largest index for the uniquification suffix that we will try while | 45 // The largest index for the uniquification suffix that we will try while |
| 39 // attempting to come up with a unique path. | 46 // attempting to come up with a unique path. |
| 40 static const int kMaxUniqueFiles = 100; | 47 static const int kMaxUniqueFiles = 100; |
| 41 | 48 |
| 42 enum FilenameConflictAction { | 49 enum FilenameConflictAction { |
| 43 UNIQUIFY, | 50 UNIQUIFY, |
| 44 OVERWRITE, | 51 OVERWRITE, |
| 45 PROMPT, | 52 PROMPT, |
| 46 }; | 53 }; |
| (...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 91 bool create_directory, | 98 bool create_directory, |
| 92 FilenameConflictAction conflict_action, | 99 FilenameConflictAction conflict_action, |
| 93 const ReservedPathCallback& callback); | 100 const ReservedPathCallback& callback); |
| 94 | 101 |
| 95 // Returns true if |path| is in use by an existing path reservation. Should | 102 // Returns true if |path| is in use by an existing path reservation. Should |
| 96 // only be called on the FILE thread. Currently only used by tests. | 103 // only be called on the FILE thread. Currently only used by tests. |
| 97 static bool IsPathInUseForTesting(const base::FilePath& path); | 104 static bool IsPathInUseForTesting(const base::FilePath& path); |
| 98 }; | 105 }; |
| 99 | 106 |
| 100 #endif // CHROME_BROWSER_DOWNLOAD_DOWNLOAD_PATH_RESERVATION_TRACKER_H_ | 107 #endif // CHROME_BROWSER_DOWNLOAD_DOWNLOAD_PATH_RESERVATION_TRACKER_H_ |
| OLD | NEW |