Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(400)

Side by Side Diff: chrome/browser/download/save_file.cc

Issue 4247: Port some things in browser/{download,history}, minor things in common.... (Closed) Base URL: http://src.chromium.org/svn/trunk/src/
Patch Set: '' Created 12 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « chrome/browser/download/save_file.h ('k') | chrome/browser/download/save_types.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. 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 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 "chrome/browser/download/save_file.h" 5 #include "chrome/browser/download/save_file.h"
6 6
7 #include "base/basictypes.h"
7 #include "base/file_util.h" 8 #include "base/file_util.h"
8 #include "base/logging.h" 9 #include "base/logging.h"
9 #include "base/path_service.h" 10 #include "base/path_service.h"
11 #include "base/string_util.h"
10 #include "chrome/browser/download/save_types.h" 12 #include "chrome/browser/download/save_types.h"
13 #if defined(OS_WIN)
11 #include "chrome/common/win_util.h" 14 #include "chrome/common/win_util.h"
12 #include "chrome/common/win_safe_util.h" 15 #include "chrome/common/win_safe_util.h"
16 #endif
13 17
14 SaveFile::SaveFile(const SaveFileCreateInfo* info) 18 SaveFile::SaveFile(const SaveFileCreateInfo* info)
15 : info_(info), 19 : info_(info),
16 file_(NULL), 20 file_(NULL),
17 bytes_so_far_(0), 21 bytes_so_far_(0),
18 path_renamed_(false), 22 path_renamed_(false),
19 in_progress_(true) { 23 in_progress_(true) {
20 DCHECK(info); 24 DCHECK(info);
21 DCHECK(info->path.empty()); 25 DCHECK(info->path.empty());
22 if (file_util::CreateTemporaryFileName(&full_path_)) 26 if (file_util::CreateTemporaryFileName(&full_path_))
23 Open(L"wb"); 27 Open("wb");
24 } 28 }
25 29
26 SaveFile::~SaveFile() { 30 SaveFile::~SaveFile() {
27 Close(); 31 Close();
28 } 32 }
29 33
30 // Return false indicate that we got disk error, save file manager will tell 34 // 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 35 // SavePackage this error, then SavePackage will call its Cancel() method to
32 // cancel whole save job. 36 // cancel whole save job.
33 bool SaveFile::AppendDataToFile(const char* data, int data_len) { 37 bool SaveFile::AppendDataToFile(const char* data, size_t data_len) {
34 if (file_) { 38 if (file_) {
35 if (data_len == fwrite(data, 1, data_len, file_)) { 39 if (data_len == fwrite(data, 1, data_len, file_)) {
36 bytes_so_far_ += data_len; 40 bytes_so_far_ += data_len;
37 return true; 41 return true;
38 } else { 42 } else {
39 Close(); 43 Close();
40 return false; 44 return false;
41 } 45 }
42 } 46 }
43 // No file_, treat it as disk error. 47 // No file_, treat it as disk error.
44 return false; 48 return false;
45 } 49 }
46 50
47 void SaveFile::Cancel() { 51 void SaveFile::Cancel() {
48 Close(); 52 Close();
49 // If this job has been canceled, and it has created file, 53 // If this job has been canceled, and it has created file,
50 // We need to delete this created file. 54 // We need to delete this created file.
51 if (!full_path_.empty()) { 55 if (!full_path_.empty()) {
52 DeleteFile(full_path_.c_str()); 56 file_util::Delete(full_path_, false);
53 } 57 }
54 } 58 }
55 59
56 // Rename the file when we have final name. 60 // Rename the file when we have final name.
57 bool SaveFile::Rename(const std::wstring& new_path) { 61 bool SaveFile::Rename(const std::wstring& new_path) {
58 Close(); 62 Close();
59 63
60 DCHECK(!path_renamed()); 64 DCHECK(!path_renamed());
61 // We cannot rename because rename will keep the same security descriptor 65 // We cannot rename because rename will keep the same security descriptor
62 // on the destination file. We want to recreate the security descriptor 66 // 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 67 // with the security that makes sense in the new path.
64 // is FALSE and the new file already exists, the function overwrites the 68 if (!file_util::CopyFile(full_path_, new_path))
65 // existing file and succeeds.
66 if (!CopyFile(full_path_.c_str(), new_path.c_str(), FALSE))
67 return false; 69 return false;
68 70
69 DeleteFile(full_path_.c_str()); 71 file_util::Delete(full_path_, false);
70 72
71 full_path_ = new_path; 73 full_path_ = new_path;
72 path_renamed_ = true; 74 path_renamed_ = true;
73 75
74 // Still in saving process, reopen the file. 76 // Still in saving process, reopen the file.
75 if (in_progress_ && !Open(L"a+b")) 77 if (in_progress_ && !Open("a+b"))
76 return false; 78 return false;
77 return true; 79 return true;
78 } 80 }
79 81
80 void SaveFile::Finish() { 82 void SaveFile::Finish() {
81 Close(); 83 Close();
82 in_progress_ = false; 84 in_progress_ = false;
83 } 85 }
84 86
85 void SaveFile::Close() { 87 void SaveFile::Close() {
86 if (file_) { 88 if (file_) {
87 fclose(file_); 89 file_util::CloseFile(file_);
88 file_ = NULL; 90 file_ = NULL;
89 } 91 }
90 } 92 }
91 93
92 bool SaveFile::Open(const wchar_t* open_mode) { 94 bool SaveFile::Open(const char* open_mode) {
93 DCHECK(!full_path_.empty()); 95 DCHECK(!full_path_.empty());
94 if (_wfopen_s(&file_, full_path_.c_str(), open_mode)) { 96 file_ = file_util::OpenFile(full_path_, open_mode);
95 file_ = NULL; 97 if (!file_) {
96 return false; 98 return false;
97 } 99 }
100 #if defined(OS_WIN)
98 // Sets the zone to tell Windows that this file comes from the Internet. 101 // 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. 102 // We ignore the return value because a failure is not fatal.
103 // TODO(port): Similarly mark on Mac.
100 win_util::SetInternetZoneIdentifier(full_path_); 104 win_util::SetInternetZoneIdentifier(full_path_);
105 #endif
101 return true; 106 return true;
102 } 107 }
103 108
OLDNEW
« no previous file with comments | « chrome/browser/download/save_file.h ('k') | chrome/browser/download/save_types.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698