blob: e7e5d1f95d8f772988b9d418104199fee1caf918 [file] [log] [blame]
[email protected]569edabd2012-02-03 23:10:041// Copyright (c) 2012 The Chromium Authors. All rights reserved.
license.botbf09a502008-08-24 00:55:552// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
[email protected]ac510e12008-08-05 19:46:314
[email protected]5d99d63b2008-08-19 09:26:575#include <windows.h>
Bruce Dawson8dcf6bc2017-12-07 17:46:046#include <KnownFolders.h>
[email protected]ac510e12008-08-05 19:46:317#include <shlobj.h>
8
[email protected]dea1d7d2012-09-20 16:24:529#include "base/base_paths.h"
wfh16d2f122015-03-13 14:34:4710#include "base/environment.h"
[email protected]57999812013-02-24 05:40:5211#include "base/files/file_path.h"
[email protected]ac510e12008-08-05 19:46:3112#include "base/path_service.h"
wfh16d2f122015-03-13 14:34:4713#include "base/strings/utf_string_conversions.h"
thakisd62f54472016-04-04 02:21:1014#include "base/win/current_module.h"
[email protected]b2721b02012-08-30 09:16:5515#include "base/win/scoped_co_mem.h"
[email protected]935aa542010-10-15 01:59:1516#include "base/win/windows_version.h"
[email protected]ac510e12008-08-05 19:46:3117
[email protected]631a5472013-02-18 06:14:5918using base::FilePath;
19
[email protected]ac510e12008-08-05 19:46:3120namespace base {
21
[email protected]4792a262008-11-19 16:50:0322bool PathProviderWin(int key, FilePath* result) {
[email protected]ac510e12008-08-05 19:46:3123 // We need to go compute the value. It would be nice to support paths with
24 // names longer than MAX_PATH, but the system functions don't seem to be
25 // designed for it either, with the exception of GetTempPath (but other
26 // things will surely break if the temp path is too long, so we don't bother
27 // handling it.
28 wchar_t system_buffer[MAX_PATH];
29 system_buffer[0] = 0;
30
[email protected]4792a262008-11-19 16:50:0331 FilePath cur;
[email protected]ac510e12008-08-05 19:46:3132 switch (key) {
33 case base::FILE_EXE:
aranovskii8dcedce2015-06-24 09:03:5134 if (GetModuleFileName(NULL, system_buffer, MAX_PATH) == 0)
35 return false;
[email protected]4792a262008-11-19 16:50:0336 cur = FilePath(system_buffer);
[email protected]ac510e12008-08-05 19:46:3137 break;
38 case base::FILE_MODULE: {
39 // the resource containing module is assumed to be the one that
40 // this code lives in, whether that's a dll or exe
thakisd62f54472016-04-04 02:21:1041 if (GetModuleFileName(CURRENT_MODULE(), system_buffer, MAX_PATH) == 0)
aranovskii8dcedce2015-06-24 09:03:5142 return false;
[email protected]4792a262008-11-19 16:50:0343 cur = FilePath(system_buffer);
[email protected]ac510e12008-08-05 19:46:3144 break;
45 }
46 case base::DIR_WINDOWS:
47 GetWindowsDirectory(system_buffer, MAX_PATH);
[email protected]4792a262008-11-19 16:50:0348 cur = FilePath(system_buffer);
[email protected]ac510e12008-08-05 19:46:3149 break;
50 case base::DIR_SYSTEM:
51 GetSystemDirectory(system_buffer, MAX_PATH);
[email protected]4792a262008-11-19 16:50:0352 cur = FilePath(system_buffer);
[email protected]ac510e12008-08-05 19:46:3153 break;
[email protected]9759ffc2011-04-25 18:03:1254 case base::DIR_PROGRAM_FILESX86:
55 if (base::win::OSInfo::GetInstance()->architecture() !=
56 base::win::OSInfo::X86_ARCHITECTURE) {
57 if (FAILED(SHGetFolderPath(NULL, CSIDL_PROGRAM_FILESX86, NULL,
58 SHGFP_TYPE_CURRENT, system_buffer)))
59 return false;
60 cur = FilePath(system_buffer);
61 break;
62 }
63 // Fall through to base::DIR_PROGRAM_FILES if we're on an X86 machine.
Nico Weber33ee64eb2018-02-02 22:52:0964 FALLTHROUGH;
[email protected]ac510e12008-08-05 19:46:3165 case base::DIR_PROGRAM_FILES:
66 if (FAILED(SHGetFolderPath(NULL, CSIDL_PROGRAM_FILES, NULL,
67 SHGFP_TYPE_CURRENT, system_buffer)))
68 return false;
[email protected]4792a262008-11-19 16:50:0369 cur = FilePath(system_buffer);
[email protected]ac510e12008-08-05 19:46:3170 break;
wfh16d2f122015-03-13 14:34:4771 case base::DIR_PROGRAM_FILES6432:
72#if !defined(_WIN64)
73 if (base::win::OSInfo::GetInstance()->wow64_status() ==
74 base::win::OSInfo::WOW64_ENABLED) {
dcheng093de9b2016-04-04 21:25:5175 std::unique_ptr<base::Environment> env(base::Environment::Create());
wfh16d2f122015-03-13 14:34:4776 std::string programfiles_w6432;
77 // 32-bit process running in WOW64 sets ProgramW6432 environment
78 // variable. See
79 // https://msdn.microsoft.com/library/windows/desktop/aa384274.aspx.
80 if (!env->GetVar("ProgramW6432", &programfiles_w6432))
81 return false;
82 // GetVar returns UTF8 - convert back to Wide.
83 cur = FilePath(UTF8ToWide(programfiles_w6432));
84 break;
85 }
86#endif
87 if (FAILED(SHGetFolderPath(NULL, CSIDL_PROGRAM_FILES, NULL,
88 SHGFP_TYPE_CURRENT, system_buffer)))
89 return false;
90 cur = FilePath(system_buffer);
91 break;
[email protected]ac510e12008-08-05 19:46:3192 case base::DIR_IE_INTERNET_CACHE:
93 if (FAILED(SHGetFolderPath(NULL, CSIDL_INTERNET_CACHE, NULL,
94 SHGFP_TYPE_CURRENT, system_buffer)))
95 return false;
[email protected]4792a262008-11-19 16:50:0396 cur = FilePath(system_buffer);
[email protected]ac510e12008-08-05 19:46:3197 break;
98 case base::DIR_COMMON_START_MENU:
99 if (FAILED(SHGetFolderPath(NULL, CSIDL_COMMON_PROGRAMS, NULL,
100 SHGFP_TYPE_CURRENT, system_buffer)))
101 return false;
[email protected]4792a262008-11-19 16:50:03102 cur = FilePath(system_buffer);
[email protected]ac510e12008-08-05 19:46:31103 break;
104 case base::DIR_START_MENU:
105 if (FAILED(SHGetFolderPath(NULL, CSIDL_PROGRAMS, NULL,
106 SHGFP_TYPE_CURRENT, system_buffer)))
107 return false;
[email protected]4792a262008-11-19 16:50:03108 cur = FilePath(system_buffer);
[email protected]ac510e12008-08-05 19:46:31109 break;
110 case base::DIR_APP_DATA:
111 if (FAILED(SHGetFolderPath(NULL, CSIDL_APPDATA, NULL, SHGFP_TYPE_CURRENT,
112 system_buffer)))
113 return false;
[email protected]4792a262008-11-19 16:50:03114 cur = FilePath(system_buffer);
[email protected]ac510e12008-08-05 19:46:31115 break;
[email protected]bf3e52c32012-04-04 05:18:47116 case base::DIR_COMMON_APP_DATA:
117 if (FAILED(SHGetFolderPath(NULL, CSIDL_COMMON_APPDATA, NULL,
118 SHGFP_TYPE_CURRENT, system_buffer)))
119 return false;
120 cur = FilePath(system_buffer);
121 break;
[email protected]ac510e12008-08-05 19:46:31122 case base::DIR_LOCAL_APP_DATA:
123 if (FAILED(SHGetFolderPath(NULL, CSIDL_LOCAL_APPDATA, NULL,
124 SHGFP_TYPE_CURRENT, system_buffer)))
125 return false;
[email protected]4792a262008-11-19 16:50:03126 cur = FilePath(system_buffer);
[email protected]ac510e12008-08-05 19:46:31127 break;
[email protected]14a25e502010-06-15 06:53:52128 case base::DIR_SOURCE_ROOT: {
129 FilePath executableDir;
[email protected]37088fef2008-08-15 17:32:10130 // On Windows, unit tests execute two levels deep from the source root.
131 // For example: chrome/{Debug|Release}/ui_tests.exe
[email protected]14a25e502010-06-15 06:53:52132 PathService::Get(base::DIR_EXE, &executableDir);
133 cur = executableDir.DirName().DirName();
[email protected]37088fef2008-08-15 17:32:10134 break;
[email protected]14a25e502010-06-15 06:53:52135 }
[email protected]b2721b02012-08-30 09:16:55136 case base::DIR_APP_SHORTCUTS: {
137 if (win::GetVersion() < win::VERSION_WIN8)
138 return false;
139
140 base::win::ScopedCoMem<wchar_t> path_buf;
141 if (FAILED(SHGetKnownFolderPath(FOLDERID_ApplicationShortcuts, 0, NULL,
142 &path_buf)))
143 return false;
144
145 cur = FilePath(string16(path_buf));
146 break;
147 }
[email protected]dea1d7d2012-09-20 16:24:52148 case base::DIR_USER_DESKTOP:
149 if (FAILED(SHGetFolderPath(NULL, CSIDL_DESKTOPDIRECTORY, NULL,
150 SHGFP_TYPE_CURRENT, system_buffer))) {
151 return false;
152 }
153 cur = FilePath(system_buffer);
154 break;
155 case base::DIR_COMMON_DESKTOP:
156 if (FAILED(SHGetFolderPath(NULL, CSIDL_COMMON_DESKTOPDIRECTORY, NULL,
157 SHGFP_TYPE_CURRENT, system_buffer))) {
158 return false;
159 }
160 cur = FilePath(system_buffer);
161 break;
162 case base::DIR_USER_QUICK_LAUNCH:
[email protected]da4d4fb2014-08-08 18:17:53163 if (!PathService::Get(base::DIR_APP_DATA, &cur))
[email protected]dea1d7d2012-09-20 16:24:52164 return false;
[email protected]da4d4fb2014-08-08 18:17:53165 // According to various sources, appending
166 // "Microsoft\Internet Explorer\Quick Launch" to %appdata% is the only
167 // reliable way to get the quick launch folder across all versions of
168 // Windows.
169 // http://stackoverflow.com/questions/76080/how-do-you-reliably-get-the-quick-
170 // http://www.microsoft.com/technet/scriptcenter/resources/qanda/sept05/hey0901.mspx
pmonette565a8802016-06-21 00:03:43171 cur = cur.Append(FILE_PATH_LITERAL("Microsoft"))
172 .Append(FILE_PATH_LITERAL("Internet Explorer"))
173 .Append(FILE_PATH_LITERAL("Quick Launch"));
[email protected]dea1d7d2012-09-20 16:24:52174 break;
[email protected]e5f9d822012-11-06 22:27:01175 case base::DIR_TASKBAR_PINS:
176 if (!PathService::Get(base::DIR_USER_QUICK_LAUNCH, &cur))
177 return false;
pmonette5057ca3b2016-07-04 16:48:40178 cur = cur.Append(FILE_PATH_LITERAL("User Pinned"))
179 .Append(FILE_PATH_LITERAL("TaskBar"));
180 break;
181 case base::DIR_IMPLICIT_APP_SHORTCUTS:
182 if (!PathService::Get(base::DIR_USER_QUICK_LAUNCH, &cur))
183 return false;
184 cur = cur.Append(FILE_PATH_LITERAL("User Pinned"))
185 .Append(FILE_PATH_LITERAL("ImplicitAppShortcuts"));
[email protected]e5f9d822012-11-06 22:27:01186 break;
[email protected]3f18e8d2014-03-26 01:41:04187 case base::DIR_WINDOWS_FONTS:
188 if (FAILED(SHGetFolderPath(
189 NULL, CSIDL_FONTS, NULL, SHGFP_TYPE_CURRENT, system_buffer))) {
190 return false;
191 }
192 cur = FilePath(system_buffer);
193 break;
[email protected]ac510e12008-08-05 19:46:31194 default:
195 return false;
196 }
197
[email protected]4792a262008-11-19 16:50:03198 *result = cur;
[email protected]ac510e12008-08-05 19:46:31199 return true;
200}
201
202} // namespace base