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: /** michael@0: * Utility singleton for manipulating bookmarks. michael@0: */ michael@0: var Bookmarks = { michael@0: get metroRoot() { michael@0: return PlacesUtils.annotations.getItemsWithAnnotation('metro/bookmarksRoot', {})[0]; michael@0: }, michael@0: michael@0: logging: false, michael@0: log: function(msg) { michael@0: if (this.logging) { michael@0: Services.console.logStringMessage(msg); michael@0: } michael@0: }, michael@0: michael@0: addForURI: function bh_addForURI(aURI, aTitle, callback) { michael@0: this.isURIBookmarked(aURI, function (isBookmarked) { michael@0: if (isBookmarked) michael@0: return; michael@0: michael@0: let bookmarkTitle = aTitle || aURI.spec; michael@0: let bookmarkService = PlacesUtils.bookmarks; michael@0: let bookmarkId = bookmarkService.insertBookmark(Bookmarks.metroRoot, michael@0: aURI, michael@0: bookmarkService.DEFAULT_INDEX, michael@0: bookmarkTitle); michael@0: michael@0: // XXX Used for browser-chrome tests michael@0: let event = document.createEvent("Events"); michael@0: event.initEvent("BookmarkCreated", true, false); michael@0: window.dispatchEvent(event); michael@0: michael@0: if (callback) michael@0: callback(bookmarkId); michael@0: }); michael@0: }, michael@0: michael@0: _isMetroBookmark: function(aItemId) { michael@0: return PlacesUtils.bookmarks.getFolderIdForItem(aItemId) == Bookmarks.metroRoot; michael@0: }, michael@0: michael@0: isURIBookmarked: function bh_isURIBookmarked(aURI, callback) { michael@0: if (!callback) michael@0: return; michael@0: PlacesUtils.asyncGetBookmarkIds(aURI, aItemIds => { michael@0: callback(aItemIds && aItemIds.length > 0 && aItemIds.some(this._isMetroBookmark)); michael@0: }); michael@0: }, michael@0: michael@0: removeForURI: function bh_removeForURI(aURI, callback) { michael@0: // XXX blargle xpconnect! might not matter, but a method on michael@0: // nsINavBookmarksService that takes an array of items to michael@0: // delete would be faster. better yet, a method that takes a URI! michael@0: PlacesUtils.asyncGetBookmarkIds(aURI, (aItemIds) => { michael@0: aItemIds.forEach((aItemId) => { michael@0: if (this._isMetroBookmark(aItemId)) { michael@0: PlacesUtils.bookmarks.removeItem(aItemId); michael@0: } michael@0: }); michael@0: michael@0: if (callback) michael@0: callback(); michael@0: michael@0: // XXX Used for browser-chrome tests michael@0: let event = document.createEvent("Events"); michael@0: event.initEvent("BookmarkRemoved", true, false); michael@0: window.dispatchEvent(event); michael@0: }); michael@0: } michael@0: };