widget/windows/WinUtils.h

Thu, 15 Jan 2015 15:59:08 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 15 Jan 2015 15:59:08 +0100
branch
TOR_BUG_9701
changeset 10
ac0c01689b40
permissions
-rw-r--r--

Implement a real Private Browsing Mode condition by changing the API/ABI;
This solves Tor bug #9701, complying with disk avoidance documented in
https://www.torproject.org/projects/torbrowser/design/#disk-avoidance.

michael@0 1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
michael@0 2 /* This Source Code Form is subject to the terms of the Mozilla Public
michael@0 3 * License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0 4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 5
michael@0 6 #ifndef mozilla_widget_WinUtils_h__
michael@0 7 #define mozilla_widget_WinUtils_h__
michael@0 8
michael@0 9 #include "nscore.h"
michael@0 10 #include <windows.h>
michael@0 11 #include <shobjidl.h>
michael@0 12 #include <uxtheme.h>
michael@0 13 #include <dwmapi.h>
michael@0 14 #include "nsAutoPtr.h"
michael@0 15 #include "nsString.h"
michael@0 16 #include "nsRegion.h"
michael@0 17
michael@0 18 #include "nsIRunnable.h"
michael@0 19 #include "nsICryptoHash.h"
michael@0 20 #ifdef MOZ_PLACES
michael@0 21 #include "nsIFaviconService.h"
michael@0 22 #endif
michael@0 23 #include "nsIDownloader.h"
michael@0 24 #include "nsIURI.h"
michael@0 25 #include "nsIWidget.h"
michael@0 26
michael@0 27 #include "mozilla/Attributes.h"
michael@0 28
michael@0 29 class nsWindow;
michael@0 30 class nsWindowBase;
michael@0 31 struct KeyPair;
michael@0 32 struct nsIntRect;
michael@0 33 class nsIThread;
michael@0 34
michael@0 35 namespace mozilla {
michael@0 36 namespace widget {
michael@0 37
michael@0 38 // More complete QS definitions for MsgWaitForMultipleObjects() and
michael@0 39 // GetQueueStatus() that include newer win8 specific defines.
michael@0 40
michael@0 41 #ifndef QS_RAWINPUT
michael@0 42 #define QS_RAWINPUT 0x0400
michael@0 43 #endif
michael@0 44
michael@0 45 #ifndef QS_TOUCH
michael@0 46 #define QS_TOUCH 0x0800
michael@0 47 #define QS_POINTER 0x1000
michael@0 48 #endif
michael@0 49
michael@0 50 #define MOZ_QS_ALLEVENT (QS_KEY | QS_MOUSEMOVE | QS_MOUSEBUTTON | \
michael@0 51 QS_POSTMESSAGE | QS_TIMER | QS_PAINT | \
michael@0 52 QS_SENDMESSAGE | QS_HOTKEY | \
michael@0 53 QS_ALLPOSTMESSAGE | QS_RAWINPUT | \
michael@0 54 QS_TOUCH | QS_POINTER)
michael@0 55
michael@0 56 // Logging macros
michael@0 57 #define LogFunction() mozilla::widget::WinUtils::Log(__FUNCTION__)
michael@0 58 #define LogThread() mozilla::widget::WinUtils::Log("%s: IsMainThread:%d ThreadId:%X", __FUNCTION__, NS_IsMainThread(), GetCurrentThreadId())
michael@0 59 #define LogThis() mozilla::widget::WinUtils::Log("[%X] %s", this, __FUNCTION__)
michael@0 60 #define LogException(e) mozilla::widget::WinUtils::Log("%s Exception:%s", __FUNCTION__, e->ToString()->Data())
michael@0 61 #define LogHRESULT(hr) mozilla::widget::WinUtils::Log("%s hr=%X", __FUNCTION__, hr)
michael@0 62
michael@0 63 class myDownloadObserver MOZ_FINAL : public nsIDownloadObserver
michael@0 64 {
michael@0 65 public:
michael@0 66 NS_DECL_ISUPPORTS
michael@0 67 NS_DECL_NSIDOWNLOADOBSERVER
michael@0 68 };
michael@0 69
michael@0 70 class WinUtils {
michael@0 71 public:
michael@0 72 /**
michael@0 73 * Functions to convert between logical pixels as used by most Windows APIs
michael@0 74 * and physical (device) pixels.
michael@0 75 */
michael@0 76 static double LogToPhysFactor();
michael@0 77 static double PhysToLogFactor();
michael@0 78 static int32_t LogToPhys(double aValue);
michael@0 79 static double PhysToLog(int32_t aValue);
michael@0 80
michael@0 81 /**
michael@0 82 * Logging helpers that dump output to prlog module 'Widget', console, and
michael@0 83 * OutputDebugString. Note these output in both debug and release builds.
michael@0 84 */
michael@0 85 static void Log(const char *fmt, ...);
michael@0 86 static void LogW(const wchar_t *fmt, ...);
michael@0 87
michael@0 88 /**
michael@0 89 * PeekMessage() and GetMessage() are wrapper methods for PeekMessageW(),
michael@0 90 * GetMessageW(), ITfMessageMgr::PeekMessageW() and
michael@0 91 * ITfMessageMgr::GetMessageW().
michael@0 92 * Don't call the native APIs directly. You MUST use these methods instead.
michael@0 93 */
michael@0 94 static bool PeekMessage(LPMSG aMsg, HWND aWnd, UINT aFirstMessage,
michael@0 95 UINT aLastMessage, UINT aOption);
michael@0 96 static bool GetMessage(LPMSG aMsg, HWND aWnd, UINT aFirstMessage,
michael@0 97 UINT aLastMessage);
michael@0 98 /**
michael@0 99 * Gets the value of a string-typed registry value.
michael@0 100 *
michael@0 101 * @param aRoot The registry root to search in.
michael@0 102 * @param aKeyName The name of the registry key to open.
michael@0 103 * @param aValueName The name of the registry value in the specified key whose
michael@0 104 * value is to be retrieved. Can be null, to retrieve the key's unnamed/
michael@0 105 * default value.
michael@0 106 * @param aBuffer The buffer into which to store the string value. Can be
michael@0 107 * null, in which case the return value indicates just whether the value
michael@0 108 * exists.
michael@0 109 * @param aBufferLength The size of aBuffer, in bytes.
michael@0 110 * @return Whether the value exists and is a string.
michael@0 111 */
michael@0 112 static bool GetRegistryKey(HKEY aRoot,
michael@0 113 char16ptr_t aKeyName,
michael@0 114 char16ptr_t aValueName,
michael@0 115 wchar_t* aBuffer,
michael@0 116 DWORD aBufferLength);
michael@0 117
michael@0 118 /**
michael@0 119 * Checks whether the registry key exists in either 32bit or 64bit branch on
michael@0 120 * the environment.
michael@0 121 *
michael@0 122 * @param aRoot The registry root of aName.
michael@0 123 * @param aKeyName The name of the registry key to check.
michael@0 124 * @return TRUE if it exists and is readable. Otherwise, FALSE.
michael@0 125 */
michael@0 126 static bool HasRegistryKey(HKEY aRoot,
michael@0 127 char16ptr_t aKeyName);
michael@0 128
michael@0 129 /**
michael@0 130 * GetTopLevelHWND() returns a window handle of the top level window which
michael@0 131 * aWnd belongs to. Note that the result may not be our window, i.e., it
michael@0 132 * may not be managed by nsWindow.
michael@0 133 *
michael@0 134 * See follwing table for the detail of the result window type.
michael@0 135 *
michael@0 136 * +-------------------------+-----------------------------------------------+
michael@0 137 * | | aStopIfNotPopup |
michael@0 138 * +-------------------------+-----------------------+-----------------------+
michael@0 139 * | | TRUE | FALSE |
michael@0 140 + +-----------------+-------+-----------------------+-----------------------+
michael@0 141 * | | | * an independent top level window |
michael@0 142 * | | TRUE | * a pupup window (WS_POPUP) |
michael@0 143 * | | | * an owned top level window (like dialog) |
michael@0 144 * | aStopIfNotChild +-------+-----------------------+-----------------------+
michael@0 145 * | | | * independent window | * only an independent |
michael@0 146 * | | FALSE | * non-popup-owned- | top level window |
michael@0 147 * | | | window like dialog | |
michael@0 148 * +-----------------+-------+-----------------------+-----------------------+
michael@0 149 */
michael@0 150 static HWND GetTopLevelHWND(HWND aWnd,
michael@0 151 bool aStopIfNotChild = false,
michael@0 152 bool aStopIfNotPopup = true);
michael@0 153
michael@0 154 /**
michael@0 155 * SetNSWindowBasePtr() associates an nsWindowBase to aWnd. If aWidget is
michael@0 156 * nullptr, it dissociate any nsBaseWidget pointer from aWnd.
michael@0 157 * GetNSWindowBasePtr() returns an nsWindowBase pointer which was associated by
michael@0 158 * SetNSWindowBasePtr().
michael@0 159 * GetNSWindowPtr() is a legacy api for win32 nsWindow and should be avoided
michael@0 160 * outside of nsWindow src.
michael@0 161 */
michael@0 162 static bool SetNSWindowBasePtr(HWND aWnd, nsWindowBase* aWidget);
michael@0 163 static nsWindowBase* GetNSWindowBasePtr(HWND aWnd);
michael@0 164 static nsWindow* GetNSWindowPtr(HWND aWnd);
michael@0 165
michael@0 166 /**
michael@0 167 * GetMonitorCount() returns count of monitors on the environment.
michael@0 168 */
michael@0 169 static int32_t GetMonitorCount();
michael@0 170
michael@0 171 /**
michael@0 172 * IsOurProcessWindow() returns TRUE if aWnd belongs our process.
michael@0 173 * Otherwise, FALSE.
michael@0 174 */
michael@0 175 static bool IsOurProcessWindow(HWND aWnd);
michael@0 176
michael@0 177 /**
michael@0 178 * FindOurProcessWindow() returns the nearest ancestor window which
michael@0 179 * belongs to our process. If it fails to find our process's window by the
michael@0 180 * top level window, returns nullptr. And note that this is using
michael@0 181 * ::GetParent() for climbing the window hierarchy, therefore, it gives
michael@0 182 * up at an owned top level window except popup window (e.g., dialog).
michael@0 183 */
michael@0 184 static HWND FindOurProcessWindow(HWND aWnd);
michael@0 185
michael@0 186 /**
michael@0 187 * FindOurWindowAtPoint() returns the topmost child window which belongs to
michael@0 188 * our process's top level window.
michael@0 189 *
michael@0 190 * NOTE: the topmost child window may NOT be our process's window like a
michael@0 191 * plugin's window.
michael@0 192 */
michael@0 193 static HWND FindOurWindowAtPoint(const POINT& aPointInScreen);
michael@0 194
michael@0 195 /**
michael@0 196 * InitMSG() returns an MSG struct which was initialized by the params.
michael@0 197 * Don't trust the other members in the result.
michael@0 198 */
michael@0 199 static MSG InitMSG(UINT aMessage, WPARAM wParam, LPARAM lParam, HWND aWnd);
michael@0 200
michael@0 201 /**
michael@0 202 * GetScanCode() returns a scan code for the LPARAM of WM_KEYDOWN, WM_KEYUP,
michael@0 203 * WM_CHAR and WM_UNICHAR.
michael@0 204 *
michael@0 205 */
michael@0 206 static WORD GetScanCode(LPARAM aLParam)
michael@0 207 {
michael@0 208 return (aLParam >> 16) & 0xFF;
michael@0 209 }
michael@0 210
michael@0 211 /**
michael@0 212 * IsExtendedScanCode() returns TRUE if the LPARAM indicates the key message
michael@0 213 * is an extended key event.
michael@0 214 */
michael@0 215 static bool IsExtendedScanCode(LPARAM aLParam)
michael@0 216 {
michael@0 217 return (aLParam & 0x1000000) != 0;
michael@0 218 }
michael@0 219
michael@0 220 /**
michael@0 221 * GetInternalMessage() converts a native message to an internal message.
michael@0 222 * If there is no internal message for the given native message, returns
michael@0 223 * the native message itself.
michael@0 224 */
michael@0 225 static UINT GetInternalMessage(UINT aNativeMessage);
michael@0 226
michael@0 227 /**
michael@0 228 * GetNativeMessage() converts an internal message to a native message.
michael@0 229 * If aInternalMessage is a native message, returns the native message itself.
michael@0 230 */
michael@0 231 static UINT GetNativeMessage(UINT aInternalMessage);
michael@0 232
michael@0 233 /**
michael@0 234 * GetMouseInputSource() returns a pointing device information. The value is
michael@0 235 * one of nsIDOMMouseEvent::MOZ_SOURCE_*. This method MUST be called during
michael@0 236 * mouse message handling.
michael@0 237 */
michael@0 238 static uint16_t GetMouseInputSource();
michael@0 239
michael@0 240 static bool GetIsMouseFromTouch(uint32_t aEventType);
michael@0 241
michael@0 242 /**
michael@0 243 * SHCreateItemFromParsingName() calls native SHCreateItemFromParsingName()
michael@0 244 * API which is available on Vista and up.
michael@0 245 */
michael@0 246 static HRESULT SHCreateItemFromParsingName(PCWSTR pszPath, IBindCtx *pbc,
michael@0 247 REFIID riid, void **ppv);
michael@0 248
michael@0 249 /**
michael@0 250 * SHGetKnownFolderPath() calls native SHGetKnownFolderPath()
michael@0 251 * API which is available on Vista and up.
michael@0 252 */
michael@0 253 static HRESULT SHGetKnownFolderPath(REFKNOWNFOLDERID rfid,
michael@0 254 DWORD dwFlags,
michael@0 255 HANDLE hToken,
michael@0 256 PWSTR *ppszPath);
michael@0 257 /**
michael@0 258 * GetShellItemPath return the file or directory path of a shell item.
michael@0 259 * Internally calls IShellItem's GetDisplayName.
michael@0 260 *
michael@0 261 * aItem the shell item containing the path.
michael@0 262 * aResultString the resulting string path.
michael@0 263 * returns true if a path was retreived.
michael@0 264 */
michael@0 265 static bool GetShellItemPath(IShellItem* aItem,
michael@0 266 nsString& aResultString);
michael@0 267
michael@0 268 /**
michael@0 269 * ConvertHRGNToRegion converts a Windows HRGN to an nsIntRegion.
michael@0 270 *
michael@0 271 * aRgn the HRGN to convert.
michael@0 272 * returns the nsIntRegion.
michael@0 273 */
michael@0 274 static nsIntRegion ConvertHRGNToRegion(HRGN aRgn);
michael@0 275
michael@0 276 /**
michael@0 277 * ToIntRect converts a Windows RECT to a nsIntRect.
michael@0 278 *
michael@0 279 * aRect the RECT to convert.
michael@0 280 * returns the nsIntRect.
michael@0 281 */
michael@0 282 static nsIntRect ToIntRect(const RECT& aRect);
michael@0 283
michael@0 284 /**
michael@0 285 * Returns true if the context or IME state is enabled. Otherwise, false.
michael@0 286 */
michael@0 287 static bool IsIMEEnabled(const InputContext& aInputContext);
michael@0 288 static bool IsIMEEnabled(IMEState::Enabled aIMEState);
michael@0 289
michael@0 290 /**
michael@0 291 * Returns modifier key array for aModifiers. This is for
michael@0 292 * nsIWidget::SynthethizeNative*Event().
michael@0 293 */
michael@0 294 static void SetupKeyModifiersSequence(nsTArray<KeyPair>* aArray,
michael@0 295 uint32_t aModifiers);
michael@0 296
michael@0 297 // dwmapi.dll function typedefs and declarations
michael@0 298 typedef HRESULT (WINAPI*DwmExtendFrameIntoClientAreaProc)(HWND hWnd, const MARGINS *pMarInset);
michael@0 299 typedef HRESULT (WINAPI*DwmIsCompositionEnabledProc)(BOOL *pfEnabled);
michael@0 300 typedef HRESULT (WINAPI*DwmSetIconicThumbnailProc)(HWND hWnd, HBITMAP hBitmap, DWORD dwSITFlags);
michael@0 301 typedef HRESULT (WINAPI*DwmSetIconicLivePreviewBitmapProc)(HWND hWnd, HBITMAP hBitmap, POINT *pptClient, DWORD dwSITFlags);
michael@0 302 typedef HRESULT (WINAPI*DwmGetWindowAttributeProc)(HWND hWnd, DWORD dwAttribute, LPCVOID pvAttribute, DWORD cbAttribute);
michael@0 303 typedef HRESULT (WINAPI*DwmSetWindowAttributeProc)(HWND hWnd, DWORD dwAttribute, LPCVOID pvAttribute, DWORD cbAttribute);
michael@0 304 typedef HRESULT (WINAPI*DwmInvalidateIconicBitmapsProc)(HWND hWnd);
michael@0 305 typedef HRESULT (WINAPI*DwmDefWindowProcProc)(HWND hWnd, UINT msg, LPARAM lParam, WPARAM wParam, LRESULT *aRetValue);
michael@0 306 typedef HRESULT (WINAPI*DwmGetCompositionTimingInfoProc)(HWND hWnd, DWM_TIMING_INFO *info);
michael@0 307
michael@0 308 static DwmExtendFrameIntoClientAreaProc dwmExtendFrameIntoClientAreaPtr;
michael@0 309 static DwmIsCompositionEnabledProc dwmIsCompositionEnabledPtr;
michael@0 310 static DwmSetIconicThumbnailProc dwmSetIconicThumbnailPtr;
michael@0 311 static DwmSetIconicLivePreviewBitmapProc dwmSetIconicLivePreviewBitmapPtr;
michael@0 312 static DwmGetWindowAttributeProc dwmGetWindowAttributePtr;
michael@0 313 static DwmSetWindowAttributeProc dwmSetWindowAttributePtr;
michael@0 314 static DwmInvalidateIconicBitmapsProc dwmInvalidateIconicBitmapsPtr;
michael@0 315 static DwmDefWindowProcProc dwmDwmDefWindowProcPtr;
michael@0 316 static DwmGetCompositionTimingInfoProc dwmGetCompositionTimingInfoPtr;
michael@0 317
michael@0 318 static void Initialize();
michael@0 319
michael@0 320 static bool ShouldHideScrollbars();
michael@0 321
michael@0 322 private:
michael@0 323 typedef HRESULT (WINAPI * SHCreateItemFromParsingNamePtr)(PCWSTR pszPath,
michael@0 324 IBindCtx *pbc,
michael@0 325 REFIID riid,
michael@0 326 void **ppv);
michael@0 327 static SHCreateItemFromParsingNamePtr sCreateItemFromParsingName;
michael@0 328 typedef HRESULT (WINAPI * SHGetKnownFolderPathPtr)(REFKNOWNFOLDERID rfid,
michael@0 329 DWORD dwFlags,
michael@0 330 HANDLE hToken,
michael@0 331 PWSTR *ppszPath);
michael@0 332 static SHGetKnownFolderPathPtr sGetKnownFolderPath;
michael@0 333 };
michael@0 334
michael@0 335 #ifdef MOZ_PLACES
michael@0 336 class AsyncFaviconDataReady MOZ_FINAL : public nsIFaviconDataCallback
michael@0 337 {
michael@0 338 public:
michael@0 339 NS_DECL_ISUPPORTS
michael@0 340 NS_DECL_NSIFAVICONDATACALLBACK
michael@0 341
michael@0 342 AsyncFaviconDataReady(nsIURI *aNewURI,
michael@0 343 nsCOMPtr<nsIThread> &aIOThread,
michael@0 344 const bool aURLShortcut);
michael@0 345 nsresult OnFaviconDataNotAvailable(void);
michael@0 346 private:
michael@0 347 nsCOMPtr<nsIURI> mNewURI;
michael@0 348 nsCOMPtr<nsIThread> mIOThread;
michael@0 349 const bool mURLShortcut;
michael@0 350 };
michael@0 351 #endif
michael@0 352
michael@0 353 /**
michael@0 354 * Asynchronously tries add the list to the build
michael@0 355 */
michael@0 356 class AsyncEncodeAndWriteIcon : public nsIRunnable
michael@0 357 {
michael@0 358 public:
michael@0 359 const bool mURLShortcut;
michael@0 360 NS_DECL_THREADSAFE_ISUPPORTS
michael@0 361 NS_DECL_NSIRUNNABLE
michael@0 362
michael@0 363 // Warning: AsyncEncodeAndWriteIcon assumes ownership of the aData buffer passed in
michael@0 364 AsyncEncodeAndWriteIcon(const nsAString &aIconPath,
michael@0 365 uint8_t *aData, uint32_t aDataLen, uint32_t aStride,
michael@0 366 uint32_t aWidth, uint32_t aHeight,
michael@0 367 const bool aURLShortcut);
michael@0 368 virtual ~AsyncEncodeAndWriteIcon();
michael@0 369
michael@0 370 private:
michael@0 371 nsAutoString mIconPath;
michael@0 372 nsAutoCString mMimeTypeOfInputData;
michael@0 373 nsAutoArrayPtr<uint8_t> mBuffer;
michael@0 374 HMODULE sDwmDLL;
michael@0 375 uint32_t mBufferLength;
michael@0 376 uint32_t mStride;
michael@0 377 uint32_t mWidth;
michael@0 378 uint32_t mHeight;
michael@0 379 };
michael@0 380
michael@0 381
michael@0 382 class AsyncDeleteIconFromDisk : public nsIRunnable
michael@0 383 {
michael@0 384 public:
michael@0 385 NS_DECL_THREADSAFE_ISUPPORTS
michael@0 386 NS_DECL_NSIRUNNABLE
michael@0 387
michael@0 388 AsyncDeleteIconFromDisk(const nsAString &aIconPath);
michael@0 389 virtual ~AsyncDeleteIconFromDisk();
michael@0 390
michael@0 391 private:
michael@0 392 nsAutoString mIconPath;
michael@0 393 };
michael@0 394
michael@0 395 class AsyncDeleteAllFaviconsFromDisk : public nsIRunnable
michael@0 396 {
michael@0 397 public:
michael@0 398 NS_DECL_THREADSAFE_ISUPPORTS
michael@0 399 NS_DECL_NSIRUNNABLE
michael@0 400
michael@0 401 AsyncDeleteAllFaviconsFromDisk();
michael@0 402 virtual ~AsyncDeleteAllFaviconsFromDisk();
michael@0 403 };
michael@0 404
michael@0 405 class FaviconHelper
michael@0 406 {
michael@0 407 public:
michael@0 408 static const char kJumpListCacheDir[];
michael@0 409 static const char kShortcutCacheDir[];
michael@0 410 static nsresult ObtainCachedIconFile(nsCOMPtr<nsIURI> aFaviconPageURI,
michael@0 411 nsString &aICOFilePath,
michael@0 412 nsCOMPtr<nsIThread> &aIOThread,
michael@0 413 bool aURLShortcut);
michael@0 414
michael@0 415 static nsresult HashURI(nsCOMPtr<nsICryptoHash> &aCryptoHash,
michael@0 416 nsIURI *aUri,
michael@0 417 nsACString& aUriHash);
michael@0 418
michael@0 419 static nsresult GetOutputIconPath(nsCOMPtr<nsIURI> aFaviconPageURI,
michael@0 420 nsCOMPtr<nsIFile> &aICOFile,
michael@0 421 bool aURLShortcut);
michael@0 422
michael@0 423 static nsresult
michael@0 424 CacheIconFileFromFaviconURIAsync(nsCOMPtr<nsIURI> aFaviconPageURI,
michael@0 425 nsCOMPtr<nsIFile> aICOFile,
michael@0 426 nsCOMPtr<nsIThread> &aIOThread,
michael@0 427 bool aURLShortcut);
michael@0 428
michael@0 429 static int32_t GetICOCacheSecondsTimeout();
michael@0 430 };
michael@0 431
michael@0 432
michael@0 433
michael@0 434 } // namespace widget
michael@0 435 } // namespace mozilla
michael@0 436
michael@0 437 #endif // mozilla_widget_WinUtils_h__

mercurial