| 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 #include "chrome/browser/download/download_path_reservation_tracker.h" | 5 #include "chrome/browser/download/download_path_reservation_tracker.h" |
| 6 | 6 |
| 7 #include <stddef.h> | 7 #include <stddef.h> |
| 8 | 8 |
| 9 #include <map> | 9 #include <map> |
| 10 | 10 |
| 11 #include "base/bind.h" | 11 #include "base/bind.h" |
| 12 #include "base/callback.h" | 12 #include "base/callback.h" |
| 13 #include "base/files/file_util.h" | 13 #include "base/files/file_util.h" |
| 14 #include "base/logging.h" | 14 #include "base/logging.h" |
| 15 #include "base/macros.h" | 15 #include "base/macros.h" |
| 16 #include "base/path_service.h" | 16 #include "base/path_service.h" |
| 17 #include "base/stl_util.h" | 17 #include "base/stl_util.h" |
| 18 #include "base/strings/string_util.h" | 18 #include "base/strings/string_util.h" |
| 19 #include "base/strings/stringprintf.h" | 19 #include "base/strings/stringprintf.h" |
| 20 #include "base/third_party/icu/icu_utf.h" | 20 #include "base/third_party/icu/icu_utf.h" |
| 21 #include "build/build_config.h" | 21 #include "build/build_config.h" |
| 22 #include "chrome/common/chrome_paths.h" | 22 #include "chrome/common/chrome_paths.h" |
| 23 #include "chrome/common/features.h" |
| 23 #include "content/public/browser/browser_thread.h" | 24 #include "content/public/browser/browser_thread.h" |
| 24 #include "content/public/browser/download_item.h" | 25 #include "content/public/browser/download_item.h" |
| 25 | 26 |
| 26 using content::BrowserThread; | 27 using content::BrowserThread; |
| 27 using content::DownloadItem; | 28 using content::DownloadItem; |
| 28 | 29 |
| 29 namespace { | 30 namespace { |
| 30 | 31 |
| 31 typedef DownloadItem* ReservationKey; | 32 typedef DownloadItem* ReservationKey; |
| 32 typedef std::map<ReservationKey, base::FilePath> ReservationMap; | 33 typedef std::map<ReservationKey, base::FilePath> ReservationMap; |
| (...skipping 124 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 157 // - Returns true if |reserved_path| has been successfully verified. | 158 // - Returns true if |reserved_path| has been successfully verified. |
| 158 bool CreateReservation( | 159 bool CreateReservation( |
| 159 ReservationKey key, | 160 ReservationKey key, |
| 160 const base::FilePath& suggested_path, | 161 const base::FilePath& suggested_path, |
| 161 const base::FilePath& default_download_path, | 162 const base::FilePath& default_download_path, |
| 162 bool create_directory, | 163 bool create_directory, |
| 163 DownloadPathReservationTracker::FilenameConflictAction conflict_action, | 164 DownloadPathReservationTracker::FilenameConflictAction conflict_action, |
| 164 base::FilePath* reserved_path) { | 165 base::FilePath* reserved_path) { |
| 165 DCHECK_CURRENTLY_ON(BrowserThread::FILE); | 166 DCHECK_CURRENTLY_ON(BrowserThread::FILE); |
| 166 DCHECK(suggested_path.IsAbsolute()); | 167 DCHECK(suggested_path.IsAbsolute()); |
| 167 | |
| 168 // Create a reservation map if one doesn't exist. It will be automatically | 168 // Create a reservation map if one doesn't exist. It will be automatically |
| 169 // deleted when all the reservations are revoked. | 169 // deleted when all the reservations are revoked. |
| 170 if (g_reservation_map == NULL) | 170 if (g_reservation_map == NULL) |
| 171 g_reservation_map = new ReservationMap; | 171 g_reservation_map = new ReservationMap; |
| 172 ReservationMap& reservations = *g_reservation_map; | 172 ReservationMap& reservations = *g_reservation_map; |
| 173 | 173 |
| 174 // Erase the reservation if it already exists. This can happen during | 174 // Erase the reservation if it already exists. This can happen during |
| 175 // automatic resumption where a new target determination request may be issued | 175 // automatic resumption where a new target determination request may be issued |
| 176 // for a DownloadItem without an intervening transition to INTERRUPTED. | 176 // for a DownloadItem without an intervening transition to INTERRUPTED. |
| 177 // | 177 // |
| (...skipping 17 matching lines...) Expand all Loading... |
| 195 (create_directory || | 195 (create_directory || |
| 196 (!default_download_path.empty() && | 196 (!default_download_path.empty() && |
| 197 (default_download_path == target_dir)))) { | 197 (default_download_path == target_dir)))) { |
| 198 base::CreateDirectory(target_dir); | 198 base::CreateDirectory(target_dir); |
| 199 } | 199 } |
| 200 | 200 |
| 201 // Check writability of the suggested path. If we can't write to it, default | 201 // Check writability of the suggested path. If we can't write to it, default |
| 202 // to the user's "My Documents" directory. We'll prompt them in this case. | 202 // to the user's "My Documents" directory. We'll prompt them in this case. |
| 203 if (!base::PathIsWritable(target_dir)) { | 203 if (!base::PathIsWritable(target_dir)) { |
| 204 DVLOG(1) << "Unable to write to directory \"" << target_dir.value() << "\""; | 204 DVLOG(1) << "Unable to write to directory \"" << target_dir.value() << "\""; |
| 205 #if BUILDFLAG(ANDROID_JAVA_UI) |
| 206 // On Android, DIR_USER_DOCUMENTS is in reality a subdirectory |
| 207 // of DIR_ANDROID_APP_DATA which isn't accessible by other apps. |
| 208 reserved_path->clear(); |
| 209 return false; |
| 210 #else |
| 205 is_path_writeable = false; | 211 is_path_writeable = false; |
| 206 PathService::Get(chrome::DIR_USER_DOCUMENTS, &target_dir); | 212 PathService::Get(chrome::DIR_USER_DOCUMENTS, &target_dir); |
| 207 target_path = target_dir.Append(filename); | 213 target_path = target_dir.Append(filename); |
| 214 #endif // BUILDFLAG(ANDROID_JAVA_UI) |
| 208 } | 215 } |
| 209 | 216 |
| 210 if (is_path_writeable) { | 217 if (is_path_writeable) { |
| 211 // Check the limit of file name length if it could be obtained. When the | 218 // Check the limit of file name length if it could be obtained. When the |
| 212 // suggested name exceeds the limit, truncate or prompt the user. | 219 // suggested name exceeds the limit, truncate or prompt the user. |
| 213 int max_length = base::GetMaximumPathComponentLength(target_dir); | 220 int max_length = base::GetMaximumPathComponentLength(target_dir); |
| 214 if (max_length != -1) { | 221 if (max_length != -1) { |
| 215 int limit = max_length - kIntermediateNameSuffixLength; | 222 int limit = max_length - kIntermediateNameSuffixLength; |
| 216 if (limit <= 0 || !TruncateFileName(&target_path, limit)) | 223 if (limit <= 0 || !TruncateFileName(&target_path, limit)) |
| 217 name_too_long = true; | 224 name_too_long = true; |
| (...skipping 171 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 389 base::Bind(&RunGetReservedPathCallback, | 396 base::Bind(&RunGetReservedPathCallback, |
| 390 callback, | 397 callback, |
| 391 base::Owned(reserved_path))); | 398 base::Owned(reserved_path))); |
| 392 } | 399 } |
| 393 | 400 |
| 394 // static | 401 // static |
| 395 bool DownloadPathReservationTracker::IsPathInUseForTesting( | 402 bool DownloadPathReservationTracker::IsPathInUseForTesting( |
| 396 const base::FilePath& path) { | 403 const base::FilePath& path) { |
| 397 return IsPathInUse(path); | 404 return IsPathInUse(path); |
| 398 } | 405 } |
| OLD | NEW |