[M108 Migration][VD] Support set time and time zone offset
[platform/framework/web/chromium-efl.git] / base / base_paths_win.cc
1 // Copyright 2012 The Chromium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include <windows.h>
6 #include <KnownFolders.h>
7 #include <shlobj.h>
8
9 #include "base/base_paths.h"
10 #include "base/environment.h"
11 #include "base/files/file_path.h"
12 #include "base/path_service.h"
13 #include "base/strings/string_util.h"
14 #include "base/strings/utf_string_conversions.h"
15 #include "base/win/current_module.h"
16 #include "base/win/scoped_co_mem.h"
17 #include "base/win/windows_version.h"
18
19 using base::FilePath;
20
21 namespace base {
22
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
28   // handling it.
29   wchar_t system_buffer[MAX_PATH];
30   system_buffer[0] = 0;
31
32   FilePath cur;
33   switch (key) {
34     case base::FILE_EXE:
35       if (GetModuleFileName(NULL, system_buffer, MAX_PATH) == 0)
36         return false;
37       cur = FilePath(system_buffer);
38       break;
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       if (GetModuleFileName(CURRENT_MODULE(), system_buffer, MAX_PATH) == 0)
43         return false;
44       cur = FilePath(system_buffer);
45       break;
46     }
47     case base::DIR_WINDOWS:
48       GetWindowsDirectory(system_buffer, MAX_PATH);
49       cur = FilePath(system_buffer);
50       break;
51     case base::DIR_SYSTEM:
52       GetSystemDirectory(system_buffer, MAX_PATH);
53       cur = FilePath(system_buffer);
54       break;
55     case base::DIR_PROGRAM_FILESX86:
56       if (win::OSInfo::GetArchitecture() != win::OSInfo::X86_ARCHITECTURE) {
57         if (FAILED(SHGetFolderPath(NULL, CSIDL_PROGRAM_FILESX86, NULL,
58                                    SHGFP_TYPE_CURRENT, system_buffer)))
59           return false;
60         cur = FilePath(system_buffer);
61         break;
62       }
63       // Fall through to base::DIR_PROGRAM_FILES if we're on an X86 machine.
64       [[fallthrough]];
65     case base::DIR_PROGRAM_FILES:
66       if (FAILED(SHGetFolderPath(NULL, CSIDL_PROGRAM_FILES, NULL,
67                                  SHGFP_TYPE_CURRENT, system_buffer)))
68         return false;
69       cur = FilePath(system_buffer);
70       break;
71     case base::DIR_PROGRAM_FILES6432:
72 #if !defined(_WIN64)
73       if (base::win::OSInfo::GetInstance()->IsWowX86OnAMD64()) {
74         std::unique_ptr<base::Environment> env(base::Environment::Create());
75         std::string programfiles_w6432;
76         // 32-bit process running in WOW64 sets ProgramW6432 environment
77         // variable. See
78         // https://msdn.microsoft.com/library/windows/desktop/aa384274.aspx.
79         if (!env->GetVar("ProgramW6432", &programfiles_w6432))
80           return false;
81         // GetVar returns UTF8 - convert back to Wide.
82         cur = FilePath(UTF8ToWide(programfiles_w6432));
83         break;
84       }
85 #endif
86       if (FAILED(SHGetFolderPath(NULL, CSIDL_PROGRAM_FILES, NULL,
87                                  SHGFP_TYPE_CURRENT, system_buffer)))
88         return false;
89       cur = FilePath(system_buffer);
90       break;
91     case base::DIR_IE_INTERNET_CACHE:
92       if (FAILED(SHGetFolderPath(NULL, CSIDL_INTERNET_CACHE, NULL,
93                                  SHGFP_TYPE_CURRENT, system_buffer)))
94         return false;
95       cur = FilePath(system_buffer);
96       break;
97     case base::DIR_COMMON_START_MENU:
98       if (FAILED(SHGetFolderPath(NULL, CSIDL_COMMON_PROGRAMS, NULL,
99                                  SHGFP_TYPE_CURRENT, system_buffer)))
100         return false;
101       cur = FilePath(system_buffer);
102       break;
103     case base::DIR_START_MENU:
104       if (FAILED(SHGetFolderPath(NULL, CSIDL_PROGRAMS, NULL, SHGFP_TYPE_CURRENT,
105                                  system_buffer)))
106         return false;
107       cur = FilePath(system_buffer);
108       break;
109     case base::DIR_COMMON_STARTUP:
110       if (FAILED(SHGetFolderPath(nullptr, CSIDL_COMMON_STARTUP, nullptr,
111                                  SHGFP_TYPE_CURRENT, system_buffer)))
112         return false;
113       cur = FilePath(system_buffer);
114       break;
115     case base::DIR_USER_STARTUP:
116       if (FAILED(SHGetFolderPath(nullptr, CSIDL_STARTUP, nullptr,
117                                  SHGFP_TYPE_CURRENT, system_buffer)))
118         return false;
119       cur = FilePath(system_buffer);
120       break;
121     case base::DIR_ROAMING_APP_DATA:
122       if (FAILED(SHGetFolderPath(NULL, CSIDL_APPDATA, NULL, SHGFP_TYPE_CURRENT,
123                                  system_buffer)))
124         return false;
125       cur = FilePath(system_buffer);
126       break;
127     case base::DIR_COMMON_APP_DATA:
128       if (FAILED(SHGetFolderPath(NULL, CSIDL_COMMON_APPDATA, NULL,
129                                  SHGFP_TYPE_CURRENT, system_buffer)))
130         return false;
131       cur = FilePath(system_buffer);
132       break;
133     case base::DIR_LOCAL_APP_DATA:
134       if (FAILED(SHGetFolderPath(NULL, CSIDL_LOCAL_APPDATA, NULL,
135                                  SHGFP_TYPE_CURRENT, system_buffer)))
136         return false;
137       cur = FilePath(system_buffer);
138       break;
139     case base::DIR_SRC_TEST_DATA_ROOT: {
140       FilePath executableDir;
141       // On Windows, unit tests execute two levels deep from the source root.
142       // For example:  chrome/{Debug|Release}/ui_tests.exe
143       PathService::Get(base::DIR_EXE, &executableDir);
144       cur = executableDir.DirName().DirName();
145       break;
146     }
147     case base::DIR_APP_SHORTCUTS: {
148       if (win::GetVersion() < win::Version::WIN8)
149         return false;
150
151       base::win::ScopedCoMem<wchar_t> path_buf;
152       if (FAILED(SHGetKnownFolderPath(FOLDERID_ApplicationShortcuts, 0, NULL,
153                                       &path_buf)))
154         return false;
155
156       cur = FilePath(path_buf.get());
157       break;
158     }
159     case base::DIR_USER_DESKTOP:
160       if (FAILED(SHGetFolderPath(NULL, CSIDL_DESKTOPDIRECTORY, NULL,
161                                  SHGFP_TYPE_CURRENT, system_buffer))) {
162         return false;
163       }
164       cur = FilePath(system_buffer);
165       break;
166     case base::DIR_COMMON_DESKTOP:
167       if (FAILED(SHGetFolderPath(NULL, CSIDL_COMMON_DESKTOPDIRECTORY, NULL,
168                                  SHGFP_TYPE_CURRENT, system_buffer))) {
169         return false;
170       }
171       cur = FilePath(system_buffer);
172       break;
173     case base::DIR_USER_QUICK_LAUNCH:
174       if (!PathService::Get(base::DIR_ROAMING_APP_DATA, &cur))
175         return false;
176       // According to various sources, appending
177       // "Microsoft\Internet Explorer\Quick Launch" to %appdata% is the only
178       // reliable way to get the quick launch folder across all versions of
179       // Windows.
180       // http://stackoverflow.com/questions/76080/how-do-you-reliably-get-the-quick-
181       // http://www.microsoft.com/technet/scriptcenter/resources/qanda/sept05/hey0901.mspx
182       cur = cur.Append(FILE_PATH_LITERAL("Microsoft"))
183                 .Append(FILE_PATH_LITERAL("Internet Explorer"))
184                 .Append(FILE_PATH_LITERAL("Quick Launch"));
185       break;
186     case base::DIR_TASKBAR_PINS: {
187       if (!PathService::Get(base::DIR_USER_QUICK_LAUNCH, &cur))
188         return false;
189       cur = cur.Append(FILE_PATH_LITERAL("User Pinned"))
190                 .Append(FILE_PATH_LITERAL("TaskBar"));
191       break;
192     }
193     case base::DIR_IMPLICIT_APP_SHORTCUTS:
194       if (!PathService::Get(base::DIR_USER_QUICK_LAUNCH, &cur))
195         return false;
196       cur = cur.Append(FILE_PATH_LITERAL("User Pinned"))
197                 .Append(FILE_PATH_LITERAL("ImplicitAppShortcuts"));
198       break;
199     case base::DIR_WINDOWS_FONTS:
200       if (FAILED(SHGetFolderPath(NULL, CSIDL_FONTS, NULL, SHGFP_TYPE_CURRENT,
201                                  system_buffer))) {
202         return false;
203       }
204       cur = FilePath(system_buffer);
205       break;
206     default:
207       return false;
208   }
209
210   *result = cur;
211   return true;
212 }
213
214 }  // namespace base