2016-02-12 00:37:20 +03:00
|
|
|
/* globals XPCOMUtils, Services, gPrincipal, EventEmitter, PlacesUtils, Task, Bookmarks */
|
2016-02-01 22:02:30 +03:00
|
|
|
|
|
|
|
"use strict";
|
|
|
|
|
2016-02-09 00:01:42 +03:00
|
|
|
const {Ci, Cu} = require("chrome");
|
2016-02-05 22:25:35 +03:00
|
|
|
const base64 = require("sdk/base64");
|
2016-04-28 23:18:14 +03:00
|
|
|
const simplePrefs = require("sdk/simple-prefs");
|
2016-02-01 22:02:30 +03:00
|
|
|
|
|
|
|
Cu.import("resource://gre/modules/XPCOMUtils.jsm");
|
|
|
|
Cu.import("resource://gre/modules/Services.jsm");
|
|
|
|
|
2016-02-09 00:01:42 +03:00
|
|
|
XPCOMUtils.defineLazyGetter(this, "EventEmitter", function() {
|
2016-02-01 22:02:30 +03:00
|
|
|
const {EventEmitter} = Cu.import("resource://devtools/shared/event-emitter.js", {});
|
|
|
|
return EventEmitter;
|
|
|
|
});
|
|
|
|
|
|
|
|
XPCOMUtils.defineLazyModuleGetter(this, "Task",
|
|
|
|
"resource://gre/modules/Task.jsm");
|
|
|
|
|
|
|
|
XPCOMUtils.defineLazyModuleGetter(this, "PlacesUtils",
|
|
|
|
"resource://gre/modules/PlacesUtils.jsm");
|
|
|
|
|
2016-02-12 00:37:20 +03:00
|
|
|
XPCOMUtils.defineLazyModuleGetter(this, "Bookmarks",
|
|
|
|
"resource://gre/modules/Bookmarks.jsm");
|
|
|
|
|
2016-02-09 00:01:42 +03:00
|
|
|
XPCOMUtils.defineLazyGetter(this, "gPrincipal", function() {
|
2016-02-01 22:02:30 +03:00
|
|
|
let uri = Services.io.newURI("about:newtab", null, null);
|
|
|
|
return Services.scriptSecurityManager.getNoAppCodebasePrincipal(uri);
|
|
|
|
});
|
|
|
|
|
2016-05-24 22:42:02 +03:00
|
|
|
const {
|
|
|
|
LINKS_QUERY_LIMIT,
|
|
|
|
FRECENT_RESULTS_TIME_LIMIT,
|
|
|
|
HIGHLIGHTS_THRESHOLDS
|
|
|
|
} = require("../common/constants");
|
2016-02-25 04:36:32 +03:00
|
|
|
|
2016-04-15 02:52:16 +03:00
|
|
|
const REV_HOST_BLACKLIST = [
|
|
|
|
"moc.elgoog.www.",
|
|
|
|
"ac.elgoog.www.",
|
|
|
|
"moc.elgoog.radnelac.",
|
|
|
|
"moc.elgoog.liam.",
|
|
|
|
"moc.oohay.liam.",
|
|
|
|
"moc.oohay.hcraes.",
|
|
|
|
"tsohlacol.",
|
|
|
|
"oc.t.",
|
|
|
|
".",
|
|
|
|
].map(item => `'${item}'`);
|
|
|
|
|
2016-04-28 23:18:14 +03:00
|
|
|
const PREF_BLOCKED_URLS = "query.blockedURLs";
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Singleton that manages the list of blocked urls
|
|
|
|
*/
|
|
|
|
class BlockedURLs extends Set {
|
|
|
|
|
|
|
|
constructor(prefName = PREF_BLOCKED_URLS) {
|
|
|
|
let urls = [];
|
|
|
|
try {
|
|
|
|
urls = JSON.parse(simplePrefs.prefs[prefName]);
|
|
|
|
if (typeof urls[Symbol.iterator] !== "function") {
|
|
|
|
urls = [];
|
|
|
|
simplePrefs.prefs[prefName] = "[]";
|
|
|
|
}
|
|
|
|
} catch (e) {
|
|
|
|
Cu.reportError(e);
|
|
|
|
simplePrefs.prefs[prefName] = "[]";
|
|
|
|
}
|
|
|
|
|
|
|
|
super(urls);
|
|
|
|
|
|
|
|
this._prefName = prefName;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Add url and persist to pref
|
2016-04-29 00:11:28 +03:00
|
|
|
*
|
|
|
|
* @param {String} url a url to block
|
|
|
|
* @returns {Boolean} true if the item has been added
|
2016-04-28 23:18:14 +03:00
|
|
|
*/
|
|
|
|
save(url) {
|
2016-04-29 00:11:28 +03:00
|
|
|
if (!this.has(url)) {
|
|
|
|
this.add(url);
|
2016-04-28 23:18:14 +03:00
|
|
|
simplePrefs.prefs[this._prefName] = JSON.stringify(this.items());
|
2016-04-29 00:11:28 +03:00
|
|
|
return true;
|
2016-04-28 23:18:14 +03:00
|
|
|
}
|
2016-04-29 00:11:28 +03:00
|
|
|
return false;
|
2016-04-28 23:18:14 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Remove a url and persist to pref
|
2016-04-29 00:11:28 +03:00
|
|
|
*
|
|
|
|
* @param {String} url a url to unblock
|
|
|
|
* @returns {Boolean} true if the item has been remove
|
2016-04-28 23:18:14 +03:00
|
|
|
*/
|
|
|
|
remove(url) {
|
2016-04-29 00:11:28 +03:00
|
|
|
if (this.has(url)) {
|
|
|
|
this.delete(url);
|
2016-04-28 23:18:14 +03:00
|
|
|
simplePrefs.prefs[this._prefName] = JSON.stringify(this.items());
|
2016-04-29 00:11:28 +03:00
|
|
|
return true;
|
2016-04-28 23:18:14 +03:00
|
|
|
}
|
2016-04-29 00:11:28 +03:00
|
|
|
return false;
|
2016-04-28 23:18:14 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Clear blocked url set and persist
|
2016-04-29 00:11:28 +03:00
|
|
|
*
|
|
|
|
* @returns {Boolean} whether or not blocklist was cleared
|
2016-04-28 23:18:14 +03:00
|
|
|
*/
|
|
|
|
clear() {
|
|
|
|
if (this.size !== 0) {
|
|
|
|
simplePrefs.prefs[this._prefName] = "[]";
|
|
|
|
super.clear();
|
2016-04-29 00:11:28 +03:00
|
|
|
return true;
|
2016-04-28 23:18:14 +03:00
|
|
|
}
|
2016-04-29 00:11:28 +03:00
|
|
|
return false;
|
2016-04-28 23:18:14 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return url set as an array ordered by insertion time
|
|
|
|
*/
|
|
|
|
items() {
|
|
|
|
return [...this];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-02-01 22:02:30 +03:00
|
|
|
/**
|
|
|
|
* Singleton that checks if a given link should be displayed on about:newtab
|
|
|
|
* or if we should rather not do it for security reasons. URIs that inherit
|
|
|
|
* their caller's principal will be filtered.
|
|
|
|
*/
|
|
|
|
let LinkChecker = {
|
|
|
|
_cache: new Map(),
|
|
|
|
|
|
|
|
get flags() {
|
|
|
|
return Ci.nsIScriptSecurityManager.DISALLOW_INHERIT_PRINCIPAL |
|
|
|
|
Ci.nsIScriptSecurityManager.DONT_REPORT_ERRORS;
|
|
|
|
},
|
|
|
|
|
|
|
|
checkLoadURI: function LinkChecker_checkLoadURI(aURI) {
|
|
|
|
if (!this._cache.has(aURI)) {
|
|
|
|
this._cache.set(aURI, this._doCheckLoadURI(aURI));
|
|
|
|
}
|
|
|
|
|
|
|
|
return this._cache.get(aURI);
|
|
|
|
},
|
|
|
|
|
|
|
|
_doCheckLoadURI: function LinkChecker_doCheckLoadURI(aURI) {
|
|
|
|
let result = false;
|
2016-03-01 22:28:22 +03:00
|
|
|
// check for 'place:' protocol
|
|
|
|
if (aURI.startsWith("place:")) {
|
|
|
|
return false;
|
|
|
|
}
|
2016-02-01 22:02:30 +03:00
|
|
|
try {
|
|
|
|
Services.scriptSecurityManager.
|
|
|
|
checkLoadURIStrWithPrincipal(gPrincipal, aURI, this.flags);
|
|
|
|
result = true;
|
|
|
|
} catch (e) {
|
|
|
|
// We got a weird URI or one that would inherit the caller's principal.
|
|
|
|
Cu.reportError(e);
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/* Queries history to retrieve the most visited sites. Emits events when the
|
|
|
|
* history changes.
|
|
|
|
* Implements the EventEmitter interface.
|
|
|
|
*/
|
|
|
|
let Links = function Links() {
|
|
|
|
EventEmitter.decorate(this);
|
2016-04-28 23:18:14 +03:00
|
|
|
this.blockedURLs = new BlockedURLs();
|
2016-02-01 22:02:30 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
Links.prototype = {
|
|
|
|
/**
|
|
|
|
* A set of functions called by @mozilla.org/browser/nav-historyservice
|
|
|
|
* All history events are emitted from this object.
|
|
|
|
*/
|
|
|
|
historyObserver: {
|
|
|
|
onDeleteURI: function historyObserver_onDeleteURI(aURI) {
|
2016-04-28 23:18:14 +03:00
|
|
|
// let observers remove sensitive data associated with deleted visit
|
2016-02-01 22:02:30 +03:00
|
|
|
gLinks.emit("deleteURI", {
|
|
|
|
url: aURI.spec,
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
onClearHistory: function historyObserver_onClearHistory() {
|
|
|
|
gLinks.emit("clearHistory");
|
|
|
|
},
|
|
|
|
|
|
|
|
onFrecencyChanged: function historyObserver_onFrecencyChanged(aURI,
|
2016-02-29 23:36:37 +03:00
|
|
|
aNewFrecency, aGUID, aHidden, aLastVisitDate) {
|
2016-02-01 22:02:30 +03:00
|
|
|
// The implementation of the query in getLinks excludes hidden and
|
|
|
|
// unvisited pages, so it's important to exclude them here, too.
|
|
|
|
if (!aHidden && aLastVisitDate) {
|
|
|
|
gLinks.emit("linkChanged", {
|
|
|
|
url: aURI.spec,
|
|
|
|
frecency: aNewFrecency,
|
|
|
|
lastVisitDate: aLastVisitDate,
|
|
|
|
type: "history",
|
|
|
|
});
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
onManyFrecenciesChanged: function historyObserver_onManyFrecenciesChanged() {
|
|
|
|
// Called when frecencies are invalidated and also when clearHistory is called
|
|
|
|
// See toolkit/components/places/tests/unit/test_frecency_observers.js
|
|
|
|
gLinks.emit("manyLinksChanged");
|
|
|
|
},
|
|
|
|
|
|
|
|
onTitleChanged: function historyObserver_onTitleChanged(aURI, aNewTitle) {
|
|
|
|
gLinks.emit("linkChanged", {
|
|
|
|
url: aURI.spec,
|
|
|
|
title: aNewTitle
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
QueryInterface: XPCOMUtils.generateQI([Ci.nsINavHistoryObserver,
|
2016-02-12 00:37:20 +03:00
|
|
|
Ci.nsISupportsWeakReference])
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* A set of functions called by @mozilla.org/browser/nav-bookmarks-service
|
|
|
|
* All bookmark events are emitted from this object.
|
|
|
|
*/
|
|
|
|
bookmarksObserver: {
|
|
|
|
onItemAdded(id, folderId, index, type) {
|
|
|
|
if (type === Bookmarks.TYPE_BOOKMARK) {
|
|
|
|
gLinks.getBookmark({id}).then(bookmark => {
|
|
|
|
gLinks.emit("bookmarkAdded", bookmark);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
onItemRemoved(id, folderId, index, type, uri) {
|
|
|
|
if (type === Bookmarks.TYPE_BOOKMARK) {
|
|
|
|
gLinks.emit("bookmarkRemoved", {bookmarkId: id, url: uri.spec});
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
onItemChanged(id, property, isAnnotation, value, lastModified, type) {
|
|
|
|
if (type === Bookmarks.TYPE_BOOKMARK) {
|
|
|
|
gLinks.getBookmark({id}).then(bookmark => {
|
|
|
|
gLinks.emit("bookmarkChanged", bookmark);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
QueryInterface: XPCOMUtils.generateQI([Ci.nsINavBookmarkObserver,
|
|
|
|
Ci.nsISupportsWeakReference])
|
2016-02-01 22:02:30 +03:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Must be called before the provider is used.
|
|
|
|
* Makes it easy to disable under pref
|
|
|
|
*/
|
|
|
|
init: function PlacesProvider_init() {
|
|
|
|
PlacesUtils.history.addObserver(this.historyObserver, true);
|
2016-02-12 00:37:20 +03:00
|
|
|
PlacesUtils.bookmarks.addObserver(this.bookmarksObserver, true);
|
2016-02-01 22:02:30 +03:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Must be called before the provider is unloaded.
|
|
|
|
*/
|
|
|
|
uninit: function PlacesProvider_uninit() {
|
|
|
|
PlacesUtils.history.removeObserver(this.historyObserver);
|
2016-02-12 00:37:20 +03:00
|
|
|
PlacesUtils.bookmarks.removeObserver(this.bookmarksObserver);
|
2016-02-01 22:02:30 +03:00
|
|
|
},
|
|
|
|
|
2016-05-12 01:00:48 +03:00
|
|
|
/**
|
|
|
|
* Removes a bookmark
|
|
|
|
*
|
|
|
|
* @param {String} bookmarkGuid the bookmark guid
|
|
|
|
* @returns {Promise} Returns a promise set to an object representing the removed bookmark
|
|
|
|
*/
|
|
|
|
asyncDeleteBookmark: function PlacesProvider_asyncDeleteBookmark(bookmarkGuid) {
|
|
|
|
return Bookmarks.remove(bookmarkGuid);
|
|
|
|
},
|
|
|
|
|
2016-06-07 18:03:54 +03:00
|
|
|
/**
|
|
|
|
* Adds a bookmark
|
|
|
|
*
|
|
|
|
* @param {String} url the url to bookmark
|
|
|
|
* @returns {Promise} Returns a promise set to an object representing the bookmark
|
|
|
|
*/
|
|
|
|
asyncAddBookmark: function PlacesProvider_asyncAddBookmark(url) {
|
|
|
|
return Bookmarks.insert({url, type: Bookmarks.TYPE_BOOKMARK, parentGuid: Bookmarks.unfiledGuid});
|
|
|
|
},
|
|
|
|
|
2016-02-22 20:52:58 +03:00
|
|
|
/**
|
|
|
|
* Removes a history link
|
|
|
|
*
|
|
|
|
* @param {String} url
|
|
|
|
* @returns {Promise} Returns a promise set to true if link was removed
|
|
|
|
*/
|
|
|
|
deleteHistoryLink: function PlacesProvider_deleteHistoryLink(url) {
|
|
|
|
return PlacesUtils.history.remove(url);
|
|
|
|
},
|
|
|
|
|
2016-04-29 00:11:28 +03:00
|
|
|
/**
|
|
|
|
* Blocks a URL
|
|
|
|
*/
|
|
|
|
blockURL(url) {
|
|
|
|
if (this.blockedURLs.save(url)) {
|
|
|
|
this.emit("linkChanged", {url, blocked: true});
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Unblocks a URL
|
|
|
|
*/
|
|
|
|
unblockURL(url) {
|
|
|
|
if (this.blockedURLs.remove(url)) {
|
|
|
|
this.emit("linkChanged", {url, blocked: false});
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Unblocks all URLs
|
|
|
|
*/
|
|
|
|
unblockAll(url) {
|
|
|
|
if (this.blockedURLs.clear()) {
|
|
|
|
this.emit("manyLinksChanged");
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2016-02-13 01:21:54 +03:00
|
|
|
/**
|
2016-02-25 04:36:32 +03:00
|
|
|
* Gets links from history based on supplied options
|
2016-02-13 01:21:54 +03:00
|
|
|
*
|
2016-02-25 04:36:32 +03:00
|
|
|
* @param {Object} options
|
2016-02-25 20:16:12 +03:00
|
|
|
* {Integer} limit: Maximum number of results to return. Max 20
|
2016-03-01 04:48:07 +03:00
|
|
|
* {Milliseconds} beforeDate: Only return links older than beforeDate
|
|
|
|
* {Milliseconds} afterDate: Only return links fresher than afterDate
|
2016-03-09 00:03:30 +03:00
|
|
|
* {String} order: if order=="bookmarksFrecency", orders by bookmarks and frecency, otherwise by recency
|
2016-02-13 01:21:54 +03:00
|
|
|
*
|
|
|
|
* @returns {Promise} Returns a promise with the array of links as payload.
|
|
|
|
*/
|
2016-04-15 01:42:24 +03:00
|
|
|
_getHistoryLinks: Task.async(function*(options = {}) {
|
2016-02-13 01:21:54 +03:00
|
|
|
|
2016-04-28 23:18:14 +03:00
|
|
|
let {limit, afterDate, beforeDate, order, ignoreBlocked} = options;
|
2016-05-24 22:42:02 +03:00
|
|
|
if (!limit || limit.options > LINKS_QUERY_LIMIT) {
|
|
|
|
limit = LINKS_QUERY_LIMIT;
|
2016-02-13 01:21:54 +03:00
|
|
|
}
|
|
|
|
|
2016-04-28 23:18:14 +03:00
|
|
|
let blockedURLs = ignoreBlocked ? [] : this.blockedURLs.items().map(item => `"${item}"`);
|
|
|
|
|
2016-02-25 04:36:32 +03:00
|
|
|
// setup binding parameters
|
|
|
|
let params = {limit: limit};
|
|
|
|
|
2016-03-01 04:48:07 +03:00
|
|
|
// setup afterDate binding and sql clause
|
|
|
|
let afterDateClause = "";
|
|
|
|
if (afterDate) {
|
|
|
|
afterDateClause = "AND moz_places.last_visit_date > :afterDate * 1000";
|
|
|
|
params.afterDate = afterDate;
|
|
|
|
}
|
|
|
|
|
|
|
|
// setup beforeDate binding and sql clause
|
|
|
|
let beforeDateClause = "";
|
|
|
|
if (beforeDate) {
|
|
|
|
beforeDateClause = "AND moz_places.last_visit_date < :beforeDate * 1000";
|
|
|
|
params.beforeDate = beforeDate;
|
2016-02-25 04:36:32 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// setup order by clause
|
2016-03-09 00:03:30 +03:00
|
|
|
let orderbyClause = (order === "bookmarksFrecency") ?
|
|
|
|
"ORDER BY bookmarkDateCreated DESC, frecency DESC, lastVisitDate DESC, url" :
|
2016-02-25 04:36:32 +03:00
|
|
|
"ORDER BY lastVisitDate DESC, frecency DESC, url" ;
|
|
|
|
|
|
|
|
// construct sql query
|
2016-02-24 00:49:09 +03:00
|
|
|
let sqlQuery = `SELECT moz_places.url as url,
|
2016-05-19 02:46:14 +03:00
|
|
|
moz_places.guid as guid,
|
2016-02-24 00:49:09 +03:00
|
|
|
moz_favicons.data as favicon,
|
2016-02-25 03:02:05 +03:00
|
|
|
moz_favicons.mime_type as mimeType,
|
2016-02-24 00:49:09 +03:00
|
|
|
moz_places.title,
|
2016-02-25 03:02:05 +03:00
|
|
|
moz_places.frecency,
|
2016-02-25 20:44:06 +03:00
|
|
|
moz_places.last_visit_date / 1000 as lastVisitDate,
|
2016-02-24 00:49:09 +03:00
|
|
|
"history" as type,
|
2016-02-25 03:02:05 +03:00
|
|
|
moz_bookmarks.guid as bookmarkGuid,
|
|
|
|
moz_bookmarks.dateAdded / 1000 as bookmarkDateCreated
|
2016-02-13 01:21:54 +03:00
|
|
|
FROM moz_places
|
|
|
|
LEFT JOIN moz_favicons
|
2016-02-25 03:02:05 +03:00
|
|
|
ON moz_places.favicon_id = moz_favicons.id
|
2016-02-24 00:49:09 +03:00
|
|
|
LEFT JOIN moz_bookmarks
|
|
|
|
ON moz_places.id = moz_bookmarks.fk
|
2016-02-13 01:21:54 +03:00
|
|
|
WHERE hidden = 0 AND last_visit_date NOTNULL
|
2016-04-28 23:18:14 +03:00
|
|
|
AND moz_places.url NOT IN (${blockedURLs})
|
2016-03-01 04:48:07 +03:00
|
|
|
${afterDateClause}
|
|
|
|
${beforeDateClause}
|
2016-02-25 04:36:32 +03:00
|
|
|
${orderbyClause}
|
2016-02-13 01:21:54 +03:00
|
|
|
LIMIT :limit`;
|
|
|
|
|
|
|
|
let links = yield this.executePlacesQuery(sqlQuery, {
|
2016-05-19 02:46:14 +03:00
|
|
|
columns: ["url", "guid", "favicon", "mimeType", "title", "lastVisitDate",
|
2016-02-25 03:02:05 +03:00
|
|
|
"frecency", "type", "bookmarkGuid", "bookmarkDateCreated"],
|
2016-02-25 04:36:32 +03:00
|
|
|
params: params
|
2016-02-13 01:21:54 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
links = this._faviconBytesToDataURI(links);
|
|
|
|
return links.filter(link => LinkChecker.checkLoadURI(link.url));
|
|
|
|
}),
|
|
|
|
|
2016-02-25 04:36:32 +03:00
|
|
|
/**
|
|
|
|
* Gets the top recent links
|
|
|
|
*
|
|
|
|
* @param {Integer} options
|
2016-02-25 20:16:12 +03:00
|
|
|
* limit: Maximum number of results to return. Max 20.
|
2016-02-25 04:36:32 +03:00
|
|
|
*
|
|
|
|
* @returns {Promise} Returns a promise with the array of links as payload.
|
|
|
|
*/
|
|
|
|
getRecentLinks: function PlacesProvider_getRecentLinks(options) {
|
|
|
|
return this._getHistoryLinks(options);
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets the top frecent links - visited in last 72 hours and ordered by frecency
|
|
|
|
*
|
|
|
|
* @param {Integer} options
|
2016-02-25 20:16:12 +03:00
|
|
|
* limit: Maximum number of results to return. Max 20.
|
2016-02-25 04:36:32 +03:00
|
|
|
*
|
|
|
|
* @returns {Promise} Returns a promise with the array of links as payload.
|
|
|
|
*/
|
2016-04-15 01:42:24 +03:00
|
|
|
getFrecentLinks: function PlacesProvider_getFrecentLinks(options = {}) {
|
2016-03-09 00:03:30 +03:00
|
|
|
options.order = "bookmarksFrecency";
|
2016-03-01 04:48:07 +03:00
|
|
|
options.afterDate = (new Date()).getTime() - FRECENT_RESULTS_TIME_LIMIT;
|
2016-02-25 04:36:32 +03:00
|
|
|
return this._getHistoryLinks(options);
|
|
|
|
},
|
|
|
|
|
2016-02-01 22:02:30 +03:00
|
|
|
/**
|
|
|
|
* Gets the top frecent sites.
|
|
|
|
*
|
2016-02-12 00:37:20 +03:00
|
|
|
* @param {Object} options
|
2016-02-25 20:16:12 +03:00
|
|
|
* options.limit: Maximum number of results to return. Max 20.
|
2016-02-01 22:02:30 +03:00
|
|
|
*
|
|
|
|
* @returns {Promise} Returns a promise with the array of links as payload.
|
|
|
|
*/
|
2016-04-15 01:42:24 +03:00
|
|
|
getTopFrecentSites: Task.async(function*(options = {}) {
|
2016-02-01 22:02:30 +03:00
|
|
|
|
2016-04-28 23:18:14 +03:00
|
|
|
let {limit, ignoreBlocked} = options;
|
2016-05-24 22:42:02 +03:00
|
|
|
if (!limit || limit.options > LINKS_QUERY_LIMIT) {
|
|
|
|
limit = LINKS_QUERY_LIMIT;
|
2016-02-01 22:02:30 +03:00
|
|
|
}
|
|
|
|
|
2016-04-28 23:18:14 +03:00
|
|
|
let blockedURLs = ignoreBlocked ? [] : this.blockedURLs.items().map(item => `"${item}"`);
|
|
|
|
|
2016-02-01 22:02:30 +03:00
|
|
|
// this query does "GROUP BY rev_host" to remove urls from same domain.
|
|
|
|
// Note that unlike mysql, sqlite picks the last raw from groupby bucket.
|
|
|
|
// Which is why subselect orders frecency and last_visit_date backwards.
|
|
|
|
// In general the groupby behavior in the absence of aggregates is not
|
|
|
|
// defined in SQL, hence we are relying on sqlite implementation that may
|
|
|
|
// change in the future.
|
2016-05-19 02:46:14 +03:00
|
|
|
let sqlQuery = `SELECT url, title, frecency, guid,
|
2016-02-25 20:44:06 +03:00
|
|
|
last_visit_date / 1000 as lastVisitDate, favicon, mimeType,
|
2016-02-12 00:37:20 +03:00
|
|
|
"history" as type
|
|
|
|
FROM
|
|
|
|
(
|
2016-05-19 02:46:14 +03:00
|
|
|
SELECT rev_host, moz_places.url, moz_favicons.data as favicon, mime_type as mimeType, title, frecency, last_visit_date, moz_places.guid as guid
|
2016-02-12 00:37:20 +03:00
|
|
|
FROM moz_places
|
|
|
|
LEFT JOIN moz_favicons
|
|
|
|
ON favicon_id = moz_favicons.id
|
|
|
|
WHERE hidden = 0 AND last_visit_date NOTNULL
|
2016-04-28 23:18:14 +03:00
|
|
|
AND moz_places.url NOT IN (${blockedURLs})
|
2016-02-12 00:37:20 +03:00
|
|
|
ORDER BY rev_host, frecency, last_visit_date, moz_places.url DESC
|
|
|
|
)
|
|
|
|
GROUP BY rev_host
|
|
|
|
ORDER BY frecency DESC, lastVisitDate DESC, url
|
|
|
|
LIMIT :limit`;
|
|
|
|
|
|
|
|
let links = yield this.executePlacesQuery(sqlQuery, {
|
2016-05-19 02:46:14 +03:00
|
|
|
columns: ["url", "guid", "title", "lastVisitDate", "frecency", "favicon", "mimeType", "type"],
|
2016-02-12 00:37:20 +03:00
|
|
|
params: {limit}
|
|
|
|
});
|
|
|
|
|
|
|
|
links = this._faviconBytesToDataURI(links);
|
|
|
|
return links.filter(link => LinkChecker.checkLoadURI(link.url));
|
|
|
|
}),
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets the most recent bookmarks
|
|
|
|
*
|
|
|
|
* @param {Object} options
|
2016-02-25 20:16:12 +03:00
|
|
|
* options.limit: Maximum number of results to return. Max 20.
|
2016-02-12 00:37:20 +03:00
|
|
|
*
|
|
|
|
* @returns {Promise} Returns a promise with the array of links as payload.
|
|
|
|
*/
|
2016-04-15 01:42:24 +03:00
|
|
|
getRecentBookmarks: Task.async(function*(options = {}) {
|
2016-04-28 23:18:14 +03:00
|
|
|
let {limit, afterDate, beforeDate, ignoreBlocked} = options;
|
2016-05-24 22:42:02 +03:00
|
|
|
if (!limit || limit.options > LINKS_QUERY_LIMIT) {
|
|
|
|
limit = LINKS_QUERY_LIMIT;
|
2016-02-12 00:37:20 +03:00
|
|
|
}
|
|
|
|
|
2016-04-28 23:18:14 +03:00
|
|
|
let blockedURLs = ignoreBlocked ? [] : this.blockedURLs.items().map(item => `"${item}"`);
|
|
|
|
|
2016-03-18 00:13:01 +03:00
|
|
|
// setup binding parameters
|
|
|
|
let params = {limit: limit, type: Bookmarks.TYPE_BOOKMARK};
|
|
|
|
|
|
|
|
// setup afterDate binding and sql clause
|
|
|
|
let afterDateClause = "";
|
|
|
|
if (afterDate) {
|
|
|
|
afterDateClause = "AND b.lastModified > :afterDate * 1000";
|
|
|
|
params.afterDate = afterDate;
|
|
|
|
}
|
|
|
|
|
|
|
|
// setup beforeDate binding and sql clause
|
|
|
|
let beforeDateClause = "";
|
|
|
|
if (beforeDate) {
|
|
|
|
beforeDateClause = "AND b.lastModified < :beforeDate * 1000";
|
|
|
|
params.beforeDate = beforeDate;
|
|
|
|
}
|
|
|
|
|
2016-05-19 02:46:14 +03:00
|
|
|
let sqlQuery = `SELECT p.url, p.title, p.frecency, p.guid,
|
2016-02-25 20:44:06 +03:00
|
|
|
p.last_visit_date / 1000 as lastVisitDate,
|
2016-02-25 21:04:54 +03:00
|
|
|
b.lastModified / 1000 as lastModified,
|
2016-03-01 01:02:48 +03:00
|
|
|
b.dateAdded / 1000 as bookmarkDateCreated,
|
2016-02-12 00:37:20 +03:00
|
|
|
b.id as bookmarkId,
|
|
|
|
b.title as bookmarkTitle,
|
|
|
|
b.guid as bookmarkGuid,
|
|
|
|
"bookmark" as type,
|
|
|
|
f.data as favicon,
|
|
|
|
f.mime_type as mimeType
|
|
|
|
FROM moz_places p, moz_bookmarks b
|
|
|
|
LEFT JOIN moz_favicons f
|
|
|
|
ON p.favicon_id = f.id
|
|
|
|
WHERE b.fk = p.id
|
2016-04-28 23:18:14 +03:00
|
|
|
AND p.url NOT IN (${blockedURLs})
|
2016-02-12 00:37:20 +03:00
|
|
|
AND p.rev_host IS NOT NULL
|
|
|
|
AND b.type = :type
|
2016-03-18 00:13:01 +03:00
|
|
|
${afterDateClause}
|
|
|
|
${beforeDateClause}
|
2016-05-03 22:46:02 +03:00
|
|
|
ORDER BY bookmarkDateCreated DESC, b.lastModified DESC, lastVisitDate DESC, b.id DESC
|
2016-02-12 00:37:20 +03:00
|
|
|
LIMIT :limit`;
|
|
|
|
|
|
|
|
let links = yield this.executePlacesQuery(sqlQuery, {
|
2016-05-19 02:46:14 +03:00
|
|
|
columns: ["bookmarkId", "bookmarkTitle", "bookmarkGuid", "bookmarkDateCreated", "url", "guid",
|
2016-03-18 00:13:01 +03:00
|
|
|
"title", "lastVisitDate", "frecency", "type", "lastModified", "favicon", "mimeType"],
|
|
|
|
params: params,
|
2016-02-12 00:37:20 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
links = this._faviconBytesToDataURI(links);
|
|
|
|
return links.filter(link => LinkChecker.checkLoadURI(link.url));
|
|
|
|
}),
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets a specific bookmark given an id
|
|
|
|
*
|
|
|
|
* @param {Object} options
|
|
|
|
* options.id: bookmark ID
|
|
|
|
*/
|
2016-04-15 01:42:24 +03:00
|
|
|
getBookmark: Task.async(function*(options = {}) {
|
2016-02-12 00:37:20 +03:00
|
|
|
let {id} = options;
|
|
|
|
|
2016-05-19 02:46:14 +03:00
|
|
|
let sqlQuery = `SELECT p.url, p.title, p.frecency, p.guid,
|
2016-02-25 20:44:06 +03:00
|
|
|
p.last_visit_date / 1000 as lastVisitDate,
|
2016-02-25 21:04:54 +03:00
|
|
|
b.lastModified / 1000 as lastModified,
|
2016-02-12 00:37:20 +03:00
|
|
|
b.id as bookmarkId,
|
|
|
|
b.title as bookmarkTitle,
|
|
|
|
b.guid as bookmarkGuid,
|
|
|
|
"bookmark" as type,
|
|
|
|
f.data as favicon,
|
|
|
|
f.mime_type as mimeType
|
|
|
|
FROM moz_places p, moz_bookmarks b
|
|
|
|
LEFT JOIN moz_favicons f
|
|
|
|
ON p.favicon_id = f.id
|
|
|
|
WHERE b.fk = p.id
|
|
|
|
AND p.rev_host IS NOT NULL
|
|
|
|
AND b.type = :type
|
|
|
|
AND b.id = :id
|
|
|
|
ORDER BY b.lastModified, lastVisitDate DESC, b.id`;
|
2016-02-01 22:02:30 +03:00
|
|
|
|
|
|
|
let links = yield this.executePlacesQuery(sqlQuery, {
|
2016-05-19 02:46:14 +03:00
|
|
|
columns: ["bookmarkId", "bookmarkTitle", "bookmarkGuid", "url", "guid", "title", "lastVisitDate", "frecency", "type", "lastModified", "favicon", "mimeType"],
|
2016-02-12 00:37:20 +03:00
|
|
|
params: {id, type: Bookmarks.TYPE_BOOKMARK}
|
2016-02-01 22:02:30 +03:00
|
|
|
});
|
|
|
|
|
2016-02-12 00:37:20 +03:00
|
|
|
links = this._faviconBytesToDataURI(links);
|
|
|
|
links.filter(link => LinkChecker.checkLoadURI(link.url));
|
|
|
|
if (links.length) {
|
|
|
|
return links[0];
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}),
|
2016-04-15 02:52:16 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Obtain a set of links for highlights
|
|
|
|
*
|
|
|
|
* @param {Object} options
|
|
|
|
* {Integer} limit: Maximum number of results to return. Max 20
|
|
|
|
*
|
|
|
|
* @returns {Promise} Returns a promise with the array of links as payload.
|
|
|
|
*/
|
|
|
|
getHighlightsLinks: Task.async(function*(options = {}) {
|
|
|
|
|
2016-04-28 23:18:14 +03:00
|
|
|
let {limit, ignoreBlocked} = options;
|
2016-05-24 22:42:02 +03:00
|
|
|
if (!limit || limit.options > LINKS_QUERY_LIMIT) {
|
|
|
|
limit = LINKS_QUERY_LIMIT;
|
2016-04-15 02:52:16 +03:00
|
|
|
}
|
|
|
|
|
2016-04-28 23:18:14 +03:00
|
|
|
let blockedURLs = ignoreBlocked ? [] : this.blockedURLs.items().map(item => `"${item}"`);
|
|
|
|
|
2016-04-15 02:52:16 +03:00
|
|
|
let params = {limitBookmarks: 1, limitHistory: (limit - 1)};
|
|
|
|
|
|
|
|
let sqlQuery = `SELECT DISTINCT * FROM (
|
|
|
|
SELECT * FROM (
|
|
|
|
SELECT p.url as url,
|
2016-05-19 02:46:14 +03:00
|
|
|
p.guid as guid,
|
2016-04-15 02:52:16 +03:00
|
|
|
f.data as favicon,
|
|
|
|
f.mime_type as mimeType,
|
|
|
|
p.title as title,
|
|
|
|
p.frecency as frecency,
|
|
|
|
p.last_visit_date / 1000 as lastVisitDate,
|
|
|
|
"bookmark" as type,
|
|
|
|
b.id as bookmarkId,
|
|
|
|
b.guid as bookmarkGuid,
|
|
|
|
b.title as bookmarkTitle,
|
|
|
|
b.lastModified / 1000 as lastModified,
|
|
|
|
b.dateAdded / 1000 as bookmarkDateCreated
|
|
|
|
FROM moz_places p
|
|
|
|
INNER JOIN moz_bookmarks b
|
|
|
|
ON b.fk = p.id
|
|
|
|
LEFT JOIN moz_favicons f
|
|
|
|
ON p.favicon_id = f.id
|
|
|
|
WHERE date(b.dateAdded / 1000 / 1000, 'unixepoch') > date('now', '${HIGHLIGHTS_THRESHOLDS.created}')
|
2016-04-28 23:18:14 +03:00
|
|
|
AND p.url NOT IN (${blockedURLs})
|
2016-04-15 02:52:16 +03:00
|
|
|
AND p.visit_count <= 3
|
|
|
|
ORDER BY b.dateAdded DESC
|
|
|
|
LIMIT :limitBookmarks
|
|
|
|
)
|
|
|
|
|
|
|
|
UNION ALL
|
|
|
|
|
|
|
|
SELECT * FROM (
|
|
|
|
SELECT p.url as url,
|
2016-05-19 02:46:14 +03:00
|
|
|
p.guid as guid,
|
2016-04-15 02:52:16 +03:00
|
|
|
f.data as favicon,
|
|
|
|
f.mime_type as mimeType,
|
|
|
|
p.title as title,
|
|
|
|
p.frecency as frecency,
|
|
|
|
p.last_visit_date / 1000 as lastVisitDate,
|
|
|
|
"history" as type,
|
|
|
|
b.id as bookmarkId,
|
|
|
|
b.guid as bookmarkGuid,
|
|
|
|
b.title as bookmarkTitle,
|
|
|
|
b.lastModified / 1000 as lastModified,
|
|
|
|
b.dateAdded / 1000 as bookmarkDateCreated
|
|
|
|
FROM moz_places p
|
|
|
|
LEFT JOIN moz_bookmarks b
|
|
|
|
ON b.fk = p.id
|
|
|
|
LEFT JOIN moz_favicons f
|
|
|
|
ON p.favicon_id = f.id
|
|
|
|
WHERE datetime(p.last_visit_date / 1000 / 1000, 'unixepoch') < datetime('now', '${HIGHLIGHTS_THRESHOLDS.visited}')
|
2016-04-28 23:18:14 +03:00
|
|
|
AND p.url NOT IN (${blockedURLs})
|
2016-04-15 02:52:16 +03:00
|
|
|
AND p.visit_count <= 3
|
|
|
|
AND p.title NOT NULL
|
|
|
|
AND p.rev_host NOT IN (${REV_HOST_BLACKLIST})
|
|
|
|
GROUP BY p.rev_host
|
|
|
|
ORDER BY p.last_visit_date DESC
|
|
|
|
LIMIT :limitHistory
|
|
|
|
)
|
|
|
|
)`;
|
|
|
|
|
|
|
|
let links = yield this.executePlacesQuery(sqlQuery, {
|
2016-05-19 02:46:14 +03:00
|
|
|
columns: ["bookmarkId", "bookmarkTitle", "bookmarkGuid", "bookmarkDateCreated", "url", "guid",
|
2016-04-15 02:52:16 +03:00
|
|
|
"title", "lastVisitDate", "frecency", "type", "lastModified", "favicon", "mimeType"],
|
|
|
|
params: params,
|
|
|
|
});
|
|
|
|
|
|
|
|
links = this._faviconBytesToDataURI(links);
|
|
|
|
return links.filter(link => LinkChecker.checkLoadURI(link.url));
|
|
|
|
}),
|
|
|
|
|
2016-02-12 00:37:20 +03:00
|
|
|
/**
|
|
|
|
* From an Array of links, if favicons are present, convert to data URIs
|
|
|
|
*
|
|
|
|
* @param {Array} links
|
|
|
|
* an array containing objects with favicon data and mimeTypes
|
|
|
|
*
|
|
|
|
* @returns {Array} an array of links with favicons as data uri
|
|
|
|
*/
|
|
|
|
_faviconBytesToDataURI(links) {
|
|
|
|
return links.map(link => {
|
2016-02-05 22:25:35 +03:00
|
|
|
if (link.favicon) {
|
|
|
|
let encodedData = base64.encode(String.fromCharCode.apply(null, link.favicon));
|
2016-02-12 00:37:20 +03:00
|
|
|
link.favicon = `data:${link.mimeType};base64,${encodedData}`;
|
2016-02-05 22:25:35 +03:00
|
|
|
}
|
2016-02-12 00:37:20 +03:00
|
|
|
delete link.mimeType;
|
2016-02-05 22:25:35 +03:00
|
|
|
return link;
|
|
|
|
});
|
2016-02-12 00:37:20 +03:00
|
|
|
},
|
2016-02-01 22:02:30 +03:00
|
|
|
|
2016-03-23 23:37:03 +03:00
|
|
|
/**
|
|
|
|
* Gets History size
|
|
|
|
*
|
|
|
|
* @returns {Promise} Returns a promise with the count of moz_places records
|
|
|
|
*/
|
2016-04-28 23:18:14 +03:00
|
|
|
getHistorySize: Task.async(function*(options = {}) {
|
|
|
|
let {ignoreBlocked} = options;
|
|
|
|
let blockedURLs = ignoreBlocked ? [] : this.blockedURLs.items().map(item => `"${item}"`);
|
|
|
|
|
2016-03-23 23:37:03 +03:00
|
|
|
let sqlQuery = `SELECT count(1) as count
|
|
|
|
FROM moz_places
|
2016-04-28 23:18:14 +03:00
|
|
|
WHERE hidden = 0 AND last_visit_date NOT NULL
|
|
|
|
AND url NOT IN (${blockedURLs})`;
|
2016-03-23 23:37:03 +03:00
|
|
|
|
|
|
|
let result = yield this.executePlacesQuery(sqlQuery);
|
|
|
|
return result[0][0];
|
|
|
|
}),
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets Bookmarks count
|
|
|
|
*
|
|
|
|
* @returns {Promise} Returns a promise with the count of bookmarks
|
|
|
|
*/
|
2016-04-28 23:18:14 +03:00
|
|
|
getBookmarksSize: Task.async(function*(options = {}) {
|
|
|
|
let {ignoreBlocked} = options;
|
|
|
|
let blockedURLs = ignoreBlocked ? [] : this.blockedURLs.items().map(item => `"${item}"`);
|
|
|
|
|
2016-03-23 23:37:03 +03:00
|
|
|
let sqlQuery = `SELECT count(1) as count
|
2016-04-28 23:18:14 +03:00
|
|
|
FROM moz_bookmarks b, moz_places p
|
|
|
|
WHERE type = :type
|
|
|
|
AND b.fk = p.id
|
|
|
|
AND p.url NOT IN (${blockedURLs})`;
|
2016-03-23 23:37:03 +03:00
|
|
|
|
|
|
|
let result = yield this.executePlacesQuery(sqlQuery, {params: {type: Bookmarks.TYPE_BOOKMARK}});
|
|
|
|
return result[0][0];
|
|
|
|
}),
|
|
|
|
|
2016-02-01 22:02:30 +03:00
|
|
|
/**
|
|
|
|
* Executes arbitrary query against places database
|
|
|
|
*
|
|
|
|
* @param {String} aSql
|
|
|
|
* SQL query to execute
|
|
|
|
* @param {Object} [optional] aOptions
|
|
|
|
* aOptions.columns - an array of column names. if supplied the return
|
|
|
|
* items will consists of objects keyed on column names. Otherwise
|
|
|
|
* array of raw values is returned in the select order
|
|
|
|
* aOptions.param - an object of SQL binding parameters
|
|
|
|
* aOptions.callback - a callback to handle query raws
|
|
|
|
*
|
|
|
|
* @returns {Promise} Returns a promise with the array of retrieved items
|
|
|
|
*/
|
2016-04-15 01:42:24 +03:00
|
|
|
executePlacesQuery: function PlacesProvider_executePlacesQuery(aSql, aOptions = {}) {
|
2016-02-01 22:02:30 +03:00
|
|
|
let {columns, params, callback} = aOptions;
|
|
|
|
let items = [];
|
|
|
|
let queryError = null;
|
|
|
|
return Task.spawn(function*() {
|
|
|
|
let conn = yield PlacesUtils.promiseDBConnection();
|
|
|
|
yield conn.executeCached(aSql, params, aRow => {
|
|
|
|
try {
|
|
|
|
// check if caller wants to handle query raws
|
|
|
|
if (callback) {
|
|
|
|
callback(aRow);
|
|
|
|
}
|
|
|
|
// otherwise fill in the item and add items array
|
|
|
|
else {
|
|
|
|
let item = null;
|
|
|
|
// if columns array is given construct an object
|
|
|
|
if (columns && Array.isArray(columns)) {
|
|
|
|
item = {};
|
|
|
|
columns.forEach(column => {
|
|
|
|
item[column] = aRow.getResultByName(column);
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
// if no columns - make an array of raw values
|
|
|
|
item = [];
|
|
|
|
for (let i = 0; i < aRow.numEntries; i++) {
|
|
|
|
item.push(aRow.getResultByIndex(i));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
items.push(item);
|
|
|
|
}
|
|
|
|
} catch (e) {
|
|
|
|
queryError = e;
|
|
|
|
throw StopIteration;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
if (queryError) {
|
|
|
|
throw new Error(queryError);
|
|
|
|
}
|
|
|
|
return items;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Singleton that serves as the default link provider for the grid.
|
|
|
|
*/
|
2016-03-04 02:18:38 +03:00
|
|
|
const gLinks = new Links();
|
2016-02-01 22:02:30 +03:00
|
|
|
|
|
|
|
exports.PlacesProvider = {
|
2016-02-27 01:45:00 +03:00
|
|
|
LinkChecker,
|
2016-02-01 22:02:30 +03:00
|
|
|
links: gLinks,
|
2016-04-28 23:18:14 +03:00
|
|
|
BlockedURLs,
|
2016-02-01 22:02:30 +03:00
|
|
|
};
|