OLD | NEW |
---|---|
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 "base/file_util.h" | 5 #include "base/file_util.h" |
6 | 6 |
7 #include <windows.h> | 7 #include <windows.h> |
8 #include <psapi.h> | 8 #include <psapi.h> |
9 #include <shellapi.h> | 9 #include <shellapi.h> |
10 #include <shlobj.h> | 10 #include <shlobj.h> |
11 #include <time.h> | 11 #include <time.h> |
12 | 12 |
13 #include <algorithm> | 13 #include <algorithm> |
14 #include <limits> | 14 #include <limits> |
15 #include <string> | 15 #include <string> |
16 | 16 |
17 #include "base/files/file_path.h" | 17 #include "base/files/file_path.h" |
18 #include "base/logging.h" | 18 #include "base/logging.h" |
19 #include "base/metrics/histogram.h" | 19 #include "base/metrics/histogram.h" |
20 #include "base/process/process_handle.h" | 20 #include "base/process/process_handle.h" |
21 #include "base/rand_util.h" | 21 #include "base/rand_util.h" |
22 #include "base/strings/string_number_conversions.h" | 22 #include "base/strings/string_number_conversions.h" |
23 #include "base/strings/string_util.h" | 23 #include "base/strings/string_util.h" |
24 #include "base/strings/utf_string_conversions.h" | 24 #include "base/strings/utf_string_conversions.h" |
25 #include "base/threading/thread_restrictions.h" | 25 #include "base/threading/thread_restrictions.h" |
26 #include "base/time/time.h" | 26 #include "base/time/time.h" |
27 #include "base/win/scoped_comptr.h" | |
27 #include "base/win/scoped_handle.h" | 28 #include "base/win/scoped_handle.h" |
28 #include "base/win/windows_version.h" | 29 #include "base/win/windows_version.h" |
29 | 30 |
30 namespace base { | 31 namespace base { |
31 | 32 |
32 namespace { | 33 namespace { |
33 | 34 |
35 // This GUID is associated with any 'don't ask me again' settings that the | |
36 // user can select for different file types. | |
37 // {2676A9A2-D919-4fee-9187-152100393AB2} | |
38 static const GUID kClientID = { 0x2676a9a2, 0xd919, 0x4fee, | |
Lei Zhang
2013/08/03 00:28:26
no need to be static.
Greg Billock
2013/08/05 21:42:30
Done.
| |
39 { 0x91, 0x87, 0x15, 0x21, 0x0, 0x39, 0x3a, 0xb2 } }; | |
40 | |
34 const DWORD kFileShareAll = | 41 const DWORD kFileShareAll = |
35 FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE; | 42 FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE; |
36 | 43 |
37 bool ShellCopy(const FilePath& from_path, | 44 bool ShellCopy(const FilePath& from_path, |
38 const FilePath& to_path, | 45 const FilePath& to_path, |
39 bool recursive) { | 46 bool recursive) { |
40 // WinXP SHFileOperation doesn't like trailing separators. | 47 // WinXP SHFileOperation doesn't like trailing separators. |
41 FilePath stripped_from = from_path.StripTrailingSeparators(); | 48 FilePath stripped_from = from_path.StripTrailingSeparators(); |
42 FilePath stripped_to = to_path.StripTrailingSeparators(); | 49 FilePath stripped_to = to_path.StripTrailingSeparators(); |
43 | 50 |
(...skipping 21 matching lines...) Expand all Loading... | |
65 file_operation.pFrom = double_terminated_path_from; | 72 file_operation.pFrom = double_terminated_path_from; |
66 file_operation.pTo = double_terminated_path_to; | 73 file_operation.pTo = double_terminated_path_to; |
67 file_operation.fFlags = FOF_NOERRORUI | FOF_SILENT | FOF_NOCONFIRMATION | | 74 file_operation.fFlags = FOF_NOERRORUI | FOF_SILENT | FOF_NOCONFIRMATION | |
68 FOF_NOCONFIRMMKDIR; | 75 FOF_NOCONFIRMMKDIR; |
69 if (!recursive) | 76 if (!recursive) |
70 file_operation.fFlags |= FOF_NORECURSION | FOF_FILESONLY; | 77 file_operation.fFlags |= FOF_NORECURSION | FOF_FILESONLY; |
71 | 78 |
72 return (SHFileOperation(&file_operation) == 0); | 79 return (SHFileOperation(&file_operation) == 0); |
73 } | 80 } |
74 | 81 |
82 // Sets the Zone Identifier on the file to "Internet" (3). Returns true if the | |
83 // function succeeds, false otherwise. A failure is expected on system where | |
84 // the Zone Identifier is not supported, like a machine with a FAT32 filesystem. | |
85 // This function does not invoke Windows Attachment Execution Services. | |
86 // | |
87 // |full_path| is the path to the downloaded file. | |
88 bool SetInternetZoneIdentifierDirectly(const base::FilePath& full_path) { | |
89 const DWORD kShare = FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE; | |
90 std::wstring path = full_path.value() + L":Zone.Identifier"; | |
91 HANDLE file = CreateFile(path.c_str(), GENERIC_WRITE, kShare, NULL, | |
92 OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL); | |
93 if (INVALID_HANDLE_VALUE == file) | |
94 return false; | |
95 | |
96 static const char kIdentifier[] = "[ZoneTransfer]\r\nZoneId=3\r\n"; | |
97 // Don't include trailing null in data written. | |
98 static const DWORD kIdentifierSize = arraysize(kIdentifier) - 1; | |
99 DWORD written = 0; | |
100 BOOL result = WriteFile(file, kIdentifier, kIdentifierSize, &written, NULL); | |
101 BOOL flush_result = FlushFileBuffers(file); | |
102 CloseHandle(file); | |
103 | |
104 if (!result || !flush_result || written != kIdentifierSize) { | |
105 NOTREACHED(); | |
106 return false; | |
107 } | |
108 | |
109 return true; | |
110 } | |
111 | |
75 } // namespace | 112 } // namespace |
76 | 113 |
77 FilePath MakeAbsoluteFilePath(const FilePath& input) { | 114 FilePath MakeAbsoluteFilePath(const FilePath& input) { |
78 ThreadRestrictions::AssertIOAllowed(); | 115 ThreadRestrictions::AssertIOAllowed(); |
79 wchar_t file_path[MAX_PATH]; | 116 wchar_t file_path[MAX_PATH]; |
80 if (!_wfullpath(file_path, input.value().c_str(), MAX_PATH)) | 117 if (!_wfullpath(file_path, input.value().c_str(), MAX_PATH)) |
81 return FilePath(); | 118 return FilePath(); |
82 return FilePath(file_path); | 119 return FilePath(file_path); |
83 } | 120 } |
84 | 121 |
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
140 ThreadRestrictions::AssertIOAllowed(); | 177 ThreadRestrictions::AssertIOAllowed(); |
141 | 178 |
142 if (path.value().length() >= MAX_PATH) | 179 if (path.value().length() >= MAX_PATH) |
143 return false; | 180 return false; |
144 | 181 |
145 return MoveFileEx(path.value().c_str(), NULL, | 182 return MoveFileEx(path.value().c_str(), NULL, |
146 MOVEFILE_DELAY_UNTIL_REBOOT | | 183 MOVEFILE_DELAY_UNTIL_REBOOT | |
147 MOVEFILE_REPLACE_EXISTING) != FALSE; | 184 MOVEFILE_REPLACE_EXISTING) != FALSE; |
148 } | 185 } |
149 | 186 |
187 HRESULT ScanAndSaveDownloadedFile(const base::FilePath& full_path, | |
188 const std::string& source_url) { | |
189 base::win::ScopedComPtr<IAttachmentExecute> attachment_services; | |
190 HRESULT hr = attachment_services.CreateInstance(CLSID_AttachmentServices); | |
191 | |
192 if (FAILED(hr)) { | |
193 // The thread must have COM initialized. | |
194 DCHECK_NE(CO_E_NOTINITIALIZED, hr); | |
195 | |
196 // We don't have Attachment Execution Services, it must be a pre-XP.SP2 | |
197 // Windows installation, or the thread does not have COM initialized. Try to | |
198 // set the zone information directly. Failure is not considered an error. | |
199 SetInternetZoneIdentifierDirectly(full_path); | |
200 return hr; | |
201 } | |
202 | |
203 hr = attachment_services->SetClientGuid(kClientID); | |
204 if (FAILED(hr)) | |
205 return hr; | |
206 | |
207 hr = attachment_services->SetLocalPath(full_path.value().c_str()); | |
208 if (FAILED(hr)) | |
209 return hr; | |
210 | |
211 if (!source_url.empty()) { | |
212 hr = attachment_services->SetSource(UTF8ToWide(source_url).c_str()); | |
213 if (FAILED(hr)) | |
214 return hr; | |
215 } | |
216 | |
217 // A failure in the Save() call below could result in the downloaded file | |
218 // being deleted. | |
219 return attachment_services->Save(); | |
220 } | |
221 | |
150 bool ReplaceFile(const FilePath& from_path, | 222 bool ReplaceFile(const FilePath& from_path, |
151 const FilePath& to_path, | 223 const FilePath& to_path, |
152 PlatformFileError* error) { | 224 PlatformFileError* error) { |
153 ThreadRestrictions::AssertIOAllowed(); | 225 ThreadRestrictions::AssertIOAllowed(); |
154 // Try a simple move first. It will only succeed when |to_path| doesn't | 226 // Try a simple move first. It will only succeed when |to_path| doesn't |
155 // already exist. | 227 // already exist. |
156 if (::MoveFile(from_path.value().c_str(), to_path.value().c_str())) | 228 if (::MoveFile(from_path.value().c_str(), to_path.value().c_str())) |
157 return true; | 229 return true; |
158 // Try the full-blown replace if the move fails, as ReplaceFile will only | 230 // Try the full-blown replace if the move fails, as ReplaceFile will only |
159 // succeed when |to_path| does exist. When writing to a network share, we may | 231 // succeed when |to_path| does exist. When writing to a network share, we may |
(...skipping 590 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
750 // Like Move, this function is not transactional, so we just | 822 // Like Move, this function is not transactional, so we just |
751 // leave the copied bits behind if deleting from_path fails. | 823 // leave the copied bits behind if deleting from_path fails. |
752 // If to_path exists previously then we have already overwritten | 824 // If to_path exists previously then we have already overwritten |
753 // it by now, we don't get better off by deleting the new bits. | 825 // it by now, we don't get better off by deleting the new bits. |
754 } | 826 } |
755 return false; | 827 return false; |
756 } | 828 } |
757 | 829 |
758 } // namespace internal | 830 } // namespace internal |
759 } // namespace base | 831 } // namespace base |
OLD | NEW |