|
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 |
|
5 'use strict'; |
|
6 |
|
7 /** |
|
8 * Utility singleton for manipulating bookmarks. |
|
9 */ |
|
10 var Bookmarks = { |
|
11 get metroRoot() { |
|
12 return PlacesUtils.annotations.getItemsWithAnnotation('metro/bookmarksRoot', {})[0]; |
|
13 }, |
|
14 |
|
15 logging: false, |
|
16 log: function(msg) { |
|
17 if (this.logging) { |
|
18 Services.console.logStringMessage(msg); |
|
19 } |
|
20 }, |
|
21 |
|
22 addForURI: function bh_addForURI(aURI, aTitle, callback) { |
|
23 this.isURIBookmarked(aURI, function (isBookmarked) { |
|
24 if (isBookmarked) |
|
25 return; |
|
26 |
|
27 let bookmarkTitle = aTitle || aURI.spec; |
|
28 let bookmarkService = PlacesUtils.bookmarks; |
|
29 let bookmarkId = bookmarkService.insertBookmark(Bookmarks.metroRoot, |
|
30 aURI, |
|
31 bookmarkService.DEFAULT_INDEX, |
|
32 bookmarkTitle); |
|
33 |
|
34 // XXX Used for browser-chrome tests |
|
35 let event = document.createEvent("Events"); |
|
36 event.initEvent("BookmarkCreated", true, false); |
|
37 window.dispatchEvent(event); |
|
38 |
|
39 if (callback) |
|
40 callback(bookmarkId); |
|
41 }); |
|
42 }, |
|
43 |
|
44 _isMetroBookmark: function(aItemId) { |
|
45 return PlacesUtils.bookmarks.getFolderIdForItem(aItemId) == Bookmarks.metroRoot; |
|
46 }, |
|
47 |
|
48 isURIBookmarked: function bh_isURIBookmarked(aURI, callback) { |
|
49 if (!callback) |
|
50 return; |
|
51 PlacesUtils.asyncGetBookmarkIds(aURI, aItemIds => { |
|
52 callback(aItemIds && aItemIds.length > 0 && aItemIds.some(this._isMetroBookmark)); |
|
53 }); |
|
54 }, |
|
55 |
|
56 removeForURI: function bh_removeForURI(aURI, callback) { |
|
57 // XXX blargle xpconnect! might not matter, but a method on |
|
58 // nsINavBookmarksService that takes an array of items to |
|
59 // delete would be faster. better yet, a method that takes a URI! |
|
60 PlacesUtils.asyncGetBookmarkIds(aURI, (aItemIds) => { |
|
61 aItemIds.forEach((aItemId) => { |
|
62 if (this._isMetroBookmark(aItemId)) { |
|
63 PlacesUtils.bookmarks.removeItem(aItemId); |
|
64 } |
|
65 }); |
|
66 |
|
67 if (callback) |
|
68 callback(); |
|
69 |
|
70 // XXX Used for browser-chrome tests |
|
71 let event = document.createEvent("Events"); |
|
72 event.initEvent("BookmarkRemoved", true, false); |
|
73 window.dispatchEvent(event); |
|
74 }); |
|
75 } |
|
76 }; |