1 // Copyright (c) 2011 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 "xwalk/runtime/browser/runtime_platform_util.h"
12 #include "base/bind.h"
13 #include "base/bind_helpers.h"
14 #include "base/file_util.h"
15 #include "base/files/file_path.h"
16 #include "base/logging.h"
17 #include "base/strings/string_util.h"
18 #include "base/strings/utf_string_conversions.h"
19 #include "base/win/registry.h"
20 #include "base/win/scoped_co_mem.h"
21 #include "base/win/scoped_comptr.h"
22 #include "base/win/windows_version.h"
23 #include "content/public/browser/browser_thread.h"
24 #include "ui/base/win/shell.h"
25 #include "ui/gfx/native_widget_types.h"
28 using content::BrowserThread;
32 void ShowItemInFolderOnFileThread(const base::FilePath& full_path) {
33 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE));
34 base::FilePath dir = full_path.DirName().AsEndingWithSeparator();
35 // ParseDisplayName will fail if the directory is "C:", it must be "C:\\".
39 typedef HRESULT (WINAPI *SHOpenFolderAndSelectItemsFuncPtr)(
40 PCIDLIST_ABSOLUTE pidl_Folder,
42 PCUITEMID_CHILD_ARRAY pidls,
45 static SHOpenFolderAndSelectItemsFuncPtr open_folder_and_select_itemsPtr =
47 static bool initialize_open_folder_proc = true;
48 if (initialize_open_folder_proc) {
49 initialize_open_folder_proc = false;
50 // The SHOpenFolderAndSelectItems API is exposed by shell32 version 6
51 // and does not exist in Win2K. We attempt to retrieve this function export
52 // from shell32 and if it does not exist, we just invoke ShellExecute to
53 // open the folder thus losing the functionality to select the item in
55 HMODULE shell32_base = GetModuleHandle(L"shell32.dll");
57 NOTREACHED() << " " << __FUNCTION__ << "(): Can't open shell32.dll";
60 open_folder_and_select_itemsPtr =
61 reinterpret_cast<SHOpenFolderAndSelectItemsFuncPtr>
62 (GetProcAddress(shell32_base, "SHOpenFolderAndSelectItems"));
64 if (!open_folder_and_select_itemsPtr) {
65 ShellExecute(NULL, L"open", dir.value().c_str(), NULL, NULL, SW_SHOW);
69 base::win::ScopedComPtr<IShellFolder> desktop;
70 HRESULT hr = SHGetDesktopFolder(desktop.Receive());
74 base::win::ScopedCoMem<ITEMIDLIST> dir_item;
75 hr = desktop->ParseDisplayName(NULL, NULL,
76 const_cast<wchar_t *>(dir.value().c_str()),
77 NULL, &dir_item, NULL);
81 base::win::ScopedCoMem<ITEMIDLIST> file_item;
82 hr = desktop->ParseDisplayName(NULL, NULL,
83 const_cast<wchar_t *>(full_path.value().c_str()),
84 NULL, &file_item, NULL);
88 const ITEMIDLIST* highlight[] = {
92 hr = (*open_folder_and_select_itemsPtr)(dir_item, arraysize(highlight),
96 // On some systems, the above call mysteriously fails with "file not
97 // found" even though the file is there. In these cases, ShellExecute()
98 // seems to work as a fallback (although it won't select the file).
99 if (hr == ERROR_FILE_NOT_FOUND) {
100 ShellExecute(NULL, L"open", dir.value().c_str(), NULL, NULL, SW_SHOW);
102 LPTSTR message = NULL;
103 DWORD message_length = FormatMessage(
104 FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM,
105 0, hr, 0, reinterpret_cast<LPTSTR>(&message), 0, NULL);
106 LOG(WARNING) << " " << __FUNCTION__
107 << "(): Can't open full_path = \""
108 << full_path.value() << "\""
110 << " " << reinterpret_cast<LPTSTR>(&message);
117 // Old ShellExecute crashes the process when the command for a given scheme
118 // is empty. This function tells if it is.
119 bool ValidateShellCommandForScheme(const std::string& scheme) {
120 base::win::RegKey key;
121 std::wstring registry_path = base::ASCIIToWide(scheme) +
122 L"\\shell\\open\\command";
123 key.Open(HKEY_CLASSES_ROOT, registry_path.c_str(), KEY_READ);
127 key.ReadValue(NULL, NULL, &size, NULL);
135 namespace platform_util {
137 void ShowItemInFolder(const base::FilePath& full_path) {
138 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
139 BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE,
140 base::Bind(&ShowItemInFolderOnFileThread, full_path));
143 void OpenItem(const base::FilePath& full_path) {
144 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
145 BrowserThread::PostTask(
146 BrowserThread::FILE, FROM_HERE,
147 base::Bind(base::IgnoreResult(&ui::win::OpenItemViaShell), full_path));
150 void OpenExternal(const GURL& url) {
151 // Quote the input scheme to be sure that the command does not have
152 // parameters unexpected by the external program. This url should already
153 // have been escaped.
154 std::string escaped_url = url.spec();
155 escaped_url.insert(0, "\"");
158 // According to Mozilla in uriloader/exthandler/win/nsOSHelperAppService.cpp:
159 // "Some versions of windows (Win2k before SP3, Win XP before SP1) crash in
160 // ShellExecute on long URLs (bug 161357 on bugzilla.mozilla.org). IE 5 and 6
161 // support URLS of 2083 chars in length, 2K is safe."
162 const size_t kMaxUrlLength = 2048;
163 if (escaped_url.length() > kMaxUrlLength) {
168 if (base::win::GetVersion() < base::win::VERSION_WIN7) {
169 if (!ValidateShellCommandForScheme(url.scheme()))
173 if (reinterpret_cast<ULONG_PTR>(ShellExecuteA(NULL, "open",
174 escaped_url.c_str(), NULL, NULL,
175 SW_SHOWNORMAL)) <= 32) {
176 // We fail to execute the call. We could display a message to the user.
177 // TODO(nsylvain): we should also add a dialog to warn on errors. See
183 #if !defined(USE_AURA)
184 gfx::NativeWindow GetTopLevel(gfx::NativeView view) {
185 return ::GetAncestor(view, GA_ROOT);
188 gfx::NativeView GetParent(gfx::NativeView view) {
189 return ::GetParent(view);
192 bool IsWindowActive(gfx::NativeWindow window) {
193 return ::GetForegroundWindow() == window;
196 void ActivateWindow(gfx::NativeWindow window) {
197 ::SetForegroundWindow(window);
200 bool IsVisible(gfx::NativeView view) {
201 // MSVC complains if we don't include != 0.
202 return ::IsWindowVisible(view) != 0;
206 } // namespace platform_util