Bug 1049521 - Only register for types when there is no default in either of HKLM or HKCU and fix users affected by bad file associations. r=rstrong, a=sledru
authorBrian Bondy <netzen@gmail.com>
Sun, 05 Oct 2014 19:02:51 -0700
changeset 216929 d126cd83b4b8
parent 216928 bbf1c4e2ddce
child 216930 bbc35ec2c90e
push id3974
push userrstrong@mozilla.com
push date2014-10-06 02:08 +0000
treeherdermozilla-beta@d126cd83b4b8 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersrstrong, sledru
bugs1049521
milestone33.0
Bug 1049521 - Only register for types when there is no default in either of HKLM or HKCU and fix users affected by bad file associations. r=rstrong, a=sledru
browser/installer/windows/nsis/shared.nsh
--- a/browser/installer/windows/nsis/shared.nsh
+++ b/browser/installer/windows/nsis/shared.nsh
@@ -391,16 +391,25 @@ FunctionEnd
     ${If} ${FileExists} "$QUICKLAUNCH\${BrandFullName}.lnk"
       ShellLink::SetShortCutWorkingDirectory "$QUICKLAUNCH\${BrandFullName}.lnk" \
                                              "$INSTDIR"
     ${EndIf}
   ${EndUnless}
 !macroend
 !define ShowShortcuts "!insertmacro ShowShortcuts"
 
+!macro AddAssociationIfNoneExist FILE_TYPE
+  ClearErrors
+  EnumRegKey $7 HKCR "${FILE_TYPE}" 0
+  ${If} ${Errors}
+    WriteRegStr SHCTX "SOFTWARE\Classes\${FILE_TYPE}"  "" "FirefoxHTML"
+  ${EndIf}
+!macroend
+!define AddAssociationIfNoneExist "!insertmacro AddAssociationIfNoneExist"
+
 ; Adds the protocol and file handler registry entries for making Firefox the
 ; default handler (uses SHCTX).
 !macro SetHandlers
   ${GetLongPath} "$INSTDIR\${FileMainEXE}" $8
 
   StrCpy $0 "SOFTWARE\Classes"
   StrCpy $2 "$\"$8$\" -osint -url $\"%1$\""
 
@@ -425,45 +434,22 @@ FunctionEnd
     WriteRegStr SHCTX "$0\.xht"   "" "FirefoxHTML"
   ${EndIf}
 
   ReadRegStr $6 SHCTX "$0\.xhtml" ""
   ${If} "$6" != "FirefoxHTML"
     WriteRegStr SHCTX "$0\.xhtml" "" "FirefoxHTML"
   ${EndIf}
 
-  ; Only add .oga if it's not present
-  ${CheckIfRegistryKeyExists} "$0" ".oga" $7
-  ${If} $7 == "false"
-    WriteRegStr SHCTX "$0\.oga"  "" "FirefoxHTML"
-  ${EndIf}
-
-  ; Only add .ogg if it's not present
-  ${CheckIfRegistryKeyExists} "$0" ".ogg" $7
-  ${If} $7 == "false"
-    WriteRegStr SHCTX "$0\.ogg"  "" "FirefoxHTML"
-  ${EndIf}
-
-  ; Only add .ogv if it's not present
-  ${CheckIfRegistryKeyExists} "$0" ".ogv" $7
-  ${If} $7 == "false"
-    WriteRegStr SHCTX "$0\.ogv"  "" "FirefoxHTML"
-  ${EndIf}
-
-  ; Only add .pdf if it's not present
-  ${CheckIfRegistryKeyExists} "$0" ".pdf" $7
-  ${If} $7 == "false"
-    WriteRegStr SHCTX "$0\.pdf"  "" "FirefoxHTML"
-  ${EndIf}
-
-  ; Only add webm if it's not present
-  ${CheckIfRegistryKeyExists} "$0" ".webm" $7
-  ${If} $7 == "false"
-    WriteRegStr SHCTX "$0\.webm"  "" "FirefoxHTML"
-  ${EndIf}
+  ${AddAssociationIfNoneExist} ".pdf"
+  ${AddAssociationIfNoneExist} ".oga"
+  ${AddAssociationIfNoneExist} ".ogg"
+  ${AddAssociationIfNoneExist} ".ogv"
+  ${AddAssociationIfNoneExist} ".pdf"
+  ${AddAssociationIfNoneExist} ".webm"
 
   ; An empty string is used for the 5th param because FirefoxHTML is not a
   ; protocol handler
   ${AddDisabledDDEHandlerValues} "FirefoxHTML" "$2" "$8,1" \
                                  "${AppRegName} HTML Document" ""
 
   ${AddDisabledDDEHandlerValues} "FirefoxURL" "$2" "$8,1" "${AppRegName} URL" \
                                  "true"
@@ -688,16 +674,50 @@ FunctionEnd
       SetShellVarContext all     ; Set SHCTX to all users (e.g. HKLM)
     ${Else}
       SetShellVarContext current  ; Set SHCTX to the current user (e.g. HKCU)
     ${EndIf}
   ${EndIf}
 !macroend
 !define SetUninstallKeys "!insertmacro SetUninstallKeys"
 
+; Due to a bug when associating some file handlers, only SHCTX was checked for
+; some file types such as ".pdf". SHCTX is set to HKCU or HKLM depending on
+; whether the installer has write access to HKLM. The bug would happen when
+; HCKU was checked and didn't exist since programs aren't required to set the
+; HKCU Software\Classes keys when associating handlers. The fix uses the merged
+; view in HKCR to check for existance of an existing association. This macro
+; cleans affected installations by removing the HKLM and HKCU value if it is set
+; to FirefoxHTML when there is a value for PersistentHandler or by removing the
+; HKCU value when the HKLM value has a value other than an empty string.
+!macro FixBadFileAssociation FILE_TYPE
+  ; Only delete the default value in case the key has values for OpenWithList,
+  ; OpenWithProgids, PersistentHandler, etc.
+  ReadRegStr $0 HKCU "Software\Classes\${FILE_TYPE}" ""
+  ReadRegStr $1 HKLM "Software\Classes\${FILE_TYPE}" ""
+  ReadRegStr $2 HKCR "${FILE_TYPE}\PersistentHandler" ""
+  ${If} "$2" != ""
+    ; Since there is a persistent handler remove FirefoxHTML as the default
+    ; value from both HKCU and HKLM if it set to FirefoxHTML.
+    ${If} "$0" == "FirefoxHTML"
+      DeleteRegValue HKCU "Software\Classes\${FILE_TYPE}" ""
+    ${EndIf}
+    ${If} "$1" == "FirefoxHTML"
+      DeleteRegValue HKLM "Software\Classes\${FILE_TYPE}" ""
+    ${EndIf}
+  ${ElseIf} "$0" == "FirefoxHTML"
+    ; Since KHCU is set to FirefoxHTML remove FirefoxHTML as the default value
+    ; from HKCU if HKLM is set to a value other than an empty string.
+    ${If} "$1" != ""
+      DeleteRegValue HKCU "Software\Classes\${FILE_TYPE}" ""
+    ${EndIf}
+  ${EndIf}
+!macroend
+!define FixBadFileAssociation "!insertmacro FixBadFileAssociation"
+
 ; Add app specific handler registry entries under Software\Classes if they
 ; don't exist (does not use SHCTX).
 !macro FixClassKeys
   StrCpy $1 "SOFTWARE\Classes"
 
   ; File handler keys and name value pairs that may need to be created during
   ; install or upgrade.
   ReadRegStr $0 HKCR ".shtml" "Content Type"
@@ -714,16 +734,24 @@ FunctionEnd
     ${WriteRegStr2} $TmpVal "$1\.xht" "Content Type" "application/xhtml+xml" 0
   ${EndIf}
 
   ReadRegStr $0 HKCR ".xhtml" "Content Type"
   ${If} "$0" == ""
     ${WriteRegStr2} $TmpVal "$1\.xhtml" "" "xhtmlfile" 0
     ${WriteRegStr2} $TmpVal "$1\.xhtml" "Content Type" "application/xhtml+xml" 0
   ${EndIf}
+
+  ; Remove possibly badly associated file types
+  ${FixBadFileAssociation} ".pdf"
+  ${FixBadFileAssociation} ".oga"
+  ${FixBadFileAssociation} ".ogg"
+  ${FixBadFileAssociation} ".ogv"
+  ${FixBadFileAssociation} ".pdf"
+  ${FixBadFileAssociation} ".webm"
 !macroend
 !define FixClassKeys "!insertmacro FixClassKeys"
 
 ; Updates protocol handlers if their registry open command value is for this
 ; install location (uses SHCTX).
 !macro UpdateProtocolHandlers
   ; Store the command to open the app with an url in a register for easy access.
   ${GetLongPath} "$INSTDIR\${FileMainEXE}" $8