diff options
author | tfarina@chromium.org <tfarina@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2011-06-25 00:21:07 +0000 |
---|---|---|
committer | tfarina@chromium.org <tfarina@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2011-06-25 00:21:07 +0000 |
commit | 9cbab6520cd7684fd90982a4fe6fb84c87ba4492 (patch) | |
tree | 49fbed1c40ca3a5052fe7af8e9b3a4c994f3796b /ui/base | |
parent | a6e9dce2f8c811744693bfdd85a585ea6d26e752 (diff) | |
download | chromium_src-9cbab6520cd7684fd90982a4fe6fb84c87ba4492.zip chromium_src-9cbab6520cd7684fd90982a4fe6fb84c87ba4492.tar.gz chromium_src-9cbab6520cd7684fd90982a4fe6fb84c87ba4492.tar.bz2 |
Move app/win/* files to base/win/, ui/base/win and chrome/common/ directories.
BUG=72317
TEST=None
R=rsesek@chromium.org,brettw@chromium.org
Review URL: http://codereview.chromium.org/7231016
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@90464 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'ui/base')
-rw-r--r-- | ui/base/win/shell.cc | 112 | ||||
-rw-r--r-- | ui/base/win/shell.h | 42 |
2 files changed, 154 insertions, 0 deletions
diff --git a/ui/base/win/shell.cc b/ui/base/win/shell.cc new file mode 100644 index 0000000..c78730c --- /dev/null +++ b/ui/base/win/shell.cc @@ -0,0 +1,112 @@ +// Copyright (c) 2011 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include "ui/base/win/shell.h" + +#include <shellapi.h> +#include <shlobj.h> + +#include "base/file_path.h" +#include "base/native_library.h" +#include "base/string_util.h" +#include "base/win/scoped_comptr.h" +#include "base/win/win_util.h" +#include "base/win/windows_version.h" + +namespace ui { +namespace win { + +namespace { + +const wchar_t kShell32[] = L"shell32.dll"; +const char kSHGetPropertyStoreForWindow[] = "SHGetPropertyStoreForWindow"; + +// Define the type of SHGetPropertyStoreForWindow is SHGPSFW. +typedef DECLSPEC_IMPORT HRESULT (STDAPICALLTYPE *SHGPSFW)(HWND hwnd, + REFIID riid, + void** ppv); + +} // namespace + +// Open an item via a shell execute command. Error code checking and casting +// explanation: http://msdn2.microsoft.com/en-us/library/ms647732.aspx +bool OpenItemViaShell(const FilePath& full_path) { + HINSTANCE h = ::ShellExecuteW( + NULL, NULL, full_path.value().c_str(), NULL, + full_path.DirName().value().c_str(), SW_SHOWNORMAL); + + LONG_PTR error = reinterpret_cast<LONG_PTR>(h); + if (error > 32) + return true; + + if ((error == SE_ERR_NOASSOC)) + return OpenItemWithExternalApp(full_path.value()); + + return false; +} + +bool OpenItemViaShellNoZoneCheck(const FilePath& full_path) { + SHELLEXECUTEINFO sei = { sizeof(sei) }; + sei.fMask = SEE_MASK_NOZONECHECKS | SEE_MASK_FLAG_DDEWAIT; + sei.nShow = SW_SHOWNORMAL; + sei.lpVerb = NULL; + sei.lpFile = full_path.value().c_str(); + if (::ShellExecuteExW(&sei)) + return true; + LONG_PTR error = reinterpret_cast<LONG_PTR>(sei.hInstApp); + if ((error == SE_ERR_NOASSOC)) + return OpenItemWithExternalApp(full_path.value()); + return false; +} + +// Show the Windows "Open With" dialog box to ask the user to pick an app to +// open the file with. +bool OpenItemWithExternalApp(const string16& full_path) { + SHELLEXECUTEINFO sei = { sizeof(sei) }; + sei.fMask = SEE_MASK_FLAG_DDEWAIT; + sei.nShow = SW_SHOWNORMAL; + sei.lpVerb = L"openas"; + sei.lpFile = full_path.c_str(); + return (TRUE == ::ShellExecuteExW(&sei)); +} + +void SetAppIdForWindow(const string16& app_id, HWND hwnd) { + // This functionality is only available on Win7+. + if (base::win::GetVersion() < base::win::VERSION_WIN7) + return; + + // Load Shell32.dll into memory. + // TODO(brg): Remove this mechanism when the Win7 SDK is available in trunk. + std::wstring shell32_filename(kShell32); + FilePath shell32_filepath(shell32_filename); + base::NativeLibrary shell32_library = base::LoadNativeLibrary( + shell32_filepath, NULL); + + if (!shell32_library) + return; + + // Get the function pointer for SHGetPropertyStoreForWindow. + void* function = base::GetFunctionPointerFromNativeLibrary( + shell32_library, + kSHGetPropertyStoreForWindow); + + if (!function) { + base::UnloadNativeLibrary(shell32_library); + return; + } + + // Set the application's name. + base::win::ScopedComPtr<IPropertyStore> pps; + SHGPSFW SHGetPropertyStoreForWindow = static_cast<SHGPSFW>(function); + HRESULT result = SHGetPropertyStoreForWindow( + hwnd, __uuidof(*pps), reinterpret_cast<void**>(pps.Receive())); + if (S_OK == result) + base::win::SetAppIdForPropertyStore(pps, app_id.c_str()); + + // Cleanup. + base::UnloadNativeLibrary(shell32_library); +} + +} // namespace win +} // namespace ui diff --git a/ui/base/win/shell.h b/ui/base/win/shell.h new file mode 100644 index 0000000..cc32477 --- /dev/null +++ b/ui/base/win/shell.h @@ -0,0 +1,42 @@ +// Copyright (c) 2011 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#ifndef UI_BASE_WIN_SHELL_H_ +#define UI_BASE_WIN_SHELL_H_ +#pragma once + +#include <windows.h> + +#include "base/string16.h" + +class FilePath; + +namespace ui { +namespace win { + +// Open or run a file via the Windows shell. In the event that there is no +// default application registered for the file specified by 'full_path', +// ask the user, via the Windows "Open With" dialog. +// Returns 'true' on successful open, 'false' otherwise. +bool OpenItemViaShell(const FilePath& full_path); + +// The download manager now writes the alternate data stream with the +// zone on all downloads. This function is equivalent to OpenItemViaShell +// without showing the zone warning dialog. +bool OpenItemViaShellNoZoneCheck(const FilePath& full_path); + +// Ask the user, via the Windows "Open With" dialog, for an application to use +// to open the file specified by 'full_path'. +// Returns 'true' on successful open, 'false' otherwise. +bool OpenItemWithExternalApp(const string16& full_path); + +// Sets the application id given as the Application Model ID for the window +// specified. This method is used to insure that different web applications +// do not group together on the Win7 task bar. +void SetAppIdForWindow(const string16& app_id, HWND hwnd); + +} // namespace win +} // namespace ui + +#endif // UI_BASE_WIN_SHELL_H_ |