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 "chrome/browser/platform_util.h"
12 #include "base/bind.h"
13 #include "base/bind_helpers.h"
14 #include "base/files/file_path.h"
15 #include "base/logging.h"
16 #include "base/strings/string_util.h"
17 #include "base/strings/utf_string_conversions.h"
18 #include "base/win/registry.h"
19 #include "base/win/scoped_co_mem.h"
20 #include "base/win/scoped_comptr.h"
21 #include "base/win/windows_version.h"
22 #include "content/public/browser/browser_thread.h"
23 #include "ui/base/win/shell.h"
24 #include "ui/gfx/native_widget_types.h"
27 using content::BrowserThread;
31 void ShowItemInFolderOnFileThread(const base::FilePath& full_path) {
32 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE));
33 base::FilePath dir = full_path.DirName().AsEndingWithSeparator();
34 // ParseDisplayName will fail if the directory is "C:", it must be "C:\\".
38 typedef HRESULT (WINAPI *SHOpenFolderAndSelectItemsFuncPtr)(
39 PCIDLIST_ABSOLUTE pidl_Folder,
41 PCUITEMID_CHILD_ARRAY pidls,
44 static SHOpenFolderAndSelectItemsFuncPtr open_folder_and_select_itemsPtr =
46 static bool initialize_open_folder_proc = true;
47 if (initialize_open_folder_proc) {
48 initialize_open_folder_proc = false;
49 // The SHOpenFolderAndSelectItems API is exposed by shell32 version 6
50 // and does not exist in Win2K. We attempt to retrieve this function export
51 // from shell32 and if it does not exist, we just invoke ShellExecute to
52 // open the folder thus losing the functionality to select the item in
54 HMODULE shell32_base = GetModuleHandle(L"shell32.dll");
56 NOTREACHED() << " " << __FUNCTION__ << "(): Can't open shell32.dll";
59 open_folder_and_select_itemsPtr =
60 reinterpret_cast<SHOpenFolderAndSelectItemsFuncPtr>
61 (GetProcAddress(shell32_base, "SHOpenFolderAndSelectItems"));
63 if (!open_folder_and_select_itemsPtr) {
64 ShellExecute(NULL, L"open", dir.value().c_str(), NULL, NULL, SW_SHOW);
68 base::win::ScopedComPtr<IShellFolder> desktop;
69 HRESULT hr = SHGetDesktopFolder(desktop.Receive());
73 base::win::ScopedCoMem<ITEMIDLIST> dir_item;
74 hr = desktop->ParseDisplayName(NULL, NULL,
75 const_cast<wchar_t *>(dir.value().c_str()),
76 NULL, &dir_item, NULL);
80 base::win::ScopedCoMem<ITEMIDLIST> file_item;
81 hr = desktop->ParseDisplayName(NULL, NULL,
82 const_cast<wchar_t *>(full_path.value().c_str()),
83 NULL, &file_item, NULL);
87 const ITEMIDLIST* highlight[] = { file_item };
89 hr = (*open_folder_and_select_itemsPtr)(dir_item, arraysize(highlight),
93 // On some systems, the above call mysteriously fails with "file not
94 // found" even though the file is there. In these cases, ShellExecute()
95 // seems to work as a fallback (although it won't select the file).
96 if (hr == ERROR_FILE_NOT_FOUND) {
97 ShellExecute(NULL, L"open", dir.value().c_str(), NULL, NULL, SW_SHOW);
99 LPTSTR message = NULL;
100 DWORD message_length = FormatMessage(
101 FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM,
102 0, hr, 0, reinterpret_cast<LPTSTR>(&message), 0, NULL);
103 LOG(WARNING) << " " << __FUNCTION__
104 << "(): Can't open full_path = \""
105 << full_path.value() << "\""
107 << " " << reinterpret_cast<LPTSTR>(&message);
114 // Old ShellExecute crashes the process when the command for a given scheme
115 // is empty. This function tells if it is.
116 bool ValidateShellCommandForScheme(const std::string& scheme) {
117 base::win::RegKey key;
118 std::wstring registry_path = ASCIIToWide(scheme) +
119 L"\\shell\\open\\command";
120 key.Open(HKEY_CLASSES_ROOT, registry_path.c_str(), KEY_READ);
124 key.ReadValue(NULL, NULL, &size, NULL);
132 namespace platform_util {
134 void ShowItemInFolder(const base::FilePath& full_path) {
135 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
136 BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE,
137 base::Bind(&ShowItemInFolderOnFileThread, full_path));
140 void OpenItem(const base::FilePath& full_path) {
141 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
142 BrowserThread::PostTask(
143 BrowserThread::FILE, FROM_HERE,
144 base::Bind(base::IgnoreResult(&ui::win::OpenItemViaShell), full_path));
147 void OpenExternal(const GURL& url) {
148 // Quote the input scheme to be sure that the command does not have
149 // parameters unexpected by the external program. This url should already
150 // have been escaped.
151 std::string escaped_url = url.spec();
152 escaped_url.insert(0, "\"");
155 // According to Mozilla in uriloader/exthandler/win/nsOSHelperAppService.cpp:
156 // "Some versions of windows (Win2k before SP3, Win XP before SP1) crash in
157 // ShellExecute on long URLs (bug 161357 on bugzilla.mozilla.org). IE 5 and 6
158 // support URLS of 2083 chars in length, 2K is safe."
159 const size_t kMaxUrlLength = 2048;
160 if (escaped_url.length() > kMaxUrlLength) {
165 if (base::win::GetVersion() < base::win::VERSION_WIN7) {
166 if (!ValidateShellCommandForScheme(url.scheme()))
170 if (reinterpret_cast<ULONG_PTR>(ShellExecuteA(NULL, "open",
171 escaped_url.c_str(), NULL, NULL,
172 SW_SHOWNORMAL)) <= 32) {
173 // We fail to execute the call. We could display a message to the user.
174 // TODO(nsylvain): we should also add a dialog to warn on errors. See
180 #if !defined(USE_AURA)
181 gfx::NativeWindow GetTopLevel(gfx::NativeView view) {
182 return ::GetAncestor(view, GA_ROOT);
185 gfx::NativeView GetParent(gfx::NativeView view) {
186 return ::GetParent(view);
189 bool IsWindowActive(gfx::NativeWindow window) {
190 return ::GetForegroundWindow() == window;
193 void ActivateWindow(gfx::NativeWindow window) {
194 ::SetForegroundWindow(window);
197 bool IsVisible(gfx::NativeView view) {
198 // MSVC complains if we don't include != 0.
199 return ::IsWindowVisible(view) != 0;
203 } // namespace platform_util