chrome_paths_win.cc 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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
  3. // found in the LICENSE file.
  4. #include "chrome/common/chrome_paths_internal.h"
  5. #include <windows.h>
  6. #include <knownfolders.h>
  7. #include <shellapi.h>
  8. #include <shlobj.h>
  9. #include <shobjidl.h>
  10. #include "base/files/file_path.h"
  11. #include "base/path_service.h"
  12. #include "base/win/scoped_co_mem.h"
  13. #include "chrome/common/chrome_constants.h"
  14. namespace chrome {
  15. namespace {
  16. // Generic function to call SHGetFolderPath().
  17. bool GetUserDirectory(int csidl_folder, base::FilePath* result) {
  18. // We need to go compute the value. It would be nice to support paths
  19. // with names longer than MAX_PATH, but the system functions don't seem
  20. // to be designed for it either, with the exception of GetTempPath
  21. // (but other things will surely break if the temp path is too long,
  22. // so we don't bother handling it.
  23. wchar_t path_buf[MAX_PATH];
  24. path_buf[0] = 0;
  25. if (FAILED(SHGetFolderPath(NULL, csidl_folder, NULL, SHGFP_TYPE_CURRENT,
  26. path_buf))) {
  27. return false;
  28. }
  29. *result = base::FilePath(path_buf);
  30. return true;
  31. }
  32. } // namespace
  33. bool GetDefaultUserDataDirectory(base::FilePath* result) {
  34. return PathService::Get(base::DIR_LOCAL_APP_DATA, result);
  35. }
  36. void GetUserCacheDirectory(const base::FilePath& profile_dir,
  37. base::FilePath* result) {
  38. // This function does more complicated things on Mac/Linux.
  39. *result = profile_dir;
  40. }
  41. bool GetUserDocumentsDirectory(base::FilePath* result) {
  42. return GetUserDirectory(CSIDL_MYDOCUMENTS, result);
  43. }
  44. // Return a default path for downloads that is safe.
  45. // We just use 'Downloads' under DIR_USER_DOCUMENTS. Localizing
  46. // 'downloads' is not a good idea because Chrome's UI language
  47. // can be changed.
  48. bool GetUserDownloadsDirectorySafe(base::FilePath* result) {
  49. if (!GetUserDocumentsDirectory(result))
  50. return false;
  51. *result = result->Append(L"Downloads");
  52. return true;
  53. }
  54. // On Vista and higher, use the downloads known folder. Since it can be
  55. // relocated to point to a "dangerous" folder, callers should validate that the
  56. // returned path is not dangerous before using it.
  57. bool GetUserDownloadsDirectory(base::FilePath* result) {
  58. typedef HRESULT(WINAPI * GetKnownFolderPath)(REFKNOWNFOLDERID, DWORD, HANDLE,
  59. PWSTR*);
  60. GetKnownFolderPath f = reinterpret_cast<GetKnownFolderPath>(
  61. GetProcAddress(GetModuleHandle(L"shell32.dll"), "SHGetKnownFolderPath"));
  62. base::win::ScopedCoMem<wchar_t> path_buf;
  63. if (f && SUCCEEDED(f(FOLDERID_Downloads, 0, NULL, &path_buf))) {
  64. *result = base::FilePath(std::wstring(path_buf));
  65. return true;
  66. }
  67. return GetUserDownloadsDirectorySafe(result);
  68. }
  69. bool GetUserMusicDirectory(base::FilePath* result) {
  70. return GetUserDirectory(CSIDL_MYMUSIC, result);
  71. }
  72. bool GetUserPicturesDirectory(base::FilePath* result) {
  73. return GetUserDirectory(CSIDL_MYPICTURES, result);
  74. }
  75. bool GetUserVideosDirectory(base::FilePath* result) {
  76. return GetUserDirectory(CSIDL_MYVIDEO, result);
  77. }
  78. bool ProcessNeedsProfileDir(const std::string& process_type) {
  79. // On windows we don't want subprocesses other than the browser process and
  80. // service processes to be able to use the profile directory because if it
  81. // lies on a network share the sandbox will prevent us from accessing it.
  82. if (process_type.empty())
  83. return true;
  84. return false;
  85. }
  86. } // namespace chrome