bookmarks.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394
  1. /* This Source Code Form is subject to the terms of the Mozilla Public
  2. * License, v. 2.0. If a copy of the MPL was not distributed with this
  3. * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
  4. "use strict";
  5. module.metadata = {
  6. "stability": "unstable",
  7. "engines": {
  8. "Firefox": "*"
  9. }
  10. };
  11. /*
  12. * Requiring hosts so they can subscribe to client messages
  13. */
  14. require('./host/host-bookmarks');
  15. require('./host/host-tags');
  16. require('./host/host-query');
  17. const { Cc, Ci } = require('chrome');
  18. const { Class } = require('../core/heritage');
  19. const { send } = require('../addon/events');
  20. const { defer, reject, all, resolve, promised } = require('../core/promise');
  21. const { EventTarget } = require('../event/target');
  22. const { emit } = require('../event/core');
  23. const { identity, defer:async } = require('../lang/functional');
  24. const { extend, merge } = require('../util/object');
  25. const { fromIterator } = require('../util/array');
  26. const {
  27. constructTree, fetchItem, createQuery,
  28. isRootGroup, createQueryOptions
  29. } = require('./utils');
  30. const {
  31. bookmarkContract, groupContract, separatorContract
  32. } = require('./contract');
  33. const bmsrv = Cc["@mozilla.org/browser/nav-bookmarks-service;1"].
  34. getService(Ci.nsINavBookmarksService);
  35. /*
  36. * Mapping of uncreated bookmarks with their created
  37. * counterparts
  38. */
  39. const itemMap = new WeakMap();
  40. /*
  41. * Constant used by nsIHistoryQuery; 1 is a bookmark query
  42. * https://developer.mozilla.org/en-US/docs/XPCOM_Interface_Reference/nsINavHistoryQueryOptions
  43. */
  44. const BOOKMARK_QUERY = 1;
  45. /*
  46. * Bookmark Item classes
  47. */
  48. const Bookmark = Class({
  49. extends: [
  50. bookmarkContract.properties(identity)
  51. ],
  52. initialize: function initialize (options) {
  53. merge(this, bookmarkContract(extend(defaults, options)));
  54. },
  55. type: 'bookmark',
  56. toString: function () '[object Bookmark]'
  57. });
  58. exports.Bookmark = Bookmark;
  59. const Group = Class({
  60. extends: [
  61. groupContract.properties(identity)
  62. ],
  63. initialize: function initialize (options) {
  64. // Don't validate if root group
  65. if (isRootGroup(options))
  66. merge(this, options);
  67. else
  68. merge(this, groupContract(extend(defaults, options)));
  69. },
  70. type: 'group',
  71. toString: function () '[object Group]'
  72. });
  73. exports.Group = Group;
  74. const Separator = Class({
  75. extends: [
  76. separatorContract.properties(identity)
  77. ],
  78. initialize: function initialize (options) {
  79. merge(this, separatorContract(extend(defaults, options)));
  80. },
  81. type: 'separator',
  82. toString: function () '[object Separator]'
  83. });
  84. exports.Separator = Separator;
  85. /*
  86. * Functions
  87. */
  88. function save (items, options) {
  89. items = [].concat(items);
  90. options = options || {};
  91. let emitter = EventTarget();
  92. let results = [];
  93. let errors = [];
  94. let root = constructTree(items);
  95. let cache = new Map();
  96. let isExplicitSave = item => !!~items.indexOf(item);
  97. // `walk` returns an aggregate promise indicating the completion
  98. // of the `commitItem` on each node, not whether or not that
  99. // commit was successful
  100. // Force this to be async, as if a ducktype fails validation,
  101. // the promise implementation will fire an error event, which will
  102. // not trigger the handler as it's not yet bound
  103. //
  104. // Can remove after `Promise.jsm` is implemented in Bug 881047,
  105. // which will guarantee next tick execution
  106. async(() => root.walk(preCommitItem).then(commitComplete))();
  107. function preCommitItem ({value:item}) {
  108. // Do nothing if tree root, default group (unsavable),
  109. // or if it's a dependency and not explicitly saved (in the list
  110. // of items to be saved), and not needed to be saved
  111. if (item === null || // node is the tree root
  112. isRootGroup(item) ||
  113. (getId(item) && !isExplicitSave(item)))
  114. return;
  115. return promised(validate)(item)
  116. .then(() => commitItem(item, options))
  117. .then(data => construct(data, cache))
  118. .then(savedItem => {
  119. // If item was just created, make a map between
  120. // the creation object and created object,
  121. // so we can reference the item that doesn't have an id
  122. if (!getId(item))
  123. saveId(item, savedItem.id);
  124. // Emit both the processed item, and original item
  125. // so a mapping can be understood in handler
  126. emit(emitter, 'data', savedItem, item);
  127. // Push to results iff item was explicitly saved
  128. if (isExplicitSave(item))
  129. results[items.indexOf(item)] = savedItem;
  130. }, reason => {
  131. // Force reason to be a string for consistency
  132. reason = reason + '';
  133. // Emit both the reason, and original item
  134. // so a mapping can be understood in handler
  135. emit(emitter, 'error', reason + '', item);
  136. // Store unsaved item in results list
  137. results[items.indexOf(item)] = item;
  138. errors.push(reason);
  139. });
  140. }
  141. // Called when traversal of the node tree is completed and all
  142. // items have been committed
  143. function commitComplete () {
  144. emit(emitter, 'end', results);
  145. }
  146. return emitter;
  147. }
  148. exports.save = save;
  149. function search (queries, options) {
  150. queries = [].concat(queries);
  151. let emitter = EventTarget();
  152. let cache = new Map();
  153. let queryObjs = queries.map(createQuery.bind(null, BOOKMARK_QUERY));
  154. let optionsObj = createQueryOptions(BOOKMARK_QUERY, options);
  155. // Can remove after `Promise.jsm` is implemented in Bug 881047,
  156. // which will guarantee next tick execution
  157. async(() => {
  158. send('sdk-places-query', { queries: queryObjs, options: optionsObj })
  159. .then(handleQueryResponse);
  160. })();
  161. function handleQueryResponse (data) {
  162. let deferreds = data.map(item => {
  163. return construct(item, cache).then(bookmark => {
  164. emit(emitter, 'data', bookmark);
  165. return bookmark;
  166. }, reason => {
  167. emit(emitter, 'error', reason);
  168. errors.push(reason);
  169. });
  170. });
  171. all(deferreds).then(data => {
  172. emit(emitter, 'end', data);
  173. }, () => emit(emitter, 'end', []));
  174. }
  175. return emitter;
  176. }
  177. exports.search = search;
  178. function remove (items) {
  179. return [].concat(items).map(item => {
  180. item.remove = true;
  181. return item;
  182. });
  183. }
  184. exports.remove = remove;
  185. /*
  186. * Internal Utilities
  187. */
  188. function commitItem (item, options) {
  189. // Get the item's ID, or getId it's saved version if it exists
  190. let id = getId(item);
  191. let data = normalize(item);
  192. let promise;
  193. data.id = id;
  194. if (!id) {
  195. promise = send('sdk-places-bookmarks-create', data);
  196. } else if (item.remove) {
  197. promise = send('sdk-places-bookmarks-remove', { id: id });
  198. } else {
  199. promise = send('sdk-places-bookmarks-last-updated', {
  200. id: id
  201. }).then(function (updated) {
  202. // If attempting to save an item that is not the
  203. // latest snapshot of a bookmark item, execute
  204. // the resolution function
  205. if (updated !== item.updated && options.resolve)
  206. return fetchItem(id)
  207. .then(options.resolve.bind(null, data));
  208. else
  209. return data;
  210. }).then(send.bind(null, 'sdk-places-bookmarks-save'));
  211. }
  212. return promise;
  213. }
  214. /*
  215. * Turns a bookmark item into a plain object,
  216. * converts `tags` from Set to Array, group instance to an id
  217. */
  218. function normalize (item) {
  219. let data = merge({}, item);
  220. // Circumvent prototype property of `type`
  221. delete data.type;
  222. data.type = item.type;
  223. data.tags = [];
  224. if (item.tags) {
  225. data.tags = fromIterator(item.tags);
  226. }
  227. data.group = getId(data.group) || exports.UNSORTED.id;
  228. return data;
  229. }
  230. /*
  231. * Takes a data object and constructs a BookmarkItem instance
  232. * of it, recursively generating parent instances as well.
  233. *
  234. * Pass in a `cache` Map to reuse instances of
  235. * bookmark items to reduce overhead;
  236. * The cache object is a map of id to a deferred with a
  237. * promise that resolves to the bookmark item.
  238. */
  239. function construct (object, cache, forced) {
  240. let item = instantiate(object);
  241. let deferred = defer();
  242. // Item could not be instantiated
  243. if (!item)
  244. return resolve(null);
  245. // Return promise for item if found in the cache,
  246. // and not `forced`. `forced` indicates that this is the construct
  247. // call that should not read from cache, but should actually perform
  248. // the construction, as it was set before several async calls
  249. if (cache.has(item.id) && !forced)
  250. return cache.get(item.id).promise;
  251. else if (cache.has(item.id))
  252. deferred = cache.get(item.id);
  253. else
  254. cache.set(item.id, deferred);
  255. // When parent group is found in cache, use
  256. // the same deferred value
  257. if (item.group && cache.has(item.group)) {
  258. cache.get(item.group).promise.then(group => {
  259. item.group = group;
  260. deferred.resolve(item);
  261. });
  262. // If not in the cache, and a root group, return
  263. // the premade instance
  264. } else if (rootGroups.get(item.group)) {
  265. item.group = rootGroups.get(item.group);
  266. deferred.resolve(item);
  267. // If not in the cache or a root group, fetch the parent
  268. } else {
  269. cache.set(item.group, defer());
  270. fetchItem(item.group).then(group => {
  271. return construct(group, cache, true);
  272. }).then(group => {
  273. item.group = group;
  274. deferred.resolve(item);
  275. }, deferred.reject);
  276. }
  277. return deferred.promise;
  278. }
  279. function instantiate (object) {
  280. if (object.type === 'bookmark')
  281. return Bookmark(object);
  282. if (object.type === 'group')
  283. return Group(object);
  284. if (object.type === 'separator')
  285. return Separator(object);
  286. return null;
  287. }
  288. /**
  289. * Validates a bookmark item; will throw an error if ininvalid,
  290. * to be used with `promised`. As bookmark items check on their class,
  291. * this only checks ducktypes
  292. */
  293. function validate (object) {
  294. if (!isDuckType(object)) return true;
  295. let contract = object.type === 'bookmark' ? bookmarkContract :
  296. object.type === 'group' ? groupContract :
  297. object.type === 'separator' ? separatorContract :
  298. null;
  299. if (!contract) {
  300. throw Error('No type specified');
  301. }
  302. // If object has a property set, and undefined,
  303. // manually override with default as it'll fail otherwise
  304. let withDefaults = Object.keys(defaults).reduce((obj, prop) => {
  305. if (obj[prop] == null) obj[prop] = defaults[prop];
  306. return obj;
  307. }, extend(object));
  308. contract(withDefaults);
  309. }
  310. function isDuckType (item) {
  311. return !(item instanceof Bookmark) &&
  312. !(item instanceof Group) &&
  313. !(item instanceof Separator);
  314. }
  315. function saveId (unsaved, id) {
  316. itemMap.set(unsaved, id);
  317. }
  318. // Fetches an item's ID from itself, or from the mapped items
  319. function getId (item) {
  320. return typeof item === 'number' ? item :
  321. item ? item.id || itemMap.get(item) :
  322. null;
  323. }
  324. /*
  325. * Set up the default, root groups
  326. */
  327. let defaultGroupMap = {
  328. MENU: bmsrv.bookmarksMenuFolder,
  329. TOOLBAR: bmsrv.toolbarFolder,
  330. UNSORTED: bmsrv.unfiledBookmarksFolder
  331. };
  332. let rootGroups = new Map();
  333. for (let i in defaultGroupMap) {
  334. let group = Object.freeze(Group({ title: i, id: defaultGroupMap[i] }));
  335. rootGroups.set(defaultGroupMap[i], group);
  336. exports[i] = group;
  337. }
  338. let defaults = {
  339. group: exports.UNSORTED,
  340. index: -1
  341. };