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 <windows.h> | 5 #include <windows.h> |
6 #include <shlobj.h> | 6 #include <shlobj.h> |
7 | 7 |
8 #include "base/base_paths.h" | 8 #include "base/base_paths.h" |
9 #include "base/environment.h" | 9 #include "base/environment.h" |
10 #include "base/files/file_path.h" | 10 #include "base/files/file_path.h" |
11 #include "base/path_service.h" | 11 #include "base/path_service.h" |
12 #include "base/strings/utf_string_conversions.h" | 12 #include "base/strings/utf_string_conversions.h" |
13 #include "base/win/scoped_co_mem.h" | 13 #include "base/win/scoped_co_mem.h" |
14 #include "base/win/windows_version.h" | 14 #include "base/win/windows_version.h" |
15 | 15 |
16 // http://blogs.msdn.com/oldnewthing/archive/2004/10/25/247180.aspx | 16 // http://blogs.msdn.com/oldnewthing/archive/2004/10/25/247180.aspx |
17 extern "C" IMAGE_DOS_HEADER __ImageBase; | 17 extern "C" IMAGE_DOS_HEADER __ImageBase; |
18 | 18 |
19 using base::FilePath; | 19 using base::FilePath; |
20 | 20 |
21 namespace base { | 21 namespace base { |
22 | 22 |
| 23 bool GetLocalAppData(FilePath* result) { |
| 24 wchar_t system_buffer[MAX_PATH]; |
| 25 system_buffer[0] = 0; |
| 26 if (FAILED(SHGetFolderPath(NULL, CSIDL_LOCAL_APPDATA, NULL, |
| 27 SHGFP_TYPE_CURRENT, system_buffer))) |
| 28 return false; |
| 29 *result = FilePath(system_buffer); |
| 30 return true; |
| 31 } |
| 32 |
| 33 bool GetFileExe(FilePath* result) { |
| 34 wchar_t system_buffer[MAX_PATH]; |
| 35 system_buffer[0] = 0; |
| 36 if (GetModuleFileName(NULL, system_buffer, MAX_PATH) == 0) |
| 37 return false; |
| 38 *result = FilePath(system_buffer); |
| 39 return true; |
| 40 } |
| 41 |
| 42 bool GetFileModule(FilePath* result) { |
| 43 wchar_t system_buffer[MAX_PATH]; |
| 44 system_buffer[0] = 0; |
| 45 // The resource containing module is assumed to be the one that this code |
| 46 // lives in, whether that's a dll or exe. |
| 47 HMODULE this_module = reinterpret_cast<HMODULE>(&__ImageBase); |
| 48 if (GetModuleFileName(this_module, system_buffer, MAX_PATH) == 0) |
| 49 return false; |
| 50 *result = FilePath(system_buffer); |
| 51 return true; |
| 52 } |
| 53 |
| 54 bool GetProgramFilesX86(FilePath* result) { |
| 55 wchar_t system_buffer[MAX_PATH]; |
| 56 system_buffer[0] = 0; |
| 57 if (FAILED(SHGetFolderPath(NULL, CSIDL_PROGRAM_FILESX86, NULL, |
| 58 SHGFP_TYPE_CURRENT, system_buffer))) |
| 59 return false; |
| 60 *result = FilePath(system_buffer); |
| 61 return true; |
| 62 } |
| 63 |
| 64 bool GetProgramFiles(FilePath* result) { |
| 65 wchar_t system_buffer[MAX_PATH]; |
| 66 system_buffer[0] = 0; |
| 67 if (FAILED(SHGetFolderPath(NULL, CSIDL_PROGRAM_FILES, NULL, |
| 68 SHGFP_TYPE_CURRENT, system_buffer))) |
| 69 return false; |
| 70 *result = FilePath(system_buffer); |
| 71 return true; |
| 72 } |
| 73 |
23 bool PathProviderWin(int key, FilePath* result) { | 74 bool PathProviderWin(int key, FilePath* result) { |
24 // We need to go compute the value. It would be nice to support paths with | 75 // We need to go compute the value. It would be nice to support paths with |
25 // names longer than MAX_PATH, but the system functions don't seem to be | 76 // names longer than MAX_PATH, but the system functions don't seem to be |
26 // designed for it either, with the exception of GetTempPath (but other | 77 // designed for it either, with the exception of GetTempPath (but other |
27 // things will surely break if the temp path is too long, so we don't bother | 78 // things will surely break if the temp path is too long, so we don't bother |
28 // handling it. | 79 // handling it. |
29 wchar_t system_buffer[MAX_PATH]; | 80 wchar_t system_buffer[MAX_PATH]; |
30 system_buffer[0] = 0; | 81 system_buffer[0] = 0; |
31 | 82 |
32 FilePath cur; | 83 FilePath cur; |
33 switch (key) { | 84 switch (key) { |
34 case base::FILE_EXE: | 85 case base::FILE_EXE: |
35 if (GetModuleFileName(NULL, system_buffer, MAX_PATH) == 0) | 86 if (!GetFileExe(&cur)) |
36 return false; | 87 return false; |
37 cur = FilePath(system_buffer); | |
38 break; | 88 break; |
39 case base::FILE_MODULE: { | 89 case base::FILE_MODULE: |
40 // the resource containing module is assumed to be the one that | 90 if (!GetFileModule(&cur)) |
41 // this code lives in, whether that's a dll or exe | |
42 HMODULE this_module = reinterpret_cast<HMODULE>(&__ImageBase); | |
43 if (GetModuleFileName(this_module, system_buffer, MAX_PATH) == 0) | |
44 return false; | 91 return false; |
45 cur = FilePath(system_buffer); | |
46 break; | 92 break; |
47 } | |
48 case base::DIR_WINDOWS: | 93 case base::DIR_WINDOWS: |
49 GetWindowsDirectory(system_buffer, MAX_PATH); | 94 GetWindowsDirectory(system_buffer, MAX_PATH); |
50 cur = FilePath(system_buffer); | 95 cur = FilePath(system_buffer); |
51 break; | 96 break; |
52 case base::DIR_SYSTEM: | 97 case base::DIR_SYSTEM: |
53 GetSystemDirectory(system_buffer, MAX_PATH); | 98 GetSystemDirectory(system_buffer, MAX_PATH); |
54 cur = FilePath(system_buffer); | 99 cur = FilePath(system_buffer); |
55 break; | 100 break; |
56 case base::DIR_PROGRAM_FILESX86: | 101 case base::DIR_PROGRAM_FILESX86: |
57 if (base::win::OSInfo::GetInstance()->architecture() != | 102 if (base::win::OSInfo::GetInstance()->architecture() != |
58 base::win::OSInfo::X86_ARCHITECTURE) { | 103 base::win::OSInfo::X86_ARCHITECTURE) { |
59 if (FAILED(SHGetFolderPath(NULL, CSIDL_PROGRAM_FILESX86, NULL, | 104 if (!GetProgramFilesX86(&cur)) |
60 SHGFP_TYPE_CURRENT, system_buffer))) | |
61 return false; | 105 return false; |
62 cur = FilePath(system_buffer); | |
63 break; | 106 break; |
64 } | 107 } |
65 // Fall through to base::DIR_PROGRAM_FILES if we're on an X86 machine. | 108 // Fall through to base::DIR_PROGRAM_FILES if we're on an X86 machine. |
66 case base::DIR_PROGRAM_FILES: | 109 case base::DIR_PROGRAM_FILES: |
67 if (FAILED(SHGetFolderPath(NULL, CSIDL_PROGRAM_FILES, NULL, | 110 if (!GetProgramFiles(&cur)) |
68 SHGFP_TYPE_CURRENT, system_buffer))) | |
69 return false; | 111 return false; |
70 cur = FilePath(system_buffer); | |
71 break; | 112 break; |
72 case base::DIR_PROGRAM_FILES6432: | 113 case base::DIR_PROGRAM_FILES6432: |
73 #if !defined(_WIN64) | 114 #if !defined(_WIN64) |
74 if (base::win::OSInfo::GetInstance()->wow64_status() == | 115 if (base::win::OSInfo::GetInstance()->wow64_status() == |
75 base::win::OSInfo::WOW64_ENABLED) { | 116 base::win::OSInfo::WOW64_ENABLED) { |
76 scoped_ptr<base::Environment> env(base::Environment::Create()); | 117 scoped_ptr<base::Environment> env(base::Environment::Create()); |
77 std::string programfiles_w6432; | 118 std::string programfiles_w6432; |
78 // 32-bit process running in WOW64 sets ProgramW6432 environment | 119 // 32-bit process running in WOW64 sets ProgramW6432 environment |
79 // variable. See | 120 // variable. See |
80 // https://msdn.microsoft.com/library/windows/desktop/aa384274.aspx. | 121 // https://msdn.microsoft.com/library/windows/desktop/aa384274.aspx. |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
114 return false; | 155 return false; |
115 cur = FilePath(system_buffer); | 156 cur = FilePath(system_buffer); |
116 break; | 157 break; |
117 case base::DIR_COMMON_APP_DATA: | 158 case base::DIR_COMMON_APP_DATA: |
118 if (FAILED(SHGetFolderPath(NULL, CSIDL_COMMON_APPDATA, NULL, | 159 if (FAILED(SHGetFolderPath(NULL, CSIDL_COMMON_APPDATA, NULL, |
119 SHGFP_TYPE_CURRENT, system_buffer))) | 160 SHGFP_TYPE_CURRENT, system_buffer))) |
120 return false; | 161 return false; |
121 cur = FilePath(system_buffer); | 162 cur = FilePath(system_buffer); |
122 break; | 163 break; |
123 case base::DIR_LOCAL_APP_DATA: | 164 case base::DIR_LOCAL_APP_DATA: |
124 if (FAILED(SHGetFolderPath(NULL, CSIDL_LOCAL_APPDATA, NULL, | 165 if (!GetLocalAppData(&cur)) |
125 SHGFP_TYPE_CURRENT, system_buffer))) | |
126 return false; | 166 return false; |
127 cur = FilePath(system_buffer); | |
128 break; | 167 break; |
129 case base::DIR_SOURCE_ROOT: { | 168 case base::DIR_SOURCE_ROOT: { |
130 FilePath executableDir; | 169 FilePath executableDir; |
131 // On Windows, unit tests execute two levels deep from the source root. | 170 // On Windows, unit tests execute two levels deep from the source root. |
132 // For example: chrome/{Debug|Release}/ui_tests.exe | 171 // For example: chrome/{Debug|Release}/ui_tests.exe |
133 PathService::Get(base::DIR_EXE, &executableDir); | 172 PathService::Get(base::DIR_EXE, &executableDir); |
134 cur = executableDir.DirName().DirName(); | 173 cur = executableDir.DirName().DirName(); |
135 break; | 174 break; |
136 } | 175 } |
137 case base::DIR_APP_SHORTCUTS: { | 176 case base::DIR_APP_SHORTCUTS: { |
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
188 break; | 227 break; |
189 default: | 228 default: |
190 return false; | 229 return false; |
191 } | 230 } |
192 | 231 |
193 *result = cur; | 232 *result = cur; |
194 return true; | 233 return true; |
195 } | 234 } |
196 | 235 |
197 } // namespace base | 236 } // namespace base |
OLD | NEW |