| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 "content/browser/download/download_file_impl.h" | 5 #include "content/browser/download/download_file_impl.h" |
| 6 | 6 |
| 7 #include <string> | 7 #include <string> |
| 8 | 8 |
| 9 #include "base/file_util.h" | 9 #include "base/file_util.h" |
| 10 #include "base/stringprintf.h" | |
| 11 #include "content/browser/download/download_create_info.h" | 10 #include "content/browser/download/download_create_info.h" |
| 12 #include "content/public/browser/browser_thread.h" | 11 #include "content/public/browser/browser_thread.h" |
| 13 #include "content/public/browser/download_manager.h" | 12 #include "content/public/browser/download_manager.h" |
| 14 | 13 |
| 15 using content::BrowserThread; | 14 using content::BrowserThread; |
| 16 using content::DownloadId; | 15 using content::DownloadId; |
| 17 | 16 |
| 18 namespace { | |
| 19 | |
| 20 // The maximum number of 'uniquified' files we will try to create. | |
| 21 // This is used when the filename we're trying to download is already in use, | |
| 22 // so we create a new unique filename by appending " (nnn)" before the | |
| 23 // extension, where 1 <= nnn <= kMaxUniqueFiles. | |
| 24 // Also used by code that cleans up said files. | |
| 25 static const int kMaxUniqueFiles = 100; | |
| 26 | |
| 27 } | |
| 28 | |
| 29 namespace content { | |
| 30 | |
| 31 // static | |
| 32 void DownloadFile::AppendNumberToPath(FilePath* path, int number) { | |
| 33 *path = path->InsertBeforeExtensionASCII(StringPrintf(" (%d)", number)); | |
| 34 } | |
| 35 | |
| 36 // static | |
| 37 FilePath DownloadFile::AppendSuffixToPath( | |
| 38 const FilePath& path, | |
| 39 const FilePath::StringType& suffix) { | |
| 40 FilePath::StringType file_name; | |
| 41 base::SStringPrintf( | |
| 42 &file_name, PRFilePathLiteral PRFilePathLiteral, path.value().c_str(), | |
| 43 suffix.c_str()); | |
| 44 return FilePath(file_name); | |
| 45 } | |
| 46 | |
| 47 // static | |
| 48 int DownloadFile::GetUniquePathNumber(const FilePath& path) { | |
| 49 if (!file_util::PathExists(path)) | |
| 50 return 0; | |
| 51 | |
| 52 FilePath new_path; | |
| 53 for (int count = 1; count <= kMaxUniqueFiles; ++count) { | |
| 54 new_path = FilePath(path); | |
| 55 AppendNumberToPath(&new_path, count); | |
| 56 | |
| 57 if (!file_util::PathExists(new_path)) | |
| 58 return count; | |
| 59 } | |
| 60 | |
| 61 return -1; | |
| 62 } | |
| 63 | |
| 64 // static | |
| 65 int DownloadFile::GetUniquePathNumberWithSuffix( | |
| 66 const FilePath& path, | |
| 67 const FilePath::StringType& suffix) { | |
| 68 if (!file_util::PathExists(path) && | |
| 69 !file_util::PathExists(AppendSuffixToPath(path, suffix))) | |
| 70 return 0; | |
| 71 | |
| 72 FilePath new_path; | |
| 73 for (int count = 1; count <= kMaxUniqueFiles; ++count) { | |
| 74 new_path = FilePath(path); | |
| 75 AppendNumberToPath(&new_path, count); | |
| 76 | |
| 77 if (!file_util::PathExists(new_path) && | |
| 78 !file_util::PathExists(AppendSuffixToPath(new_path, suffix))) | |
| 79 return count; | |
| 80 } | |
| 81 | |
| 82 return -1; | |
| 83 } | |
| 84 | |
| 85 } | |
| 86 | |
| 87 DownloadFileImpl::DownloadFileImpl( | 17 DownloadFileImpl::DownloadFileImpl( |
| 88 const DownloadCreateInfo* info, | 18 const DownloadCreateInfo* info, |
| 89 DownloadRequestHandleInterface* request_handle, | 19 DownloadRequestHandleInterface* request_handle, |
| 90 content::DownloadManager* download_manager, | 20 content::DownloadManager* download_manager, |
| 91 bool calculate_hash) | 21 bool calculate_hash) |
| 92 : file_(info->save_info.file_path, | 22 : file_(info->save_info.file_path, |
| 93 info->url(), | 23 info->url(), |
| 94 info->referrer_url, | 24 info->referrer_url, |
| 95 info->received_bytes, | 25 info->received_bytes, |
| 96 calculate_hash, | 26 calculate_hash, |
| (...skipping 85 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 182 std::string DownloadFileImpl::DebugString() const { | 112 std::string DownloadFileImpl::DebugString() const { |
| 183 return base::StringPrintf("{" | 113 return base::StringPrintf("{" |
| 184 " id_ = " "%d" | 114 " id_ = " "%d" |
| 185 " request_handle = %s" | 115 " request_handle = %s" |
| 186 " Base File = %s" | 116 " Base File = %s" |
| 187 " }", | 117 " }", |
| 188 id_.local(), | 118 id_.local(), |
| 189 request_handle_->DebugString().c_str(), | 119 request_handle_->DebugString().c_str(), |
| 190 file_.DebugString().c_str()); | 120 file_.DebugString().c_str()); |
| 191 } | 121 } |
| OLD | NEW |