nsBrowserProfileMigratorUtils.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  * Asaf Romano <mozilla.mano@sent.com>
24  *
25  * Alternatively, the contents of this file may be used under the terms of
26  * either the GNU General Public License Version 2 or later (the "GPL"), or
27  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
28  * in which case the provisions of the GPL or the LGPL are applicable instead
29  * of those above. If you wish to allow use of your version of this file only
30  * under the terms of either the GPL or the LGPL, and not to allow others to
31  * use your version of this file under the terms of the MPL, indicate your
32  * decision by deleting the provisions above and replace them with the notice
33  * and other provisions required by the GPL or the LGPL. If you do not delete
34  * the provisions above, a recipient may use your version of this file under
35  * the terms of any one of the MPL, the GPL or the LGPL.
36  *
37  * ***** END LICENSE BLOCK ***** */
38 
40 #include "nsINavBookmarksService.h"
41 #include "nsBrowserCompsCID.h"
42 #include "nsToolkitCompsCID.h"
43 #include "nsIPlacesImportExportService.h"
44 #include "nsIFile.h"
45 #include "nsIInputStream.h"
46 #include "nsILineInputStream.h"
47 #include "nsIProperties.h"
48 #include "nsIProfileMigrator.h"
49 
50 #include "nsIURI.h"
51 #include "nsNetUtil.h"
52 #include "nsISupportsPrimitives.h"
53 
54 #include "nsAppDirectoryServiceDefs.h"
55 #include "nsIRDFService.h"
56 #include "nsIStringBundle.h"
57 #include "nsISupportsArray.h"
58 #include "nsXPCOMCID.h"
59 
60 #define MIGRATION_BUNDLE "chrome://browser/locale/migration/migration.properties"
61 
62 #define BOOKMARKS_FILE_NAME NS_LITERAL_STRING("bookmarks.html")
63 
64 void SetUnicharPref(const char* aPref, const nsAString& aValue,
65  nsIPrefBranch* aPrefs)
66 {
67  nsCOMPtr<nsISupportsString> supportsString =
68  do_CreateInstance(NS_SUPPORTS_STRING_CONTRACTID);
69  if (supportsString) {
70  supportsString->SetData(aValue);
71  aPrefs->SetComplexValue(aPref, NS_GET_IID(nsISupportsString),
72  supportsString);
73  }
74 }
75 
76 void SetProxyPref(const nsAString& aHostPort, const char* aPref,
77  const char* aPortPref, nsIPrefBranch* aPrefs)
78 {
79  nsCOMPtr<nsIURI> uri;
80  nsCAutoString host;
81  PRInt32 portValue;
82 
83  // try parsing it as a URI first
84  if (NS_SUCCEEDED(NS_NewURI(getter_AddRefs(uri), aHostPort))
85  && NS_SUCCEEDED(uri->GetHost(host))
86  && !host.IsEmpty()
87  && NS_SUCCEEDED(uri->GetPort(&portValue))) {
88  SetUnicharPref(aPref, NS_ConvertUTF8toUTF16(host), aPrefs);
89  aPrefs->SetIntPref(aPortPref, portValue);
90  }
91  else {
92  nsAutoString hostPort(aHostPort);
93  PRInt32 portDelimOffset = hostPort.RFindChar(':');
94  if (portDelimOffset > 0) {
95  SetUnicharPref(aPref, Substring(hostPort, 0, portDelimOffset), aPrefs);
96  nsAutoString port(Substring(hostPort, portDelimOffset + 1));
97  nsresult stringErr;
98  portValue = port.ToInteger(&stringErr);
99  if (NS_SUCCEEDED(stringErr))
100  aPrefs->SetIntPref(aPortPref, portValue);
101  }
102  else
103  SetUnicharPref(aPref, hostPort, aPrefs);
104  }
105 }
106 
107 void ParseOverrideServers(const nsAString& aServers, nsIPrefBranch* aBranch)
108 {
109  // Windows (and Opera) formats its proxy override list in the form:
110  // server;server;server where server is a server name or ip address,
111  // or "<local>". Mozilla's format is server,server,server, and <local>
112  // must be translated to "localhost,127.0.0.1"
113  nsAutoString override(aServers);
114  PRInt32 left = 0, right = 0;
115  for (;;) {
116  right = override.FindChar(';', right);
117  const nsAString& host = Substring(override, left,
118  (right < 0 ? override.Length() : right) - left);
119  if (host.EqualsLiteral("<local>"))
120  override.Replace(left, 7, NS_LITERAL_STRING("localhost,127.0.0.1"));
121  if (right < 0)
122  break;
123  left = right + 1;
124  override.Replace(right, 1, NS_LITERAL_STRING(","));
125  }
126  SetUnicharPref("network.proxy.no_proxies_on", override, aBranch);
127 }
128 
129 void GetMigrateDataFromArray(MigrationData* aDataArray, PRInt32 aDataArrayLength,
130  PRBool aReplace, nsIFile* aSourceProfile,
131  PRUint16* aResult)
132 {
133  nsCOMPtr<nsIFile> sourceFile;
134  PRBool exists;
135  MigrationData* cursor;
136  MigrationData* end = aDataArray + aDataArrayLength;
137  for (cursor = aDataArray; cursor < end && cursor->fileName; ++cursor) {
138  // When in replace mode, all items can be imported.
139  // When in non-replace mode, only items that do not require file replacement
140  // can be imported.
141  if (aReplace || !cursor->replaceOnly) {
142  aSourceProfile->Clone(getter_AddRefs(sourceFile));
143  sourceFile->Append(nsDependentString(cursor->fileName));
144  sourceFile->Exists(&exists);
145  if (exists)
146  *aResult |= cursor->sourceFlag;
147  }
148  NS_Free(cursor->fileName);
149  cursor->fileName = nsnull;
150  }
151 }
152 
153 void
154 GetProfilePath(nsIProfileStartup* aStartup, nsCOMPtr<nsIFile>& aProfileDir)
155 {
156  if (aStartup) {
157  aStartup->GetDirectory(getter_AddRefs(aProfileDir));
158  }
159  else {
160  nsCOMPtr<nsIProperties> dirSvc
161  (do_GetService(NS_DIRECTORY_SERVICE_CONTRACTID));
162  if (dirSvc) {
163  dirSvc->Get(NS_APP_USER_PROFILE_50_DIR, NS_GET_IID(nsIFile),
164  (void**) getter_AddRefs(aProfileDir));
165  }
166  }
167 }
168 
169 nsresult
170 AnnotatePersonalToolbarFolder(nsIFile* aSourceBookmarksFile,
171  nsIFile* aTargetBookmarksFile,
172  const char* aToolbarFolderName)
173 {
174  nsCOMPtr<nsIInputStream> fileInputStream;
175  nsresult rv = NS_NewLocalFileInputStream(getter_AddRefs(fileInputStream),
176  aSourceBookmarksFile);
177  NS_ENSURE_SUCCESS(rv, rv);
178 
179  nsCOMPtr<nsIOutputStream> outputStream;
180  rv = NS_NewLocalFileOutputStream(getter_AddRefs(outputStream),
181  aTargetBookmarksFile);
182  NS_ENSURE_SUCCESS(rv, rv);
183 
184  nsCOMPtr<nsILineInputStream> lineInputStream =
185  do_QueryInterface(fileInputStream, &rv);
186  NS_ENSURE_SUCCESS(rv, rv);
187 
188  nsCAutoString sourceBuffer;
189  nsCAutoString targetBuffer;
190  PRBool moreData = PR_FALSE;
191  PRUint32 bytesWritten = 0;
192  do {
193  lineInputStream->ReadLine(sourceBuffer, &moreData);
194  if (!moreData)
195  break;
196 
197  PRInt32 nameOffset = sourceBuffer.Find(aToolbarFolderName);
198  if (nameOffset >= 0) {
199  // Found the personal toolbar name on a line, check to make sure it's
200  // actually a folder.
201  NS_NAMED_LITERAL_CSTRING(folderPrefix, "<DT><H3 ");
202  PRInt32 folderPrefixOffset = sourceBuffer.Find(folderPrefix);
203  if (folderPrefixOffset >= 0)
204  sourceBuffer.Insert(NS_LITERAL_CSTRING("PERSONAL_TOOLBAR_FOLDER=\"true\" "),
205  folderPrefixOffset + folderPrefix.Length());
206  }
207 
208  targetBuffer.Assign(sourceBuffer);
209  targetBuffer.Append("\r\n");
210  outputStream->Write(targetBuffer.get(), targetBuffer.Length(),
211  &bytesWritten);
212  }
213  while (1);
214 
215  outputStream->Close();
216 
217  return NS_OK;
218 }
219 
220 nsresult
221 ImportBookmarksHTML(nsIFile* aBookmarksFile,
222  PRBool aImportIntoRoot,
223  PRBool aOverwriteDefaults,
224  const PRUnichar* aImportSourceNameKey)
225 {
226  nsresult rv;
227 
228  nsCOMPtr<nsILocalFile> localFile(do_QueryInterface(aBookmarksFile));
229  NS_ENSURE_TRUE(localFile, NS_ERROR_FAILURE);
230  nsCOMPtr<nsIPlacesImportExportService> importer = do_GetService(NS_PLACESIMPORTEXPORTSERVICE_CONTRACTID, &rv);
231  NS_ENSURE_SUCCESS(rv, rv);
232 
233  // Import file directly into the bookmarks root folder.
234  if (aImportIntoRoot) {
235  rv = importer->ImportHTMLFromFile(localFile, aOverwriteDefaults);
236  NS_ENSURE_SUCCESS(rv, rv);
237  return NS_OK;
238  }
239 
240  // Get the source application name.
241  nsCOMPtr<nsIStringBundleService> bundleService =
242  do_GetService(NS_STRINGBUNDLE_CONTRACTID, &rv);
243  NS_ENSURE_SUCCESS(rv, rv);
244  nsCOMPtr<nsIStringBundle> bundle;
245  rv = bundleService->CreateBundle(MIGRATION_BUNDLE, getter_AddRefs(bundle));
246  NS_ENSURE_SUCCESS(rv, rv);
247 
248  nsString sourceName;
249  rv = bundle->GetStringFromName(aImportSourceNameKey,
250  getter_Copies(sourceName));
251  NS_ENSURE_SUCCESS(rv, rv);
252 
253  const PRUnichar* sourceNameStrings[] = { sourceName.get() };
254  nsString importedBookmarksTitle;
255  rv = bundle->FormatStringFromName(NS_LITERAL_STRING("importedBookmarksFolder").get(),
256  sourceNameStrings, 1,
257  getter_Copies(importedBookmarksTitle));
258  NS_ENSURE_SUCCESS(rv, rv);
259 
260  // Get the bookmarks service.
261  nsCOMPtr<nsINavBookmarksService> bms =
262  do_GetService(NS_NAVBOOKMARKSSERVICE_CONTRACTID, &rv);
263  NS_ENSURE_SUCCESS(rv, rv);
264 
265  // Create an imported bookmarks folder under the bookmarks menu.
266  PRInt64 root;
267  rv = bms->GetBookmarksMenuFolder(&root);
268  NS_ENSURE_SUCCESS(rv, rv);
269 
270  PRInt64 folder;
271  rv = bms->CreateFolder(root, NS_ConvertUTF16toUTF8(importedBookmarksTitle),
272  nsINavBookmarksService::DEFAULT_INDEX, &folder);
273  NS_ENSURE_SUCCESS(rv, rv);
274 
275  // Import the bookmarks into the folder.
276  return importer->ImportHTMLFromFileToFolder(localFile, folder, PR_FALSE);
277 }
278 
279 nsresult
280 InitializeBookmarks(nsIFile* aTargetProfile)
281 {
282  nsCOMPtr<nsIFile> bookmarksFile;
283  aTargetProfile->Clone(getter_AddRefs(bookmarksFile));
284  bookmarksFile->Append(BOOKMARKS_FILE_NAME);
285 
286  nsresult rv = ImportBookmarksHTML(bookmarksFile, PR_TRUE, PR_TRUE, EmptyString().get());
287  NS_ENSURE_SUCCESS(rv, rv);
288  return NS_OK;
289 }
nsresult InitializeBookmarks(nsIFile *aTargetProfile)
const NS_APP_USER_PROFILE_50_DIR
return NS_OK
#define MIGRATION_BUNDLE
var dirSvc
onPageChanged aValue
Definition: FeedWriter.js:1395
void ParseOverrideServers(const nsAString &aServers, nsIPrefBranch *aBranch)
nsresult ImportBookmarksHTML(nsIFile *aBookmarksFile, PRBool aImportIntoRoot, PRBool aOverwriteDefaults, const PRUnichar *aImportSourceNameKey)
nsresult AnnotatePersonalToolbarFolder(nsIFile *aSourceBookmarksFile, nsIFile *aTargetBookmarksFile, const char *aToolbarFolderName)
function right(ele) rect(ele).right
const nsIPrefBranch
#define BOOKMARKS_FILE_NAME
var bundle
var uri
Definition: FeedWriter.js:1135
void SetUnicharPref(const char *aPref, const nsAString &aValue, nsIPrefBranch *aPrefs)
void GetProfilePath(nsIProfileStartup *aStartup, nsCOMPtr< nsIFile > &aProfileDir)
void SetProxyPref(const nsAString &aHostPort, const char *aPref, const char *aPortPref, nsIPrefBranch *aPrefs)
const nsISupportsString
#define NS_PLACESIMPORTEXPORTSERVICE_CONTRACTID
void GetMigrateDataFromArray(MigrationData *aDataArray, PRInt32 aDataArrayLength, PRBool aReplace, nsIFile *aSourceProfile, PRUint16 *aResult)