[M85 Dev][EFL] Fix crashes at webview launch
[platform/framework/web/chromium-efl.git] / base / base_paths_win.cc
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.
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()->wow64_status() ==
74           base::win::OSInfo::WOW64_ENABLED) {
75         std::unique_ptr<base::Environment> env(base::Environment::Create());
76         std::string programfiles_w6432;
77         // 32-bit process running in WOW64 sets ProgramW6432 environment
78         // variable. See
79         // https://msdn.microsoft.com/library/windows/desktop/aa384274.aspx.
80         if (!env->GetVar("ProgramW6432", &programfiles_w6432))
81           return false;
82         // GetVar returns UTF8 - convert back to Wide.
83         cur = FilePath(UTF8ToWide(programfiles_w6432));
84         break;
85       }
86 #endif
87       if (FAILED(SHGetFolderPath(NULL, CSIDL_PROGRAM_FILES, NULL,
88                                  SHGFP_TYPE_CURRENT, system_buffer)))
89         return false;
90       cur = FilePath(system_buffer);
91       break;
92     case base::DIR_IE_INTERNET_CACHE:
93       if (FAILED(SHGetFolderPath(NULL, CSIDL_INTERNET_CACHE, NULL,
94                                  SHGFP_TYPE_CURRENT, system_buffer)))
95         return false;
96       cur = FilePath(system_buffer);
97       break;
98     case base::DIR_COMMON_START_MENU:
99       if (FAILED(SHGetFolderPath(NULL, CSIDL_COMMON_PROGRAMS, NULL,
100                                  SHGFP_TYPE_CURRENT, system_buffer)))
101         return false;
102       cur = FilePath(system_buffer);
103       break;
104     case base::DIR_START_MENU:
105       if (FAILED(SHGetFolderPath(NULL, CSIDL_PROGRAMS, NULL, SHGFP_TYPE_CURRENT,
106                                  system_buffer)))
107         return false;
108       cur = FilePath(system_buffer);
109       break;
110     case base::DIR_COMMON_STARTUP:
111       if (FAILED(SHGetFolderPath(nullptr, CSIDL_COMMON_STARTUP, nullptr,
112                                  SHGFP_TYPE_CURRENT, system_buffer)))
113         return false;
114       cur = FilePath(system_buffer);
115       break;
116     case base::DIR_USER_STARTUP:
117       if (FAILED(SHGetFolderPath(nullptr, CSIDL_STARTUP, nullptr,
118                                  SHGFP_TYPE_CURRENT, system_buffer)))
119         return false;
120       cur = FilePath(system_buffer);
121       break;
122     case base::DIR_APP_DATA:
123       if (FAILED(SHGetFolderPath(NULL, CSIDL_APPDATA, NULL, SHGFP_TYPE_CURRENT,
124                                  system_buffer)))
125         return false;
126       cur = FilePath(system_buffer);
127       break;
128     case base::DIR_COMMON_APP_DATA:
129       if (FAILED(SHGetFolderPath(NULL, CSIDL_COMMON_APPDATA, NULL,
130                                  SHGFP_TYPE_CURRENT, system_buffer)))
131         return false;
132       cur = FilePath(system_buffer);
133       break;
134     case base::DIR_LOCAL_APP_DATA:
135       if (FAILED(SHGetFolderPath(NULL, CSIDL_LOCAL_APPDATA, NULL,
136                                  SHGFP_TYPE_CURRENT, system_buffer)))
137         return false;
138       cur = FilePath(system_buffer);
139       break;
140     case base::DIR_SOURCE_ROOT: {
141       FilePath executableDir;
142       // On Windows, unit tests execute two levels deep from the source root.
143       // For example:  chrome/{Debug|Release}/ui_tests.exe
144       PathService::Get(base::DIR_EXE, &executableDir);
145       cur = executableDir.DirName().DirName();
146       break;
147     }
148     case base::DIR_APP_SHORTCUTS: {
149       if (win::GetVersion() < win::Version::WIN8)
150         return false;
151
152       base::win::ScopedCoMem<wchar_t> path_buf;
153       if (FAILED(SHGetKnownFolderPath(FOLDERID_ApplicationShortcuts, 0, NULL,
154                                       &path_buf)))
155         return false;
156
157       cur = FilePath(path_buf.get());
158       break;
159     }
160     case base::DIR_USER_DESKTOP:
161       if (FAILED(SHGetFolderPath(NULL, CSIDL_DESKTOPDIRECTORY, NULL,
162                                  SHGFP_TYPE_CURRENT, system_buffer))) {
163         return false;
164       }
165       cur = FilePath(system_buffer);
166       break;
167     case base::DIR_COMMON_DESKTOP:
168       if (FAILED(SHGetFolderPath(NULL, CSIDL_COMMON_DESKTOPDIRECTORY, NULL,
169                                  SHGFP_TYPE_CURRENT, system_buffer))) {
170         return false;
171       }
172       cur = FilePath(system_buffer);
173       break;
174     case base::DIR_USER_QUICK_LAUNCH:
175       if (!PathService::Get(base::DIR_APP_DATA, &cur))
176         return false;
177       // According to various sources, appending
178       // "Microsoft\Internet Explorer\Quick Launch" to %appdata% is the only
179       // reliable way to get the quick launch folder across all versions of
180       // Windows.
181       // http://stackoverflow.com/questions/76080/how-do-you-reliably-get-the-quick-
182       // http://www.microsoft.com/technet/scriptcenter/resources/qanda/sept05/hey0901.mspx
183       cur = cur.Append(FILE_PATH_LITERAL("Microsoft"))
184                 .Append(FILE_PATH_LITERAL("Internet Explorer"))
185                 .Append(FILE_PATH_LITERAL("Quick Launch"));
186       break;
187     case base::DIR_TASKBAR_PINS: {
188       if (!PathService::Get(base::DIR_USER_QUICK_LAUNCH, &cur))
189         return false;
190       cur = cur.Append(FILE_PATH_LITERAL("User Pinned"))
191                 .Append(FILE_PATH_LITERAL("TaskBar"));
192       break;
193     }
194     case base::DIR_IMPLICIT_APP_SHORTCUTS:
195       if (!PathService::Get(base::DIR_USER_QUICK_LAUNCH, &cur))
196         return false;
197       cur = cur.Append(FILE_PATH_LITERAL("User Pinned"))
198                 .Append(FILE_PATH_LITERAL("ImplicitAppShortcuts"));
199       break;
200     case base::DIR_WINDOWS_FONTS:
201       if (FAILED(SHGetFolderPath(NULL, CSIDL_FONTS, NULL, SHGFP_TYPE_CURRENT,
202                                  system_buffer))) {
203         return false;
204       }
205       cur = FilePath(system_buffer);
206       break;
207     default:
208       return false;
209   }
210
211   *result = cur;
212   return true;
213 }
214
215 }  // namespace base