Bug 298943 - bookmarks.html should be copied from default profile only if a backup does not exist (copy from backup first) - r=mconnor r=Neil@p a=chase

This commit is contained in:
bsmedberg%covad.net 2005-08-08 13:37:51 +00:00
Родитель 565bc86f68
Коммит c64982ab30
4 изменённых файлов: 105 добавлений и 130 удалений

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

@ -54,6 +54,7 @@
#include "nsIBrowserHandler.h"
#include "nsIDOMWindow.h"
#include "nsIObserverService.h"
#include "nsIPrefService.h"
#include "nsIRDFContainer.h"
#include "nsIRDFContainerUtils.h"
#include "nsIRDFService.h"
@ -179,7 +180,6 @@ static NS_DEFINE_CID(kRDFContainerCID, NS_RDFCONTAINER_CID);
static NS_DEFINE_CID(kRDFContainerUtilsCID, NS_RDFCONTAINERUTILS_CID);
static NS_DEFINE_CID(kIOServiceCID, NS_IOSERVICE_CID);
static NS_DEFINE_CID(kCharsetConverterManagerCID, NS_ICHARSETCONVERTERMANAGER_CID);
static NS_DEFINE_CID(kPrefCID, NS_PREF_CID);
static NS_DEFINE_CID(kStringBundleServiceCID, NS_STRINGBUNDLESERVICE_CID);
static NS_DEFINE_CID(kPlatformCharsetCID, NS_PLATFORMCHARSET_CID);
static NS_DEFINE_CID(kCacheServiceCID, NS_CACHESERVICE_CID);
@ -1678,13 +1678,15 @@ nsBookmarksService::Init()
}
}
nsCOMPtr<nsIPref> prefServ(do_GetService(kPrefCID, &rv));
if (NS_SUCCEEDED(rv) && (prefServ))
{
// get browser icon pref
prefServ->GetBoolPref("browser.chrome.site_icons", &mBrowserIcons);
nsCOMPtr<nsIPrefService> prefServ(do_GetService(NS_PREFSERVICE_CONTRACTID, &rv));
if (prefServ) {
prefServ->GetBranch("browser.bookmarks.", getter_AddRefs(mBookmarksPrefs));
nsCOMPtr<nsIPrefBranch> prefb(do_QueryInterface(prefServ));
if (prefb)
// get browser icon pref
prefb->GetBoolPref("browser.chrome.site_icons", &mBrowserIcons);
}
if (mPersonalToolbarName.IsEmpty())
@ -2457,8 +2459,8 @@ NS_IMETHODIMP nsBookmarksService::Observe(nsISupports *aSubject, const char *aTo
{
nsCOMPtr<nsIFile> bookmarksFile;
rv = GetBookmarksFile(getter_AddRefs(bookmarksFile));
NS_GetSpecialDirectory(NS_APP_BOOKMARKS_50_FILE,
getter_AddRefs(bookmarksFile));
if (bookmarksFile)
{
bookmarksFile->Remove(PR_FALSE);
@ -4245,7 +4247,8 @@ nsBookmarksService::Flush()
if (mBookmarksAvailable == PR_TRUE)
{
nsCOMPtr<nsIFile> bookmarksFile;
rv = GetBookmarksFile(getter_AddRefs(bookmarksFile));
rv = NS_GetSpecialDirectory(NS_APP_BOOKMARKS_50_FILE,
getter_AddRefs(bookmarksFile));
// Oh well, couldn't get the bookmarks file. Guess there
// aren't any bookmarks for us to write out.
@ -4279,7 +4282,8 @@ nsBookmarksService::SaveToBackup()
nsresult rv;
nsCOMPtr<nsIFile> bookmarksFile;
rv = GetBookmarksFile(getter_AddRefs(bookmarksFile));
rv = NS_GetSpecialDirectory(NS_APP_BOOKMARKS_50_FILE,
getter_AddRefs(bookmarksFile));;
if (NS_FAILED(rv) || !bookmarksFile)
return;
@ -4310,94 +4314,6 @@ nsBookmarksService::SaveToBackup()
}
}
// Attempt to restore a truncated or non-existent bookmarks.html file
// from the backup file generated by the last successful write.
void
nsBookmarksService::MaybeRestoreFromBackup(nsIFile* aBookmarkFile, nsIFile* aParentFolder)
{
if (!aBookmarkFile)
return;
PRBool exists;
aBookmarkFile->Exists(&exists);
if (exists)
{
PRInt64 fileSize;
aBookmarkFile->GetFileSize(&fileSize);
if (fileSize == 0)
{
aBookmarkFile->Remove(PR_FALSE);
exists = PR_FALSE;
}
}
if (!exists)
{
nsCOMPtr<nsIFile> backupFile;
aParentFolder->Clone(getter_AddRefs(backupFile));
if (aParentFolder && backupFile)
{
backupFile->Append(NS_LITERAL_STRING("bookmarks.bak"));
backupFile->Exists(&exists);
if (exists)
{
nsAutoString bookmarksFileName;
aBookmarkFile->GetLeafName(bookmarksFileName);
backupFile->CopyTo(aParentFolder, bookmarksFileName);
}
}
}
}
nsresult
nsBookmarksService::GetBookmarksFile(nsIFile* *aResult)
{
nsresult rv;
nsCOMPtr<nsIFile> bookmarksFile, parentFolder;
// First we see if the user has set a pref for the location of the
// bookmarks file.
nsCOMPtr<nsIPref> prefServ(do_GetService(kPrefCID, &rv));
if (NS_SUCCEEDED(rv))
{
nsCOMPtr<nsISupportsString> prefVal;
rv = prefServ->GetComplexValue("browser.bookmarks.file",
NS_GET_IID(nsISupportsString),
getter_AddRefs(prefVal));
if (NS_SUCCEEDED(rv))
{
nsAutoString bookmarkPath;
prefVal->GetData(bookmarkPath);
rv = NS_NewLocalFile(bookmarkPath, PR_TRUE,
(nsILocalFile**)(nsIFile**) getter_AddRefs(bookmarksFile));
if (NS_SUCCEEDED(rv))
{
*aResult = bookmarksFile;
NS_ADDREF(*aResult);
bookmarksFile->GetParent(getter_AddRefs(parentFolder));
if (parentFolder)
MaybeRestoreFromBackup(*aResult, parentFolder);
return NS_OK;
}
}
}
// Otherwise, we look for bookmarks.html in the current profile
// directory using the magic directory service.
rv = NS_GetSpecialDirectory(NS_APP_BOOKMARKS_50_FILE, aResult);
NS_ENSURE_SUCCESS(rv, rv);
NS_GetSpecialDirectory(NS_APP_USER_PROFILE_50_DIR, getter_AddRefs(parentFolder));
if (parentFolder)
MaybeRestoreFromBackup(*aResult, parentFolder);
return NS_OK;
}
NS_IMETHODIMP
nsBookmarksService::ReadBookmarks(PRBool *didLoadBookmarks)
{
@ -4505,7 +4421,8 @@ nsBookmarksService::LoadBookmarks()
if (NS_FAILED(rv)) return NS_OK;
nsCOMPtr<nsIFile> bookmarksFile;
rv = GetBookmarksFile(getter_AddRefs(bookmarksFile));
rv = NS_GetSpecialDirectory(NS_APP_BOOKMARKS_50_FILE,
getter_AddRefs(bookmarksFile));
// Lack of Bookmarks file is non-fatal
if (NS_FAILED(rv)) return NS_OK;

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

@ -57,7 +57,7 @@
#include "nsICacheService.h"
#include "nsICacheSession.h"
#include "nsITransactionManager.h"
#include "nsIPref.h"
#include "nsIPrefBranch.h"
class nsIOutputStream;
@ -112,8 +112,6 @@ protected:
nsresult GetBookmarkToPing(nsIRDFResource **theBookmark);
nsresult GetBookmarksFile(nsIFile* *aResult);
nsresult WriteBookmarks(nsIFile* bookmarksFile, nsIRDFDataSource *ds,
nsIRDFResource *root);
@ -175,7 +173,6 @@ protected:
nsresult UpdateLivemarkChildren(nsIRDFResource* aSource);
void SaveToBackup();
void MaybeRestoreFromBackup(nsIFile* aBookmarkFile, nsIFile* aParentFolder);
// nsIStreamObserver methods:
NS_DECL_NSIREQUESTOBSERVER

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

@ -55,6 +55,7 @@ MOZILLA_INTERNAL_API = 1
REQUIRES = \
xpcom \
string \
pref \
xulapp \
$(NULL)

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

@ -39,6 +39,8 @@
#include "nsIFile.h"
#include "nsISimpleEnumerator.h"
#include "nsIPrefService.h"
#include "nsIPrefBranch.h"
#include "nsAppDirectoryServiceDefs.h"
#include "nsCategoryManagerUtils.h"
@ -65,8 +67,10 @@ public:
const nsModuleComponentInfo *aInfo);
private:
void EnsureProfileFile(const nsACString& aLeafName, nsIFile* aProfileDir,
nsIFile* aTarget);
nsresult RestoreBookmarksFromBackup(const nsACString& aLeafName,
nsIFile* aParentDir, nsIFile* aTarget);
void EnsureProfileFile(const nsACString& aLeafName,
nsIFile* aParentDir, nsIFile* aTarget);
class AppendingEnumerator : public nsISimpleEnumerator
{
@ -97,13 +101,26 @@ nsBrowserDirectoryProvider::GetFile(const char *aKey, PRBool *aPersist,
// NOTE: This function can be reentrant through the NS_GetSpecialDirectory
// call, so be careful not to cause infinite recursion.
char const* leafName;
nsCOMPtr<nsIFile> file;
if (!strcmp(aKey, NS_APP_USER_PANELS_50_FILE)) {
leafName = "panels.rdf";
}
else if (!strcmp(aKey, NS_APP_BOOKMARKS_50_FILE)) {
char const* leafName = nsnull;
PRBool restoreBookmarksBackup = PR_FALSE;
if (!strcmp(aKey, NS_APP_BOOKMARKS_50_FILE)) {
restoreBookmarksBackup = PR_TRUE;
leafName = "bookmarks.html";
nsCOMPtr<nsIPrefBranch> prefs(do_GetService(NS_PREFSERVICE_CONTRACTID));
if (prefs) {
nsXPIDLCString path;
rv = prefs->GetCharPref("browser.bookmarks.file", getter_Copies(path));
if (NS_SUCCEEDED(rv)) {
NS_NewNativeLocalFile(path, PR_TRUE, (nsILocalFile**)(nsIFile**) getter_AddRefs(file));
}
}
}
else if (!strcmp(aKey, NS_APP_USER_PANELS_50_FILE)) {
leafName = "panels.rdf";
}
else if (!strcmp(aKey, NS_APP_SEARCH_50_FILE)) {
leafName = "search.rdf";
@ -112,20 +129,44 @@ nsBrowserDirectoryProvider::GetFile(const char *aKey, PRBool *aPersist,
return NS_ERROR_FAILURE;
}
nsCOMPtr<nsIFile> profile;
rv = NS_GetSpecialDirectory(NS_APP_USER_PROFILE_50_DIR, getter_AddRefs(profile));
if (NS_FAILED(rv))
return rv;
nsCOMPtr<nsIFile> file;
rv = profile->Clone(getter_AddRefs(file));
if (NS_FAILED(rv))
return rv;
nsDependentCString leafstr(leafName);
file->AppendNative(leafstr);
EnsureProfileFile(leafstr, profile, file);
nsCOMPtr<nsIFile> parentDir;
if (file) {
rv = file->GetParent(getter_AddRefs(parentDir));
if (NS_FAILED(rv))
return rv;
}
else {
rv = NS_GetSpecialDirectory(NS_APP_USER_PROFILE_50_DIR, getter_AddRefs(parentDir));
if (NS_FAILED(rv))
return rv;
rv = parentDir->Clone(getter_AddRefs(file));
if (NS_FAILED(rv))
return rv;
file->AppendNative(leafstr);
}
PRBool exists;
rv = file->Exists(&exists);
if (restoreBookmarksBackup && NS_SUCCEEDED(rv) && exists) {
PRInt64 fileSize;
file->GetFileSize(&fileSize);
if (fileSize == 0)
{
file->Remove(PR_FALSE);
exists = PR_FALSE;
}
}
if (NS_SUCCEEDED(rv) || !exists) {
if (!restoreBookmarksBackup ||
NS_FAILED(RestoreBookmarksFromBackup(leafstr, parentDir, file)))
EnsureProfileFile(leafstr, parentDir, file);
}
*aPersist = PR_TRUE;
NS_ADDREF(*aResult = file);
@ -222,17 +263,34 @@ static const nsModuleComponentInfo components[] = {
NS_IMPL_NSGETMODULE(BrowserDirProvider, components)
void
nsBrowserDirectoryProvider::EnsureProfileFile(const nsACString& aLeafName,
nsIFile* aProfile,
nsIFile* aTarget)
nsresult
nsBrowserDirectoryProvider::RestoreBookmarksFromBackup(const nsACString& aLeafName,
nsIFile* aParentDir,
nsIFile* aTarget)
{
nsresult rv;
nsCOMPtr<nsIFile> backupFile;
rv = aParentDir->Clone(getter_AddRefs(backupFile));
if (NS_FAILED(rv))
return rv;
backupFile->AppendNative(nsDependentCString("bookmarks.bak"));
PRBool exists;
rv = aTarget->Exists(&exists);
if (NS_FAILED(rv) || exists)
return;
rv = backupFile->Exists(&exists);
if (NS_FAILED(rv) || !exists)
return NS_ERROR_FAILURE;
return backupFile->CopyToNative(aParentDir, aLeafName);
}
void
nsBrowserDirectoryProvider::EnsureProfileFile(const nsACString& aLeafName,
nsIFile* aParentDir,
nsIFile* aTarget)
{
nsresult rv;
nsCOMPtr<nsIFile> defaults;
rv = NS_GetSpecialDirectory(NS_APP_PROFILE_DEFAULTS_50_DIR,
@ -241,11 +299,13 @@ nsBrowserDirectoryProvider::EnsureProfileFile(const nsACString& aLeafName,
return;
defaults->AppendNative(aLeafName);
PRBool exists;
rv = defaults->Exists(&exists);
if (NS_FAILED(rv) || !exists)
return;
defaults->CopyToNative(aProfile, aLeafName);
defaults->CopyToNative(aParentDir, aLeafName);
}
NS_IMPL_ISUPPORTS1(nsBrowserDirectoryProvider::AppendingEnumerator,