Bug 1338843 - Use the install path to distinguish between multiple installations when checking default browser status. r=emk
authorMatt Howell <mhowell@mozilla.com>
Mon, 13 Feb 2017 15:36:28 -0800
changeset 343142 ba64203b54e19bdfc588e41fea7cf35dff3bbb60
parent 343141 699380af7093f933dfd0c462f4d8e884b67af709
child 343143 b7a2f7ff5e87ed79ae1d236d593caf4b63a1b5b8
push id31371
push usercbook@mozilla.com
push dateThu, 16 Feb 2017 12:15:11 +0000
treeherdermozilla-central@8c8b54b13be7 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersemk
bugs1338843
milestone54.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 1338843 - Use the install path to distinguish between multiple installations when checking default browser status. r=emk MozReview-Commit-ID: DIcyAs92dm0
browser/components/shell/nsWindowsShellService.cpp
--- a/browser/components/shell/nsWindowsShellService.cpp
+++ b/browser/components/shell/nsWindowsShellService.cpp
@@ -204,33 +204,59 @@ nsWindowsShellService::ShortcutMaintenan
     return NS_ERROR_UNEXPECTED;
 
   appHelperPath.AppendLiteral(" /UpdateShortcutAppUserModelIds");
 
   return LaunchHelper(appHelperPath);
 }
 
 static bool
-IsAARDefault(const RefPtr<IApplicationAssociationRegistration>& pAAR,
-             LPCWSTR aClassName)
+IsPathDefaultForClass(const RefPtr<IApplicationAssociationRegistration>& pAAR,
+                      wchar_t *exePath, LPCWSTR aClassName)
 {
   // Make sure the Prog ID matches what we have
   LPWSTR registeredApp;
   bool isProtocol = *aClassName != L'.';
   ASSOCIATIONTYPE queryType = isProtocol ? AT_URLPROTOCOL : AT_FILEEXTENSION;
   HRESULT hr = pAAR->QueryCurrentDefault(aClassName, queryType, AL_EFFECTIVE,
                                          &registeredApp);
   if (FAILED(hr)) {
     return false;
   }
 
   LPCWSTR progID = isProtocol ? L"FirefoxURL" : L"FirefoxHTML";
   bool isDefault = !wcsnicmp(registeredApp, progID, wcslen(progID));
+
+  nsAutoString regAppName(registeredApp);
   CoTaskMemFree(registeredApp);
 
+  if (isDefault) {
+    // Make sure the application path for this progID is this installation.
+    regAppName.AppendLiteral("\\shell\\open\\command");
+    HKEY theKey;
+    nsresult rv = OpenKeyForReading(HKEY_CLASSES_ROOT, regAppName, &theKey);
+    if (NS_FAILED(rv)) {
+      return false;
+    }
+
+    wchar_t cmdFromReg[MAX_BUF] = L"";
+    DWORD len = sizeof(cmdFromReg);
+    DWORD res = ::RegQueryValueExW(theKey, nullptr, nullptr, nullptr,
+                                   (LPBYTE)cmdFromReg, &len);
+    ::RegCloseKey(theKey);
+    if (REG_FAILED(res)) {
+      return false;
+    }
+
+    wchar_t fullCmd[MAX_BUF] = L"";
+    _snwprintf(fullCmd, MAX_BUF, L"\"%s\" -osint -url \"%%1\"", exePath);
+
+    isDefault = _wcsicmp(fullCmd, cmdFromReg) == 0;
+  }
+
   return isDefault;
 }
 
 static nsresult
 GetAppRegName(nsAutoString &aAppRegName)
 {
   nsresult rv;
   nsCOMPtr<nsIProperties> dirSvc =
@@ -274,19 +300,29 @@ nsWindowsShellService::IsDefaultBrowser(
                                 nullptr,
                                 CLSCTX_INPROC,
                                 IID_IApplicationAssociationRegistration,
                                 getter_AddRefs(pAAR));
   if (FAILED(hr)) {
     return NS_OK;
   }
 
-  *aIsDefaultBrowser = IsAARDefault(pAAR, L"http");
+  wchar_t exePath[MAX_BUF] = L"";
+  if (!::GetModuleFileNameW(0, exePath, MAX_BUF)) {
+    return NS_OK;
+  }
+  // Convert the path to a long path since GetModuleFileNameW returns the path
+  // that was used to launch Firefox which is not necessarily a long path.
+  if (!::GetLongPathNameW(exePath, exePath, MAX_BUF)) {
+    return NS_OK;
+  }
+
+  *aIsDefaultBrowser = IsPathDefaultForClass(pAAR, exePath, L"http");
   if (*aIsDefaultBrowser && aForAllTypes) {
-    *aIsDefaultBrowser = IsAARDefault(pAAR, L".html");
+    *aIsDefaultBrowser = IsPathDefaultForClass(pAAR, exePath, L".html");
   }
   return NS_OK;
 }
 
 nsresult
 nsWindowsShellService::LaunchControlPanelDefaultsSelectionUI()
 {
   IApplicationAssociationRegistrationUI* pAARUI;