2015-06-26 00:52:57 +03: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/. */
|
|
|
|
|
|
|
|
"use strict";
|
|
|
|
|
2019-01-17 21:18:31 +03:00
|
|
|
const { AppConstants } = ChromeUtils.import(
|
|
|
|
"resource://gre/modules/AppConstants.jsm"
|
|
|
|
);
|
|
|
|
const { Services } = ChromeUtils.import("resource://gre/modules/Services.jsm");
|
2019-07-05 11:44:55 +03:00
|
|
|
|
2018-01-30 08:17:48 +03:00
|
|
|
ChromeUtils.defineModuleGetter(
|
|
|
|
this,
|
|
|
|
"EventDispatcher",
|
|
|
|
"resource://gre/modules/Messaging.jsm"
|
|
|
|
);
|
2019-07-05 11:44:55 +03:00
|
|
|
|
2018-01-30 08:17:48 +03:00
|
|
|
ChromeUtils.defineModuleGetter(
|
|
|
|
this,
|
|
|
|
"PlacesUtils",
|
|
|
|
"resource://gre/modules/PlacesUtils.jsm"
|
|
|
|
);
|
|
|
|
ChromeUtils.defineModuleGetter(
|
|
|
|
this,
|
|
|
|
"PrivateBrowsingUtils",
|
|
|
|
"resource://gre/modules/PrivateBrowsingUtils.jsm"
|
|
|
|
);
|
2015-08-20 02:03:15 +03:00
|
|
|
|
2019-04-09 13:05:51 +03:00
|
|
|
const EXPORTED_SYMBOLS = ["PushRecord"];
|
2015-06-26 00:52:57 +03:00
|
|
|
|
2017-07-29 00:13:57 +03:00
|
|
|
const prefs = Services.prefs.getBranch("dom.push.");
|
2015-06-26 00:52:57 +03:00
|
|
|
|
2015-12-08 23:41:41 +03:00
|
|
|
/**
|
|
|
|
* The push subscription record, stored in IndexedDB.
|
|
|
|
*/
|
2015-06-26 00:52:57 +03:00
|
|
|
function PushRecord(props) {
|
|
|
|
this.pushEndpoint = props.pushEndpoint;
|
|
|
|
this.scope = props.scope;
|
|
|
|
this.originAttributes = props.originAttributes;
|
|
|
|
this.pushCount = props.pushCount || 0;
|
|
|
|
this.lastPush = props.lastPush || 0;
|
2015-09-17 15:08:50 +03:00
|
|
|
this.p256dhPublicKey = props.p256dhPublicKey;
|
|
|
|
this.p256dhPrivateKey = props.p256dhPrivateKey;
|
2015-12-08 23:26:42 +03:00
|
|
|
this.authenticationSecret = props.authenticationSecret;
|
2015-12-16 20:21:22 +03:00
|
|
|
this.systemRecord = !!props.systemRecord;
|
2016-03-16 12:53:13 +03:00
|
|
|
this.appServerKey = props.appServerKey;
|
2016-06-04 23:17:12 +03:00
|
|
|
this.recentMessageIDs = props.recentMessageIDs;
|
2015-06-26 00:52:57 +03:00
|
|
|
this.setQuota(props.quota);
|
2015-08-06 22:05:47 +03:00
|
|
|
this.ctime = typeof props.ctime === "number" ? props.ctime : 0;
|
2015-06-26 00:52:57 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
PushRecord.prototype = {
|
|
|
|
setQuota(suggestedQuota) {
|
2016-05-10 06:54:05 +03:00
|
|
|
if (this.quotaApplies()) {
|
|
|
|
let quota = +suggestedQuota;
|
2017-07-29 00:13:57 +03:00
|
|
|
this.quota =
|
|
|
|
quota >= 0 ? quota : prefs.getIntPref("maxQuotaPerSubscription");
|
2015-10-06 18:14:25 +03:00
|
|
|
} else {
|
2016-05-10 06:54:05 +03:00
|
|
|
this.quota = Infinity;
|
2015-10-06 18:14:25 +03:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
resetQuota() {
|
2015-12-16 20:21:22 +03:00
|
|
|
this.quota = this.quotaApplies()
|
2017-07-29 00:13:57 +03:00
|
|
|
? prefs.getIntPref("maxQuotaPerSubscription")
|
|
|
|
: Infinity;
|
2015-06-26 00:52:57 +03:00
|
|
|
},
|
|
|
|
|
|
|
|
updateQuota(lastVisit) {
|
|
|
|
if (this.isExpired() || !this.quotaApplies()) {
|
|
|
|
// Ignore updates if the registration is already expired, or isn't
|
|
|
|
// subject to quota.
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (lastVisit < 0) {
|
|
|
|
// If the user cleared their history, but retained the push permission,
|
|
|
|
// mark the registration as expired.
|
|
|
|
this.quota = 0;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (lastVisit > this.lastPush) {
|
|
|
|
// If the user visited the site since the last time we received a
|
2016-05-10 06:54:05 +03:00
|
|
|
// notification, reset the quota. `Math.max(0, ...)` ensures the
|
|
|
|
// last visit date isn't in the future.
|
|
|
|
let daysElapsed = Math.max(
|
|
|
|
0,
|
|
|
|
(Date.now() - lastVisit) / 24 / 60 / 60 / 1000
|
|
|
|
);
|
2015-11-17 08:33:11 +03:00
|
|
|
this.quota = Math.min(
|
2015-06-26 00:52:57 +03:00
|
|
|
Math.round(8 * Math.pow(daysElapsed, -0.8)),
|
2017-07-29 00:13:57 +03:00
|
|
|
prefs.getIntPref("maxQuotaPerSubscription")
|
2015-06-26 00:52:57 +03:00
|
|
|
);
|
2015-08-06 22:05:47 +03:00
|
|
|
}
|
2015-06-26 00:52:57 +03:00
|
|
|
},
|
|
|
|
|
|
|
|
receivedPush(lastVisit) {
|
|
|
|
this.updateQuota(lastVisit);
|
|
|
|
this.pushCount++;
|
|
|
|
this.lastPush = Date.now();
|
|
|
|
},
|
|
|
|
|
2016-06-04 23:17:12 +03:00
|
|
|
/**
|
|
|
|
* Records a message ID sent to this push registration. We track the last few
|
|
|
|
* messages sent to each registration to avoid firing duplicate events for
|
|
|
|
* unacknowledged messages.
|
|
|
|
*/
|
|
|
|
noteRecentMessageID(id) {
|
|
|
|
if (this.recentMessageIDs) {
|
|
|
|
this.recentMessageIDs.unshift(id);
|
|
|
|
} else {
|
|
|
|
this.recentMessageIDs = [id];
|
|
|
|
}
|
|
|
|
// Drop older message IDs from the end of the list.
|
|
|
|
let maxRecentMessageIDs = Math.min(
|
|
|
|
this.recentMessageIDs.length,
|
2017-07-29 00:13:57 +03:00
|
|
|
Math.max(prefs.getIntPref("maxRecentMessageIDsPerSubscription"), 0)
|
2016-06-04 23:17:12 +03:00
|
|
|
);
|
|
|
|
this.recentMessageIDs.length = maxRecentMessageIDs || 0;
|
|
|
|
},
|
|
|
|
|
|
|
|
hasRecentMessageID(id) {
|
|
|
|
return this.recentMessageIDs && this.recentMessageIDs.includes(id);
|
|
|
|
},
|
|
|
|
|
2015-11-17 08:33:11 +03:00
|
|
|
reduceQuota() {
|
2015-12-16 20:21:22 +03:00
|
|
|
if (!this.quotaApplies()) {
|
|
|
|
return;
|
|
|
|
}
|
2015-11-17 08:33:11 +03:00
|
|
|
this.quota = Math.max(this.quota - 1, 0);
|
|
|
|
},
|
|
|
|
|
2015-06-26 00:52:57 +03:00
|
|
|
/**
|
|
|
|
* Queries the Places database for the last time a user visited the site
|
|
|
|
* associated with a push registration.
|
|
|
|
*
|
|
|
|
* @returns {Promise} A promise resolved with either the last time the user
|
|
|
|
* visited the site, or `-Infinity` if the site is not in the user's history.
|
|
|
|
* The time is expressed in milliseconds since Epoch.
|
|
|
|
*/
|
2016-02-08 16:42:07 +03:00
|
|
|
async getLastVisit() {
|
2015-06-26 00:52:57 +03:00
|
|
|
if (!this.quotaApplies() || this.isTabOpen()) {
|
|
|
|
// If the registration isn't subject to quota, or the user already
|
2015-10-23 20:07:05 +03:00
|
|
|
// has the site open, skip expensive database queries.
|
2016-02-08 16:42:07 +03:00
|
|
|
return Date.now();
|
2015-06-26 00:52:57 +03:00
|
|
|
}
|
2015-10-23 20:07:05 +03:00
|
|
|
|
|
|
|
if (AppConstants.MOZ_ANDROID_HISTORY) {
|
2017-02-02 01:35:44 +03:00
|
|
|
let result = await EventDispatcher.instance.sendRequestForResult({
|
2015-10-23 20:07:05 +03:00
|
|
|
type: "History:GetPrePathLastVisitedTimeMilliseconds",
|
|
|
|
prePath: this.uri.prePath,
|
|
|
|
});
|
2016-02-08 16:42:07 +03:00
|
|
|
return result == 0 ? -Infinity : result;
|
2015-10-23 20:07:05 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Places History transition types that can fire a
|
|
|
|
// `pushsubscriptionchange` event when the user visits a site with expired push
|
|
|
|
// registrations. Visits only count if the user sees the origin in the address
|
|
|
|
// bar. This excludes embedded resources, downloads, and framed links.
|
|
|
|
const QUOTA_REFRESH_TRANSITIONS_SQL = [
|
|
|
|
Ci.nsINavHistoryService.TRANSITION_LINK,
|
|
|
|
Ci.nsINavHistoryService.TRANSITION_TYPED,
|
|
|
|
Ci.nsINavHistoryService.TRANSITION_BOOKMARK,
|
|
|
|
Ci.nsINavHistoryService.TRANSITION_REDIRECT_PERMANENT,
|
2019-04-09 13:05:51 +03:00
|
|
|
Ci.nsINavHistoryService.TRANSITION_REDIRECT_TEMPORARY,
|
2015-10-23 20:07:05 +03:00
|
|
|
].join(",");
|
|
|
|
|
2016-02-08 16:42:07 +03:00
|
|
|
let db = await PlacesUtils.promiseDBConnection();
|
|
|
|
// We're using a custom query instead of `nsINavHistoryQueryOptions`
|
|
|
|
// because the latter doesn't expose a way to filter by transition type:
|
|
|
|
// `setTransitions` performs a logical "and," but we want an "or." We
|
Bug 977177 - Move favicons to a separate store. r=adw
This patch moves favicons blobs to a separate database names favicons.sqlite.
The dabatase is then ATTACHED to the main Places connection, so that its tables
can be used as if they were all part of the same database.
The favicons.database contains 3 tables:
1. moz_pages_w_icons
This is the way to join with moz_places, through page_url_hash and page_url.
We are not using the place id to avoid possible mismatches between places.sqlite
and favicons.sqlite. This way the database is "portable" and reusable even
if places.sqlite changes.
2. moz_icons
Contains icons payloads, each payload can either be an SVG or a PNG. These
are the only stored formats, any other format is rescaled and converted to
PNG. ICO files are split into single frames and stored as multiple PNGs.
SVG are distinguishable through width == UINT16_MAX
In future the table will also contain mask-icon color for SVG and average
color for PNGs.
The fixed_icon_url_hash is "fixed" to allow quickly fetch root icons, that
means icons like "domain/favicon.ico" that can also be reused for any page
under that domain.
3. moz_icons_to_pages
This is the relation table between icons and pages.
Each page can have multiple icons, each icon can be used by multiple pages.
There is a FOREIGN_KEY constraint between this (child) table and icons
or pages (parents), so that it's not possible to insert non-existing ids
in this table, and if an entry is removed from a parent table, the relation
will be automatically removed from here.
Note though that removing from the relation table won't remove from the
parent tables.
Since the relations are now many-many, it's no more possible to simply join
places with the icons table and obtain a single icon, thus it's suggested that
consumers go through the "page-icon" protocol.
The migration process from the old favicons table is async and interruptible,
it will be restarted along with the favicons service until the temp preference
places.favicons.convertPayloads is set to true.
MozReview-Commit-ID: CUCoL9smRyt
--HG--
extra : rebase_source : 4d25966596dcdf63c9c872425c5bf147406d25ac
2016-11-14 18:22:46 +03:00
|
|
|
// also avoid an unneeded left join with favicons, and an `ORDER BY`
|
2016-02-08 16:42:07 +03:00
|
|
|
// clause that emits a suboptimal index warning.
|
|
|
|
let rows = await db.executeCached(
|
|
|
|
`SELECT MAX(visit_date) AS lastVisit
|
|
|
|
FROM moz_places p
|
|
|
|
JOIN moz_historyvisits ON p.id = place_id
|
|
|
|
WHERE rev_host = get_unreversed_host(:host || '.') || '.'
|
|
|
|
AND url BETWEEN :prePath AND :prePath || X'FFFF'
|
|
|
|
AND visit_type IN (${QUOTA_REFRESH_TRANSITIONS_SQL})
|
|
|
|
`,
|
|
|
|
{
|
|
|
|
// Restrict the query to all pages for this origin.
|
|
|
|
host: this.uri.host,
|
|
|
|
prePath: this.uri.prePath,
|
2015-06-26 00:52:57 +03:00
|
|
|
}
|
2016-02-08 16:42:07 +03:00
|
|
|
);
|
|
|
|
|
|
|
|
if (!rows.length) {
|
|
|
|
return -Infinity;
|
|
|
|
}
|
|
|
|
// Places records times in microseconds.
|
|
|
|
let lastVisit = rows[0].getResultByName("lastVisit");
|
|
|
|
|
|
|
|
return lastVisit / 1000;
|
|
|
|
},
|
2015-06-26 00:52:57 +03:00
|
|
|
|
|
|
|
isTabOpen() {
|
2018-08-19 04:13:14 +03:00
|
|
|
for (let window of Services.wm.getEnumerator("navigator:browser")) {
|
2015-06-26 00:52:57 +03:00
|
|
|
if (window.closed || PrivateBrowsingUtils.isWindowPrivate(window)) {
|
|
|
|
continue;
|
|
|
|
}
|
2020-04-14 20:07:38 +03:00
|
|
|
for (let tab of window.gBrowser.tabs) {
|
|
|
|
let tabURI = tab.linkedBrowser.currentURI;
|
2015-08-20 02:03:15 +03:00
|
|
|
if (tabURI.prePath == this.uri.prePath) {
|
2015-06-26 00:52:57 +03:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
},
|
|
|
|
|
2015-08-20 02:03:15 +03:00
|
|
|
/**
|
|
|
|
* Indicates whether the registration can deliver push messages to its
|
2015-12-16 20:21:22 +03:00
|
|
|
* associated service worker. System subscriptions are exempt from the
|
|
|
|
* permission check.
|
2015-08-20 02:03:15 +03:00
|
|
|
*/
|
|
|
|
hasPermission() {
|
2017-07-29 00:13:57 +03:00
|
|
|
if (
|
|
|
|
this.systemRecord ||
|
|
|
|
prefs.getBoolPref("testing.ignorePermission", false)
|
|
|
|
) {
|
2015-12-16 20:21:22 +03:00
|
|
|
return true;
|
|
|
|
}
|
2015-10-06 02:39:34 +03:00
|
|
|
let permission = Services.perms.testExactPermissionFromPrincipal(
|
|
|
|
this.principal,
|
|
|
|
"desktop-notification"
|
|
|
|
);
|
2015-08-20 02:03:15 +03:00
|
|
|
return permission == Ci.nsIPermissionManager.ALLOW_ACTION;
|
|
|
|
},
|
|
|
|
|
2015-10-06 18:14:25 +03:00
|
|
|
quotaChanged() {
|
2015-11-11 01:27:47 +03:00
|
|
|
if (!this.hasPermission()) {
|
|
|
|
return Promise.resolve(false);
|
|
|
|
}
|
2015-10-06 18:14:25 +03:00
|
|
|
return this.getLastVisit().then(lastVisit => lastVisit > this.lastPush);
|
|
|
|
},
|
|
|
|
|
2015-06-26 00:52:57 +03:00
|
|
|
quotaApplies() {
|
2015-12-16 20:21:22 +03:00
|
|
|
return !this.systemRecord;
|
2015-06-26 00:52:57 +03:00
|
|
|
},
|
|
|
|
|
|
|
|
isExpired() {
|
|
|
|
return this.quota === 0;
|
|
|
|
},
|
|
|
|
|
2015-11-14 02:18:10 +03:00
|
|
|
matchesOriginAttributes(pattern) {
|
2015-12-16 20:21:22 +03:00
|
|
|
if (this.systemRecord) {
|
|
|
|
return false;
|
|
|
|
}
|
2015-11-14 02:18:10 +03:00
|
|
|
return ChromeUtils.originAttributesMatchPattern(
|
|
|
|
this.principal.originAttributes,
|
|
|
|
pattern
|
|
|
|
);
|
|
|
|
},
|
|
|
|
|
2016-03-23 05:20:36 +03:00
|
|
|
hasAuthenticationSecret() {
|
|
|
|
return (
|
|
|
|
!!this.authenticationSecret && this.authenticationSecret.byteLength == 16
|
|
|
|
);
|
|
|
|
},
|
|
|
|
|
2016-03-16 12:53:13 +03:00
|
|
|
matchesAppServerKey(key) {
|
|
|
|
if (!this.appServerKey) {
|
|
|
|
return !key;
|
|
|
|
}
|
|
|
|
if (!key) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return (
|
|
|
|
this.appServerKey.length === key.length &&
|
|
|
|
this.appServerKey.every((value, index) => value === key[index])
|
|
|
|
);
|
|
|
|
},
|
|
|
|
|
2015-11-11 01:27:47 +03:00
|
|
|
toSubscription() {
|
2015-06-26 00:52:57 +03:00
|
|
|
return {
|
2015-12-08 23:41:41 +03:00
|
|
|
endpoint: this.pushEndpoint,
|
2015-06-26 00:52:57 +03:00
|
|
|
lastPush: this.lastPush,
|
|
|
|
pushCount: this.pushCount,
|
2015-09-17 15:08:50 +03:00
|
|
|
p256dhKey: this.p256dhPublicKey,
|
2016-07-20 20:47:04 +03:00
|
|
|
p256dhPrivateKey: this.p256dhPrivateKey,
|
2015-12-08 23:26:42 +03:00
|
|
|
authenticationSecret: this.authenticationSecret,
|
2016-03-16 12:53:13 +03:00
|
|
|
appServerKey: this.appServerKey,
|
2015-12-08 23:41:41 +03:00
|
|
|
quota: this.quotaApplies() ? this.quota : -1,
|
2016-04-24 05:41:59 +03:00
|
|
|
systemRecord: this.systemRecord,
|
2015-06-26 00:52:57 +03:00
|
|
|
};
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
2015-08-20 02:03:15 +03:00
|
|
|
// Define lazy getters for the principal and scope URI. IndexedDB can't store
|
|
|
|
// `nsIPrincipal` objects, so we keep them in a private weak map.
|
2015-09-15 21:19:45 +03:00
|
|
|
var principals = new WeakMap();
|
2015-08-20 02:03:15 +03:00
|
|
|
Object.defineProperties(PushRecord.prototype, {
|
|
|
|
principal: {
|
|
|
|
get() {
|
2015-12-16 20:21:22 +03:00
|
|
|
if (this.systemRecord) {
|
|
|
|
return Services.scriptSecurityManager.getSystemPrincipal();
|
|
|
|
}
|
2015-08-20 02:03:15 +03:00
|
|
|
let principal = principals.get(this);
|
|
|
|
if (!principal) {
|
2016-10-20 20:29:58 +03:00
|
|
|
let uri = Services.io.newURI(this.scope);
|
|
|
|
// Allow tests to omit origin attributes.
|
|
|
|
let originSuffix = this.originAttributes || "";
|
2019-07-08 19:37:45 +03:00
|
|
|
principal = Services.scriptSecurityManager.createContentPrincipal(
|
2016-10-20 20:29:58 +03:00
|
|
|
uri,
|
|
|
|
ChromeUtils.createOriginAttributesFromOrigin(originSuffix)
|
|
|
|
);
|
2015-08-20 02:03:15 +03:00
|
|
|
principals.set(this, principal);
|
|
|
|
}
|
|
|
|
return principal;
|
|
|
|
},
|
|
|
|
configurable: true,
|
|
|
|
},
|
|
|
|
|
|
|
|
uri: {
|
|
|
|
get() {
|
|
|
|
return this.principal.URI;
|
|
|
|
},
|
|
|
|
configurable: true,
|
|
|
|
},
|
2015-06-26 00:52:57 +03:00
|
|
|
});
|