Bug 716819 Move nsToolkit::VistaCreateItemFromParsingNameInit() and nsToolkit::createItemFromParsingName to WinUtils r=jimm
authorMasayuki Nakano <masayuki@d-toybox.com>
Wed, 11 Jan 2012 11:09:41 +0900
changeset 85429 f1569a132a83874ff49657fc96527514981c32cc
parent 85428 0e41b25f9a59e108ba6f1b707c0fd79ad97afa29
child 85430 78e4144065ccb92c41672519a2842b49898a67ed
push id805
push userakeybl@mozilla.com
push dateWed, 01 Feb 2012 18:17:35 +0000
treeherdermozilla-aurora@6fb3bf232436 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersjimm
bugs716819
milestone12.0a1
Bug 716819 Move nsToolkit::VistaCreateItemFromParsingNameInit() and nsToolkit::createItemFromParsingName to WinUtils r=jimm
widget/windows/JumpListItem.cpp
widget/windows/WinUtils.cpp
widget/windows/WinUtils.h
widget/windows/nsFilePicker.cpp
widget/windows/nsToolkit.cpp
widget/windows/nsToolkit.h
--- a/widget/windows/JumpListItem.cpp
+++ b/widget/windows/JumpListItem.cpp
@@ -50,17 +50,17 @@
 #include "nsNetUtil.h"
 #include "nsCRT.h"
 #include "nsNetCID.h"
 #include "nsCExternalHandlerService.h"
 #include "nsCycleCollectionParticipant.h"
 #include "mozIAsyncFavicons.h"
 #include "mozilla/Preferences.h"
 #include "JumpListBuilder.h"
-#include "nsToolkit.h"
+#include "WinUtils.h"
 
 namespace mozilla {
 namespace widget {
 
 const char JumpListItem::kJumpListCacheDir[] = "jumpListCache";
 
 // ISUPPORTS Impl's
 NS_IMPL_ISUPPORTS1(JumpListItem,
@@ -733,23 +733,25 @@ nsresult JumpListLink::GetShellItem(nsCO
   rv = link->GetUri(getter_AddRefs(uri));
   NS_ENSURE_SUCCESS(rv, rv);
 
   nsCAutoString spec;
   rv = uri->GetSpec(spec);
   NS_ENSURE_SUCCESS(rv, rv);
 
   // Load vista+ SHCreateItemFromParsingName
-  if (!nsToolkit::VistaCreateItemFromParsingNameInit())
+  if (!WinUtils::VistaCreateItemFromParsingNameInit()) {
     return NS_ERROR_UNEXPECTED;
+  }
 
   // Create the IShellItem
-  if (FAILED(nsToolkit::createItemFromParsingName(NS_ConvertASCIItoUTF16(spec).get(),
-             NULL, IID_PPV_ARGS(&psi))))
+  if (FAILED(WinUtils::SHCreateItemFromParsingName(
+               NS_ConvertASCIItoUTF16(spec).get(), NULL, IID_PPV_ARGS(&psi)))) {
     return NS_ERROR_INVALID_ARG;
+  }
 
   // Set the title
   nsAutoString linkTitle;
   link->GetUriTitle(linkTitle);
 
   IPropertyStore* pPropStore = nsnull;
   HRESULT hres = psi->GetPropertyStore(GPS_DEFAULT, IID_IPropertyStore, (void**)&pPropStore);
   if (FAILED(hres))
--- a/widget/windows/WinUtils.cpp
+++ b/widget/windows/WinUtils.cpp
@@ -60,16 +60,19 @@
 #include "nsWindowDefs.h"
 #include "nsGUIEvent.h"
 #include "nsIDOMMouseEvent.h"
 #include "mozilla/Preferences.h"
 
 namespace mozilla {
 namespace widget {
 
+// SHCreateItemFromParsingName is only available on vista and up.
+WinUtils::SHCreateItemFromParsingNamePtr WinUtils::sCreateItemFromParsingName = nsnull;
+
 /* static */ 
 WinUtils::WinVersion
 WinUtils::GetWindowsVersion()
 {
   static PRInt32 version = 0;
 
   if (version) {
     return static_cast<WinVersion>(version);
@@ -358,10 +361,42 @@ WinUtils::InitMSG(UINT aMessage, WPARAM 
 {
   MSG msg;
   msg.message = aMessage;
   msg.wParam  = wParam;
   msg.lParam  = lParam;
   return msg;
 }
 
+/* static */
+bool
+WinUtils::VistaCreateItemFromParsingNameInit()
+{
+  // Load and store Vista+ SHCreateItemFromParsingName
+  if (sCreateItemFromParsingName) {
+    return true;
+  }
+  static HMODULE sShellDll = nsnull;
+  if (sShellDll) {
+    return false;
+  }
+  static const PRUnichar kSehllLibraryName[] =  L"shell32.dll";
+  sShellDll = ::LoadLibraryW(kSehllLibraryName);
+  if (!sShellDll) {
+    return false;
+  }
+  sCreateItemFromParsingName = (SHCreateItemFromParsingNamePtr)
+    GetProcAddress(sShellDll, "SHCreateItemFromParsingName");
+  return sCreateItemFromParsingName != nsnull;
+}
+
+/* static */
+HRESULT
+WinUtils::SHCreateItemFromParsingName(PCWSTR pszPath, IBindCtx *pbc,
+                                      REFIID riid, void **ppv)
+{
+  NS_ENSURE_TRUE(sCreateItemFromParsingName, E_FAIL);
+  return sCreateItemFromParsingName(pszPath, pbc, riid, ppv);
+}
+
+
 } // namespace widget
 } // namespace mozilla
--- a/widget/windows/WinUtils.h
+++ b/widget/windows/WinUtils.h
@@ -46,16 +46,17 @@
  *
  * ***** END LICENSE BLOCK ***** */
 
 #ifndef mozilla_widget_WinUtils_h__
 #define mozilla_widget_WinUtils_h__
 
 #include "nscore.h"
 #include <windows.h>
+#include <shobjidl.h>
 
 class nsWindow;
 
 namespace mozilla {
 namespace widget {
 
 class WinUtils {
 public:
@@ -193,14 +194,36 @@ public:
   static UINT GetNativeMessage(UINT aInternalMessage);
 
   /**
    * GetMouseInputSource() returns a pointing device information.  The value is
    * one of nsIDOMMouseEvent::MOZ_SOURCE_*.  This method MUST be called during
    * mouse message handling.
    */
   static PRUint16 GetMouseInputSource();
+
+  /**
+   * VistaCreateItemFromParsingNameInit() initializes the static pointer for
+   * SHCreateItemFromParsingName() API which is usable only on Vista and later.
+   * This returns TRUE if the API is available.  Otherwise, FALSE.
+   */
+  static bool VistaCreateItemFromParsingNameInit();
+
+  /**
+   * SHCreateItemFromParsingName() calls native SHCreateItemFromParsingName()
+   * API.  Note that you must call VistaCreateItemFromParsingNameInit() before
+   * calling this.  And the result must be TRUE.  Otherwise, returns E_FAIL.
+   */
+  static HRESULT SHCreateItemFromParsingName(PCWSTR pszPath, IBindCtx *pbc,
+                                             REFIID riid, void **ppv);
+
+private:
+  typedef HRESULT (WINAPI * SHCreateItemFromParsingNamePtr)(PCWSTR pszPath,
+                                                            IBindCtx *pbc,
+                                                            REFIID riid,
+                                                            void **ppv);
+  static SHCreateItemFromParsingNamePtr sCreateItemFromParsingName;
 };
 
 } // namespace widget
 } // namespace mozilla
 
 #endif // mozilla_widget_WinUtils_h__
--- a/widget/windows/nsFilePicker.cpp
+++ b/widget/windows/nsFilePicker.cpp
@@ -614,21 +614,22 @@ nsFilePicker::ShowFolderPicker(const nsS
 
   // options
   FILEOPENDIALOGOPTIONS fos = FOS_PICKFOLDERS;
   dialog->SetOptions(fos);
  
   // initial strings
   dialog->SetTitle(mTitle.get());
   if (!aInitialDir.IsEmpty() &&
-      nsToolkit::VistaCreateItemFromParsingNameInit()) {
+      WinUtils::VistaCreateItemFromParsingNameInit()) {
     nsRefPtr<IShellItem> folder;
-    if (SUCCEEDED(nsToolkit::createItemFromParsingName(aInitialDir.get(), NULL,
-                                                       IID_IShellItem,
-                                                       getter_AddRefs(folder)))) {
+    if (SUCCEEDED(
+          WinUtils::SHCreateItemFromParsingName(aInitialDir.get(), NULL,
+                                                IID_IShellItem,
+                                                getter_AddRefs(folder)))) {
       dialog->SetFolder(folder);
     }
   }
 
   AutoDestroyTmpWindow adtw((HWND)(mParentWidget.get() ?
     mParentWidget->GetNativeData(NS_NATIVE_TMP_WINDOW) : NULL));
  
   // display
@@ -939,21 +940,22 @@ nsFilePicker::ShowFilePicker(const nsStr
   if (!mDefaultExtension.IsEmpty()) {
     dialog->SetDefaultExtension(mDefaultExtension.get());
   } else if (IsDefaultPathHtml()) {
     dialog->SetDefaultExtension(htmExt.get());
   }
 
   // initial location
   if (!aInitialDir.IsEmpty() &&
-      nsToolkit::VistaCreateItemFromParsingNameInit()) {
+      WinUtils::VistaCreateItemFromParsingNameInit()) {
     nsRefPtr<IShellItem> folder;
-    if (SUCCEEDED(nsToolkit::createItemFromParsingName(aInitialDir.get(), NULL,
-                                                       IID_IShellItem,
-                                                       getter_AddRefs(folder)))) {
+    if (SUCCEEDED(
+          WinUtils::SHCreateItemFromParsingName(aInitialDir.get(), NULL,
+                                                IID_IShellItem,
+                                                getter_AddRefs(folder)))) {
       dialog->SetFolder(folder);
     }
   }
 
   // filter types and the default index
   if (!mComFilterList.IsEmpty()) {
     dialog->SetFileTypes(mComFilterList.Length(), mComFilterList.get());
     dialog->SetFileTypeIndex(mSelectedType);
--- a/widget/windows/nsToolkit.cpp
+++ b/widget/windows/nsToolkit.cpp
@@ -52,21 +52,16 @@
 
 // unknwn.h is needed to build with WIN32_LEAN_AND_MEAN
 #include <unknwn.h>
 
 nsToolkit* nsToolkit::gToolkit = nsnull;
 HINSTANCE nsToolkit::mDllInstance = 0;
 static const unsigned long kD3DUsageDelay = 5000;
 
-// SHCreateItemFromParsingName is only available on vista and up.
-nsToolkit::SHCreateItemFromParsingNamePtr nsToolkit::createItemFromParsingName = nsnull;
-const PRUnichar nsToolkit::kSehllLibraryName[] =  L"shell32.dll";
-HMODULE nsToolkit::sShellDll = nsnull;
-
 static void
 StartAllowingD3D9(nsITimer *aTimer, void *aClosure)
 {
   nsWindow::StartAllowingD3D9(true);
 }
 
 MouseTrailer*       nsToolkit::gMouseTrailer;
 
@@ -120,34 +115,16 @@ nsToolkit::Shutdown()
 
 void
 nsToolkit::StartAllowingD3D9()
 {
   nsToolkit::GetToolkit()->mD3D9Timer->Cancel();
   nsWindow::StartAllowingD3D9(false);
 }
 
-// Load and store Vista+ SHCreateItemFromParsingName
-bool
-nsToolkit::VistaCreateItemFromParsingNameInit()
-{
-  if (createItemFromParsingName)
-    return true;
-  if (sShellDll)
-    return false;
-  sShellDll = LoadLibraryW(kSehllLibraryName);
-  if (!sShellDll)
-    return false;
-  createItemFromParsingName = (SHCreateItemFromParsingNamePtr)
-    GetProcAddress(sShellDll, "SHCreateItemFromParsingName");
-  if (createItemFromParsingName == nsnull)
-    return false;
-  return true;
-}
-
 //-------------------------------------------------------------------------
 //
 // Return the nsToolkit for the current thread.  If a toolkit does not
 // yet exist, then one will be created...
 //
 //-------------------------------------------------------------------------
 // static
 nsToolkit* nsToolkit::GetToolkit()
--- a/widget/windows/nsToolkit.h
+++ b/widget/windows/nsToolkit.h
@@ -39,18 +39,16 @@
 #ifndef nsToolkit_h__
 #define nsToolkit_h__
 
 #include "nsdefs.h"
 
 #include "nsITimer.h"
 #include "nsCOMPtr.h"
 #include <windows.h>
-#include <shobjidl.h>
-#include <imm.h>
 
 // Avoid including windowsx.h to prevent macro pollution
 #ifndef GET_X_LPARAM
 #define GET_X_LPARAM(pt) (short(LOWORD(pt)))
 #endif
 #ifndef GET_Y_LPARAM
 #define GET_Y_LPARAM(pt) (short(HIWORD(pt)))
 #endif
@@ -108,23 +106,17 @@ public:
     static nsToolkit* GetToolkit();
 
     static HINSTANCE mDllInstance;
     static MouseTrailer *gMouseTrailer;
 
     static void Startup(HMODULE hModule);
     static void Shutdown();
     static void StartAllowingD3D9();
-    static bool VistaCreateItemFromParsingNameInit();
-
-    typedef HRESULT (WINAPI * SHCreateItemFromParsingNamePtr)(PCWSTR pszPath, IBindCtx *pbc, REFIID riid, void **ppv);
-    static SHCreateItemFromParsingNamePtr createItemFromParsingName;
 
 protected:
     static nsToolkit* gToolkit;
 
     nsCOMPtr<nsITimer> mD3D9Timer;
     MouseTrailer mMouseTrailer;
-    static const PRUnichar kSehllLibraryName[];
-    static HMODULE sShellDll;
 };
 
 #endif  // TOOLKIT_H