Bug 556644 - 3. Let the browser reset bookmarks from the omnijar, r=sdwilsh sr=vlad a=blocking2.0

This commit is contained in:
Michael Wu 2010-08-10 15:15:26 -07:00
Родитель f70ee665c2
Коммит 9b8be226d8
4 изменённых файлов: 71 добавлений и 15 удалений

Просмотреть файл

@ -52,6 +52,11 @@ const XULNS = "http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul";
Cu.import("resource://gre/modules/XPCOMUtils.jsm");
Cu.import("resource://gre/modules/Services.jsm");
XPCOMUtils.defineLazyGetter(this, "NetUtil", function() {
Cu.import("resource://gre/modules/NetUtil.jsm");
return NetUtil;
});
const PREF_EM_NEW_ADDONS_LIST = "extensions.newAddons";
const PREF_PLUGINS_NOTIFYUSER = "plugins.update.notifyUser";
const PREF_PLUGINS_UPDATEURL = "plugins.update.url";
@ -840,16 +845,18 @@ BrowserGlue.prototype = {
var dirService = Cc["@mozilla.org/file/directory_service;1"].
getService(Ci.nsIProperties);
var bookmarksFile = null;
var bookmarksURI = null;
if (restoreDefaultBookmarks) {
// User wants to restore bookmarks.html file from default profile folder
bookmarksFile = dirService.get("profDef", Ci.nsILocalFile);
bookmarksFile.append("bookmarks.html");
bookmarksURI = NetUtil.newURI("resource:///defaults/profile/bookmarks.html");
}
else {
var bookmarksFile = dirService.get("BMarks", Ci.nsILocalFile);
if (bookmarksFile.exists())
bookmarksURI = NetUtil.newURI(bookmarksFile);
}
else
bookmarksFile = dirService.get("BMarks", Ci.nsILocalFile);
if (bookmarksFile.exists()) {
if (bookmarksURI) {
// Add an import observer. It will ensure that smart bookmarks are
// created once the operation is complete.
Services.obs.addObserver(this, "bookmarks-restore-success", false);
@ -859,7 +866,7 @@ BrowserGlue.prototype = {
try {
var importer = Cc["@mozilla.org/browser/places/import-export-service;1"].
getService(Ci.nsIPlacesImportExportService);
importer.importHTMLFromFile(bookmarksFile, true /* overwrite existing */);
importer.importHTMLFromURI(bookmarksURI, true /* overwrite existing */);
} catch (err) {
// Report the error, but ignore it.
Cu.reportError("Bookmarks.html file could be corrupt. " + err);

Просмотреть файл

@ -39,13 +39,14 @@
#include "nsISupports.idl"
interface nsILocalFile;
interface nsIURI;
/**
* The PlacesImportExport interface provides methods for importing
* and exporting Places data.
*/
[scriptable, uuid(21c00314-fa63-11db-8314-0800200c9a66)]
[scriptable, uuid(47a4a09e-c708-4e68-b2f2-664d982ce026)]
interface nsIPlacesImportExportService: nsISupports
{
/**
@ -64,6 +65,11 @@ interface nsIPlacesImportExportService: nsISupports
*/
void importHTMLFromFile(in nsILocalFile aFile, in boolean aIsInitialImport);
/**
* Same thing as importHTMLFromFile, but takes a URI instead
*/
void importHTMLFromURI(in nsIURI aURI, in boolean aIsInitialImport);
/**
* Loads the given bookmarks.html file and puts it in the given folder
*

Просмотреть файл

@ -2181,6 +2181,33 @@ nsPlacesImportExportService::ImportHTMLFromFile(nsILocalFile* aFile,
}
NS_IMETHODIMP
nsPlacesImportExportService::ImportHTMLFromURI(nsIURI* aURI,
PRBool aIsInitialImport)
{
NotifyImportObservers(RESTORE_BEGIN_NSIOBSERVER_TOPIC, -1, aIsInitialImport);
// this version is exposed on the interface and disallows changing of roots
nsresult rv = ImportHTMLFromURIInternal(aURI,
PR_FALSE,
0,
aIsInitialImport);
if (NS_FAILED(rv)) {
NotifyImportObservers(RESTORE_FAILED_NSIOBSERVER_TOPIC,
-1,
aIsInitialImport);
}
else {
NotifyImportObservers(RESTORE_SUCCESS_NSIOBSERVER_TOPIC,
-1,
aIsInitialImport);
}
return rv;
}
NS_IMETHODIMP
nsPlacesImportExportService::ImportHTMLFromFileToFolder(nsILocalFile* aFile,
PRInt64 aFolderId,
@ -2217,8 +2244,7 @@ nsPlacesImportExportService::ImportHTMLFromFileInternal(nsILocalFile* aFile,
PRInt64 aFolder,
PRBool aIsImportDefaults)
{
nsresult rv = EnsureServiceState();
NS_ENSURE_SUCCESS(rv, rv);
nsresult rv;
nsCOMPtr<nsIFile> file = do_QueryInterface(aFile);
NS_ENSURE_STATE(file);
@ -2237,6 +2263,24 @@ nsPlacesImportExportService::ImportHTMLFromFileInternal(nsILocalFile* aFile,
return NS_ERROR_INVALID_ARG;
}
nsCOMPtr<nsIIOService> ioservice = do_GetIOService(&rv);
NS_ENSURE_SUCCESS(rv, rv);
nsCOMPtr<nsIURI> fileURI;
rv = ioservice->NewFileURI(file, getter_AddRefs(fileURI));
NS_ENSURE_SUCCESS(rv, rv);
return ImportHTMLFromURIInternal(fileURI, aAllowRootChanges, aFolder, aIsImportDefaults);
}
nsresult
nsPlacesImportExportService::ImportHTMLFromURIInternal(nsIURI* aURI,
PRBool aAllowRootChanges,
PRInt64 aFolder,
PRBool aIsImportDefaults)
{
nsresult rv = EnsureServiceState();
NS_ENSURE_SUCCESS(rv, rv);
nsCOMPtr<nsIParser> parser = do_CreateInstance(kParserCID);
NS_ENSURE_TRUE(parser, NS_ERROR_OUT_OF_MEMORY);
@ -2250,16 +2294,13 @@ nsPlacesImportExportService::ImportHTMLFromFileInternal(nsILocalFile* aFile,
// will confuse the parser.
nsCOMPtr<nsIIOService> ioservice = do_GetIOService(&rv);
NS_ENSURE_SUCCESS(rv, rv);
nsCOMPtr<nsIURI> fileURI;
rv = ioservice->NewFileURI(file, getter_AddRefs(fileURI));
NS_ENSURE_SUCCESS(rv, rv);
rv = ioservice->NewChannelFromURI(fileURI, getter_AddRefs(mImportChannel));
rv = ioservice->NewChannelFromURI(aURI, getter_AddRefs(mImportChannel));
NS_ENSURE_SUCCESS(rv, rv);
rv = mImportChannel->SetContentType(NS_LITERAL_CSTRING("text/html"));
NS_ENSURE_SUCCESS(rv, rv);
// Init parser.
rv = parser->Parse(fileURI, nsnull);
rv = parser->Parse(aURI, nsnull);
NS_ENSURE_SUCCESS(rv, rv);
// Run the import in batch mode, so it will be executed in a transaction

Просмотреть файл

@ -50,6 +50,8 @@ class nsPlacesImportExportService : public nsIPlacesImportExportService,
nsresult ImportHTMLFromFileInternal(nsILocalFile* aFile, PRBool aAllowRootChanges,
PRInt64 aFolder, PRBool aIsImportDefaults);
nsresult ImportHTMLFromURIInternal(nsIURI* aURI, PRBool aAllowRootChanges,
PRInt64 aFolder, PRBool aIsImportDefaults);
nsresult WriteContainer(nsINavHistoryResultNode* aFolder, const nsACString& aIndent, nsIOutputStream* aOutput);
nsresult WriteContainerHeader(nsINavHistoryResultNode* aFolder, const nsACString& aIndent, nsIOutputStream* aOutput);
nsresult WriteTitle(nsINavHistoryResultNode* aItem, nsIOutputStream* aOutput);