Bug 1276676 - Remove _serializeNodeAsJSONToOutputStream; r?:mak
MozReview-Commit-ID: FOEneylyTja
--- a/toolkit/components/places/PlacesUtils.jsm
+++ b/toolkit/components/places/PlacesUtils.jsm
@@ -1202,217 +1202,16 @@ this.PlacesUtils = {
root.containerOpen = false;
if (!didSuppressNotifications)
result.suppressNotifications = false;
}
return urls;
},
/**
- * Serializes the given node (and all its descendents) as JSON
- * and writes the serialization to the given output stream.
- *
- * @param aNode
- * An nsINavHistoryResultNode
- * @param aStream
- * An nsIOutputStream. NOTE: it only uses the write(str, len)
- * method of nsIOutputStream. The caller is responsible for
- * closing the stream.
- */
- _serializeNodeAsJSONToOutputStream: function (aNode, aStream) {
- function addGenericProperties(aPlacesNode, aJSNode) {
- aJSNode.title = aPlacesNode.title;
- aJSNode.id = aPlacesNode.itemId;
- let guid = aPlacesNode.bookmarkGuid;
- if (guid) {
- aJSNode.itemGuid = guid;
- var parent = aPlacesNode.parent;
- if (parent)
- aJSNode.parent = parent.itemId;
-
- var dateAdded = aPlacesNode.dateAdded;
- if (dateAdded)
- aJSNode.dateAdded = dateAdded;
- var lastModified = aPlacesNode.lastModified;
- if (lastModified)
- aJSNode.lastModified = lastModified;
-
- // XXX need a hasAnnos api
- var annos = [];
- try {
- annos = PlacesUtils.getAnnotationsForItem(aJSNode.id).filter(function(anno) {
- // XXX should whitelist this instead, w/ a pref for
- // backup/restore of non-whitelisted annos
- // XXX causes JSON encoding errors, so utf-8 encode
- //anno.value = unescape(encodeURIComponent(anno.value));
- if (anno.name == PlacesUtils.LMANNO_FEEDURI)
- aJSNode.livemark = 1;
- return true;
- });
- } catch(ex) {}
- if (annos.length != 0)
- aJSNode.annos = annos;
- }
- // XXXdietrich - store annos for non-bookmark items
- }
-
- function addURIProperties(aPlacesNode, aJSNode) {
- aJSNode.type = PlacesUtils.TYPE_X_MOZ_PLACE;
- aJSNode.uri = aPlacesNode.uri;
- if (aJSNode.id && aJSNode.id != -1) {
- // harvest bookmark-specific properties
- var keyword = PlacesUtils.bookmarks.getKeywordForBookmark(aJSNode.id);
- if (keyword)
- aJSNode.keyword = keyword;
- }
-
- if (aPlacesNode.tags)
- aJSNode.tags = aPlacesNode.tags;
-
- // last character-set
- var uri = PlacesUtils._uri(aPlacesNode.uri);
- try {
- var lastCharset = PlacesUtils.annotations.getPageAnnotation(
- uri, PlacesUtils.CHARSET_ANNO);
- aJSNode.charset = lastCharset;
- } catch (e) {}
- }
-
- function addSeparatorProperties(aPlacesNode, aJSNode) {
- aJSNode.type = PlacesUtils.TYPE_X_MOZ_PLACE_SEPARATOR;
- }
-
- function addContainerProperties(aPlacesNode, aJSNode) {
- var concreteId = PlacesUtils.getConcreteItemId(aPlacesNode);
- if (concreteId != -1) {
- // This is a bookmark or a tag container.
- if (PlacesUtils.nodeIsQuery(aPlacesNode) ||
- concreteId != aPlacesNode.itemId) {
- aJSNode.type = PlacesUtils.TYPE_X_MOZ_PLACE;
- aJSNode.uri = aPlacesNode.uri;
- // folder shortcut
- aJSNode.concreteId = concreteId;
- }
- else { // Bookmark folder or a shortcut we should convert to folder.
- aJSNode.type = PlacesUtils.TYPE_X_MOZ_PLACE_CONTAINER;
-
- // Mark root folders.
- if (aJSNode.id == PlacesUtils.placesRootId)
- aJSNode.root = "placesRoot";
- else if (aJSNode.id == PlacesUtils.bookmarksMenuFolderId)
- aJSNode.root = "bookmarksMenuFolder";
- else if (aJSNode.id == PlacesUtils.tagsFolderId)
- aJSNode.root = "tagsFolder";
- else if (aJSNode.id == PlacesUtils.unfiledBookmarksFolderId)
- aJSNode.root = "unfiledBookmarksFolder";
- else if (aJSNode.id == PlacesUtils.toolbarFolderId)
- aJSNode.root = "toolbarFolder";
- }
- }
- else {
- // This is a grouped container query, generated on the fly.
- aJSNode.type = PlacesUtils.TYPE_X_MOZ_PLACE;
- aJSNode.uri = aPlacesNode.uri;
- }
- }
-
- function appendConvertedComplexNode(aNode, aSourceNode, aArray) {
- var repr = {};
-
- for (let [name, value] in Iterator(aNode))
- repr[name] = value;
-
- // write child nodes
- var children = repr.children = [];
- if (!aNode.livemark) {
- asContainer(aSourceNode);
- var wasOpen = aSourceNode.containerOpen;
- if (!wasOpen)
- aSourceNode.containerOpen = true;
- var cc = aSourceNode.childCount;
- for (var i = 0; i < cc; ++i) {
- var childNode = aSourceNode.getChild(i);
- appendConvertedNode(aSourceNode.getChild(i), i, children);
- }
- if (!wasOpen)
- aSourceNode.containerOpen = false;
- }
-
- aArray.push(repr);
- return true;
- }
-
- function appendConvertedNode(bNode, aIndex, aArray) {
- var node = {};
-
- // set index in order received
- // XXX handy shortcut, but are there cases where we don't want
- // to export using the sorting provided by the query?
- if (aIndex)
- node.index = aIndex;
-
- addGenericProperties(bNode, node);
-
- var parent = bNode.parent;
- var grandParent = parent ? parent.parent : null;
- if (grandParent)
- node.grandParentId = grandParent.itemId;
-
- if (PlacesUtils.nodeIsURI(bNode)) {
- // Tag root accept only folder nodes
- if (parent && parent.itemId == PlacesUtils.tagsFolderId)
- return false;
-
- // Check for url validity, since we can't halt while writing a backup.
- // This will throw if we try to serialize an invalid url and it does
- // not make sense saving a wrong or corrupt uri node.
- try {
- PlacesUtils._uri(bNode.uri);
- } catch (ex) {
- return false;
- }
-
- addURIProperties(bNode, node);
- }
- else if (PlacesUtils.nodeIsContainer(bNode)) {
- // Tag containers accept only uri nodes
- if (grandParent && grandParent.itemId == PlacesUtils.tagsFolderId)
- return false;
-
- addContainerProperties(bNode, node);
- }
- else if (PlacesUtils.nodeIsSeparator(bNode)) {
- // Tag root accept only folder nodes
- // Tag containers accept only uri nodes
- if ((parent && parent.itemId == PlacesUtils.tagsFolderId) ||
- (grandParent && grandParent.itemId == PlacesUtils.tagsFolderId))
- return false;
-
- addSeparatorProperties(bNode, node);
- }
-
- if (!node.feedURI && node.type == PlacesUtils.TYPE_X_MOZ_PLACE_CONTAINER)
- return appendConvertedComplexNode(node, bNode, aArray);
-
- aArray.push(node);
- return true;
- }
-
- // serialize to stream
- var array = [];
- if (appendConvertedNode(aNode, null, array)) {
- var json = JSON.stringify(array[0]);
- aStream.write(json, json.length);
- }
- else {
- throw Cr.NS_ERROR_UNEXPECTED;
- }
- },
-
- /**
* Gets a shared Sqlite.jsm readonly connection to the Places database,
* usable only for SELECT queries.
*
* This is intended to be used mostly internally, components outside of
* Places should, when possible, use API calls and file bugs to get proper
* APIs, where they are missing.
* Keep in mind the Places DB schema is by no means frozen or even stable.
* Your custom queries can - and will - break overtime.