[M120 Migration]Fix for crash during chrome exit
[platform/framework/web/chromium-efl.git] / chrome / browser / platform_util_win.cc
1 // Copyright 2011 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 "chrome/browser/platform_util.h"
6
7 #include <windows.h>  // Must be in front of other Windows header files.
8
9 #include <commdlg.h>
10 #include <dwmapi.h>
11 #include <shellapi.h>
12 #include <shlobj.h>
13 #include <stddef.h>
14 #include <wrl/client.h>
15
16 #include "base/base_paths.h"
17 #include "base/files/file_path.h"
18 #include "base/files/file_util.h"
19 #include "base/functional/bind.h"
20 #include "base/logging.h"
21 #include "base/path_service.h"
22 #include "base/strings/string_util.h"
23 #include "base/strings/utf_string_conversions.h"
24 #include "base/task/thread_pool.h"
25 #include "base/threading/scoped_blocking_call.h"
26 #include "base/win/registry.h"
27 #include "base/win/scoped_co_mem.h"
28 #include "chrome/browser/platform_util_internal.h"
29 #include "content/public/browser/browser_thread.h"
30 #include "ui/base/win/shell.h"
31 #include "ui/gfx/native_widget_types.h"
32 #include "url/gurl.h"
33
34 using content::BrowserThread;
35
36 namespace platform_util {
37
38 namespace {
39
40 void ShowItemInFolderOnWorkerThread(const base::FilePath& full_path) {
41   base::ScopedBlockingCall scoped_blocking_call(FROM_HERE,
42                                                 base::BlockingType::MAY_BLOCK);
43   base::FilePath dir = full_path.DirName().AsEndingWithSeparator();
44   // ParseDisplayName will fail if the directory is "C:", it must be "C:\\".
45   if (dir.empty())
46     return;
47
48   Microsoft::WRL::ComPtr<IShellFolder> desktop;
49   HRESULT hr = SHGetDesktopFolder(&desktop);
50   if (FAILED(hr))
51     return;
52
53   base::win::ScopedCoMem<ITEMIDLIST> dir_item;
54   hr = desktop->ParseDisplayName(NULL, NULL,
55                                  const_cast<wchar_t *>(dir.value().c_str()),
56                                  NULL, &dir_item, NULL);
57   if (FAILED(hr))
58     return;
59
60   base::win::ScopedCoMem<ITEMIDLIST> file_item;
61   hr = desktop->ParseDisplayName(NULL, NULL,
62       const_cast<wchar_t *>(full_path.value().c_str()),
63       NULL, &file_item, NULL);
64   if (FAILED(hr))
65     return;
66
67   const ITEMIDLIST* highlight[] = {file_item};
68
69   // Skip opening the folder during browser tests, to avoid leaving an open
70   // file explorer window behind.
71   if (!platform_util::internal::AreShellOperationsAllowed())
72     return;
73
74   hr = SHOpenFolderAndSelectItems(dir_item, std::size(highlight), highlight, 0);
75   if (FAILED(hr)) {
76     // On some systems, the above call mysteriously fails with "file not
77     // found" even though the file is there.  In these cases, ShellExecute()
78     // seems to work as a fallback (although it won't select the file).
79     if (hr == ERROR_FILE_NOT_FOUND) {
80       ShellExecute(NULL, L"open", dir.value().c_str(), NULL, NULL, SW_SHOW);
81     } else {
82       LOG(WARNING) << " " << __func__ << "(): Can't open full_path = \""
83                    << full_path.value() << "\""
84                    << " hr = " << logging::SystemErrorCodeToString(hr);
85     }
86   }
87 }
88
89 void OpenExternalOnWorkerThread(const GURL& url) {
90   base::ScopedBlockingCall scoped_blocking_call(FROM_HERE,
91                                                 base::BlockingType::MAY_BLOCK);
92   // Quote the input scheme to be sure that the command does not have
93   // parameters unexpected by the external program. This url should already
94   // have been escaped.
95   std::string escaped_url = url.spec();
96   escaped_url.insert(0, "\"");
97   escaped_url += "\"";
98
99   // According to Mozilla in uriloader/exthandler/win/nsOSHelperAppService.cpp:
100   // "Some versions of windows (Win2k before SP3, Win XP before SP1) crash in
101   // ShellExecute on long URLs (bug 161357 on bugzilla.mozilla.org). IE 5 and 6
102   // support URLS of 2083 chars in length, 2K is safe."
103   //
104   // It may be possible to increase this. https://crbug.com/727909
105   const size_t kMaxUrlLength = 2048;
106   if (escaped_url.length() > kMaxUrlLength)
107     return;
108
109   // Specify %windir%\system32 as the CWD so that any new proc spawned does not
110   // inherit this proc's CWD. Without this, uninstalls may be broken by a
111   // long-lived child proc that holds a handle to the browser's version
112   // directory (the browser's CWD). A process's CWD is in the standard list of
113   // directories to search when loading a DLL, and precedes the system directory
114   // when safe DLL search mode is disabled (not the default). Setting the CWD to
115   // the system directory is a nice way to mitigate a potential DLL search order
116   // hijack for processes that don't implement their own mitigation.
117   base::FilePath system_dir;
118   base::PathService::Get(base::DIR_SYSTEM, &system_dir);
119   if (reinterpret_cast<ULONG_PTR>(ShellExecuteA(
120           NULL, "open", escaped_url.c_str(), NULL,
121           system_dir.AsUTF8Unsafe().c_str(), SW_SHOWNORMAL)) <= 32) {
122     // On failure, it may be good to display a message to the user.
123     // https://crbug.com/727913
124     return;
125   }
126 }
127
128 }  // namespace
129
130 void ShowItemInFolder(Profile* profile, const base::FilePath& full_path) {
131   base::ThreadPool::CreateCOMSTATaskRunner(
132       {base::MayBlock(), base::TaskPriority::USER_BLOCKING})
133       ->PostTask(FROM_HERE,
134                  base::BindOnce(&ShowItemInFolderOnWorkerThread, full_path));
135 }
136
137 namespace internal {
138
139 void PlatformOpenVerifiedItem(const base::FilePath& path, OpenItemType type) {
140   // May result in an interactive dialog.
141   base::ScopedBlockingCall scoped_blocking_call(FROM_HERE,
142                                                 base::BlockingType::MAY_BLOCK);
143   switch (type) {
144     case OPEN_FILE:
145       ui::win::OpenFileViaShell(path);
146       break;
147
148     case OPEN_FOLDER:
149       ui::win::OpenFolderViaShell(path);
150       break;
151   }
152 }
153
154 }  // namespace internal
155
156 void OpenExternal(const GURL& url) {
157   DCHECK_CURRENTLY_ON(BrowserThread::UI);
158
159   base::ThreadPool::CreateCOMSTATaskRunner(
160       {base::MayBlock(), base::TaskPriority::USER_BLOCKING})
161       ->PostTask(FROM_HERE, base::BindOnce(&OpenExternalOnWorkerThread, url));
162 }
163
164 }  // namespace platform_util