| OLD | NEW |
| (Empty) |
| 1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "chrome/browser/save_file.h" | |
| 6 | |
| 7 #include "base/file_util.h" | |
| 8 #include "base/logging.h" | |
| 9 #include "base/path_service.h" | |
| 10 #include "chrome/browser/save_types.h" | |
| 11 #include "chrome/common/win_util.h" | |
| 12 #include "chrome/common/win_safe_util.h" | |
| 13 | |
| 14 SaveFile::SaveFile(const SaveFileCreateInfo* info) | |
| 15 : info_(info), | |
| 16 file_(NULL), | |
| 17 bytes_so_far_(0), | |
| 18 path_renamed_(false), | |
| 19 in_progress_(true) { | |
| 20 DCHECK(info); | |
| 21 DCHECK(info->path.empty()); | |
| 22 if (file_util::CreateTemporaryFileName(&full_path_)) | |
| 23 Open(L"wb"); | |
| 24 } | |
| 25 | |
| 26 SaveFile::~SaveFile() { | |
| 27 Close(); | |
| 28 } | |
| 29 | |
| 30 // Return false indicate that we got disk error, save file manager will tell | |
| 31 // SavePackage this error, then SavePackage will call its Cancel() method to | |
| 32 // cancel whole save job. | |
| 33 bool SaveFile::AppendDataToFile(const char* data, int data_len) { | |
| 34 if (file_) { | |
| 35 if (data_len == fwrite(data, 1, data_len, file_)) { | |
| 36 bytes_so_far_ += data_len; | |
| 37 return true; | |
| 38 } else { | |
| 39 Close(); | |
| 40 return false; | |
| 41 } | |
| 42 } | |
| 43 // No file_, treat it as disk error. | |
| 44 return false; | |
| 45 } | |
| 46 | |
| 47 void SaveFile::Cancel() { | |
| 48 Close(); | |
| 49 // If this job has been canceled, and it has created file, | |
| 50 // We need to delete this created file. | |
| 51 if (!full_path_.empty()) { | |
| 52 DeleteFile(full_path_.c_str()); | |
| 53 } | |
| 54 } | |
| 55 | |
| 56 // Rename the file when we have final name. | |
| 57 bool SaveFile::Rename(const std::wstring& new_path) { | |
| 58 Close(); | |
| 59 | |
| 60 DCHECK(!path_renamed()); | |
| 61 // We cannot rename because rename will keep the same security descriptor | |
| 62 // on the destination file. We want to recreate the security descriptor | |
| 63 // with the security that makes sense in the new path. If the last parameter | |
| 64 // is FALSE and the new file already exists, the function overwrites the | |
| 65 // existing file and succeeds. | |
| 66 if (!CopyFile(full_path_.c_str(), new_path.c_str(), FALSE)) | |
| 67 return false; | |
| 68 | |
| 69 DeleteFile(full_path_.c_str()); | |
| 70 | |
| 71 full_path_ = new_path; | |
| 72 path_renamed_ = true; | |
| 73 | |
| 74 // Still in saving process, reopen the file. | |
| 75 if (in_progress_ && !Open(L"a+b")) | |
| 76 return false; | |
| 77 return true; | |
| 78 } | |
| 79 | |
| 80 void SaveFile::Finish() { | |
| 81 Close(); | |
| 82 in_progress_ = false; | |
| 83 } | |
| 84 | |
| 85 void SaveFile::Close() { | |
| 86 if (file_) { | |
| 87 fclose(file_); | |
| 88 file_ = NULL; | |
| 89 } | |
| 90 } | |
| 91 | |
| 92 bool SaveFile::Open(const wchar_t* open_mode) { | |
| 93 DCHECK(!full_path_.empty()); | |
| 94 if (_wfopen_s(&file_, full_path_.c_str(), open_mode)) { | |
| 95 file_ = NULL; | |
| 96 return false; | |
| 97 } | |
| 98 // Sets the zone to tell Windows that this file comes from the Internet. | |
| 99 // We ignore the return value because a failure is not fatal. | |
| 100 win_util::SetInternetZoneIdentifier(full_path_); | |
| 101 return true; | |
| 102 } | |
| 103 | |
| OLD | NEW |