235380 - firefox should set itself as default browser only for the current user. Change Windows Shell Service to add an additional option - whether to set default browser for all users or just the current user. Also make the code read out of HKCU instead of HKLM.

This commit is contained in:
ben%bengoodger.com 2004-06-30 20:27:30 +00:00
Родитель 3745250f67
Коммит 1dfa27baee
9 изменённых файлов: 92 добавлений и 48 удалений

Просмотреть файл

@ -535,7 +535,7 @@ function delayedStartup()
(IPS.BUTTON_TITLE_NO * IPS.BUTTON_POS_1), (IPS.BUTTON_TITLE_NO * IPS.BUTTON_POS_1),
null, null, null, checkboxLabel, checkEveryTime); null, null, null, checkboxLabel, checkEveryTime);
if (rv == 0) if (rv == 0)
shell.setDefaultBrowser(true); shell.setDefaultBrowser(true, false);
shell.shouldCheckDefaultBrowser = checkEveryTime.value; shell.shouldCheckDefaultBrowser = checkEveryTime.value;
} }
#endif #endif

Просмотреть файл

@ -102,7 +102,7 @@ function onOK()
var shell = Components.classes["@mozilla.org/browser/shell-service;1"] var shell = Components.classes["@mozilla.org/browser/shell-service;1"]
.getService(Components.interfaces.nsIShellService); .getService(Components.interfaces.nsIShellService);
if ("shouldBeDefaultBrowser" in parent && parent.shouldBeDefautBrowser) if ("shouldBeDefaultBrowser" in parent && parent.shouldBeDefautBrowser)
shell.setDefaultBrowser(true); shell.setDefaultBrowser(true, false);
} }
function Startup() function Startup()
@ -230,7 +230,7 @@ function checkNow()
(IPS.BUTTON_TITLE_NO * IPS.BUTTON_POS_1), (IPS.BUTTON_TITLE_NO * IPS.BUTTON_POS_1),
null, null, null, null, { }); null, null, null, null, { });
if (rv == 0) if (rv == 0)
shell.setDefaultBrowser(true); shell.setDefaultBrowser(true, false);
} }
else { else {
promptMessage = shellBundle.getFormattedString("alreadyDefaultBrowser", promptMessage = shellBundle.getFormattedString("alreadyDefaultBrowser",

Просмотреть файл

@ -59,8 +59,11 @@ interface nsIShellService : nsISupports
* @param aClaimAllTypes Register Firefox as the handler for * @param aClaimAllTypes Register Firefox as the handler for
* additional protocols (ftp, chrome etc) * additional protocols (ftp, chrome etc)
* and web documents (.html, .xhtml etc). * and web documents (.html, .xhtml etc).
* @param aForAllUsers Whether or not Firefox should attempt
* to become the default browser for all
* users on a multi-user system.
*/ */
void setDefaultBrowser(in boolean aClaimAllTypes); void setDefaultBrowser(in boolean aClaimAllTypes, in boolean aForAllUsers);
/** /**
* Used to determine whether or not to show a "Set Default Browser" * Used to determine whether or not to show a "Set Default Browser"

Просмотреть файл

@ -43,8 +43,12 @@ interface nsIWindowsShellService : nsIShellService
/** /**
* Restores system settings to what they were before Firefox * Restores system settings to what they were before Firefox
* modified them. * modified them.
*
* @param aForAllUsers Whether or not Firefox should restore
* settings for all users on a multi-user
* system.
*/ */
void restoreFileSettings(); void restoreFileSettings(in boolean aForAllUsers);
/** /**
* The desktop background color, visible when no background image is * The desktop background color, visible when no background image is

Просмотреть файл

@ -49,7 +49,7 @@ nsGNOMEShellService::IsDefaultBrowser(PRBool aStartupCheck, PRBool* aIsDefaultBr
} }
NS_IMETHODIMP NS_IMETHODIMP
nsGNOMEShellService::SetDefaultBrowser(PRBool aClaimAllTypes) nsGNOMEShellService::SetDefaultBrowser(PRBool aClaimAllTypes, PRBool aForAllUsers)
{ {
return NS_OK; return NS_OK;
} }

Просмотреть файл

@ -49,7 +49,7 @@ nsMacShellService::IsDefaultBrowser(PRBool aStartupCheck, PRBool* aIsDefaultBrow
} }
NS_IMETHODIMP NS_IMETHODIMP
nsMacShellService::SetDefaultBrowser(PRBool aClaimAllTypes) nsMacShellService::SetDefaultBrowser(PRBool aClaimAllTypes, PRBool aForAllUsers)
{ {
return NS_OK; return NS_OK;
} }

Просмотреть файл

@ -70,7 +70,7 @@ nsSetDefaultBrowser.prototype = {
// First, get winhooks service. // First, get winhooks service.
var shell = Components.classes["@mozilla.org/browser/shell-service;1"] var shell = Components.classes["@mozilla.org/browser/shell-service;1"]
.getService(Components.interfaces.nsIShellService); .getService(Components.interfaces.nsIShellService);
shell.isDefaultBrowser = true; shell.setDefaultBrowser(true, true);
// Now, get the cmd line service. // Now, get the cmd line service.
var cmdLineService = Components.classes[ "@mozilla.org/appshell/commandLineService;1" ] var cmdLineService = Components.classes[ "@mozilla.org/appshell/commandLineService;1" ]

Просмотреть файл

@ -71,6 +71,49 @@
NS_IMPL_ISUPPORTS2(nsWindowsShellService, nsIWindowsShellService, nsIShellService) NS_IMPL_ISUPPORTS2(nsWindowsShellService, nsIWindowsShellService, nsIShellService)
static nsresult
OpenUserKeyForReading(HKEY aStartKey, const char* aKeyName, HKEY* aKey)
{
DWORD result = ::RegOpenKeyEx(aStartKey, aKeyName, 0, KEY_READ, aKey);
switch (result) {
case ERROR_SUCCESS:
break;
case ERROR_ACCESS_DENIED:
return NS_ERROR_FILE_ACCESS_DENIED;
case ERROR_FILE_NOT_FOUND:
if (aStartKey == HKEY_LOCAL_MACHINE) {
// prevent infinite recursion on the second pass through here if
// ::RegOpenKeyEx fails in the all-users case.
return NS_ERROR_NOT_AVAILABLE;
}
return OpenUserKeyForReading(HKEY_LOCAL_MACHINE, aKeyName, aKey);
}
return NS_OK;
}
static nsresult
OpenKeyForWriting(const char* aKeyName, HKEY* aKey, PRBool aForAllUsers, PRBool aCreate)
{
nsresult rv = NS_OK;
HKEY rootKey = aForAllUsers ? HKEY_LOCAL_MACHINE : HKEY_CURRENT_USER;
DWORD result = ::RegOpenKeyEx(rootKey, aKeyName, 0, KEY_READ | KEY_WRITE, aKey);
switch (result) {
case ERROR_SUCCESS:
break;
case ERROR_ACCESS_DENIED:
return NS_ERROR_FILE_ACCESS_DENIED;
case ERROR_FILE_NOT_FOUND:
if (aCreate)
result = ::RegCreateKey(HKEY_LOCAL_MACHINE, aKeyName, aKey);
rv = NS_ERROR_FILE_NOT_FOUND;
break;
}
return rv;
}
/////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////
// Default Browser Registry Settings // Default Browser Registry Settings
// //
@ -80,11 +123,11 @@ NS_IMPL_ISUPPORTS2(nsWindowsShellService, nsIWindowsShellService, nsIShellServic
// .htm .html .shtml .xht .xhtml // .htm .html .shtml .xht .xhtml
// are mapped like so: // are mapped like so:
// //
// HKLM\SOFTWARE\Classes\.<ext>\ (default) REG_SZ FirefoxHTML // HKCU\SOFTWARE\Classes\.<ext>\ (default) REG_SZ FirefoxHTML
// //
// as aliases to the class: // as aliases to the class:
// //
// HKLM\SOFTWARE\Classes\FirefoxHTML\ // HKCU\SOFTWARE\Classes\FirefoxHTML\
// DefaultIcon (default) REG_SZ <appname>,1 // DefaultIcon (default) REG_SZ <appname>,1
// shell\open\command (default) REG_SZ <appname> -url "%1" // shell\open\command (default) REG_SZ <appname> -url "%1"
// //
@ -94,7 +137,7 @@ NS_IMPL_ISUPPORTS2(nsWindowsShellService, nsIWindowsShellService, nsIShellServic
// HTTP, HTTPS, FTP, GOPHER, CHROME // HTTP, HTTPS, FTP, GOPHER, CHROME
// are mapped like so: // are mapped like so:
// //
// HKLM\SOFTWARE\Classes\<protocol>\ // HKCU\SOFTWARE\Classes\<protocol>\
// DefaultIcon (default) REG_SZ <appname>,1 // DefaultIcon (default) REG_SZ <appname>,1
// shell\open\command (default) REG_SZ <appname> -url "%1" // shell\open\command (default) REG_SZ <appname> -url "%1"
// shell\open\ddeexec (default) REG_SZ "%1",,-1,0,,,, // shell\open\ddeexec (default) REG_SZ "%1",,-1,0,,,,
@ -107,7 +150,7 @@ NS_IMPL_ISUPPORTS2(nsWindowsShellService, nsIWindowsShellService, nsIShellServic
// The following keys are set to make Firefox appear in the Windows XP // The following keys are set to make Firefox appear in the Windows XP
// Start Menu as the browser: // Start Menu as the browser:
// //
// HKLM\SOFTWARE\Clients\StartMenuInternet // HKCU\SOFTWARE\Clients\StartMenuInternet
// firefox.exe\DefaultIcon (default) REG_SZ <appname>,0 // firefox.exe\DefaultIcon (default) REG_SZ <appname>,0
// firefox.exe\shell\open\command (default) REG_SZ <appname> // firefox.exe\shell\open\command (default) REG_SZ <appname>
// firefox.exe\shell\properties (default) REG_SZ Firefox &Options // firefox.exe\shell\properties (default) REG_SZ Firefox &Options
@ -117,7 +160,7 @@ NS_IMPL_ISUPPORTS2(nsWindowsShellService, nsIWindowsShellService, nsIShellServic
// --------------------- // ---------------------
// Every key that is set has the previous value stored in: // Every key that is set has the previous value stored in:
// //
// HKLM\SOFTWARE\Mozilla\Desktop\ <keyname> REG_SZ oldval // HKCU\SOFTWARE\Mozilla\Desktop\ <keyname> REG_SZ oldval
// //
// If there is no previous value, an empty value is set to indicate that the // If there is no previous value, an empty value is set to indicate that the
// key should be removed completely. // key should be removed completely.
@ -240,9 +283,8 @@ nsWindowsShellService::IsDefaultBrowser(PRBool aStartupCheck, PRBool* aIsDefault
::ZeroMemory(currValue, sizeof(currValue)); ::ZeroMemory(currValue, sizeof(currValue));
HKEY theKey; HKEY theKey;
DWORD result = ::RegOpenKeyEx(HKEY_LOCAL_MACHINE, nsresult rv = OpenUserKeyForReading(HKEY_CURRENT_USER, key.get(), &theKey);
key.get(), 0, KEY_READ, &theKey); if (NS_SUCCEEDED(rv)) {
if (REG_SUCCEEDED(result)) {
DWORD len = sizeof currValue; DWORD len = sizeof currValue;
DWORD result = ::RegQueryValueEx(theKey, settings->valueName, NULL, NULL, (LPBYTE)currValue, &len); DWORD result = ::RegQueryValueEx(theKey, settings->valueName, NULL, NULL, (LPBYTE)currValue, &len);
if (REG_FAILED(result) || strcmp(data.get(), currValue) != 0) { if (REG_FAILED(result) || strcmp(data.get(), currValue) != 0) {
@ -263,13 +305,12 @@ nsWindowsShellService::IsDefaultBrowser(PRBool aStartupCheck, PRBool* aIsDefault
} }
NS_IMETHODIMP NS_IMETHODIMP
nsWindowsShellService::SetDefaultBrowser(PRBool aClaimAllTypes) nsWindowsShellService::SetDefaultBrowser(PRBool aClaimAllTypes, PRBool aForAllUsers)
{ {
// Locate the Backup key // Locate the Backup key
HKEY backupKey; HKEY backupKey;
DWORD result = ::RegOpenKey(HKEY_LOCAL_MACHINE, MOZ_BACKUP_REGISTRY, &backupKey); nsresult rv = OpenKeyForWriting(MOZ_BACKUP_REGISTRY, &backupKey, aForAllUsers, PR_TRUE);
if (result == ERROR_FILE_NOT_FOUND) if (NS_FAILED(rv) && rv != NS_ERROR_FILE_NOT_FOUND) return rv;
result = ::RegCreateKey(HKEY_LOCAL_MACHINE, MOZ_BACKUP_REGISTRY, &backupKey);
SETTING* settings; SETTING* settings;
SETTING* end = gSettings + sizeof(gSettings)/sizeof(SETTING); SETTING* end = gSettings + sizeof(gSettings)/sizeof(SETTING);
@ -301,11 +342,12 @@ nsWindowsShellService::SetDefaultBrowser(PRBool aClaimAllTypes)
PRBool replaceExisting = aClaimAllTypes ? PR_TRUE : !(settings->flags & NON_ESSENTIAL); PRBool replaceExisting = aClaimAllTypes ? PR_TRUE : !(settings->flags & NON_ESSENTIAL);
SetRegKey(key.get(), settings->valueName, data.get(), SetRegKey(key.get(), settings->valueName, data.get(),
PR_TRUE, backupKey, replaceExisting); PR_TRUE, backupKey, replaceExisting, aForAllUsers);
} }
// Select the Default Browser for the Windows XP Start Menu // Select the Default Browser for the Windows XP Start Menu
SetRegKey(NS_LITERAL_CSTRING(SMI).get(), "", exeName.get(), PR_TRUE, backupKey, aClaimAllTypes); SetRegKey(NS_LITERAL_CSTRING(SMI).get(), "", exeName.get(), PR_TRUE,
backupKey, aClaimAllTypes, aForAllUsers);
nsCOMPtr<nsIStringBundleService> bundleService(do_GetService("@mozilla.org/intl/stringbundle;1")); nsCOMPtr<nsIStringBundleService> bundleService(do_GetService("@mozilla.org/intl/stringbundle;1"));
nsCOMPtr<nsIStringBundle> bundle, brandBundle; nsCOMPtr<nsIStringBundle> bundle, brandBundle;
@ -319,7 +361,8 @@ nsWindowsShellService::SetDefaultBrowser(PRBool aClaimAllTypes)
nsCAutoString key1(NS_LITERAL_CSTRING(SMI)); nsCAutoString key1(NS_LITERAL_CSTRING(SMI));
key1.Append(exeName); key1.Append(exeName);
key1.Append("\\"); key1.Append("\\");
SetRegKey(key1.get(), "", NS_ConvertUCS2toUTF8(brandFullName).get(), PR_TRUE, backupKey, aClaimAllTypes); SetRegKey(key1.get(), "", NS_ConvertUCS2toUTF8(brandFullName).get(), PR_TRUE,
backupKey, aClaimAllTypes, aForAllUsers);
// Set the Options menu item title // Set the Options menu item title
nsXPIDLString brandShortName; nsXPIDLString brandShortName;
@ -334,7 +377,8 @@ nsWindowsShellService::SetDefaultBrowser(PRBool aClaimAllTypes)
nsCAutoString key2(NS_LITERAL_CSTRING(SMI "%APPEXE%\\shell\\properties")); nsCAutoString key2(NS_LITERAL_CSTRING(SMI "%APPEXE%\\shell\\properties"));
PRInt32 offset = key2.Find("%APPEXE%"); PRInt32 offset = key2.Find("%APPEXE%");
key2.Replace(offset, 8, exeName); key2.Replace(offset, 8, exeName);
SetRegKey(key2.get(), "", NS_ConvertUCS2toUTF8(optionsTitle).get(), PR_TRUE, backupKey, aClaimAllTypes); SetRegKey(key2.get(), "", NS_ConvertUCS2toUTF8(optionsTitle).get(), PR_TRUE,
backupKey, aClaimAllTypes, aForAllUsers);
// Refresh the Shell // Refresh the Shell
::SendMessageTimeout(HWND_BROADCAST, WM_SETTINGCHANGE, NULL, ::SendMessageTimeout(HWND_BROADCAST, WM_SETTINGCHANGE, NULL,
@ -345,19 +389,18 @@ nsWindowsShellService::SetDefaultBrowser(PRBool aClaimAllTypes)
} }
NS_IMETHODIMP NS_IMETHODIMP
nsWindowsShellService::RestoreFileSettings() nsWindowsShellService::RestoreFileSettings(PRBool aForAllUsers)
{ {
// Locate the Backup key // Locate the Backup key
HKEY backupKey; HKEY backupKey;
DWORD result = ::RegOpenKey(HKEY_LOCAL_MACHINE, MOZ_BACKUP_REGISTRY, &backupKey); nsresult rv = OpenKeyForWriting(MOZ_BACKUP_REGISTRY, &backupKey, aForAllUsers, PR_FALSE);
if (result == ERROR_FILE_NOT_FOUND) if (NS_FAILED(rv)) return rv;
result = ::RegCreateKey(HKEY_LOCAL_MACHINE, MOZ_BACKUP_REGISTRY, &backupKey);
DWORD i = 0; DWORD i = 0;
do { do {
char origKeyName[MAX_BUF]; char origKeyName[MAX_BUF];
DWORD len = sizeof origKeyName; DWORD len = sizeof origKeyName;
result = ::RegEnumValue(backupKey, i++, origKeyName, &len, 0, 0, 0, 0); DWORD result = ::RegEnumValue(backupKey, i++, origKeyName, &len, 0, 0, 0, 0);
if (REG_SUCCEEDED(result)) { if (REG_SUCCEEDED(result)) {
char origValue[MAX_BUF]; char origValue[MAX_BUF];
DWORD len = sizeof origValue; DWORD len = sizeof origValue;
@ -385,25 +428,23 @@ nsWindowsShellService::RestoreFileSettings()
void void
nsWindowsShellService::SetRegKey(const char* aKeyName, const char* aValueName, nsWindowsShellService::SetRegKey(const char* aKeyName, const char* aValueName,
const char* aValue, PRBool aBackup, const char* aValue, PRBool aBackup,
HKEY aBackupKey, PRBool aReplaceExisting) HKEY aBackupKey, PRBool aReplaceExisting,
PRBool aForAllUsers)
{ {
char buf[MAX_BUF]; char buf[MAX_BUF];
DWORD len = sizeof buf; DWORD len = sizeof buf;
HKEY theKey; HKEY theKey;
DWORD result = ::RegOpenKeyEx(HKEY_LOCAL_MACHINE, aKeyName, 0, KEY_READ | KEY_WRITE, &theKey); nsresult rv = OpenKeyForWriting(aKeyName, &theKey, aForAllUsers, PR_TRUE);
if (result == ERROR_FILE_NOT_FOUND) if (NS_FAILED(rv) && rv != NS_ERROR_FILE_NOT_FOUND) return;
result = ::RegCreateKey(HKEY_LOCAL_MACHINE, aKeyName, &theKey);
if (REG_FAILED(result))
return;
// If we're not allowed to replace an existing key, and one exists (i.e. the // If we're not allowed to replace an existing key, and one exists (i.e. the
// result isn't ERROR_FILE_NOT_FOUND, then just return now. // result isn't ERROR_FILE_NOT_FOUND, then just return now.
if (!aReplaceExisting) if (!aReplaceExisting && rv != NS_ERROR_FILE_NOT_FOUND)
return; return;
// Get the old value // Get the old value
result = ::RegQueryValueEx(theKey, aValueName, NULL, NULL, (LPBYTE)buf, &len); DWORD result = ::RegQueryValueEx(theKey, aValueName, NULL, NULL, (LPBYTE)buf, &len);
// Back up the old value // Back up the old value
if (aBackup && REG_SUCCEEDED(result)) if (aBackup && REG_SUCCEEDED(result))
@ -646,14 +687,12 @@ nsWindowsShellService::OpenPreferredApplication(PRInt32 aApplication)
// Find the default application for this class. // Find the default application for this class.
HKEY theKey; HKEY theKey;
DWORD result = ::RegOpenKeyEx(HKEY_LOCAL_MACHINE, clientKey.get(), nsresult rv = OpenUserKeyForReading(HKEY_CURRENT_USER, clientKey.get(), &theKey);
0, KEY_READ, &theKey); if (NS_FAILED(rv)) return rv;
if (REG_FAILED(result))
return NS_OK;
char buf[MAX_BUF]; char buf[MAX_BUF];
DWORD type, len = sizeof buf; DWORD type, len = sizeof buf;
result = ::RegQueryValueEx(theKey, "", 0, &type, (LPBYTE)&buf, &len); DWORD result = ::RegQueryValueEx(theKey, "", 0, &type, (LPBYTE)&buf, &len);
if (REG_FAILED(result) || nsDependentCString(buf).IsEmpty()) if (REG_FAILED(result) || nsDependentCString(buf).IsEmpty())
return NS_OK; return NS_OK;
@ -662,16 +701,14 @@ nsWindowsShellService::OpenPreferredApplication(PRInt32 aApplication)
clientKey.Append(buf); clientKey.Append(buf);
clientKey.Append("\\shell\\open\\command"); clientKey.Append("\\shell\\open\\command");
result = ::RegOpenKeyEx(HKEY_LOCAL_MACHINE, clientKey.get(), rv = OpenUserKeyForReading(HKEY_CURRENT_USER, clientKey.get(), &theKey);
0, KEY_READ, &theKey); if (NS_FAILED(rv)) return rv;
if (REG_FAILED(result))
return NS_OK;
::ZeroMemory(buf, sizeof(buf)); ::ZeroMemory(buf, sizeof(buf));
len = sizeof buf; len = sizeof buf;
result = ::RegQueryValueEx(theKey, "", 0, &type, (LPBYTE)&buf, &len); result = ::RegQueryValueEx(theKey, "", 0, &type, (LPBYTE)&buf, &len);
if (REG_FAILED(result) || nsDependentCString(buf).IsEmpty()) if (REG_FAILED(result) || nsDependentCString(buf).IsEmpty())
return NS_OK; return NS_ERROR_FAILURE;
nsCAutoString path; path.Assign(buf); nsCAutoString path; path.Assign(buf);

Просмотреть файл

@ -58,7 +58,7 @@ protected:
PRBool GetMailAccountKey(HKEY* aResult); PRBool GetMailAccountKey(HKEY* aResult);
void SetRegKey(const char* aKeyName, const char* aValueName, void SetRegKey(const char* aKeyName, const char* aValueName,
const char* aValue, PRBool aBackup, HKEY aBackupKey, const char* aValue, PRBool aBackup, HKEY aBackupKey,
PRBool aReplaceExisting); PRBool aReplaceExisting, PRBool aForAllUsers);
private: private:
PRBool mCheckedThisSession; PRBool mCheckedThisSession;