2014-06-25 05:12:07 +00:00
|
|
|
/* -*- indent-tabs-mode: nil; js-indent-level: 2 -*- */
|
2012-05-21 11:12:37 +00:00
|
|
|
/* 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/. */
|
2010-04-26 19:04:17 +00:00
|
|
|
|
2018-01-22 15:41:12 +00:00
|
|
|
this.EXPORTED_SYMBOLS = ["PlacesUtils"];
|
2010-06-30 21:24:03 +00:00
|
|
|
|
2015-03-20 08:39:25 +00:00
|
|
|
Cu.importGlobalProperties(["URL"]);
|
|
|
|
|
2018-01-29 23:20:18 +00:00
|
|
|
ChromeUtils.import("resource://gre/modules/XPCOMUtils.jsm");
|
|
|
|
ChromeUtils.import("resource://gre/modules/AppConstants.jsm");
|
2017-08-09 20:03:36 +00:00
|
|
|
|
|
|
|
XPCOMUtils.defineLazyModuleGetters(this, {
|
|
|
|
Services: "resource://gre/modules/Services.jsm",
|
|
|
|
NetUtil: "resource://gre/modules/NetUtil.jsm",
|
|
|
|
OS: "resource://gre/modules/osfile.jsm",
|
|
|
|
Sqlite: "resource://gre/modules/Sqlite.jsm",
|
|
|
|
Bookmarks: "resource://gre/modules/Bookmarks.jsm",
|
|
|
|
History: "resource://gre/modules/History.jsm",
|
|
|
|
AsyncShutdown: "resource://gre/modules/AsyncShutdown.jsm",
|
|
|
|
PlacesSyncUtils: "resource://gre/modules/PlacesSyncUtils.jsm",
|
|
|
|
});
|
2013-03-25 13:44:41 +00:00
|
|
|
|
2018-02-13 13:34:44 +00:00
|
|
|
XPCOMUtils.defineLazyGetter(this, "MOZ_ACTION_REGEX", () => {
|
|
|
|
return /^moz-action:([^,]+),(.*)$/;
|
|
|
|
});
|
|
|
|
|
2015-04-07 08:09:49 +00:00
|
|
|
// On Mac OSX, the transferable system converts "\r\n" to "\n\n", where
|
|
|
|
// we really just want "\n". On other platforms, the transferable system
|
|
|
|
// converts "\r\n" to "\n".
|
|
|
|
const NEWLINE = AppConstants.platform == "macosx" ? "\n" : "\r\n";
|
2010-04-26 19:04:17 +00:00
|
|
|
|
2017-05-11 15:22:27 +00:00
|
|
|
// Timers resolution is not always good, it can have a 16ms precision on Win.
|
|
|
|
const TIMERS_RESOLUTION_SKEW_MS = 16;
|
|
|
|
|
2010-04-26 19:04:17 +00:00
|
|
|
function QI_node(aNode, aIID) {
|
|
|
|
var result = null;
|
|
|
|
try {
|
|
|
|
result = aNode.QueryInterface(aIID);
|
2016-12-31 02:47:25 +00:00
|
|
|
} catch (e) {
|
2010-04-26 19:04:17 +00:00
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
2015-09-24 11:32:23 +00:00
|
|
|
function asContainer(aNode) {
|
|
|
|
return QI_node(aNode, Ci.nsINavHistoryContainerResultNode);
|
|
|
|
}
|
|
|
|
function asQuery(aNode) {
|
|
|
|
return QI_node(aNode, Ci.nsINavHistoryQueryResultNode);
|
|
|
|
}
|
2010-04-26 19:04:17 +00:00
|
|
|
|
2015-03-20 08:39:25 +00:00
|
|
|
/**
|
|
|
|
* Sends a bookmarks notification through the given observers.
|
|
|
|
*
|
|
|
|
* @param observers
|
|
|
|
* array of nsINavBookmarkObserver objects.
|
|
|
|
* @param notification
|
|
|
|
* the notification name.
|
|
|
|
* @param args
|
|
|
|
* array of arguments to pass to the notification.
|
|
|
|
*/
|
|
|
|
function notify(observers, notification, args) {
|
|
|
|
for (let observer of observers) {
|
|
|
|
try {
|
|
|
|
observer[notification](...args);
|
|
|
|
} catch (ex) {}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sends a keyword change notification.
|
|
|
|
*
|
|
|
|
* @param url
|
|
|
|
* the url to notify about.
|
|
|
|
* @param keyword
|
|
|
|
* The keyword to notify, or empty string if a keyword was removed.
|
|
|
|
*/
|
2017-05-12 12:42:39 +00:00
|
|
|
async function notifyKeywordChange(url, keyword, source) {
|
2015-03-20 08:39:25 +00:00
|
|
|
// Notify bookmarks about the removal.
|
|
|
|
let bookmarks = [];
|
2017-05-12 12:42:39 +00:00
|
|
|
await PlacesUtils.bookmarks.fetch({ url }, b => bookmarks.push(b));
|
2015-03-20 08:39:25 +00:00
|
|
|
for (let bookmark of bookmarks) {
|
2017-05-12 12:42:39 +00:00
|
|
|
bookmark.id = await PlacesUtils.promiseItemId(bookmark.guid);
|
|
|
|
bookmark.parentId = await PlacesUtils.promiseItemId(bookmark.parentGuid);
|
2015-03-20 08:39:25 +00:00
|
|
|
}
|
|
|
|
let observers = PlacesUtils.bookmarks.getObservers();
|
|
|
|
for (let bookmark of bookmarks) {
|
|
|
|
notify(observers, "onItemChanged", [ bookmark.id, "keyword", false,
|
|
|
|
keyword,
|
|
|
|
bookmark.lastModified * 1000,
|
|
|
|
bookmark.type,
|
|
|
|
bookmark.parentId,
|
2015-08-05 21:10:11 +00:00
|
|
|
bookmark.guid, bookmark.parentGuid,
|
2016-08-11 20:13:22 +00:00
|
|
|
"", source
|
2015-03-20 08:39:25 +00:00
|
|
|
]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-05-22 17:02:41 +00:00
|
|
|
/**
|
|
|
|
* Serializes the given node in JSON format.
|
|
|
|
*
|
|
|
|
* @param aNode
|
|
|
|
* An nsINavHistoryResultNode
|
|
|
|
* @param aIsLivemark
|
|
|
|
* Whether the node represents a livemark.
|
|
|
|
*/
|
|
|
|
function serializeNode(aNode, aIsLivemark) {
|
|
|
|
let data = {};
|
|
|
|
|
|
|
|
data.title = aNode.title;
|
|
|
|
data.id = aNode.itemId;
|
|
|
|
data.livemark = aIsLivemark;
|
2017-09-08 06:01:29 +00:00
|
|
|
// Add an instanceId so we can tell which instance of an FF session the data
|
|
|
|
// is coming from.
|
|
|
|
data.instanceId = PlacesUtils.instanceId;
|
2015-05-22 17:02:41 +00:00
|
|
|
|
|
|
|
let guid = aNode.bookmarkGuid;
|
2018-02-12 21:51:08 +00:00
|
|
|
let grandParentId;
|
|
|
|
|
2017-12-07 14:15:39 +00:00
|
|
|
// Some nodes, e.g. the unfiled/menu/toolbar ones can have a virtual guid, so
|
|
|
|
// we ignore any that are a folder shortcut. These will be handled below.
|
|
|
|
if (guid && !PlacesUtils.bookmarks.isVirtualRootItem(guid)) {
|
|
|
|
// TODO: Really guid should be set on everything, however currently this upsets
|
|
|
|
// the drag 'n' drop / cut/copy/paste operations.
|
2015-05-22 17:02:41 +00:00
|
|
|
data.itemGuid = guid;
|
2018-02-12 21:51:08 +00:00
|
|
|
if (aNode.parent) {
|
2015-05-22 17:02:41 +00:00
|
|
|
data.parent = aNode.parent.itemId;
|
2018-02-12 21:51:08 +00:00
|
|
|
data.parentGuid = aNode.parent.bookmarkGuid;
|
|
|
|
}
|
|
|
|
|
2015-05-22 17:02:41 +00:00
|
|
|
let grandParent = aNode.parent && aNode.parent.parent;
|
2018-02-12 21:51:08 +00:00
|
|
|
if (grandParent) {
|
|
|
|
grandParentId = grandParent.itemId;
|
|
|
|
}
|
2015-05-22 17:02:41 +00:00
|
|
|
|
|
|
|
data.dateAdded = aNode.dateAdded;
|
|
|
|
data.lastModified = aNode.lastModified;
|
|
|
|
|
|
|
|
let annos = PlacesUtils.getAnnotationsForItem(data.id);
|
|
|
|
if (annos.length > 0)
|
|
|
|
data.annos = annos;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (PlacesUtils.nodeIsURI(aNode)) {
|
|
|
|
// Check for url validity.
|
|
|
|
NetUtil.newURI(aNode.uri);
|
|
|
|
|
|
|
|
// Tag root accepts only folder nodes, not URIs.
|
|
|
|
if (data.parent == PlacesUtils.tagsFolderId)
|
|
|
|
throw new Error("Unexpected node type");
|
|
|
|
|
|
|
|
data.type = PlacesUtils.TYPE_X_MOZ_PLACE;
|
|
|
|
data.uri = aNode.uri;
|
|
|
|
|
|
|
|
if (aNode.tags)
|
|
|
|
data.tags = aNode.tags;
|
2016-12-31 02:47:25 +00:00
|
|
|
} else if (PlacesUtils.nodeIsContainer(aNode)) {
|
2015-05-22 17:02:41 +00:00
|
|
|
// Tag containers accept only uri nodes.
|
2018-02-12 21:51:08 +00:00
|
|
|
if (grandParentId == PlacesUtils.tagsFolderId)
|
2015-05-22 17:02:41 +00:00
|
|
|
throw new Error("Unexpected node type");
|
|
|
|
|
|
|
|
let concreteId = PlacesUtils.getConcreteItemId(aNode);
|
|
|
|
if (concreteId != -1) {
|
|
|
|
// This is a bookmark or a tag container.
|
|
|
|
if (PlacesUtils.nodeIsQuery(aNode) || concreteId != aNode.itemId) {
|
|
|
|
// This is a folder shortcut.
|
|
|
|
data.type = PlacesUtils.TYPE_X_MOZ_PLACE;
|
|
|
|
data.uri = aNode.uri;
|
|
|
|
data.concreteId = concreteId;
|
2017-12-07 14:15:39 +00:00
|
|
|
data.concreteGuid = PlacesUtils.getConcreteItemGuid(aNode);
|
2016-12-31 02:47:25 +00:00
|
|
|
} else {
|
2015-05-22 17:02:41 +00:00
|
|
|
// This is a bookmark folder.
|
|
|
|
data.type = PlacesUtils.TYPE_X_MOZ_PLACE_CONTAINER;
|
|
|
|
}
|
2016-12-31 02:47:25 +00:00
|
|
|
} else {
|
2015-05-22 17:02:41 +00:00
|
|
|
// This is a grouped container query, dynamically generated.
|
|
|
|
data.type = PlacesUtils.TYPE_X_MOZ_PLACE;
|
|
|
|
data.uri = aNode.uri;
|
|
|
|
}
|
2016-12-31 02:47:25 +00:00
|
|
|
} else if (PlacesUtils.nodeIsSeparator(aNode)) {
|
2015-05-22 17:02:41 +00:00
|
|
|
// Tag containers don't accept separators.
|
|
|
|
if (data.parent == PlacesUtils.tagsFolderId ||
|
2018-02-12 21:51:08 +00:00
|
|
|
grandParentId == PlacesUtils.tagsFolderId)
|
2015-05-22 17:02:41 +00:00
|
|
|
throw new Error("Unexpected node type");
|
|
|
|
|
|
|
|
data.type = PlacesUtils.TYPE_X_MOZ_PLACE_SEPARATOR;
|
|
|
|
}
|
|
|
|
|
|
|
|
return JSON.stringify(data);
|
|
|
|
}
|
|
|
|
|
2016-08-10 19:54:45 +00:00
|
|
|
// Imposed to limit database size.
|
|
|
|
const DB_URL_LENGTH_MAX = 65536;
|
|
|
|
const DB_TITLE_LENGTH_MAX = 4096;
|
2017-11-27 17:15:21 +00:00
|
|
|
const DB_DESCRIPTION_LENGTH_MAX = 256;
|
2016-08-10 19:54:45 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* List of bookmark object validators, one per each known property.
|
|
|
|
* Validators must throw if the property value is invalid and return a fixed up
|
|
|
|
* version of the value, if needed.
|
|
|
|
*/
|
|
|
|
const BOOKMARK_VALIDATORS = Object.freeze({
|
|
|
|
guid: simpleValidateFunc(v => PlacesUtils.isValidGuid(v)),
|
2018-02-20 16:31:41 +00:00
|
|
|
parentGuid: simpleValidateFunc(v => PlacesUtils.isValidGuid(v)),
|
|
|
|
guidPrefix: simpleValidateFunc(v => PlacesUtils.isValidGuidPrefix(v)),
|
2016-08-10 19:54:45 +00:00
|
|
|
index: simpleValidateFunc(v => Number.isInteger(v) &&
|
|
|
|
v >= PlacesUtils.bookmarks.DEFAULT_INDEX),
|
|
|
|
dateAdded: simpleValidateFunc(v => v.constructor.name == "Date"),
|
|
|
|
lastModified: simpleValidateFunc(v => v.constructor.name == "Date"),
|
|
|
|
type: simpleValidateFunc(v => Number.isInteger(v) &&
|
2017-06-15 17:24:17 +00:00
|
|
|
[ PlacesUtils.bookmarks.TYPE_BOOKMARK,
|
|
|
|
PlacesUtils.bookmarks.TYPE_FOLDER,
|
|
|
|
PlacesUtils.bookmarks.TYPE_SEPARATOR ].includes(v)),
|
2016-08-10 19:54:45 +00:00
|
|
|
title: v => {
|
2017-06-06 21:37:22 +00:00
|
|
|
if (v === null) {
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
if (typeof(v) == "string") {
|
|
|
|
return v.slice(0, DB_TITLE_LENGTH_MAX);
|
|
|
|
}
|
|
|
|
throw new Error("Invalid title");
|
2016-08-10 19:54:45 +00:00
|
|
|
},
|
|
|
|
url: v => {
|
|
|
|
simpleValidateFunc(val => (typeof(val) == "string" && val.length <= DB_URL_LENGTH_MAX) ||
|
|
|
|
(val instanceof Ci.nsIURI && val.spec.length <= DB_URL_LENGTH_MAX) ||
|
|
|
|
(val instanceof URL && val.href.length <= DB_URL_LENGTH_MAX)
|
|
|
|
).call(this, v);
|
|
|
|
if (typeof(v) === "string")
|
|
|
|
return new URL(v);
|
|
|
|
if (v instanceof Ci.nsIURI)
|
|
|
|
return new URL(v.spec);
|
|
|
|
return v;
|
2016-08-11 20:13:22 +00:00
|
|
|
},
|
|
|
|
source: simpleValidateFunc(v => Number.isInteger(v) &&
|
|
|
|
Object.values(PlacesUtils.bookmarks.SOURCES).includes(v)),
|
2017-05-12 07:04:51 +00:00
|
|
|
annos: simpleValidateFunc(v => Array.isArray(v) && v.length),
|
|
|
|
keyword: simpleValidateFunc(v => (typeof(v) == "string") && v.length),
|
|
|
|
charset: simpleValidateFunc(v => (typeof(v) == "string") && v.length),
|
|
|
|
postData: simpleValidateFunc(v => (typeof(v) == "string") && v.length),
|
|
|
|
tags: simpleValidateFunc(v => Array.isArray(v) && v.length),
|
2016-08-10 19:54:45 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
// Sync bookmark records can contain additional properties.
|
2016-09-16 21:55:20 +00:00
|
|
|
const SYNC_BOOKMARK_VALIDATORS = Object.freeze({
|
|
|
|
// Sync uses Places GUIDs for all records except roots.
|
2017-11-06 20:17:07 +00:00
|
|
|
recordId: simpleValidateFunc(v => typeof v == "string" && (
|
|
|
|
(PlacesSyncUtils.bookmarks.ROOTS.includes(v) || PlacesUtils.isValidGuid(v)))),
|
|
|
|
parentRecordId: v => SYNC_BOOKMARK_VALIDATORS.recordId(v),
|
2016-09-16 21:55:20 +00:00
|
|
|
// Sync uses kinds instead of types, which distinguish between livemarks,
|
|
|
|
// queries, and smart bookmarks.
|
2016-08-10 19:54:45 +00:00
|
|
|
kind: simpleValidateFunc(v => typeof v == "string" &&
|
|
|
|
Object.values(PlacesSyncUtils.bookmarks.KINDS).includes(v)),
|
|
|
|
query: simpleValidateFunc(v => v === null || (typeof v == "string" && v)),
|
|
|
|
folder: simpleValidateFunc(v => typeof v == "string" && v &&
|
|
|
|
v.length <= Ci.nsITaggingService.MAX_TAG_LENGTH),
|
|
|
|
tags: v => {
|
|
|
|
if (v === null) {
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
if (!Array.isArray(v)) {
|
|
|
|
throw new Error("Invalid tag array");
|
|
|
|
}
|
|
|
|
for (let tag of v) {
|
|
|
|
if (typeof tag != "string" || !tag ||
|
|
|
|
tag.length > Ci.nsITaggingService.MAX_TAG_LENGTH) {
|
|
|
|
throw new Error(`Invalid tag: ${tag}`);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return v;
|
|
|
|
},
|
|
|
|
keyword: simpleValidateFunc(v => v === null || typeof v == "string"),
|
|
|
|
description: simpleValidateFunc(v => v === null || typeof v == "string"),
|
|
|
|
loadInSidebar: simpleValidateFunc(v => v === true || v === false),
|
2017-01-17 19:45:08 +00:00
|
|
|
dateAdded: simpleValidateFunc(v => typeof v === "number"
|
|
|
|
&& v > PlacesSyncUtils.bookmarks.EARLIEST_BOOKMARK_TIMESTAMP),
|
2016-09-20 08:52:58 +00:00
|
|
|
feed: v => v === null ? v : BOOKMARK_VALIDATORS.url(v),
|
2016-08-10 19:54:45 +00:00
|
|
|
site: v => v === null ? v : BOOKMARK_VALIDATORS.url(v),
|
2016-09-16 21:55:20 +00:00
|
|
|
title: BOOKMARK_VALIDATORS.title,
|
|
|
|
url: BOOKMARK_VALIDATORS.url,
|
|
|
|
});
|
2016-08-10 19:54:45 +00:00
|
|
|
|
2016-11-18 22:15:59 +00:00
|
|
|
// Sync change records are passed between `PlacesSyncUtils` and the Sync
|
|
|
|
// bookmarks engine, and are used to update an item's sync status and change
|
|
|
|
// counter at the end of a sync.
|
|
|
|
const SYNC_CHANGE_RECORD_VALIDATORS = Object.freeze({
|
|
|
|
modified: simpleValidateFunc(v => typeof v == "number" && v >= 0),
|
|
|
|
counter: simpleValidateFunc(v => typeof v == "number" && v >= 0),
|
|
|
|
status: simpleValidateFunc(v => typeof v == "number" &&
|
|
|
|
Object.values(PlacesUtils.bookmarks.SYNC_STATUS).includes(v)),
|
|
|
|
tombstone: simpleValidateFunc(v => v === true || v === false),
|
|
|
|
synced: simpleValidateFunc(v => v === true || v === false),
|
|
|
|
});
|
|
|
|
|
2012-10-31 16:13:28 +00:00
|
|
|
this.PlacesUtils = {
|
2010-04-26 19:04:17 +00:00
|
|
|
// Place entries that are containers, e.g. bookmark folders or queries.
|
|
|
|
TYPE_X_MOZ_PLACE_CONTAINER: "text/x-moz-place-container",
|
|
|
|
// Place entries that are bookmark separators.
|
|
|
|
TYPE_X_MOZ_PLACE_SEPARATOR: "text/x-moz-place-separator",
|
|
|
|
// Place entries that are not containers or separators
|
|
|
|
TYPE_X_MOZ_PLACE: "text/x-moz-place",
|
|
|
|
// Place entries in shortcut url format (url\ntitle)
|
|
|
|
TYPE_X_MOZ_URL: "text/x-moz-url",
|
|
|
|
// Place entries formatted as HTML anchors
|
|
|
|
TYPE_HTML: "text/html",
|
|
|
|
// Place entries as raw URL text
|
|
|
|
TYPE_UNICODE: "text/unicode",
|
2011-06-30 13:34:02 +00:00
|
|
|
// Used to track the action that populated the clipboard.
|
|
|
|
TYPE_X_MOZ_PLACE_ACTION: "text/x-moz-place-action",
|
2010-04-26 19:04:17 +00:00
|
|
|
|
|
|
|
EXCLUDE_FROM_BACKUP_ANNO: "places/excludeFromBackup",
|
|
|
|
LMANNO_FEEDURI: "livemark/feedURI",
|
|
|
|
LMANNO_SITEURI: "livemark/siteURI",
|
|
|
|
POST_DATA_ANNO: "bookmarkProperties/POSTData",
|
|
|
|
READ_ONLY_ANNO: "placesInternal/READ_ONLY",
|
2013-03-21 17:06:15 +00:00
|
|
|
CHARSET_ANNO: "URIProperties/characterSet",
|
2016-09-29 20:44:08 +00:00
|
|
|
MOBILE_ROOT_ANNO: "mobile/bookmarksRoot",
|
2010-04-26 19:04:17 +00:00
|
|
|
|
|
|
|
TOPIC_SHUTDOWN: "places-shutdown",
|
|
|
|
TOPIC_INIT_COMPLETE: "places-init-complete",
|
|
|
|
TOPIC_DATABASE_LOCKED: "places-database-locked",
|
|
|
|
TOPIC_EXPIRATION_FINISHED: "places-expiration-finished",
|
|
|
|
TOPIC_FEEDBACK_UPDATED: "places-autocomplete-feedback-updated",
|
|
|
|
TOPIC_FAVICONS_EXPIRED: "places-favicons-expired",
|
|
|
|
TOPIC_VACUUM_STARTING: "places-vacuum-starting",
|
2012-12-08 19:50:54 +00:00
|
|
|
TOPIC_BOOKMARKS_RESTORE_BEGIN: "bookmarks-restore-begin",
|
|
|
|
TOPIC_BOOKMARKS_RESTORE_SUCCESS: "bookmarks-restore-success",
|
|
|
|
TOPIC_BOOKMARKS_RESTORE_FAILED: "bookmarks-restore-failed",
|
2010-04-26 19:04:17 +00:00
|
|
|
|
2018-02-13 13:34:44 +00:00
|
|
|
ACTION_SCHEME: "moz-action:",
|
|
|
|
|
2015-09-24 11:32:23 +00:00
|
|
|
asContainer: aNode => asContainer(aNode),
|
|
|
|
asQuery: aNode => asQuery(aNode),
|
2010-04-26 19:04:17 +00:00
|
|
|
|
2010-04-26 19:04:20 +00:00
|
|
|
endl: NEWLINE,
|
|
|
|
|
2016-05-13 15:09:06 +00:00
|
|
|
/**
|
|
|
|
* Is a string a valid GUID?
|
|
|
|
*
|
|
|
|
* @param guid: (String)
|
|
|
|
* @return (Boolean)
|
|
|
|
*/
|
|
|
|
isValidGuid(guid) {
|
2016-08-10 19:54:45 +00:00
|
|
|
return typeof guid == "string" && guid &&
|
|
|
|
(/^[a-zA-Z0-9\-_]{12}$/.test(guid));
|
2016-05-13 15:09:06 +00:00
|
|
|
},
|
|
|
|
|
2018-02-20 16:31:41 +00:00
|
|
|
/**
|
|
|
|
* Is a string a valid GUID prefix?
|
|
|
|
*
|
|
|
|
* @param guidPrefix: (String)
|
|
|
|
* @return (Boolean)
|
|
|
|
*/
|
|
|
|
isValidGuidPrefix(guidPrefix) {
|
|
|
|
return typeof guidPrefix == "string" && guidPrefix &&
|
|
|
|
(/^[a-zA-Z0-9\-_]{1,11}$/.test(guidPrefix));
|
|
|
|
},
|
|
|
|
|
2016-05-13 15:09:06 +00:00
|
|
|
/**
|
|
|
|
* Converts a string or n URL object to an nsIURI.
|
|
|
|
*
|
|
|
|
* @param url (URL) or (String)
|
|
|
|
* the URL to convert.
|
|
|
|
* @return nsIURI for the given URL.
|
|
|
|
*/
|
|
|
|
toURI(url) {
|
|
|
|
url = (url instanceof URL) ? url.href : url;
|
|
|
|
|
|
|
|
return NetUtil.newURI(url);
|
|
|
|
},
|
|
|
|
|
2016-05-13 14:32:48 +00:00
|
|
|
/**
|
|
|
|
* Convert a Date object to a PRTime (microseconds).
|
|
|
|
*
|
|
|
|
* @param date
|
|
|
|
* the Date object to convert.
|
|
|
|
* @return microseconds from the epoch.
|
|
|
|
*/
|
|
|
|
toPRTime(date) {
|
2015-04-28 12:19:43 +00:00
|
|
|
if (typeof date != "number" && date.constructor.name != "Date")
|
|
|
|
throw new Error("Invalid value passed to toPRTime");
|
2016-05-13 14:32:48 +00:00
|
|
|
return date * 1000;
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
2016-05-24 12:56:10 +00:00
|
|
|
* Convert a PRTime to a Date object.
|
2016-05-13 14:32:48 +00:00
|
|
|
*
|
|
|
|
* @param time
|
|
|
|
* microseconds from the epoch.
|
2016-05-24 12:56:10 +00:00
|
|
|
* @return a Date object.
|
2016-05-13 14:32:48 +00:00
|
|
|
*/
|
2016-05-24 12:56:10 +00:00
|
|
|
toDate(time) {
|
2015-04-28 12:19:43 +00:00
|
|
|
if (typeof time != "number")
|
|
|
|
throw new Error("Invalid value passed to toDate");
|
2016-05-24 12:56:10 +00:00
|
|
|
return new Date(parseInt(time / 1000));
|
2016-05-13 14:32:48 +00:00
|
|
|
},
|
|
|
|
|
2011-06-30 13:34:02 +00:00
|
|
|
/**
|
|
|
|
* Wraps a string in a nsISupportsString wrapper.
|
|
|
|
* @param aString
|
|
|
|
* The string to wrap.
|
|
|
|
* @returns A nsISupportsString object containing a string.
|
|
|
|
*/
|
|
|
|
toISupportsString: function PU_toISupportsString(aString) {
|
|
|
|
let s = Cc["@mozilla.org/supports-string;1"].
|
|
|
|
createInstance(Ci.nsISupportsString);
|
|
|
|
s.data = aString;
|
|
|
|
return s;
|
|
|
|
},
|
|
|
|
|
2010-04-26 19:04:17 +00:00
|
|
|
getFormattedString: function PU_getFormattedString(key, params) {
|
2010-06-30 21:24:03 +00:00
|
|
|
return bundle.formatStringFromName(key, params, params.length);
|
2010-04-26 19:04:17 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
getString: function PU_getString(key) {
|
2010-06-30 21:24:03 +00:00
|
|
|
return bundle.GetStringFromName(key);
|
2010-04-26 19:04:17 +00:00
|
|
|
},
|
|
|
|
|
2016-08-03 01:00:26 +00:00
|
|
|
/**
|
|
|
|
* Makes a moz-action URI for the given action and set of parameters.
|
|
|
|
*
|
|
|
|
* @param type
|
|
|
|
* The action type.
|
|
|
|
* @param params
|
|
|
|
* A JS object of action params.
|
|
|
|
* @returns A moz-action URI as a string.
|
|
|
|
*/
|
|
|
|
mozActionURI(type, params) {
|
|
|
|
let encodedParams = {};
|
|
|
|
for (let key in params) {
|
2016-10-19 13:09:01 +00:00
|
|
|
// Strip null or undefined.
|
|
|
|
// Regardless, don't encode them or they would be converted to a string.
|
|
|
|
if (params[key] === null || params[key] === undefined) {
|
|
|
|
continue;
|
|
|
|
}
|
2016-08-03 01:00:26 +00:00
|
|
|
encodedParams[key] = encodeURIComponent(params[key]);
|
|
|
|
}
|
2018-02-13 13:34:44 +00:00
|
|
|
return this.ACTION_SCHEME + type + "," + JSON.stringify(encodedParams);
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Parses a moz-action URL and returns its parts.
|
|
|
|
*
|
|
|
|
* @param url A moz-action URI.
|
|
|
|
* @note URL is in the format moz-action:ACTION,JSON_ENCODED_PARAMS
|
|
|
|
*/
|
|
|
|
parseActionUrl(url) {
|
|
|
|
if (url instanceof Ci.nsIURI)
|
|
|
|
url = url.spec;
|
|
|
|
else if (url instanceof URL)
|
|
|
|
url = url.href;
|
|
|
|
// Faster bailout.
|
|
|
|
if (!url.startsWith(this.ACTION_SCHEME))
|
|
|
|
return null;
|
|
|
|
|
|
|
|
try {
|
|
|
|
let [, type, params] = url.match(MOZ_ACTION_REGEX);
|
|
|
|
let action = {
|
|
|
|
type,
|
|
|
|
params: JSON.parse(params)
|
|
|
|
};
|
|
|
|
for (let key in action.params) {
|
|
|
|
action.params[key] = decodeURIComponent(action.params[key]);
|
|
|
|
}
|
|
|
|
return action;
|
|
|
|
} catch (ex) {
|
|
|
|
Cu.reportError(`Invalid action url "${url}"`);
|
|
|
|
return null;
|
|
|
|
}
|
2016-08-03 01:00:26 +00:00
|
|
|
},
|
|
|
|
|
2018-01-22 19:21:55 +00:00
|
|
|
/**
|
|
|
|
* Parses matchBuckets strings (for example, "suggestion:4,general:Infinity")
|
|
|
|
* like those used in the browser.urlbar.matchBuckets preference.
|
|
|
|
*
|
|
|
|
* @param str
|
|
|
|
* A matchBuckets string.
|
|
|
|
* @returns An array of the form: [
|
|
|
|
* [bucketName_0, bucketPriority_0],
|
|
|
|
* [bucketName_1, bucketPriority_1],
|
|
|
|
* ...
|
|
|
|
* [bucketName_n, bucketPriority_n]
|
|
|
|
* ]
|
|
|
|
*/
|
|
|
|
convertMatchBucketsStringToArray(str) {
|
|
|
|
return str.split(",")
|
|
|
|
.map(v => {
|
|
|
|
let bucket = v.split(":");
|
|
|
|
return [ bucket[0].trim().toLowerCase(), Number(bucket[1]) ];
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
2017-12-07 14:15:39 +00:00
|
|
|
/**
|
|
|
|
* Determines if a folder is generated from a query.
|
|
|
|
* @param aNode a result true.
|
|
|
|
* @returns true if the node is a folder generated from a query.
|
|
|
|
*/
|
|
|
|
isQueryGeneratedFolder(node) {
|
|
|
|
if (!node.parent) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return this.nodeIsFolder(node) && this.nodeIsQuery(node.parent);
|
|
|
|
},
|
|
|
|
|
2010-04-26 19:04:17 +00:00
|
|
|
/**
|
|
|
|
* Determines whether or not a ResultNode is a Bookmark folder.
|
|
|
|
* @param aNode
|
|
|
|
* A result node
|
|
|
|
* @returns true if the node is a Bookmark folder, false otherwise
|
|
|
|
*/
|
|
|
|
nodeIsFolder: function PU_nodeIsFolder(aNode) {
|
|
|
|
return (aNode.type == Ci.nsINavHistoryResultNode.RESULT_TYPE_FOLDER ||
|
|
|
|
aNode.type == Ci.nsINavHistoryResultNode.RESULT_TYPE_FOLDER_SHORTCUT);
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Determines whether or not a ResultNode represents a bookmarked URI.
|
|
|
|
* @param aNode
|
|
|
|
* A result node
|
|
|
|
* @returns true if the node represents a bookmarked URI, false otherwise
|
|
|
|
*/
|
|
|
|
nodeIsBookmark: function PU_nodeIsBookmark(aNode) {
|
|
|
|
return aNode.type == Ci.nsINavHistoryResultNode.RESULT_TYPE_URI &&
|
|
|
|
aNode.itemId != -1;
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Determines whether or not a ResultNode is a Bookmark separator.
|
|
|
|
* @param aNode
|
|
|
|
* A result node
|
|
|
|
* @returns true if the node is a Bookmark separator, false otherwise
|
|
|
|
*/
|
|
|
|
nodeIsSeparator: function PU_nodeIsSeparator(aNode) {
|
2013-03-25 13:44:41 +00:00
|
|
|
return aNode.type == Ci.nsINavHistoryResultNode.RESULT_TYPE_SEPARATOR;
|
2010-04-26 19:04:17 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Determines whether or not a ResultNode is a URL item.
|
|
|
|
* @param aNode
|
|
|
|
* A result node
|
|
|
|
* @returns true if the node is a URL item, false otherwise
|
|
|
|
*/
|
|
|
|
nodeIsURI: function PU_nodeIsURI(aNode) {
|
2013-03-25 13:44:41 +00:00
|
|
|
return aNode.type == Ci.nsINavHistoryResultNode.RESULT_TYPE_URI;
|
2010-04-26 19:04:17 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Determines whether or not a ResultNode is a Query item.
|
|
|
|
* @param aNode
|
|
|
|
* A result node
|
|
|
|
* @returns true if the node is a Query item, false otherwise
|
|
|
|
*/
|
|
|
|
nodeIsQuery: function PU_nodeIsQuery(aNode) {
|
|
|
|
return aNode.type == Ci.nsINavHistoryResultNode.RESULT_TYPE_QUERY;
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Generator for a node's ancestors.
|
|
|
|
* @param aNode
|
|
|
|
* A result node
|
|
|
|
*/
|
2014-12-16 23:28:39 +00:00
|
|
|
nodeAncestors: function* PU_nodeAncestors(aNode) {
|
2010-04-26 19:04:17 +00:00
|
|
|
let node = aNode.parent;
|
|
|
|
while (node) {
|
|
|
|
yield node;
|
|
|
|
node = node.parent;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2016-08-10 19:54:45 +00:00
|
|
|
/**
|
|
|
|
* Checks validity of an object, filling up default values for optional
|
|
|
|
* properties.
|
|
|
|
*
|
2017-07-13 17:09:49 +00:00
|
|
|
* @param {string} name
|
|
|
|
* The operation name. This is included in the error message if
|
|
|
|
* validation fails.
|
2016-08-10 19:54:45 +00:00
|
|
|
* @param validators (object)
|
|
|
|
* An object containing input validators. Keys should be field names;
|
|
|
|
* values should be validation functions.
|
|
|
|
* @param props (object)
|
|
|
|
* The object to validate.
|
|
|
|
* @param behavior (object) [optional]
|
|
|
|
* Object defining special behavior for some of the properties.
|
|
|
|
* The following behaviors may be optionally set:
|
2017-03-13 23:45:43 +00:00
|
|
|
* - required: this property is required.
|
|
|
|
* - replaceWith: this property will be overwritten with the value
|
|
|
|
* provided
|
2016-08-10 19:54:45 +00:00
|
|
|
* - requiredIf: if the provided condition is satisfied, then this
|
|
|
|
* property is required.
|
|
|
|
* - validIf: if the provided condition is not satisfied, then this
|
|
|
|
* property is invalid.
|
|
|
|
* - defaultValue: an undefined property should default to this value.
|
2017-10-07 09:15:47 +00:00
|
|
|
* - fixup: a function invoked when validation fails, takes the input
|
|
|
|
* object as argument and must fix the property.
|
2016-08-10 19:54:45 +00:00
|
|
|
*
|
|
|
|
* @return a validated and normalized item.
|
|
|
|
* @throws if the object contains invalid data.
|
|
|
|
* @note any unknown properties are pass-through.
|
|
|
|
*/
|
2017-07-13 17:09:49 +00:00
|
|
|
validateItemProperties(name, validators, props, behavior = {}) {
|
2016-08-10 19:54:45 +00:00
|
|
|
if (!props)
|
2017-07-13 17:09:49 +00:00
|
|
|
throw new Error(`${name}: Input should be a valid object`);
|
2016-08-10 19:54:45 +00:00
|
|
|
// Make a shallow copy of `props` to avoid mutating the original object
|
|
|
|
// when filling in defaults.
|
|
|
|
let input = Object.assign({}, props);
|
|
|
|
let normalizedInput = {};
|
|
|
|
let required = new Set();
|
|
|
|
for (let prop in behavior) {
|
|
|
|
if (behavior[prop].hasOwnProperty("required") && behavior[prop].required) {
|
|
|
|
required.add(prop);
|
|
|
|
}
|
|
|
|
if (behavior[prop].hasOwnProperty("requiredIf") && behavior[prop].requiredIf(input)) {
|
|
|
|
required.add(prop);
|
|
|
|
}
|
|
|
|
if (behavior[prop].hasOwnProperty("validIf") && input[prop] !== undefined &&
|
|
|
|
!behavior[prop].validIf(input)) {
|
2017-10-07 09:15:47 +00:00
|
|
|
if (behavior[prop].hasOwnProperty("fixup")) {
|
|
|
|
behavior[prop].fixup(input);
|
|
|
|
} else {
|
|
|
|
throw new Error(`${name}: Invalid value for property '${prop}': ${JSON.stringify(input[prop])}`);
|
|
|
|
}
|
2016-08-10 19:54:45 +00:00
|
|
|
}
|
|
|
|
if (behavior[prop].hasOwnProperty("defaultValue") && input[prop] === undefined) {
|
|
|
|
input[prop] = behavior[prop].defaultValue;
|
|
|
|
}
|
2017-03-13 23:45:43 +00:00
|
|
|
if (behavior[prop].hasOwnProperty("replaceWith")) {
|
|
|
|
input[prop] = behavior[prop].replaceWith;
|
|
|
|
}
|
2016-08-10 19:54:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for (let prop in input) {
|
|
|
|
if (required.has(prop)) {
|
|
|
|
required.delete(prop);
|
|
|
|
} else if (input[prop] === undefined) {
|
|
|
|
// Skip undefined properties that are not required.
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (validators.hasOwnProperty(prop)) {
|
|
|
|
try {
|
|
|
|
normalizedInput[prop] = validators[prop](input[prop], input);
|
|
|
|
} catch (ex) {
|
2017-10-07 09:15:47 +00:00
|
|
|
if (behavior.hasOwnProperty(prop) && behavior[prop].hasOwnProperty("fixup")) {
|
|
|
|
behavior[prop].fixup(input);
|
|
|
|
normalizedInput[prop] = input[prop];
|
|
|
|
} else {
|
|
|
|
throw new Error(`${name}: Invalid value for property '${prop}': ${JSON.stringify(input[prop])}`);
|
|
|
|
}
|
2016-08-10 19:54:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (required.size > 0)
|
2017-07-13 17:09:49 +00:00
|
|
|
throw new Error(`${name}: The following properties were expected: ${[...required].join(", ")}`);
|
2016-08-10 19:54:45 +00:00
|
|
|
return normalizedInput;
|
|
|
|
},
|
|
|
|
|
|
|
|
BOOKMARK_VALIDATORS,
|
|
|
|
SYNC_BOOKMARK_VALIDATORS,
|
2016-11-18 22:15:59 +00:00
|
|
|
SYNC_CHANGE_RECORD_VALIDATORS,
|
2016-08-10 19:54:45 +00:00
|
|
|
|
2018-01-22 15:41:12 +00:00
|
|
|
QueryInterface: XPCOMUtils.generateQI([Ci.nsIObserver]),
|
2010-04-26 19:04:17 +00:00
|
|
|
|
2010-11-22 19:34:57 +00:00
|
|
|
_shutdownFunctions: [],
|
2016-12-31 02:47:25 +00:00
|
|
|
registerShutdownFunction: function PU_registerShutdownFunction(aFunc) {
|
2010-11-22 19:34:57 +00:00
|
|
|
// If this is the first registered function, add the shutdown observer.
|
|
|
|
if (this._shutdownFunctions.length == 0) {
|
2017-04-14 19:51:38 +00:00
|
|
|
Services.obs.addObserver(this, this.TOPIC_SHUTDOWN);
|
2010-11-22 19:34:57 +00:00
|
|
|
}
|
|
|
|
this._shutdownFunctions.push(aFunc);
|
|
|
|
},
|
2010-06-30 21:24:03 +00:00
|
|
|
|
2016-11-03 18:42:06 +00:00
|
|
|
// nsIObserver
|
2016-12-31 02:47:25 +00:00
|
|
|
observe: function PU_observe(aSubject, aTopic, aData) {
|
2011-10-27 09:11:45 +00:00
|
|
|
switch (aTopic) {
|
|
|
|
case this.TOPIC_SHUTDOWN:
|
|
|
|
Services.obs.removeObserver(this, this.TOPIC_SHUTDOWN);
|
2012-02-28 07:49:00 +00:00
|
|
|
while (this._shutdownFunctions.length > 0) {
|
|
|
|
this._shutdownFunctions.shift().apply(this);
|
|
|
|
}
|
2011-10-27 09:11:45 +00:00
|
|
|
break;
|
2010-04-26 19:04:17 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2016-12-29 23:34:54 +00:00
|
|
|
onPageAnnotationSet() {},
|
|
|
|
onPageAnnotationRemoved() {},
|
2010-06-30 21:24:03 +00:00
|
|
|
|
2010-04-26 19:04:17 +00:00
|
|
|
/**
|
|
|
|
* Determines whether or not a ResultNode is a host container.
|
|
|
|
* @param aNode
|
|
|
|
* A result node
|
|
|
|
* @returns true if the node is a host container, false otherwise
|
|
|
|
*/
|
|
|
|
nodeIsHost: function PU_nodeIsHost(aNode) {
|
|
|
|
return aNode.type == Ci.nsINavHistoryResultNode.RESULT_TYPE_QUERY &&
|
|
|
|
aNode.parent &&
|
|
|
|
asQuery(aNode.parent).queryOptions.resultType ==
|
|
|
|
Ci.nsINavHistoryQueryOptions.RESULTS_AS_SITE_QUERY;
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Determines whether or not a ResultNode is a day container.
|
|
|
|
* @param node
|
|
|
|
* A NavHistoryResultNode
|
|
|
|
* @returns true if the node is a day container, false otherwise
|
|
|
|
*/
|
|
|
|
nodeIsDay: function PU_nodeIsDay(aNode) {
|
|
|
|
var resultType;
|
|
|
|
return aNode.type == Ci.nsINavHistoryResultNode.RESULT_TYPE_QUERY &&
|
|
|
|
aNode.parent &&
|
|
|
|
((resultType = asQuery(aNode.parent).queryOptions.resultType) ==
|
|
|
|
Ci.nsINavHistoryQueryOptions.RESULTS_AS_DATE_QUERY ||
|
|
|
|
resultType == Ci.nsINavHistoryQueryOptions.RESULTS_AS_DATE_SITE_QUERY);
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Determines whether or not a result-node is a tag container.
|
|
|
|
* @param aNode
|
|
|
|
* A result-node
|
|
|
|
* @returns true if the node is a tag container, false otherwise
|
|
|
|
*/
|
|
|
|
nodeIsTagQuery: function PU_nodeIsTagQuery(aNode) {
|
|
|
|
return aNode.type == Ci.nsINavHistoryResultNode.RESULT_TYPE_QUERY &&
|
|
|
|
asQuery(aNode).queryOptions.resultType ==
|
|
|
|
Ci.nsINavHistoryQueryOptions.RESULTS_AS_TAG_CONTENTS;
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Determines whether or not a ResultNode is a container.
|
|
|
|
* @param aNode
|
|
|
|
* A result node
|
|
|
|
* @returns true if the node is a container item, false otherwise
|
|
|
|
*/
|
|
|
|
containerTypes: [Ci.nsINavHistoryResultNode.RESULT_TYPE_FOLDER,
|
|
|
|
Ci.nsINavHistoryResultNode.RESULT_TYPE_FOLDER_SHORTCUT,
|
2011-11-29 01:22:51 +00:00
|
|
|
Ci.nsINavHistoryResultNode.RESULT_TYPE_QUERY],
|
2010-04-26 19:04:17 +00:00
|
|
|
nodeIsContainer: function PU_nodeIsContainer(aNode) {
|
2015-10-11 18:24:32 +00:00
|
|
|
return this.containerTypes.includes(aNode.type);
|
2010-04-26 19:04:17 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Determines whether or not a ResultNode is an history related container.
|
|
|
|
* @param node
|
|
|
|
* A result node
|
|
|
|
* @returns true if the node is an history related container, false otherwise
|
|
|
|
*/
|
|
|
|
nodeIsHistoryContainer: function PU_nodeIsHistoryContainer(aNode) {
|
|
|
|
var resultType;
|
|
|
|
return this.nodeIsQuery(aNode) &&
|
|
|
|
((resultType = asQuery(aNode).queryOptions.resultType) ==
|
|
|
|
Ci.nsINavHistoryQueryOptions.RESULTS_AS_DATE_SITE_QUERY ||
|
|
|
|
resultType == Ci.nsINavHistoryQueryOptions.RESULTS_AS_DATE_QUERY ||
|
|
|
|
resultType == Ci.nsINavHistoryQueryOptions.RESULTS_AS_SITE_QUERY ||
|
|
|
|
this.nodeIsDay(aNode) ||
|
|
|
|
this.nodeIsHost(aNode));
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets the concrete item-id for the given node. Generally, this is just
|
|
|
|
* node.itemId, but for folder-shortcuts that's node.folderItemId.
|
|
|
|
*/
|
|
|
|
getConcreteItemId: function PU_getConcreteItemId(aNode) {
|
|
|
|
if (aNode.type == Ci.nsINavHistoryResultNode.RESULT_TYPE_FOLDER_SHORTCUT)
|
|
|
|
return asQuery(aNode).folderItemId;
|
|
|
|
else if (PlacesUtils.nodeIsTagQuery(aNode)) {
|
|
|
|
// RESULTS_AS_TAG_CONTENTS queries are similar to folder shortcuts
|
|
|
|
// so we can still get the concrete itemId for them.
|
|
|
|
var queries = aNode.getQueries();
|
|
|
|
var folders = queries[0].getFolders();
|
|
|
|
return folders[0];
|
|
|
|
}
|
|
|
|
return aNode.itemId;
|
|
|
|
},
|
|
|
|
|
2015-04-23 13:34:55 +00:00
|
|
|
/**
|
|
|
|
* Gets the concrete item-guid for the given node. For everything but folder
|
|
|
|
* shortcuts, this is just node.bookmarkGuid. For folder shortcuts, this is
|
|
|
|
* node.targetFolderGuid (see nsINavHistoryService.idl for the semantics).
|
|
|
|
*
|
|
|
|
* @param aNode
|
|
|
|
* a result node.
|
|
|
|
* @return the concrete item-guid for aNode.
|
|
|
|
* @note unlike getConcreteItemId, this doesn't allow retrieving the guid of a
|
|
|
|
* ta container.
|
|
|
|
*/
|
|
|
|
getConcreteItemGuid(aNode) {
|
|
|
|
if (aNode.type == Ci.nsINavHistoryResultNode.RESULT_TYPE_FOLDER_SHORTCUT)
|
|
|
|
return asQuery(aNode).targetFolderGuid;
|
|
|
|
return aNode.bookmarkGuid;
|
|
|
|
},
|
|
|
|
|
2014-10-30 21:36:00 +00:00
|
|
|
/**
|
|
|
|
* Reverse a host based on the moz_places algorithm, that is reverse the host
|
|
|
|
* string and add a trailing period. For example "google.com" becomes
|
|
|
|
* "moc.elgoog.".
|
|
|
|
*
|
|
|
|
* @param url
|
|
|
|
* the URL to generate a rev host for.
|
|
|
|
* @return the reversed host string.
|
|
|
|
*/
|
2015-05-22 19:23:57 +00:00
|
|
|
getReversedHost(url) {
|
|
|
|
return url.host.split("").reverse().join("") + ".";
|
|
|
|
},
|
2014-10-30 21:36:00 +00:00
|
|
|
|
2010-04-26 19:04:17 +00:00
|
|
|
/**
|
|
|
|
* String-wraps a result node according to the rules of the specified
|
2015-05-22 17:02:41 +00:00
|
|
|
* content type for copy or move operations.
|
|
|
|
*
|
2010-04-26 19:04:17 +00:00
|
|
|
* @param aNode
|
|
|
|
* The Result node to wrap (serialize)
|
|
|
|
* @param aType
|
|
|
|
* The content type to serialize as
|
2015-05-22 17:02:41 +00:00
|
|
|
* @param [optional] aFeedURI
|
2010-04-26 19:04:17 +00:00
|
|
|
* Used instead of the node's URI if provided.
|
2015-05-22 17:02:41 +00:00
|
|
|
* This is useful for wrapping a livemark as TYPE_X_MOZ_URL,
|
2010-04-26 19:04:17 +00:00
|
|
|
* TYPE_HTML or TYPE_UNICODE.
|
2014-03-04 20:44:21 +00:00
|
|
|
* @return A string serialization of the node
|
2010-04-26 19:04:17 +00:00
|
|
|
*/
|
2015-05-22 17:02:41 +00:00
|
|
|
wrapNode(aNode, aType, aFeedURI) {
|
2010-04-26 19:04:17 +00:00
|
|
|
// when wrapping a node, we want all the items, even if the original
|
|
|
|
// query options are excluding them.
|
2015-05-22 17:02:41 +00:00
|
|
|
// This can happen when copying from the left hand pane of the bookmarks
|
|
|
|
// organizer.
|
2010-04-26 19:04:17 +00:00
|
|
|
// @return [node, shouldClose]
|
2015-05-22 17:02:41 +00:00
|
|
|
function gatherDataFromNode(node, gatherDataFunc) {
|
|
|
|
if (PlacesUtils.nodeIsFolder(node) &&
|
|
|
|
node.type != Ci.nsINavHistoryResultNode.RESULT_TYPE_FOLDER_SHORTCUT &&
|
|
|
|
asQuery(node).queryOptions.excludeItems) {
|
2015-10-14 16:49:19 +00:00
|
|
|
let folderRoot = PlacesUtils.getFolderContents(node.itemId, false, true).root;
|
2015-05-22 17:02:41 +00:00
|
|
|
try {
|
2015-10-14 16:49:19 +00:00
|
|
|
return gatherDataFunc(folderRoot);
|
2015-05-22 17:02:41 +00:00
|
|
|
} finally {
|
2015-10-14 16:49:19 +00:00
|
|
|
folderRoot.containerOpen = false;
|
2015-05-22 17:02:41 +00:00
|
|
|
}
|
2010-04-26 19:04:17 +00:00
|
|
|
}
|
2015-05-22 17:02:41 +00:00
|
|
|
// If we didn't create our own query, do not alter the node's state.
|
|
|
|
return gatherDataFunc(node);
|
2010-04-26 19:04:17 +00:00
|
|
|
}
|
|
|
|
|
2015-05-22 17:02:41 +00:00
|
|
|
function gatherDataHtml(node) {
|
|
|
|
let htmlEscape = s => s.replace(/&/g, "&")
|
|
|
|
.replace(/>/g, ">")
|
|
|
|
.replace(/</g, "<")
|
|
|
|
.replace(/"/g, """)
|
|
|
|
.replace(/'/g, "'");
|
2012-02-24 12:42:24 +00:00
|
|
|
|
2015-05-22 17:02:41 +00:00
|
|
|
// escape out potential HTML in the title
|
|
|
|
let escapedTitle = node.title ? htmlEscape(node.title) : "";
|
2012-02-24 12:42:24 +00:00
|
|
|
|
2015-05-22 17:02:41 +00:00
|
|
|
if (aFeedURI) {
|
|
|
|
return `<A HREF="${aFeedURI}">${escapedTitle}</A>${NEWLINE}`;
|
2010-04-26 19:04:17 +00:00
|
|
|
}
|
|
|
|
|
2015-05-22 17:02:41 +00:00
|
|
|
if (PlacesUtils.nodeIsContainer(node)) {
|
|
|
|
asContainer(node);
|
|
|
|
let wasOpen = node.containerOpen;
|
|
|
|
if (!wasOpen)
|
|
|
|
node.containerOpen = true;
|
2012-02-24 12:42:24 +00:00
|
|
|
|
2015-05-22 17:02:41 +00:00
|
|
|
let childString = "<DL><DT>" + escapedTitle + "</DT>" + NEWLINE;
|
|
|
|
let cc = node.childCount;
|
|
|
|
for (let i = 0; i < cc; ++i) {
|
|
|
|
childString += "<DD>"
|
|
|
|
+ NEWLINE
|
|
|
|
+ gatherDataHtml(node.getChild(i))
|
|
|
|
+ "</DD>"
|
|
|
|
+ NEWLINE;
|
2010-04-26 19:04:17 +00:00
|
|
|
}
|
2015-05-22 17:02:41 +00:00
|
|
|
node.containerOpen = wasOpen;
|
|
|
|
return childString + "</DL>" + NEWLINE;
|
2010-04-26 19:04:17 +00:00
|
|
|
}
|
2015-05-22 17:02:41 +00:00
|
|
|
if (PlacesUtils.nodeIsURI(node))
|
|
|
|
return `<A HREF="${node.uri}">${escapedTitle}</A>${NEWLINE}`;
|
|
|
|
if (PlacesUtils.nodeIsSeparator(node))
|
|
|
|
return "<HR>" + NEWLINE;
|
|
|
|
return "";
|
2010-04-26 19:04:17 +00:00
|
|
|
}
|
|
|
|
|
2015-05-22 17:02:41 +00:00
|
|
|
function gatherDataText(node) {
|
|
|
|
if (aFeedURI) {
|
|
|
|
return aFeedURI;
|
2012-02-24 12:42:24 +00:00
|
|
|
}
|
|
|
|
|
2015-05-22 17:02:41 +00:00
|
|
|
if (PlacesUtils.nodeIsContainer(node)) {
|
|
|
|
asContainer(node);
|
|
|
|
let wasOpen = node.containerOpen;
|
2010-04-26 19:04:17 +00:00
|
|
|
if (!wasOpen)
|
2015-05-22 17:02:41 +00:00
|
|
|
node.containerOpen = true;
|
2010-04-26 19:04:17 +00:00
|
|
|
|
2015-05-22 17:02:41 +00:00
|
|
|
let childString = node.title + NEWLINE;
|
|
|
|
let cc = node.childCount;
|
2010-04-26 19:04:17 +00:00
|
|
|
for (let i = 0; i < cc; ++i) {
|
2015-05-22 17:02:41 +00:00
|
|
|
let child = node.getChild(i);
|
2010-04-26 19:04:17 +00:00
|
|
|
let suffix = i < (cc - 1) ? NEWLINE : "";
|
|
|
|
childString += gatherDataText(child) + suffix;
|
|
|
|
}
|
2015-05-22 17:02:41 +00:00
|
|
|
node.containerOpen = wasOpen;
|
2010-04-26 19:04:17 +00:00
|
|
|
return childString;
|
|
|
|
}
|
2015-05-22 17:02:41 +00:00
|
|
|
if (PlacesUtils.nodeIsURI(node))
|
|
|
|
return node.uri;
|
|
|
|
if (PlacesUtils.nodeIsSeparator(node))
|
2010-04-26 19:04:17 +00:00
|
|
|
return "--------------------";
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
|
2015-05-22 17:02:41 +00:00
|
|
|
switch (aType) {
|
|
|
|
case this.TYPE_X_MOZ_PLACE:
|
|
|
|
case this.TYPE_X_MOZ_PLACE_SEPARATOR:
|
|
|
|
case this.TYPE_X_MOZ_PLACE_CONTAINER: {
|
|
|
|
// Serialize the node to JSON.
|
|
|
|
return serializeNode(aNode, aFeedURI);
|
|
|
|
}
|
|
|
|
case this.TYPE_X_MOZ_URL: {
|
|
|
|
if (aFeedURI || PlacesUtils.nodeIsURI(aNode))
|
|
|
|
return (aFeedURI || aNode.uri) + NEWLINE + aNode.title;
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
case this.TYPE_HTML: {
|
|
|
|
return gatherDataFromNode(aNode, gatherDataHtml);
|
|
|
|
}
|
|
|
|
}
|
2010-04-26 19:04:17 +00:00
|
|
|
|
2015-05-22 17:02:41 +00:00
|
|
|
// Otherwise, we wrap as TYPE_UNICODE.
|
|
|
|
return gatherDataFromNode(aNode, gatherDataText);
|
2010-04-26 19:04:17 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Unwraps data from the Clipboard or the current Drag Session.
|
|
|
|
* @param blob
|
|
|
|
* A blob (string) of data, in some format we potentially know how
|
|
|
|
* to parse.
|
|
|
|
* @param type
|
|
|
|
* The content type of the blob.
|
|
|
|
* @returns An array of objects representing each item contained by the source.
|
|
|
|
*/
|
|
|
|
unwrapNodes: function PU_unwrapNodes(blob, type) {
|
|
|
|
// We split on "\n" because the transferable system converts "\r\n" to "\n"
|
|
|
|
var nodes = [];
|
2016-08-04 07:28:58 +00:00
|
|
|
switch (type) {
|
2010-04-26 19:04:17 +00:00
|
|
|
case this.TYPE_X_MOZ_PLACE:
|
|
|
|
case this.TYPE_X_MOZ_PLACE_SEPARATOR:
|
|
|
|
case this.TYPE_X_MOZ_PLACE_CONTAINER:
|
2013-05-23 22:28:31 +00:00
|
|
|
nodes = JSON.parse("[" + blob + "]");
|
2010-04-26 19:04:17 +00:00
|
|
|
break;
|
2016-02-03 21:12:18 +00:00
|
|
|
case this.TYPE_X_MOZ_URL: {
|
|
|
|
let parts = blob.split("\n");
|
2010-04-26 19:04:17 +00:00
|
|
|
// data in this type has 2 parts per entry, so if there are fewer
|
|
|
|
// than 2 parts left, the blob is malformed and we should stop
|
|
|
|
// but drag and drop of files from the shell has parts.length = 1
|
|
|
|
if (parts.length != 1 && parts.length % 2)
|
|
|
|
break;
|
2016-11-10 22:48:04 +00:00
|
|
|
for (let i = 0; i < parts.length; i = i + 2) {
|
2016-02-03 21:12:18 +00:00
|
|
|
let uriString = parts[i];
|
|
|
|
let titleString = "";
|
2016-11-10 22:48:04 +00:00
|
|
|
if (parts.length > i + 1)
|
|
|
|
titleString = parts[i + 1];
|
2010-04-26 19:04:17 +00:00
|
|
|
else {
|
|
|
|
// for drag and drop of files, try to use the leafName as title
|
|
|
|
try {
|
2018-02-06 09:49:14 +00:00
|
|
|
titleString = Services.io.newURI(uriString).QueryInterface(Ci.nsIURL)
|
2010-04-26 19:04:17 +00:00
|
|
|
.fileName;
|
2016-12-31 02:47:25 +00:00
|
|
|
} catch (e) {}
|
2010-04-26 19:04:17 +00:00
|
|
|
}
|
2018-02-06 09:49:14 +00:00
|
|
|
// note: Services.io.newURI() will throw if uriString is not a valid URI
|
|
|
|
if (Services.io.newURI(uriString)) {
|
2010-04-26 19:04:17 +00:00
|
|
|
nodes.push({ uri: uriString,
|
2016-08-16 19:44:15 +00:00
|
|
|
title: titleString ? titleString : uriString,
|
2010-04-26 19:04:17 +00:00
|
|
|
type: this.TYPE_X_MOZ_URL });
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
2016-02-03 21:12:18 +00:00
|
|
|
}
|
|
|
|
case this.TYPE_UNICODE: {
|
|
|
|
let parts = blob.split("\n");
|
|
|
|
for (let i = 0; i < parts.length; i++) {
|
|
|
|
let uriString = parts[i];
|
2010-04-26 19:04:17 +00:00
|
|
|
// text/uri-list is converted to TYPE_UNICODE but it could contain
|
|
|
|
// comments line prepended by #, we should skip them
|
2017-01-17 15:48:17 +00:00
|
|
|
if (uriString.substr(0, 1) == "\x23")
|
2010-04-26 19:04:17 +00:00
|
|
|
continue;
|
2018-02-06 09:49:14 +00:00
|
|
|
// note: Services.io.newURI) will throw if uriString is not a valid URI
|
|
|
|
if (uriString != "" && Services.io.newURI(uriString))
|
2010-04-26 19:04:17 +00:00
|
|
|
nodes.push({ uri: uriString,
|
|
|
|
title: uriString,
|
|
|
|
type: this.TYPE_X_MOZ_URL });
|
|
|
|
}
|
|
|
|
break;
|
2016-02-03 21:12:18 +00:00
|
|
|
}
|
2010-04-26 19:04:17 +00:00
|
|
|
default:
|
|
|
|
throw Cr.NS_ERROR_INVALID_ARG;
|
|
|
|
}
|
|
|
|
return nodes;
|
|
|
|
},
|
|
|
|
|
2017-05-11 15:22:27 +00:00
|
|
|
/**
|
|
|
|
* Validate an input PageInfo object, returning a valid PageInfo object.
|
|
|
|
*
|
|
|
|
* @param pageInfo: (PageInfo)
|
|
|
|
* @return (PageInfo)
|
|
|
|
*/
|
|
|
|
validatePageInfo(pageInfo, validateVisits = true) {
|
|
|
|
let info = {
|
|
|
|
visits: [],
|
|
|
|
};
|
|
|
|
|
2017-06-23 18:30:27 +00:00
|
|
|
if (typeof pageInfo != "object" || !pageInfo) {
|
|
|
|
throw new TypeError("pageInfo must be an object");
|
|
|
|
}
|
|
|
|
|
2017-05-11 15:22:27 +00:00
|
|
|
if (!pageInfo.url) {
|
|
|
|
throw new TypeError("PageInfo object must have a url property");
|
|
|
|
}
|
|
|
|
|
|
|
|
info.url = this.normalizeToURLOrGUID(pageInfo.url);
|
|
|
|
|
2017-06-23 18:30:27 +00:00
|
|
|
if (typeof pageInfo.guid === "string" && this.isValidGuid(pageInfo.guid)) {
|
|
|
|
info.guid = pageInfo.guid;
|
|
|
|
} else if (pageInfo.guid) {
|
|
|
|
throw new TypeError(`guid property of PageInfo object: ${pageInfo.guid} is invalid`);
|
2017-05-11 15:22:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (typeof pageInfo.title === "string") {
|
|
|
|
info.title = pageInfo.title;
|
|
|
|
} else if (pageInfo.title != null && pageInfo.title != undefined) {
|
|
|
|
throw new TypeError(`title property of PageInfo object: ${pageInfo.title} must be a string if provided`);
|
|
|
|
}
|
|
|
|
|
2017-06-23 18:30:27 +00:00
|
|
|
if (typeof pageInfo.description === "string" || pageInfo.description === null) {
|
|
|
|
info.description = pageInfo.description ? pageInfo.description.slice(0, DB_DESCRIPTION_LENGTH_MAX) : null;
|
|
|
|
} else if (pageInfo.description !== undefined) {
|
|
|
|
throw new TypeError(`description property of pageInfo object: ${pageInfo.description} must be either a string or null if provided`);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (pageInfo.previewImageURL || pageInfo.previewImageURL === null) {
|
|
|
|
let previewImageURL = pageInfo.previewImageURL;
|
|
|
|
|
|
|
|
if (previewImageURL === null) {
|
|
|
|
info.previewImageURL = null;
|
|
|
|
} else if (typeof(previewImageURL) === "string" && previewImageURL.length <= DB_URL_LENGTH_MAX) {
|
|
|
|
info.previewImageURL = new URL(previewImageURL);
|
|
|
|
} else if (previewImageURL instanceof Ci.nsIURI && previewImageURL.spec.length <= DB_URL_LENGTH_MAX) {
|
|
|
|
info.previewImageURL = new URL(previewImageURL.spec);
|
|
|
|
} else if (previewImageURL instanceof URL && previewImageURL.href.length <= DB_URL_LENGTH_MAX) {
|
|
|
|
info.previewImageURL = previewImageURL;
|
|
|
|
} else {
|
|
|
|
throw new TypeError("previewImageURL property of pageInfo object: ${previewImageURL} is invalid");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!validateVisits) {
|
|
|
|
return info;
|
|
|
|
}
|
|
|
|
|
2017-05-11 15:22:27 +00:00
|
|
|
if (!pageInfo.visits || !Array.isArray(pageInfo.visits) || !pageInfo.visits.length) {
|
|
|
|
throw new TypeError("PageInfo object must have an array of visits");
|
|
|
|
}
|
|
|
|
|
|
|
|
for (let inVisit of pageInfo.visits) {
|
|
|
|
let visit = {
|
|
|
|
date: new Date(),
|
|
|
|
transition: inVisit.transition || History.TRANSITIONS.LINK,
|
|
|
|
};
|
|
|
|
|
|
|
|
if (!PlacesUtils.history.isValidTransition(visit.transition)) {
|
|
|
|
throw new TypeError(`transition: ${visit.transition} is not a valid transition type`);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (inVisit.date) {
|
|
|
|
PlacesUtils.history.ensureDate(inVisit.date);
|
|
|
|
if (inVisit.date > (Date.now() + TIMERS_RESOLUTION_SKEW_MS)) {
|
|
|
|
throw new TypeError(`date: ${inVisit.date} cannot be a future date`);
|
|
|
|
}
|
|
|
|
visit.date = inVisit.date;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (inVisit.referrer) {
|
|
|
|
visit.referrer = this.normalizeToURLOrGUID(inVisit.referrer);
|
|
|
|
}
|
|
|
|
info.visits.push(visit);
|
|
|
|
}
|
|
|
|
return info;
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Normalize a key to either a string (if it is a valid GUID) or an
|
|
|
|
* instance of `URL` (if it is a `URL`, `nsIURI`, or a string
|
|
|
|
* representing a valid url).
|
|
|
|
*
|
|
|
|
* @throws (TypeError)
|
|
|
|
* If the key is neither a valid guid nor a valid url.
|
|
|
|
*/
|
|
|
|
normalizeToURLOrGUID(key) {
|
|
|
|
if (typeof key === "string") {
|
|
|
|
// A string may be a URL or a guid
|
|
|
|
if (this.isValidGuid(key)) {
|
|
|
|
return key;
|
|
|
|
}
|
|
|
|
return new URL(key);
|
|
|
|
}
|
|
|
|
if (key instanceof URL) {
|
|
|
|
return key;
|
|
|
|
}
|
|
|
|
if (key instanceof Ci.nsIURI) {
|
|
|
|
return new URL(key.spec);
|
|
|
|
}
|
|
|
|
throw new TypeError("Invalid url or guid: " + key);
|
|
|
|
},
|
|
|
|
|
2010-04-26 19:04:17 +00:00
|
|
|
/**
|
|
|
|
* Generates a nsINavHistoryResult for the contents of a folder.
|
|
|
|
* @param folderId
|
|
|
|
* The folder to open
|
|
|
|
* @param [optional] excludeItems
|
|
|
|
* True to hide all items (individual bookmarks). This is used on
|
|
|
|
* the left places pane so you just get a folder hierarchy.
|
|
|
|
* @param [optional] expandQueries
|
|
|
|
* True to make query items expand as new containers. For managing,
|
|
|
|
* you want this to be false, for menus and such, you want this to
|
|
|
|
* be true.
|
|
|
|
* @returns A nsINavHistoryResult containing the contents of the
|
|
|
|
* folder. The result.root is guaranteed to be open.
|
|
|
|
*/
|
|
|
|
getFolderContents:
|
|
|
|
function PU_getFolderContents(aFolderId, aExcludeItems, aExpandQueries) {
|
2017-06-29 18:28:11 +00:00
|
|
|
if (typeof aFolderId !== "number") {
|
|
|
|
throw new Error("aFolderId should be a number.");
|
|
|
|
}
|
2010-04-26 19:04:17 +00:00
|
|
|
var query = this.history.getNewQuery();
|
|
|
|
query.setFolders([aFolderId], 1);
|
|
|
|
var options = this.history.getNewQueryOptions();
|
|
|
|
options.excludeItems = aExcludeItems;
|
|
|
|
options.expandQueries = aExpandQueries;
|
|
|
|
|
|
|
|
var result = this.history.executeQuery(query, options);
|
|
|
|
result.root.containerOpen = true;
|
|
|
|
return result;
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Fetch all annotations for an item, including all properties of each
|
|
|
|
* annotation which would be required to recreate it.
|
|
|
|
* @param aItemId
|
|
|
|
* The identifier of the itme for which annotations are to be
|
|
|
|
* retrieved.
|
|
|
|
* @return Array of objects, each containing the following properties:
|
|
|
|
* name, flags, expires, mimeType, type, value
|
|
|
|
*/
|
|
|
|
getAnnotationsForItem: function PU_getAnnotationsForItem(aItemId) {
|
|
|
|
var annosvc = this.annotations;
|
|
|
|
var annos = [], val = null;
|
|
|
|
var annoNames = annosvc.getItemAnnotationNames(aItemId);
|
|
|
|
for (var i = 0; i < annoNames.length; i++) {
|
2013-08-03 11:06:47 +00:00
|
|
|
var flags = {}, exp = {}, storageType = {};
|
|
|
|
annosvc.getItemAnnotationInfo(aItemId, annoNames[i], flags, exp, storageType);
|
|
|
|
val = annosvc.getItemAnnotation(aItemId, annoNames[i]);
|
2010-04-26 19:04:17 +00:00
|
|
|
annos.push({name: annoNames[i],
|
|
|
|
flags: flags.value,
|
|
|
|
expires: exp.value,
|
|
|
|
value: val});
|
|
|
|
}
|
|
|
|
return annos;
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Annotate an item with a batch of annotations.
|
|
|
|
* @param aItemId
|
|
|
|
* The identifier of the item for which annotations are to be set
|
|
|
|
* @param aAnnotations
|
|
|
|
* Array of objects, each containing the following properties:
|
2013-08-03 11:06:47 +00:00
|
|
|
* name, flags, expires.
|
2010-04-26 19:04:17 +00:00
|
|
|
* If the value for an annotation is not set it will be removed.
|
|
|
|
*/
|
2017-10-03 20:18:10 +00:00
|
|
|
setAnnotationsForItem: function PU_setAnnotationsForItem(aItemId, aAnnos, aSource, aDontUpdateLastModified) {
|
2010-04-26 19:04:17 +00:00
|
|
|
var annosvc = this.annotations;
|
|
|
|
|
|
|
|
aAnnos.forEach(function(anno) {
|
2013-08-03 11:06:47 +00:00
|
|
|
if (anno.value === undefined || anno.value === null) {
|
2016-08-11 20:13:22 +00:00
|
|
|
annosvc.removeItemAnnotation(aItemId, anno.name, aSource);
|
2016-12-31 02:47:25 +00:00
|
|
|
} else {
|
2013-08-03 11:06:47 +00:00
|
|
|
let flags = ("flags" in anno) ? anno.flags : 0;
|
|
|
|
let expires = ("expires" in anno) ?
|
|
|
|
anno.expires : Ci.nsIAnnotationService.EXPIRE_NEVER;
|
2010-04-26 19:04:17 +00:00
|
|
|
annosvc.setItemAnnotation(aItemId, anno.name, anno.value, flags,
|
2017-10-03 20:18:10 +00:00
|
|
|
expires, aSource, aDontUpdateLastModified);
|
2010-04-26 19:04:17 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
// Identifier getters for special folders.
|
|
|
|
// You should use these everywhere PlacesUtils is available to avoid XPCOM
|
|
|
|
// traversal just to get roots' ids.
|
|
|
|
get placesRootId() {
|
|
|
|
delete this.placesRootId;
|
|
|
|
return this.placesRootId = this.bookmarks.placesRoot;
|
|
|
|
},
|
|
|
|
|
|
|
|
get bookmarksMenuFolderId() {
|
|
|
|
delete this.bookmarksMenuFolderId;
|
|
|
|
return this.bookmarksMenuFolderId = this.bookmarks.bookmarksMenuFolder;
|
|
|
|
},
|
|
|
|
|
|
|
|
get toolbarFolderId() {
|
|
|
|
delete this.toolbarFolderId;
|
|
|
|
return this.toolbarFolderId = this.bookmarks.toolbarFolder;
|
|
|
|
},
|
|
|
|
|
|
|
|
get tagsFolderId() {
|
|
|
|
delete this.tagsFolderId;
|
|
|
|
return this.tagsFolderId = this.bookmarks.tagsFolder;
|
|
|
|
},
|
|
|
|
|
|
|
|
get unfiledBookmarksFolderId() {
|
|
|
|
delete this.unfiledBookmarksFolderId;
|
|
|
|
return this.unfiledBookmarksFolderId = this.bookmarks.unfiledBookmarksFolder;
|
|
|
|
},
|
|
|
|
|
2016-09-29 20:44:08 +00:00
|
|
|
get mobileFolderId() {
|
|
|
|
delete this.mobileFolderId;
|
|
|
|
return this.mobileFolderId = this.bookmarks.mobileFolder;
|
|
|
|
},
|
|
|
|
|
2010-04-26 19:04:17 +00:00
|
|
|
/**
|
2017-12-21 09:16:48 +00:00
|
|
|
* Checks if item is a root.
|
2010-04-26 19:04:17 +00:00
|
|
|
*
|
2017-12-21 09:16:48 +00:00
|
|
|
* @param {Number|String} guid The guid or id of the item to look for.
|
|
|
|
* @returns {Boolean} true if guid is a root, false otherwise.
|
2010-04-26 19:04:17 +00:00
|
|
|
*/
|
2017-12-21 09:16:48 +00:00
|
|
|
isRootItem(guid) {
|
|
|
|
if (typeof guid === "string") {
|
|
|
|
return guid == PlacesUtils.bookmarks.menuGuid ||
|
|
|
|
guid == PlacesUtils.bookmarks.toolbarGuid ||
|
|
|
|
guid == PlacesUtils.bookmarks.unfiledGuid ||
|
|
|
|
guid == PlacesUtils.bookmarks.tagsGuid ||
|
|
|
|
guid == PlacesUtils.bookmarks.rootGuid ||
|
|
|
|
guid == PlacesUtils.bookmarks.mobileGuid;
|
|
|
|
}
|
|
|
|
return guid == PlacesUtils.bookmarksMenuFolderId ||
|
|
|
|
guid == PlacesUtils.toolbarFolderId ||
|
|
|
|
guid == PlacesUtils.unfiledBookmarksFolderId ||
|
|
|
|
guid == PlacesUtils.tagsFolderId ||
|
|
|
|
guid == PlacesUtils.placesRootId ||
|
|
|
|
guid == PlacesUtils.mobileFolderId;
|
2010-04-26 19:04:17 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns a nsNavHistoryContainerResultNode with forced excludeItems and
|
|
|
|
* expandQueries.
|
|
|
|
* @param aNode
|
|
|
|
* The node to convert
|
|
|
|
* @param [optional] excludeItems
|
|
|
|
* True to hide all items (individual bookmarks). This is used on
|
|
|
|
* the left places pane so you just get a folder hierarchy.
|
|
|
|
* @param [optional] expandQueries
|
|
|
|
* True to make query items expand as new containers. For managing,
|
|
|
|
* you want this to be false, for menus and such, you want this to
|
|
|
|
* be true.
|
|
|
|
* @returns A nsINavHistoryContainerResultNode containing the unfiltered
|
|
|
|
* contents of the container.
|
|
|
|
* @note The returned container node could be open or closed, we don't
|
|
|
|
* guarantee its status.
|
|
|
|
*/
|
|
|
|
getContainerNodeWithOptions:
|
|
|
|
function PU_getContainerNodeWithOptions(aNode, aExcludeItems, aExpandQueries) {
|
|
|
|
if (!this.nodeIsContainer(aNode))
|
|
|
|
throw Cr.NS_ERROR_INVALID_ARG;
|
|
|
|
|
|
|
|
// excludeItems is inherited by child containers in an excludeItems view.
|
|
|
|
var excludeItems = asQuery(aNode).queryOptions.excludeItems ||
|
|
|
|
asQuery(aNode.parentResult.root).queryOptions.excludeItems;
|
|
|
|
// expandQueries is inherited by child containers in an expandQueries view.
|
|
|
|
var expandQueries = asQuery(aNode).queryOptions.expandQueries &&
|
|
|
|
asQuery(aNode.parentResult.root).queryOptions.expandQueries;
|
|
|
|
|
|
|
|
// If our options are exactly what we expect, directly return the node.
|
|
|
|
if (excludeItems == aExcludeItems && expandQueries == aExpandQueries)
|
|
|
|
return aNode;
|
|
|
|
|
|
|
|
// Otherwise, get contents manually.
|
|
|
|
var queries = {}, options = {};
|
|
|
|
this.history.queryStringToQueries(aNode.uri, queries, {}, options);
|
|
|
|
options.value.excludeItems = aExcludeItems;
|
|
|
|
options.value.expandQueries = aExpandQueries;
|
|
|
|
return this.history.executeQueries(queries.value,
|
|
|
|
queries.value.length,
|
|
|
|
options.value).root;
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns true if a container has uri nodes in its first level.
|
|
|
|
* Has better performance than (getURLsForContainerNode(node).length > 0).
|
|
|
|
* @param aNode
|
|
|
|
* The container node to search through.
|
|
|
|
* @returns true if the node contains uri nodes, false otherwise.
|
|
|
|
*/
|
2014-11-03 10:44:47 +00:00
|
|
|
hasChildURIs: function PU_hasChildURIs(aNode) {
|
2010-04-26 19:04:17 +00:00
|
|
|
if (!this.nodeIsContainer(aNode))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
let root = this.getContainerNodeWithOptions(aNode, false, true);
|
|
|
|
let result = root.parentResult;
|
|
|
|
let didSuppressNotifications = false;
|
|
|
|
let wasOpen = root.containerOpen;
|
|
|
|
if (!wasOpen) {
|
|
|
|
didSuppressNotifications = result.suppressNotifications;
|
|
|
|
if (!didSuppressNotifications)
|
|
|
|
result.suppressNotifications = true;
|
|
|
|
|
|
|
|
root.containerOpen = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
let found = false;
|
|
|
|
for (let i = 0; i < root.childCount && !found; i++) {
|
|
|
|
let child = root.getChild(i);
|
2014-11-03 10:44:47 +00:00
|
|
|
if (this.nodeIsURI(child))
|
|
|
|
found = true;
|
2010-04-26 19:04:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!wasOpen) {
|
|
|
|
root.containerOpen = false;
|
|
|
|
if (!didSuppressNotifications)
|
|
|
|
result.suppressNotifications = false;
|
|
|
|
}
|
|
|
|
return found;
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns an array containing all the uris in the first level of the
|
|
|
|
* passed in container.
|
|
|
|
* If you only need to know if the node contains uris, use hasChildURIs.
|
|
|
|
* @param aNode
|
|
|
|
* The container node to search through
|
|
|
|
* @returns array of uris in the first level of the container.
|
|
|
|
*/
|
|
|
|
getURLsForContainerNode: function PU_getURLsForContainerNode(aNode) {
|
|
|
|
let urls = [];
|
|
|
|
if (!this.nodeIsContainer(aNode))
|
|
|
|
return urls;
|
|
|
|
|
|
|
|
let root = this.getContainerNodeWithOptions(aNode, false, true);
|
|
|
|
let result = root.parentResult;
|
|
|
|
let wasOpen = root.containerOpen;
|
|
|
|
let didSuppressNotifications = false;
|
|
|
|
if (!wasOpen) {
|
|
|
|
didSuppressNotifications = result.suppressNotifications;
|
|
|
|
if (!didSuppressNotifications)
|
|
|
|
result.suppressNotifications = true;
|
|
|
|
|
|
|
|
root.containerOpen = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (let i = 0; i < root.childCount; ++i) {
|
|
|
|
let child = root.getChild(i);
|
|
|
|
if (this.nodeIsURI(child))
|
|
|
|
urls.push({uri: child.uri, isBookmark: this.nodeIsBookmark(child)});
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!wasOpen) {
|
|
|
|
root.containerOpen = false;
|
|
|
|
if (!didSuppressNotifications)
|
|
|
|
result.suppressNotifications = false;
|
|
|
|
}
|
|
|
|
return urls;
|
|
|
|
},
|
|
|
|
|
2014-05-22 16:06:57 +00:00
|
|
|
/**
|
2016-02-08 13:42:07 +00:00
|
|
|
* Gets a shared Sqlite.jsm readonly connection to the Places database,
|
|
|
|
* usable only for SELECT queries.
|
|
|
|
*
|
|
|
|
* This is intended to be used mostly internally, components outside of
|
|
|
|
* Places should, when possible, use API calls and file bugs to get proper
|
|
|
|
* APIs, where they are missing.
|
2014-05-22 16:06:57 +00:00
|
|
|
* Keep in mind the Places DB schema is by no means frozen or even stable.
|
|
|
|
* Your custom queries can - and will - break overtime.
|
2016-02-08 13:42:07 +00:00
|
|
|
*
|
|
|
|
* Example:
|
2017-05-12 15:28:48 +00:00
|
|
|
* let db = await PlacesUtils.promiseDBConnection();
|
|
|
|
* let rows = await db.executeCached(sql, params);
|
2014-05-22 16:06:57 +00:00
|
|
|
*/
|
|
|
|
promiseDBConnection: () => gAsyncDBConnPromised,
|
|
|
|
|
2015-03-20 08:39:25 +00:00
|
|
|
/**
|
2016-02-08 13:42:07 +00:00
|
|
|
* Performs a read/write operation on the Places database through a Sqlite.jsm
|
|
|
|
* wrapped connection to the Places database.
|
2015-05-05 10:44:16 +00:00
|
|
|
*
|
2016-02-08 13:42:07 +00:00
|
|
|
* This is intended to be used only by Places itself, always use APIs if you
|
|
|
|
* need to modify the Places database. Use promiseDBConnection if you need to
|
|
|
|
* SELECT from the database and there's no covering API.
|
2015-03-20 08:39:25 +00:00
|
|
|
* Keep in mind the Places DB schema is by no means frozen or even stable.
|
|
|
|
* Your custom queries can - and will - break overtime.
|
2015-05-05 10:44:16 +00:00
|
|
|
*
|
|
|
|
* As all operations on the Places database are asynchronous, if shutdown
|
|
|
|
* is initiated while an operation is pending, this could cause dataloss.
|
|
|
|
* Using `withConnectionWrapper` ensures that shutdown waits until all
|
|
|
|
* operations are complete before proceeding.
|
|
|
|
*
|
|
|
|
* Example:
|
2017-05-12 15:28:48 +00:00
|
|
|
* await withConnectionWrapper("Bookmarks: Remove a bookmark", Task.async(function*(db) {
|
2015-05-05 10:44:16 +00:00
|
|
|
* // Proceed with the db, asynchronously.
|
|
|
|
* // Shutdown will not interrupt operations that take place here.
|
|
|
|
* }));
|
|
|
|
*
|
|
|
|
* @param {string} name The name of the operation. Used for debugging, logging
|
|
|
|
* and crash reporting.
|
|
|
|
* @param {function(db)} task A function that takes as argument a Sqlite.jsm
|
|
|
|
* connection and returns a Promise. Shutdown is guaranteed to not interrupt
|
|
|
|
* execution of `task`.
|
2015-03-20 08:39:25 +00:00
|
|
|
*/
|
2017-06-09 16:51:09 +00:00
|
|
|
async withConnectionWrapper(name, task) {
|
2015-05-05 10:44:16 +00:00
|
|
|
if (!name) {
|
|
|
|
throw new TypeError("Expecting a user-readable name");
|
|
|
|
}
|
2017-06-09 16:51:09 +00:00
|
|
|
let db = await gAsyncDBWrapperPromised;
|
|
|
|
return db.executeBeforeShutdown(name, task);
|
2015-05-05 10:44:16 +00:00
|
|
|
},
|
2011-10-27 09:11:45 +00:00
|
|
|
|
2013-03-21 17:06:15 +00:00
|
|
|
/**
|
|
|
|
* Sets the character-set for a URI.
|
|
|
|
*
|
2017-05-12 07:04:51 +00:00
|
|
|
* @param {nsIURI} aURI
|
|
|
|
* @param {String} aCharset character-set value.
|
2013-03-21 17:06:15 +00:00
|
|
|
* @return {Promise}
|
|
|
|
*/
|
|
|
|
setCharsetForURI: function PU_setCharsetForURI(aURI, aCharset) {
|
2017-05-12 12:55:27 +00:00
|
|
|
return new Promise(resolve => {
|
|
|
|
|
|
|
|
// Delaying to catch issues with asynchronous behavior while waiting
|
|
|
|
// to implement asynchronous annotations in bug 699844.
|
|
|
|
Services.tm.dispatchToMainThread(function() {
|
|
|
|
if (aCharset && aCharset.length > 0) {
|
|
|
|
PlacesUtils.annotations.setPageAnnotation(
|
|
|
|
aURI, PlacesUtils.CHARSET_ANNO, aCharset, 0,
|
|
|
|
Ci.nsIAnnotationService.EXPIRE_NEVER);
|
|
|
|
} else {
|
|
|
|
PlacesUtils.annotations.removePageAnnotation(
|
|
|
|
aURI, PlacesUtils.CHARSET_ANNO);
|
|
|
|
}
|
|
|
|
resolve();
|
|
|
|
});
|
2013-03-21 17:06:15 +00:00
|
|
|
|
2017-05-12 12:55:27 +00:00
|
|
|
});
|
2013-03-21 17:06:15 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets the last saved character-set for a URI.
|
|
|
|
*
|
|
|
|
* @param aURI nsIURI
|
|
|
|
* @return {Promise}
|
|
|
|
* @resolve a character-set or null.
|
|
|
|
*/
|
2013-04-04 17:32:27 +00:00
|
|
|
getCharsetForURI: function PU_getCharsetForURI(aURI) {
|
2017-05-12 12:55:27 +00:00
|
|
|
return new Promise(resolve => {
|
2013-03-21 17:06:15 +00:00
|
|
|
|
2017-05-12 12:55:27 +00:00
|
|
|
Services.tm.dispatchToMainThread(function() {
|
|
|
|
let charset = null;
|
2013-03-21 17:06:15 +00:00
|
|
|
|
2017-05-12 12:55:27 +00:00
|
|
|
try {
|
|
|
|
charset = PlacesUtils.annotations.getPageAnnotation(aURI,
|
|
|
|
PlacesUtils.CHARSET_ANNO);
|
|
|
|
} catch (ex) { }
|
2013-03-21 17:06:15 +00:00
|
|
|
|
2017-05-12 12:55:27 +00:00
|
|
|
resolve(charset);
|
|
|
|
});
|
2013-03-21 17:06:15 +00:00
|
|
|
|
2017-05-12 12:55:27 +00:00
|
|
|
});
|
2013-06-19 07:18:38 +00:00
|
|
|
},
|
|
|
|
|
2014-03-03 16:51:42 +00:00
|
|
|
/**
|
|
|
|
* Gets favicon data for a given page url.
|
|
|
|
*
|
|
|
|
* @param aPageUrl url of the page to look favicon for.
|
|
|
|
* @resolves to an object representing a favicon entry, having the following
|
|
|
|
* properties: { uri, dataLen, data, mimeType }
|
|
|
|
* @rejects JavaScript exception if the given url has no associated favicon.
|
|
|
|
*/
|
2016-12-29 23:34:54 +00:00
|
|
|
promiseFaviconData(aPageUrl) {
|
2017-05-12 12:55:27 +00:00
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
PlacesUtils.favicons.getFaviconDataForPage(NetUtil.newURI(aPageUrl),
|
|
|
|
function(aURI, aDataLen, aData, aMimeType) {
|
|
|
|
if (aURI) {
|
|
|
|
resolve({ uri: aURI,
|
|
|
|
dataLen: aDataLen,
|
|
|
|
data: aData,
|
|
|
|
mimeType: aMimeType });
|
|
|
|
} else {
|
|
|
|
reject();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
2014-03-12 12:12:15 +00:00
|
|
|
},
|
|
|
|
|
2014-09-26 13:17:25 +00:00
|
|
|
/**
|
|
|
|
* Gets the favicon link url (moz-anno:) for a given page url.
|
|
|
|
*
|
|
|
|
* @param aPageURL url of the page to lookup the favicon for.
|
|
|
|
* @resolves to the nsIURL of the favicon link
|
|
|
|
* @rejects if the given url has no associated favicon.
|
|
|
|
*/
|
2016-12-29 23:34:54 +00:00
|
|
|
promiseFaviconLinkUrl(aPageUrl) {
|
2017-05-12 12:55:27 +00:00
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
if (!(aPageUrl instanceof Ci.nsIURI))
|
|
|
|
aPageUrl = NetUtil.newURI(aPageUrl);
|
|
|
|
|
|
|
|
PlacesUtils.favicons.getFaviconURLForPage(aPageUrl, uri => {
|
|
|
|
if (uri) {
|
|
|
|
uri = PlacesUtils.favicons.getFaviconLinkForIcon(uri);
|
|
|
|
resolve(uri);
|
|
|
|
} else {
|
|
|
|
reject("favicon not found for uri");
|
|
|
|
}
|
|
|
|
});
|
2014-09-26 13:17:25 +00:00
|
|
|
});
|
|
|
|
},
|
|
|
|
|
2017-03-15 15:08:28 +00:00
|
|
|
/**
|
|
|
|
* Returns the passed URL with a #size ref for the specified size and
|
|
|
|
* devicePixelRatio.
|
|
|
|
*
|
|
|
|
* @param window
|
|
|
|
* The window where the icon will appear.
|
|
|
|
* @param href
|
|
|
|
* The string href we should add the ref to.
|
|
|
|
* @param size
|
|
|
|
* The target image size
|
|
|
|
* @return The URL with the fragment at the end, in the same formar as input.
|
|
|
|
*/
|
|
|
|
urlWithSizeRef(window, href, size) {
|
|
|
|
return href + (href.includes("#") ? "&" : "#") +
|
|
|
|
"size=" + (Math.round(size) * window.devicePixelRatio);
|
|
|
|
},
|
|
|
|
|
2014-03-12 12:12:15 +00:00
|
|
|
/**
|
|
|
|
* Get the unique id for an item (a bookmark, a folder or a separator) given
|
|
|
|
* its item id.
|
|
|
|
*
|
|
|
|
* @param aItemId
|
|
|
|
* an item id
|
|
|
|
* @return {Promise}
|
|
|
|
* @resolves to the GUID.
|
|
|
|
* @rejects if aItemId is invalid.
|
|
|
|
*/
|
2015-05-25 15:18:31 +00:00
|
|
|
promiseItemGuid(aItemId) {
|
2017-10-15 18:50:30 +00:00
|
|
|
return GuidHelper.getItemGuid(aItemId);
|
2015-05-25 15:18:31 +00:00
|
|
|
},
|
2014-03-12 12:12:15 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the item id for an item (a bookmark, a folder or a separator) given
|
|
|
|
* its unique id.
|
|
|
|
*
|
2014-09-20 07:57:34 +00:00
|
|
|
* @param aGuid
|
2014-03-12 12:12:15 +00:00
|
|
|
* an item GUID
|
2015-05-25 15:18:31 +00:00
|
|
|
* @return {Promise}
|
2017-05-12 07:04:51 +00:00
|
|
|
* @resolves to the item id.
|
2014-03-12 12:12:15 +00:00
|
|
|
* @rejects if there's no item for the given GUID.
|
|
|
|
*/
|
2015-05-25 15:18:31 +00:00
|
|
|
promiseItemId(aGuid) {
|
2017-10-15 18:50:30 +00:00
|
|
|
return GuidHelper.getItemId(aGuid);
|
2015-05-25 15:18:31 +00:00
|
|
|
},
|
|
|
|
|
2017-05-12 07:04:51 +00:00
|
|
|
/**
|
|
|
|
* Get the item ids for multiple items (a bookmark, a folder or a separator)
|
|
|
|
* given the unique ids for each item.
|
|
|
|
*
|
|
|
|
* @param {Array} aGuids An array of item GUIDs.
|
|
|
|
* @return {Promise}
|
|
|
|
* @resolves to a Map of item ids.
|
|
|
|
* @rejects if not all of the GUIDs could be found.
|
|
|
|
*/
|
2017-03-13 23:45:43 +00:00
|
|
|
promiseManyItemIds(aGuids) {
|
|
|
|
return GuidHelper.getManyItemIds(aGuids);
|
|
|
|
},
|
|
|
|
|
2015-05-25 15:18:31 +00:00
|
|
|
/**
|
|
|
|
* Invalidate the GUID cache for the given itemId.
|
|
|
|
*
|
|
|
|
* @param aItemId
|
|
|
|
* an item id
|
|
|
|
*/
|
|
|
|
invalidateCachedGuidFor(aItemId) {
|
2017-10-15 18:50:30 +00:00
|
|
|
GuidHelper.invalidateCacheForItemId(aItemId);
|
2015-05-25 15:18:31 +00:00
|
|
|
},
|
2014-05-22 16:06:57 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Asynchronously retrieve a JS-object representation of a places bookmarks
|
|
|
|
* item (a bookmark, a folder, or a separator) along with all of its
|
|
|
|
* descendants.
|
|
|
|
*
|
2014-09-20 07:57:34 +00:00
|
|
|
* @param [optional] aItemGuid
|
2014-05-22 16:06:57 +00:00
|
|
|
* the (topmost) item to be queried. If it's not passed, the places
|
|
|
|
* root is queried: that is, you get a representation of the entire
|
|
|
|
* bookmarks hierarchy.
|
|
|
|
* @param [optional] aOptions
|
|
|
|
* Options for customizing the query behavior, in the form of a JS
|
|
|
|
* object with any of the following properties:
|
|
|
|
* - excludeItemsCallback: a function for excluding items, along with
|
|
|
|
* their descendants. Given an item object (that has everything set
|
|
|
|
* apart its potential children data), it should return true if the
|
|
|
|
* item should be excluded. Once an item is excluded, the function
|
|
|
|
* isn't called for any of its descendants. This isn't called for
|
|
|
|
* the root item.
|
|
|
|
* WARNING: since the function may be called for each item, using
|
|
|
|
* this option can slow down the process significantly if the
|
|
|
|
* callback does anything that's not relatively trivial. It is
|
|
|
|
* highly recommended to avoid any synchronous I/O or DB queries.
|
2014-09-17 11:59:23 +00:00
|
|
|
* - includeItemIds: opt-in to include the deprecated id property.
|
2014-09-20 07:57:34 +00:00
|
|
|
* Use it if you must. It'll be removed once the switch to GUIDs is
|
2014-09-17 11:59:23 +00:00
|
|
|
* complete.
|
2014-05-22 16:06:57 +00:00
|
|
|
*
|
|
|
|
* @return {Promise}
|
|
|
|
* @resolves to a JS object that represents either a single item or a
|
|
|
|
* bookmarks tree. Each node in the tree has the following properties set:
|
2014-09-20 07:57:34 +00:00
|
|
|
* - guid (string): the item's GUID (same as aItemGuid for the top item).
|
2014-09-17 11:59:23 +00:00
|
|
|
* - [deprecated] id (number): the item's id. This is only if
|
|
|
|
* aOptions.includeItemIds is set.
|
2016-04-06 00:30:55 +00:00
|
|
|
* - type (string): the item's type. @see PlacesUtils.TYPE_X_*
|
2017-08-29 14:27:38 +00:00
|
|
|
* - typeCode (number): the item's type in numeric format.
|
|
|
|
* @see PlacesUtils.bookmarks.TYPE_*
|
2014-05-22 16:06:57 +00:00
|
|
|
* - title (string): the item's title. If it has no title, this property
|
|
|
|
* isn't set.
|
|
|
|
* - dateAdded (number, microseconds from the epoch): the date-added value of
|
|
|
|
* the item.
|
|
|
|
* - lastModified (number, microseconds from the epoch): the last-modified
|
|
|
|
* value of the item.
|
|
|
|
* - annos (see getAnnotationsForItem): the item's annotations. This is not
|
|
|
|
* set if there are no annotations set for the item).
|
2016-04-06 00:30:55 +00:00
|
|
|
* - index: the item's index under it's parent.
|
2014-05-22 16:06:57 +00:00
|
|
|
*
|
2014-09-20 07:57:34 +00:00
|
|
|
* The root object (i.e. the one for aItemGuid) also has the following
|
2014-05-22 16:06:57 +00:00
|
|
|
* properties set:
|
2014-09-20 07:57:34 +00:00
|
|
|
* - parentGuid (string): the GUID of the root's parent. This isn't set if
|
2014-05-22 16:06:57 +00:00
|
|
|
* the root item is the places root.
|
|
|
|
* - itemsCount (number, not enumerable): the number of items, including the
|
|
|
|
* root item itself, which are represented in the resolved object.
|
|
|
|
*
|
|
|
|
* Bookmark items also have the following properties:
|
|
|
|
* - uri (string): the item's url.
|
|
|
|
* - tags (string): csv string of the bookmark's tags.
|
|
|
|
* - charset (string): the last known charset of the bookmark.
|
|
|
|
* - keyword (string): the bookmark's keyword (unset if none).
|
2015-04-16 10:13:43 +00:00
|
|
|
* - postData (string): the bookmark's keyword postData (unset if none).
|
2014-05-22 16:06:57 +00:00
|
|
|
* - iconuri (string): the bookmark's favicon url.
|
|
|
|
* The last four properties are not set at all if they're irrelevant (e.g.
|
|
|
|
* |charset| is not set if no charset was previously set for the bookmark
|
|
|
|
* url).
|
|
|
|
*
|
|
|
|
* Folders may also have the following properties:
|
|
|
|
* - children (array): the folder's children information, each of them
|
|
|
|
* having the same set of properties as above.
|
|
|
|
*
|
|
|
|
* @rejects if the query failed for any reason.
|
2014-09-20 07:57:34 +00:00
|
|
|
* @note if aItemGuid points to a non-existent item, the returned promise is
|
2014-05-22 16:06:57 +00:00
|
|
|
* resolved to null.
|
|
|
|
*/
|
2017-05-12 12:42:39 +00:00
|
|
|
async promiseBookmarksTree(aItemGuid = "", aOptions = {}) {
|
2017-05-12 12:45:01 +00:00
|
|
|
let createItemInfoObject = async function(aRow, aIncludeParentGuid) {
|
2014-05-22 16:06:57 +00:00
|
|
|
let item = {};
|
|
|
|
let copyProps = (...props) => {
|
|
|
|
for (let prop of props) {
|
|
|
|
let val = aRow.getResultByName(prop);
|
|
|
|
if (val !== null)
|
|
|
|
item[prop] = val;
|
|
|
|
}
|
|
|
|
};
|
2014-09-17 11:59:23 +00:00
|
|
|
copyProps("guid", "title", "index", "dateAdded", "lastModified");
|
2014-09-20 07:57:34 +00:00
|
|
|
if (aIncludeParentGuid)
|
|
|
|
copyProps("parentGuid");
|
2014-05-22 16:06:57 +00:00
|
|
|
|
2014-09-17 11:59:23 +00:00
|
|
|
let itemId = aRow.getResultByName("id");
|
|
|
|
if (aOptions.includeItemIds)
|
|
|
|
item.id = itemId;
|
|
|
|
|
|
|
|
// Cache it for promiseItemId consumers regardless.
|
2015-05-25 15:18:31 +00:00
|
|
|
GuidHelper.updateCache(itemId, item.guid);
|
2014-09-17 11:59:23 +00:00
|
|
|
|
2014-05-22 16:06:57 +00:00
|
|
|
let type = aRow.getResultByName("type");
|
2017-08-29 14:27:38 +00:00
|
|
|
item.typeCode = type;
|
2014-05-22 16:06:57 +00:00
|
|
|
if (type == Ci.nsINavBookmarksService.TYPE_BOOKMARK)
|
|
|
|
copyProps("charset", "tags", "iconuri");
|
|
|
|
|
|
|
|
// Add annotations.
|
|
|
|
if (aRow.getResultByName("has_annos")) {
|
|
|
|
try {
|
2014-09-17 11:59:23 +00:00
|
|
|
item.annos = PlacesUtils.getAnnotationsForItem(itemId);
|
2014-05-22 16:06:57 +00:00
|
|
|
} catch (e) {
|
|
|
|
Cu.reportError("Unexpected error while reading annotations " + e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (type) {
|
|
|
|
case Ci.nsINavBookmarksService.TYPE_BOOKMARK:
|
|
|
|
item.type = PlacesUtils.TYPE_X_MOZ_PLACE;
|
|
|
|
// If this throws due to an invalid url, the item will be skipped.
|
|
|
|
item.uri = NetUtil.newURI(aRow.getResultByName("url")).spec;
|
|
|
|
// Keywords are cached, so this should be decently fast.
|
2017-05-12 12:45:01 +00:00
|
|
|
let entry = await PlacesUtils.keywords.fetch({ url: item.uri });
|
2015-04-16 10:13:43 +00:00
|
|
|
if (entry) {
|
|
|
|
item.keyword = entry.keyword;
|
|
|
|
item.postData = entry.postData;
|
|
|
|
}
|
2014-05-22 16:06:57 +00:00
|
|
|
break;
|
|
|
|
case Ci.nsINavBookmarksService.TYPE_FOLDER:
|
|
|
|
item.type = PlacesUtils.TYPE_X_MOZ_PLACE_CONTAINER;
|
|
|
|
// Mark root folders.
|
2014-09-17 11:59:23 +00:00
|
|
|
if (itemId == PlacesUtils.placesRootId)
|
2014-05-22 16:06:57 +00:00
|
|
|
item.root = "placesRoot";
|
2014-09-17 11:59:23 +00:00
|
|
|
else if (itemId == PlacesUtils.bookmarksMenuFolderId)
|
2014-05-22 16:06:57 +00:00
|
|
|
item.root = "bookmarksMenuFolder";
|
2014-09-17 11:59:23 +00:00
|
|
|
else if (itemId == PlacesUtils.unfiledBookmarksFolderId)
|
2014-05-22 16:06:57 +00:00
|
|
|
item.root = "unfiledBookmarksFolder";
|
2014-09-17 11:59:23 +00:00
|
|
|
else if (itemId == PlacesUtils.toolbarFolderId)
|
2014-05-22 16:06:57 +00:00
|
|
|
item.root = "toolbarFolder";
|
2016-09-29 20:44:08 +00:00
|
|
|
else if (itemId == PlacesUtils.mobileFolderId)
|
|
|
|
item.root = "mobileFolder";
|
2014-05-22 16:06:57 +00:00
|
|
|
break;
|
|
|
|
case Ci.nsINavBookmarksService.TYPE_SEPARATOR:
|
|
|
|
item.type = PlacesUtils.TYPE_X_MOZ_PLACE_SEPARATOR;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
Cu.reportError("Unexpected bookmark type");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return item;
|
2017-01-05 21:43:29 +00:00
|
|
|
};
|
2014-05-22 16:06:57 +00:00
|
|
|
|
|
|
|
const QUERY_STR =
|
2016-08-30 09:04:22 +00:00
|
|
|
`/* do not warn (bug no): cannot use an index */
|
|
|
|
WITH RECURSIVE
|
2014-09-20 07:57:34 +00:00
|
|
|
descendants(fk, level, type, id, guid, parent, parentGuid, position,
|
2014-08-23 23:12:43 +00:00
|
|
|
title, dateAdded, lastModified) AS (
|
|
|
|
SELECT b1.fk, 0, b1.type, b1.id, b1.guid, b1.parent,
|
|
|
|
(SELECT guid FROM moz_bookmarks WHERE id = b1.parent),
|
|
|
|
b1.position, b1.title, b1.dateAdded, b1.lastModified
|
|
|
|
FROM moz_bookmarks b1 WHERE b1.guid=:item_guid
|
|
|
|
UNION ALL
|
|
|
|
SELECT b2.fk, level + 1, b2.type, b2.id, b2.guid, b2.parent,
|
|
|
|
descendants.guid, b2.position, b2.title, b2.dateAdded,
|
|
|
|
b2.lastModified
|
|
|
|
FROM moz_bookmarks b2
|
|
|
|
JOIN descendants ON b2.parent = descendants.id AND b2.id <> :tags_folder)
|
2014-09-20 07:57:34 +00:00
|
|
|
SELECT d.level, d.id, d.guid, d.parent, d.parentGuid, d.type,
|
2017-06-06 21:37:22 +00:00
|
|
|
d.position AS [index], IFNULL(d.title, "") AS title, d.dateAdded,
|
|
|
|
d.lastModified, h.url, (SELECT icon_url FROM moz_icons i
|
Bug 977177 - Update favicons API consumers. r=adw
Updates consumers to the new behavior.
Some consumers are changed to use the "page-icon:" protocol, since it's not
trivial to join the icons table and get a single result out of it. In most cases
the join would return multiple results since a page can have multiple icon payloads.
These consumers for now will return the biggest payload, bug 1347532 will fix
some of them to properly pass a #size=NN fragment.
Note that, even before, these were just "moz-anno:favicon:" uris, and the
payload had to be fetched from the database.
Some other consumers for now just fallback to the largest payload, by passing 0
to GetFaviconURLForPage.
The favicon optimization still happens on the main-thread, bug 1346139 will
handle that problem.
Most of the changes involve handling the modified IconData objects, that now
retain an array of payloads, rather than just one. But note that .ico files are
not yet split into single frames, due to imagelib missing APIs that will be handled
in bug 1337402.
The other changes involve fixing queries to properly join with the new tables.
Finally, note that thanks to the FOREIGN KEYS support, removing from moz_icons or
moz_pages_w_icons will also remove relations from moz_icons_to_pages.
The system only supports square icons, so icons are resized based on their larger side.
This doesn't include new tests, those will be in a following changeset.
MozReview-Commit-ID: JUkpquhpS8y
--HG--
rename : toolkit/components/places/tests/unit/test_svg_favicon.js => toolkit/components/places/tests/favicons/test_svg_favicon.js
extra : rebase_source : fa49c4a81d6ab6b34a2f19ee4175e889a6e9d734
2016-09-28 14:14:30 +00:00
|
|
|
JOIN moz_icons_to_pages ON icon_id = i.id
|
|
|
|
JOIN moz_pages_w_icons pi ON page_id = pi.id
|
|
|
|
WHERE pi.page_url_hash = hash(h.url) AND pi.page_url = h.url
|
|
|
|
ORDER BY width DESC LIMIT 1) AS iconuri,
|
2014-08-23 23:12:43 +00:00
|
|
|
(SELECT GROUP_CONCAT(t.title, ',')
|
|
|
|
FROM moz_bookmarks b2
|
|
|
|
JOIN moz_bookmarks t ON t.id = +b2.parent AND t.parent = :tags_folder
|
|
|
|
WHERE b2.fk = h.id
|
|
|
|
) AS tags,
|
|
|
|
EXISTS (SELECT 1 FROM moz_items_annos
|
|
|
|
WHERE item_id = d.id LIMIT 1) AS has_annos,
|
|
|
|
(SELECT a.content FROM moz_annos a
|
|
|
|
JOIN moz_anno_attributes n ON a.anno_attribute_id = n.id
|
|
|
|
WHERE place_id = h.id AND n.name = :charset_anno
|
|
|
|
) AS charset
|
|
|
|
FROM descendants d
|
|
|
|
LEFT JOIN moz_bookmarks b3 ON b3.id = d.parent
|
|
|
|
LEFT JOIN moz_places h ON h.id = d.fk
|
|
|
|
ORDER BY d.level, d.parent, d.position`;
|
2014-05-22 16:06:57 +00:00
|
|
|
|
|
|
|
|
2014-09-20 07:57:34 +00:00
|
|
|
if (!aItemGuid)
|
2014-11-04 16:40:33 +00:00
|
|
|
aItemGuid = this.bookmarks.rootGuid;
|
2014-05-22 16:06:57 +00:00
|
|
|
|
|
|
|
let hasExcludeItemsCallback =
|
|
|
|
aOptions.hasOwnProperty("excludeItemsCallback");
|
|
|
|
let excludedParents = new Set();
|
2014-09-20 07:57:34 +00:00
|
|
|
let shouldExcludeItem = (aItem, aParentGuid) => {
|
|
|
|
let exclude = excludedParents.has(aParentGuid) ||
|
2014-05-22 16:06:57 +00:00
|
|
|
aOptions.excludeItemsCallback(aItem);
|
|
|
|
if (exclude) {
|
|
|
|
if (aItem.type == this.TYPE_X_MOZ_PLACE_CONTAINER)
|
|
|
|
excludedParents.add(aItem.guid);
|
|
|
|
}
|
|
|
|
return exclude;
|
|
|
|
};
|
|
|
|
|
2015-04-16 10:13:43 +00:00
|
|
|
let rootItem = null;
|
2014-05-22 16:06:57 +00:00
|
|
|
let parentsMap = new Map();
|
2017-05-12 12:42:39 +00:00
|
|
|
let conn = await this.promiseDBConnection();
|
|
|
|
let rows = await conn.executeCached(QUERY_STR,
|
2015-04-16 10:13:43 +00:00
|
|
|
{ tags_folder: PlacesUtils.tagsFolderId,
|
|
|
|
charset_anno: PlacesUtils.CHARSET_ANNO,
|
|
|
|
item_guid: aItemGuid });
|
2015-08-26 00:43:07 +00:00
|
|
|
let yieldCounter = 0;
|
2015-04-16 10:13:43 +00:00
|
|
|
for (let row of rows) {
|
|
|
|
let item;
|
|
|
|
if (!rootItem) {
|
|
|
|
try {
|
2014-05-22 16:06:57 +00:00
|
|
|
// This is the first row.
|
2017-05-12 12:42:39 +00:00
|
|
|
rootItem = item = await createItemInfoObject(row, true);
|
2017-06-15 17:24:17 +00:00
|
|
|
Object.defineProperty(rootItem, "itemsCount", { value: 1,
|
|
|
|
writable: true,
|
|
|
|
enumerable: false,
|
|
|
|
configurable: false });
|
2016-08-04 07:28:58 +00:00
|
|
|
} catch (ex) {
|
2015-04-16 10:13:43 +00:00
|
|
|
throw new Error("Failed to fetch the data for the root item " + ex);
|
2014-05-22 16:06:57 +00:00
|
|
|
}
|
2015-04-16 10:13:43 +00:00
|
|
|
} else {
|
|
|
|
try {
|
2014-05-22 16:06:57 +00:00
|
|
|
// Our query guarantees that we always visit parents ahead of their
|
|
|
|
// children.
|
2017-05-12 12:42:39 +00:00
|
|
|
item = await createItemInfoObject(row, false);
|
2015-04-16 10:13:43 +00:00
|
|
|
let parentGuid = row.getResultByName("parentGuid");
|
2014-09-20 07:57:34 +00:00
|
|
|
if (hasExcludeItemsCallback && shouldExcludeItem(item, parentGuid))
|
2015-04-16 10:13:43 +00:00
|
|
|
continue;
|
2014-05-22 16:06:57 +00:00
|
|
|
|
2014-09-20 07:57:34 +00:00
|
|
|
let parentItem = parentsMap.get(parentGuid);
|
2014-05-22 16:06:57 +00:00
|
|
|
if ("children" in parentItem)
|
|
|
|
parentItem.children.push(item);
|
|
|
|
else
|
|
|
|
parentItem.children = [item];
|
|
|
|
|
|
|
|
rootItem.itemsCount++;
|
2016-08-04 07:28:58 +00:00
|
|
|
} catch (ex) {
|
2015-04-16 10:13:43 +00:00
|
|
|
// This is a bogus child, report and skip it.
|
|
|
|
Cu.reportError("Failed to fetch the data for an item " + ex);
|
|
|
|
continue;
|
2014-05-22 16:06:57 +00:00
|
|
|
}
|
2015-04-16 10:13:43 +00:00
|
|
|
}
|
2014-05-22 16:06:57 +00:00
|
|
|
|
2015-04-16 10:13:43 +00:00
|
|
|
if (item.type == this.TYPE_X_MOZ_PLACE_CONTAINER)
|
|
|
|
parentsMap.set(item.guid, item);
|
2015-08-26 00:43:07 +00:00
|
|
|
|
|
|
|
// With many bookmarks we end up stealing the CPU - even with yielding!
|
|
|
|
// So we let everyone else have a go every few items (bug 1186714).
|
|
|
|
if (++yieldCounter % 50 == 0) {
|
2017-05-12 12:42:39 +00:00
|
|
|
await new Promise(resolve => {
|
2017-04-14 16:29:12 +00:00
|
|
|
Services.tm.dispatchToMainThread(resolve);
|
2015-08-26 00:43:07 +00:00
|
|
|
});
|
|
|
|
}
|
2014-05-22 16:06:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return rootItem;
|
2017-05-12 12:42:39 +00:00
|
|
|
}
|
2010-04-26 19:04:17 +00:00
|
|
|
};
|
|
|
|
|
2013-03-20 17:09:41 +00:00
|
|
|
XPCOMUtils.defineLazyGetter(PlacesUtils, "history", function() {
|
2014-10-08 13:01:13 +00:00
|
|
|
let hs = Cc["@mozilla.org/browser/nav-history-service;1"]
|
|
|
|
.getService(Ci.nsINavHistoryService)
|
|
|
|
.QueryInterface(Ci.nsIBrowserHistory)
|
|
|
|
.QueryInterface(Ci.nsPIPlacesDatabase);
|
|
|
|
return Object.freeze(new Proxy(hs, {
|
2016-12-29 23:34:54 +00:00
|
|
|
get(target, name) {
|
2014-10-08 13:01:13 +00:00
|
|
|
let property, object;
|
|
|
|
if (name in target) {
|
|
|
|
property = target[name];
|
|
|
|
object = target;
|
|
|
|
} else {
|
|
|
|
property = History[name];
|
|
|
|
object = History;
|
|
|
|
}
|
|
|
|
if (typeof property == "function") {
|
|
|
|
return property.bind(object);
|
|
|
|
}
|
|
|
|
return property;
|
|
|
|
}
|
|
|
|
}));
|
2013-03-20 17:09:41 +00:00
|
|
|
});
|
2010-04-26 19:04:17 +00:00
|
|
|
|
2012-03-21 19:20:47 +00:00
|
|
|
XPCOMUtils.defineLazyServiceGetter(PlacesUtils, "asyncHistory",
|
|
|
|
"@mozilla.org/browser/history;1",
|
|
|
|
"mozIAsyncHistory");
|
|
|
|
|
2010-04-26 19:04:17 +00:00
|
|
|
XPCOMUtils.defineLazyServiceGetter(PlacesUtils, "favicons",
|
|
|
|
"@mozilla.org/browser/favicon-service;1",
|
2012-01-11 05:04:01 +00:00
|
|
|
"mozIAsyncFavicons");
|
2010-04-26 19:04:17 +00:00
|
|
|
|
2017-05-15 17:32:47 +00:00
|
|
|
XPCOMUtils.defineLazyServiceGetter(this, "bmsvc",
|
|
|
|
"@mozilla.org/browser/nav-bookmarks-service;1",
|
|
|
|
"nsINavBookmarksService");
|
2014-09-22 16:32:40 +00:00
|
|
|
XPCOMUtils.defineLazyGetter(PlacesUtils, "bookmarks", () => {
|
2017-05-15 17:32:47 +00:00
|
|
|
return Object.freeze(new Proxy(Bookmarks, {
|
|
|
|
get: (target, name) => Bookmarks.hasOwnProperty(name) ? Bookmarks[name]
|
|
|
|
: bmsvc[name]
|
2014-09-22 16:32:40 +00:00
|
|
|
}));
|
|
|
|
});
|
2010-04-26 19:04:17 +00:00
|
|
|
|
|
|
|
XPCOMUtils.defineLazyServiceGetter(PlacesUtils, "annotations",
|
|
|
|
"@mozilla.org/browser/annotation-service;1",
|
|
|
|
"nsIAnnotationService");
|
|
|
|
|
|
|
|
XPCOMUtils.defineLazyServiceGetter(PlacesUtils, "tagging",
|
|
|
|
"@mozilla.org/browser/tagging-service;1",
|
|
|
|
"nsITaggingService");
|
|
|
|
|
2014-05-22 16:06:57 +00:00
|
|
|
XPCOMUtils.defineLazyServiceGetter(PlacesUtils, "livemarks",
|
|
|
|
"@mozilla.org/browser/livemark-service;2",
|
|
|
|
"mozIAsyncLivemarks");
|
2010-04-26 19:04:17 +00:00
|
|
|
|
2010-06-30 21:24:03 +00:00
|
|
|
XPCOMUtils.defineLazyGetter(this, "bundle", function() {
|
2010-04-26 19:04:17 +00:00
|
|
|
const PLACES_STRING_BUNDLE_URI = "chrome://places/locale/places.properties";
|
2017-10-30 16:46:27 +00:00
|
|
|
return Services.strings.createBundle(PLACES_STRING_BUNDLE_URI);
|
2010-04-26 19:04:17 +00:00
|
|
|
});
|
2010-06-30 21:24:03 +00:00
|
|
|
|
2017-09-08 06:01:29 +00:00
|
|
|
// This is just used as a reasonably-random value for copy & paste / drag operations.
|
|
|
|
XPCOMUtils.defineLazyGetter(PlacesUtils, "instanceId", () => {
|
|
|
|
return PlacesUtils.history.makeGuid();
|
|
|
|
});
|
|
|
|
|
2015-10-21 10:30:30 +00:00
|
|
|
/**
|
|
|
|
* Setup internal databases for closing properly during shutdown.
|
|
|
|
*
|
|
|
|
* 1. Places initiates shutdown.
|
|
|
|
* 2. Before places can move to the step where it closes the low-level connection,
|
|
|
|
* we need to make sure that we have closed `conn`.
|
|
|
|
* 3. Before we can close `conn`, we need to make sure that all external clients
|
|
|
|
* have stopped using `conn`.
|
|
|
|
* 4. Before we can close Sqlite, we need to close `conn`.
|
|
|
|
*/
|
|
|
|
function setupDbForShutdown(conn, name) {
|
|
|
|
try {
|
|
|
|
let state = "0. Not started.";
|
2016-06-29 12:47:36 +00:00
|
|
|
let promiseClosed = new Promise((resolve, reject) => {
|
2015-10-21 10:30:30 +00:00
|
|
|
// The service initiates shutdown.
|
|
|
|
// Before it can safely close its connection, we need to make sure
|
|
|
|
// that we have closed the high-level connection.
|
2016-06-29 12:47:36 +00:00
|
|
|
try {
|
2016-05-19 21:50:27 +00:00
|
|
|
PlacesUtils.history.connectionShutdownClient.jsclient.addBlocker(
|
|
|
|
`${name} closing as part of Places shutdown`,
|
2017-05-12 12:42:39 +00:00
|
|
|
async function() {
|
2016-06-29 12:47:36 +00:00
|
|
|
state = "1. Service has initiated shutdown";
|
|
|
|
|
|
|
|
// At this stage, all external clients have finished using the
|
|
|
|
// database. We just need to close the high-level connection.
|
2017-05-12 12:42:39 +00:00
|
|
|
await conn.close();
|
2016-06-29 12:47:36 +00:00
|
|
|
state = "2. Closed Sqlite.jsm connection.";
|
|
|
|
|
|
|
|
resolve();
|
2017-05-12 12:42:39 +00:00
|
|
|
},
|
2016-06-29 12:47:36 +00:00
|
|
|
() => state
|
|
|
|
);
|
|
|
|
} catch (ex) {
|
|
|
|
// It's too late to block shutdown, just close the connection.
|
|
|
|
conn.close();
|
|
|
|
reject(ex);
|
|
|
|
}
|
2015-03-20 08:39:25 +00:00
|
|
|
});
|
2015-10-21 10:30:30 +00:00
|
|
|
|
|
|
|
// Make sure that Sqlite.jsm doesn't close until we are done
|
|
|
|
// with the high-level connection.
|
|
|
|
Sqlite.shutdown.addBlocker(`${name} must be closed before Sqlite.jsm`,
|
2016-06-29 12:47:36 +00:00
|
|
|
() => promiseClosed.catch(Cu.reportError),
|
2015-10-21 10:30:30 +00:00
|
|
|
() => state
|
|
|
|
);
|
2016-08-04 07:28:58 +00:00
|
|
|
} catch (ex) {
|
2015-10-21 10:30:30 +00:00
|
|
|
// It's too late to block shutdown, just close the connection.
|
|
|
|
conn.close();
|
|
|
|
throw ex;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
XPCOMUtils.defineLazyGetter(this, "gAsyncDBConnPromised",
|
|
|
|
() => Sqlite.cloneStorageConnection({
|
|
|
|
connection: PlacesUtils.history.DBConnection,
|
|
|
|
readOnly: true
|
|
|
|
}).then(conn => {
|
|
|
|
setupDbForShutdown(conn, "PlacesUtils read-only connection");
|
|
|
|
return conn;
|
2016-06-29 12:47:36 +00:00
|
|
|
}).catch(Cu.reportError)
|
2015-03-20 08:39:25 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
XPCOMUtils.defineLazyGetter(this, "gAsyncDBWrapperPromised",
|
2015-10-21 10:30:30 +00:00
|
|
|
() => Sqlite.wrapStorageConnection({
|
2015-03-20 08:39:25 +00:00
|
|
|
connection: PlacesUtils.history.DBConnection,
|
2015-10-21 10:30:30 +00:00
|
|
|
}).then(conn => {
|
|
|
|
setupDbForShutdown(conn, "PlacesUtils wrapped connection");
|
|
|
|
return conn;
|
2016-06-29 12:47:36 +00:00
|
|
|
}).catch(Cu.reportError)
|
2015-03-20 08:39:25 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Keywords management API.
|
2018-02-06 14:58:31 +00:00
|
|
|
* Sooner or later these keywords will merge with search aliases, this is an
|
2015-03-20 08:39:25 +00:00
|
|
|
* interim API that should then be replaced by a unified one.
|
|
|
|
* Keywords are associated with URLs and can have POST data.
|
2018-02-06 14:58:31 +00:00
|
|
|
* The relations between URLs and keywords are the following:
|
|
|
|
* - 1 keyword can only point to 1 URL
|
|
|
|
* - 1 URL can have multiple keywords, iff they differ by POST data (included the empty one).
|
2015-03-20 08:39:25 +00:00
|
|
|
*/
|
2018-02-21 06:04:59 +00:00
|
|
|
PlacesUtils.keywords = {
|
2015-03-20 08:39:25 +00:00
|
|
|
/**
|
2015-04-03 13:03:15 +00:00
|
|
|
* Fetches a keyword entry based on keyword or URL.
|
2015-03-20 08:39:25 +00:00
|
|
|
*
|
2015-04-03 13:03:15 +00:00
|
|
|
* @param keywordOrEntry
|
|
|
|
* Either the keyword to fetch or an entry providing keyword
|
|
|
|
* or url property to find keywords for. If both properties are set,
|
|
|
|
* this returns their intersection.
|
|
|
|
* @param onResult [optional]
|
|
|
|
* Callback invoked for each found entry.
|
2015-03-20 08:39:25 +00:00
|
|
|
* @return {Promise}
|
|
|
|
* @resolves to an object in the form: { keyword, url, postData },
|
2015-04-03 13:03:15 +00:00
|
|
|
* or null if a keyword entry was not found.
|
2015-03-20 08:39:25 +00:00
|
|
|
*/
|
2016-11-10 22:48:04 +00:00
|
|
|
fetch(keywordOrEntry, onResult = null) {
|
2015-04-03 13:03:15 +00:00
|
|
|
if (typeof(keywordOrEntry) == "string")
|
|
|
|
keywordOrEntry = { keyword: keywordOrEntry };
|
|
|
|
|
|
|
|
if (keywordOrEntry === null || typeof(keywordOrEntry) != "object" ||
|
|
|
|
(("keyword" in keywordOrEntry) && typeof(keywordOrEntry.keyword) != "string"))
|
2015-03-20 08:39:25 +00:00
|
|
|
throw new Error("Invalid keyword");
|
2015-04-03 13:03:15 +00:00
|
|
|
|
|
|
|
let hasKeyword = "keyword" in keywordOrEntry;
|
|
|
|
let hasUrl = "url" in keywordOrEntry;
|
|
|
|
|
|
|
|
if (!hasKeyword && !hasUrl)
|
|
|
|
throw new Error("At least keyword or url must be provided");
|
|
|
|
if (onResult && typeof onResult != "function")
|
|
|
|
throw new Error("onResult callback must be a valid function");
|
|
|
|
|
|
|
|
if (hasUrl)
|
|
|
|
keywordOrEntry.url = new URL(keywordOrEntry.url);
|
|
|
|
if (hasKeyword)
|
|
|
|
keywordOrEntry.keyword = keywordOrEntry.keyword.trim().toLowerCase();
|
|
|
|
|
|
|
|
let safeOnResult = entry => {
|
|
|
|
if (onResult) {
|
|
|
|
try {
|
|
|
|
onResult(entry);
|
|
|
|
} catch (ex) {
|
|
|
|
Cu.reportError(ex);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2018-02-21 06:04:59 +00:00
|
|
|
return promiseKeywordsCache().then(cache => {
|
2015-04-03 13:03:15 +00:00
|
|
|
let entries = [];
|
|
|
|
if (hasKeyword) {
|
|
|
|
let entry = cache.get(keywordOrEntry.keyword);
|
|
|
|
if (entry)
|
|
|
|
entries.push(entry);
|
|
|
|
}
|
|
|
|
if (hasUrl) {
|
|
|
|
for (let entry of cache.values()) {
|
|
|
|
if (entry.url.href == keywordOrEntry.url.href)
|
|
|
|
entries.push(entry);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
entries = entries.filter(e => {
|
|
|
|
return (!hasUrl || e.url.href == keywordOrEntry.url.href) &&
|
|
|
|
(!hasKeyword || e.keyword == keywordOrEntry.keyword);
|
|
|
|
});
|
|
|
|
|
|
|
|
entries.forEach(safeOnResult);
|
|
|
|
return entries.length ? entries[0] : null;
|
|
|
|
});
|
2015-03-20 08:39:25 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Adds a new keyword and postData for the given URL.
|
|
|
|
*
|
|
|
|
* @param keywordEntry
|
|
|
|
* An object describing the keyword to insert, in the form:
|
|
|
|
* {
|
|
|
|
* keyword: non-empty string,
|
|
|
|
* URL: URL or href to associate to the keyword,
|
|
|
|
* postData: optional POST data to associate to the keyword
|
2017-05-22 17:28:39 +00:00
|
|
|
* source: The change source, forwarded to all bookmark observers.
|
|
|
|
* Defaults to nsINavBookmarksService::SOURCE_DEFAULT.
|
2015-03-20 08:39:25 +00:00
|
|
|
* }
|
|
|
|
* @note Do not define a postData property if there isn't any POST data.
|
2018-02-06 14:58:31 +00:00
|
|
|
* Defining an empty string for POST data is equivalent to not having it.
|
2015-03-20 08:39:25 +00:00
|
|
|
* @resolves when the addition is complete.
|
|
|
|
*/
|
|
|
|
insert(keywordEntry) {
|
|
|
|
if (!keywordEntry || typeof keywordEntry != "object")
|
|
|
|
throw new Error("Input should be a valid object");
|
|
|
|
|
|
|
|
if (!("keyword" in keywordEntry) || !keywordEntry.keyword ||
|
|
|
|
typeof(keywordEntry.keyword) != "string")
|
|
|
|
throw new Error("Invalid keyword");
|
|
|
|
if (("postData" in keywordEntry) && keywordEntry.postData &&
|
2018-02-06 14:58:31 +00:00
|
|
|
typeof(keywordEntry.postData) != "string")
|
2015-03-20 08:39:25 +00:00
|
|
|
throw new Error("Invalid POST data");
|
|
|
|
if (!("url" in keywordEntry))
|
|
|
|
throw new Error("undefined is not a valid URL");
|
2017-05-22 17:28:39 +00:00
|
|
|
|
|
|
|
if (!("source" in keywordEntry)) {
|
|
|
|
keywordEntry.source = PlacesUtils.bookmarks.SOURCES.DEFAULT;
|
|
|
|
}
|
|
|
|
let { keyword, url, source } = keywordEntry;
|
2015-03-20 08:39:25 +00:00
|
|
|
keyword = keyword.trim().toLowerCase();
|
2018-02-06 14:58:31 +00:00
|
|
|
let postData = keywordEntry.postData || "";
|
2015-03-20 08:39:25 +00:00
|
|
|
// This also checks href for validity
|
|
|
|
url = new URL(url);
|
|
|
|
|
2018-02-21 06:04:59 +00:00
|
|
|
return PlacesUtils.withConnectionWrapper("PlacesUtils.keywords.insert", async db => {
|
|
|
|
let cache = await promiseKeywordsCache();
|
2015-03-20 08:39:25 +00:00
|
|
|
|
2015-05-05 10:44:16 +00:00
|
|
|
// Trying to set the same keyword is a no-op.
|
|
|
|
let oldEntry = cache.get(keyword);
|
|
|
|
if (oldEntry && oldEntry.url.href == url.href &&
|
2018-02-06 14:58:31 +00:00
|
|
|
(oldEntry.postData || "") == postData) {
|
2015-05-05 10:44:16 +00:00
|
|
|
return;
|
|
|
|
}
|
2015-03-20 08:39:25 +00:00
|
|
|
|
2015-05-05 10:44:16 +00:00
|
|
|
// A keyword can only be associated to a single page.
|
|
|
|
// If another page is using the new keyword, we must update the keyword
|
|
|
|
// entry.
|
|
|
|
// Note we cannot use INSERT OR REPLACE cause it wouldn't invoke the delete
|
|
|
|
// trigger.
|
|
|
|
if (oldEntry) {
|
2017-05-12 12:42:39 +00:00
|
|
|
await db.executeCached(
|
2015-05-05 10:44:16 +00:00
|
|
|
`UPDATE moz_keywords
|
2016-06-29 12:47:36 +00:00
|
|
|
SET place_id = (SELECT id FROM moz_places WHERE url_hash = hash(:url) AND url = :url),
|
2015-05-05 10:44:16 +00:00
|
|
|
post_data = :post_data
|
|
|
|
WHERE keyword = :keyword
|
2016-12-29 23:34:54 +00:00
|
|
|
`, { url: url.href, keyword, post_data: postData });
|
2017-05-12 12:42:39 +00:00
|
|
|
await notifyKeywordChange(oldEntry.url.href, "", source);
|
2015-05-05 10:44:16 +00:00
|
|
|
} else {
|
|
|
|
// An entry for the given page could be missing, in such a case we need to
|
2016-06-29 12:47:36 +00:00
|
|
|
// create it. The IGNORE conflict can trigger on `guid`.
|
2018-02-06 14:58:31 +00:00
|
|
|
await db.executeTransaction(async () => {
|
2017-11-07 20:01:53 +00:00
|
|
|
await db.executeCached(
|
|
|
|
`INSERT OR IGNORE INTO moz_places (url, url_hash, rev_host, hidden, frecency, guid)
|
|
|
|
VALUES (:url, hash(:url), :rev_host, 0, :frecency,
|
|
|
|
IFNULL((SELECT guid FROM moz_places WHERE url_hash = hash(:url) AND url = :url),
|
|
|
|
GENERATE_GUID()))
|
|
|
|
`, { url: url.href, rev_host: PlacesUtils.getReversedHost(url),
|
|
|
|
frecency: url.protocol == "place:" ? 0 : -1 });
|
|
|
|
await db.executeCached("DELETE FROM moz_updatehostsinsert_temp");
|
2018-02-06 14:58:31 +00:00
|
|
|
|
|
|
|
// A new keyword could be assigned to an url that already has one,
|
|
|
|
// then we must replace the old keyword with the new one.
|
|
|
|
let oldKeywords = [];
|
|
|
|
for (let entry of cache.values()) {
|
|
|
|
if (entry.url.href == url.href && (entry.postData || "") == postData)
|
|
|
|
oldKeywords.push(entry.keyword);
|
|
|
|
}
|
|
|
|
if (oldKeywords.length) {
|
|
|
|
for (let oldKeyword of oldKeywords) {
|
|
|
|
await db.executeCached(
|
|
|
|
`DELETE FROM moz_keywords WHERE keyword = :oldKeyword`,
|
|
|
|
{ oldKeyword });
|
|
|
|
cache.delete(oldKeyword);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-07 20:01:53 +00:00
|
|
|
await db.executeCached(
|
|
|
|
`INSERT INTO moz_keywords (keyword, place_id, post_data)
|
|
|
|
VALUES (:keyword, (SELECT id FROM moz_places WHERE url_hash = hash(:url) AND url = :url), :post_data)
|
|
|
|
`, { url: url.href, keyword, post_data: postData });
|
|
|
|
});
|
2015-05-05 10:44:16 +00:00
|
|
|
}
|
2015-03-20 08:39:25 +00:00
|
|
|
|
2017-05-12 12:42:39 +00:00
|
|
|
await PlacesSyncUtils.bookmarks.addSyncChangesForBookmarksWithURL(
|
2016-11-20 17:57:14 +00:00
|
|
|
db, url, PlacesSyncUtils.bookmarks.determineSyncChangeDelta(source));
|
|
|
|
|
2018-02-06 14:58:31 +00:00
|
|
|
cache.set(keyword, { keyword, url, postData: postData || null });
|
2015-03-20 08:39:25 +00:00
|
|
|
|
2015-05-05 10:44:16 +00:00
|
|
|
// In any case, notify about the new keyword.
|
2017-05-12 12:42:39 +00:00
|
|
|
await notifyKeywordChange(url.href, keyword, source);
|
|
|
|
}
|
2015-05-05 10:44:16 +00:00
|
|
|
);
|
2015-03-20 08:39:25 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Removes a keyword.
|
|
|
|
*
|
|
|
|
* @param keyword
|
|
|
|
* The keyword to remove.
|
|
|
|
* @return {Promise}
|
|
|
|
* @resolves when the removal is complete.
|
|
|
|
*/
|
2016-08-11 20:13:22 +00:00
|
|
|
remove(keywordOrEntry) {
|
2017-05-22 17:28:39 +00:00
|
|
|
if (typeof(keywordOrEntry) == "string") {
|
|
|
|
keywordOrEntry = {
|
|
|
|
keyword: keywordOrEntry,
|
|
|
|
source: Ci.nsINavBookmarksService.SOURCE_DEFAULT
|
|
|
|
};
|
|
|
|
}
|
2016-08-11 20:13:22 +00:00
|
|
|
|
|
|
|
if (keywordOrEntry === null || typeof(keywordOrEntry) != "object" ||
|
|
|
|
!keywordOrEntry.keyword || typeof keywordOrEntry.keyword != "string")
|
2015-03-20 08:39:25 +00:00
|
|
|
throw new Error("Invalid keyword");
|
2016-08-11 20:13:22 +00:00
|
|
|
|
|
|
|
let { keyword,
|
|
|
|
source = Ci.nsINavBookmarksService.SOURCE_DEFAULT } = keywordOrEntry;
|
|
|
|
keyword = keywordOrEntry.keyword.trim().toLowerCase();
|
2018-02-21 06:04:59 +00:00
|
|
|
return PlacesUtils.withConnectionWrapper("PlacesUtils.keywords.remove", async db => {
|
|
|
|
let cache = await promiseKeywordsCache();
|
2015-03-20 08:39:25 +00:00
|
|
|
if (!cache.has(keyword))
|
|
|
|
return;
|
|
|
|
let { url } = cache.get(keyword);
|
|
|
|
cache.delete(keyword);
|
|
|
|
|
2017-05-12 12:42:39 +00:00
|
|
|
await db.execute(`DELETE FROM moz_keywords WHERE keyword = :keyword`,
|
2015-03-20 08:39:25 +00:00
|
|
|
{ keyword });
|
|
|
|
|
2017-05-12 12:42:39 +00:00
|
|
|
await PlacesSyncUtils.bookmarks.addSyncChangesForBookmarksWithURL(
|
2016-11-20 17:57:14 +00:00
|
|
|
db, url, PlacesSyncUtils.bookmarks.determineSyncChangeDelta(source));
|
|
|
|
|
2015-03-20 08:39:25 +00:00
|
|
|
// Notify bookmarks about the removal.
|
2017-05-12 12:42:39 +00:00
|
|
|
await notifyKeywordChange(url.href, "", source);
|
|
|
|
});
|
2018-02-21 06:04:59 +00:00
|
|
|
},
|
2015-03-20 08:39:25 +00:00
|
|
|
|
2018-02-21 06:04:59 +00:00
|
|
|
/**
|
|
|
|
* Moves all (keyword, POST data) pairs from one URL to another, and fires
|
|
|
|
* observer notifications for all affected bookmarks. If the destination URL
|
|
|
|
* already has keywords, they will be removed and replaced with the source
|
|
|
|
* URL's keywords.
|
|
|
|
*
|
|
|
|
* @param oldURL
|
|
|
|
* The source URL.
|
|
|
|
* @param newURL
|
|
|
|
* The destination URL.
|
|
|
|
* @param source
|
|
|
|
* The change source, forwarded to all bookmark observers.
|
|
|
|
* @return {Promise}
|
|
|
|
* @resolves when all keywords have been moved to the destination URL.
|
|
|
|
*/
|
|
|
|
reassign(oldURL, newURL, source = PlacesUtils.bookmarks.SOURCES.DEFAULT) {
|
|
|
|
try {
|
|
|
|
oldURL = BOOKMARK_VALIDATORS.url(oldURL);
|
|
|
|
} catch (ex) {
|
|
|
|
throw new Error(oldURL + " is not a valid source URL");
|
|
|
|
}
|
|
|
|
try {
|
|
|
|
newURL = BOOKMARK_VALIDATORS.url(newURL);
|
|
|
|
} catch (ex) {
|
|
|
|
throw new Error(oldURL + " is not a valid destination URL");
|
|
|
|
}
|
|
|
|
return PlacesUtils.withConnectionWrapper("PlacesUtils.keywords.reassign",
|
|
|
|
async function(db) {
|
|
|
|
let keywordsToReassign = [];
|
|
|
|
let keywordsToRemove = [];
|
|
|
|
let cache = await promiseKeywordsCache();
|
|
|
|
for (let [keyword, entry] of cache) {
|
|
|
|
if (entry.url.href == oldURL.href) {
|
|
|
|
keywordsToReassign.push(keyword);
|
|
|
|
}
|
|
|
|
if (entry.url.href == newURL.href) {
|
|
|
|
keywordsToRemove.push(keyword);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!keywordsToReassign.length) {
|
|
|
|
return;
|
|
|
|
}
|
2015-05-05 10:44:16 +00:00
|
|
|
|
2018-02-21 06:04:59 +00:00
|
|
|
await db.executeTransaction(async function() {
|
|
|
|
// Remove existing keywords from the new URL.
|
|
|
|
await db.executeCached(
|
|
|
|
`DELETE FROM moz_keywords WHERE keyword = :keyword`,
|
|
|
|
keywordsToRemove.map(keyword => ({ keyword })));
|
|
|
|
|
|
|
|
// Move keywords from the old URL to the new URL.
|
|
|
|
await db.executeCached(`
|
|
|
|
UPDATE moz_keywords SET
|
|
|
|
place_id = (SELECT id FROM moz_places
|
|
|
|
WHERE url_hash = hash(:newURL) AND
|
|
|
|
url = :newURL)
|
|
|
|
WHERE place_id = (SELECT id FROM moz_places
|
|
|
|
WHERE url_hash = hash(:oldURL) AND
|
|
|
|
url = :oldURL)`,
|
|
|
|
{ newURL: newURL.href, oldURL: oldURL.href });
|
|
|
|
});
|
|
|
|
for (let keyword of keywordsToReassign) {
|
|
|
|
let entry = cache.get(keyword);
|
|
|
|
entry.url = newURL;
|
|
|
|
}
|
|
|
|
for (let keyword of keywordsToRemove) {
|
|
|
|
cache.delete(keyword);
|
|
|
|
}
|
2015-08-05 21:10:16 +00:00
|
|
|
|
2018-02-21 06:04:59 +00:00
|
|
|
if (keywordsToReassign.length) {
|
|
|
|
// If we moved any keywords, notify that we removed all keywords from
|
|
|
|
// the old and new URLs, then notify for each moved keyword.
|
|
|
|
await notifyKeywordChange(oldURL, "", source);
|
|
|
|
await notifyKeywordChange(newURL, "", source);
|
|
|
|
for (let keyword of keywordsToReassign) {
|
|
|
|
await notifyKeywordChange(newURL, keyword, source);
|
|
|
|
}
|
|
|
|
} else if (keywordsToRemove.length) {
|
|
|
|
// If the old URL didn't have any keywords, but the new URL did, just
|
|
|
|
// notify that we removed all keywords from the new URL.
|
|
|
|
await notifyKeywordChange(oldURL, "", source);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Removes all orphaned keywords from the given URLs. Orphaned keywords are
|
|
|
|
* associated with URLs that are no longer bookmarked. If a given URL is still
|
|
|
|
* bookmarked, its keywords will not be removed.
|
|
|
|
*
|
|
|
|
* @param urls
|
|
|
|
* A list of URLs to check for orphaned keywords.
|
|
|
|
* @return {Promise}
|
|
|
|
* @resolves when all keywords have been removed from URLs that are no longer
|
|
|
|
* bookmarked.
|
|
|
|
*/
|
|
|
|
removeFromURLsIfNotBookmarked(urls) {
|
|
|
|
let hrefs = new Set();
|
|
|
|
for (let url of urls) {
|
|
|
|
try {
|
|
|
|
url = BOOKMARK_VALIDATORS.url(url);
|
|
|
|
} catch (ex) {
|
|
|
|
throw new Error(url + " is not a valid URL");
|
|
|
|
}
|
|
|
|
hrefs.add(url.href);
|
|
|
|
}
|
|
|
|
return PlacesUtils.withConnectionWrapper(
|
|
|
|
"PlacesUtils.keywords.removeFromURLsIfNotBookmarked",
|
|
|
|
async function(db) {
|
|
|
|
let keywordsByHref = new Map();
|
|
|
|
let cache = await promiseKeywordsCache();
|
|
|
|
for (let [keyword, entry] of cache) {
|
|
|
|
let href = entry.url.href;
|
|
|
|
if (!hrefs.has(href)) {
|
|
|
|
continue;
|
2016-08-30 09:04:22 +00:00
|
|
|
}
|
2018-02-21 06:04:59 +00:00
|
|
|
if (!keywordsByHref.has(href)) {
|
|
|
|
keywordsByHref.set(href, [keyword]);
|
|
|
|
continue;
|
2016-08-30 09:04:22 +00:00
|
|
|
}
|
2018-02-21 06:04:59 +00:00
|
|
|
let existingKeywords = keywordsByHref.get(href);
|
|
|
|
existingKeywords.push(keyword);
|
|
|
|
}
|
|
|
|
if (!keywordsByHref.size) {
|
|
|
|
return;
|
|
|
|
}
|
2016-08-30 09:04:22 +00:00
|
|
|
|
2018-02-21 06:04:59 +00:00
|
|
|
let placeInfosToRemove = [];
|
|
|
|
let rows = await db.execute(`
|
|
|
|
SELECT h.id, h.url
|
|
|
|
FROM moz_places h
|
|
|
|
JOIN moz_keywords k ON k.place_id = h.id
|
|
|
|
GROUP BY h.id
|
|
|
|
HAVING h.foreign_count = COUNT(*)`);
|
|
|
|
for (let row of rows) {
|
|
|
|
placeInfosToRemove.push({
|
|
|
|
placeId: row.getResultByName("id"),
|
|
|
|
href: row.getResultByName("url"),
|
|
|
|
});
|
|
|
|
}
|
|
|
|
if (!placeInfosToRemove.length) {
|
|
|
|
return;
|
|
|
|
}
|
2015-03-20 08:39:25 +00:00
|
|
|
|
2018-02-21 06:04:59 +00:00
|
|
|
await db.execute(`DELETE FROM moz_keywords WHERE place_id IN (${
|
|
|
|
Array.from(placeInfosToRemove.map(info => info.placeId)).join()})`);
|
|
|
|
for (let { href } of placeInfosToRemove) {
|
|
|
|
let keywords = keywordsByHref.get(href);
|
|
|
|
for (let keyword of keywords) {
|
|
|
|
cache.delete(keyword);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
},
|
2017-06-09 16:51:09 +00:00
|
|
|
|
2018-02-21 06:04:59 +00:00
|
|
|
/**
|
|
|
|
* Removes all keywords from all URLs.
|
|
|
|
*
|
|
|
|
* @return {Promise}
|
|
|
|
* @resolves when all keywords have been removed.
|
|
|
|
*/
|
|
|
|
eraseEverything() {
|
|
|
|
return PlacesUtils.withConnectionWrapper(
|
|
|
|
"PlacesUtils.keywords.eraseEverything",
|
|
|
|
async function(db) {
|
|
|
|
let cache = await promiseKeywordsCache();
|
|
|
|
if (!cache.size) {
|
|
|
|
return;
|
2017-06-09 16:51:09 +00:00
|
|
|
}
|
2018-02-21 06:04:59 +00:00
|
|
|
await db.executeCached(`DELETE FROM moz_keywords`);
|
|
|
|
cache.clear();
|
2017-06-09 16:51:09 +00:00
|
|
|
}
|
2018-02-21 06:04:59 +00:00
|
|
|
);
|
|
|
|
},
|
2017-06-09 16:51:09 +00:00
|
|
|
|
2018-02-21 06:04:59 +00:00
|
|
|
/**
|
|
|
|
* Invalidates the keywords cache, leaving all existing keywords in place.
|
|
|
|
* The cache will be repopulated on the next `PlacesUtils.keywords.*` call.
|
|
|
|
*
|
|
|
|
* @return {Promise}
|
|
|
|
* @resolves when the cache has been cleared.
|
|
|
|
*/
|
|
|
|
invalidateCachedKeywords() {
|
|
|
|
gKeywordsCachePromise = gKeywordsCachePromise.then(_ => null);
|
|
|
|
return gKeywordsCachePromise;
|
|
|
|
},
|
|
|
|
};
|
2017-06-09 16:51:09 +00:00
|
|
|
|
2018-02-21 06:04:59 +00:00
|
|
|
var gKeywordsCachePromise = Promise.resolve();
|
2017-06-09 16:51:09 +00:00
|
|
|
|
2018-02-21 06:04:59 +00:00
|
|
|
function promiseKeywordsCache() {
|
|
|
|
let promise = gKeywordsCachePromise.then(function(cache) {
|
|
|
|
if (cache) {
|
2015-05-05 10:44:16 +00:00
|
|
|
return cache;
|
2017-05-12 12:42:39 +00:00
|
|
|
}
|
2018-02-21 06:04:59 +00:00
|
|
|
return PlacesUtils.withConnectionWrapper(
|
|
|
|
"PlacesUtils: promiseKeywordsCache",
|
|
|
|
async db => {
|
|
|
|
let cache = new Map();
|
|
|
|
let rows = await db.execute(
|
|
|
|
`SELECT keyword, url, post_data
|
|
|
|
FROM moz_keywords k
|
|
|
|
JOIN moz_places h ON h.id = k.place_id
|
|
|
|
`);
|
|
|
|
let brokenKeywords = [];
|
|
|
|
for (let row of rows) {
|
|
|
|
let keyword = row.getResultByName("keyword");
|
|
|
|
try {
|
|
|
|
let entry = { keyword,
|
|
|
|
url: new URL(row.getResultByName("url")),
|
|
|
|
postData: row.getResultByName("post_data") || null };
|
|
|
|
cache.set(keyword, entry);
|
|
|
|
} catch (ex) {
|
|
|
|
// The url is invalid, don't load the keyword and remove it, or it
|
|
|
|
// would break the whole keywords API.
|
|
|
|
brokenKeywords.push(keyword);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (brokenKeywords.length) {
|
|
|
|
await db.execute(
|
|
|
|
`DELETE FROM moz_keywords
|
|
|
|
WHERE keyword IN (${brokenKeywords.map(JSON.stringify).join(",")})
|
|
|
|
`);
|
|
|
|
}
|
|
|
|
return cache;
|
|
|
|
}
|
|
|
|
);
|
|
|
|
});
|
|
|
|
gKeywordsCachePromise = promise.catch(_ => {});
|
|
|
|
return promise;
|
|
|
|
}
|
2010-06-30 21:24:03 +00:00
|
|
|
|
2014-03-12 12:12:15 +00:00
|
|
|
// Sometime soon, likely as part of the transition to mozIAsyncBookmarks,
|
|
|
|
// itemIds will be deprecated in favour of GUIDs, which play much better
|
|
|
|
// with multiple undo/redo operations. Because these GUIDs are already stored,
|
|
|
|
// and because we don't want to revise the transactions API once more when this
|
|
|
|
// happens, transactions are set to work with GUIDs exclusively, in the sense
|
|
|
|
// that they may never expose itemIds, nor do they accept them as input.
|
|
|
|
// More importantly, transactions which add or remove items guarantee to
|
2014-09-20 07:57:34 +00:00
|
|
|
// restore the GUIDs on undo/redo, so that the following transactions that may
|
2014-03-12 12:12:15 +00:00
|
|
|
// done or undo can assume the items they're interested in are stil accessible
|
|
|
|
// through the same GUID.
|
|
|
|
// The current bookmarks API, however, doesn't expose the necessary means for
|
|
|
|
// working with GUIDs. So, until it does, this helper object accesses the
|
|
|
|
// Places database directly in order to switch between GUIDs and itemIds, and
|
|
|
|
// "restore" GUIDs on items re-created items.
|
2015-09-15 18:19:45 +00:00
|
|
|
var GuidHelper = {
|
2014-09-20 07:57:34 +00:00
|
|
|
// Cache for GUID<->itemId paris.
|
|
|
|
guidsForIds: new Map(),
|
|
|
|
idsForGuids: new Map(),
|
2014-03-12 12:12:15 +00:00
|
|
|
|
2017-05-12 12:42:39 +00:00
|
|
|
async getItemId(aGuid) {
|
2014-09-20 07:57:34 +00:00
|
|
|
let cached = this.idsForGuids.get(aGuid);
|
2014-05-22 16:06:57 +00:00
|
|
|
if (cached !== undefined)
|
|
|
|
return cached;
|
|
|
|
|
2017-05-12 12:42:39 +00:00
|
|
|
let itemId = await PlacesUtils.withConnectionWrapper("GuidHelper.getItemId",
|
|
|
|
async function(db) {
|
|
|
|
let rows = await db.executeCached(
|
2015-08-31 02:17:14 +00:00
|
|
|
"SELECT b.id, b.guid from moz_bookmarks b WHERE b.guid = :guid LIMIT 1",
|
|
|
|
{ guid: aGuid });
|
|
|
|
if (rows.length == 0)
|
|
|
|
throw new Error("no item found for the given GUID");
|
2015-04-10 09:23:07 +00:00
|
|
|
|
2015-08-31 02:17:14 +00:00
|
|
|
return rows[0].getResultByName("id");
|
2017-05-12 12:42:39 +00:00
|
|
|
});
|
2014-05-22 16:06:57 +00:00
|
|
|
|
2015-05-25 15:18:31 +00:00
|
|
|
this.updateCache(itemId, aGuid);
|
2014-05-22 16:06:57 +00:00
|
|
|
return itemId;
|
2017-05-12 12:42:39 +00:00
|
|
|
},
|
2014-05-22 16:06:57 +00:00
|
|
|
|
2017-05-12 12:42:39 +00:00
|
|
|
async getManyItemIds(aGuids) {
|
2017-03-13 23:45:43 +00:00
|
|
|
let uncachedGuids = aGuids.filter(guid => !this.idsForGuids.has(guid));
|
|
|
|
if (uncachedGuids.length) {
|
2017-05-12 12:42:39 +00:00
|
|
|
await PlacesUtils.withConnectionWrapper("GuidHelper.getItemId",
|
2017-05-12 12:47:41 +00:00
|
|
|
async db => {
|
2017-03-13 23:45:43 +00:00
|
|
|
while (uncachedGuids.length) {
|
|
|
|
let chunk = uncachedGuids.splice(0, 100);
|
2017-05-12 12:42:39 +00:00
|
|
|
let rows = await db.executeCached(
|
2017-03-13 23:45:43 +00:00
|
|
|
`SELECT b.id, b.guid from moz_bookmarks b WHERE
|
|
|
|
b.guid IN (${"?,".repeat(chunk.length - 1) + "?"})
|
|
|
|
LIMIT ${chunk.length}`, chunk);
|
|
|
|
if (rows.length < chunk.length)
|
|
|
|
throw new Error("Not all items were found!");
|
|
|
|
for (let row of rows) {
|
|
|
|
this.updateCache(row.getResultByIndex(0), row.getResultByIndex(1));
|
|
|
|
}
|
|
|
|
}
|
2017-05-12 12:47:41 +00:00
|
|
|
});
|
2017-03-13 23:45:43 +00:00
|
|
|
}
|
|
|
|
return new Map(aGuids.map(guid => [guid, this.idsForGuids.get(guid)]));
|
2017-05-12 12:42:39 +00:00
|
|
|
},
|
2017-03-13 23:45:43 +00:00
|
|
|
|
2017-05-12 12:42:39 +00:00
|
|
|
async getItemGuid(aItemId) {
|
2014-09-20 07:57:34 +00:00
|
|
|
let cached = this.guidsForIds.get(aItemId);
|
2014-05-22 16:06:57 +00:00
|
|
|
if (cached !== undefined)
|
|
|
|
return cached;
|
|
|
|
|
2017-05-12 12:42:39 +00:00
|
|
|
let guid = await PlacesUtils.withConnectionWrapper("GuidHelper.getItemGuid",
|
|
|
|
async function(db) {
|
2015-04-10 09:23:07 +00:00
|
|
|
|
2017-05-12 12:42:39 +00:00
|
|
|
let rows = await db.executeCached(
|
2015-08-31 02:17:14 +00:00
|
|
|
"SELECT b.id, b.guid from moz_bookmarks b WHERE b.id = :id LIMIT 1",
|
|
|
|
{ id: aItemId });
|
|
|
|
if (rows.length == 0)
|
|
|
|
throw new Error("no item found for the given itemId");
|
|
|
|
|
|
|
|
return rows[0].getResultByName("guid");
|
2017-05-12 12:42:39 +00:00
|
|
|
});
|
2014-05-22 16:06:57 +00:00
|
|
|
|
2015-05-25 15:18:31 +00:00
|
|
|
this.updateCache(aItemId, guid);
|
2014-05-22 16:06:57 +00:00
|
|
|
return guid;
|
2017-05-12 12:42:39 +00:00
|
|
|
},
|
2014-03-12 12:12:15 +00:00
|
|
|
|
2015-05-25 15:18:31 +00:00
|
|
|
/**
|
|
|
|
* Updates the cache.
|
|
|
|
*
|
|
|
|
* @note This is the only place where the cache should be populated,
|
|
|
|
* invalidation relies on both Maps being populated at the same time.
|
|
|
|
*/
|
|
|
|
updateCache(aItemId, aGuid) {
|
|
|
|
if (typeof(aItemId) != "number" || aItemId <= 0)
|
|
|
|
throw new Error("Trying to update the GUIDs cache with an invalid itemId");
|
2018-02-20 16:31:41 +00:00
|
|
|
if (!PlacesUtils.isValidGuid(aGuid))
|
2015-05-25 15:18:31 +00:00
|
|
|
throw new Error("Trying to update the GUIDs cache with an invalid GUID");
|
2015-08-13 07:52:28 +00:00
|
|
|
this.ensureObservingRemovedItems();
|
2015-05-25 15:18:31 +00:00
|
|
|
this.guidsForIds.set(aItemId, aGuid);
|
|
|
|
this.idsForGuids.set(aGuid, aItemId);
|
|
|
|
},
|
|
|
|
|
|
|
|
invalidateCacheForItemId(aItemId) {
|
|
|
|
let guid = this.guidsForIds.get(aItemId);
|
|
|
|
this.guidsForIds.delete(aItemId);
|
|
|
|
this.idsForGuids.delete(guid);
|
|
|
|
},
|
|
|
|
|
2016-12-29 23:34:54 +00:00
|
|
|
ensureObservingRemovedItems() {
|
2014-03-12 12:12:15 +00:00
|
|
|
if (!("observer" in this)) {
|
|
|
|
/**
|
|
|
|
* This observers serves two purposes:
|
2014-09-20 07:57:34 +00:00
|
|
|
* (1) Invalidate cached id<->GUID paris on when items are removed.
|
2014-03-12 12:12:15 +00:00
|
|
|
* (2) Cache GUIDs given us free of charge by onItemAdded/onItemRemoved.
|
|
|
|
* So, for exmaple, when the NewBookmark needs the new GUID, we already
|
|
|
|
* have it cached.
|
|
|
|
*/
|
|
|
|
this.observer = {
|
|
|
|
onItemAdded: (aItemId, aParentId, aIndex, aItemType, aURI, aTitle,
|
2014-09-20 07:57:34 +00:00
|
|
|
aDateAdded, aGuid, aParentGuid) => {
|
2015-05-25 15:18:31 +00:00
|
|
|
this.updateCache(aItemId, aGuid);
|
|
|
|
this.updateCache(aParentId, aParentGuid);
|
2014-03-12 12:12:15 +00:00
|
|
|
},
|
|
|
|
onItemRemoved:
|
2014-09-20 07:57:34 +00:00
|
|
|
(aItemId, aParentId, aIndex, aItemTyep, aURI, aGuid, aParentGuid) => {
|
|
|
|
this.guidsForIds.delete(aItemId);
|
|
|
|
this.idsForGuids.delete(aGuid);
|
2015-05-25 15:18:31 +00:00
|
|
|
this.updateCache(aParentId, aParentGuid);
|
2014-03-12 12:12:15 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
QueryInterface: XPCOMUtils.generateQI(Ci.nsINavBookmarkObserver),
|
2015-03-07 15:35:07 +00:00
|
|
|
|
2016-12-29 23:34:54 +00:00
|
|
|
onBeginUpdateBatch() {},
|
|
|
|
onEndUpdateBatch() {},
|
|
|
|
onItemChanged() {},
|
|
|
|
onItemVisited() {},
|
|
|
|
onItemMoved() {},
|
2014-03-12 12:12:15 +00:00
|
|
|
};
|
2017-04-14 19:51:38 +00:00
|
|
|
PlacesUtils.bookmarks.addObserver(this.observer);
|
2014-03-12 12:12:15 +00:00
|
|
|
PlacesUtils.registerShutdownFunction(() => {
|
|
|
|
PlacesUtils.bookmarks.removeObserver(this.observer);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2016-08-10 19:54:45 +00:00
|
|
|
/**
|
|
|
|
* Executes a boolean validate function, throwing if it returns false.
|
|
|
|
*
|
|
|
|
* @param boolValidateFn
|
|
|
|
* A boolean validate function.
|
|
|
|
* @return the input value.
|
|
|
|
* @throws if input doesn't pass the validate function.
|
|
|
|
*/
|
|
|
|
function simpleValidateFunc(boolValidateFn) {
|
|
|
|
return (v, input) => {
|
|
|
|
if (!boolValidateFn(v, input))
|
|
|
|
throw new Error("Invalid value");
|
|
|
|
return v;
|
|
|
|
};
|
|
|
|
}
|