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_PROFILE
:
131 if (FAILED(SHGetFolderPath(NULL
, CSIDL_PROFILE
, NULL
, SHGFP_TYPE_CURRENT
,
134 cur
= FilePath(system_buffer
);
136 case base::DIR_LOCAL_APP_DATA_LOW
:
137 if (win::GetVersion() < win::VERSION_VISTA
)
140 // TODO(nsylvain): We should use SHGetKnownFolderPath instead. Bug 1281128
141 if (FAILED(SHGetFolderPath(NULL
, CSIDL_APPDATA
, NULL
, SHGFP_TYPE_CURRENT
,
144 cur
= FilePath(system_buffer
).DirName().AppendASCII("LocalLow");
146 case base::DIR_LOCAL_APP_DATA
:
147 if (FAILED(SHGetFolderPath(NULL
, CSIDL_LOCAL_APPDATA
, NULL
,
148 SHGFP_TYPE_CURRENT
, system_buffer
)))
150 cur
= FilePath(system_buffer
);
152 case base::DIR_SOURCE_ROOT
: {
153 FilePath executableDir
;
154 // On Windows, unit tests execute two levels deep from the source root.
155 // For example: chrome/{Debug|Release}/ui_tests.exe
156 PathService::Get(base::DIR_EXE
, &executableDir
);
157 cur
= executableDir
.DirName().DirName();
160 case base::DIR_APP_SHORTCUTS
: {
161 if (win::GetVersion() < win::VERSION_WIN8
)
164 base::win::ScopedCoMem
<wchar_t> path_buf
;
165 if (FAILED(SHGetKnownFolderPath(FOLDERID_ApplicationShortcuts
, 0, NULL
,
169 cur
= FilePath(string16(path_buf
));
172 case base::DIR_USER_DESKTOP
:
173 if (FAILED(SHGetFolderPath(NULL
, CSIDL_DESKTOPDIRECTORY
, NULL
,
174 SHGFP_TYPE_CURRENT
, system_buffer
))) {
177 cur
= FilePath(system_buffer
);
179 case base::DIR_COMMON_DESKTOP
:
180 if (FAILED(SHGetFolderPath(NULL
, CSIDL_COMMON_DESKTOPDIRECTORY
, NULL
,
181 SHGFP_TYPE_CURRENT
, system_buffer
))) {
184 cur
= FilePath(system_buffer
);
186 case base::DIR_USER_QUICK_LAUNCH
:
187 if (!GetQuickLaunchPath(false, &cur
))
190 case base::DIR_DEFAULT_USER_QUICK_LAUNCH
:
191 if (!GetQuickLaunchPath(true, &cur
))
194 case base::DIR_TASKBAR_PINS
:
195 if (!PathService::Get(base::DIR_USER_QUICK_LAUNCH
, &cur
))
197 cur
= cur
.AppendASCII("User Pinned");
198 cur
= cur
.AppendASCII("TaskBar");