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. 4 5 #include "chrome/browser/platform_util.h" 6 7 #include <commdlg.h> 8 #include <dwmapi.h> 9 #include <shellapi.h> 10 #include <shlobj.h> 11 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" 25 #include "url/gurl.h" 26 27 using content::BrowserThread; 28 29 namespace { 30 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:\\". 35 if (dir.empty()) 36 return; 37 38 typedef HRESULT (WINAPI *SHOpenFolderAndSelectItemsFuncPtr)( 39 PCIDLIST_ABSOLUTE pidl_Folder, 40 UINT cidl, 41 PCUITEMID_CHILD_ARRAY pidls, 42 DWORD flags); 43 44 static SHOpenFolderAndSelectItemsFuncPtr open_folder_and_select_itemsPtr = 45 NULL; 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 53 // the process. 54 HMODULE shell32_base = GetModuleHandle(L"shell32.dll"); 55 if (!shell32_base) { 56 NOTREACHED() << " " << __FUNCTION__ << "(): Can't open shell32.dll"; 57 return; 58 } 59 open_folder_and_select_itemsPtr = 60 reinterpret_cast<SHOpenFolderAndSelectItemsFuncPtr> 61 (GetProcAddress(shell32_base, "SHOpenFolderAndSelectItems")); 62 } 63 if (!open_folder_and_select_itemsPtr) { 64 ShellExecute(NULL, L"open", dir.value().c_str(), NULL, NULL, SW_SHOW); 65 return; 66 } 67 68 base::win::ScopedComPtr<IShellFolder> desktop; 69 HRESULT hr = SHGetDesktopFolder(desktop.Receive()); 70 if (FAILED(hr)) 71 return; 72 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); 77 if (FAILED(hr)) 78 return; 79 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); 84 if (FAILED(hr)) 85 return; 86 87 const ITEMIDLIST* highlight[] = { file_item }; 88 89 hr = (*open_folder_and_select_itemsPtr)(dir_item, arraysize(highlight), 90 highlight, NULL); 91 92 if (FAILED(hr)) { 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); 98 } else { 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() << "\"" 106 << " hr = " << hr 107 << " " << reinterpret_cast<LPTSTR>(&message); 108 if (message) 109 LocalFree(message); 110 } 111 } 112 } 113 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); 121 if (!key.Valid()) 122 return false; 123 DWORD size = 0; 124 key.ReadValue(NULL, NULL, &size, NULL); 125 if (size <= 2) 126 return false; 127 return true; 128 } 129 130 } // namespace 131 132 namespace platform_util { 133 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)); 138 } 139 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)); 145 } 146 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, "\""); 153 escaped_url += "\""; 154 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) { 161 NOTREACHED(); 162 return; 163 } 164 165 if (base::win::GetVersion() < base::win::VERSION_WIN7) { 166 if (!ValidateShellCommandForScheme(url.scheme())) 167 return; 168 } 169 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 175 // bug 1136923. 176 return; 177 } 178 } 179 180 #if !defined(USE_AURA) 181 gfx::NativeWindow GetTopLevel(gfx::NativeView view) { 182 return ::GetAncestor(view, GA_ROOT); 183 } 184 185 gfx::NativeView GetParent(gfx::NativeView view) { 186 return ::GetParent(view); 187 } 188 189 bool IsWindowActive(gfx::NativeWindow window) { 190 return ::GetForegroundWindow() == window; 191 } 192 193 void ActivateWindow(gfx::NativeWindow window) { 194 ::SetForegroundWindow(window); 195 } 196 197 bool IsVisible(gfx::NativeView view) { 198 // MSVC complains if we don't include != 0. 199 return ::IsWindowVisible(view) != 0; 200 } 201 #endif 202 203 } // namespace platform_util 204