Bug 1374237 - Part 1: Uniquify the name of the convert functions in ext-cookies.js and ext-bookmarks.js, r?mixedpuppy
Also declare all top-level functions in those files via const.
MozReview-Commit-ID: FsIEHVeotX8
--- a/browser/components/extensions/ext-bookmarks.js
+++ b/browser/components/extensions/ext-bookmarks.js
@@ -5,17 +5,17 @@
// The ext-* files are imported into the same scopes.
/* import-globals-from ext-browserAction.js */
XPCOMUtils.defineLazyModuleGetter(this, "PlacesUtils",
"resource://gre/modules/PlacesUtils.jsm");
let listenerCount = 0;
-function getTree(rootGuid, onlyChildren) {
+const getTree = (rootGuid, onlyChildren) => {
function convert(node, parent) {
let treenode = {
id: node.guid,
title: node.title || "",
index: node.index,
dateAdded: node.dateAdded / 1000,
};
@@ -52,19 +52,19 @@ function getTree(rootGuid, onlyChildren)
let children = root.children || [];
return children.map(child => convert(child, root));
}
let treenode = convert(root, null);
treenode.parentId = root.parentGuid;
// It seems like the array always just contains the root node.
return [treenode];
}).catch(e => Promise.reject({message: e.message}));
-}
+};
-function convert(result) {
+const convertBookmarks = result => {
let node = {
id: result.guid,
title: result.title || "",
index: result.index,
dateAdded: result.dateAdded.getTime(),
};
if (result.guid != PlacesUtils.bookmarks.rootGuid) {
@@ -73,17 +73,17 @@ function convert(result) {
if (result.type == PlacesUtils.bookmarks.TYPE_BOOKMARK) {
node.url = result.url.href; // Output is always URL object.
} else {
node.dateGroupModified = result.lastModified.getTime();
}
return node;
-}
+};
let observer = {
skipTags: true,
skipDescendantsOnItemRemoval: true,
onBeginUpdateBatch() {},
onEndUpdateBatch() {},
@@ -158,45 +158,45 @@ let observer = {
return;
}
this.emit("changed", {guid, info});
},
};
EventEmitter.decorate(observer);
-function decrementListeners() {
+const decrementListeners = () => {
listenerCount -= 1;
if (!listenerCount) {
PlacesUtils.bookmarks.removeObserver(observer);
}
-}
+};
-function incrementListeners() {
+const incrementListeners = () => {
listenerCount++;
if (listenerCount == 1) {
PlacesUtils.bookmarks.addObserver(observer);
}
-}
+};
this.bookmarks = class extends ExtensionAPI {
getAPI(context) {
return {
bookmarks: {
async get(idOrIdList) {
let list = Array.isArray(idOrIdList) ? idOrIdList : [idOrIdList];
try {
let bookmarks = [];
for (let id of list) {
let bookmark = await PlacesUtils.bookmarks.fetch({guid: id});
if (!bookmark) {
throw new Error("Bookmark not found");
}
- bookmarks.push(convert(bookmark));
+ bookmarks.push(convertBookmarks(bookmark));
}
return bookmarks;
} catch (error) {
return Promise.reject({message: error.message});
}
},
getChildren: function(id) {
@@ -208,21 +208,21 @@ this.bookmarks = class extends Extension
return getTree(PlacesUtils.bookmarks.rootGuid, false);
},
getSubTree: function(id) {
return getTree(id, false);
},
search: function(query) {
- return PlacesUtils.bookmarks.search(query).then(result => result.map(convert));
+ return PlacesUtils.bookmarks.search(query).then(result => result.map(convertBookmarks));
},
getRecent: function(numberOfItems) {
- return PlacesUtils.bookmarks.getRecent(numberOfItems).then(result => result.map(convert));
+ return PlacesUtils.bookmarks.getRecent(numberOfItems).then(result => result.map(convertBookmarks));
},
create: function(bookmark) {
let info = {
title: bookmark.title || "",
};
// If url is NULL or missing, it will be a folder.
@@ -239,17 +239,17 @@ this.bookmarks = class extends Extension
if (bookmark.parentId !== null) {
info.parentGuid = bookmark.parentId;
} else {
info.parentGuid = PlacesUtils.bookmarks.unfiledGuid;
}
try {
- return PlacesUtils.bookmarks.insert(info).then(convert)
+ return PlacesUtils.bookmarks.insert(info).then(convertBookmarks)
.catch(error => Promise.reject({message: error.message}));
} catch (e) {
return Promise.reject({message: `Invalid bookmark: ${JSON.stringify(info)}`});
}
},
move: function(id, destination) {
let info = {
@@ -258,17 +258,17 @@ this.bookmarks = class extends Extension
if (destination.parentId !== null) {
info.parentGuid = destination.parentId;
}
info.index = (destination.index === null) ?
PlacesUtils.bookmarks.DEFAULT_INDEX : destination.index;
try {
- return PlacesUtils.bookmarks.update(info).then(convert)
+ return PlacesUtils.bookmarks.update(info).then(convertBookmarks)
.catch(error => Promise.reject({message: error.message}));
} catch (e) {
return Promise.reject({message: `Invalid bookmark: ${JSON.stringify(info)}`});
}
},
update: function(id, changes) {
let info = {
@@ -278,17 +278,17 @@ this.bookmarks = class extends Extension
if (changes.title !== null) {
info.title = changes.title;
}
if (changes.url !== null) {
info.url = changes.url;
}
try {
- return PlacesUtils.bookmarks.update(info).then(convert)
+ return PlacesUtils.bookmarks.update(info).then(convertBookmarks)
.catch(error => Promise.reject({message: error.message}));
} catch (e) {
return Promise.reject({message: `Invalid bookmark: ${JSON.stringify(info)}`});
}
},
remove: function(id) {
let info = {
--- a/toolkit/components/extensions/ext-cookies.js
+++ b/toolkit/components/extensions/ext-cookies.js
@@ -5,17 +5,17 @@
XPCOMUtils.defineLazyModuleGetter(this, "ContextualIdentityService",
"resource://gre/modules/ContextualIdentityService.jsm");
XPCOMUtils.defineLazyModuleGetter(this, "NetUtil",
"resource://gre/modules/NetUtil.jsm");
/* globals DEFAULT_STORE, PRIVATE_STORE */
-function convert({cookie, isPrivate}) {
+const convertCookie = ({cookie, isPrivate}) => {
let result = {
name: cookie.name,
value: cookie.value,
domain: cookie.host,
hostOnly: !cookie.isDomain,
path: cookie.path,
secure: cookie.isSecure,
httpOnly: cookie.isHttpOnly,
@@ -30,25 +30,25 @@ function convert({cookie, isPrivate}) {
result.storeId = getCookieStoreIdForContainer(cookie.originAttributes.userContextId);
} else if (cookie.originAttributes.privateBrowsingId || isPrivate) {
result.storeId = PRIVATE_STORE;
} else {
result.storeId = DEFAULT_STORE;
}
return result;
-}
+};
-function isSubdomain(otherDomain, baseDomain) {
+const isSubdomain = (otherDomain, baseDomain) => {
return otherDomain == baseDomain || otherDomain.endsWith("." + baseDomain);
-}
+};
// Checks that the given extension has permission to set the given cookie for
// the given URI.
-function checkSetCookiePermissions(extension, uri, cookie) {
+const checkSetCookiePermissions = (extension, uri, cookie) => {
// Permission checks:
//
// - If the extension does not have permissions for the specified
// URL, it cannot set cookies for it.
//
// - If the specified URL could not set the given cookie, neither can
// the extension.
//
@@ -124,19 +124,19 @@ function checkSetCookiePermissions(exten
// domain cookie.
cookie.host = "." + cookie.host;
// We don't do any significant checking of path permissions. RFC2109
// suggests we only allow sites to add cookies for sub-paths, similar to
// same origin policy enforcement, but no-one implements this.
return true;
-}
+};
-function* query(detailsIn, props, context) {
+const query = function* (detailsIn, props, context) {
// Different callers want to filter on different properties. |props|
// tells us which ones they're interested in.
let details = {};
props.forEach(property => {
if (detailsIn[property] !== null) {
details[property] = detailsIn[property];
}
});
@@ -263,36 +263,36 @@ function* query(detailsIn, props, contex
}
while (enumerator.hasMoreElements()) {
let cookie = enumerator.getNext().QueryInterface(Ci.nsICookie2);
if (matches(cookie)) {
yield {cookie, isPrivate, storeId};
}
}
-}
+};
this.cookies = class extends ExtensionAPI {
getAPI(context) {
let {extension} = context;
let self = {
cookies: {
get: function(details) {
// FIXME: We don't sort by length of path and creation time.
for (let cookie of query(details, ["url", "name", "storeId"], context)) {
- return Promise.resolve(convert(cookie));
+ return Promise.resolve(convertCookie(cookie));
}
// Found no match.
return Promise.resolve(null);
},
getAll: function(details) {
let allowed = ["url", "name", "domain", "path", "secure", "session", "storeId"];
- let result = Array.from(query(details, allowed, context), convert);
+ let result = Array.from(query(details, allowed, context), convertCookie);
return Promise.resolve(result);
},
set: function(details) {
let uri = NetUtil.newURI(details.url).QueryInterface(Ci.nsIURL);
let path;
@@ -379,17 +379,17 @@ this.cookies = class extends ExtensionAP
},
onChanged: new SingletonEventManager(context, "cookies.onChanged", fire => {
let observer = (subject, topic, data) => {
let notify = (removed, cookie, cause) => {
cookie.QueryInterface(Ci.nsICookie2);
if (extension.whiteListedHosts.matchesCookie(cookie)) {
- fire.async({removed, cookie: convert({cookie, isPrivate: topic == "private-cookie-changed"}), cause});
+ fire.async({removed, cookie: convertCookie({cookie, isPrivate: topic == "private-cookie-changed"}), cause});
}
};
// We do our best effort here to map the incompatible states.
switch (data) {
case "deleted":
notify(true, subject, "explicit");
break;