[email protected] | 6c69796d | 2010-07-16 21:41:16 | [diff] [blame^] | 1 | // Copyright (c) 2010 The Chromium Authors. All rights reserved. |
license.bot | bf09a50 | 2008-08-24 00:55:55 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 4 | |
[email protected] | cdaa865 | 2008-09-13 02:48:59 | [diff] [blame] | 5 | #include "chrome/browser/download/download_file.h" |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 6 | |
| 7 | #include "base/file_util.h" |
[email protected] | e93d282 | 2009-01-30 05:59:59 | [diff] [blame] | 8 | #include "build/build_config.h" |
[email protected] | b47da72 | 2009-02-23 23:54:18 | [diff] [blame] | 9 | #include "chrome/browser/download/download_manager.h" |
[email protected] | 290c970 | 2010-03-09 04:01:36 | [diff] [blame] | 10 | #include "chrome/browser/download/download_util.h" |
[email protected] | 6c69796d | 2010-07-16 21:41:16 | [diff] [blame^] | 11 | #include "chrome/browser/history/download_types.h" |
| 12 | #include "net/base/net_errors.h" |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 13 | |
[email protected] | e93d282 | 2009-01-30 05:59:59 | [diff] [blame] | 14 | #if defined(OS_WIN) |
[email protected] | 4a0765a | 2009-05-08 23:12:25 | [diff] [blame] | 15 | #include "app/win_util.h" |
[email protected] | e93d282 | 2009-01-30 05:59:59 | [diff] [blame] | 16 | #include "chrome/common/win_safe_util.h" |
[email protected] | 8ef0637 | 2009-04-27 21:11:34 | [diff] [blame] | 17 | #elif defined(OS_MACOSX) |
[email protected] | eae033a3 | 2009-09-29 15:47:53 | [diff] [blame] | 18 | #include "chrome/browser/cocoa/file_metadata.h" |
[email protected] | e93d282 | 2009-01-30 05:59:59 | [diff] [blame] | 19 | #endif |
[email protected] | e1acf6f | 2008-10-27 20:43:33 | [diff] [blame] | 20 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 21 | DownloadFile::DownloadFile(const DownloadCreateInfo* info) |
[email protected] | 8af9d03 | 2010-02-10 00:00:32 | [diff] [blame] | 22 | : file_stream_(info->save_info.file_stream), |
[email protected] | 8ef0637 | 2009-04-27 21:11:34 | [diff] [blame] | 23 | source_url_(info->url), |
| 24 | referrer_url_(info->referrer_url), |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 25 | id_(info->download_id), |
[email protected] | 76543b9 | 2009-08-31 17:27:45 | [diff] [blame] | 26 | child_id_(info->child_id), |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 27 | render_view_id_(info->render_view_id), |
| 28 | request_id_(info->request_id), |
| 29 | bytes_so_far_(0), |
[email protected] | 8af9d03 | 2010-02-10 00:00:32 | [diff] [blame] | 30 | full_path_(info->save_info.file_path), |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 31 | path_renamed_(false), |
[email protected] | 36e93a77 | 2009-10-23 04:18:05 | [diff] [blame] | 32 | in_progress_(true), |
[email protected] | 8af9d03 | 2010-02-10 00:00:32 | [diff] [blame] | 33 | dont_sleep_(true), |
| 34 | save_info_(info->save_info) { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 35 | } |
| 36 | |
| 37 | DownloadFile::~DownloadFile() { |
| 38 | Close(); |
| 39 | } |
| 40 | |
| 41 | bool DownloadFile::Initialize() { |
[email protected] | 290c970 | 2010-03-09 04:01:36 | [diff] [blame] | 42 | if (!full_path_.empty() || |
| 43 | download_util::CreateTemporaryFileForDownload(&full_path_)) |
[email protected] | 8af9d03 | 2010-02-10 00:00:32 | [diff] [blame] | 44 | return Open(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 45 | return false; |
| 46 | } |
| 47 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 48 | bool DownloadFile::AppendDataToFile(const char* data, int data_len) { |
[email protected] | 8af9d03 | 2010-02-10 00:00:32 | [diff] [blame] | 49 | if (file_stream_.get()) { |
[email protected] | 5415e4f | 2009-02-23 23:53:31 | [diff] [blame] | 50 | // FIXME bug 595247: handle errors on file writes. |
[email protected] | 8af9d03 | 2010-02-10 00:00:32 | [diff] [blame] | 51 | size_t written = file_stream_->Write(data, data_len, NULL); |
[email protected] | 5415e4f | 2009-02-23 23:53:31 | [diff] [blame] | 52 | bytes_so_far_ += written; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 53 | return true; |
| 54 | } |
| 55 | return false; |
| 56 | } |
| 57 | |
| 58 | void DownloadFile::Cancel() { |
| 59 | Close(); |
[email protected] | 8af9d03 | 2010-02-10 00:00:32 | [diff] [blame] | 60 | if (!full_path_.empty()) |
| 61 | file_util::Delete(full_path_, false); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 62 | } |
| 63 | |
| 64 | // The UI has provided us with our finalized name. |
[email protected] | 7ae7c2cb | 2009-01-06 23:31:41 | [diff] [blame] | 65 | bool DownloadFile::Rename(const FilePath& new_path) { |
[email protected] | 2cee72c | 2010-02-11 22:25:16 | [diff] [blame] | 66 | // If the new path is same as the old one, there is no need to perform the |
| 67 | // following renaming logic. |
| 68 | if (new_path == full_path_) { |
| 69 | path_renamed_ = true; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 70 | |
[email protected] | 2cee72c | 2010-02-11 22:25:16 | [diff] [blame] | 71 | // Don't close the file if we're not done (finished or canceled). |
| 72 | if (!in_progress_) |
| 73 | Close(); |
| 74 | |
[email protected] | 8af9d03 | 2010-02-10 00:00:32 | [diff] [blame] | 75 | return true; |
[email protected] | 2cee72c | 2010-02-11 22:25:16 | [diff] [blame] | 76 | } |
| 77 | |
| 78 | Close(); |
[email protected] | 8af9d03 | 2010-02-10 00:00:32 | [diff] [blame] | 79 | |
[email protected] | cc7948a | 2009-03-13 20:01:43 | [diff] [blame] | 80 | #if defined(OS_WIN) |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 81 | // We cannot rename because rename will keep the same security descriptor |
| 82 | // on the destination file. We want to recreate the security descriptor |
| 83 | // with the security that makes sense in the new path. |
[email protected] | cc7948a | 2009-03-13 20:01:43 | [diff] [blame] | 84 | if (!file_util::RenameFileAndResetSecurityDescriptor(full_path_, new_path)) |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 85 | return false; |
[email protected] | cc7948a | 2009-03-13 20:01:43 | [diff] [blame] | 86 | #elif defined(OS_POSIX) |
[email protected] | f84233c6 | 2010-02-24 23:55:55 | [diff] [blame] | 87 | { |
| 88 | // Similarly, on Unix, we're moving a temp file created with permissions |
| 89 | // 600 to |new_path|. Here, we try to fix up the destination file with |
| 90 | // appropriate permissions. |
| 91 | struct stat st; |
[email protected] | 87391de | 2010-03-09 00:58:30 | [diff] [blame] | 92 | // First check the file existence and create an empty file if it doesn't |
| 93 | // exist. |
| 94 | if (!file_util::PathExists(new_path)) |
| 95 | file_util::WriteFile(new_path, "", 0); |
[email protected] | f84233c6 | 2010-02-24 23:55:55 | [diff] [blame] | 96 | bool stat_succeeded = (stat(new_path.value().c_str(), &st) == 0); |
| 97 | |
| 98 | // TODO(estade): Move() falls back to copying and deleting when a simple |
| 99 | // rename fails. Copying sucks for large downloads. crbug.com/8737 |
| 100 | if (!file_util::Move(full_path_, new_path)) |
[email protected] | cc7948a | 2009-03-13 20:01:43 | [diff] [blame] | 101 | return false; |
[email protected] | f84233c6 | 2010-02-24 23:55:55 | [diff] [blame] | 102 | |
| 103 | if (stat_succeeded) |
| 104 | chmod(new_path.value().c_str(), st.st_mode); |
| 105 | } |
[email protected] | cc7948a | 2009-03-13 20:01:43 | [diff] [blame] | 106 | #endif |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 107 | |
| 108 | full_path_ = new_path; |
| 109 | path_renamed_ = true; |
| 110 | |
| 111 | // We don't need to re-open the file if we're done (finished or canceled). |
| 112 | if (!in_progress_) |
| 113 | return true; |
| 114 | |
[email protected] | 8af9d03 | 2010-02-10 00:00:32 | [diff] [blame] | 115 | if (!Open()) |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 116 | return false; |
[email protected] | 4f0872b | 2010-02-10 17:47:39 | [diff] [blame] | 117 | |
| 118 | // Move to the end of the new file. |
| 119 | if (file_stream_->Seek(net::FROM_END, 0) < 0) |
| 120 | return false; |
| 121 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 122 | return true; |
| 123 | } |
| 124 | |
| 125 | void DownloadFile::Close() { |
[email protected] | 8af9d03 | 2010-02-10 00:00:32 | [diff] [blame] | 126 | if (file_stream_.get()) { |
[email protected] | b2a3a25 | 2010-06-10 22:36:38 | [diff] [blame] | 127 | #if defined(OS_CHROMEOS) |
| 128 | // Currently we don't really care about the return value, since if it fails |
| 129 | // theres not much we can do. But we might in the future. |
| 130 | file_stream_->Flush(); |
| 131 | #endif |
[email protected] | 8af9d03 | 2010-02-10 00:00:32 | [diff] [blame] | 132 | file_stream_->Close(); |
| 133 | file_stream_.reset(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 134 | } |
| 135 | } |
| 136 | |
[email protected] | 8af9d03 | 2010-02-10 00:00:32 | [diff] [blame] | 137 | bool DownloadFile::Open() { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 138 | DCHECK(!full_path_.empty()); |
[email protected] | 8af9d03 | 2010-02-10 00:00:32 | [diff] [blame] | 139 | |
| 140 | // Create a new file steram if it is not provided. |
| 141 | if (!file_stream_.get()) { |
| 142 | file_stream_.reset(new net::FileStream); |
| 143 | if (file_stream_->Open(full_path_, |
| 144 | base::PLATFORM_FILE_OPEN_ALWAYS | |
| 145 | base::PLATFORM_FILE_WRITE) != net::OK) { |
| 146 | file_stream_.reset(); |
| 147 | return false; |
| 148 | } |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 149 | } |
[email protected] | e93d282 | 2009-01-30 05:59:59 | [diff] [blame] | 150 | |
| 151 | #if defined(OS_WIN) |
[email protected] | df72c09 | 2009-09-10 19:56:17 | [diff] [blame] | 152 | AnnotateWithSourceInformation(); |
| 153 | #endif |
| 154 | return true; |
| 155 | } |
| 156 | |
| 157 | void DownloadFile::AnnotateWithSourceInformation() { |
| 158 | #if defined(OS_WIN) |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 159 | // Sets the Zone to tell Windows that this file comes from the internet. |
| 160 | // We ignore the return value because a failure is not fatal. |
[email protected] | 345e1b8 | 2009-01-05 23:36:01 | [diff] [blame] | 161 | win_util::SetInternetZoneIdentifier(full_path_); |
[email protected] | 4edb0b9 | 2009-02-26 01:57:09 | [diff] [blame] | 162 | #elif defined(OS_MACOSX) |
[email protected] | eae033a3 | 2009-09-29 15:47:53 | [diff] [blame] | 163 | file_metadata::AddQuarantineMetadataToFile(full_path_, source_url_, |
| 164 | referrer_url_); |
| 165 | file_metadata::AddOriginMetadataToFile(full_path_, source_url_, |
| 166 | referrer_url_); |
[email protected] | e93d282 | 2009-01-30 05:59:59 | [diff] [blame] | 167 | #endif |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 168 | } |