nsPhoenixProfileMigrator.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 
39 #include "nsDirectoryServiceDefs.h"
40 #include "nsIObserverService.h"
41 #include "nsIPrefService.h"
42 #include "nsIServiceManager.h"
43 #include "nsISupportsArray.h"
44 #include "nsISupportsPrimitives.h"
45 #include "nsNetCID.h"
46 #include "nsNetUtil.h"
48 
50 // nsPhoenixProfileMigrator
51 
52 #define FILE_NAME_BOOKMARKS NS_LITERAL_STRING("bookmarks.html")
53 #define FILE_NAME_COOKIES NS_LITERAL_STRING("cookies.txt")
54 #define FILE_NAME_SITEPERM_OLD NS_LITERAL_STRING("cookperm.txt")
55 #define FILE_NAME_SITEPERM_NEW NS_LITERAL_STRING("hostperm.1")
56 #define FILE_NAME_CERT8DB NS_LITERAL_STRING("cert8.db")
57 #define FILE_NAME_KEY3DB NS_LITERAL_STRING("key3.db")
58 #define FILE_NAME_SECMODDB NS_LITERAL_STRING("secmod.db")
59 #define FILE_NAME_HISTORY NS_LITERAL_STRING("history.dat")
60 #define FILE_NAME_FORMHISTORY NS_LITERAL_STRING("formhistory.dat")
61 #define FILE_NAME_LOCALSTORE NS_LITERAL_STRING("localstore.rdf")
62 #define FILE_NAME_MIMETYPES NS_LITERAL_STRING("mimeTypes.rdf")
63 #define FILE_NAME_DOWNLOADS NS_LITERAL_STRING("downloads.rdf")
64 #define FILE_NAME_PREFS NS_LITERAL_STRING("prefs.js")
65 #define FILE_NAME_USER_PREFS NS_LITERAL_STRING("user.js")
66 #define FILE_NAME_USERCHROME NS_LITERAL_STRING("userChrome.css")
67 #define FILE_NAME_USERCONTENT NS_LITERAL_STRING("userContent.css")
68 #define DIR_NAME_CHROME NS_LITERAL_STRING("chrome")
69 
71 
73 {
74  mObserverService = do_GetService("@mozilla.org/observer-service;1");
75 }
76 
78 {
79 }
80 
82 // nsIBrowserProfileMigrator
83 
84 NS_IMETHODIMP
85 nsPhoenixProfileMigrator::Migrate(PRUint16 aItems, nsIProfileStartup* aStartup, const PRUnichar* aProfile)
86 {
87  nsresult rv = NS_OK;
88 
89  // At this time the only reason for this migrator is to get data across from the
90  // Phoenix profile directory on initial run, so we don't need to support after-the-fact
91  // importing.
92  NS_ASSERTION(aStartup, "Can't migrate from Phoenix/Firebird/Firefox profiles once Firefox is running!");
93  if (!aStartup)
94  return NS_ERROR_FAILURE;
95 
96  if (!mTargetProfile) {
97  GetProfilePath(aStartup, mTargetProfile);
98  if (!mTargetProfile) return NS_ERROR_FAILURE;
99  }
100  if (!mSourceProfile)
101  GetSourceProfile(aProfile);
102 
104 
111 
112  if (aItems & nsIBrowserProfileMigrator::SETTINGS ||
115  !aItems) {
116  // Permissions (Images, Cookies, Popups)
119  }
120 
122 
123  return rv;
124 }
125 
126 NS_IMETHODIMP
127 nsPhoenixProfileMigrator::GetMigrateData(const PRUnichar* aProfile,
128  PRBool aReplace,
129  PRUint16* aResult)
130 {
131  *aResult = 0;
132  if (!mSourceProfile) {
133  GetSourceProfile(aProfile);
134  if (!mSourceProfile)
135  return NS_ERROR_FILE_NOT_FOUND;
136  }
137 
138  MigrationData data[] = { { ToNewUnicode(FILE_NAME_PREFS),
140  PR_TRUE },
141  { ToNewUnicode(FILE_NAME_USER_PREFS),
143  PR_TRUE },
144  { ToNewUnicode(FILE_NAME_COOKIES),
146  PR_TRUE },
147  { ToNewUnicode(FILE_NAME_HISTORY),
149  PR_TRUE },
150  { ToNewUnicode(FILE_NAME_BOOKMARKS),
152  PR_TRUE },
153  { ToNewUnicode(FILE_NAME_DOWNLOADS),
155  PR_TRUE },
156  { ToNewUnicode(FILE_NAME_MIMETYPES),
158  PR_TRUE },
159  { ToNewUnicode(FILE_NAME_USERCHROME),
161  PR_TRUE },
162  { ToNewUnicode(FILE_NAME_USERCONTENT),
164  PR_TRUE },
165  { ToNewUnicode(FILE_NAME_FORMHISTORY),
167  PR_TRUE } };
168 
169  // Frees file name strings allocated above.
170  GetMigrateDataFromArray(data, sizeof(data)/sizeof(MigrationData),
171  aReplace, mSourceProfile, aResult);
172 
173  // Now locate passwords
174  nsCString signonsFileName;
175  GetSignonFileName(aReplace, getter_Copies(signonsFileName));
176 
177  if (!signonsFileName.IsEmpty()) {
178  NS_ConvertASCIItoUTF16 fileName(signonsFileName);
179  nsCOMPtr<nsIFile> sourcePasswordsFile;
180  mSourceProfile->Clone(getter_AddRefs(sourcePasswordsFile));
181  sourcePasswordsFile->Append(fileName);
182 
183  PRBool exists;
184  sourcePasswordsFile->Exists(&exists);
185  if (exists)
187  }
188 
189  return NS_OK;
190 }
191 
192 NS_IMETHODIMP
193 nsPhoenixProfileMigrator::GetSourceExists(PRBool* aResult)
194 {
195  nsCOMPtr<nsISupportsArray> profiles;
196  GetSourceProfiles(getter_AddRefs(profiles));
197 
198  if (profiles) {
199  PRUint32 count;
200  profiles->Count(&count);
201  *aResult = count > 0;
202  }
203  else
204  *aResult = PR_FALSE;
205 
206  return NS_OK;
207 }
208 
209 NS_IMETHODIMP
210 nsPhoenixProfileMigrator::GetSourceHasMultipleProfiles(PRBool* aResult)
211 {
212  nsCOMPtr<nsISupportsArray> profiles;
213  GetSourceProfiles(getter_AddRefs(profiles));
214 
215  if (profiles) {
216  PRUint32 count;
217  profiles->Count(&count);
218  *aResult = count > 1;
219  }
220  else
221  *aResult = PR_FALSE;
222 
223  return NS_OK;
224 }
225 
226 NS_IMETHODIMP
227 nsPhoenixProfileMigrator::GetSourceProfiles(nsISupportsArray** aResult)
228 {
229  if (!mProfileNames && !mProfileLocations) {
230  mProfileNames = do_CreateInstance(NS_SUPPORTSARRAY_CONTRACTID);
231  mProfileLocations = do_CreateInstance(NS_SUPPORTSARRAY_CONTRACTID);
232  NS_ENSURE_TRUE(mProfileNames && mProfileLocations, NS_ERROR_UNEXPECTED);
233 
234  // Fills mProfileNames and mProfileLocations
236  }
237 
238  NS_IF_ADDREF(*aResult = mProfileNames);
239  return NS_OK;
240 }
241 NS_IMETHODIMP
242 nsPhoenixProfileMigrator::GetSourceHomePageURL(nsACString& aResult)
243 {
244  aResult.Truncate();
245  return NS_OK;
246 }
248 // nsPhoenixProfileMigrator
249 
250 nsresult
252 {
253  PRUint32 count;
254  mProfileNames->Count(&count);
255  for (PRUint32 i = 0; i < count; ++i) {
256  nsCOMPtr<nsISupportsString> str;
257  mProfileNames->QueryElementAt(i, NS_GET_IID(nsISupportsString),
258  getter_AddRefs(str));
259  nsString profileName;
260  str->GetData(profileName);
261  if (profileName.Equals(aProfile)) {
262  mProfileLocations->QueryElementAt(i, NS_GET_IID(nsILocalFile),
263  getter_AddRefs(mSourceProfile));
264  break;
265  }
266  }
267 
268  return NS_OK;
269 }
270 
271 nsresult
273 {
274  // Find the Phoenix Registry
275  nsCOMPtr<nsIProperties> fileLocator(do_GetService("@mozilla.org/file/directory_service;1"));
276  nsCOMPtr<nsILocalFile> phoenixRegistry;
277 #ifdef XP_WIN
278  fileLocator->Get(NS_WIN_APPDATA_DIR, NS_GET_IID(nsILocalFile), getter_AddRefs(phoenixRegistry));
279 
280  phoenixRegistry->Append(NS_LITERAL_STRING("Phoenix"));
281  phoenixRegistry->Append(NS_LITERAL_STRING("registry.dat"));
282 #elif defined(XP_MACOSX)
283  fileLocator->Get(NS_MAC_USER_LIB_DIR, NS_GET_IID(nsILocalFile), getter_AddRefs(phoenixRegistry));
284 
285  phoenixRegistry->Append(NS_LITERAL_STRING("Phoenix"));
286  phoenixRegistry->Append(NS_LITERAL_STRING("Application Registry"));
287 #elif defined(XP_UNIX)
288  fileLocator->Get(NS_UNIX_HOME_DIR, NS_GET_IID(nsILocalFile), getter_AddRefs(phoenixRegistry));
289 
290  phoenixRegistry->Append(NS_LITERAL_STRING(".phoenix"));
291  phoenixRegistry->Append(NS_LITERAL_STRING("appreg"));
292 #elif defined(XP_BEOS)
293  fileLocator->Get(NS_BEOS_SETTINGS_DIR, NS_GET_IID(nsILocalFile), getter_AddRefs(phoenixRegistry));
294 
295  phoenixRegistry->Append(NS_LITERAL_STRING("Phoenix"));
296  phoenixRegistry->Append(NS_LITERAL_STRING("appreg"));
297 #elif defined(XP_OS2)
298  fileLocator->Get(NS_OS2_HOME_DIR, NS_GET_IID(nsILocalFile), getter_AddRefs(phoenixRegistry));
299 
300  phoenixRegistry->Append(NS_LITERAL_STRING("Phoenix"));
301  phoenixRegistry->Append(NS_LITERAL_STRING("registry.dat"));
302 #endif
303 
304 
305  return GetProfileDataFromRegistry(phoenixRegistry, mProfileNames, mProfileLocations);
306 }
307 
308 nsresult
310 {
311  nsresult rv = NS_OK;
312  if (!aReplace)
313  return rv;
314 
315  // Prefs files
318 
319  // Security Stuff
323 
324  // User MIME Type overrides
326 
327  rv |= CopyUserStyleSheets();
328  return rv;
329 }
330 
331 nsresult
333 {
334  nsresult rv = NS_OK;
335 
336  nsCOMPtr<nsIFile> sourceUserContent;
337  mSourceProfile->Clone(getter_AddRefs(sourceUserContent));
338  sourceUserContent->Append(DIR_NAME_CHROME);
339  sourceUserContent->Append(FILE_NAME_USERCONTENT);
340 
341  PRBool exists = PR_FALSE;
342  sourceUserContent->Exists(&exists);
343  if (exists) {
344  nsCOMPtr<nsIFile> targetUserContent;
345  mTargetProfile->Clone(getter_AddRefs(targetUserContent));
346  targetUserContent->Append(DIR_NAME_CHROME);
347  nsCOMPtr<nsIFile> targetChromeDir;
348  targetUserContent->Clone(getter_AddRefs(targetChromeDir));
349  targetUserContent->Append(FILE_NAME_USERCONTENT);
350 
351  targetUserContent->Exists(&exists);
352  if (exists)
353  targetUserContent->Remove(PR_FALSE);
354 
355  rv |= sourceUserContent->CopyTo(targetChromeDir, FILE_NAME_USERCONTENT);
356  }
357 
358  nsCOMPtr<nsIFile> sourceUserChrome;
359  mSourceProfile->Clone(getter_AddRefs(sourceUserChrome));
360  sourceUserChrome->Append(DIR_NAME_CHROME);
361  sourceUserChrome->Append(FILE_NAME_USERCHROME);
362 
363  sourceUserChrome->Exists(&exists);
364  if (exists) {
365  nsCOMPtr<nsIFile> targetUserChrome;
366  mTargetProfile->Clone(getter_AddRefs(targetUserChrome));
367  targetUserChrome->Append(DIR_NAME_CHROME);
368  nsCOMPtr<nsIFile> targetChromeDir;
369  targetUserChrome->Clone(getter_AddRefs(targetChromeDir));
370  targetUserChrome->Append(FILE_NAME_USERCHROME);
371 
372  targetUserChrome->Exists(&exists);
373  if (exists)
374  targetUserChrome->Remove(PR_FALSE);
375 
376  rv |= sourceUserChrome->CopyTo(targetChromeDir, FILE_NAME_USERCHROME);
377  }
378 
379  return rv;
380 }
381 
382 nsresult
384 {
385  return aReplace ? CopyFile(FILE_NAME_COOKIES, FILE_NAME_COOKIES) : NS_OK;
386 }
387 
388 nsresult
390 {
391  return aReplace ? CopyFile(FILE_NAME_HISTORY, FILE_NAME_HISTORY) : NS_OK;
392 }
393 
394 nsresult
396 {
397  nsresult rv;
398 
399  nsCString signonsFileName;
400  if (!aReplace)
401  return NS_OK;
402 
403  // Find out what the signons file was called, this is stored in a pref
404  // in Seamonkey.
405  nsCOMPtr<nsIPrefService> psvc(do_GetService(NS_PREFSERVICE_CONTRACTID));
406  psvc->ResetPrefs();
407 
408  nsCOMPtr<nsIFile> seamonkeyPrefsFile;
409  mSourceProfile->Clone(getter_AddRefs(seamonkeyPrefsFile));
410  seamonkeyPrefsFile->Append(FILE_NAME_PREFS);
411  psvc->ReadUserPrefs(seamonkeyPrefsFile);
412 
413  nsCOMPtr<nsIPrefBranch> branch(do_QueryInterface(psvc));
414  rv = branch->GetCharPref("signon.SignonFileName", getter_Copies(signonsFileName));
415 
416  if (signonsFileName.IsEmpty())
417  return NS_ERROR_FILE_NOT_FOUND;
418 
419  NS_ConvertASCIItoUTF16 fileName(signonsFileName);
420  return aReplace ? CopyFile(fileName, fileName) : NS_OK;
421 }
422 
423 nsresult
425 {
426  // This overwrites the defaults. This might be ok in this instance.
428 }
429 
430 nsresult
432 {
433  if (!aReplace)
434  return NS_OK;
435 
436  nsresult rv = NS_OK;
440 
441  return rv;
442 }
443 
nsresult CopyFile(const nsAString &aSourceFileName, const nsAString &aTargetFileName)
#define FILE_NAME_KEY3DB
nsresult CopyHistory(PRBool aReplace)
return NS_OK
NS_IMPL_ISUPPORTS1(sbDeviceCapabilitiesUtils, sbIDeviceCapabilitiesUtils) sbDeviceCapabilitiesUtils
#define FILE_NAME_USERCHROME
const NS_PREFSERVICE_CONTRACTID
#define FILE_NAME_DOWNLOADS
nsresult CopyOtherData(PRBool aReplace)
#define FILE_NAME_BOOKMARKS
#define FILE_NAME_CERT8DB
nsresult GetSourceProfile(const PRUnichar *aProfile)
#define NOTIFY_OBSERVERS(message, item)
#define FILE_NAME_LOCALSTORE
#define FILE_NAME_COOKIES
#define FILE_NAME_SITEPERM_OLD
var count
Definition: test_bug7406.js:32
nsresult CopyCookies(PRBool aReplace)
#define MIGRATION_STARTED
nsresult GetProfileDataFromRegistry(nsILocalFile *aRegistryFile, nsISupportsArray *aProfileNames, nsISupportsArray *aProfileLocations)
#define COPY_DATA(func, replace, itemIndex)
#define MIGRATION_ENDED
nsresult CopyPasswords(PRBool aReplace)
nsresult CopyPreferences(PRBool aReplace)
#define FILE_NAME_SECMODDB
nsresult CopyBookmarks(PRBool aReplace)
#define FILE_NAME_HISTORY
#define FILE_NAME_PREFS
#define FILE_NAME_USER_PREFS
#define FILE_NAME_USERCONTENT
void GetProfilePath(nsIProfileStartup *aStartup, nsCOMPtr< nsIFile > &aProfileDir)
#define DIR_NAME_CHROME
const nsISupportsString
observe data
Definition: FeedWriter.js:1329
_getSelectedPageStyle s i
#define FILE_NAME_SITEPERM_NEW
#define FILE_NAME_FORMHISTORY
#define FILE_NAME_MIMETYPES
void GetMigrateDataFromArray(MigrationData *aDataArray, PRInt32 aDataArrayLength, PRBool aReplace, nsIFile *aSourceProfile, PRUint16 *aResult)
nsresult GetSignonFileName(PRBool aReplace, char **aFileName)