| 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 // Download utility implementation | 5 // Download utility implementation |
| 6 | 6 |
| 7 #include "chrome/browser/download/download_util.h" | 7 #include "chrome/browser/download/download_util.h" |
| 8 | 8 |
| 9 #if defined(OS_WIN) | 9 #if defined(OS_WIN) |
| 10 #include <shobjidl.h> | 10 #include <shobjidl.h> |
| (...skipping 107 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 118 const int kAllNetErrorCodes[] = { | 118 const int kAllNetErrorCodes[] = { |
| 119 #define NET_ERROR(label, value) -(value), | 119 #define NET_ERROR(label, value) -(value), |
| 120 #include "net/base/net_error_list.h" | 120 #include "net/base/net_error_list.h" |
| 121 #undef NET_ERROR | 121 #undef NET_ERROR |
| 122 }; | 122 }; |
| 123 | 123 |
| 124 } // namespace | 124 } // namespace |
| 125 | 125 |
| 126 // Download temporary file creation -------------------------------------------- | 126 // Download temporary file creation -------------------------------------------- |
| 127 | 127 |
| 128 class DefaultDownloadDirectory { | 128 bool ChooseSavableDirectory( |
| 129 public: | 129 const FilePath& website_save_dir, |
| 130 const FilePath& path() const { return path_; } | 130 const FilePath& download_save_dir, |
| 131 private: | 131 const FilePath& default_download_dir, |
| 132 DefaultDownloadDirectory() { | 132 FilePath* save_dir) { |
| 133 if (!PathService::Get(chrome::DIR_DEFAULT_DOWNLOADS, &path_)) { | 133 bool prompt_dialog = false; |
| 134 if (file_util::PathIsWritable(website_save_dir)) { |
| 135 // If the default html/websites save folder exists, |
| 136 // then use the default html/websites save folder. |
| 137 *save_dir = website_save_dir; |
| 138 } else if (file_util::PathIsWritable(download_save_dir)) { |
| 139 // If the default html/websites save folder does not exist |
| 140 // but the default download folder exists, |
| 141 // then use the default download folder. |
| 142 *save_dir = download_save_dir; |
| 143 } else { |
| 144 // If both the above folders do not exist, |
| 145 // use the user's "Downloads" folder. |
| 146 *save_dir = default_download_dir; |
| 147 prompt_dialog = true; |
| 148 if (!file_util::PathIsWritable(*save_dir)) { |
| 149 VLOG(1) << "Cannot find the user's writable \"Downloads\" folder."; |
| 150 // Create the |download_save_dir| folder if we cannot get |
| 151 // the user's writable "Downloads" folder (This will be a rare case). |
| 152 *save_dir = download_save_dir; |
| 153 } |
| 154 // Make sure that the folder does exist. |
| 155 if (!file_util::CreateDirectory(*save_dir)) |
| 156 LOG(ERROR) << "Failed to create " << (*save_dir).value(); |
| 157 } |
| 158 return prompt_dialog; |
| 159 } |
| 160 |
| 161 FilePath GetDefaultDownloadDirectoryFromPathService() { |
| 162 FilePath default_download_dir; |
| 163 if (!PathService::Get(chrome::DIR_DEFAULT_DOWNLOADS, &default_download_dir)) |
| 164 NOTREACHED(); |
| 165 if (DownloadPathIsDangerous(default_download_dir)) { |
| 166 if (!PathService::Get(chrome::DIR_DEFAULT_DOWNLOADS_SAFE, |
| 167 &default_download_dir)) { |
| 134 NOTREACHED(); | 168 NOTREACHED(); |
| 135 } | 169 } |
| 136 if (DownloadPathIsDangerous(path_)) { | |
| 137 if (!PathService::Get(chrome::DIR_DEFAULT_DOWNLOADS_SAFE, &path_)) { | |
| 138 NOTREACHED(); | |
| 139 } | |
| 140 } | |
| 141 } | 170 } |
| 142 friend struct base::DefaultLazyInstanceTraits<DefaultDownloadDirectory>; | 171 return default_download_dir; |
| 143 FilePath path_; | |
| 144 }; | |
| 145 | |
| 146 static base::LazyInstance<DefaultDownloadDirectory> | |
| 147 g_default_download_directory(base::LINKER_INITIALIZED); | |
| 148 | |
| 149 const FilePath& GetDefaultDownloadDirectory() { | |
| 150 return g_default_download_directory.Get().path(); | |
| 151 } | |
| 152 | |
| 153 bool CreateTemporaryFileForDownload(FilePath* temp_file) { | |
| 154 if (file_util::CreateTemporaryFileInDir(GetDefaultDownloadDirectory(), | |
| 155 temp_file)) | |
| 156 return true; | |
| 157 return file_util::CreateTemporaryFile(temp_file); | |
| 158 } | 172 } |
| 159 | 173 |
| 160 bool DownloadPathIsDangerous(const FilePath& download_path) { | 174 bool DownloadPathIsDangerous(const FilePath& download_path) { |
| 161 FilePath desktop_dir; | 175 FilePath desktop_dir; |
| 162 if (!PathService::Get(chrome::DIR_USER_DESKTOP, &desktop_dir)) { | 176 if (!PathService::Get(chrome::DIR_USER_DESKTOP, &desktop_dir)) { |
| 163 NOTREACHED(); | 177 NOTREACHED(); |
| 164 return false; | 178 return false; |
| 165 } | 179 } |
| 166 return (download_path == desktop_dir); | 180 return (download_path == desktop_dir); |
| 167 } | 181 } |
| (...skipping 644 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 812 FilePath GetCrDownloadPath(const FilePath& suggested_path) { | 826 FilePath GetCrDownloadPath(const FilePath& suggested_path) { |
| 813 FilePath::StringType file_name; | 827 FilePath::StringType file_name; |
| 814 base::SStringPrintf( | 828 base::SStringPrintf( |
| 815 &file_name, | 829 &file_name, |
| 816 PRFilePathLiteral FILE_PATH_LITERAL(".crdownload"), | 830 PRFilePathLiteral FILE_PATH_LITERAL(".crdownload"), |
| 817 suggested_path.value().c_str()); | 831 suggested_path.value().c_str()); |
| 818 return FilePath(file_name); | 832 return FilePath(file_name); |
| 819 } | 833 } |
| 820 | 834 |
| 821 } // namespace download_util | 835 } // namespace download_util |
| OLD | NEW |