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
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/
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
15 * The Original Code is The Browser Profile Migrator.
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.
22 * Ben Goodger <ben@bengoodger.com>
23 * Asaf Romano <mozilla.mano@sent.com>
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.
37 * ***** END LICENSE BLOCK ***** */
39 #include "nsBrowserProfileMigratorUtils.h"
40 #include "nsINavBookmarksService.h"
41 #include "nsBrowserCompsCID.h"
42 #include "nsToolkitCompsCID.h"
43 #include "nsIPlacesImportExportService.h"
45 #include "nsIInputStream.h"
46 #include "nsILineInputStream.h"
47 #include "nsIProperties.h"
48 #include "nsIProfileMigrator.h"
51 #include "nsNetUtil.h"
52 #include "nsISupportsPrimitives.h"
54 #include "nsAppDirectoryServiceDefs.h"
55 #include "nsIRDFService.h"
56 #include "nsIStringBundle.h"
57 #include "nsISupportsArray.h"
58 #include "nsXPCOMCID.h"
60 #define MIGRATION_BUNDLE "chrome://browser/locale/migration/migration.properties"
62 #define BOOKMARKS_FILE_NAME NS_LITERAL_STRING("bookmarks.html")
64 void SetUnicharPref(const char* aPref
, const nsAString
& aValue
,
65 nsIPrefBranch
* aPrefs
)
67 nsCOMPtr
<nsISupportsString
> supportsString
=
68 do_CreateInstance(NS_SUPPORTS_STRING_CONTRACTID
);
70 supportsString
->SetData(aValue
);
71 aPrefs
->SetComplexValue(aPref
, NS_GET_IID(nsISupportsString
),
76 void SetProxyPref(const nsAString
& aHostPort
, const char* aPref
,
77 const char* aPortPref
, nsIPrefBranch
* aPrefs
)
83 // try parsing it as a URI first
84 if (NS_SUCCEEDED(NS_NewURI(getter_AddRefs(uri
), aHostPort
))
85 && NS_SUCCEEDED(uri
->GetHost(host
))
87 && NS_SUCCEEDED(uri
->GetPort(&portValue
))) {
88 SetUnicharPref(aPref
, NS_ConvertUTF8toUTF16(host
), aPrefs
);
89 aPrefs
->SetIntPref(aPortPref
, portValue
);
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));
98 portValue
= port
.ToInteger(&stringErr
);
99 if (NS_SUCCEEDED(stringErr
))
100 aPrefs
->SetIntPref(aPortPref
, portValue
);
103 SetUnicharPref(aPref
, hostPort
, aPrefs
);
107 void ParseOverrideServers(const nsAString
& aServers
, nsIPrefBranch
* aBranch
)
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;
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"));
124 override
.Replace(right
, 1, NS_LITERAL_STRING(","));
126 SetUnicharPref("network.proxy.no_proxies_on", override
, aBranch
);
129 void GetMigrateDataFromArray(MigrationData
* aDataArray
, PRInt32 aDataArrayLength
,
130 PRBool aReplace
, nsIFile
* aSourceProfile
,
133 nsCOMPtr
<nsIFile
> sourceFile
;
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
141 if (aReplace
|| !cursor
->replaceOnly
) {
142 aSourceProfile
->Clone(getter_AddRefs(sourceFile
));
143 sourceFile
->Append(nsDependentString(cursor
->fileName
));
144 sourceFile
->Exists(&exists
);
146 *aResult
|= cursor
->sourceFlag
;
148 NS_Free(cursor
->fileName
);
149 cursor
->fileName
= nsnull
;
154 GetProfilePath(nsIProfileStartup
* aStartup
, nsCOMPtr
<nsIFile
>& aProfileDir
)
157 aStartup
->GetDirectory(getter_AddRefs(aProfileDir
));
160 nsCOMPtr
<nsIProperties
> dirSvc
161 (do_GetService(NS_DIRECTORY_SERVICE_CONTRACTID
));
163 dirSvc
->Get(NS_APP_USER_PROFILE_50_DIR
, NS_GET_IID(nsIFile
),
164 (void**) getter_AddRefs(aProfileDir
));
170 AnnotatePersonalToolbarFolder(nsIFile
* aSourceBookmarksFile
,
171 nsIFile
* aTargetBookmarksFile
,
172 const char* aToolbarFolderName
)
174 nsCOMPtr
<nsIInputStream
> fileInputStream
;
175 nsresult rv
= NS_NewLocalFileInputStream(getter_AddRefs(fileInputStream
),
176 aSourceBookmarksFile
);
177 NS_ENSURE_SUCCESS(rv
, rv
);
179 nsCOMPtr
<nsIOutputStream
> outputStream
;
180 rv
= NS_NewLocalFileOutputStream(getter_AddRefs(outputStream
),
181 aTargetBookmarksFile
);
182 NS_ENSURE_SUCCESS(rv
, rv
);
184 nsCOMPtr
<nsILineInputStream
> lineInputStream
=
185 do_QueryInterface(fileInputStream
, &rv
);
186 NS_ENSURE_SUCCESS(rv
, rv
);
188 nsCAutoString sourceBuffer
;
189 nsCAutoString targetBuffer
;
190 PRBool moreData
= PR_FALSE
;
191 PRUint32 bytesWritten
= 0;
193 lineInputStream
->ReadLine(sourceBuffer
, &moreData
);
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());
208 targetBuffer
.Assign(sourceBuffer
);
209 targetBuffer
.Append("\r\n");
210 outputStream
->Write(targetBuffer
.get(), targetBuffer
.Length(),
215 outputStream
->Close();
221 ImportBookmarksHTML(nsIFile
* aBookmarksFile
,
222 PRBool aImportIntoRoot
,
223 PRBool aOverwriteDefaults
,
224 const PRUnichar
* aImportSourceNameKey
)
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
);
233 // Import file directly into the bookmarks root folder.
234 if (aImportIntoRoot
) {
235 rv
= importer
->ImportHTMLFromFile(localFile
, aOverwriteDefaults
);
236 NS_ENSURE_SUCCESS(rv
, rv
);
240 // Get the source application name.
241 nsCOMPtr
<nsIStringBundleService
> bundleService
=
242 do_GetService(NS_STRINGBUNDLE_CONTRACTID
, &rv
);
243 NS_ENSURE_SUCCESS(rv
, rv
);
245 nsCOMPtr
<nsIStringBundle
> bundle
;
246 rv
= bundleService
->CreateBundle(MIGRATION_BUNDLE
, getter_AddRefs(bundle
));
247 NS_ENSURE_SUCCESS(rv
, rv
);
250 bundle
->GetStringFromName(aImportSourceNameKey
, getter_Copies(sourceName
));
252 const PRUnichar
* sourceNameStrings
[] = { sourceName
.get() };
253 nsString importedBookmarksTitle
;
254 bundle
->FormatStringFromName(NS_LITERAL_STRING("importedBookmarksFolder").get(),
255 sourceNameStrings
, 1,
256 getter_Copies(importedBookmarksTitle
));
258 // Get the bookmarks service.
259 nsCOMPtr
<nsINavBookmarksService
> bms
=
260 do_GetService(NS_NAVBOOKMARKSSERVICE_CONTRACTID
, &rv
);
261 NS_ENSURE_SUCCESS(rv
, rv
);
263 // Create an imported bookmarks folder under the bookmarks menu.
265 rv
= bms
->GetBookmarksMenuFolder(&root
);
266 NS_ENSURE_SUCCESS(rv
, rv
);
269 rv
= bms
->CreateFolder(root
, NS_ConvertUTF16toUTF8(importedBookmarksTitle
),
271 NS_ENSURE_SUCCESS(rv
, rv
);
273 // Import the bookmarks into the folder.
274 return importer
->ImportHTMLFromFileToFolder(localFile
, folder
, PR_FALSE
);
278 InitializeBookmarks(nsIFile
* aTargetProfile
)
280 nsCOMPtr
<nsIFile
> bookmarksFile
;
281 aTargetProfile
->Clone(getter_AddRefs(bookmarksFile
));
282 bookmarksFile
->Append(BOOKMARKS_FILE_NAME
);
284 nsresult rv
= ImportBookmarksHTML(bookmarksFile
, PR_TRUE
, PR_TRUE
, EmptyString().get());
285 NS_ENSURE_SUCCESS(rv
, rv
);