1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
base / base_paths_win.cc [blame]
// Copyright 2012 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "base/base_paths.h"
#include <windows.h>
#include <KnownFolders.h>
#include <shlobj.h>
#include "base/environment.h"
#include "base/files/file_path.h"
#include "base/files/file_util.h"
#include "base/path_service.h"
#include "base/strings/string_util.h"
#include "base/strings/utf_string_conversions.h"
#include "base/win/current_module.h"
#include "base/win/scoped_co_mem.h"
#include "base/win/windows_version.h"
using base::FilePath;
namespace base {
bool PathProviderWin(int key, FilePath* result) {
// We need to go compute the value. It would be nice to support paths with
// names longer than MAX_PATH, but the system functions don't seem to be
// designed for it either, with the exception of GetTempPath (but other
// things will surely break if the temp path is too long, so we don't bother
// handling it.
wchar_t system_buffer[MAX_PATH];
system_buffer[0] = 0;
FilePath cur;
switch (key) {
case base::FILE_EXE:
if (GetModuleFileName(NULL, system_buffer, MAX_PATH) == 0)
return false;
cur = FilePath(system_buffer);
break;
case base::FILE_MODULE: {
// the resource containing module is assumed to be the one that
// this code lives in, whether that's a dll or exe
if (GetModuleFileName(CURRENT_MODULE(), system_buffer, MAX_PATH) == 0)
return false;
cur = FilePath(system_buffer);
break;
}
case base::DIR_WINDOWS:
GetWindowsDirectory(system_buffer, MAX_PATH);
cur = FilePath(system_buffer);
break;
case base::DIR_SYSTEM:
GetSystemDirectory(system_buffer, MAX_PATH);
cur = FilePath(system_buffer);
break;
case base::DIR_PROGRAM_FILESX86:
if (win::OSInfo::GetArchitecture() != win::OSInfo::X86_ARCHITECTURE) {
if (FAILED(SHGetFolderPath(NULL, CSIDL_PROGRAM_FILESX86, NULL,
SHGFP_TYPE_CURRENT, system_buffer)))
return false;
cur = FilePath(system_buffer);
break;
}
// Fall through to base::DIR_PROGRAM_FILES if we're on an X86 machine.
[[fallthrough]];
case base::DIR_PROGRAM_FILES:
if (FAILED(SHGetFolderPath(NULL, CSIDL_PROGRAM_FILES, NULL,
SHGFP_TYPE_CURRENT, system_buffer)))
return false;
cur = FilePath(system_buffer);
break;
case base::DIR_PROGRAM_FILES6432:
#if !defined(_WIN64)
if (base::win::OSInfo::GetInstance()->IsWowX86OnAMD64() ||
base::win::OSInfo::GetInstance()->IsWowX86OnARM64()) {
std::unique_ptr<base::Environment> env(base::Environment::Create());
std::string programfiles_w6432;
// 32-bit process running in WOW64 sets ProgramW6432 environment
// variable. See
// https://msdn.microsoft.com/library/windows/desktop/aa384274.aspx.
if (!env->GetVar("ProgramW6432", &programfiles_w6432))
return false;
// GetVar returns UTF8 - convert back to Wide.
cur = FilePath(UTF8ToWide(programfiles_w6432));
break;
}
#endif
if (FAILED(SHGetFolderPath(NULL, CSIDL_PROGRAM_FILES, NULL,
SHGFP_TYPE_CURRENT, system_buffer)))
return false;
cur = FilePath(system_buffer);
break;
case base::DIR_IE_INTERNET_CACHE:
if (FAILED(SHGetFolderPath(NULL, CSIDL_INTERNET_CACHE, NULL,
SHGFP_TYPE_CURRENT, system_buffer)))
return false;
cur = FilePath(system_buffer);
break;
case base::DIR_COMMON_START_MENU:
if (FAILED(SHGetFolderPath(NULL, CSIDL_COMMON_PROGRAMS, NULL,
SHGFP_TYPE_CURRENT, system_buffer)))
return false;
cur = FilePath(system_buffer);
break;
case base::DIR_START_MENU:
if (FAILED(SHGetFolderPath(NULL, CSIDL_PROGRAMS, NULL, SHGFP_TYPE_CURRENT,
system_buffer)))
return false;
cur = FilePath(system_buffer);
break;
case base::DIR_COMMON_STARTUP:
if (FAILED(SHGetFolderPath(nullptr, CSIDL_COMMON_STARTUP, nullptr,
SHGFP_TYPE_CURRENT, system_buffer)))
return false;
cur = FilePath(system_buffer);
break;
case base::DIR_USER_STARTUP:
if (FAILED(SHGetFolderPath(nullptr, CSIDL_STARTUP, nullptr,
SHGFP_TYPE_CURRENT, system_buffer)))
return false;
cur = FilePath(system_buffer);
break;
case base::DIR_ROAMING_APP_DATA:
if (FAILED(SHGetFolderPath(NULL, CSIDL_APPDATA, NULL, SHGFP_TYPE_CURRENT,
system_buffer)))
return false;
cur = FilePath(system_buffer);
break;
case base::DIR_COMMON_APP_DATA:
if (FAILED(SHGetFolderPath(NULL, CSIDL_COMMON_APPDATA, NULL,
SHGFP_TYPE_CURRENT, system_buffer)))
return false;
cur = FilePath(system_buffer);
break;
case base::DIR_LOCAL_APP_DATA:
if (FAILED(SHGetFolderPath(NULL, CSIDL_LOCAL_APPDATA, NULL,
SHGFP_TYPE_CURRENT, system_buffer)))
return false;
cur = FilePath(system_buffer);
break;
case base::DIR_SRC_TEST_DATA_ROOT: {
FilePath executableDir;
// On Windows, unit tests execute two levels deep from the source root.
// For example: chrome/{Debug|Release}/ui_tests.exe
PathService::Get(base::DIR_EXE, &executableDir);
cur = executableDir.DirName().DirName();
break;
}
case base::DIR_APP_SHORTCUTS: {
base::win::ScopedCoMem<wchar_t> path_buf;
if (FAILED(SHGetKnownFolderPath(FOLDERID_ApplicationShortcuts, 0, NULL,
&path_buf)))
return false;
cur = FilePath(path_buf.get());
break;
}
case base::DIR_USER_DESKTOP:
if (FAILED(SHGetFolderPath(NULL, CSIDL_DESKTOPDIRECTORY, NULL,
SHGFP_TYPE_CURRENT, system_buffer))) {
return false;
}
cur = FilePath(system_buffer);
break;
case base::DIR_COMMON_DESKTOP:
if (FAILED(SHGetFolderPath(NULL, CSIDL_COMMON_DESKTOPDIRECTORY, NULL,
SHGFP_TYPE_CURRENT, system_buffer))) {
return false;
}
cur = FilePath(system_buffer);
break;
case base::DIR_USER_QUICK_LAUNCH:
if (!PathService::Get(base::DIR_ROAMING_APP_DATA, &cur))
return false;
// According to various sources, appending
// "Microsoft\Internet Explorer\Quick Launch" to %appdata% is the only
// reliable way to get the quick launch folder across all versions of
// Windows.
// http://stackoverflow.com/questions/76080/how-do-you-reliably-get-the-quick-
// http://www.microsoft.com/technet/scriptcenter/resources/qanda/sept05/hey0901.mspx
cur = cur.Append(FILE_PATH_LITERAL("Microsoft"))
.Append(FILE_PATH_LITERAL("Internet Explorer"))
.Append(FILE_PATH_LITERAL("Quick Launch"));
break;
case base::DIR_TASKBAR_PINS: {
if (!PathService::Get(base::DIR_USER_QUICK_LAUNCH, &cur))
return false;
cur = cur.Append(FILE_PATH_LITERAL("User Pinned"))
.Append(FILE_PATH_LITERAL("TaskBar"));
break;
}
case base::DIR_IMPLICIT_APP_SHORTCUTS:
if (!PathService::Get(base::DIR_USER_QUICK_LAUNCH, &cur))
return false;
cur = cur.Append(FILE_PATH_LITERAL("User Pinned"))
.Append(FILE_PATH_LITERAL("ImplicitAppShortcuts"));
break;
case base::DIR_WINDOWS_FONTS:
if (FAILED(SHGetFolderPath(NULL, CSIDL_FONTS, NULL, SHGFP_TYPE_CURRENT,
system_buffer))) {
return false;
}
cur = FilePath(system_buffer);
break;
case base::DIR_SYSTEM_TEMP:
// Try C:\Windows\SystemTemp, which was introduced sometime before Windows
// 10 build 19042. Do not use GetTempPath2, as it only appeared later and
// will only return the path for processes running as SYSTEM.
if (PathService::Get(DIR_WINDOWS, &cur)) {
cur = cur.Append(FILE_PATH_LITERAL("SystemTemp"));
if (PathIsWritable(cur)) {
break;
}
}
// Failing that, use C:\Program Files or C:\Program Files (x86) for older
// versions of Windows 10.
if (!PathService::Get(DIR_PROGRAM_FILES, &cur) || !PathIsWritable(cur)) {
return false;
}
break;
default:
return false;
}
*result = cur;
return true;
}
} // namespace base