nsDogbertProfileMigrator.cpp
Go to the documentation of this file.
1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* ***** BEGIN LICENSE BLOCK *****
3  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
4  *
5  * The contents of this file are subject to the Mozilla Public License Version
6  * 1.1 (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  * http://www.mozilla.org/MPL/
9  *
10  * Software distributed under the License is distributed on an "AS IS" basis,
11  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12  * for the specific language governing rights and limitations under the
13  * License.
14  *
15  * The Original Code is The Browser Profile Migrator.
16  *
17  * The Initial Developer of the Original Code is Ben Goodger.
18  * Portions created by the Initial Developer are Copyright (C) 2004
19  * the Initial Developer. All Rights Reserved.
20  *
21  * Contributor(s):
22  * Ben Goodger <ben@bengoodger.com>
23  *
24  * Alternatively, the contents of this file may be used under the terms of
25  * either the GNU General Public License Version 2 or later (the "GPL"), or
26  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
27  * in which case the provisions of the GPL or the LGPL are applicable instead
28  * of those above. If you wish to allow use of your version of this file only
29  * under the terms of either the GPL or the LGPL, and not to allow others to
30  * use your version of this file under the terms of the MPL, indicate your
31  * decision by deleting the provisions above and replace them with the notice
32  * and other provisions required by the GPL or the LGPL. If you do not delete
33  * the provisions above, a recipient may use your version of this file under
34  * the terms of any one of the MPL, the GPL or the LGPL.
35  *
36  * ***** END LICENSE BLOCK ***** */
37 
38 #include "nsAppDirectoryServiceDefs.h"
41 #include "nsICookieManager2.h"
42 #include "nsIFile.h"
43 #include "nsIInputStream.h"
44 #include "nsILineInputStream.h"
45 #include "nsIObserverService.h"
46 #include "nsIOutputStream.h"
47 #include "nsIPrefBranch.h"
48 #include "nsIPrefLocalizedString.h"
49 #include "nsIPrefService.h"
50 #include "nsIServiceManager.h"
51 #include "nsISupportsArray.h"
52 #include "nsISupportsPrimitives.h"
53 #include "nsNetCID.h"
54 #include "nsNetUtil.h"
55 #include "prprf.h"
56 #include "prenv.h"
57 #include "NSReg.h"
58 #include "nsDirectoryServiceDefs.h"
59 #include "nsDirectoryServiceUtils.h"
60 #include <stdlib.h>
61 #include <limits.h>
62 
63 #ifndef MAXPATHLEN
64 #ifdef PATH_MAX
65 #define MAXPATHLEN PATH_MAX
66 #elif defined(_MAX_PATH)
67 #define MAXPATHLEN _MAX_PATH
68 #elif defined(CCHMAXPATH)
69 #define MAXPATHLEN CCHMAXPATH
70 #else
71 #define MAXPATHLEN 1024
72 #endif
73 #endif
74 
75 #define PREF_FILE_HEADER_STRING "# Mozilla User Preferences "
76 
77 #if defined(XP_MACOSX)
78 #define OLDREG_NAME "Netscape Registry"
79 #define OLDREG_DIR NS_MAC_PREFS_DIR
80 #define PREF_FILE_NAME_IN_4x NS_LITERAL_STRING("Netscape Preferences")
81 #define COOKIES_FILE_NAME_IN_4x NS_LITERAL_STRING("MagicCookie")
82 #define BOOKMARKS_FILE_NAME_IN_4x NS_LITERAL_STRING("Bookmarks.html")
83 #define SECURITY_PATH "Security"
84 #define PSM_CERT7_DB NS_LITERAL_STRING("Certificates7")
85 #define PSM_KEY3_DB NS_LITERAL_STRING("Key Database3")
86 #define PSM_SECMODULE_DB NS_LITERAL_STRING("Security Modules")
87 
88 #elif defined(XP_WIN) || defined(XP_OS2)
89 #define OLDREG_NAME "nsreg.dat"
90 #ifdef XP_WIN
91 #define OLDREG_DIR NS_WIN_WINDOWS_DIR
92 #else
93 #define OLDREG_DIR NS_OS2_DIR
94 #endif
95 #define PREF_FILE_NAME_IN_4x NS_LITERAL_STRING("prefs.js")
96 #define COOKIES_FILE_NAME_IN_4x NS_LITERAL_STRING("cookies.txt")
97 #define BOOKMARKS_FILE_NAME_IN_4x NS_LITERAL_STRING("bookmark.htm")
98 #define PSM_CERT7_DB NS_LITERAL_STRING("cert7.db")
99 #define PSM_KEY3_DB NS_LITERAL_STRING("key3.db")
100 #define PSM_SECMODULE_DB NS_LITERAL_STRING("secmod.db")
101 
102 #elif defined(XP_UNIX)
103 #define PREF_FILE_NAME_IN_4x NS_LITERAL_STRING("preferences.js")
104 #define COOKIES_FILE_NAME_IN_4x NS_LITERAL_STRING("cookies")
105 #define BOOKMARKS_FILE_NAME_IN_4x NS_LITERAL_STRING("bookmarks.html")
106 #define PSM_CERT7_DB NS_LITERAL_STRING("cert7.db")
107 #define PSM_KEY3_DB NS_LITERAL_STRING("key3.db")
108 #define PSM_SECMODULE_DB NS_LITERAL_STRING("secmodule.db")
109 #define HOME_ENVIRONMENT_VARIABLE "HOME"
110 #define PROFILE_HOME_ENVIRONMENT_VARIABLE "PROFILE_HOME"
111 #define DEFAULT_UNIX_PROFILE_NAME "default"
112 #else
113 #error No netscape4.x profile-migrator on this platform.
114 #endif /* XP_UNIX */
115 
116 #define COOKIES_FILE_NAME_IN_5x NS_LITERAL_STRING("cookies.txt")
117 #define BOOKMARKS_FILE_NAME_IN_5x NS_LITERAL_STRING("bookmarks.html")
118 #define PREF_FILE_NAME_IN_5x NS_LITERAL_STRING("prefs.js")
119 
121 // nsDogbertProfileMigrator
122 
124 
126 {
127  mObserverService = do_GetService("@mozilla.org/observer-service;1");
128 }
129 
131 {
132 }
133 
135 // nsIBrowserProfileMigrator
136 
137 NS_IMETHODIMP
138 nsDogbertProfileMigrator::Migrate(PRUint16 aItems, nsIProfileStartup* aStartup,
139  const PRUnichar* aProfile)
140 {
141  nsresult rv = NS_OK;
142  PRBool aReplace = aStartup ? PR_TRUE : PR_FALSE;
143 
144  if (!mTargetProfile) {
145  GetProfilePath(aStartup, mTargetProfile);
146  if (!mTargetProfile) return NS_ERROR_FAILURE;
147  }
148 
149  if (!mSourceProfile) {
150  GetSourceProfile(aProfile);
151  if (!mSourceProfile)
152  return NS_ERROR_FAILURE;
153  }
154 
156 
160 
162 
163  return rv;
164 }
165 
166 // on win/mac/os2, NS4x uses a registry to determine profile locations
167 #if defined(XP_WIN) || defined(XP_MACOSX) || defined(XP_OS2)
168 void
169 nsDogbertProfileMigrator::GetSourceProfile(const PRUnichar* aProfile)
170 {
171  nsresult rv;
172 
173  nsCOMPtr<nsIFile> regFile;
174  rv = NS_GetSpecialDirectory(OLDREG_DIR, getter_AddRefs(regFile));
175  if (NS_FAILED(rv)) return;
176 
177  regFile->AppendNative(NS_LITERAL_CSTRING(OLDREG_NAME));
178 
179  nsCAutoString path;
180  rv = regFile->GetNativePath(path);
181  if (NS_FAILED(rv)) return;
182 
183  if (NR_StartupRegistry())
184  return;
185 
186  HREG reg = nsnull;
187  RKEY profile = nsnull;
188 
189  if (NR_RegOpen(path.get(), &reg))
190  goto cleanup;
191 
192  {
193  // on macos, registry entries are UTF8 encoded
194  NS_ConvertUTF16toUTF8 profileName(aProfile);
195 
196  if (NR_RegGetKey(reg, ROOTKEY_USERS, profileName.get(), &profile))
197  goto cleanup;
198  }
199 
200  char profilePath[MAXPATHLEN];
201  if (NR_RegGetEntryString(reg, profile, "ProfileLocation", profilePath, MAXPATHLEN))
202  goto cleanup;
203 
204  mSourceProfile = do_CreateInstance("@mozilla.org/file/local;1");
205  if (!mSourceProfile) goto cleanup;
206 
207  {
208  // the string is UTF8 encoded, which forces us to do some strange string-do
209  rv = mSourceProfile->InitWithPath(NS_ConvertUTF8toUTF16(profilePath));
210  }
211 
212  if (NS_FAILED(rv))
213  mSourceProfile = nsnull;
214 
215 cleanup:
216  if (reg)
217  NR_RegClose(reg);
218  NR_ShutdownRegistry();
219 }
220 #else
221 
222 #endif
223 
224 NS_IMETHODIMP
225 nsDogbertProfileMigrator::GetMigrateData(const PRUnichar* aProfile,
226  PRBool aReplace,
227  PRUint16* aResult)
228 {
229  *aResult = 0;
230  if (!mSourceProfile) {
231  GetSourceProfile(aProfile);
232  if (!mSourceProfile)
233  return NS_ERROR_FILE_NOT_FOUND;
234  }
235 
236  MigrationData data[] = { { ToNewUnicode(PREF_FILE_NAME_IN_4x),
238  PR_TRUE },
239  { ToNewUnicode(COOKIES_FILE_NAME_IN_4x),
241  PR_FALSE },
242  { ToNewUnicode(BOOKMARKS_FILE_NAME_IN_4x),
244  PR_FALSE } };
245 
246  // Frees file name strings allocated above.
247  GetMigrateDataFromArray(data, sizeof(data)/sizeof(MigrationData),
248  aReplace, mSourceProfile, aResult);
249 
250  return NS_OK;
251 }
252 
253 NS_IMETHODIMP
254 nsDogbertProfileMigrator::GetSourceExists(PRBool* aResult)
255 {
256  nsCOMPtr<nsISupportsArray> profiles;
257  GetSourceProfiles(getter_AddRefs(profiles));
258 
259  if (profiles) {
260  PRUint32 count;
261  profiles->Count(&count);
262  *aResult = count > 0;
263  }
264  else
265  *aResult = PR_FALSE;
266 
267  return NS_OK;
268 }
269 
270 NS_IMETHODIMP
271 nsDogbertProfileMigrator::GetSourceHasMultipleProfiles(PRBool* aResult)
272 {
273  nsCOMPtr<nsISupportsArray> profiles;
274  GetSourceProfiles(getter_AddRefs(profiles));
275 
276  if (profiles) {
277  PRUint32 count;
278  profiles->Count(&count);
279  *aResult = count > 1;
280  }
281  else
282  *aResult = PR_FALSE;
283 
284  return NS_OK;
285 }
286 
287 #if defined(XP_WIN) || defined(XP_OS2) || defined(XP_MACOSX)
288 NS_IMETHODIMP
289 nsDogbertProfileMigrator::GetSourceProfiles(nsISupportsArray** aResult)
290 {
291  if (!mProfiles) {
292  nsresult rv;
293 
294  mProfiles = do_CreateInstance(NS_SUPPORTSARRAY_CONTRACTID, &rv);
295  NS_ENSURE_SUCCESS(rv, rv);
296 
297  nsCOMPtr<nsIFile> regFile;
298  rv = NS_GetSpecialDirectory(OLDREG_DIR, getter_AddRefs(regFile));
299  NS_ENSURE_SUCCESS(rv, rv);
300  regFile->AppendNative(NS_LITERAL_CSTRING(OLDREG_NAME));
301 
302  nsCAutoString path;
303  rv = regFile->GetNativePath(path);
304  NS_ENSURE_SUCCESS(rv, rv);
305 
306  if (NR_StartupRegistry())
307  return NS_ERROR_FAILURE;
308 
309  HREG reg = nsnull;
310  REGENUM enumstate = 0;
311 
312  if (NR_RegOpen(path.get(), &reg)) {
313  NR_ShutdownRegistry();
314  return NS_ERROR_FAILURE;
315  }
316 
317  char profileName[MAXREGNAMELEN];
318  while (!NR_RegEnumSubkeys(reg, ROOTKEY_USERS, &enumstate,
319  profileName, MAXREGNAMELEN, REGENUM_CHILDREN)) {
320  nsCOMPtr<nsISupportsString> nameString
321  (do_CreateInstance("@mozilla.org/supports-string;1"));
322  if (nameString) {
323  nameString->SetData(NS_ConvertUTF8toUTF16(profileName));
324  mProfiles->AppendElement(nameString);
325  }
326  }
327  }
328 
329  NS_IF_ADDREF(*aResult = mProfiles);
330  return NS_OK;
331 }
332 #else // XP_UNIX
333 
334 NS_IMETHODIMP
335 nsDogbertProfileMigrator::GetSourceProfiles(nsISupportsArray** aResult)
336 {
337  nsresult rv;
338  const char* profileDir = PR_GetEnv(PROFILE_HOME_ENVIRONMENT_VARIABLE);
339 
340  if (!profileDir) {
341  profileDir = PR_GetEnv(HOME_ENVIRONMENT_VARIABLE);
342  }
343  if (!profileDir) return NS_ERROR_FAILURE;
344 
345  nsCAutoString profilePath(profileDir);
346  profilePath += "/.netscape";
347 
348  nsCOMPtr<nsILocalFile> profileFile;
349  rv = NS_NewNativeLocalFile(profilePath, PR_TRUE, getter_AddRefs(profileFile));
350  NS_ENSURE_SUCCESS(rv, rv);
351 
352  nsCOMPtr<nsIFile> prefFile;
353  rv = profileFile->Clone(getter_AddRefs(prefFile));
354  NS_ENSURE_SUCCESS(rv, rv);
355 
356  prefFile->AppendNative(NS_LITERAL_CSTRING("preferences.js"));
357 
358  PRBool exists;
359  rv = prefFile->Exists(&exists);
360  if (NS_FAILED(rv) || !exists) {
361  return NS_ERROR_FAILURE;
362  }
363 
364  mSourceProfile = profileFile;
365 
366  mProfiles = do_CreateInstance(NS_SUPPORTSARRAY_CONTRACTID, &rv);
367  NS_ENSURE_SUCCESS(rv, rv);
368 
369  nsCOMPtr<nsISupportsString> nameString
370  (do_CreateInstance("@mozilla.org/supports-string;1"));
371  if (!nameString) return NS_ERROR_FAILURE;
372 
373  nameString->SetData(NS_LITERAL_STRING("Netscape 4.x"));
374  mProfiles->AppendElement(nameString);
375  NS_ADDREF(*aResult = mProfiles);
376  return NS_OK;
377 }
378 
379 void
381 {
382  // if GetSourceProfiles didn't do its magic, we're screwed
383 }
384 #endif // GetSourceProfiles
385 
386 NS_IMETHODIMP
387 nsDogbertProfileMigrator::GetSourceHomePageURL(nsACString& aResult)
388 {
389  aResult.Truncate();
390  return NS_OK;
391 }
393 // nsDogbertProfileMigrator
394 #define F(a) nsDogbertProfileMigrator::a
395 
396 static
398  // Simple Copy Prefs
399  { "browser.anchor_color", 0, F(GetString), F(SetString), PR_FALSE, { -1 } },
400  { "browser.visited_color", 0, F(GetString), F(SetString), PR_FALSE, { -1 } },
401  { "browser.startup.homepage", 0, F(GetString), F(SetString), PR_FALSE, { -1 } },
402  { "network.cookie.cookieBehavior", 0, F(GetInt), F(SetInt), PR_FALSE, { -1 } },
403  { "network.cookie.warnAboutCookies",0, F(GetBool), F(SetBool), PR_FALSE, { -1 } },
404  { "javascript.enabled", 0, F(GetBool), F(SetBool), PR_FALSE, { -1 } },
405  { "network.proxy.type", 0, F(GetInt), F(SetInt), PR_FALSE, { -1 } },
406  { "network.proxy.no_proxies_on", 0, F(GetString), F(SetString), PR_FALSE, { -1 } },
407  { "network.proxy.autoconfig_url", 0, F(GetString), F(SetString), PR_FALSE, { -1 } },
408  { "network.proxy.ftp", 0, F(GetString), F(SetString), PR_FALSE, { -1 } },
409  { "network.proxy.ftp_port", 0, F(GetInt), F(SetInt), PR_FALSE, { -1 } },
410  { "network.proxy.gopher", 0, F(GetString), F(SetString), PR_FALSE, { -1 } },
411  { "network.proxy.gopher_port", 0, F(GetInt), F(SetInt), PR_FALSE, { -1 } },
412  { "network.proxy.http", 0, F(GetString), F(SetString), PR_FALSE, { -1 } },
413  { "network.proxy.http_port", 0, F(GetInt), F(SetInt), PR_FALSE, { -1 } },
414  { "network.proxy.ssl", 0, F(GetString), F(SetString), PR_FALSE, { -1 } },
415  { "network.proxy.ssl_port", 0, F(GetInt), F(SetInt), PR_FALSE, { -1 } },
416 
417  // Prefs with Different Names
418  { "network.hosts.socks_server", "network.proxy.socks", F(GetString), F(SetString), PR_FALSE, { -1 } },
419  { "network.hosts.socks_serverport", "network.proxy.socks_port", F(GetInt), F(SetInt), PR_FALSE, { -1 } },
420  { "browser.background_color", "browser.display.background_color", F(GetString), F(SetString), PR_FALSE, { -1 } },
421  { "browser.foreground_color", "browser.display.foreground_color", F(GetString), F(SetString), PR_FALSE, { -1 } },
422  { "browser.wfe.use_windows_colors", "browser.display.use_system_colors", F(GetBool), F(SetBool), PR_FALSE, { -1 } },
423  { "browser.use_document_colors", "browser.display.use_document_colors",F(GetBool), F(SetBool), PR_FALSE, { -1 } },
424  { "browser.use_document.fonts", "browser.display.use_document_fonts", F(GetInt), F(SetInt), PR_FALSE, { -1 } },
425  { "browser.link_expiration", "browser.history_expire_days", F(GetInt), F(SetInt), PR_FALSE, { -1 } },
426  { "browser.startup.page", "browser.startup.homepage", F(GetHomepage), F(SetWStringFromASCII), PR_FALSE, { -1 } },
427  { "general.always_load_images", "permissions.default.image", F(GetImagePref),F(SetInt), PR_FALSE, { -1 } },
428 };
429 
430 nsresult
431 nsDogbertProfileMigrator::TransformPreferences(const nsAString& aSourcePrefFileName,
432  const nsAString& aTargetPrefFileName)
433 {
434  PrefTransform* transform;
435  PrefTransform* end = gTransforms + sizeof(gTransforms)/sizeof(PrefTransform);
436 
437  // Load the source pref file
438  nsCOMPtr<nsIPrefService> psvc(do_GetService(NS_PREFSERVICE_CONTRACTID));
439  psvc->ResetPrefs();
440 
441  nsCOMPtr<nsIFile> sourcePrefsFile;
442  mSourceProfile->Clone(getter_AddRefs(sourcePrefsFile));
443  sourcePrefsFile->Append(aSourcePrefFileName);
444  psvc->ReadUserPrefs(sourcePrefsFile);
445 
446  nsCOMPtr<nsIPrefBranch> branch(do_QueryInterface(psvc));
447  for (transform = gTransforms; transform < end; ++transform)
448  transform->prefGetterFunc(transform, branch);
449 
450  // Now that we have all the pref data in memory, load the target pref file,
451  // and write it back out
452  psvc->ResetPrefs();
453  for (transform = gTransforms; transform < end; ++transform)
454  transform->prefSetterFunc(transform, branch);
455 
456  nsCOMPtr<nsIFile> targetPrefsFile;
457  mTargetProfile->Clone(getter_AddRefs(targetPrefsFile));
458  targetPrefsFile->Append(aTargetPrefFileName);
459  psvc->SavePrefFile(targetPrefsFile);
460 
461  return NS_OK;
462 }
463 
464 nsresult
466 {
467  nsresult rv = NS_OK;
468 
469  if (!aReplace)
470  return rv;
471 
472  // 1) Copy Preferences
473  TransformPreferences(PREF_FILE_NAME_IN_4x, PREF_FILE_NAME_IN_5x);
474 
475  // 2) Copy Certficates
476  rv |= CopyFile(PSM_CERT7_DB, PSM_CERT7_DB);
477  rv |= CopyFile(PSM_KEY3_DB, PSM_KEY3_DB);
478  rv |= CopyFile(PSM_SECMODULE_DB, PSM_SECMODULE_DB);
479 
480  return rv;
481 }
482 
483 nsresult
485 {
486  PrefTransform* xform = (PrefTransform*)aTransform;
487  PRInt32 val;
488  nsresult rv = aBranch->GetIntPref(xform->sourcePrefName, &val);
489  if (NS_SUCCEEDED(rv) && val == 0) {
490  xform->stringValue = "about:blank";
491  xform->prefHasValue = PR_TRUE;
492  }
493  return rv;
494 }
495 
496 nsresult
498 {
499  PrefTransform* xform = (PrefTransform*)aTransform;
500  PRBool loadImages;
501  nsresult rv = aBranch->GetBoolPref(xform->sourcePrefName, &loadImages);
502  if (NS_SUCCEEDED(rv)) {
503  xform->intValue = loadImages ? 1 : 2;
504  xform->prefHasValue = PR_TRUE;
505  }
506  return rv;
507 }
508 
509 nsresult
511 {
512  nsresult rv;
513  if (aReplace) {
514 #ifdef NEED_TO_FIX_4X_COOKIES
515  rv = CopyFile(COOKIES_FILE_NAME_IN_4x, COOKIES_FILE_NAME_IN_5x);
516  if (NS_FAILED(rv)) return rv;
517 
518  rv = FixDogbertCookies();
519 #else
520  rv = CopyFile(COOKIES_FILE_NAME_IN_4x, COOKIES_FILE_NAME_IN_5x);
521 #endif
522  }
523  else {
524  nsCOMPtr<nsICookieManager2> cookieManager(do_GetService(NS_COOKIEMANAGER_CONTRACTID));
525  if (!cookieManager)
526  return NS_ERROR_OUT_OF_MEMORY;
527 
528  nsCOMPtr<nsIFile> dogbertCookiesFile;
529  mSourceProfile->Clone(getter_AddRefs(dogbertCookiesFile));
530  dogbertCookiesFile->Append(COOKIES_FILE_NAME_IN_4x);
531 
532  rv = ImportNetscapeCookies(dogbertCookiesFile);
533  }
534  return rv;
535 }
536 
537 #if NEED_TO_FIX_4X_COOKIES
538 nsresult
539 nsDogbertProfileMigrator::FixDogbertCookies()
540 {
541  nsCOMPtr<nsIFile> dogbertCookiesFile;
542  mSourceProfile->Clone(getter_AddRefs(dogbertCookiesFile));
543  dogbertCookiesFile->Append(COOKIES_FILE_NAME_IN_4x);
544 
545  nsCOMPtr<nsIInputStream> fileInputStream;
546  NS_NewLocalFileInputStream(getter_AddRefs(fileInputStream), dogbertCookiesFile);
547  if (!fileInputStream) return NS_ERROR_OUT_OF_MEMORY;
548 
549  nsCOMPtr<nsIFile> firebirdCookiesFile;
550  mTargetProfile->Clone(getter_AddRefs(firebirdCookiesFile));
551  firebirdCookiesFile->Append(COOKIES_FILE_NAME_IN_5x);
552 
553  nsCOMPtr<nsIOutputStream> fileOutputStream;
554  NS_NewLocalFileOutputStream(getter_AddRefs(fileOutputStream), firebirdCookiesFile);
555  if (!fileOutputStream) return NS_ERROR_OUT_OF_MEMORY;
556 
557  nsCOMPtr<nsILineInputStream> lineInputStream(do_QueryInterface(fileInputStream));
558  nsCAutoString buffer, outBuffer;
559  PRBool moreData = PR_FALSE;
560  PRUint32 written = 0;
561  do {
562  nsresult rv = lineInputStream->ReadLine(buffer, &moreData);
563  if (NS_FAILED(rv)) return rv;
564 
565  if (!moreData)
566  break;
567 
568  // skip line if it is a comment or null line
569  if (buffer.IsEmpty() || buffer.CharAt(0) == '#' ||
570  buffer.CharAt(0) == '\r' || buffer.CharAt(0) == '\n') {
571  fileOutputStream->Write(buffer.get(), buffer.Length(), &written);
572  continue;
573  }
574 
575  // locate expire field, skip line if it does not contain all its fields
576  int hostIndex, isDomainIndex, pathIndex, xxxIndex, expiresIndex, nameIndex, cookieIndex;
577  hostIndex = 0;
578  if ((isDomainIndex = buffer.FindChar('\t', hostIndex)+1) == 0 ||
579  (pathIndex = buffer.FindChar('\t', isDomainIndex)+1) == 0 ||
580  (xxxIndex = buffer.FindChar('\t', pathIndex)+1) == 0 ||
581  (expiresIndex = buffer.FindChar('\t', xxxIndex)+1) == 0 ||
582  (nameIndex = buffer.FindChar('\t', expiresIndex)+1) == 0 ||
583  (cookieIndex = buffer.FindChar('\t', nameIndex)+1) == 0 )
584  continue;
585 
586  // separate the expires field from the rest of the cookie line
587  const nsDependentCSubstring prefix =
588  Substring(buffer, hostIndex, expiresIndex-hostIndex-1);
589  const nsDependentCSubstring expiresString =
590  Substring(buffer, expiresIndex, nameIndex-expiresIndex-1);
591  const nsDependentCSubstring suffix =
592  Substring(buffer, nameIndex, buffer.Length()-nameIndex);
593 
594  // correct the expires field
595  char* expiresCString = ToNewCString(expiresString);
596  unsigned long expires = strtoul(expiresCString, nsnull, 10);
597  NS_Free(expiresCString);
598 
599  // if the cookie is supposed to expire at the end of the session
600  // expires == 0. don't adjust those cookies.
601  if (expires)
602  expires -= SECONDS_BETWEEN_1900_AND_1970;
603  char dateString[36];
604  PR_snprintf(dateString, sizeof(dateString), "%lu", expires);
605 
606  // generate the output buffer and write it to file
607  outBuffer = prefix;
608  outBuffer.Append('\t');
609  outBuffer.Append(dateString);
610  outBuffer.Append('\t');
611  outBuffer.Append(suffix);
612 
613  fileOutputStream->Write(outBuffer.get(), outBuffer.Length(), &written);
614  }
615  while (1);
616 
617  return NS_OK;
618 }
619 
620 #endif // NEED_TO_FIX_4X_COOKIES
621 
622 
623 nsresult
625 {
626  // If we're blowing away existing content, just copy the file, don't do fancy importing.
627  if (aReplace) {
628  nsresult rv = InitializeBookmarks(mTargetProfile);
629  NS_ENSURE_SUCCESS(rv, rv);
630  return MigrateDogbertBookmarks();
631  }
632 
633  return ImportNetscapeBookmarks(BOOKMARKS_FILE_NAME_IN_4x,
634  NS_LITERAL_STRING("sourceNameDogbert").get());
635 }
636 
637 nsresult
639 {
640  nsresult rv;
641 
642  // Find out what the personal toolbar folder was called, this is stored in a pref
643  // in 4.x
644  nsCOMPtr<nsIPrefService> psvc(do_GetService(NS_PREFSERVICE_CONTRACTID));
645  psvc->ResetPrefs();
646 
647  nsCOMPtr<nsIFile> dogbertPrefsFile;
648  mSourceProfile->Clone(getter_AddRefs(dogbertPrefsFile));
649  dogbertPrefsFile->Append(PREF_FILE_NAME_IN_4x);
650  psvc->ReadUserPrefs(dogbertPrefsFile);
651 
652  nsCString toolbarName;
653  nsCOMPtr<nsIPrefBranch> branch(do_QueryInterface(psvc));
654  rv = branch->GetCharPref("custtoolbar.personal_toolbar_folder", getter_Copies(toolbarName));
655  // If the pref wasn't set in the user's 4.x preferences, there's no way we can "Fix" the
656  // file when importing it to set the personal toolbar folder correctly, so don't bother
657  // with the more involved file correction procedure and just copy the file over.
658  if (NS_FAILED(rv))
659  return CopyFile(BOOKMARKS_FILE_NAME_IN_4x, BOOKMARKS_FILE_NAME_IN_5x);
660 
661  // Now read the 4.x bookmarks file, correcting the Personal Toolbar Folder line
662  // and writing to the new location.
663  nsCOMPtr<nsIFile> sourceBookmarksFile;
664  mSourceProfile->Clone(getter_AddRefs(sourceBookmarksFile));
665  sourceBookmarksFile->Append(BOOKMARKS_FILE_NAME_IN_4x);
666 
667  nsCOMPtr<nsIFile> targetBookmarksFile;
668  mTargetProfile->Clone(getter_AddRefs(targetBookmarksFile));
669  targetBookmarksFile->Append(BOOKMARKS_FILE_NAME_IN_5x);
670 
671  return AnnotatePersonalToolbarFolder(sourceBookmarksFile,
672  targetBookmarksFile, toolbarName.get());
673 }
nsresult CopyFile(const nsAString &aSourceFileName, const nsAString &aTargetFileName)
nsresult ImportNetscapeBookmarks(const nsAString &aBookmarksFileName, const PRUnichar *aImportSourceNameKey)
nsresult InitializeBookmarks(nsIFile *aTargetProfile)
return NS_OK
#define MAXPATHLEN
nsresult CopyBookmarks(PRBool aReplace)
NS_IMPL_ISUPPORTS1(sbDeviceCapabilitiesUtils, sbIDeviceCapabilitiesUtils) sbDeviceCapabilitiesUtils
static nsresult GetImagePref(void *aTransform, nsIPrefBranch *aBranch)
const NS_PREFSERVICE_CONTRACTID
#define F(a)
nsresult AnnotatePersonalToolbarFolder(nsIFile *aSourceBookmarksFile, nsIFile *aTargetBookmarksFile, const char *aToolbarFolderName)
const nsIPrefBranch
void GetSourceProfile(const PRUnichar *aProfile)
#define NOTIFY_OBSERVERS(message, item)
nsresult CopyPreferences(PRBool aReplace)
var count
Definition: test_bug7406.js:32
#define MIGRATION_STARTED
#define COPY_DATA(func, replace, itemIndex)
#define MIGRATION_ENDED
this _dialogInput val(dateText)
nsresult CopyCookies(PRBool aReplace)
function cleanup()
#define PREF_FILE_NAME_IN_5x
nsresult TransformPreferences(const nsAString &aSourcePrefFileName, const nsAString &aTargetPrefFileName)
static nsresult GetHomepage(void *aTransform, nsIPrefBranch *aBranch)
#define COOKIES_FILE_NAME_IN_5x
void GetProfilePath(nsIProfileStartup *aStartup, nsCOMPtr< nsIFile > &aProfileDir)
observe data
Definition: FeedWriter.js:1329
var profileDir
#define BOOKMARKS_FILE_NAME_IN_5x
nsresult ImportNetscapeCookies(nsIFile *aCookiesFile)
static nsDogbertProfileMigrator::PrefTransform gTransforms[]
void GetMigrateDataFromArray(MigrationData *aDataArray, PRInt32 aDataArrayLength, PRBool aReplace, nsIFile *aSourceProfile, PRUint16 *aResult)