[email protected] | 569edabd | 2012-02-03 23:10:04 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
license.bot | bf09a50 | 2008-08-24 00:55:55 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
[email protected] | ac510e1 | 2008-08-05 19:46:31 | [diff] [blame] | 4 | |
[email protected] | 5d99d63b | 2008-08-19 09:26:57 | [diff] [blame] | 5 | #include <windows.h> |
Bruce Dawson | 8dcf6bc | 2017-12-07 17:46:04 | [diff] [blame] | 6 | #include <KnownFolders.h> |
[email protected] | ac510e1 | 2008-08-05 19:46:31 | [diff] [blame] | 7 | #include <shlobj.h> |
| 8 | |
[email protected] | dea1d7d | 2012-09-20 16:24:52 | [diff] [blame] | 9 | #include "base/base_paths.h" |
wfh | 16d2f12 | 2015-03-13 14:34:47 | [diff] [blame] | 10 | #include "base/environment.h" |
[email protected] | 5799981 | 2013-02-24 05:40:52 | [diff] [blame] | 11 | #include "base/files/file_path.h" |
[email protected] | ac510e1 | 2008-08-05 19:46:31 | [diff] [blame] | 12 | #include "base/path_service.h" |
wfh | 16d2f12 | 2015-03-13 14:34:47 | [diff] [blame] | 13 | #include "base/strings/utf_string_conversions.h" |
thakis | d62f5447 | 2016-04-04 02:21:10 | [diff] [blame] | 14 | #include "base/win/current_module.h" |
[email protected] | b2721b0 | 2012-08-30 09:16:55 | [diff] [blame] | 15 | #include "base/win/scoped_co_mem.h" |
[email protected] | 935aa54 | 2010-10-15 01:59:15 | [diff] [blame] | 16 | #include "base/win/windows_version.h" |
[email protected] | ac510e1 | 2008-08-05 19:46:31 | [diff] [blame] | 17 | |
[email protected] | 631a547 | 2013-02-18 06:14:59 | [diff] [blame] | 18 | using base::FilePath; |
| 19 | |
[email protected] | ac510e1 | 2008-08-05 19:46:31 | [diff] [blame] | 20 | namespace base { |
| 21 | |
[email protected] | 4792a26 | 2008-11-19 16:50:03 | [diff] [blame] | 22 | bool PathProviderWin(int key, FilePath* result) { |
[email protected] | ac510e1 | 2008-08-05 19:46:31 | [diff] [blame] | 23 | // 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] | 4792a26 | 2008-11-19 16:50:03 | [diff] [blame] | 31 | FilePath cur; |
[email protected] | ac510e1 | 2008-08-05 19:46:31 | [diff] [blame] | 32 | switch (key) { |
| 33 | case base::FILE_EXE: |
aranovskii | 8dcedce | 2015-06-24 09:03:51 | [diff] [blame] | 34 | if (GetModuleFileName(NULL, system_buffer, MAX_PATH) == 0) |
| 35 | return false; |
[email protected] | 4792a26 | 2008-11-19 16:50:03 | [diff] [blame] | 36 | cur = FilePath(system_buffer); |
[email protected] | ac510e1 | 2008-08-05 19:46:31 | [diff] [blame] | 37 | 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 |
thakis | d62f5447 | 2016-04-04 02:21:10 | [diff] [blame] | 41 | if (GetModuleFileName(CURRENT_MODULE(), system_buffer, MAX_PATH) == 0) |
aranovskii | 8dcedce | 2015-06-24 09:03:51 | [diff] [blame] | 42 | return false; |
[email protected] | 4792a26 | 2008-11-19 16:50:03 | [diff] [blame] | 43 | cur = FilePath(system_buffer); |
[email protected] | ac510e1 | 2008-08-05 19:46:31 | [diff] [blame] | 44 | break; |
| 45 | } |
| 46 | case base::DIR_WINDOWS: |
| 47 | GetWindowsDirectory(system_buffer, MAX_PATH); |
[email protected] | 4792a26 | 2008-11-19 16:50:03 | [diff] [blame] | 48 | cur = FilePath(system_buffer); |
[email protected] | ac510e1 | 2008-08-05 19:46:31 | [diff] [blame] | 49 | break; |
| 50 | case base::DIR_SYSTEM: |
| 51 | GetSystemDirectory(system_buffer, MAX_PATH); |
[email protected] | 4792a26 | 2008-11-19 16:50:03 | [diff] [blame] | 52 | cur = FilePath(system_buffer); |
[email protected] | ac510e1 | 2008-08-05 19:46:31 | [diff] [blame] | 53 | break; |
[email protected] | 9759ffc | 2011-04-25 18:03:12 | [diff] [blame] | 54 | 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 Weber | 33ee64eb | 2018-02-02 22:52:09 | [diff] [blame] | 64 | FALLTHROUGH; |
[email protected] | ac510e1 | 2008-08-05 19:46:31 | [diff] [blame] | 65 | 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] | 4792a26 | 2008-11-19 16:50:03 | [diff] [blame] | 69 | cur = FilePath(system_buffer); |
[email protected] | ac510e1 | 2008-08-05 19:46:31 | [diff] [blame] | 70 | break; |
wfh | 16d2f12 | 2015-03-13 14:34:47 | [diff] [blame] | 71 | case base::DIR_PROGRAM_FILES6432: |
| 72 | #if !defined(_WIN64) |
| 73 | if (base::win::OSInfo::GetInstance()->wow64_status() == |
| 74 | base::win::OSInfo::WOW64_ENABLED) { |
dcheng | 093de9b | 2016-04-04 21:25:51 | [diff] [blame] | 75 | std::unique_ptr<base::Environment> env(base::Environment::Create()); |
wfh | 16d2f12 | 2015-03-13 14:34:47 | [diff] [blame] | 76 | 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] | ac510e1 | 2008-08-05 19:46:31 | [diff] [blame] | 92 | 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] | 4792a26 | 2008-11-19 16:50:03 | [diff] [blame] | 96 | cur = FilePath(system_buffer); |
[email protected] | ac510e1 | 2008-08-05 19:46:31 | [diff] [blame] | 97 | 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] | 4792a26 | 2008-11-19 16:50:03 | [diff] [blame] | 102 | cur = FilePath(system_buffer); |
[email protected] | ac510e1 | 2008-08-05 19:46:31 | [diff] [blame] | 103 | 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] | 4792a26 | 2008-11-19 16:50:03 | [diff] [blame] | 108 | cur = FilePath(system_buffer); |
[email protected] | ac510e1 | 2008-08-05 19:46:31 | [diff] [blame] | 109 | 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] | 4792a26 | 2008-11-19 16:50:03 | [diff] [blame] | 114 | cur = FilePath(system_buffer); |
[email protected] | ac510e1 | 2008-08-05 19:46:31 | [diff] [blame] | 115 | break; |
[email protected] | bf3e52c3 | 2012-04-04 05:18:47 | [diff] [blame] | 116 | 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] | ac510e1 | 2008-08-05 19:46:31 | [diff] [blame] | 122 | 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] | 4792a26 | 2008-11-19 16:50:03 | [diff] [blame] | 126 | cur = FilePath(system_buffer); |
[email protected] | ac510e1 | 2008-08-05 19:46:31 | [diff] [blame] | 127 | break; |
[email protected] | 14a25e50 | 2010-06-15 06:53:52 | [diff] [blame] | 128 | case base::DIR_SOURCE_ROOT: { |
| 129 | FilePath executableDir; |
[email protected] | 37088fef | 2008-08-15 17:32:10 | [diff] [blame] | 130 | // On Windows, unit tests execute two levels deep from the source root. |
| 131 | // For example: chrome/{Debug|Release}/ui_tests.exe |
[email protected] | 14a25e50 | 2010-06-15 06:53:52 | [diff] [blame] | 132 | PathService::Get(base::DIR_EXE, &executableDir); |
| 133 | cur = executableDir.DirName().DirName(); |
[email protected] | 37088fef | 2008-08-15 17:32:10 | [diff] [blame] | 134 | break; |
[email protected] | 14a25e50 | 2010-06-15 06:53:52 | [diff] [blame] | 135 | } |
[email protected] | b2721b0 | 2012-08-30 09:16:55 | [diff] [blame] | 136 | 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] | dea1d7d | 2012-09-20 16:24:52 | [diff] [blame] | 148 | 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] | da4d4fb | 2014-08-08 18:17:53 | [diff] [blame] | 163 | if (!PathService::Get(base::DIR_APP_DATA, &cur)) |
[email protected] | dea1d7d | 2012-09-20 16:24:52 | [diff] [blame] | 164 | return false; |
[email protected] | da4d4fb | 2014-08-08 18:17:53 | [diff] [blame] | 165 | // 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 |
pmonette | 565a880 | 2016-06-21 00:03:43 | [diff] [blame] | 171 | cur = cur.Append(FILE_PATH_LITERAL("Microsoft")) |
| 172 | .Append(FILE_PATH_LITERAL("Internet Explorer")) |
| 173 | .Append(FILE_PATH_LITERAL("Quick Launch")); |
[email protected] | dea1d7d | 2012-09-20 16:24:52 | [diff] [blame] | 174 | break; |
[email protected] | e5f9d82 | 2012-11-06 22:27:01 | [diff] [blame] | 175 | case base::DIR_TASKBAR_PINS: |
| 176 | if (!PathService::Get(base::DIR_USER_QUICK_LAUNCH, &cur)) |
| 177 | return false; |
pmonette | 5057ca3b | 2016-07-04 16:48:40 | [diff] [blame] | 178 | 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] | e5f9d82 | 2012-11-06 22:27:01 | [diff] [blame] | 186 | break; |
[email protected] | 3f18e8d | 2014-03-26 01:41:04 | [diff] [blame] | 187 | 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] | ac510e1 | 2008-08-05 19:46:31 | [diff] [blame] | 194 | default: |
| 195 | return false; |
| 196 | } |
| 197 | |
[email protected] | 4792a26 | 2008-11-19 16:50:03 | [diff] [blame] | 198 | *result = cur; |
[email protected] | ac510e1 | 2008-08-05 19:46:31 | [diff] [blame] | 199 | return true; |
| 200 | } |
| 201 | |
| 202 | } // namespace base |