michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: "use strict"; michael@0: michael@0: module.metadata = { michael@0: "stability": "unstable", michael@0: "engines": { michael@0: "Firefox": "*" michael@0: } michael@0: }; michael@0: michael@0: /* michael@0: * Requiring hosts so they can subscribe to client messages michael@0: */ michael@0: require('./host/host-bookmarks'); michael@0: require('./host/host-tags'); michael@0: require('./host/host-query'); michael@0: michael@0: const { Cc, Ci } = require('chrome'); michael@0: const { Class } = require('../core/heritage'); michael@0: const { send } = require('../addon/events'); michael@0: const { defer, reject, all, resolve, promised } = require('../core/promise'); michael@0: const { EventTarget } = require('../event/target'); michael@0: const { emit } = require('../event/core'); michael@0: const { identity, defer:async } = require('../lang/functional'); michael@0: const { extend, merge } = require('../util/object'); michael@0: const { fromIterator } = require('../util/array'); michael@0: const { michael@0: constructTree, fetchItem, createQuery, michael@0: isRootGroup, createQueryOptions michael@0: } = require('./utils'); michael@0: const { michael@0: bookmarkContract, groupContract, separatorContract michael@0: } = require('./contract'); michael@0: const bmsrv = Cc["@mozilla.org/browser/nav-bookmarks-service;1"]. michael@0: getService(Ci.nsINavBookmarksService); michael@0: michael@0: /* michael@0: * Mapping of uncreated bookmarks with their created michael@0: * counterparts michael@0: */ michael@0: const itemMap = new WeakMap(); michael@0: michael@0: /* michael@0: * Constant used by nsIHistoryQuery; 1 is a bookmark query michael@0: * https://developer.mozilla.org/en-US/docs/XPCOM_Interface_Reference/nsINavHistoryQueryOptions michael@0: */ michael@0: const BOOKMARK_QUERY = 1; michael@0: michael@0: /* michael@0: * Bookmark Item classes michael@0: */ michael@0: michael@0: const Bookmark = Class({ michael@0: extends: [ michael@0: bookmarkContract.properties(identity) michael@0: ], michael@0: initialize: function initialize (options) { michael@0: merge(this, bookmarkContract(extend(defaults, options))); michael@0: }, michael@0: type: 'bookmark', michael@0: toString: function () '[object Bookmark]' michael@0: }); michael@0: exports.Bookmark = Bookmark; michael@0: michael@0: const Group = Class({ michael@0: extends: [ michael@0: groupContract.properties(identity) michael@0: ], michael@0: initialize: function initialize (options) { michael@0: // Don't validate if root group michael@0: if (isRootGroup(options)) michael@0: merge(this, options); michael@0: else michael@0: merge(this, groupContract(extend(defaults, options))); michael@0: }, michael@0: type: 'group', michael@0: toString: function () '[object Group]' michael@0: }); michael@0: exports.Group = Group; michael@0: michael@0: const Separator = Class({ michael@0: extends: [ michael@0: separatorContract.properties(identity) michael@0: ], michael@0: initialize: function initialize (options) { michael@0: merge(this, separatorContract(extend(defaults, options))); michael@0: }, michael@0: type: 'separator', michael@0: toString: function () '[object Separator]' michael@0: }); michael@0: exports.Separator = Separator; michael@0: michael@0: /* michael@0: * Functions michael@0: */ michael@0: michael@0: function save (items, options) { michael@0: items = [].concat(items); michael@0: options = options || {}; michael@0: let emitter = EventTarget(); michael@0: let results = []; michael@0: let errors = []; michael@0: let root = constructTree(items); michael@0: let cache = new Map(); michael@0: michael@0: let isExplicitSave = item => !!~items.indexOf(item); michael@0: // `walk` returns an aggregate promise indicating the completion michael@0: // of the `commitItem` on each node, not whether or not that michael@0: // commit was successful michael@0: michael@0: // Force this to be async, as if a ducktype fails validation, michael@0: // the promise implementation will fire an error event, which will michael@0: // not trigger the handler as it's not yet bound michael@0: // michael@0: // Can remove after `Promise.jsm` is implemented in Bug 881047, michael@0: // which will guarantee next tick execution michael@0: async(() => root.walk(preCommitItem).then(commitComplete))(); michael@0: michael@0: function preCommitItem ({value:item}) { michael@0: // Do nothing if tree root, default group (unsavable), michael@0: // or if it's a dependency and not explicitly saved (in the list michael@0: // of items to be saved), and not needed to be saved michael@0: if (item === null || // node is the tree root michael@0: isRootGroup(item) || michael@0: (getId(item) && !isExplicitSave(item))) michael@0: return; michael@0: michael@0: return promised(validate)(item) michael@0: .then(() => commitItem(item, options)) michael@0: .then(data => construct(data, cache)) michael@0: .then(savedItem => { michael@0: // If item was just created, make a map between michael@0: // the creation object and created object, michael@0: // so we can reference the item that doesn't have an id michael@0: if (!getId(item)) michael@0: saveId(item, savedItem.id); michael@0: michael@0: // Emit both the processed item, and original item michael@0: // so a mapping can be understood in handler michael@0: emit(emitter, 'data', savedItem, item); michael@0: michael@0: // Push to results iff item was explicitly saved michael@0: if (isExplicitSave(item)) michael@0: results[items.indexOf(item)] = savedItem; michael@0: }, reason => { michael@0: // Force reason to be a string for consistency michael@0: reason = reason + ''; michael@0: // Emit both the reason, and original item michael@0: // so a mapping can be understood in handler michael@0: emit(emitter, 'error', reason + '', item); michael@0: // Store unsaved item in results list michael@0: results[items.indexOf(item)] = item; michael@0: errors.push(reason); michael@0: }); michael@0: } michael@0: michael@0: // Called when traversal of the node tree is completed and all michael@0: // items have been committed michael@0: function commitComplete () { michael@0: emit(emitter, 'end', results); michael@0: } michael@0: michael@0: return emitter; michael@0: } michael@0: exports.save = save; michael@0: michael@0: function search (queries, options) { michael@0: queries = [].concat(queries); michael@0: let emitter = EventTarget(); michael@0: let cache = new Map(); michael@0: let queryObjs = queries.map(createQuery.bind(null, BOOKMARK_QUERY)); michael@0: let optionsObj = createQueryOptions(BOOKMARK_QUERY, options); michael@0: michael@0: // Can remove after `Promise.jsm` is implemented in Bug 881047, michael@0: // which will guarantee next tick execution michael@0: async(() => { michael@0: send('sdk-places-query', { queries: queryObjs, options: optionsObj }) michael@0: .then(handleQueryResponse); michael@0: })(); michael@0: michael@0: function handleQueryResponse (data) { michael@0: let deferreds = data.map(item => { michael@0: return construct(item, cache).then(bookmark => { michael@0: emit(emitter, 'data', bookmark); michael@0: return bookmark; michael@0: }, reason => { michael@0: emit(emitter, 'error', reason); michael@0: errors.push(reason); michael@0: }); michael@0: }); michael@0: michael@0: all(deferreds).then(data => { michael@0: emit(emitter, 'end', data); michael@0: }, () => emit(emitter, 'end', [])); michael@0: } michael@0: michael@0: return emitter; michael@0: } michael@0: exports.search = search; michael@0: michael@0: function remove (items) { michael@0: return [].concat(items).map(item => { michael@0: item.remove = true; michael@0: return item; michael@0: }); michael@0: } michael@0: michael@0: exports.remove = remove; michael@0: michael@0: /* michael@0: * Internal Utilities michael@0: */ michael@0: michael@0: function commitItem (item, options) { michael@0: // Get the item's ID, or getId it's saved version if it exists michael@0: let id = getId(item); michael@0: let data = normalize(item); michael@0: let promise; michael@0: michael@0: data.id = id; michael@0: michael@0: if (!id) { michael@0: promise = send('sdk-places-bookmarks-create', data); michael@0: } else if (item.remove) { michael@0: promise = send('sdk-places-bookmarks-remove', { id: id }); michael@0: } else { michael@0: promise = send('sdk-places-bookmarks-last-updated', { michael@0: id: id michael@0: }).then(function (updated) { michael@0: // If attempting to save an item that is not the michael@0: // latest snapshot of a bookmark item, execute michael@0: // the resolution function michael@0: if (updated !== item.updated && options.resolve) michael@0: return fetchItem(id) michael@0: .then(options.resolve.bind(null, data)); michael@0: else michael@0: return data; michael@0: }).then(send.bind(null, 'sdk-places-bookmarks-save')); michael@0: } michael@0: michael@0: return promise; michael@0: } michael@0: michael@0: /* michael@0: * Turns a bookmark item into a plain object, michael@0: * converts `tags` from Set to Array, group instance to an id michael@0: */ michael@0: function normalize (item) { michael@0: let data = merge({}, item); michael@0: // Circumvent prototype property of `type` michael@0: delete data.type; michael@0: data.type = item.type; michael@0: data.tags = []; michael@0: if (item.tags) { michael@0: data.tags = fromIterator(item.tags); michael@0: } michael@0: data.group = getId(data.group) || exports.UNSORTED.id; michael@0: michael@0: return data; michael@0: } michael@0: michael@0: /* michael@0: * Takes a data object and constructs a BookmarkItem instance michael@0: * of it, recursively generating parent instances as well. michael@0: * michael@0: * Pass in a `cache` Map to reuse instances of michael@0: * bookmark items to reduce overhead; michael@0: * The cache object is a map of id to a deferred with a michael@0: * promise that resolves to the bookmark item. michael@0: */ michael@0: function construct (object, cache, forced) { michael@0: let item = instantiate(object); michael@0: let deferred = defer(); michael@0: michael@0: // Item could not be instantiated michael@0: if (!item) michael@0: return resolve(null); michael@0: michael@0: // Return promise for item if found in the cache, michael@0: // and not `forced`. `forced` indicates that this is the construct michael@0: // call that should not read from cache, but should actually perform michael@0: // the construction, as it was set before several async calls michael@0: if (cache.has(item.id) && !forced) michael@0: return cache.get(item.id).promise; michael@0: else if (cache.has(item.id)) michael@0: deferred = cache.get(item.id); michael@0: else michael@0: cache.set(item.id, deferred); michael@0: michael@0: // When parent group is found in cache, use michael@0: // the same deferred value michael@0: if (item.group && cache.has(item.group)) { michael@0: cache.get(item.group).promise.then(group => { michael@0: item.group = group; michael@0: deferred.resolve(item); michael@0: }); michael@0: michael@0: // If not in the cache, and a root group, return michael@0: // the premade instance michael@0: } else if (rootGroups.get(item.group)) { michael@0: item.group = rootGroups.get(item.group); michael@0: deferred.resolve(item); michael@0: michael@0: // If not in the cache or a root group, fetch the parent michael@0: } else { michael@0: cache.set(item.group, defer()); michael@0: fetchItem(item.group).then(group => { michael@0: return construct(group, cache, true); michael@0: }).then(group => { michael@0: item.group = group; michael@0: deferred.resolve(item); michael@0: }, deferred.reject); michael@0: } michael@0: michael@0: return deferred.promise; michael@0: } michael@0: michael@0: function instantiate (object) { michael@0: if (object.type === 'bookmark') michael@0: return Bookmark(object); michael@0: if (object.type === 'group') michael@0: return Group(object); michael@0: if (object.type === 'separator') michael@0: return Separator(object); michael@0: return null; michael@0: } michael@0: michael@0: /** michael@0: * Validates a bookmark item; will throw an error if ininvalid, michael@0: * to be used with `promised`. As bookmark items check on their class, michael@0: * this only checks ducktypes michael@0: */ michael@0: function validate (object) { michael@0: if (!isDuckType(object)) return true; michael@0: let contract = object.type === 'bookmark' ? bookmarkContract : michael@0: object.type === 'group' ? groupContract : michael@0: object.type === 'separator' ? separatorContract : michael@0: null; michael@0: if (!contract) { michael@0: throw Error('No type specified'); michael@0: } michael@0: michael@0: // If object has a property set, and undefined, michael@0: // manually override with default as it'll fail otherwise michael@0: let withDefaults = Object.keys(defaults).reduce((obj, prop) => { michael@0: if (obj[prop] == null) obj[prop] = defaults[prop]; michael@0: return obj; michael@0: }, extend(object)); michael@0: michael@0: contract(withDefaults); michael@0: } michael@0: michael@0: function isDuckType (item) { michael@0: return !(item instanceof Bookmark) && michael@0: !(item instanceof Group) && michael@0: !(item instanceof Separator); michael@0: } michael@0: michael@0: function saveId (unsaved, id) { michael@0: itemMap.set(unsaved, id); michael@0: } michael@0: michael@0: // Fetches an item's ID from itself, or from the mapped items michael@0: function getId (item) { michael@0: return typeof item === 'number' ? item : michael@0: item ? item.id || itemMap.get(item) : michael@0: null; michael@0: } michael@0: michael@0: /* michael@0: * Set up the default, root groups michael@0: */ michael@0: michael@0: let defaultGroupMap = { michael@0: MENU: bmsrv.bookmarksMenuFolder, michael@0: TOOLBAR: bmsrv.toolbarFolder, michael@0: UNSORTED: bmsrv.unfiledBookmarksFolder michael@0: }; michael@0: michael@0: let rootGroups = new Map(); michael@0: michael@0: for (let i in defaultGroupMap) { michael@0: let group = Object.freeze(Group({ title: i, id: defaultGroupMap[i] })); michael@0: rootGroups.set(defaultGroupMap[i], group); michael@0: exports[i] = group; michael@0: } michael@0: michael@0: let defaults = { michael@0: group: exports.UNSORTED, michael@0: index: -1 michael@0: };