summaryrefslogtreecommitdiff
path: root/toolkit/jetpack/sdk/places/utils.js
blob: fe928c4ea8499dbfb730a8046330cc13bddc9001 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
/* 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/. */

'use strict';

module.metadata = {
  "stability": "experimental",
  "engines": {
    "Palemoon": "*",
    "Firefox": "*",
    "SeaMonkey": "*"
  }
};

const { Cc, Ci, Cu } = require('chrome');
const { Class } = require('../core/heritage');
const { method } = require('../lang/functional');
const { defer, promised, all } = require('../core/promise');
const { send } = require('../addon/events');
const { EventTarget } = require('../event/target');
const { merge } = require('../util/object');
const bmsrv = Cc["@mozilla.org/browser/nav-bookmarks-service;1"].
                getService(Ci.nsINavBookmarksService);

Cu.importGlobalProperties(["URL"]);

/*
 * TreeNodes are used to construct dependency trees
 * for BookmarkItems
 */
var TreeNode = Class({
  initialize: function (value) {
    this.value = value;
    this.children = [];
  },
  add: function (values) {
    [].concat(values).forEach(value => {
      this.children.push(value instanceof TreeNode ? value : TreeNode(value));
    });
  },
  get length () {
    let count = 0;
    this.walk(() => count++);
    // Do not count the current node
    return --count;
  },
  get: method(get),
  walk: method(walk),
  toString: () => '[object TreeNode]'
});
exports.TreeNode = TreeNode;

/*
 * Descends down from `node` applying `fn` to each in order.
 * `fn` can return values or promises -- if promise returned,
 * children are not processed until resolved. `fn` is passed
 * one argument, the current node, `curr`.
 */
function walk (curr, fn) {
  return promised(fn)(curr).then(val => {
    return all(curr.children.map(child => walk(child, fn)));
  });
}

/*
 * Descends from the TreeNode `node`, returning
 * the node with value `value` if found or `null`
 * otherwise
 */
function get (node, value) {
  if (node.value === value) return node;
  for (let child of node.children) {
    let found = get(child, value);
    if (found) return found;
  }
  return null;
}

/*
 * Constructs a tree of bookmark nodes
 * returning the root (value: null);
 */

function constructTree (items) {
  let root = TreeNode(null);
  items.forEach(treeify.bind(null, root));

  function treeify (root, item) {
    // If node already exists, skip
    let node = root.get(item);
    if (node) return node;
    node = TreeNode(item);

    let parentNode = item.group ? treeify(root, item.group) : root;
    parentNode.add(node);

    return node;
  }

  return root;
}
exports.constructTree = constructTree;

/*
 * Shortcut for converting an id, or an object with an id, into
 * an object with corresponding bookmark data
 */
function fetchItem (item) {
  return send('sdk-places-bookmarks-get', { id: item.id || item });
}
exports.fetchItem = fetchItem;

/*
 * Takes an ID or an object with ID and checks it against
 * the root bookmark folders
 */
function isRootGroup (id) {
  id = id && id.id;
  return ~[bmsrv.bookmarksMenuFolder, bmsrv.toolbarFolder,
    bmsrv.unfiledBookmarksFolder
  ].indexOf(id);
}
exports.isRootGroup = isRootGroup;

/*
 * Merges appropriate options into query based off of url
 * 4 scenarios:
 *
 * 'moz.com' // domain: moz.com, domainIsHost: true
 *    --> 'http://moz.com', 'http://moz.com/thunderbird'
 * '*.moz.com' // domain: moz.com, domainIsHost: false
 *    --> 'http://moz.com', 'http://moz.com/index', 'http://ff.moz.com/test'
 * 'http://moz.com' // uri: http://moz.com/
 *    --> 'http://moz.com/'
 * 'http://moz.com/*' // uri: http://moz.com/, domain: moz.com, domainIsHost: true
 *    --> 'http://moz.com/', 'http://moz.com/thunderbird'
 */

function urlQueryParser (query, url) {
  if (!url) return;
  if (/^https?:\/\//.test(url)) {
    query.uri = url.charAt(url.length - 1) === '/' ? url : url + '/';
    if (/\*$/.test(url)) {
      // Wildcard searches on URIs are not supported, so try to extract a
      // domain and filter the data later.
      url = url.replace(/\*$/, '');
      try {
        query.domain = new URL(url).hostname;
        query.domainIsHost = true;
        // Unfortunately here we cannot use an expando to store the wildcard,
        // cause the query is a wrapped native XPCOM object, so we reuse uri.
        // We clearly don't want to query for both uri and domain, thus we'll
        // have to handle this in host-query.js::execute()
        query.uri = url;
      } catch (ex) {
        // Cannot extract an host cause it's not a valid uri, the query will
        // just return nothing.
      }
    }
  } else {
    if (/^\*/.test(url)) {
      query.domain = url.replace(/^\*\./, '');
      query.domainIsHost = false;
    } else {
      query.domain = url;
      query.domainIsHost = true;
    }
  }
}
exports.urlQueryParser = urlQueryParser;

/*
 * Takes an EventEmitter and returns a promise that
 * aggregates results and handles a bulk resolve and reject
 */

function promisedEmitter (emitter) {
  let { promise, resolve, reject } = defer();
  let errors = [];
  emitter.on('error', error => errors.push(error));
  emitter.on('end', (items) => {
    if (errors.length) reject(errors[0]);
    else resolve(items);
  });
  return promise;
}
exports.promisedEmitter = promisedEmitter;


// https://developer.mozilla.org/en-US/docs/XPCOM_Interface_Reference/nsINavHistoryQueryOptions
function createQuery (type, query) {
  query = query || {};
  let qObj = {
    searchTerms: query.query
  };

  urlQueryParser(qObj, query.url);

  // 0 === history
  if (type === 0) {
    // PRTime used by query is in microseconds, not milliseconds
    qObj.beginTime = (query.from || 0) * 1000;
    qObj.endTime = (query.to || new Date()) * 1000;

    // Set reference time to Epoch
    qObj.beginTimeReference = 0;
    qObj.endTimeReference = 0;
  }
  // 1 === bookmarks
  else if (type === 1) {
    qObj.tags = query.tags;
    qObj.folder = query.group && query.group.id;
  }
  // 2 === unified (not implemented on platform)
  else if (type === 2) {

  }

  return qObj;
}
exports.createQuery = createQuery;

// https://developer.mozilla.org/en-US/docs/XPCOM_Interface_Reference/nsINavHistoryQueryOptions

const SORT_MAP = {
  title: 1,
  date: 3, // sort by visit date
  url: 5,
  visitCount: 7,
  // keywords currently unsupported
  // keyword: 9,
  dateAdded: 11, // bookmarks only
  lastModified: 13 // bookmarks only
};

function createQueryOptions (type, options) {
  options = options || {};
  let oObj = {};
  oObj.sortingMode = SORT_MAP[options.sort] || 0;
  if (options.descending && options.sort)
    oObj.sortingMode++;

  // Resolve to default sort if ineligible based on query type
  if (type === 0 && // history
      (options.sort === 'dateAdded' || options.sort === 'lastModified'))
    oObj.sortingMode = 0;

  oObj.maxResults = typeof options.count === 'number' ? options.count : 0;

  oObj.queryType = type;

  return oObj;
}
exports.createQueryOptions = createQueryOptions;


function mapBookmarkItemType (type) {
  if (typeof type === 'number') {
    if (bmsrv.TYPE_BOOKMARK === type) return 'bookmark';
    if (bmsrv.TYPE_FOLDER === type) return 'group';
    if (bmsrv.TYPE_SEPARATOR === type) return 'separator';
  } else {
    if ('bookmark' === type) return bmsrv.TYPE_BOOKMARK;
    if ('group' === type) return bmsrv.TYPE_FOLDER;
    if ('separator' === type) return bmsrv.TYPE_SEPARATOR;
  }
}
exports.mapBookmarkItemType = mapBookmarkItemType;