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.h" | 5 #include "content/browser/download/download_file.h" |
6 | 6 |
7 #include <string> | |
8 | |
9 #include "base/file_util.h" | 7 #include "base/file_util.h" |
10 #include "base/stringprintf.h" | 8 #include "base/stringprintf.h" |
11 #include "content/browser/download/download_create_info.h" | |
12 #include "content/browser/download/download_manager.h" | |
13 #include "content/public/browser/browser_thread.h" | |
14 | |
15 using content::BrowserThread; | |
16 | 9 |
17 namespace { | 10 namespace { |
18 | 11 |
19 // The maximum number of 'uniquified' files we will try to create. | 12 // The maximum number of 'uniquified' files we will try to create. |
20 // This is used when the filename we're trying to download is already in use, | 13 // This is used when the filename we're trying to download is already in use, |
21 // so we create a new unique filename by appending " (nnn)" before the | 14 // so we create a new unique filename by appending " (nnn)" before the |
22 // extension, where 1 <= nnn <= kMaxUniqueFiles. | 15 // extension, where 1 <= nnn <= kMaxUniqueFiles. |
23 // Also used by code that cleans up said files. | 16 // Also used by code that cleans up said files. |
24 static const int kMaxUniqueFiles = 100; | 17 static const int kMaxUniqueFiles = 100; |
25 | 18 |
26 } | 19 } |
27 | 20 |
28 DownloadFile::DownloadFile(const DownloadCreateInfo* info, | 21 // static |
29 DownloadRequestHandleInterface* request_handle, | |
30 DownloadManager* download_manager) | |
31 : BaseFile(info->save_info.file_path, | |
32 info->url(), | |
33 info->referrer_url, | |
34 info->received_bytes, | |
35 info->save_info.file_stream), | |
36 id_(info->download_id), | |
37 request_handle_(request_handle), | |
38 download_manager_(download_manager) { | |
39 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | |
40 } | |
41 | |
42 DownloadFile::~DownloadFile() { | |
43 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | |
44 } | |
45 | |
46 void DownloadFile::CancelDownloadRequest() { | |
47 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | |
48 request_handle_->CancelRequest(); | |
49 } | |
50 | |
51 DownloadManager* DownloadFile::GetDownloadManager() { | |
52 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | |
53 return download_manager_.get(); | |
54 } | |
55 | |
56 std::string DownloadFile::DebugString() const { | |
57 return base::StringPrintf("{" | |
58 " id_ = " "%d" | |
59 " request_handle = %s" | |
60 " Base File = %s" | |
61 " }", | |
62 id_.local(), | |
63 request_handle_->DebugString().c_str(), | |
64 BaseFile::DebugString().c_str()); | |
65 } | |
66 | |
67 void DownloadFile::AppendNumberToPath(FilePath* path, int number) { | 22 void DownloadFile::AppendNumberToPath(FilePath* path, int number) { |
68 *path = path->InsertBeforeExtensionASCII(StringPrintf(" (%d)", number)); | 23 *path = path->InsertBeforeExtensionASCII(StringPrintf(" (%d)", number)); |
69 } | 24 } |
70 | 25 |
| 26 // static |
| 27 FilePath DownloadFile::AppendSuffixToPath( |
| 28 const FilePath& path, |
| 29 const FilePath::StringType& suffix) { |
| 30 FilePath::StringType file_name; |
| 31 base::SStringPrintf( |
| 32 &file_name, PRFilePathLiteral PRFilePathLiteral, path.value().c_str(), |
| 33 suffix.c_str()); |
| 34 return FilePath(file_name); |
| 35 } |
| 36 |
| 37 // static |
71 int DownloadFile::GetUniquePathNumber(const FilePath& path) { | 38 int DownloadFile::GetUniquePathNumber(const FilePath& path) { |
72 if (!file_util::PathExists(path)) | 39 if (!file_util::PathExists(path)) |
73 return 0; | 40 return 0; |
74 | 41 |
75 FilePath new_path; | 42 FilePath new_path; |
76 for (int count = 1; count <= kMaxUniqueFiles; ++count) { | 43 for (int count = 1; count <= kMaxUniqueFiles; ++count) { |
77 new_path = FilePath(path); | 44 new_path = FilePath(path); |
78 AppendNumberToPath(&new_path, count); | 45 AppendNumberToPath(&new_path, count); |
79 | 46 |
80 if (!file_util::PathExists(new_path)) | 47 if (!file_util::PathExists(new_path)) |
81 return count; | 48 return count; |
82 } | 49 } |
83 | 50 |
84 return -1; | 51 return -1; |
85 } | 52 } |
86 | 53 |
87 FilePath DownloadFile::AppendSuffixToPath( | 54 // static |
88 const FilePath& path, | |
89 const FilePath::StringType& suffix) { | |
90 FilePath::StringType file_name; | |
91 base::SStringPrintf( | |
92 &file_name, PRFilePathLiteral PRFilePathLiteral, path.value().c_str(), | |
93 suffix.c_str()); | |
94 return FilePath(file_name); | |
95 } | |
96 | |
97 int DownloadFile::GetUniquePathNumberWithSuffix( | 55 int DownloadFile::GetUniquePathNumberWithSuffix( |
98 const FilePath& path, | 56 const FilePath& path, |
99 const FilePath::StringType& suffix) { | 57 const FilePath::StringType& suffix) { |
100 if (!file_util::PathExists(path) && | 58 if (!file_util::PathExists(path) && |
101 !file_util::PathExists(AppendSuffixToPath(path, suffix))) | 59 !file_util::PathExists(AppendSuffixToPath(path, suffix))) |
102 return 0; | 60 return 0; |
103 | 61 |
104 FilePath new_path; | 62 FilePath new_path; |
105 for (int count = 1; count <= kMaxUniqueFiles; ++count) { | 63 for (int count = 1; count <= kMaxUniqueFiles; ++count) { |
106 new_path = FilePath(path); | 64 new_path = FilePath(path); |
107 AppendNumberToPath(&new_path, count); | 65 AppendNumberToPath(&new_path, count); |
108 | 66 |
109 if (!file_util::PathExists(new_path) && | 67 if (!file_util::PathExists(new_path) && |
110 !file_util::PathExists(AppendSuffixToPath(new_path, suffix))) | 68 !file_util::PathExists(AppendSuffixToPath(new_path, suffix))) |
111 return count; | 69 return count; |
112 } | 70 } |
113 | 71 |
114 return -1; | 72 return -1; |
115 } | 73 } |
OLD | NEW |