Bug 1157709 - Removed all uses of PlacesUtils.asyncGetBookmarkIds. r=mak77

MozReview-Commit-ID: FPhhbbIxOwz
This commit is contained in:
Pauline 2017-05-11 14:23:03 +01:00
Родитель 831876bb4f
Коммит 7a2cca54c5
6 изменённых файлов: 59 добавлений и 191 удалений

Просмотреть файл

@ -1287,7 +1287,7 @@ var BookmarkingUI = {
if (!this._shouldUpdateStarState()) {
return this.STATUS_UNSTARRED;
}
if (this._pendingStmt)
if (this._pendingUpdate)
return this.STATUS_UPDATING;
return this.button.hasAttribute("starred") ? this.STATUS_STARRED
: this.STATUS_UNSTARRED;
@ -1642,7 +1642,7 @@ var BookmarkingUI = {
},
_hasBookmarksObserver: false,
_itemIds: [],
_itemGuids: [],
uninit: function BUI_uninit() {
this._updateBookmarkPageMenuItem(true);
CustomizableUI.removeListener(this);
@ -1653,9 +1653,8 @@ var BookmarkingUI = {
PlacesUtils.removeLazyBookmarkObserver(this);
}
if (this._pendingStmt) {
this._pendingStmt.cancel();
delete this._pendingStmt;
if (this._pendingUpdate) {
delete this._pendingUpdate;
}
},
@ -1667,43 +1666,42 @@ var BookmarkingUI = {
},
updateStarState: function BUI_updateStarState() {
// Reset tracked values.
this._uri = gBrowser.currentURI;
this._itemIds = [];
this._itemGuids = [];
let aItemGuids = [];
if (this._pendingStmt) {
this._pendingStmt.cancel();
delete this._pendingStmt;
}
// those objects are use to check if we are in the current iteration before
// returning any result.
let pendingUpdate = this._pendingUpdate = {};
this._pendingStmt = PlacesUtils.asyncGetBookmarkIds(this._uri, (aItemIds, aURI) => {
// Safety check that the bookmarked URI equals the tracked one.
if (!aURI.equals(this._uri)) {
Components.utils.reportError("BookmarkingUI did not receive current URI");
return;
}
PlacesUtils.bookmarks.fetch({url: this._uri}, b => aItemGuids.push(b.guid))
.catch(Components.utils.reportError)
.then(() => {
if (pendingUpdate != this._pendingUpdate) {
return;
}
// It's possible that onItemAdded gets called before the async statement
// calls back. For such an edge case, retain all unique entries from both
// arrays.
this._itemIds = this._itemIds.filter(
id => !aItemIds.includes(id)
).concat(aItemIds);
// It's possible that onItemAdded gets called before the async statement
// calls back. For such an edge case, retain all unique entries from the
// array.
this._itemGuids = this._itemGuids.filter(
guid => !aItemGuids.includes(guid)
).concat(aItemGuids);
this._updateStar();
this._updateStar();
// Start observing bookmarks if needed.
if (!this._hasBookmarksObserver) {
try {
PlacesUtils.addLazyBookmarkObserver(this);
this._hasBookmarksObserver = true;
} catch (ex) {
Components.utils.reportError("BookmarkingUI failed adding a bookmarks observer: " + ex);
}
}
// Start observing bookmarks if needed.
if (!this._hasBookmarksObserver) {
try {
PlacesUtils.addLazyBookmarkObserver(this);
this._hasBookmarksObserver = true;
} catch (ex) {
Components.utils.reportError("BookmarkingUI failed adding a bookmarks observer: " + ex);
}
}
delete this._pendingStmt;
});
delete this._pendingUpdate;
});
},
_updateStar: function BUI__updateStar() {
@ -1715,7 +1713,7 @@ var BookmarkingUI = {
return;
}
if (this._itemIds.length > 0) {
if (this._itemGuids.length > 0) {
this.broadcaster.setAttribute("starred", "true");
this.broadcaster.setAttribute("buttontooltiptext", this._starredTooltip);
if (this.button.getAttribute("overflowedItem") == "true") {
@ -1735,7 +1733,7 @@ var BookmarkingUI = {
* to the default (Bookmark This Page) for OS X.
*/
_updateBookmarkPageMenuItem: function BUI__updateBookmarkPageMenuItem(forceReset) {
let isStarred = !forceReset && this._itemIds.length > 0;
let isStarred = !forceReset && this._itemGuids.length > 0;
let label = isStarred ? "editlabel" : "bookmarklabel";
if (this.broadcaster) {
this.broadcaster.setAttribute("label", this.broadcaster.getAttribute(label));
@ -1832,7 +1830,7 @@ var BookmarkingUI = {
}
// Handle special case when the button is in the panel.
let isBookmarked = this._itemIds.length > 0;
let isBookmarked = this._itemGuids.length > 0;
if (this._currentAreaType == CustomizableUI.TYPE_MENU_PANEL) {
this._showSubview();
@ -1846,7 +1844,7 @@ var BookmarkingUI = {
}
// Ignore clicks on the star if we are updating its state.
if (!this._pendingStmt) {
if (!this._pendingUpdate) {
if (!isBookmarked)
this._showBookmarkedNotification();
PlacesCommandHook.bookmarkCurrentPage(true);
@ -1911,49 +1909,48 @@ var BookmarkingUI = {
},
// nsINavBookmarkObserver
onItemAdded: function BUI_onItemAdded(aItemId, aParentId, aIndex, aItemType,
aURI) {
onItemAdded(aItemId, aParentId, aIndex, aItemType, aURI, aTitle, aDateAdded, aGuid) {
if (aURI && aURI.equals(this._uri)) {
// If a new bookmark has been added to the tracked uri, register it.
if (!this._itemIds.includes(aItemId)) {
this._itemIds.push(aItemId);
if (!this._itemGuids.includes(aGuid)) {
this._itemGuids.push(aGuid);
// Only need to update the UI if it wasn't marked as starred before:
if (this._itemIds.length == 1) {
if (this._itemGuids.length == 1) {
this._updateStar();
}
}
}
},
onItemRemoved: function BUI_onItemRemoved(aItemId) {
let index = this._itemIds.indexOf(aItemId);
onItemRemoved(aItemId, aParentId, aIndex, aItemType, aURI, aGuid) {
let index = this._itemGuids.indexOf(aGuid);
// If one of the tracked bookmarks has been removed, unregister it.
if (index != -1) {
this._itemIds.splice(index, 1);
this._itemGuids.splice(index, 1);
// Only need to update the UI if the page is no longer starred
if (this._itemIds.length == 0) {
if (this._itemGuids.length == 0) {
this._updateStar();
}
}
},
onItemChanged: function BUI_onItemChanged(aItemId, aProperty,
aIsAnnotationProperty, aNewValue) {
onItemChanged(aItemId, aProperty, aIsAnnotationProperty, aNewValue, aLastModified,
aItemType, aParentId, aGuid) {
if (aProperty == "uri") {
let index = this._itemIds.indexOf(aItemId);
let index = this._itemGuids.indexOf(aGuid);
// If the changed bookmark was tracked, check if it is now pointing to
// a different uri and unregister it.
if (index != -1 && aNewValue != this._uri.spec) {
this._itemIds.splice(index, 1);
this._itemGuids.splice(index, 1);
// Only need to update the UI if the page is no longer starred
if (this._itemIds.length == 0) {
if (this._itemGuids.length == 0) {
this._updateStar();
}
} else if (index == -1 && aNewValue == this._uri.spec) {
// If another bookmark is now pointing to the tracked uri, register it.
this._itemIds.push(aItemId);
this._itemGuids.push(aGuid);
// Only need to update the UI if it wasn't marked as starred before:
if (this._itemIds.length == 1) {
if (this._itemGuids.length == 1) {
this._updateStar();
}
}
@ -1988,8 +1985,8 @@ var BookmarkingUI = {
this._starButtonLabel = currentLabel;
if (currentLabel == this._starButtonLabel) {
let desiredLabel = this._itemIds.length > 0 ? this._starButtonOverflowedStarredLabel
: this._starButtonOverflowedLabel;
let desiredLabel = this._itemGuids.length > 0 ? this._starButtonOverflowedStarredLabel
: this._starButtonOverflowedLabel;
aNode.setAttribute("label", desiredLabel);
}
},

Просмотреть файл

@ -1504,15 +1504,7 @@ PT.Tag.prototype = {
*execute(aURIs, aTags) {
let onUndo = [], onRedo = [];
for (let uri of aURIs) {
let promiseIsBookmarked = function* () {
let deferred = Promise.defer();
PlacesUtils.asyncGetBookmarkIds(
uri, ids => { deferred.resolve(ids.length > 0); });
return deferred.promise;
};
if (yield promiseIsBookmarked(uri)) {
if (!(yield PlacesUtils.bookmarks.fetch({ url: uri }))) {
// Tagging is only allowed for bookmarked URIs (but see 424160).
let createTxn = TransactionsHistory.getRawTransaction(
PT.NewBookmark({ url: uri

Просмотреть файл

@ -1549,41 +1549,6 @@ this.PlacesUtils = {
});
},
/**
* Given a uri returns list of itemIds associated to it.
*
* @param aURI
* nsIURI or spec of the page.
* @param aCallback
* Function to be called when done.
* The function will receive an array of itemIds associated to aURI and
* aURI itself.
*
* @return A object with a .cancel() method allowing to cancel the request.
*
* @note Children of live bookmarks folders are excluded. The callback function is
* not invoked if the request is cancelled or hits an error.
*/
asyncGetBookmarkIds: function PU_asyncGetBookmarkIds(aURI, aCallback) {
let abort = false;
let itemIds = [];
Task.spawn(function* () {
let conn = yield this.promiseDBConnection();
const QUERY_STR = `SELECT b.id FROM moz_bookmarks b
JOIN moz_places h on h.id = b.fk
WHERE h.url_hash = hash(:url) AND h.url = :url`;
let spec = aURI instanceof Ci.nsIURI ? aURI.spec : aURI;
yield conn.executeCached(QUERY_STR, { url: spec }, aRow => {
if (abort)
throw StopIteration;
itemIds.push(aRow.getResultByIndex(0));
});
if (!abort)
aCallback(itemIds, aURI);
}.bind(this)).then(null, Cu.reportError);
return { cancel: () => { abort = true; } };
},
/**
* Lazily adds a bookmarks observer, waiting for the bookmarks service to be
* alive before registering the observer. This is especially useful in the

Просмотреть файл

@ -1,77 +0,0 @@
/**
* This file tests PlacesUtils.asyncGetBookmarkIds method.
*/
const TEST_URL = "http://www.example.com/";
var promiseAsyncGetBookmarkIds = Task.async(function* (url) {
yield PlacesTestUtils.promiseAsyncUpdates();
return new Promise(resolve => {
PlacesUtils.asyncGetBookmarkIds(url, (itemIds, uri) => {
Assert.equal(uri, url);
resolve({ itemIds, url });
});
});
});
add_task(function* test_no_bookmark() {
let { itemIds, url } = yield promiseAsyncGetBookmarkIds(TEST_URL);
Assert.equal(itemIds.length, 0);
Assert.equal(url, TEST_URL);
});
add_task(function* test_one_bookmark() {
let bookmark = yield PlacesUtils.bookmarks.insert({
parentGuid: PlacesUtils.bookmarks.unfiledGuid,
url: TEST_URL,
title: "test"
});
let itemId = yield PlacesUtils.promiseItemId(bookmark.guid);
{
let { itemIds, url } = yield promiseAsyncGetBookmarkIds(NetUtil.newURI(TEST_URL));
Assert.equal(itemIds.length, 1);
Assert.equal(itemIds[0], itemId);
Assert.equal(url.spec, TEST_URL);
}
{
let { itemIds, url } = yield promiseAsyncGetBookmarkIds(TEST_URL);
Assert.equal(itemIds.length, 1);
Assert.equal(itemIds[0], itemId);
Assert.equal(url, TEST_URL);
}
yield PlacesUtils.bookmarks.remove(bookmark);
});
add_task(function* test_multiple_bookmarks() {
let ids = [];
let bookmark1 = yield PlacesUtils.bookmarks.insert({
parentGuid: PlacesUtils.bookmarks.unfiledGuid,
url: TEST_URL,
title: "test"
});
ids.push((yield PlacesUtils.promiseItemId(bookmark1.guid)));
let bookmark2 = yield PlacesUtils.bookmarks.insert({
parentGuid: PlacesUtils.bookmarks.unfiledGuid,
url: TEST_URL,
title: "test"
});
ids.push((yield PlacesUtils.promiseItemId(bookmark2.guid)));
let { itemIds, url } = yield promiseAsyncGetBookmarkIds(TEST_URL);
Assert.deepEqual(ids, itemIds);
Assert.equal(url, TEST_URL);
yield PlacesUtils.bookmarks.remove(bookmark1);
yield PlacesUtils.bookmarks.remove(bookmark2);
});
add_task(function* test_cancel() {
let pending = PlacesUtils.asyncGetBookmarkIds(TEST_URL, () => {
Assert.ok(false, "A canceled pending statement should not be invoked");
});
pending.cancel();
let { itemIds, url } = yield promiseAsyncGetBookmarkIds(TEST_URL);
Assert.equal(itemIds.length, 0);
Assert.equal(url, TEST_URL);
});

Просмотреть файл

@ -1120,14 +1120,6 @@ add_task(function* test_tag_uri() {
, parentGuid: rootGuid };
let unbookmarked_uri = NetUtil.newURI("http://un.bookmarked.uri");
function* promiseIsBookmarked(aURI) {
let deferred = Promise.defer();
PlacesUtils.asyncGetBookmarkIds(aURI, ids => {
deferred.resolve(ids.length > 0);
});
return deferred.promise;
}
yield PT.batch(function* () {
bm_info_a.guid = yield PT.NewBookmark(bm_info_a).transact();
bm_info_b.guid = yield PT.NewBookmark(bm_info_b).transact();
@ -1142,7 +1134,7 @@ add_task(function* test_tag_uri() {
let tagWillAlsoBookmark = new Set();
for (let url of urls) {
if (!(yield promiseIsBookmarked(url))) {
if (!(yield bmsvc.fetch({ url }))) {
tagWillAlsoBookmark.add(url);
}
}
@ -1150,16 +1142,16 @@ add_task(function* test_tag_uri() {
function* ensureTagsSet() {
for (let url of urls) {
ensureTagsForURI(url, tags);
Assert.ok(yield promiseIsBookmarked(url));
Assert.ok(yield bmsvc.fetch({ url }));
}
}
function* ensureTagsUnset() {
for (let url of urls) {
ensureTagsForURI(url, []);
if (tagWillAlsoBookmark.has(url))
Assert.ok(!(yield promiseIsBookmarked(url)));
Assert.ok(!(yield bmsvc.fetch({ url })));
else
Assert.ok(yield promiseIsBookmarked(url));
Assert.ok(yield bmsvc.fetch({ url }));
}
}

Просмотреть файл

@ -109,7 +109,6 @@ skip-if = true
[test_frecency_observers.js]
[test_placeURIs.js]
[test_PlacesSearchAutocompleteProvider.js]
[test_PlacesUtils_asyncGetBookmarkIds.js]
[test_PlacesUtils_invalidateCachedGuidFor.js]
[test_PlacesUtils_lazyobservers.js]
[test_placesTxn.js]