Bug 867104 - Add a crashtest. r=ehsan
[gecko.git] / widget / windows / WinUtils.h
blobb244cfe7b9d2bed06f56b7d60e7bfa386a9268da
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 "nsAutoPtr.h"
13 #include "nsString.h"
14 #include "nsRegion.h"
15 #include "nsRect.h"
17 #include "nsThreadUtils.h"
18 #include "nsICryptoHash.h"
19 #ifdef MOZ_PLACES
20 #include "nsIFaviconService.h"
21 #endif
22 #include "nsIDownloader.h"
23 #include "nsIURI.h"
25 #include "mozilla/Attributes.h"
27 class nsWindow;
29 namespace mozilla {
30 namespace widget {
32 class myDownloadObserver MOZ_FINAL : public nsIDownloadObserver
34 public:
35 NS_DECL_ISUPPORTS
36 NS_DECL_NSIDOWNLOADOBSERVER
39 class WinUtils {
40 public:
41 enum WinVersion {
42 WINXP_VERSION = 0x501,
43 WIN2K3_VERSION = 0x502,
44 VISTA_VERSION = 0x600,
45 // WIN2K8_VERSION = VISTA_VERSION,
46 WIN7_VERSION = 0x601,
47 // WIN2K8R2_VERSION = WIN7_VERSION
48 WIN8_VERSION = 0x602
50 static WinVersion GetWindowsVersion();
52 // Retrieves the Service Pack version number.
53 // Returns true on success, false on failure.
54 static bool GetWindowsServicePackVersion(UINT& aOutMajor, UINT& aOutMinor);
56 /**
57 * PeekMessage() and GetMessage() are wrapper methods for PeekMessageW(),
58 * GetMessageW(), ITfMessageMgr::PeekMessageW() and
59 * ITfMessageMgr::GetMessageW().
60 * Don't call the native APIs directly. You MUST use these methods instead.
62 static bool PeekMessage(LPMSG aMsg, HWND aWnd, UINT aFirstMessage,
63 UINT aLastMessage, UINT aOption);
64 static bool GetMessage(LPMSG aMsg, HWND aWnd, UINT aFirstMessage,
65 UINT aLastMessage);
66 /**
67 * Gets the value of a string-typed registry value.
69 * @param aRoot The registry root to search in.
70 * @param aKeyName The name of the registry key to open.
71 * @param aValueName The name of the registry value in the specified key whose
72 * value is to be retrieved. Can be null, to retrieve the key's unnamed/
73 * default value.
74 * @param aBuffer The buffer into which to store the string value. Can be
75 * null, in which case the return value indicates just whether the value
76 * exists.
77 * @param aBufferLength The size of aBuffer, in bytes.
78 * @return Whether the value exists and is a string.
80 static bool GetRegistryKey(HKEY aRoot,
81 const PRUnichar* aKeyName,
82 const PRUnichar* aValueName,
83 PRUnichar* aBuffer,
84 DWORD aBufferLength);
86 /**
87 * Checks whether the registry key exists in either 32bit or 64bit branch on
88 * the environment.
90 * @param aRoot The registry root of aName.
91 * @param aKeyName The name of the registry key to check.
92 * @return TRUE if it exists and is readable. Otherwise, FALSE.
94 static bool HasRegistryKey(HKEY aRoot,
95 const PRUnichar* aKeyName);
97 /**
98 * GetTopLevelHWND() returns a window handle of the top level window which
99 * aWnd belongs to. Note that the result may not be our window, i.e., it
100 * may not be managed by nsWindow.
102 * See follwing table for the detail of the result window type.
104 * +-------------------------+-----------------------------------------------+
105 * | | aStopIfNotPopup |
106 * +-------------------------+-----------------------+-----------------------+
107 * | | TRUE | FALSE |
108 + +-----------------+-------+-----------------------+-----------------------+
109 * | | | * an independent top level window |
110 * | | TRUE | * a pupup window (WS_POPUP) |
111 * | | | * an owned top level window (like dialog) |
112 * | aStopIfNotChild +-------+-----------------------+-----------------------+
113 * | | | * independent window | * only an independent |
114 * | | FALSE | * non-popup-owned- | top level window |
115 * | | | window like dialog | |
116 * +-----------------+-------+-----------------------+-----------------------+
118 static HWND GetTopLevelHWND(HWND aWnd,
119 bool aStopIfNotChild = false,
120 bool aStopIfNotPopup = true);
123 * SetNSWindowPtr() associates an nsWindow to aWnd. If aWindow is NULL,
124 * it dissociate any nsWindow pointer from aWnd.
125 * GetNSWindowPtr() returns an nsWindow pointer which was associated by
126 * SetNSWindowPtr().
128 static bool SetNSWindowPtr(HWND aWnd, nsWindow* aWindow);
129 static nsWindow* GetNSWindowPtr(HWND aWnd);
132 * GetMonitorCount() returns count of monitors on the environment.
134 static int32_t GetMonitorCount();
137 * IsOurProcessWindow() returns TRUE if aWnd belongs our process.
138 * Otherwise, FALSE.
140 static bool IsOurProcessWindow(HWND aWnd);
143 * FindOurProcessWindow() returns the nearest ancestor window which
144 * belongs to our process. If it fails to find our process's window by the
145 * top level window, returns NULL. And note that this is using ::GetParent()
146 * for climbing the window hierarchy, therefore, it gives up at an owned top
147 * level window except popup window (e.g., dialog).
149 static HWND FindOurProcessWindow(HWND aWnd);
152 * FindOurWindowAtPoint() returns the topmost child window which belongs to
153 * our process's top level window.
155 * NOTE: the topmost child window may NOT be our process's window like a
156 * plugin's window.
158 static HWND FindOurWindowAtPoint(const POINT& aPointInScreen);
161 * InitMSG() returns an MSG struct which was initialized by the params.
162 * Don't trust the other members in the result.
164 static MSG InitMSG(UINT aMessage, WPARAM wParam, LPARAM lParam);
167 * GetScanCode() returns a scan code for the LPARAM of WM_KEYDOWN, WM_KEYUP,
168 * WM_CHAR and WM_UNICHAR.
171 static WORD GetScanCode(LPARAM aLParam)
173 return (aLParam >> 16) & 0xFF;
177 * IsExtendedScanCode() returns TRUE if the LPARAM indicates the key message
178 * is an extended key event.
180 static bool IsExtendedScanCode(LPARAM aLParam)
182 return (aLParam & 0x1000000) != 0;
186 * GetInternalMessage() converts a native message to an internal message.
187 * If there is no internal message for the given native message, returns
188 * the native message itself.
190 static UINT GetInternalMessage(UINT aNativeMessage);
193 * GetNativeMessage() converts an internal message to a native message.
194 * If aInternalMessage is a native message, returns the native message itself.
196 static UINT GetNativeMessage(UINT aInternalMessage);
199 * GetMouseInputSource() returns a pointing device information. The value is
200 * one of nsIDOMMouseEvent::MOZ_SOURCE_*. This method MUST be called during
201 * mouse message handling.
203 static uint16_t GetMouseInputSource();
206 * SHCreateItemFromParsingName() calls native SHCreateItemFromParsingName()
207 * API which is available on Vista and up.
209 static HRESULT SHCreateItemFromParsingName(PCWSTR pszPath, IBindCtx *pbc,
210 REFIID riid, void **ppv);
213 * SHGetKnownFolderPath() calls native SHGetKnownFolderPath()
214 * API which is available on Vista and up.
216 static HRESULT SHGetKnownFolderPath(REFKNOWNFOLDERID rfid,
217 DWORD dwFlags,
218 HANDLE hToken,
219 PWSTR *ppszPath);
221 * GetShellItemPath return the file or directory path of a shell item.
222 * Internally calls IShellItem's GetDisplayName.
224 * aItem the shell item containing the path.
225 * aResultString the resulting string path.
226 * returns true if a path was retreived.
228 static bool GetShellItemPath(IShellItem* aItem,
229 nsString& aResultString);
232 * ConvertHRGNToRegion converts a Windows HRGN to an nsIntRegion.
234 * aRgn the HRGN to convert.
235 * returns the nsIntRegion.
237 static nsIntRegion ConvertHRGNToRegion(HRGN aRgn);
240 * ToIntRect converts a Windows RECT to a nsIntRect.
242 * aRect the RECT to convert.
243 * returns the nsIntRect.
245 static nsIntRect ToIntRect(const RECT& aRect);
247 private:
248 typedef HRESULT (WINAPI * SHCreateItemFromParsingNamePtr)(PCWSTR pszPath,
249 IBindCtx *pbc,
250 REFIID riid,
251 void **ppv);
252 static SHCreateItemFromParsingNamePtr sCreateItemFromParsingName;
253 typedef HRESULT (WINAPI * SHGetKnownFolderPathPtr)(REFKNOWNFOLDERID rfid,
254 DWORD dwFlags,
255 HANDLE hToken,
256 PWSTR *ppszPath);
257 static SHGetKnownFolderPathPtr sGetKnownFolderPath;
260 #ifdef MOZ_PLACES
261 class AsyncFaviconDataReady MOZ_FINAL : public nsIFaviconDataCallback
263 public:
264 NS_DECL_ISUPPORTS
265 NS_DECL_NSIFAVICONDATACALLBACK
267 AsyncFaviconDataReady(nsIURI *aNewURI,
268 nsCOMPtr<nsIThread> &aIOThread,
269 const bool aURLShortcut);
270 nsresult OnFaviconDataNotAvailable(void);
271 private:
272 nsCOMPtr<nsIURI> mNewURI;
273 nsCOMPtr<nsIThread> mIOThread;
274 const bool mURLShortcut;
276 #endif
279 * Asynchronously tries add the list to the build
281 class AsyncWriteIconToDisk : public nsIRunnable
283 public:
284 const bool mURLShortcut;
285 NS_DECL_ISUPPORTS
286 NS_DECL_NSIRUNNABLE
288 // Warning: AsyncWriteIconToDisk assumes ownership of the aData buffer passed in
289 AsyncWriteIconToDisk(const nsAString &aIconPath,
290 const nsACString &aMimeTypeOfInputData,
291 uint8_t *aData,
292 uint32_t aDataLen,
293 const bool aURLShortcut);
294 virtual ~AsyncWriteIconToDisk();
296 private:
297 nsAutoString mIconPath;
298 nsAutoCString mMimeTypeOfInputData;
299 nsAutoArrayPtr<uint8_t> mBuffer;
300 uint32_t mBufferLength;
303 class AsyncDeleteIconFromDisk : public nsIRunnable
305 public:
306 NS_DECL_ISUPPORTS
307 NS_DECL_NSIRUNNABLE
309 AsyncDeleteIconFromDisk(const nsAString &aIconPath);
310 virtual ~AsyncDeleteIconFromDisk();
312 private:
313 nsAutoString mIconPath;
316 class AsyncDeleteAllFaviconsFromDisk : public nsIRunnable
318 public:
319 NS_DECL_ISUPPORTS
320 NS_DECL_NSIRUNNABLE
322 AsyncDeleteAllFaviconsFromDisk();
323 virtual ~AsyncDeleteAllFaviconsFromDisk();
326 class FaviconHelper
328 public:
329 static const char kJumpListCacheDir[];
330 static const char kShortcutCacheDir[];
331 static nsresult ObtainCachedIconFile(nsCOMPtr<nsIURI> aFaviconPageURI,
332 nsString &aICOFilePath,
333 nsCOMPtr<nsIThread> &aIOThread,
334 bool aURLShortcut);
336 static nsresult HashURI(nsCOMPtr<nsICryptoHash> &aCryptoHash,
337 nsIURI *aUri,
338 nsACString& aUriHash);
340 static nsresult GetOutputIconPath(nsCOMPtr<nsIURI> aFaviconPageURI,
341 nsCOMPtr<nsIFile> &aICOFile,
342 bool aURLShortcut);
344 static nsresult
345 CacheIconFileFromFaviconURIAsync(nsCOMPtr<nsIURI> aFaviconPageURI,
346 nsCOMPtr<nsIFile> aICOFile,
347 nsCOMPtr<nsIThread> &aIOThread,
348 bool aURLShortcut);
350 static int32_t GetICOCacheSecondsTimeout();
355 } // namespace widget
356 } // namespace mozilla
358 #endif // mozilla_widget_WinUtils_h__