widget/windows/WinUtils.h

Thu, 22 Jan 2015 13:21:57 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 22 Jan 2015 13:21:57 +0100
branch
TOR_BUG_9701
changeset 15
b8a032363ba2
permissions
-rw-r--r--

Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6

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

mercurial