Limit App Host installation / usage for platform apps to Google Chome only.
[chromium-blink-merge.git] / base / base_paths_win.cc
bloba06d9084550436966e99f485b922d7a144c5609a
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.
6 #include <windows.h>
7 #include <shlobj.h>
9 #include "base/base_paths.h"
10 #include "base/file_path.h"
11 #include "base/file_util.h"
12 #include "base/path_service.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;
19 namespace {
21 bool GetQuickLaunchPath(bool default_user, FilePath* result) {
22 if (default_user) {
23 wchar_t system_buffer[MAX_PATH];
24 system_buffer[0] = 0;
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,
29 system_buffer))) {
30 return false;
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
35 // PathService cache.
36 return false;
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");
46 return true;
49 } // namespace
51 namespace base {
53 bool PathProviderWin(int key, FilePath* result) {
55 // We need to go compute the value. It would be nice to support paths with
56 // names longer than MAX_PATH, but the system functions don't seem to be
57 // designed for it either, with the exception of GetTempPath (but other
58 // things will surely break if the temp path is too long, so we don't bother
59 // handling it.
60 wchar_t system_buffer[MAX_PATH];
61 system_buffer[0] = 0;
63 FilePath cur;
64 switch (key) {
65 case base::FILE_EXE:
66 GetModuleFileName(NULL, system_buffer, MAX_PATH);
67 cur = FilePath(system_buffer);
68 break;
69 case base::FILE_MODULE: {
70 // the resource containing module is assumed to be the one that
71 // this code lives in, whether that's a dll or exe
72 HMODULE this_module = reinterpret_cast<HMODULE>(&__ImageBase);
73 GetModuleFileName(this_module, system_buffer, MAX_PATH);
74 cur = FilePath(system_buffer);
75 break;
77 case base::DIR_WINDOWS:
78 GetWindowsDirectory(system_buffer, MAX_PATH);
79 cur = FilePath(system_buffer);
80 break;
81 case base::DIR_SYSTEM:
82 GetSystemDirectory(system_buffer, MAX_PATH);
83 cur = FilePath(system_buffer);
84 break;
85 case base::DIR_PROGRAM_FILESX86:
86 if (base::win::OSInfo::GetInstance()->architecture() !=
87 base::win::OSInfo::X86_ARCHITECTURE) {
88 if (FAILED(SHGetFolderPath(NULL, CSIDL_PROGRAM_FILESX86, NULL,
89 SHGFP_TYPE_CURRENT, system_buffer)))
90 return false;
91 cur = FilePath(system_buffer);
92 break;
94 // Fall through to base::DIR_PROGRAM_FILES if we're on an X86 machine.
95 case base::DIR_PROGRAM_FILES:
96 if (FAILED(SHGetFolderPath(NULL, CSIDL_PROGRAM_FILES, NULL,
97 SHGFP_TYPE_CURRENT, system_buffer)))
98 return false;
99 cur = FilePath(system_buffer);
100 break;
101 case base::DIR_IE_INTERNET_CACHE:
102 if (FAILED(SHGetFolderPath(NULL, CSIDL_INTERNET_CACHE, NULL,
103 SHGFP_TYPE_CURRENT, system_buffer)))
104 return false;
105 cur = FilePath(system_buffer);
106 break;
107 case base::DIR_COMMON_START_MENU:
108 if (FAILED(SHGetFolderPath(NULL, CSIDL_COMMON_PROGRAMS, NULL,
109 SHGFP_TYPE_CURRENT, system_buffer)))
110 return false;
111 cur = FilePath(system_buffer);
112 break;
113 case base::DIR_START_MENU:
114 if (FAILED(SHGetFolderPath(NULL, CSIDL_PROGRAMS, NULL,
115 SHGFP_TYPE_CURRENT, system_buffer)))
116 return false;
117 cur = FilePath(system_buffer);
118 break;
119 case base::DIR_APP_DATA:
120 if (FAILED(SHGetFolderPath(NULL, CSIDL_APPDATA, NULL, SHGFP_TYPE_CURRENT,
121 system_buffer)))
122 return false;
123 cur = FilePath(system_buffer);
124 break;
125 case base::DIR_COMMON_APP_DATA:
126 if (FAILED(SHGetFolderPath(NULL, CSIDL_COMMON_APPDATA, NULL,
127 SHGFP_TYPE_CURRENT, system_buffer)))
128 return false;
129 cur = FilePath(system_buffer);
130 break;
131 case base::DIR_PROFILE:
132 if (FAILED(SHGetFolderPath(NULL, CSIDL_PROFILE, NULL, SHGFP_TYPE_CURRENT,
133 system_buffer)))
134 return false;
135 cur = FilePath(system_buffer);
136 break;
137 case base::DIR_LOCAL_APP_DATA_LOW:
138 if (win::GetVersion() < win::VERSION_VISTA)
139 return false;
141 // TODO(nsylvain): We should use SHGetKnownFolderPath instead. Bug 1281128
142 if (FAILED(SHGetFolderPath(NULL, CSIDL_APPDATA, NULL, SHGFP_TYPE_CURRENT,
143 system_buffer)))
144 return false;
145 cur = FilePath(system_buffer).DirName().AppendASCII("LocalLow");
146 break;
147 case base::DIR_LOCAL_APP_DATA:
148 if (FAILED(SHGetFolderPath(NULL, CSIDL_LOCAL_APPDATA, NULL,
149 SHGFP_TYPE_CURRENT, system_buffer)))
150 return false;
151 cur = FilePath(system_buffer);
152 break;
153 case base::DIR_SOURCE_ROOT: {
154 FilePath executableDir;
155 // On Windows, unit tests execute two levels deep from the source root.
156 // For example: chrome/{Debug|Release}/ui_tests.exe
157 PathService::Get(base::DIR_EXE, &executableDir);
158 cur = executableDir.DirName().DirName();
159 break;
161 case base::DIR_APP_SHORTCUTS: {
162 if (win::GetVersion() < win::VERSION_WIN8)
163 return false;
165 base::win::ScopedCoMem<wchar_t> path_buf;
166 if (FAILED(SHGetKnownFolderPath(FOLDERID_ApplicationShortcuts, 0, NULL,
167 &path_buf)))
168 return false;
170 cur = FilePath(string16(path_buf));
171 break;
173 case base::DIR_USER_DESKTOP:
174 if (FAILED(SHGetFolderPath(NULL, CSIDL_DESKTOPDIRECTORY, NULL,
175 SHGFP_TYPE_CURRENT, system_buffer))) {
176 return false;
178 cur = FilePath(system_buffer);
179 break;
180 case base::DIR_COMMON_DESKTOP:
181 if (FAILED(SHGetFolderPath(NULL, CSIDL_COMMON_DESKTOPDIRECTORY, NULL,
182 SHGFP_TYPE_CURRENT, system_buffer))) {
183 return false;
185 cur = FilePath(system_buffer);
186 break;
187 case base::DIR_USER_QUICK_LAUNCH:
188 if (!GetQuickLaunchPath(false, &cur))
189 return false;
190 break;
191 case base::DIR_DEFAULT_USER_QUICK_LAUNCH:
192 if (!GetQuickLaunchPath(true, &cur))
193 return false;
194 break;
195 case base::DIR_TASKBAR_PINS:
196 if (!PathService::Get(base::DIR_USER_QUICK_LAUNCH, &cur))
197 return false;
198 cur = cur.AppendASCII("User Pinned");
199 cur = cur.AppendASCII("TaskBar");
200 break;
201 default:
202 return false;
205 *result = cur;
206 return true;
209 } // namespace base