Mac: More robust "window under location" for interactive_ui_tests
[chromium-blink-merge.git] / base / base_paths_win.cc
blob5bef3106aaf010f8f50029a1a580a1d3bd20865a
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.
5 #include <windows.h>
6 #include <shlobj.h>
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;
17 using base::FilePath;
19 namespace base {
21 bool PathProviderWin(int key, FilePath* result) {
22 // We need to go compute the value. It would be nice to support paths with
23 // names longer than MAX_PATH, but the system functions don't seem to be
24 // designed for it either, with the exception of GetTempPath (but other
25 // things will surely break if the temp path is too long, so we don't bother
26 // handling it.
27 wchar_t system_buffer[MAX_PATH];
28 system_buffer[0] = 0;
30 FilePath cur;
31 switch (key) {
32 case base::FILE_EXE:
33 GetModuleFileName(NULL, system_buffer, MAX_PATH);
34 cur = FilePath(system_buffer);
35 break;
36 case base::FILE_MODULE: {
37 // the resource containing module is assumed to be the one that
38 // this code lives in, whether that's a dll or exe
39 HMODULE this_module = reinterpret_cast<HMODULE>(&__ImageBase);
40 GetModuleFileName(this_module, system_buffer, MAX_PATH);
41 cur = FilePath(system_buffer);
42 break;
44 case base::DIR_WINDOWS:
45 GetWindowsDirectory(system_buffer, MAX_PATH);
46 cur = FilePath(system_buffer);
47 break;
48 case base::DIR_SYSTEM:
49 GetSystemDirectory(system_buffer, MAX_PATH);
50 cur = FilePath(system_buffer);
51 break;
52 case base::DIR_PROGRAM_FILESX86:
53 if (base::win::OSInfo::GetInstance()->architecture() !=
54 base::win::OSInfo::X86_ARCHITECTURE) {
55 if (FAILED(SHGetFolderPath(NULL, CSIDL_PROGRAM_FILESX86, NULL,
56 SHGFP_TYPE_CURRENT, system_buffer)))
57 return false;
58 cur = FilePath(system_buffer);
59 break;
61 // Fall through to base::DIR_PROGRAM_FILES if we're on an X86 machine.
62 case base::DIR_PROGRAM_FILES:
63 if (FAILED(SHGetFolderPath(NULL, CSIDL_PROGRAM_FILES, NULL,
64 SHGFP_TYPE_CURRENT, system_buffer)))
65 return false;
66 cur = FilePath(system_buffer);
67 break;
68 case base::DIR_IE_INTERNET_CACHE:
69 if (FAILED(SHGetFolderPath(NULL, CSIDL_INTERNET_CACHE, NULL,
70 SHGFP_TYPE_CURRENT, system_buffer)))
71 return false;
72 cur = FilePath(system_buffer);
73 break;
74 case base::DIR_COMMON_START_MENU:
75 if (FAILED(SHGetFolderPath(NULL, CSIDL_COMMON_PROGRAMS, NULL,
76 SHGFP_TYPE_CURRENT, system_buffer)))
77 return false;
78 cur = FilePath(system_buffer);
79 break;
80 case base::DIR_START_MENU:
81 if (FAILED(SHGetFolderPath(NULL, CSIDL_PROGRAMS, NULL,
82 SHGFP_TYPE_CURRENT, system_buffer)))
83 return false;
84 cur = FilePath(system_buffer);
85 break;
86 case base::DIR_APP_DATA:
87 if (FAILED(SHGetFolderPath(NULL, CSIDL_APPDATA, NULL, SHGFP_TYPE_CURRENT,
88 system_buffer)))
89 return false;
90 cur = FilePath(system_buffer);
91 break;
92 case base::DIR_COMMON_APP_DATA:
93 if (FAILED(SHGetFolderPath(NULL, CSIDL_COMMON_APPDATA, NULL,
94 SHGFP_TYPE_CURRENT, system_buffer)))
95 return false;
96 cur = FilePath(system_buffer);
97 break;
98 case base::DIR_LOCAL_APP_DATA:
99 if (FAILED(SHGetFolderPath(NULL, CSIDL_LOCAL_APPDATA, NULL,
100 SHGFP_TYPE_CURRENT, system_buffer)))
101 return false;
102 cur = FilePath(system_buffer);
103 break;
104 case base::DIR_SOURCE_ROOT: {
105 FilePath executableDir;
106 // On Windows, unit tests execute two levels deep from the source root.
107 // For example: chrome/{Debug|Release}/ui_tests.exe
108 PathService::Get(base::DIR_EXE, &executableDir);
109 cur = executableDir.DirName().DirName();
110 break;
112 case base::DIR_APP_SHORTCUTS: {
113 if (win::GetVersion() < win::VERSION_WIN8)
114 return false;
116 base::win::ScopedCoMem<wchar_t> path_buf;
117 if (FAILED(SHGetKnownFolderPath(FOLDERID_ApplicationShortcuts, 0, NULL,
118 &path_buf)))
119 return false;
121 cur = FilePath(string16(path_buf));
122 break;
124 case base::DIR_USER_DESKTOP:
125 if (FAILED(SHGetFolderPath(NULL, CSIDL_DESKTOPDIRECTORY, NULL,
126 SHGFP_TYPE_CURRENT, system_buffer))) {
127 return false;
129 cur = FilePath(system_buffer);
130 break;
131 case base::DIR_COMMON_DESKTOP:
132 if (FAILED(SHGetFolderPath(NULL, CSIDL_COMMON_DESKTOPDIRECTORY, NULL,
133 SHGFP_TYPE_CURRENT, system_buffer))) {
134 return false;
136 cur = FilePath(system_buffer);
137 break;
138 case base::DIR_USER_QUICK_LAUNCH:
139 if (!PathService::Get(base::DIR_APP_DATA, &cur))
140 return false;
141 // According to various sources, appending
142 // "Microsoft\Internet Explorer\Quick Launch" to %appdata% is the only
143 // reliable way to get the quick launch folder across all versions of
144 // Windows.
145 // http://stackoverflow.com/questions/76080/how-do-you-reliably-get-the-quick-
146 // http://www.microsoft.com/technet/scriptcenter/resources/qanda/sept05/hey0901.mspx
147 cur = cur.AppendASCII("Microsoft")
148 .AppendASCII("Internet Explorer")
149 .AppendASCII("Quick Launch");
150 break;
151 case base::DIR_TASKBAR_PINS:
152 if (!PathService::Get(base::DIR_USER_QUICK_LAUNCH, &cur))
153 return false;
154 cur = cur.AppendASCII("User Pinned");
155 cur = cur.AppendASCII("TaskBar");
156 break;
157 case base::DIR_WINDOWS_FONTS:
158 if (FAILED(SHGetFolderPath(
159 NULL, CSIDL_FONTS, NULL, SHGFP_TYPE_CURRENT, system_buffer))) {
160 return false;
162 cur = FilePath(system_buffer);
163 break;
164 default:
165 return false;
168 *result = cur;
169 return true;
172 } // namespace base