summaryrefslogtreecommitdiffstats
path: root/testing/condprofile/condprof/scenarii/bookmark.js
blob: 70ef028320f5f0874ca19d1775fd542f1ecb0e80 (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
/* 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/. */
// 'arguments' is defined by the marionette harness.
/* global arguments */

const { PlacesUtils } = ChromeUtils.importESModule(
  "resource://gre/modules/PlacesUtils.sys.mjs"
);

let resolve = arguments[3];

try {
  // Get the number of current bookmarks
  PlacesUtils.promiseBookmarksTree(PlacesUtils.bookmarks.unfiledGuid, {
    includeItemIds: true,
  }).then(root => {
    let count = root.itemsCount;
    let maxBookmarks = arguments[2];
    // making sure we don't exceed the maximum number of bookmarks
    if (count >= maxBookmarks) {
      let toRemove = count - maxBookmarks + 1;
      console.log("We've reached the maximum number of bookmarks");
      console.log("Removing  " + toRemove);
      let children = root.children;
      for (let i = 0, p = Promise.resolve(); i < toRemove; i++) {
        p = p.then(
          _ =>
            new Promise(resolve =>
              PlacesUtils.bookmarks.remove(children[i].guid).then(res => {
                console.log("removed one bookmark");
                resolve(res);
              })
            )
        );
      }
    }
    // now adding the bookmark
    PlacesUtils.bookmarks
      .insert({
        parentGuid: PlacesUtils.bookmarks.unfiledGuid,
        url: arguments[0],
        title: arguments[1],
      })
      .then(res => {
        resolve(res);
      });
  });
} catch (error) {
  let res = { logs: {}, result: 1, result_message: error.toString() };
  resolve(res);
}