2013-03-18 05:05:30 +00:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
|
|
|
|
* You can obtain one at http://mozilla.org/MPL/2.0/. */
|
|
|
|
|
|
|
|
this.EXPORTED_SYMBOLS = [ "BookmarkJSONUtils" ];
|
|
|
|
|
|
|
|
const Ci = Components.interfaces;
|
|
|
|
const Cc = Components.classes;
|
|
|
|
const Cu = Components.utils;
|
2013-07-31 14:51:09 +00:00
|
|
|
const Cr = Components.results;
|
2013-03-18 05:05:30 +00:00
|
|
|
|
2014-02-04 13:43:20 +00:00
|
|
|
Cu.import("resource://gre/modules/XPCOMUtils.jsm");
|
2013-03-18 05:05:30 +00:00
|
|
|
Cu.import("resource://gre/modules/Services.jsm");
|
|
|
|
Cu.import("resource://gre/modules/NetUtil.jsm");
|
2013-11-18 06:36:40 +00:00
|
|
|
Cu.import("resource://gre/modules/osfile.jsm");
|
2013-03-18 05:05:30 +00:00
|
|
|
Cu.import("resource://gre/modules/PlacesUtils.jsm");
|
2015-04-16 10:13:43 +00:00
|
|
|
Cu.import("resource://gre/modules/PromiseUtils.jsm");
|
2013-03-18 05:05:30 +00:00
|
|
|
Cu.import("resource://gre/modules/Task.jsm");
|
2014-02-04 13:43:20 +00:00
|
|
|
|
|
|
|
XPCOMUtils.defineLazyModuleGetter(this, "PlacesBackups",
|
|
|
|
"resource://gre/modules/PlacesBackups.jsm");
|
|
|
|
XPCOMUtils.defineLazyModuleGetter(this, "Deprecated",
|
|
|
|
"resource://gre/modules/Deprecated.jsm");
|
|
|
|
|
|
|
|
XPCOMUtils.defineLazyGetter(this, "gTextDecoder", () => new TextDecoder());
|
|
|
|
XPCOMUtils.defineLazyGetter(this, "gTextEncoder", () => new TextEncoder());
|
2013-03-18 05:05:30 +00:00
|
|
|
|
2014-03-19 13:33:02 +00:00
|
|
|
/**
|
|
|
|
* Generates an hash for the given string.
|
|
|
|
*
|
|
|
|
* @note The generated hash is returned in base64 form. Mind the fact base64
|
|
|
|
* is case-sensitive if you are going to reuse this code.
|
|
|
|
*/
|
|
|
|
function generateHash(aString) {
|
|
|
|
let cryptoHash = Cc["@mozilla.org/security/hash;1"]
|
|
|
|
.createInstance(Ci.nsICryptoHash);
|
|
|
|
cryptoHash.init(Ci.nsICryptoHash.MD5);
|
|
|
|
let stringStream = Cc["@mozilla.org/io/string-input-stream;1"]
|
|
|
|
.createInstance(Ci.nsIStringInputStream);
|
|
|
|
stringStream.data = aString;
|
|
|
|
cryptoHash.updateFromStream(stringStream, -1);
|
|
|
|
// base64 allows the '/' char, but we can't use it for filenames.
|
2015-03-09 14:02:57 +00:00
|
|
|
return cryptoHash.finish(true).replace(/\//g, "-");
|
2014-03-19 13:33:02 +00:00
|
|
|
}
|
|
|
|
|
2013-03-18 05:05:30 +00:00
|
|
|
this.BookmarkJSONUtils = Object.freeze({
|
|
|
|
/**
|
|
|
|
* Import bookmarks from a url.
|
|
|
|
*
|
2014-03-07 07:41:47 +00:00
|
|
|
* @param aSpec
|
2013-03-18 05:05:30 +00:00
|
|
|
* url of the bookmark data.
|
|
|
|
* @param aReplace
|
|
|
|
* Boolean if true, replace existing bookmarks, else merge.
|
|
|
|
*
|
|
|
|
* @return {Promise}
|
|
|
|
* @resolves When the new bookmarks have been created.
|
|
|
|
* @rejects JavaScript exception.
|
|
|
|
*/
|
2014-03-07 07:41:47 +00:00
|
|
|
importFromURL: function BJU_importFromURL(aSpec, aReplace) {
|
|
|
|
return Task.spawn(function* () {
|
|
|
|
notifyObservers(PlacesUtils.TOPIC_BOOKMARKS_RESTORE_BEGIN);
|
|
|
|
try {
|
|
|
|
let importer = new BookmarkImporter(aReplace);
|
|
|
|
yield importer.importFromURL(aSpec);
|
|
|
|
|
|
|
|
notifyObservers(PlacesUtils.TOPIC_BOOKMARKS_RESTORE_SUCCESS);
|
|
|
|
} catch(ex) {
|
|
|
|
Cu.reportError("Failed to restore bookmarks from " + aSpec + ": " + ex);
|
|
|
|
notifyObservers(PlacesUtils.TOPIC_BOOKMARKS_RESTORE_FAILED);
|
|
|
|
}
|
|
|
|
});
|
2013-03-18 05:05:30 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Restores bookmarks and tags from a JSON file.
|
|
|
|
* @note any item annotated with "places/excludeFromBackup" won't be removed
|
|
|
|
* before executing the restore.
|
|
|
|
*
|
2014-02-04 13:43:20 +00:00
|
|
|
* @param aFilePath
|
2014-05-24 17:48:00 +00:00
|
|
|
* OS.File path string of bookmarks in JSON or JSONlz4 format to be restored.
|
2013-03-18 05:05:30 +00:00
|
|
|
* @param aReplace
|
|
|
|
* Boolean if true, replace existing bookmarks, else merge.
|
|
|
|
*
|
|
|
|
* @return {Promise}
|
|
|
|
* @resolves When the new bookmarks have been created.
|
|
|
|
* @rejects JavaScript exception.
|
2014-03-07 07:41:47 +00:00
|
|
|
* @deprecated passing an nsIFile is deprecated
|
2013-03-18 05:05:30 +00:00
|
|
|
*/
|
2014-02-04 13:43:20 +00:00
|
|
|
importFromFile: function BJU_importFromFile(aFilePath, aReplace) {
|
2014-03-07 07:41:47 +00:00
|
|
|
if (aFilePath instanceof Ci.nsIFile) {
|
|
|
|
Deprecated.warning("Passing an nsIFile to BookmarksJSONUtils.importFromFile " +
|
|
|
|
"is deprecated. Please use an OS.File path string instead.",
|
|
|
|
"https://developer.mozilla.org/docs/JavaScript_OS.File");
|
|
|
|
aFilePath = aFilePath.path;
|
|
|
|
}
|
|
|
|
|
|
|
|
return Task.spawn(function* () {
|
|
|
|
notifyObservers(PlacesUtils.TOPIC_BOOKMARKS_RESTORE_BEGIN);
|
|
|
|
try {
|
|
|
|
if (!(yield OS.File.exists(aFilePath)))
|
|
|
|
throw new Error("Cannot restore from nonexisting json file");
|
|
|
|
|
|
|
|
let importer = new BookmarkImporter(aReplace);
|
2014-05-24 17:48:00 +00:00
|
|
|
if (aFilePath.endsWith("jsonlz4")) {
|
|
|
|
yield importer.importFromCompressedFile(aFilePath);
|
|
|
|
} else {
|
|
|
|
yield importer.importFromURL(OS.Path.toFileURI(aFilePath));
|
|
|
|
}
|
2014-03-07 07:41:47 +00:00
|
|
|
notifyObservers(PlacesUtils.TOPIC_BOOKMARKS_RESTORE_SUCCESS);
|
|
|
|
} catch(ex) {
|
|
|
|
Cu.reportError("Failed to restore bookmarks from " + aFilePath + ": " + ex);
|
|
|
|
notifyObservers(PlacesUtils.TOPIC_BOOKMARKS_RESTORE_FAILED);
|
|
|
|
throw ex;
|
|
|
|
}
|
|
|
|
});
|
2013-03-18 05:05:30 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
2014-02-04 13:43:20 +00:00
|
|
|
* Serializes bookmarks using JSON, and writes to the supplied file path.
|
2013-03-18 05:05:30 +00:00
|
|
|
*
|
2014-02-04 13:43:20 +00:00
|
|
|
* @param aFilePath
|
2014-05-24 17:48:00 +00:00
|
|
|
* OS.File path string for the bookmarks file to be created.
|
2014-03-19 13:33:02 +00:00
|
|
|
* @param [optional] aOptions
|
|
|
|
* Object containing options for the export:
|
|
|
|
* - failIfHashIs: if the generated file would have the same hash
|
|
|
|
* defined here, will reject with ex.becauseSameHash
|
2014-05-24 17:48:00 +00:00
|
|
|
* - compress: if true, writes file using lz4 compression
|
2013-03-18 05:05:30 +00:00
|
|
|
* @return {Promise}
|
2014-03-19 13:33:02 +00:00
|
|
|
* @resolves once the file has been created, to an object with the
|
|
|
|
* following properties:
|
|
|
|
* - count: number of exported bookmarks
|
|
|
|
* - hash: file hash for contents comparison
|
2013-03-18 05:05:30 +00:00
|
|
|
* @rejects JavaScript exception.
|
2014-02-04 13:43:20 +00:00
|
|
|
* @deprecated passing an nsIFile is deprecated
|
2013-03-18 05:05:30 +00:00
|
|
|
*/
|
2014-03-19 13:33:02 +00:00
|
|
|
exportToFile: function BJU_exportToFile(aFilePath, aOptions={}) {
|
2014-02-04 13:43:20 +00:00
|
|
|
if (aFilePath instanceof Ci.nsIFile) {
|
|
|
|
Deprecated.warning("Passing an nsIFile to BookmarksJSONUtils.exportToFile " +
|
2014-03-07 07:41:47 +00:00
|
|
|
"is deprecated. Please use an OS.File path string instead.",
|
2014-02-04 13:43:20 +00:00
|
|
|
"https://developer.mozilla.org/docs/JavaScript_OS.File");
|
|
|
|
aFilePath = aFilePath.path;
|
|
|
|
}
|
|
|
|
return Task.spawn(function* () {
|
|
|
|
let [bookmarks, count] = yield PlacesBackups.getBookmarksTree();
|
|
|
|
let startTime = Date.now();
|
|
|
|
let jsonString = JSON.stringify(bookmarks);
|
|
|
|
// Report the time taken to convert the tree to JSON.
|
|
|
|
try {
|
|
|
|
Services.telemetry
|
|
|
|
.getHistogramById("PLACES_BACKUPS_TOJSON_MS")
|
|
|
|
.add(Date.now() - startTime);
|
|
|
|
} catch (ex) {
|
|
|
|
Components.utils.reportError("Unable to report telemetry.");
|
|
|
|
}
|
|
|
|
|
2014-03-19 13:33:02 +00:00
|
|
|
let hash = generateHash(jsonString);
|
|
|
|
|
|
|
|
if (hash === aOptions.failIfHashIs) {
|
|
|
|
let e = new Error("Hash conflict");
|
|
|
|
e.becauseSameHash = true;
|
|
|
|
throw e;
|
|
|
|
}
|
|
|
|
|
2014-02-14 14:42:35 +00:00
|
|
|
// Do not write to the tmp folder, otherwise if it has a different
|
|
|
|
// filesystem writeAtomic will fail. Eventual dangling .tmp files should
|
|
|
|
// be cleaned up by the caller.
|
2014-05-24 17:48:00 +00:00
|
|
|
let writeOptions = { tmpPath: OS.Path.join(aFilePath + ".tmp") };
|
|
|
|
if (aOptions.compress)
|
|
|
|
writeOptions.compression = "lz4";
|
|
|
|
|
|
|
|
yield OS.File.writeAtomic(aFilePath, jsonString, writeOptions);
|
2014-03-19 13:33:02 +00:00
|
|
|
return { count: count, hash: hash };
|
2014-02-04 13:43:20 +00:00
|
|
|
});
|
2013-03-18 05:05:30 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2014-03-07 07:41:47 +00:00
|
|
|
function BookmarkImporter(aReplace) {
|
|
|
|
this._replace = aReplace;
|
|
|
|
}
|
2013-03-18 05:05:30 +00:00
|
|
|
BookmarkImporter.prototype = {
|
|
|
|
/**
|
|
|
|
* Import bookmarks from a url.
|
|
|
|
*
|
2014-03-07 07:41:47 +00:00
|
|
|
* @param aSpec
|
2013-03-18 05:05:30 +00:00
|
|
|
* url of the bookmark data.
|
|
|
|
*
|
|
|
|
* @return {Promise}
|
|
|
|
* @resolves When the new bookmarks have been created.
|
|
|
|
* @rejects JavaScript exception.
|
|
|
|
*/
|
2015-04-16 10:13:43 +00:00
|
|
|
importFromURL(spec) {
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
let streamObserver = {
|
|
|
|
onStreamComplete: (aLoader, aContext, aStatus, aLength, aResult) => {
|
|
|
|
let converter = Cc["@mozilla.org/intl/scriptableunicodeconverter"].
|
|
|
|
createInstance(Ci.nsIScriptableUnicodeConverter);
|
|
|
|
converter.charset = "UTF-8";
|
|
|
|
try {
|
|
|
|
let jsonString = converter.convertFromByteArray(aResult,
|
|
|
|
aResult.length);
|
|
|
|
resolve(this.importFromJSON(jsonString));
|
|
|
|
} catch (ex) {
|
|
|
|
Cu.reportError("Failed to import from URL: " + ex);
|
|
|
|
reject(ex);
|
|
|
|
}
|
2014-03-07 07:41:47 +00:00
|
|
|
}
|
2015-04-16 10:13:43 +00:00
|
|
|
};
|
2013-03-18 05:05:30 +00:00
|
|
|
|
2015-04-16 10:13:43 +00:00
|
|
|
let uri = NetUtil.newURI(spec);
|
|
|
|
let channel = NetUtil.newChannel({
|
|
|
|
uri,
|
2015-08-18 07:01:42 +00:00
|
|
|
loadingPrincipal: Services.scriptSecurityManager.createCodebasePrincipal(uri, {}),
|
2015-08-06 18:17:24 +00:00
|
|
|
contentPolicyType: Ci.nsIContentPolicy.TYPE_INTERNAL_XMLHTTPREQUEST
|
2015-04-16 10:13:43 +00:00
|
|
|
});
|
|
|
|
let streamLoader = Cc["@mozilla.org/network/stream-loader;1"]
|
|
|
|
.createInstance(Ci.nsIStreamLoader);
|
2013-03-18 05:05:30 +00:00
|
|
|
streamLoader.init(streamObserver);
|
|
|
|
channel.asyncOpen(streamLoader, channel);
|
2015-04-16 10:13:43 +00:00
|
|
|
});
|
2013-03-18 05:05:30 +00:00
|
|
|
},
|
|
|
|
|
2014-05-24 17:48:00 +00:00
|
|
|
/**
|
|
|
|
* Import bookmarks from a compressed file.
|
|
|
|
*
|
|
|
|
* @param aFilePath
|
|
|
|
* OS.File path string of the bookmark data.
|
|
|
|
*
|
|
|
|
* @return {Promise}
|
|
|
|
* @resolves When the new bookmarks have been created.
|
|
|
|
* @rejects JavaScript exception.
|
|
|
|
*/
|
|
|
|
importFromCompressedFile: function* BI_importFromCompressedFile(aFilePath) {
|
|
|
|
let aResult = yield OS.File.read(aFilePath, { compression: "lz4" });
|
|
|
|
let converter = Cc["@mozilla.org/intl/scriptableunicodeconverter"].
|
|
|
|
createInstance(Ci.nsIScriptableUnicodeConverter);
|
|
|
|
converter.charset = "UTF-8";
|
|
|
|
let jsonString = converter.convertFromByteArray(aResult, aResult.length);
|
|
|
|
yield this.importFromJSON(jsonString);
|
|
|
|
},
|
|
|
|
|
2013-03-18 05:05:30 +00:00
|
|
|
/**
|
|
|
|
* Import bookmarks from a JSON string.
|
|
|
|
*
|
|
|
|
* @param aString
|
|
|
|
* JSON string of serialized bookmark data.
|
|
|
|
*/
|
2015-04-16 10:13:43 +00:00
|
|
|
importFromJSON: Task.async(function* (aString) {
|
|
|
|
this._importPromises = [];
|
|
|
|
let deferred = PromiseUtils.defer();
|
2013-03-18 05:05:30 +00:00
|
|
|
let nodes =
|
|
|
|
PlacesUtils.unwrapNodes(aString, PlacesUtils.TYPE_X_MOZ_PLACE_CONTAINER);
|
|
|
|
|
|
|
|
if (nodes.length == 0 || !nodes[0].children ||
|
|
|
|
nodes[0].children.length == 0) {
|
2014-03-07 07:41:47 +00:00
|
|
|
deferred.resolve(); // Nothing to restore
|
2013-04-17 05:36:02 +00:00
|
|
|
} else {
|
|
|
|
// Ensure tag folder gets processed last
|
|
|
|
nodes[0].children.sort(function sortRoots(aNode, bNode) {
|
|
|
|
return (aNode.root && aNode.root == "tagsFolder") ? 1 :
|
|
|
|
(bNode.root && bNode.root == "tagsFolder") ? -1 : 0;
|
|
|
|
});
|
2013-03-18 05:05:30 +00:00
|
|
|
|
2013-04-17 05:36:02 +00:00
|
|
|
let batch = {
|
|
|
|
nodes: nodes[0].children,
|
|
|
|
runBatched: function runBatched() {
|
2014-03-07 07:41:47 +00:00
|
|
|
if (this._replace) {
|
2013-04-17 05:36:02 +00:00
|
|
|
// Get roots excluded from the backup, we will not remove them
|
|
|
|
// before restoring.
|
|
|
|
let excludeItems = PlacesUtils.annotations.getItemsWithAnnotation(
|
|
|
|
PlacesUtils.EXCLUDE_FROM_BACKUP_ANNO);
|
|
|
|
// Delete existing children of the root node, excepting:
|
|
|
|
// 1. special folders: delete the child nodes
|
|
|
|
// 2. tags folder: untag via the tagging api
|
|
|
|
let root = PlacesUtils.getFolderContents(PlacesUtils.placesRootId,
|
2013-03-18 05:05:30 +00:00
|
|
|
false, false).root;
|
2013-04-17 05:36:02 +00:00
|
|
|
let childIds = [];
|
|
|
|
for (let i = 0; i < root.childCount; i++) {
|
|
|
|
let childId = root.getChild(i).itemId;
|
|
|
|
if (excludeItems.indexOf(childId) == -1 &&
|
|
|
|
childId != PlacesUtils.tagsFolderId) {
|
|
|
|
childIds.push(childId);
|
|
|
|
}
|
2013-03-18 05:05:30 +00:00
|
|
|
}
|
2013-04-17 05:36:02 +00:00
|
|
|
root.containerOpen = false;
|
2013-03-18 05:05:30 +00:00
|
|
|
|
2013-04-17 05:36:02 +00:00
|
|
|
for (let i = 0; i < childIds.length; i++) {
|
|
|
|
let rootItemId = childIds[i];
|
|
|
|
if (PlacesUtils.isRootItem(rootItemId)) {
|
|
|
|
PlacesUtils.bookmarks.removeFolderChildren(rootItemId);
|
|
|
|
} else {
|
|
|
|
PlacesUtils.bookmarks.removeItem(rootItemId);
|
|
|
|
}
|
2013-03-18 05:05:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-04-17 05:36:02 +00:00
|
|
|
let searchIds = [];
|
|
|
|
let folderIdMap = [];
|
2013-03-18 05:05:30 +00:00
|
|
|
|
2013-07-05 02:22:05 +00:00
|
|
|
for (let node of batch.nodes) {
|
|
|
|
if (!node.children || node.children.length == 0)
|
|
|
|
continue; // Nothing to restore for this root
|
|
|
|
|
|
|
|
if (node.root) {
|
|
|
|
let container = PlacesUtils.placesRootId; // Default to places root
|
|
|
|
switch (node.root) {
|
|
|
|
case "bookmarksMenuFolder":
|
|
|
|
container = PlacesUtils.bookmarksMenuFolderId;
|
|
|
|
break;
|
|
|
|
case "tagsFolder":
|
|
|
|
container = PlacesUtils.tagsFolderId;
|
|
|
|
break;
|
|
|
|
case "unfiledBookmarksFolder":
|
|
|
|
container = PlacesUtils.unfiledBookmarksFolderId;
|
|
|
|
break;
|
|
|
|
case "toolbarFolder":
|
|
|
|
container = PlacesUtils.toolbarFolderId;
|
|
|
|
break;
|
|
|
|
}
|
2013-03-18 05:05:30 +00:00
|
|
|
|
2013-07-05 02:22:05 +00:00
|
|
|
// Insert the data into the db
|
|
|
|
for (let child of node.children) {
|
|
|
|
let index = child.index;
|
|
|
|
let [folders, searches] =
|
|
|
|
this.importJSONNode(child, container, index, 0);
|
|
|
|
for (let i = 0; i < folders.length; i++) {
|
|
|
|
if (folders[i])
|
|
|
|
folderIdMap[i] = folders[i];
|
2013-04-17 05:36:02 +00:00
|
|
|
}
|
2013-07-05 02:22:05 +00:00
|
|
|
searchIds = searchIds.concat(searches);
|
2013-06-06 01:50:28 +00:00
|
|
|
}
|
2013-07-05 02:22:05 +00:00
|
|
|
} else {
|
|
|
|
this.importJSONNode(
|
|
|
|
node, PlacesUtils.placesRootId, node.index, 0);
|
2013-04-17 05:36:02 +00:00
|
|
|
}
|
2013-07-05 02:22:05 +00:00
|
|
|
}
|
2013-03-18 05:05:30 +00:00
|
|
|
|
2013-07-05 02:22:05 +00:00
|
|
|
// Fixup imported place: uris that contain folders
|
|
|
|
searchIds.forEach(function(aId) {
|
|
|
|
let oldURI = PlacesUtils.bookmarks.getBookmarkURI(aId);
|
|
|
|
let uri = fixupQuery(oldURI, folderIdMap);
|
|
|
|
if (!uri.equals(oldURI)) {
|
|
|
|
PlacesUtils.bookmarks.changeBookmarkURI(aId, uri);
|
|
|
|
}
|
|
|
|
});
|
2013-04-17 05:36:02 +00:00
|
|
|
|
2013-07-05 02:22:05 +00:00
|
|
|
deferred.resolve();
|
2013-04-17 05:36:02 +00:00
|
|
|
}.bind(this)
|
|
|
|
};
|
2013-03-18 05:05:30 +00:00
|
|
|
|
2013-04-17 05:36:02 +00:00
|
|
|
PlacesUtils.bookmarks.runInBatchMode(batch, null);
|
|
|
|
}
|
2015-04-16 10:13:43 +00:00
|
|
|
yield deferred.promise;
|
|
|
|
// TODO (bug 1095426) once converted to the new bookmarks API, methods will
|
|
|
|
// yield, so this hack should not be needed anymore.
|
|
|
|
try {
|
|
|
|
yield Promise.all(this._importPromises);
|
|
|
|
} finally {
|
|
|
|
delete this._importPromises;
|
|
|
|
}
|
|
|
|
}),
|
2013-03-18 05:05:30 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Takes a JSON-serialized node and inserts it into the db.
|
|
|
|
*
|
|
|
|
* @param aData
|
|
|
|
* The unwrapped data blob of dropped or pasted data.
|
|
|
|
* @param aContainer
|
|
|
|
* The container the data was dropped or pasted into
|
|
|
|
* @param aIndex
|
|
|
|
* The index within the container the item was dropped or pasted at
|
|
|
|
* @return an array containing of maps of old folder ids to new folder ids,
|
|
|
|
* and an array of saved search ids that need to be fixed up.
|
|
|
|
* eg: [[[oldFolder1, newFolder1]], [search1]]
|
|
|
|
*/
|
|
|
|
importJSONNode: function BI_importJSONNode(aData, aContainer, aIndex,
|
|
|
|
aGrandParentId) {
|
2013-07-05 02:22:05 +00:00
|
|
|
let folderIdMap = [];
|
|
|
|
let searchIds = [];
|
|
|
|
let id = -1;
|
|
|
|
switch (aData.type) {
|
|
|
|
case PlacesUtils.TYPE_X_MOZ_PLACE_CONTAINER:
|
|
|
|
if (aContainer == PlacesUtils.tagsFolderId) {
|
|
|
|
// Node is a tag
|
|
|
|
if (aData.children) {
|
|
|
|
aData.children.forEach(function(aChild) {
|
|
|
|
try {
|
|
|
|
PlacesUtils.tagging.tagURI(
|
|
|
|
NetUtil.newURI(aChild.uri), [aData.title]);
|
|
|
|
} catch (ex) {
|
|
|
|
// Invalid tag child, skip it
|
2013-03-18 05:05:30 +00:00
|
|
|
}
|
|
|
|
});
|
2013-07-05 02:22:05 +00:00
|
|
|
return [folderIdMap, searchIds];
|
|
|
|
}
|
2014-02-04 13:43:20 +00:00
|
|
|
} else if (aData.annos &&
|
|
|
|
aData.annos.some(anno => anno.name == PlacesUtils.LMANNO_FEEDURI)) {
|
2013-07-05 02:22:05 +00:00
|
|
|
// Node is a livemark
|
|
|
|
let feedURI = null;
|
|
|
|
let siteURI = null;
|
|
|
|
aData.annos = aData.annos.filter(function(aAnno) {
|
|
|
|
switch (aAnno.name) {
|
|
|
|
case PlacesUtils.LMANNO_FEEDURI:
|
|
|
|
feedURI = NetUtil.newURI(aAnno.value);
|
|
|
|
return false;
|
|
|
|
case PlacesUtils.LMANNO_SITEURI:
|
|
|
|
siteURI = NetUtil.newURI(aAnno.value);
|
|
|
|
return false;
|
|
|
|
default:
|
|
|
|
return true;
|
2013-03-18 05:05:30 +00:00
|
|
|
}
|
2013-07-05 02:22:05 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
if (feedURI) {
|
2015-04-25 08:51:48 +00:00
|
|
|
let lmPromise = PlacesUtils.livemarks.addLivemark({
|
2013-07-05 02:22:05 +00:00
|
|
|
title: aData.title,
|
|
|
|
feedURI: feedURI,
|
|
|
|
parentId: aContainer,
|
|
|
|
index: aIndex,
|
|
|
|
lastModified: aData.lastModified,
|
|
|
|
siteURI: siteURI
|
2014-03-12 15:24:23 +00:00
|
|
|
}).then(function (aLivemark) {
|
|
|
|
let id = aLivemark.id;
|
|
|
|
if (aData.dateAdded)
|
|
|
|
PlacesUtils.bookmarks.setItemDateAdded(id, aData.dateAdded);
|
|
|
|
if (aData.annos && aData.annos.length)
|
|
|
|
PlacesUtils.setAnnotationsForItem(id, aData.annos);
|
2015-04-25 08:51:48 +00:00
|
|
|
});
|
|
|
|
this._importPromises.push(lmPromise);
|
2013-03-18 05:05:30 +00:00
|
|
|
}
|
2013-07-05 02:22:05 +00:00
|
|
|
} else {
|
|
|
|
id = PlacesUtils.bookmarks.createFolder(
|
|
|
|
aContainer, aData.title, aIndex);
|
|
|
|
folderIdMap[aData.id] = id;
|
|
|
|
// Process children
|
|
|
|
if (aData.children) {
|
|
|
|
for (let i = 0; i < aData.children.length; i++) {
|
|
|
|
let child = aData.children[i];
|
|
|
|
let [folders, searches] =
|
|
|
|
this.importJSONNode(child, id, i, aContainer);
|
|
|
|
for (let j = 0; j < folders.length; j++) {
|
|
|
|
if (folders[j])
|
|
|
|
folderIdMap[j] = folders[j];
|
|
|
|
}
|
|
|
|
searchIds = searchIds.concat(searches);
|
2013-06-06 01:50:28 +00:00
|
|
|
}
|
2013-03-18 05:05:30 +00:00
|
|
|
}
|
2013-07-05 02:22:05 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case PlacesUtils.TYPE_X_MOZ_PLACE:
|
|
|
|
id = PlacesUtils.bookmarks.insertBookmark(
|
|
|
|
aContainer, NetUtil.newURI(aData.uri), aIndex, aData.title);
|
2015-04-16 10:13:43 +00:00
|
|
|
if (aData.keyword) {
|
|
|
|
// POST data could be set in 2 ways:
|
|
|
|
// 1. new backups have a postData property
|
|
|
|
// 2. old backups have an item annotation
|
|
|
|
let postDataAnno = aData.annos &&
|
|
|
|
aData.annos.find(anno => anno.name == PlacesUtils.POST_DATA_ANNO);
|
|
|
|
let postData = aData.postData || (postDataAnno && postDataAnno.value);
|
|
|
|
let kwPromise = PlacesUtils.keywords.insert({ keyword: aData.keyword,
|
|
|
|
url: aData.uri,
|
|
|
|
postData });
|
|
|
|
this._importPromises.push(kwPromise);
|
|
|
|
}
|
2013-07-05 02:22:05 +00:00
|
|
|
if (aData.tags) {
|
2015-05-21 20:19:37 +00:00
|
|
|
let tags = aData.tags.split(",").filter(aTag =>
|
|
|
|
aTag.length <= Ci.nsITaggingService.MAX_TAG_LENGTH);
|
|
|
|
if (tags.length) {
|
|
|
|
try {
|
|
|
|
PlacesUtils.tagging.tagURI(NetUtil.newURI(aData.uri), tags);
|
|
|
|
} catch (ex) {
|
|
|
|
// Invalid tag child, skip it.
|
|
|
|
Cu.reportError(`Unable to set tags "${tags.join(", ")}" for ${aData.uri}: ${ex}`);
|
|
|
|
}
|
|
|
|
}
|
2013-07-05 02:22:05 +00:00
|
|
|
}
|
|
|
|
if (aData.charset) {
|
|
|
|
PlacesUtils.annotations.setPageAnnotation(
|
|
|
|
NetUtil.newURI(aData.uri), PlacesUtils.CHARSET_ANNO, aData.charset,
|
|
|
|
0, Ci.nsIAnnotationService.EXPIRE_NEVER);
|
|
|
|
}
|
|
|
|
if (aData.uri.substr(0, 6) == "place:")
|
|
|
|
searchIds.push(id);
|
|
|
|
if (aData.icon) {
|
|
|
|
try {
|
|
|
|
// Create a fake faviconURI to use (FIXME: bug 523932)
|
|
|
|
let faviconURI = NetUtil.newURI("fake-favicon-uri:" + aData.uri);
|
|
|
|
PlacesUtils.favicons.replaceFaviconDataFromDataURL(
|
|
|
|
faviconURI, aData.icon, 0);
|
|
|
|
PlacesUtils.favicons.setAndFetchFaviconForPage(
|
|
|
|
NetUtil.newURI(aData.uri), faviconURI, false,
|
|
|
|
PlacesUtils.favicons.FAVICON_LOAD_NON_PRIVATE);
|
|
|
|
} catch (ex) {
|
|
|
|
Components.utils.reportError("Failed to import favicon data:" + ex);
|
2013-06-06 01:50:28 +00:00
|
|
|
}
|
2013-07-05 02:22:05 +00:00
|
|
|
}
|
|
|
|
if (aData.iconUri) {
|
|
|
|
try {
|
|
|
|
PlacesUtils.favicons.setAndFetchFaviconForPage(
|
|
|
|
NetUtil.newURI(aData.uri), NetUtil.newURI(aData.iconUri), false,
|
|
|
|
PlacesUtils.favicons.FAVICON_LOAD_NON_PRIVATE);
|
|
|
|
} catch (ex) {
|
|
|
|
Components.utils.reportError("Failed to import favicon URI:" + ex);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case PlacesUtils.TYPE_X_MOZ_PLACE_SEPARATOR:
|
|
|
|
id = PlacesUtils.bookmarks.insertSeparator(aContainer, aIndex);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
// Unknown node type
|
|
|
|
}
|
2013-03-18 05:05:30 +00:00
|
|
|
|
2013-07-05 02:22:05 +00:00
|
|
|
// Set generic properties, valid for all nodes
|
|
|
|
if (id != -1 && aContainer != PlacesUtils.tagsFolderId &&
|
|
|
|
aGrandParentId != PlacesUtils.tagsFolderId) {
|
|
|
|
if (aData.dateAdded)
|
|
|
|
PlacesUtils.bookmarks.setItemDateAdded(id, aData.dateAdded);
|
|
|
|
if (aData.lastModified)
|
|
|
|
PlacesUtils.bookmarks.setItemLastModified(id, aData.lastModified);
|
|
|
|
if (aData.annos && aData.annos.length)
|
|
|
|
PlacesUtils.setAnnotationsForItem(id, aData.annos);
|
|
|
|
}
|
2013-03-18 05:05:30 +00:00
|
|
|
|
2013-07-05 02:22:05 +00:00
|
|
|
return [folderIdMap, searchIds];
|
2013-03-18 05:05:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function notifyObservers(topic) {
|
|
|
|
Services.obs.notifyObservers(null, topic, "json");
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Replaces imported folder ids with their local counterparts in a place: URI.
|
|
|
|
*
|
|
|
|
* @param aURI
|
|
|
|
* A place: URI with folder ids.
|
|
|
|
* @param aFolderIdMap
|
|
|
|
* An array mapping old folder id to new folder ids.
|
|
|
|
* @returns the fixed up URI if all matched. If some matched, it returns
|
|
|
|
* the URI with only the matching folders included. If none matched
|
|
|
|
* it returns the input URI unchanged.
|
|
|
|
*/
|
|
|
|
function fixupQuery(aQueryURI, aFolderIdMap) {
|
|
|
|
let convert = function(str, p1, offset, s) {
|
|
|
|
return "folder=" + aFolderIdMap[p1];
|
|
|
|
}
|
|
|
|
let stringURI = aQueryURI.spec.replace(/folder=([0-9]+)/g, convert);
|
|
|
|
|
|
|
|
return NetUtil.newURI(stringURI);
|
|
|
|
}
|