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.
8 #include "base/base_paths.h"
9 #include "base/environment.h"
10 #include "base/files/file_path.h"
11 #include "base/path_service.h"
12 #include "base/strings/utf_string_conversions.h"
13 #include "base/win/scoped_co_mem.h"
14 #include "base/win/windows_version.h"
16 // http://blogs.msdn.com/oldnewthing/archive/2004/10/25/247180.aspx
17 extern "C" IMAGE_DOS_HEADER __ImageBase
;
23 bool PathProviderWin(int key
, FilePath
* result
) {
24 // 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
26 // 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
29 wchar_t system_buffer
[MAX_PATH
];
35 if (GetModuleFileName(NULL
, system_buffer
, MAX_PATH
) == 0)
37 cur
= FilePath(system_buffer
);
39 case base::FILE_MODULE
: {
40 // the resource containing module is assumed to be the one that
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)
45 cur
= FilePath(system_buffer
);
48 case base::DIR_WINDOWS
:
49 GetWindowsDirectory(system_buffer
, MAX_PATH
);
50 cur
= FilePath(system_buffer
);
52 case base::DIR_SYSTEM
:
53 GetSystemDirectory(system_buffer
, MAX_PATH
);
54 cur
= FilePath(system_buffer
);
56 case base::DIR_PROGRAM_FILESX86
:
57 if (base::win::OSInfo::GetInstance()->architecture() !=
58 base::win::OSInfo::X86_ARCHITECTURE
) {
59 if (FAILED(SHGetFolderPath(NULL
, CSIDL_PROGRAM_FILESX86
, NULL
,
60 SHGFP_TYPE_CURRENT
, system_buffer
)))
62 cur
= FilePath(system_buffer
);
65 // Fall through to base::DIR_PROGRAM_FILES if we're on an X86 machine.
66 case base::DIR_PROGRAM_FILES
:
67 if (FAILED(SHGetFolderPath(NULL
, CSIDL_PROGRAM_FILES
, NULL
,
68 SHGFP_TYPE_CURRENT
, system_buffer
)))
70 cur
= FilePath(system_buffer
);
72 case base::DIR_PROGRAM_FILES6432
:
74 if (base::win::OSInfo::GetInstance()->wow64_status() ==
75 base::win::OSInfo::WOW64_ENABLED
) {
76 scoped_ptr
<base::Environment
> env(base::Environment::Create());
77 std::string programfiles_w6432
;
78 // 32-bit process running in WOW64 sets ProgramW6432 environment
80 // https://msdn.microsoft.com/library/windows/desktop/aa384274.aspx.
81 if (!env
->GetVar("ProgramW6432", &programfiles_w6432
))
83 // GetVar returns UTF8 - convert back to Wide.
84 cur
= FilePath(UTF8ToWide(programfiles_w6432
));
88 if (FAILED(SHGetFolderPath(NULL
, CSIDL_PROGRAM_FILES
, NULL
,
89 SHGFP_TYPE_CURRENT
, system_buffer
)))
91 cur
= FilePath(system_buffer
);
93 case base::DIR_IE_INTERNET_CACHE
:
94 if (FAILED(SHGetFolderPath(NULL
, CSIDL_INTERNET_CACHE
, NULL
,
95 SHGFP_TYPE_CURRENT
, system_buffer
)))
97 cur
= FilePath(system_buffer
);
99 case base::DIR_COMMON_START_MENU
:
100 if (FAILED(SHGetFolderPath(NULL
, CSIDL_COMMON_PROGRAMS
, NULL
,
101 SHGFP_TYPE_CURRENT
, system_buffer
)))
103 cur
= FilePath(system_buffer
);
105 case base::DIR_START_MENU
:
106 if (FAILED(SHGetFolderPath(NULL
, CSIDL_PROGRAMS
, NULL
,
107 SHGFP_TYPE_CURRENT
, system_buffer
)))
109 cur
= FilePath(system_buffer
);
111 case base::DIR_APP_DATA
:
112 if (FAILED(SHGetFolderPath(NULL
, CSIDL_APPDATA
, NULL
, SHGFP_TYPE_CURRENT
,
115 cur
= FilePath(system_buffer
);
117 case base::DIR_COMMON_APP_DATA
:
118 if (FAILED(SHGetFolderPath(NULL
, CSIDL_COMMON_APPDATA
, NULL
,
119 SHGFP_TYPE_CURRENT
, system_buffer
)))
121 cur
= FilePath(system_buffer
);
123 case base::DIR_LOCAL_APP_DATA
:
124 if (FAILED(SHGetFolderPath(NULL
, CSIDL_LOCAL_APPDATA
, NULL
,
125 SHGFP_TYPE_CURRENT
, system_buffer
)))
127 cur
= FilePath(system_buffer
);
129 case base::DIR_SOURCE_ROOT
: {
130 FilePath executableDir
;
131 // On Windows, unit tests execute two levels deep from the source root.
132 // For example: chrome/{Debug|Release}/ui_tests.exe
133 PathService::Get(base::DIR_EXE
, &executableDir
);
134 cur
= executableDir
.DirName().DirName();
137 case base::DIR_APP_SHORTCUTS
: {
138 if (win::GetVersion() < win::VERSION_WIN8
)
141 base::win::ScopedCoMem
<wchar_t> path_buf
;
142 if (FAILED(SHGetKnownFolderPath(FOLDERID_ApplicationShortcuts
, 0, NULL
,
146 cur
= FilePath(string16(path_buf
));
149 case base::DIR_USER_DESKTOP
:
150 if (FAILED(SHGetFolderPath(NULL
, CSIDL_DESKTOPDIRECTORY
, NULL
,
151 SHGFP_TYPE_CURRENT
, system_buffer
))) {
154 cur
= FilePath(system_buffer
);
156 case base::DIR_COMMON_DESKTOP
:
157 if (FAILED(SHGetFolderPath(NULL
, CSIDL_COMMON_DESKTOPDIRECTORY
, NULL
,
158 SHGFP_TYPE_CURRENT
, system_buffer
))) {
161 cur
= FilePath(system_buffer
);
163 case base::DIR_USER_QUICK_LAUNCH
:
164 if (!PathService::Get(base::DIR_APP_DATA
, &cur
))
166 // According to various sources, appending
167 // "Microsoft\Internet Explorer\Quick Launch" to %appdata% is the only
168 // reliable way to get the quick launch folder across all versions of
170 // http://stackoverflow.com/questions/76080/how-do-you-reliably-get-the-quick-
171 // http://www.microsoft.com/technet/scriptcenter/resources/qanda/sept05/hey0901.mspx
172 cur
= cur
.AppendASCII("Microsoft")
173 .AppendASCII("Internet Explorer")
174 .AppendASCII("Quick Launch");
176 case base::DIR_TASKBAR_PINS
:
177 if (!PathService::Get(base::DIR_USER_QUICK_LAUNCH
, &cur
))
179 cur
= cur
.AppendASCII("User Pinned");
180 cur
= cur
.AppendASCII("TaskBar");
182 case base::DIR_WINDOWS_FONTS
:
183 if (FAILED(SHGetFolderPath(
184 NULL
, CSIDL_FONTS
, NULL
, SHGFP_TYPE_CURRENT
, system_buffer
))) {
187 cur
= FilePath(system_buffer
);