This source file includes following definitions.
- ShowItemInFolderOnFileThread
- ValidateShellCommandForScheme
- OpenExternalOnFileThread
- ShowItemInFolder
- OpenItem
- OpenExternal
- GetTopLevel
- GetParent
- IsWindowActive
- ActivateWindow
- IsVisible
#include "chrome/browser/platform_util.h"
#include <commdlg.h>
#include <dwmapi.h>
#include <shellapi.h>
#include <shlobj.h>
#include "base/bind.h"
#include "base/bind_helpers.h"
#include "base/files/file_path.h"
#include "base/logging.h"
#include "base/strings/string_util.h"
#include "base/strings/utf_string_conversions.h"
#include "base/win/registry.h"
#include "base/win/scoped_co_mem.h"
#include "base/win/scoped_comptr.h"
#include "base/win/windows_version.h"
#include "chrome/browser/lifetime/application_lifetime.h"
#include "chrome/browser/ui/host_desktop.h"
#include "content/public/browser/browser_thread.h"
#include "ui/base/win/shell.h"
#include "ui/gfx/native_widget_types.h"
#include "url/gurl.h"
using content::BrowserThread;
namespace {
void ShowItemInFolderOnFileThread(const base::FilePath& full_path) {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE));
base::FilePath dir = full_path.DirName().AsEndingWithSeparator();
if (dir.empty())
return;
typedef HRESULT (WINAPI *SHOpenFolderAndSelectItemsFuncPtr)(
PCIDLIST_ABSOLUTE pidl_Folder,
UINT cidl,
PCUITEMID_CHILD_ARRAY pidls,
DWORD flags);
static SHOpenFolderAndSelectItemsFuncPtr open_folder_and_select_itemsPtr =
NULL;
static bool initialize_open_folder_proc = true;
if (initialize_open_folder_proc) {
initialize_open_folder_proc = false;
HMODULE shell32_base = GetModuleHandle(L"shell32.dll");
if (!shell32_base) {
NOTREACHED() << " " << __FUNCTION__ << "(): Can't open shell32.dll";
return;
}
open_folder_and_select_itemsPtr =
reinterpret_cast<SHOpenFolderAndSelectItemsFuncPtr>
(GetProcAddress(shell32_base, "SHOpenFolderAndSelectItems"));
}
if (!open_folder_and_select_itemsPtr) {
ShellExecute(NULL, L"open", dir.value().c_str(), NULL, NULL, SW_SHOW);
return;
}
base::win::ScopedComPtr<IShellFolder> desktop;
HRESULT hr = SHGetDesktopFolder(desktop.Receive());
if (FAILED(hr))
return;
base::win::ScopedCoMem<ITEMIDLIST> dir_item;
hr = desktop->ParseDisplayName(NULL, NULL,
const_cast<wchar_t *>(dir.value().c_str()),
NULL, &dir_item, NULL);
if (FAILED(hr))
return;
base::win::ScopedCoMem<ITEMIDLIST> file_item;
hr = desktop->ParseDisplayName(NULL, NULL,
const_cast<wchar_t *>(full_path.value().c_str()),
NULL, &file_item, NULL);
if (FAILED(hr))
return;
const ITEMIDLIST* highlight[] = { file_item };
hr = (*open_folder_and_select_itemsPtr)(dir_item, arraysize(highlight),
highlight, NULL);
if (FAILED(hr)) {
if (hr == ERROR_FILE_NOT_FOUND) {
ShellExecute(NULL, L"open", dir.value().c_str(), NULL, NULL, SW_SHOW);
} else {
LPTSTR message = NULL;
DWORD message_length = FormatMessage(
FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM,
0, hr, 0, reinterpret_cast<LPTSTR>(&message), 0, NULL);
LOG(WARNING) << " " << __FUNCTION__
<< "(): Can't open full_path = \""
<< full_path.value() << "\""
<< " hr = " << hr
<< " " << reinterpret_cast<LPTSTR>(&message);
if (message)
LocalFree(message);
}
}
}
bool ValidateShellCommandForScheme(const std::string& scheme) {
base::win::RegKey key;
std::wstring registry_path = base::ASCIIToWide(scheme) +
L"\\shell\\open\\command";
key.Open(HKEY_CLASSES_ROOT, registry_path.c_str(), KEY_READ);
if (!key.Valid())
return false;
DWORD size = 0;
key.ReadValue(NULL, NULL, &size, NULL);
if (size <= 2)
return false;
return true;
}
void OpenExternalOnFileThread(const GURL& url) {
std::string escaped_url = url.spec();
escaped_url.insert(0, "\"");
escaped_url += "\"";
const size_t kMaxUrlLength = 2048;
if (escaped_url.length() > kMaxUrlLength) {
NOTREACHED();
return;
}
if (base::win::GetVersion() < base::win::VERSION_WIN7) {
if (!ValidateShellCommandForScheme(url.scheme()))
return;
}
if (reinterpret_cast<ULONG_PTR>(ShellExecuteA(NULL, "open",
escaped_url.c_str(), NULL, NULL,
SW_SHOWNORMAL)) <= 32) {
return;
}
}
}
namespace platform_util {
void ShowItemInFolder(Profile* profile, const base::FilePath& full_path) {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
if (chrome::GetActiveDesktop() == chrome::HOST_DESKTOP_TYPE_ASH)
chrome::ActivateDesktopHelper(chrome::ASH_KEEP_RUNNING);
BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE,
base::Bind(&ShowItemInFolderOnFileThread, full_path));
}
void OpenItem(Profile* profile, const base::FilePath& full_path) {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
if (chrome::GetActiveDesktop() == chrome::HOST_DESKTOP_TYPE_ASH)
chrome::ActivateDesktopHelper(chrome::ASH_KEEP_RUNNING);
BrowserThread::PostTask(
BrowserThread::FILE, FROM_HERE,
base::Bind(base::IgnoreResult(&ui::win::OpenItemViaShell), full_path));
}
void OpenExternal(Profile* profile, const GURL& url) {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
if (chrome::GetActiveDesktop() == chrome::HOST_DESKTOP_TYPE_ASH &&
!url.SchemeIsHTTPOrHTTPS())
chrome::ActivateDesktopHelper(chrome::ASH_KEEP_RUNNING);
BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE,
base::Bind(&OpenExternalOnFileThread, url));
}
#if !defined(USE_AURA)
gfx::NativeWindow GetTopLevel(gfx::NativeView view) {
return ::GetAncestor(view, GA_ROOT);
}
gfx::NativeView GetParent(gfx::NativeView view) {
return ::GetParent(view);
}
bool IsWindowActive(gfx::NativeWindow window) {
return ::GetForegroundWindow() == window;
}
void ActivateWindow(gfx::NativeWindow window) {
::SetForegroundWindow(window);
}
bool IsVisible(gfx::NativeView view) {
return ::IsWindowVisible(view) != 0;
}
#endif
}