1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/widget/windows/WinUtils.h Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,437 @@ 1.4 +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ 1.5 +/* This Source Code Form is subject to the terms of the Mozilla Public 1.6 + * License, v. 2.0. If a copy of the MPL was not distributed with this 1.7 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.8 + 1.9 +#ifndef mozilla_widget_WinUtils_h__ 1.10 +#define mozilla_widget_WinUtils_h__ 1.11 + 1.12 +#include "nscore.h" 1.13 +#include <windows.h> 1.14 +#include <shobjidl.h> 1.15 +#include <uxtheme.h> 1.16 +#include <dwmapi.h> 1.17 +#include "nsAutoPtr.h" 1.18 +#include "nsString.h" 1.19 +#include "nsRegion.h" 1.20 + 1.21 +#include "nsIRunnable.h" 1.22 +#include "nsICryptoHash.h" 1.23 +#ifdef MOZ_PLACES 1.24 +#include "nsIFaviconService.h" 1.25 +#endif 1.26 +#include "nsIDownloader.h" 1.27 +#include "nsIURI.h" 1.28 +#include "nsIWidget.h" 1.29 + 1.30 +#include "mozilla/Attributes.h" 1.31 + 1.32 +class nsWindow; 1.33 +class nsWindowBase; 1.34 +struct KeyPair; 1.35 +struct nsIntRect; 1.36 +class nsIThread; 1.37 + 1.38 +namespace mozilla { 1.39 +namespace widget { 1.40 + 1.41 +// More complete QS definitions for MsgWaitForMultipleObjects() and 1.42 +// GetQueueStatus() that include newer win8 specific defines. 1.43 + 1.44 +#ifndef QS_RAWINPUT 1.45 +#define QS_RAWINPUT 0x0400 1.46 +#endif 1.47 + 1.48 +#ifndef QS_TOUCH 1.49 +#define QS_TOUCH 0x0800 1.50 +#define QS_POINTER 0x1000 1.51 +#endif 1.52 + 1.53 +#define MOZ_QS_ALLEVENT (QS_KEY | QS_MOUSEMOVE | QS_MOUSEBUTTON | \ 1.54 + QS_POSTMESSAGE | QS_TIMER | QS_PAINT | \ 1.55 + QS_SENDMESSAGE | QS_HOTKEY | \ 1.56 + QS_ALLPOSTMESSAGE | QS_RAWINPUT | \ 1.57 + QS_TOUCH | QS_POINTER) 1.58 + 1.59 +// Logging macros 1.60 +#define LogFunction() mozilla::widget::WinUtils::Log(__FUNCTION__) 1.61 +#define LogThread() mozilla::widget::WinUtils::Log("%s: IsMainThread:%d ThreadId:%X", __FUNCTION__, NS_IsMainThread(), GetCurrentThreadId()) 1.62 +#define LogThis() mozilla::widget::WinUtils::Log("[%X] %s", this, __FUNCTION__) 1.63 +#define LogException(e) mozilla::widget::WinUtils::Log("%s Exception:%s", __FUNCTION__, e->ToString()->Data()) 1.64 +#define LogHRESULT(hr) mozilla::widget::WinUtils::Log("%s hr=%X", __FUNCTION__, hr) 1.65 + 1.66 +class myDownloadObserver MOZ_FINAL : public nsIDownloadObserver 1.67 +{ 1.68 +public: 1.69 + NS_DECL_ISUPPORTS 1.70 + NS_DECL_NSIDOWNLOADOBSERVER 1.71 +}; 1.72 + 1.73 +class WinUtils { 1.74 +public: 1.75 + /** 1.76 + * Functions to convert between logical pixels as used by most Windows APIs 1.77 + * and physical (device) pixels. 1.78 + */ 1.79 + static double LogToPhysFactor(); 1.80 + static double PhysToLogFactor(); 1.81 + static int32_t LogToPhys(double aValue); 1.82 + static double PhysToLog(int32_t aValue); 1.83 + 1.84 + /** 1.85 + * Logging helpers that dump output to prlog module 'Widget', console, and 1.86 + * OutputDebugString. Note these output in both debug and release builds. 1.87 + */ 1.88 + static void Log(const char *fmt, ...); 1.89 + static void LogW(const wchar_t *fmt, ...); 1.90 + 1.91 + /** 1.92 + * PeekMessage() and GetMessage() are wrapper methods for PeekMessageW(), 1.93 + * GetMessageW(), ITfMessageMgr::PeekMessageW() and 1.94 + * ITfMessageMgr::GetMessageW(). 1.95 + * Don't call the native APIs directly. You MUST use these methods instead. 1.96 + */ 1.97 + static bool PeekMessage(LPMSG aMsg, HWND aWnd, UINT aFirstMessage, 1.98 + UINT aLastMessage, UINT aOption); 1.99 + static bool GetMessage(LPMSG aMsg, HWND aWnd, UINT aFirstMessage, 1.100 + UINT aLastMessage); 1.101 + /** 1.102 + * Gets the value of a string-typed registry value. 1.103 + * 1.104 + * @param aRoot The registry root to search in. 1.105 + * @param aKeyName The name of the registry key to open. 1.106 + * @param aValueName The name of the registry value in the specified key whose 1.107 + * value is to be retrieved. Can be null, to retrieve the key's unnamed/ 1.108 + * default value. 1.109 + * @param aBuffer The buffer into which to store the string value. Can be 1.110 + * null, in which case the return value indicates just whether the value 1.111 + * exists. 1.112 + * @param aBufferLength The size of aBuffer, in bytes. 1.113 + * @return Whether the value exists and is a string. 1.114 + */ 1.115 + static bool GetRegistryKey(HKEY aRoot, 1.116 + char16ptr_t aKeyName, 1.117 + char16ptr_t aValueName, 1.118 + wchar_t* aBuffer, 1.119 + DWORD aBufferLength); 1.120 + 1.121 + /** 1.122 + * Checks whether the registry key exists in either 32bit or 64bit branch on 1.123 + * the environment. 1.124 + * 1.125 + * @param aRoot The registry root of aName. 1.126 + * @param aKeyName The name of the registry key to check. 1.127 + * @return TRUE if it exists and is readable. Otherwise, FALSE. 1.128 + */ 1.129 + static bool HasRegistryKey(HKEY aRoot, 1.130 + char16ptr_t aKeyName); 1.131 + 1.132 + /** 1.133 + * GetTopLevelHWND() returns a window handle of the top level window which 1.134 + * aWnd belongs to. Note that the result may not be our window, i.e., it 1.135 + * may not be managed by nsWindow. 1.136 + * 1.137 + * See follwing table for the detail of the result window type. 1.138 + * 1.139 + * +-------------------------+-----------------------------------------------+ 1.140 + * | | aStopIfNotPopup | 1.141 + * +-------------------------+-----------------------+-----------------------+ 1.142 + * | | TRUE | FALSE | 1.143 + + +-----------------+-------+-----------------------+-----------------------+ 1.144 + * | | | * an independent top level window | 1.145 + * | | TRUE | * a pupup window (WS_POPUP) | 1.146 + * | | | * an owned top level window (like dialog) | 1.147 + * | aStopIfNotChild +-------+-----------------------+-----------------------+ 1.148 + * | | | * independent window | * only an independent | 1.149 + * | | FALSE | * non-popup-owned- | top level window | 1.150 + * | | | window like dialog | | 1.151 + * +-----------------+-------+-----------------------+-----------------------+ 1.152 + */ 1.153 + static HWND GetTopLevelHWND(HWND aWnd, 1.154 + bool aStopIfNotChild = false, 1.155 + bool aStopIfNotPopup = true); 1.156 + 1.157 + /** 1.158 + * SetNSWindowBasePtr() associates an nsWindowBase to aWnd. If aWidget is 1.159 + * nullptr, it dissociate any nsBaseWidget pointer from aWnd. 1.160 + * GetNSWindowBasePtr() returns an nsWindowBase pointer which was associated by 1.161 + * SetNSWindowBasePtr(). 1.162 + * GetNSWindowPtr() is a legacy api for win32 nsWindow and should be avoided 1.163 + * outside of nsWindow src. 1.164 + */ 1.165 + static bool SetNSWindowBasePtr(HWND aWnd, nsWindowBase* aWidget); 1.166 + static nsWindowBase* GetNSWindowBasePtr(HWND aWnd); 1.167 + static nsWindow* GetNSWindowPtr(HWND aWnd); 1.168 + 1.169 + /** 1.170 + * GetMonitorCount() returns count of monitors on the environment. 1.171 + */ 1.172 + static int32_t GetMonitorCount(); 1.173 + 1.174 + /** 1.175 + * IsOurProcessWindow() returns TRUE if aWnd belongs our process. 1.176 + * Otherwise, FALSE. 1.177 + */ 1.178 + static bool IsOurProcessWindow(HWND aWnd); 1.179 + 1.180 + /** 1.181 + * FindOurProcessWindow() returns the nearest ancestor window which 1.182 + * belongs to our process. If it fails to find our process's window by the 1.183 + * top level window, returns nullptr. And note that this is using 1.184 + * ::GetParent() for climbing the window hierarchy, therefore, it gives 1.185 + * up at an owned top level window except popup window (e.g., dialog). 1.186 + */ 1.187 + static HWND FindOurProcessWindow(HWND aWnd); 1.188 + 1.189 + /** 1.190 + * FindOurWindowAtPoint() returns the topmost child window which belongs to 1.191 + * our process's top level window. 1.192 + * 1.193 + * NOTE: the topmost child window may NOT be our process's window like a 1.194 + * plugin's window. 1.195 + */ 1.196 + static HWND FindOurWindowAtPoint(const POINT& aPointInScreen); 1.197 + 1.198 + /** 1.199 + * InitMSG() returns an MSG struct which was initialized by the params. 1.200 + * Don't trust the other members in the result. 1.201 + */ 1.202 + static MSG InitMSG(UINT aMessage, WPARAM wParam, LPARAM lParam, HWND aWnd); 1.203 + 1.204 + /** 1.205 + * GetScanCode() returns a scan code for the LPARAM of WM_KEYDOWN, WM_KEYUP, 1.206 + * WM_CHAR and WM_UNICHAR. 1.207 + * 1.208 + */ 1.209 + static WORD GetScanCode(LPARAM aLParam) 1.210 + { 1.211 + return (aLParam >> 16) & 0xFF; 1.212 + } 1.213 + 1.214 + /** 1.215 + * IsExtendedScanCode() returns TRUE if the LPARAM indicates the key message 1.216 + * is an extended key event. 1.217 + */ 1.218 + static bool IsExtendedScanCode(LPARAM aLParam) 1.219 + { 1.220 + return (aLParam & 0x1000000) != 0; 1.221 + } 1.222 + 1.223 + /** 1.224 + * GetInternalMessage() converts a native message to an internal message. 1.225 + * If there is no internal message for the given native message, returns 1.226 + * the native message itself. 1.227 + */ 1.228 + static UINT GetInternalMessage(UINT aNativeMessage); 1.229 + 1.230 + /** 1.231 + * GetNativeMessage() converts an internal message to a native message. 1.232 + * If aInternalMessage is a native message, returns the native message itself. 1.233 + */ 1.234 + static UINT GetNativeMessage(UINT aInternalMessage); 1.235 + 1.236 + /** 1.237 + * GetMouseInputSource() returns a pointing device information. The value is 1.238 + * one of nsIDOMMouseEvent::MOZ_SOURCE_*. This method MUST be called during 1.239 + * mouse message handling. 1.240 + */ 1.241 + static uint16_t GetMouseInputSource(); 1.242 + 1.243 + static bool GetIsMouseFromTouch(uint32_t aEventType); 1.244 + 1.245 + /** 1.246 + * SHCreateItemFromParsingName() calls native SHCreateItemFromParsingName() 1.247 + * API which is available on Vista and up. 1.248 + */ 1.249 + static HRESULT SHCreateItemFromParsingName(PCWSTR pszPath, IBindCtx *pbc, 1.250 + REFIID riid, void **ppv); 1.251 + 1.252 + /** 1.253 + * SHGetKnownFolderPath() calls native SHGetKnownFolderPath() 1.254 + * API which is available on Vista and up. 1.255 + */ 1.256 + static HRESULT SHGetKnownFolderPath(REFKNOWNFOLDERID rfid, 1.257 + DWORD dwFlags, 1.258 + HANDLE hToken, 1.259 + PWSTR *ppszPath); 1.260 + /** 1.261 + * GetShellItemPath return the file or directory path of a shell item. 1.262 + * Internally calls IShellItem's GetDisplayName. 1.263 + * 1.264 + * aItem the shell item containing the path. 1.265 + * aResultString the resulting string path. 1.266 + * returns true if a path was retreived. 1.267 + */ 1.268 + static bool GetShellItemPath(IShellItem* aItem, 1.269 + nsString& aResultString); 1.270 + 1.271 + /** 1.272 + * ConvertHRGNToRegion converts a Windows HRGN to an nsIntRegion. 1.273 + * 1.274 + * aRgn the HRGN to convert. 1.275 + * returns the nsIntRegion. 1.276 + */ 1.277 + static nsIntRegion ConvertHRGNToRegion(HRGN aRgn); 1.278 + 1.279 + /** 1.280 + * ToIntRect converts a Windows RECT to a nsIntRect. 1.281 + * 1.282 + * aRect the RECT to convert. 1.283 + * returns the nsIntRect. 1.284 + */ 1.285 + static nsIntRect ToIntRect(const RECT& aRect); 1.286 + 1.287 + /** 1.288 + * Returns true if the context or IME state is enabled. Otherwise, false. 1.289 + */ 1.290 + static bool IsIMEEnabled(const InputContext& aInputContext); 1.291 + static bool IsIMEEnabled(IMEState::Enabled aIMEState); 1.292 + 1.293 + /** 1.294 + * Returns modifier key array for aModifiers. This is for 1.295 + * nsIWidget::SynthethizeNative*Event(). 1.296 + */ 1.297 + static void SetupKeyModifiersSequence(nsTArray<KeyPair>* aArray, 1.298 + uint32_t aModifiers); 1.299 + 1.300 + // dwmapi.dll function typedefs and declarations 1.301 + typedef HRESULT (WINAPI*DwmExtendFrameIntoClientAreaProc)(HWND hWnd, const MARGINS *pMarInset); 1.302 + typedef HRESULT (WINAPI*DwmIsCompositionEnabledProc)(BOOL *pfEnabled); 1.303 + typedef HRESULT (WINAPI*DwmSetIconicThumbnailProc)(HWND hWnd, HBITMAP hBitmap, DWORD dwSITFlags); 1.304 + typedef HRESULT (WINAPI*DwmSetIconicLivePreviewBitmapProc)(HWND hWnd, HBITMAP hBitmap, POINT *pptClient, DWORD dwSITFlags); 1.305 + typedef HRESULT (WINAPI*DwmGetWindowAttributeProc)(HWND hWnd, DWORD dwAttribute, LPCVOID pvAttribute, DWORD cbAttribute); 1.306 + typedef HRESULT (WINAPI*DwmSetWindowAttributeProc)(HWND hWnd, DWORD dwAttribute, LPCVOID pvAttribute, DWORD cbAttribute); 1.307 + typedef HRESULT (WINAPI*DwmInvalidateIconicBitmapsProc)(HWND hWnd); 1.308 + typedef HRESULT (WINAPI*DwmDefWindowProcProc)(HWND hWnd, UINT msg, LPARAM lParam, WPARAM wParam, LRESULT *aRetValue); 1.309 + typedef HRESULT (WINAPI*DwmGetCompositionTimingInfoProc)(HWND hWnd, DWM_TIMING_INFO *info); 1.310 + 1.311 + static DwmExtendFrameIntoClientAreaProc dwmExtendFrameIntoClientAreaPtr; 1.312 + static DwmIsCompositionEnabledProc dwmIsCompositionEnabledPtr; 1.313 + static DwmSetIconicThumbnailProc dwmSetIconicThumbnailPtr; 1.314 + static DwmSetIconicLivePreviewBitmapProc dwmSetIconicLivePreviewBitmapPtr; 1.315 + static DwmGetWindowAttributeProc dwmGetWindowAttributePtr; 1.316 + static DwmSetWindowAttributeProc dwmSetWindowAttributePtr; 1.317 + static DwmInvalidateIconicBitmapsProc dwmInvalidateIconicBitmapsPtr; 1.318 + static DwmDefWindowProcProc dwmDwmDefWindowProcPtr; 1.319 + static DwmGetCompositionTimingInfoProc dwmGetCompositionTimingInfoPtr; 1.320 + 1.321 + static void Initialize(); 1.322 + 1.323 + static bool ShouldHideScrollbars(); 1.324 + 1.325 +private: 1.326 + typedef HRESULT (WINAPI * SHCreateItemFromParsingNamePtr)(PCWSTR pszPath, 1.327 + IBindCtx *pbc, 1.328 + REFIID riid, 1.329 + void **ppv); 1.330 + static SHCreateItemFromParsingNamePtr sCreateItemFromParsingName; 1.331 + typedef HRESULT (WINAPI * SHGetKnownFolderPathPtr)(REFKNOWNFOLDERID rfid, 1.332 + DWORD dwFlags, 1.333 + HANDLE hToken, 1.334 + PWSTR *ppszPath); 1.335 + static SHGetKnownFolderPathPtr sGetKnownFolderPath; 1.336 +}; 1.337 + 1.338 +#ifdef MOZ_PLACES 1.339 +class AsyncFaviconDataReady MOZ_FINAL : public nsIFaviconDataCallback 1.340 +{ 1.341 +public: 1.342 + NS_DECL_ISUPPORTS 1.343 + NS_DECL_NSIFAVICONDATACALLBACK 1.344 + 1.345 + AsyncFaviconDataReady(nsIURI *aNewURI, 1.346 + nsCOMPtr<nsIThread> &aIOThread, 1.347 + const bool aURLShortcut); 1.348 + nsresult OnFaviconDataNotAvailable(void); 1.349 +private: 1.350 + nsCOMPtr<nsIURI> mNewURI; 1.351 + nsCOMPtr<nsIThread> mIOThread; 1.352 + const bool mURLShortcut; 1.353 +}; 1.354 +#endif 1.355 + 1.356 +/** 1.357 + * Asynchronously tries add the list to the build 1.358 + */ 1.359 +class AsyncEncodeAndWriteIcon : public nsIRunnable 1.360 +{ 1.361 +public: 1.362 + const bool mURLShortcut; 1.363 + NS_DECL_THREADSAFE_ISUPPORTS 1.364 + NS_DECL_NSIRUNNABLE 1.365 + 1.366 + // Warning: AsyncEncodeAndWriteIcon assumes ownership of the aData buffer passed in 1.367 + AsyncEncodeAndWriteIcon(const nsAString &aIconPath, 1.368 + uint8_t *aData, uint32_t aDataLen, uint32_t aStride, 1.369 + uint32_t aWidth, uint32_t aHeight, 1.370 + const bool aURLShortcut); 1.371 + virtual ~AsyncEncodeAndWriteIcon(); 1.372 + 1.373 +private: 1.374 + nsAutoString mIconPath; 1.375 + nsAutoCString mMimeTypeOfInputData; 1.376 + nsAutoArrayPtr<uint8_t> mBuffer; 1.377 + HMODULE sDwmDLL; 1.378 + uint32_t mBufferLength; 1.379 + uint32_t mStride; 1.380 + uint32_t mWidth; 1.381 + uint32_t mHeight; 1.382 +}; 1.383 + 1.384 + 1.385 +class AsyncDeleteIconFromDisk : public nsIRunnable 1.386 +{ 1.387 +public: 1.388 + NS_DECL_THREADSAFE_ISUPPORTS 1.389 + NS_DECL_NSIRUNNABLE 1.390 + 1.391 + AsyncDeleteIconFromDisk(const nsAString &aIconPath); 1.392 + virtual ~AsyncDeleteIconFromDisk(); 1.393 + 1.394 +private: 1.395 + nsAutoString mIconPath; 1.396 +}; 1.397 + 1.398 +class AsyncDeleteAllFaviconsFromDisk : public nsIRunnable 1.399 +{ 1.400 +public: 1.401 + NS_DECL_THREADSAFE_ISUPPORTS 1.402 + NS_DECL_NSIRUNNABLE 1.403 + 1.404 + AsyncDeleteAllFaviconsFromDisk(); 1.405 + virtual ~AsyncDeleteAllFaviconsFromDisk(); 1.406 +}; 1.407 + 1.408 +class FaviconHelper 1.409 +{ 1.410 +public: 1.411 + static const char kJumpListCacheDir[]; 1.412 + static const char kShortcutCacheDir[]; 1.413 + static nsresult ObtainCachedIconFile(nsCOMPtr<nsIURI> aFaviconPageURI, 1.414 + nsString &aICOFilePath, 1.415 + nsCOMPtr<nsIThread> &aIOThread, 1.416 + bool aURLShortcut); 1.417 + 1.418 + static nsresult HashURI(nsCOMPtr<nsICryptoHash> &aCryptoHash, 1.419 + nsIURI *aUri, 1.420 + nsACString& aUriHash); 1.421 + 1.422 + static nsresult GetOutputIconPath(nsCOMPtr<nsIURI> aFaviconPageURI, 1.423 + nsCOMPtr<nsIFile> &aICOFile, 1.424 + bool aURLShortcut); 1.425 + 1.426 + static nsresult 1.427 + CacheIconFileFromFaviconURIAsync(nsCOMPtr<nsIURI> aFaviconPageURI, 1.428 + nsCOMPtr<nsIFile> aICOFile, 1.429 + nsCOMPtr<nsIThread> &aIOThread, 1.430 + bool aURLShortcut); 1.431 + 1.432 + static int32_t GetICOCacheSecondsTimeout(); 1.433 +}; 1.434 + 1.435 + 1.436 + 1.437 +} // namespace widget 1.438 +} // namespace mozilla 1.439 + 1.440 +#endif // mozilla_widget_WinUtils_h__