OLD | NEW |
1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 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 <propvarutil.h> | 8 #include <propvarutil.h> |
9 #include <psapi.h> | 9 #include <psapi.h> |
10 #include <shellapi.h> | 10 #include <shellapi.h> |
11 #include <shlobj.h> | 11 #include <shlobj.h> |
12 #include <time.h> | 12 #include <time.h> |
13 #include <string> | 13 #include <string> |
14 | 14 |
15 #include "base/file_path.h" | 15 #include "base/file_path.h" |
16 #include "base/logging.h" | 16 #include "base/logging.h" |
17 #include "base/metrics/histogram.h" | 17 #include "base/metrics/histogram.h" |
18 #include "base/pe_image.h" | 18 #include "base/pe_image.h" |
19 #include "base/scoped_comptr_win.h" | 19 #include "base/scoped_comptr_win.h" |
20 #include "base/scoped_handle.h" | 20 #include "base/scoped_handle.h" |
21 #include "base/string_number_conversions.h" | 21 #include "base/string_number_conversions.h" |
22 #include "base/string_util.h" | 22 #include "base/string_util.h" |
23 #include "base/time.h" | 23 #include "base/time.h" |
24 #include "base/utf_string_conversions.h" | 24 #include "base/utf_string_conversions.h" |
25 #include "base/win_util.h" | 25 #include "base/win_util.h" |
| 26 #include "base/win/windows_version.h" |
26 | 27 |
27 namespace file_util { | 28 namespace file_util { |
28 | 29 |
29 namespace { | 30 namespace { |
30 | 31 |
31 const DWORD kFileShareAll = | 32 const DWORD kFileShareAll = |
32 FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE; | 33 FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE; |
33 | 34 |
34 // Helper for NormalizeFilePath(), defined below. | 35 // Helper for NormalizeFilePath(), defined below. |
35 bool DevicePathToDriveLetterPath(const FilePath& device_path, | 36 bool DevicePathToDriveLetterPath(const FilePath& device_path, |
(...skipping 218 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
254 return ShellCopy(from_path, to_path, true); | 255 return ShellCopy(from_path, to_path, true); |
255 | 256 |
256 // The following code assumes that from path is a directory. | 257 // The following code assumes that from path is a directory. |
257 DCHECK(DirectoryExists(from_path)); | 258 DCHECK(DirectoryExists(from_path)); |
258 | 259 |
259 // Instead of creating a new directory, we copy the old one to include the | 260 // Instead of creating a new directory, we copy the old one to include the |
260 // security information of the folder as part of the copy. | 261 // security information of the folder as part of the copy. |
261 if (!PathExists(to_path)) { | 262 if (!PathExists(to_path)) { |
262 // Except that Vista fails to do that, and instead do a recursive copy if | 263 // Except that Vista fails to do that, and instead do a recursive copy if |
263 // the target directory doesn't exist. | 264 // the target directory doesn't exist. |
264 if (win_util::GetWinVersion() >= win_util::WINVERSION_VISTA) | 265 if (base::win::GetVersion() >= base::win::VERSION_VISTA) |
265 CreateDirectory(to_path); | 266 CreateDirectory(to_path); |
266 else | 267 else |
267 ShellCopy(from_path, to_path, false); | 268 ShellCopy(from_path, to_path, false); |
268 } | 269 } |
269 | 270 |
270 FilePath directory = from_path.Append(L"*.*"); | 271 FilePath directory = from_path.Append(L"*.*"); |
271 return ShellCopy(directory, to_path, false); | 272 return ShellCopy(directory, to_path, false); |
272 } | 273 } |
273 | 274 |
274 bool CopyAndDeleteDirectory(const FilePath& from_path, | 275 bool CopyAndDeleteDirectory(const FilePath& from_path, |
(...skipping 120 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
395 | 396 |
396 if (arguments && FAILED(i_shell_link->SetArguments(arguments))) | 397 if (arguments && FAILED(i_shell_link->SetArguments(arguments))) |
397 return false; | 398 return false; |
398 | 399 |
399 if (description && FAILED(i_shell_link->SetDescription(description))) | 400 if (description && FAILED(i_shell_link->SetDescription(description))) |
400 return false; | 401 return false; |
401 | 402 |
402 if (icon && FAILED(i_shell_link->SetIconLocation(icon, icon_index))) | 403 if (icon && FAILED(i_shell_link->SetIconLocation(icon, icon_index))) |
403 return false; | 404 return false; |
404 | 405 |
405 if (app_id && (win_util::GetWinVersion() >= win_util::WINVERSION_WIN7)) { | 406 if (app_id && (base::win::GetVersion() >= base::win::VERSION_WIN7)) { |
406 ScopedComPtr<IPropertyStore> property_store; | 407 ScopedComPtr<IPropertyStore> property_store; |
407 if (FAILED(property_store.QueryFrom(i_shell_link))) | 408 if (FAILED(property_store.QueryFrom(i_shell_link))) |
408 return false; | 409 return false; |
409 | 410 |
410 if (!win_util::SetAppIdForPropertyStore(property_store, app_id)) | 411 if (!win_util::SetAppIdForPropertyStore(property_store, app_id)) |
411 return false; | 412 return false; |
412 } | 413 } |
413 | 414 |
414 result = i_persist_file->Save(destination, TRUE); | 415 result = i_persist_file->Save(destination, TRUE); |
415 return SUCCEEDED(result); | 416 return SUCCEEDED(result); |
(...skipping 29 matching lines...) Expand all Loading... |
445 | 446 |
446 if (arguments && FAILED(i_shell_link->SetArguments(arguments))) | 447 if (arguments && FAILED(i_shell_link->SetArguments(arguments))) |
447 return false; | 448 return false; |
448 | 449 |
449 if (description && FAILED(i_shell_link->SetDescription(description))) | 450 if (description && FAILED(i_shell_link->SetDescription(description))) |
450 return false; | 451 return false; |
451 | 452 |
452 if (icon && FAILED(i_shell_link->SetIconLocation(icon, icon_index))) | 453 if (icon && FAILED(i_shell_link->SetIconLocation(icon, icon_index))) |
453 return false; | 454 return false; |
454 | 455 |
455 if (app_id && win_util::GetWinVersion() >= win_util::WINVERSION_WIN7) { | 456 if (app_id && base::win::GetVersion() >= base::win::VERSION_WIN7) { |
456 ScopedComPtr<IPropertyStore> property_store; | 457 ScopedComPtr<IPropertyStore> property_store; |
457 if (FAILED(property_store.QueryFrom(i_shell_link))) | 458 if (FAILED(property_store.QueryFrom(i_shell_link))) |
458 return false; | 459 return false; |
459 | 460 |
460 if (!win_util::SetAppIdForPropertyStore(property_store, app_id)) | 461 if (!win_util::SetAppIdForPropertyStore(property_store, app_id)) |
461 return false; | 462 return false; |
462 } | 463 } |
463 | 464 |
464 HRESULT result = i_persist_file->Save(destination, TRUE); | 465 HRESULT result = i_persist_file->Save(destination, TRUE); |
465 return SUCCEEDED(result); | 466 return SUCCEEDED(result); |
466 } | 467 } |
467 | 468 |
468 bool TaskbarPinShortcutLink(const wchar_t* shortcut) { | 469 bool TaskbarPinShortcutLink(const wchar_t* shortcut) { |
469 // "Pin to taskbar" is only supported after Win7. | 470 // "Pin to taskbar" is only supported after Win7. |
470 if (win_util::GetWinVersion() < win_util::WINVERSION_WIN7) | 471 if (base::win::GetVersion() < base::win::VERSION_WIN7) |
471 return false; | 472 return false; |
472 | 473 |
473 int result = reinterpret_cast<int>(ShellExecute(NULL, L"taskbarpin", shortcut, | 474 int result = reinterpret_cast<int>(ShellExecute(NULL, L"taskbarpin", shortcut, |
474 NULL, NULL, 0)); | 475 NULL, NULL, 0)); |
475 return result > 32; | 476 return result > 32; |
476 } | 477 } |
477 | 478 |
478 bool TaskbarUnpinShortcutLink(const wchar_t* shortcut) { | 479 bool TaskbarUnpinShortcutLink(const wchar_t* shortcut) { |
479 // "Unpin from taskbar" is only supported after Win7. | 480 // "Unpin from taskbar" is only supported after Win7. |
480 if (win_util::GetWinVersion() < win_util::WINVERSION_WIN7) | 481 if (base::win::GetVersion() < base::win::VERSION_WIN7) |
481 return false; | 482 return false; |
482 | 483 |
483 int result = reinterpret_cast<int>(ShellExecute(NULL, L"taskbarunpin", | 484 int result = reinterpret_cast<int>(ShellExecute(NULL, L"taskbarunpin", |
484 shortcut, NULL, NULL, 0)); | 485 shortcut, NULL, NULL, 0)); |
485 return result > 32; | 486 return result > 32; |
486 } | 487 } |
487 | 488 |
488 bool GetTempDir(FilePath* path) { | 489 bool GetTempDir(FilePath* path) { |
489 wchar_t temp_path[MAX_PATH + 1]; | 490 wchar_t temp_path[MAX_PATH + 1]; |
490 DWORD path_len = ::GetTempPath(MAX_PATH, temp_path); | 491 DWORD path_len = ::GetTempPath(MAX_PATH, temp_path); |
(...skipping 508 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
999 if (::GetMappedFileNameW(cp, file_view, mapped_file_path, kMaxPathLength)) { | 1000 if (::GetMappedFileNameW(cp, file_view, mapped_file_path, kMaxPathLength)) { |
1000 *nt_path = FilePath(mapped_file_path); | 1001 *nt_path = FilePath(mapped_file_path); |
1001 success = true; | 1002 success = true; |
1002 } | 1003 } |
1003 ::UnmapViewOfFile(file_view); | 1004 ::UnmapViewOfFile(file_view); |
1004 return success; | 1005 return success; |
1005 } | 1006 } |
1006 | 1007 |
1007 bool PreReadImage(const wchar_t* file_path, size_t size_to_read, | 1008 bool PreReadImage(const wchar_t* file_path, size_t size_to_read, |
1008 size_t step_size) { | 1009 size_t step_size) { |
1009 if (win_util::GetWinVersion() > win_util::WINVERSION_XP) { | 1010 if (base::win::GetVersion() > base::win::VERSION_XP) { |
1010 // Vista+ branch. On these OSes, the forced reads through the DLL actually | 1011 // Vista+ branch. On these OSes, the forced reads through the DLL actually |
1011 // slows warm starts. The solution is to sequentially read file contents | 1012 // slows warm starts. The solution is to sequentially read file contents |
1012 // with an optional cap on total amount to read. | 1013 // with an optional cap on total amount to read. |
1013 ScopedHandle file( | 1014 ScopedHandle file( |
1014 CreateFile(file_path, | 1015 CreateFile(file_path, |
1015 GENERIC_READ, | 1016 GENERIC_READ, |
1016 FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE, | 1017 FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE, |
1017 NULL, | 1018 NULL, |
1018 OPEN_EXISTING, | 1019 OPEN_EXISTING, |
1019 FILE_FLAG_SEQUENTIAL_SCAN, | 1020 FILE_FLAG_SEQUENTIAL_SCAN, |
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1063 uint8 unused = *(touch + offset); | 1064 uint8 unused = *(touch + offset); |
1064 offset += step_size; | 1065 offset += step_size; |
1065 } | 1066 } |
1066 FreeLibrary(dll_module); | 1067 FreeLibrary(dll_module); |
1067 } | 1068 } |
1068 | 1069 |
1069 return true; | 1070 return true; |
1070 } | 1071 } |
1071 | 1072 |
1072 } // namespace file_util | 1073 } // namespace file_util |
OLD | NEW |