38 #include "nsAppDirectoryServiceDefs.h"
40 #include "nsICookieManager2.h"
42 #include "nsILineInputStream.h"
43 #include "nsIOutputStream.h"
44 #include "nsIPrefBranch.h"
45 #include "nsIPrefLocalizedString.h"
46 #include "nsIPrefService.h"
48 #include "nsIServiceManager.h"
49 #include "nsISupportsPrimitives.h"
52 #include "nsNetUtil.h"
57 #define NEED_TO_FIX_4X_COOKIES 1
58 #define SECONDS_BETWEEN_1900_AND_1970 2208988800UL
61 #define FILE_NAME_PREFS_5X NS_LITERAL_STRING("prefs.js")
76 return NS_ERROR_INVALID_ARG;
79 return NS_ERROR_OUT_OF_MEMORY;
81 return NS_ERROR_FAILURE;
86 nsISupportsArray* aProfileNames,
87 nsISupportsArray* aProfileLocations)
93 PRBool regFileExists = PR_FALSE;
94 rv = aRegistryFile->Exists(®FileExists);
95 NS_ENSURE_SUCCESS(rv, rv);
97 return NS_ERROR_FILE_NOT_FOUND;
100 nsCAutoString regPath;
101 rv = aRegistryFile->GetNativePath(regPath);
102 NS_ENSURE_SUCCESS(rv, rv);
104 if ((errCode = NR_StartupRegistry()))
108 if ((errCode = NR_RegOpen(regPath.get(), ®))) {
109 NR_ShutdownRegistry();
115 if ((errCode = NR_RegGetKey(reg, ROOTKEY_COMMON,
"Profiles", &profilesTree))) {
117 NR_ShutdownRegistry();
122 char profileStr[MAXREGPATHLEN];
123 REGENUM enumState = nsnull;
125 while (!NR_RegEnumSubkeys(reg, profilesTree, &enumState, profileStr,
126 sizeof(profileStr), REGENUM_CHILDREN))
129 if (NR_RegGetKey(reg, profilesTree, profileStr, &profileKey))
135 errCode = NR_RegGetEntryString(reg, profileKey, (
char *)
"migrated",
136 migratedStr,
sizeof(migratedStr));
137 if ((errCode != REGERR_OK && errCode != REGERR_BUFTOOSMALL) ||
138 strcmp(migratedStr,
"no") == 0)
143 regInfo.size =
sizeof(REGINFO);
145 if (NR_RegGetEntryInfo(reg, profileKey, (
char *)
"directory", ®Info))
148 nsCAutoString dirStr;
149 dirStr.SetLength(regInfo.entryLength);
151 errCode = NR_RegGetEntryString(reg, profileKey, (
char *)
"directory",
152 dirStr.BeginWriting(), regInfo.entryLength);
154 dirStr.SetLength(regInfo.entryLength-1);
156 nsCOMPtr<nsILocalFile>
dir;
158 rv = NS_NewNativeLocalFile(EmptyCString(), PR_TRUE, getter_AddRefs(dir));
159 if (NS_FAILED(rv))
break;
160 dir->SetPersistentDescriptor(dirStr);
162 rv = NS_NewLocalFile(NS_ConvertUTF8toUTF16(dirStr), PR_TRUE,
163 getter_AddRefs(dir));
164 if (NS_FAILED(rv))
break;
168 dir->Exists(&exists);
171 aProfileLocations->AppendElement(dir);
174 nsString profileName;
175 CopyUTF8toUTF16(nsDependentCString(profileStr), profileName);
177 nsCOMPtr<nsISupportsString> profileNameString(
178 do_CreateInstance(
"@mozilla.org/supports-string;1"));
180 profileNameString->SetData(profileName);
181 aProfileNames->AppendElement(profileNameString);
185 NR_ShutdownRegistry();
190 #define GETPREF(xform, method, value) \
191 nsresult rv = aBranch->method(xform->sourcePrefName, value); \
192 if (NS_SUCCEEDED(rv)) \
193 xform->prefHasValue = PR_TRUE; \
196 #define SETPREF(xform, method, value) \
197 if (xform->prefHasValue) { \
198 return aBranch->method(xform->targetPrefName ? xform->targetPrefName : xform->sourcePrefName, value); \
220 nsCOMPtr<nsIPrefLocalizedString> prefValue;
223 getter_AddRefs(prefValue));
225 if (NS_SUCCEEDED(rv) && prefValue) {
227 prefValue->ToString(getter_Copies(data));
229 xform->
stringValue = ToNewCString(NS_ConvertUTF16toUTF8(data));
240 nsCOMPtr<nsIPrefLocalizedString> pls(do_CreateInstance(
"@mozilla.org/pref-localizedstring;1"));
242 pls->SetData(data.get());
253 nsCOMPtr<nsIPrefLocalizedString> pls(do_CreateInstance(
"@mozilla.org/pref-localizedstring;1"));
255 pls->SetData(data.get());
293 nsCOMPtr<nsIFile> sourceFile;
296 sourceFile->Append(aSourceFileName);
297 PRBool exists = PR_FALSE;
298 sourceFile->Exists(&exists);
302 nsCOMPtr<nsIFile> targetFile;
305 targetFile->Append(aTargetFileName);
306 targetFile->Exists(&exists);
308 targetFile->Remove(PR_FALSE);
315 const PRUnichar* aImportSourceNameKey)
317 nsCOMPtr<nsIFile> bookmarksFile;
319 bookmarksFile->Append(aBookmarksFileName);
328 nsCOMPtr<nsIInputStream> cookiesStream;
329 rv = NS_NewLocalFileInputStream(getter_AddRefs(cookiesStream), aCookiesFile);
330 if (NS_FAILED(rv))
return rv;
332 nsCOMPtr<nsILineInputStream> lineInputStream(do_QueryInterface(cookiesStream));
335 static NS_NAMED_LITERAL_CSTRING(kTrue,
"TRUE");
337 nsCAutoString buffer;
338 PRBool isMore = PR_TRUE;
339 PRInt32 hostIndex = 0, isDomainIndex, pathIndex, secureIndex, expiresIndex, nameIndex, cookieIndex;
343 PRInt64 currentTime = PR_Now() / PR_USEC_PER_SEC;
345 nsCOMPtr<nsICookieManager2>
cookieManager(do_GetService(NS_COOKIEMANAGER_CONTRACTID, &rv));
346 if (NS_FAILED(rv))
return rv;
361 while (isMore && NS_SUCCEEDED(lineInputStream->ReadLine(buffer, &isMore))) {
362 if (buffer.IsEmpty() || buffer.First() ==
'#')
369 if ((isDomainIndex = buffer.FindChar(
'\t', hostIndex) + 1) == 0 ||
370 (pathIndex = buffer.FindChar(
'\t', isDomainIndex) + 1) == 0 ||
371 (secureIndex = buffer.FindChar(
'\t', pathIndex) + 1) == 0 ||
372 (expiresIndex = buffer.FindChar(
'\t', secureIndex) + 1) == 0 ||
373 (nameIndex = buffer.FindChar(
'\t', expiresIndex) + 1) == 0 ||
374 (cookieIndex = buffer.FindChar(
'\t', nameIndex) + 1) == 0)
379 char *iter = buffer.BeginWriting();
380 *(iter += nameIndex - 1) =
char(0);
381 numInts = PR_sscanf(buffer.get() + expiresIndex,
"%lld", &expires);
382 if (numInts != 1 || expires < currentTime)
385 isDomain = Substring(buffer, isDomainIndex, pathIndex - isDomainIndex - 1).Equals(kTrue);
386 const nsDependentCSubstring host =
387 Substring(buffer, hostIndex, isDomainIndex - hostIndex - 1);
390 if (isDomain && !host.IsEmpty() && host.First() !=
'.' ||
391 host.FindChar(
':') != -1)
395 rv = cookieManager->Add(host,
396 Substring(buffer, pathIndex, secureIndex - pathIndex - 1),
397 Substring(buffer, nameIndex, cookieIndex - nameIndex - 1),
398 Substring(buffer, cookieIndex, buffer.Length() - cookieIndex),
399 Substring(buffer, secureIndex, expiresIndex - secureIndex - 1).Equals(kTrue),
418 nsCOMPtr<nsIFile> sourcePrefsName;
421 psvc->ReadUserPrefs(sourcePrefsName);
423 nsCOMPtr<nsIPrefBranch> branch(do_QueryInterface(psvc));
424 rv = branch->GetCharPref(
"signon.SignonFileName", aFileName);
434 nsCOMPtr<nsISimpleEnumerator> entries;
435 nsresult rv =
mSourceProfile->GetDirectoryEntries(getter_AddRefs(entries));
436 if (NS_FAILED(rv))
return rv;
438 nsCAutoString fileName;
441 rv = entries->HasMoreElements(&hasMore);
442 if (NS_FAILED(rv) || !hasMore)
break;
444 nsCOMPtr<nsISupports> supp;
445 rv = entries->GetNext(getter_AddRefs(supp));
446 if (NS_FAILED(rv))
break;
448 nsCOMPtr<nsIFile> currFile(do_QueryInterface(supp));
450 nsCOMPtr<nsIURI>
uri;
451 rv = NS_NewFileURI(getter_AddRefs(uri), currFile);
452 if (NS_FAILED(rv))
break;
453 nsCOMPtr<nsIURL>
url(do_QueryInterface(uri));
456 url->GetFileExtension(extn);
458 if (extn.Equals(
"s", CaseInsensitiveCompare)) {
459 url->GetFileName(fileName);
465 *aResult = ToNewCString(fileName);
nsresult CopyFile(const nsAString &aSourceFileName, const nsAString &aTargetFileName)
nsresult ImportNetscapeBookmarks(const nsAString &aBookmarksFileName, const PRUnichar *aImportSourceNameKey)
static nsresult SetInt(void *aTransform, nsIPrefBranch *aBranch)
static nsresult GetInt(void *aTransform, nsIPrefBranch *aBranch)
const NS_PREFSERVICE_CONTRACTID
const nsIPrefLocalizedString
static nsresult regerr2nsresult(REGERR errCode)
#define SETPREF(xform, method, value)
#define FILE_NAME_PREFS_5X
nsresult ImportBookmarksHTML(nsIFile *aBookmarksFile, PRBool aImportIntoRoot, PRBool aOverwriteDefaults, const PRUnichar *aImportSourceNameKey)
static nsresult SetWString(void *aTransform, nsIPrefBranch *aBranch)
nsCOMPtr< nsIFile > mTargetProfile
nsresult GetProfileDataFromRegistry(nsILocalFile *aRegistryFile, nsISupportsArray *aProfileNames, nsISupportsArray *aProfileLocations)
static nsresult GetWString(void *aTransform, nsIPrefBranch *aBranch)
static nsresult GetBool(void *aTransform, nsIPrefBranch *aBranch)
nsresult LocateSignonsFile(char **aResult)
static nsresult SetWStringFromASCII(void *aTransform, nsIPrefBranch *aBranch)
static nsresult SetString(void *aTransform, nsIPrefBranch *aBranch)
StringArrayEnumerator prototype hasMore
static nsresult SetBool(void *aTransform, nsIPrefBranch *aBranch)
static nsresult GetString(void *aTransform, nsIPrefBranch *aBranch)
#define GETPREF(xform, method, value)
nsNetscapeProfileMigratorBase()
nsresult ImportNetscapeCookies(nsIFile *aCookiesFile)
nsCOMPtr< nsILocalFile > mSourceProfile
nsresult GetSignonFileName(PRBool aReplace, char **aFileName)