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/files/file_path.h"
10 #include "base/path_service.h"
11 #include "base/win/scoped_co_mem.h"
12 #include "base/win/windows_version.h"
14 // http://blogs.msdn.com/oldnewthing/archive/2004/10/25/247180.aspx
15 extern "C" IMAGE_DOS_HEADER __ImageBase
;
21 bool GetQuickLaunchPath(bool default_user
, FilePath
* result
) {
23 wchar_t system_buffer
[MAX_PATH
];
25 // As per MSDN, passing -1 for |hToken| indicates the Default user:
26 // http://msdn.microsoft.com/library/windows/desktop/bb762181.aspx
27 if (FAILED(SHGetFolderPath(NULL
, CSIDL_APPDATA
,
28 reinterpret_cast<HANDLE
>(-1), SHGFP_TYPE_CURRENT
,
32 *result
= FilePath(system_buffer
);
33 } else if (!PathService::Get(base::DIR_APP_DATA
, result
)) {
34 // For the current user, grab the APPDATA directory directly from the
38 // According to various sources, appending
39 // "Microsoft\Internet Explorer\Quick Launch" to %appdata% is the only
40 // reliable way to get the quick launch folder across all versions of Windows.
41 // http://stackoverflow.com/questions/76080/how-do-you-reliably-get-the-quick-
42 // http://www.microsoft.com/technet/scriptcenter/resources/qanda/sept05/hey0901.mspx
43 *result
= result
->AppendASCII("Microsoft");
44 *result
= result
->AppendASCII("Internet Explorer");
45 *result
= result
->AppendASCII("Quick Launch");
53 bool PathProviderWin(int key
, FilePath
* result
) {
54 // We need to go compute the value. It would be nice to support paths with
55 // names longer than MAX_PATH, but the system functions don't seem to be
56 // designed for it either, with the exception of GetTempPath (but other
57 // things will surely break if the temp path is too long, so we don't bother
59 wchar_t system_buffer
[MAX_PATH
];
65 GetModuleFileName(NULL
, system_buffer
, MAX_PATH
);
66 cur
= FilePath(system_buffer
);
68 case base::FILE_MODULE
: {
69 // the resource containing module is assumed to be the one that
70 // this code lives in, whether that's a dll or exe
71 HMODULE this_module
= reinterpret_cast<HMODULE
>(&__ImageBase
);
72 GetModuleFileName(this_module
, system_buffer
, MAX_PATH
);
73 cur
= FilePath(system_buffer
);
76 case base::DIR_WINDOWS
:
77 GetWindowsDirectory(system_buffer
, MAX_PATH
);
78 cur
= FilePath(system_buffer
);
80 case base::DIR_SYSTEM
:
81 GetSystemDirectory(system_buffer
, MAX_PATH
);
82 cur
= FilePath(system_buffer
);
84 case base::DIR_PROGRAM_FILESX86
:
85 if (base::win::OSInfo::GetInstance()->architecture() !=
86 base::win::OSInfo::X86_ARCHITECTURE
) {
87 if (FAILED(SHGetFolderPath(NULL
, CSIDL_PROGRAM_FILESX86
, NULL
,
88 SHGFP_TYPE_CURRENT
, system_buffer
)))
90 cur
= FilePath(system_buffer
);
93 // Fall through to base::DIR_PROGRAM_FILES if we're on an X86 machine.
94 case base::DIR_PROGRAM_FILES
:
95 if (FAILED(SHGetFolderPath(NULL
, CSIDL_PROGRAM_FILES
, NULL
,
96 SHGFP_TYPE_CURRENT
, system_buffer
)))
98 cur
= FilePath(system_buffer
);
100 case base::DIR_IE_INTERNET_CACHE
:
101 if (FAILED(SHGetFolderPath(NULL
, CSIDL_INTERNET_CACHE
, NULL
,
102 SHGFP_TYPE_CURRENT
, system_buffer
)))
104 cur
= FilePath(system_buffer
);
106 case base::DIR_COMMON_START_MENU
:
107 if (FAILED(SHGetFolderPath(NULL
, CSIDL_COMMON_PROGRAMS
, NULL
,
108 SHGFP_TYPE_CURRENT
, system_buffer
)))
110 cur
= FilePath(system_buffer
);
112 case base::DIR_START_MENU
:
113 if (FAILED(SHGetFolderPath(NULL
, CSIDL_PROGRAMS
, NULL
,
114 SHGFP_TYPE_CURRENT
, system_buffer
)))
116 cur
= FilePath(system_buffer
);
118 case base::DIR_APP_DATA
:
119 if (FAILED(SHGetFolderPath(NULL
, CSIDL_APPDATA
, NULL
, SHGFP_TYPE_CURRENT
,
122 cur
= FilePath(system_buffer
);
124 case base::DIR_COMMON_APP_DATA
:
125 if (FAILED(SHGetFolderPath(NULL
, CSIDL_COMMON_APPDATA
, NULL
,
126 SHGFP_TYPE_CURRENT
, system_buffer
)))
128 cur
= FilePath(system_buffer
);
130 case base::DIR_LOCAL_APP_DATA_LOW
:
131 if (win::GetVersion() < win::VERSION_VISTA
)
134 // TODO(nsylvain): We should use SHGetKnownFolderPath instead. Bug 1281128
135 if (FAILED(SHGetFolderPath(NULL
, CSIDL_APPDATA
, NULL
, SHGFP_TYPE_CURRENT
,
138 cur
= FilePath(system_buffer
).DirName().AppendASCII("LocalLow");
140 case base::DIR_LOCAL_APP_DATA
:
141 if (FAILED(SHGetFolderPath(NULL
, CSIDL_LOCAL_APPDATA
, NULL
,
142 SHGFP_TYPE_CURRENT
, system_buffer
)))
144 cur
= FilePath(system_buffer
);
146 case base::DIR_SOURCE_ROOT
: {
147 FilePath executableDir
;
148 // On Windows, unit tests execute two levels deep from the source root.
149 // For example: chrome/{Debug|Release}/ui_tests.exe
150 PathService::Get(base::DIR_EXE
, &executableDir
);
151 cur
= executableDir
.DirName().DirName();
154 case base::DIR_APP_SHORTCUTS
: {
155 if (win::GetVersion() < win::VERSION_WIN8
)
158 base::win::ScopedCoMem
<wchar_t> path_buf
;
159 if (FAILED(SHGetKnownFolderPath(FOLDERID_ApplicationShortcuts
, 0, NULL
,
163 cur
= FilePath(string16(path_buf
));
166 case base::DIR_USER_DESKTOP
:
167 if (FAILED(SHGetFolderPath(NULL
, CSIDL_DESKTOPDIRECTORY
, NULL
,
168 SHGFP_TYPE_CURRENT
, system_buffer
))) {
171 cur
= FilePath(system_buffer
);
173 case base::DIR_COMMON_DESKTOP
:
174 if (FAILED(SHGetFolderPath(NULL
, CSIDL_COMMON_DESKTOPDIRECTORY
, NULL
,
175 SHGFP_TYPE_CURRENT
, system_buffer
))) {
178 cur
= FilePath(system_buffer
);
180 case base::DIR_USER_QUICK_LAUNCH
:
181 if (!GetQuickLaunchPath(false, &cur
))
184 case base::DIR_DEFAULT_USER_QUICK_LAUNCH
:
185 if (!GetQuickLaunchPath(true, &cur
))
188 case base::DIR_TASKBAR_PINS
:
189 if (!PathService::Get(base::DIR_USER_QUICK_LAUNCH
, &cur
))
191 cur
= cur
.AppendASCII("User Pinned");
192 cur
= cur
.AppendASCII("TaskBar");
194 case base::DIR_WINDOWS_FONTS
:
195 if (FAILED(SHGetFolderPath(
196 NULL
, CSIDL_FONTS
, NULL
, SHGFP_TYPE_CURRENT
, system_buffer
))) {
199 cur
= FilePath(system_buffer
);