2012-02-24 16:42:24 +04: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/. */
|
2009-09-04 02:19:29 +04:00
|
|
|
|
2015-04-25 11:51:48 +03:00
|
|
|
const { classes: Cc, interfaces: Ci, results: Cr, utils: Cu } = Components;
|
2006-10-19 06:24:30 +04:00
|
|
|
|
2016-11-03 21:42:06 +03:00
|
|
|
// Modules and services.
|
2011-06-15 03:54:33 +04:00
|
|
|
|
2012-02-24 16:42:24 +04:00
|
|
|
Cu.import("resource://gre/modules/XPCOMUtils.jsm");
|
|
|
|
Cu.import("resource://gre/modules/Services.jsm");
|
2011-06-15 03:54:33 +04:00
|
|
|
XPCOMUtils.defineLazyModuleGetter(this, "PlacesUtils",
|
|
|
|
"resource://gre/modules/PlacesUtils.jsm");
|
|
|
|
XPCOMUtils.defineLazyModuleGetter(this, "NetUtil",
|
|
|
|
"resource://gre/modules/NetUtil.jsm");
|
2014-05-27 16:26:22 +04:00
|
|
|
XPCOMUtils.defineLazyModuleGetter(this, "Task",
|
|
|
|
"resource://gre/modules/Task.jsm");
|
2014-03-12 19:24:23 +04:00
|
|
|
XPCOMUtils.defineLazyModuleGetter(this, "Deprecated",
|
|
|
|
"resource://gre/modules/Deprecated.jsm");
|
2011-06-15 03:54:33 +04:00
|
|
|
|
2016-11-12 02:22:34 +03:00
|
|
|
XPCOMUtils.defineLazyGetter(this, "asyncHistory", function() {
|
2012-02-24 16:42:24 +04:00
|
|
|
// Lazily add an history observer when it's actually needed.
|
|
|
|
PlacesUtils.history.addObserver(PlacesUtils.livemarks, true);
|
2015-04-25 11:51:48 +03:00
|
|
|
return PlacesUtils.asyncHistory;
|
2012-02-24 16:42:24 +04:00
|
|
|
});
|
2009-11-06 18:27:51 +03:00
|
|
|
|
2016-11-03 21:42:06 +03:00
|
|
|
// Constants
|
2012-02-24 16:42:24 +04:00
|
|
|
|
|
|
|
// Delay between reloads of consecute livemarks.
|
|
|
|
const RELOAD_DELAY_MS = 500;
|
|
|
|
// Expire livemarks after this time.
|
|
|
|
const EXPIRE_TIME_MS = 3600000; // 1 hour.
|
|
|
|
// Expire livemarks after this time on error.
|
|
|
|
const ONERROR_EXPIRE_TIME_MS = 300000; // 5 minutes.
|
2009-11-06 18:27:51 +03:00
|
|
|
|
2016-11-03 21:42:06 +03:00
|
|
|
// Livemarks cache.
|
2015-04-25 11:51:48 +03:00
|
|
|
|
|
|
|
XPCOMUtils.defineLazyGetter(this, "CACHE_SQL", () => {
|
|
|
|
function getAnnoSQLFragment(aAnnoParam) {
|
|
|
|
return `SELECT a.content
|
|
|
|
FROM moz_items_annos a
|
|
|
|
JOIN moz_anno_attributes n ON n.id = a.anno_attribute_id
|
|
|
|
WHERE a.item_id = b.id
|
|
|
|
AND n.name = ${aAnnoParam}`;
|
|
|
|
}
|
|
|
|
|
|
|
|
return `SELECT b.id, b.title, b.parent As parentId, b.position AS 'index',
|
|
|
|
b.guid, b.dateAdded, b.lastModified, p.guid AS parentGuid,
|
|
|
|
( ${getAnnoSQLFragment(":feedURI_anno")} ) AS feedURI,
|
|
|
|
( ${getAnnoSQLFragment(":siteURI_anno")} ) AS siteURI
|
|
|
|
FROM moz_bookmarks b
|
|
|
|
JOIN moz_bookmarks p ON b.parent = p.id
|
|
|
|
JOIN moz_items_annos a ON a.item_id = b.id
|
|
|
|
JOIN moz_anno_attributes n ON a.anno_attribute_id = n.id
|
|
|
|
WHERE b.type = :folder_type
|
|
|
|
AND n.name = :feedURI_anno`;
|
|
|
|
});
|
|
|
|
|
|
|
|
XPCOMUtils.defineLazyGetter(this, "gLivemarksCachePromised",
|
|
|
|
Task.async(function* () {
|
|
|
|
let livemarksMap = new Map();
|
|
|
|
let conn = yield PlacesUtils.promiseDBConnection();
|
|
|
|
let rows = yield conn.executeCached(CACHE_SQL,
|
|
|
|
{ folder_type: Ci.nsINavBookmarksService.TYPE_FOLDER,
|
|
|
|
feedURI_anno: PlacesUtils.LMANNO_FEEDURI,
|
|
|
|
siteURI_anno: PlacesUtils.LMANNO_SITEURI });
|
|
|
|
for (let row of rows) {
|
|
|
|
let siteURI = row.getResultByName("siteURI");
|
|
|
|
let livemark = new Livemark({
|
|
|
|
id: row.getResultByName("id"),
|
|
|
|
guid: row.getResultByName("guid"),
|
|
|
|
title: row.getResultByName("title"),
|
2015-04-29 11:57:20 +03:00
|
|
|
parentId: row.getResultByName("parentId"),
|
2015-04-25 11:51:48 +03:00
|
|
|
parentGuid: row.getResultByName("parentGuid"),
|
2015-04-29 11:57:20 +03:00
|
|
|
index: row.getResultByName("index"),
|
2015-04-25 11:51:48 +03:00
|
|
|
dateAdded: row.getResultByName("dateAdded"),
|
|
|
|
lastModified: row.getResultByName("lastModified"),
|
|
|
|
feedURI: NetUtil.newURI(row.getResultByName("feedURI")),
|
|
|
|
siteURI: siteURI ? NetUtil.newURI(siteURI) : null
|
|
|
|
});
|
|
|
|
livemarksMap.set(livemark.guid, livemark);
|
|
|
|
}
|
|
|
|
return livemarksMap;
|
|
|
|
})
|
|
|
|
);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Convert a Date object to a PRTime (microseconds).
|
|
|
|
*
|
|
|
|
* @param date
|
|
|
|
* the Date object to convert.
|
|
|
|
* @return microseconds from the epoch.
|
|
|
|
*/
|
|
|
|
function toPRTime(date) {
|
|
|
|
return date * 1000;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Convert a PRTime to a Date object.
|
|
|
|
*
|
|
|
|
* @param time
|
|
|
|
* microseconds from the epoch.
|
|
|
|
* @return a Date object or undefined if time was not defined.
|
|
|
|
*/
|
|
|
|
function toDate(time) {
|
|
|
|
return time ? new Date(parseInt(time / 1000)) : undefined;
|
|
|
|
}
|
|
|
|
|
2016-11-03 21:42:06 +03:00
|
|
|
// LivemarkService
|
2007-06-12 19:56:36 +04:00
|
|
|
|
2015-04-25 11:51:48 +03:00
|
|
|
function LivemarkService() {
|
2012-02-24 16:42:24 +04:00
|
|
|
// Cleanup on shutdown.
|
|
|
|
Services.obs.addObserver(this, PlacesUtils.TOPIC_SHUTDOWN, true);
|
2013-11-20 14:00:02 +04:00
|
|
|
|
2015-04-25 11:51:48 +03:00
|
|
|
// Observe bookmarks but don't init the service just for that.
|
2012-02-24 16:42:24 +04:00
|
|
|
PlacesUtils.addLazyBookmarkObserver(this, true);
|
|
|
|
}
|
2008-09-27 15:05:28 +04:00
|
|
|
|
2012-02-24 16:42:24 +04:00
|
|
|
LivemarkService.prototype = {
|
2015-04-25 11:51:48 +03:00
|
|
|
// This is just an helper for code readability.
|
|
|
|
_promiseLivemarksMap: () => gLivemarksCachePromised,
|
2008-09-27 15:05:28 +04:00
|
|
|
|
2012-03-08 14:14:59 +04:00
|
|
|
_reloading: false,
|
2015-04-25 11:51:48 +03:00
|
|
|
_startReloadTimer(livemarksMap, forceUpdate, reloaded) {
|
2012-02-24 16:42:24 +04:00
|
|
|
if (this._reloadTimer) {
|
|
|
|
this._reloadTimer.cancel();
|
2016-12-31 05:47:25 +03:00
|
|
|
} else {
|
2012-02-24 16:42:24 +04:00
|
|
|
this._reloadTimer = Cc["@mozilla.org/timer;1"]
|
|
|
|
.createInstance(Ci.nsITimer);
|
|
|
|
}
|
2015-04-25 11:51:48 +03:00
|
|
|
|
2012-03-08 14:14:59 +04:00
|
|
|
this._reloading = true;
|
2015-04-25 11:51:48 +03:00
|
|
|
this._reloadTimer.initWithCallback(() => {
|
|
|
|
// Find first livemark to be reloaded.
|
|
|
|
for (let [ guid, livemark ] of livemarksMap) {
|
|
|
|
if (!reloaded.has(guid)) {
|
|
|
|
reloaded.add(guid);
|
|
|
|
livemark.reload(forceUpdate);
|
|
|
|
this._startReloadTimer(livemarksMap, forceUpdate, reloaded);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// All livemarks have been reloaded.
|
|
|
|
this._reloading = false;
|
|
|
|
}, RELOAD_DELAY_MS, Ci.nsITimer.TYPE_ONE_SHOT);
|
2012-02-24 16:42:24 +04:00
|
|
|
},
|
2007-08-03 00:19:44 +04:00
|
|
|
|
2016-11-03 21:42:06 +03:00
|
|
|
// nsIObserver
|
2009-11-06 18:27:51 +03:00
|
|
|
|
2015-04-25 11:51:48 +03:00
|
|
|
observe(aSubject, aTopic, aData) {
|
2012-02-24 16:42:24 +04:00
|
|
|
if (aTopic == PlacesUtils.TOPIC_SHUTDOWN) {
|
|
|
|
if (this._reloadTimer) {
|
2012-03-08 14:14:59 +04:00
|
|
|
this._reloading = false;
|
2012-02-24 16:42:24 +04:00
|
|
|
this._reloadTimer.cancel();
|
|
|
|
delete this._reloadTimer;
|
|
|
|
}
|
|
|
|
|
2015-04-25 11:51:48 +03:00
|
|
|
// Stop any ongoing network fetch.
|
|
|
|
this._promiseLivemarksMap().then(livemarksMap => {
|
|
|
|
for (let livemark of livemarksMap.values()) {
|
|
|
|
livemark.terminate();
|
|
|
|
}
|
|
|
|
});
|
2011-06-15 03:54:33 +04:00
|
|
|
}
|
2012-02-24 16:42:24 +04:00
|
|
|
},
|
|
|
|
|
2016-11-03 21:42:06 +03:00
|
|
|
// mozIAsyncLivemarks
|
2012-02-24 16:42:24 +04:00
|
|
|
|
2015-04-25 11:51:48 +03:00
|
|
|
addLivemark(aLivemarkInfo) {
|
|
|
|
if (!aLivemarkInfo) {
|
|
|
|
throw new Components.Exception("Invalid arguments", Cr.NS_ERROR_INVALID_ARG);
|
|
|
|
}
|
|
|
|
let hasParentId = "parentId" in aLivemarkInfo;
|
|
|
|
let hasParentGuid = "parentGuid" in aLivemarkInfo;
|
|
|
|
let hasIndex = "index" in aLivemarkInfo;
|
|
|
|
// Must provide at least non-null parent guid/id, index and feedURI.
|
|
|
|
if ((!hasParentId && !hasParentGuid) ||
|
|
|
|
(hasParentId && aLivemarkInfo.parentId < 1) ||
|
2016-11-11 01:48:04 +03:00
|
|
|
(hasParentGuid && !/^[a-zA-Z0-9\-_]{12}$/.test(aLivemarkInfo.parentGuid)) ||
|
2015-04-25 11:51:48 +03:00
|
|
|
(hasIndex && aLivemarkInfo.index < Ci.nsINavBookmarksService.DEFAULT_INDEX) ||
|
2012-02-24 16:42:24 +04:00
|
|
|
!(aLivemarkInfo.feedURI instanceof Ci.nsIURI) ||
|
|
|
|
(aLivemarkInfo.siteURI && !(aLivemarkInfo.siteURI instanceof Ci.nsIURI)) ||
|
|
|
|
(aLivemarkInfo.guid && !/^[a-zA-Z0-9\-_]{12}$/.test(aLivemarkInfo.guid))) {
|
2015-04-25 11:51:48 +03:00
|
|
|
throw new Components.Exception("Invalid arguments", Cr.NS_ERROR_INVALID_ARG);
|
2009-06-13 02:39:42 +04:00
|
|
|
}
|
2011-06-15 03:54:33 +04:00
|
|
|
|
2015-04-25 11:51:48 +03:00
|
|
|
return Task.spawn(function* () {
|
|
|
|
if (!aLivemarkInfo.parentGuid)
|
|
|
|
aLivemarkInfo.parentGuid = yield PlacesUtils.promiseItemGuid(aLivemarkInfo.parentId);
|
|
|
|
|
|
|
|
let livemarksMap = yield this._promiseLivemarksMap();
|
2014-03-12 19:24:23 +04:00
|
|
|
|
2012-02-24 16:42:24 +04:00
|
|
|
// Disallow adding a livemark inside another livemark.
|
2015-04-25 11:51:48 +03:00
|
|
|
if (livemarksMap.has(aLivemarkInfo.parentGuid)) {
|
|
|
|
throw new Components.Exception("Cannot create a livemark inside a livemark", Cr.NS_ERROR_INVALID_ARG);
|
2012-02-24 16:42:24 +04:00
|
|
|
}
|
|
|
|
|
2015-04-25 11:51:48 +03:00
|
|
|
// Create a new livemark.
|
|
|
|
let folder = yield PlacesUtils.bookmarks.insert({
|
|
|
|
type: PlacesUtils.bookmarks.TYPE_FOLDER,
|
|
|
|
parentGuid: aLivemarkInfo.parentGuid,
|
|
|
|
title: aLivemarkInfo.title,
|
|
|
|
index: aLivemarkInfo.index,
|
|
|
|
guid: aLivemarkInfo.guid,
|
2016-08-11 23:13:22 +03:00
|
|
|
dateAdded: toDate(aLivemarkInfo.dateAdded) || toDate(aLivemarkInfo.lastModified),
|
|
|
|
source: aLivemarkInfo.source,
|
2015-04-25 11:51:48 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
// Set feed and site URI annotations.
|
|
|
|
let id = yield PlacesUtils.promiseItemId(folder.guid);
|
|
|
|
|
|
|
|
// Create the internal Livemark object.
|
|
|
|
let livemark = new Livemark({ id
|
|
|
|
, title: folder.title
|
|
|
|
, parentGuid: folder.parentGuid
|
|
|
|
, parentId: yield PlacesUtils.promiseItemId(folder.parentGuid)
|
|
|
|
, index: folder.index
|
|
|
|
, feedURI: aLivemarkInfo.feedURI
|
|
|
|
, siteURI: aLivemarkInfo.siteURI
|
|
|
|
, guid: folder.guid
|
|
|
|
, dateAdded: toPRTime(folder.dateAdded)
|
|
|
|
, lastModified: toPRTime(folder.lastModified)
|
|
|
|
});
|
|
|
|
|
2016-10-30 21:44:11 +03:00
|
|
|
livemark.writeFeedURI(aLivemarkInfo.feedURI, aLivemarkInfo.source);
|
2015-04-25 11:51:48 +03:00
|
|
|
if (aLivemarkInfo.siteURI) {
|
2016-10-30 21:44:11 +03:00
|
|
|
livemark.writeSiteURI(aLivemarkInfo.siteURI, aLivemarkInfo.source);
|
2012-02-24 16:42:24 +04:00
|
|
|
}
|
|
|
|
|
2015-05-29 13:36:52 +03:00
|
|
|
if (aLivemarkInfo.lastModified) {
|
|
|
|
yield PlacesUtils.bookmarks.update({ guid: folder.guid,
|
2016-10-30 21:44:11 +03:00
|
|
|
lastModified: toDate(aLivemarkInfo.lastModified),
|
|
|
|
source: aLivemarkInfo.source });
|
2015-05-29 13:36:52 +03:00
|
|
|
livemark.lastModified = aLivemarkInfo.lastModified;
|
|
|
|
}
|
|
|
|
|
2015-04-25 11:51:48 +03:00
|
|
|
livemarksMap.set(folder.guid, livemark);
|
2013-11-20 14:00:02 +04:00
|
|
|
|
2015-04-25 11:51:48 +03:00
|
|
|
return livemark;
|
|
|
|
}.bind(this));
|
2006-10-19 06:24:30 +04:00
|
|
|
},
|
|
|
|
|
2015-04-25 11:51:48 +03:00
|
|
|
removeLivemark(aLivemarkInfo) {
|
2012-02-24 16:42:24 +04:00
|
|
|
if (!aLivemarkInfo) {
|
2015-04-25 11:51:48 +03:00
|
|
|
throw new Components.Exception("Invalid arguments", Cr.NS_ERROR_INVALID_ARG);
|
2011-06-15 03:54:33 +04:00
|
|
|
}
|
2012-02-24 16:42:24 +04:00
|
|
|
// Accept either a guid or an id.
|
2015-04-25 11:51:48 +03:00
|
|
|
let hasGuid = "guid" in aLivemarkInfo;
|
|
|
|
let hasId = "id" in aLivemarkInfo;
|
|
|
|
if ((hasGuid && !/^[a-zA-Z0-9\-_]{12}$/.test(aLivemarkInfo.guid)) ||
|
|
|
|
(hasId && aLivemarkInfo.id < 1) ||
|
|
|
|
(!hasId && !hasGuid)) {
|
|
|
|
throw new Components.Exception("Invalid arguments", Cr.NS_ERROR_INVALID_ARG);
|
2009-06-23 03:00:51 +04:00
|
|
|
}
|
|
|
|
|
2015-04-25 11:51:48 +03:00
|
|
|
return Task.spawn(function* () {
|
|
|
|
if (!aLivemarkInfo.guid)
|
|
|
|
aLivemarkInfo.guid = yield PlacesUtils.promiseItemGuid(aLivemarkInfo.id);
|
2014-03-12 19:24:23 +04:00
|
|
|
|
2015-04-25 11:51:48 +03:00
|
|
|
let livemarksMap = yield this._promiseLivemarksMap();
|
|
|
|
if (!livemarksMap.has(aLivemarkInfo.guid))
|
|
|
|
throw new Components.Exception("Invalid livemark", Cr.NS_ERROR_INVALID_ARG);
|
2013-11-20 14:00:02 +04:00
|
|
|
|
2016-10-30 21:44:11 +03:00
|
|
|
yield PlacesUtils.bookmarks.remove(aLivemarkInfo.guid,
|
|
|
|
{ source: aLivemarkInfo.source });
|
2015-04-25 11:51:48 +03:00
|
|
|
}.bind(this));
|
2006-10-19 06:24:30 +04:00
|
|
|
},
|
|
|
|
|
2015-04-25 11:51:48 +03:00
|
|
|
reloadLivemarks(aForceUpdate) {
|
2012-03-08 14:14:59 +04:00
|
|
|
// Check if there's a currently running reload, to save some useless work.
|
|
|
|
let notWorthRestarting =
|
|
|
|
this._forceUpdate || // We're already forceUpdating.
|
|
|
|
!aForceUpdate; // The caller didn't request a forced update.
|
|
|
|
if (this._reloading && notWorthRestarting) {
|
|
|
|
// Ignore this call.
|
|
|
|
return;
|
2013-11-20 14:00:02 +04:00
|
|
|
}
|
2012-03-08 14:14:59 +04:00
|
|
|
|
2015-04-25 11:51:48 +03:00
|
|
|
this._promiseLivemarksMap().then(livemarksMap => {
|
2012-03-08 14:14:59 +04:00
|
|
|
this._forceUpdate = !!aForceUpdate;
|
2015-04-25 11:51:48 +03:00
|
|
|
// Livemarks reloads happen on a timer for performance reasons.
|
|
|
|
this._startReloadTimer(livemarksMap, this._forceUpdate, new Set());
|
2013-11-20 14:00:02 +04:00
|
|
|
});
|
2006-10-19 06:24:30 +04:00
|
|
|
},
|
|
|
|
|
2015-04-25 11:51:48 +03:00
|
|
|
getLivemark(aLivemarkInfo) {
|
2012-02-24 16:42:24 +04:00
|
|
|
if (!aLivemarkInfo) {
|
2015-04-25 11:51:48 +03:00
|
|
|
throw new Components.Exception("Invalid arguments", Cr.NS_ERROR_INVALID_ARG);
|
2012-02-24 16:42:24 +04:00
|
|
|
}
|
|
|
|
// Accept either a guid or an id.
|
2015-04-25 11:51:48 +03:00
|
|
|
let hasGuid = "guid" in aLivemarkInfo;
|
|
|
|
let hasId = "id" in aLivemarkInfo;
|
|
|
|
if ((hasGuid && !/^[a-zA-Z0-9\-_]{12}$/.test(aLivemarkInfo.guid)) ||
|
|
|
|
(hasId && aLivemarkInfo.id < 1) ||
|
|
|
|
(!hasId && !hasGuid)) {
|
|
|
|
throw new Components.Exception("Invalid arguments", Cr.NS_ERROR_INVALID_ARG);
|
2006-10-19 06:24:30 +04:00
|
|
|
}
|
|
|
|
|
2015-04-25 11:51:48 +03:00
|
|
|
return Task.spawn(function*() {
|
|
|
|
if (!aLivemarkInfo.guid)
|
|
|
|
aLivemarkInfo.guid = yield PlacesUtils.promiseItemGuid(aLivemarkInfo.id);
|
2014-03-12 19:24:23 +04:00
|
|
|
|
2015-04-25 11:51:48 +03:00
|
|
|
let livemarksMap = yield this._promiseLivemarksMap();
|
|
|
|
if (!livemarksMap.has(aLivemarkInfo.guid))
|
|
|
|
throw new Components.Exception("Invalid livemark", Cr.NS_ERROR_INVALID_ARG);
|
2013-11-20 14:00:02 +04:00
|
|
|
|
2015-04-25 11:51:48 +03:00
|
|
|
return livemarksMap.get(aLivemarkInfo.guid);
|
|
|
|
}.bind(this));
|
2006-10-19 06:24:30 +04:00
|
|
|
},
|
|
|
|
|
2016-11-03 21:42:06 +03:00
|
|
|
// nsINavBookmarkObserver
|
2012-02-24 16:42:24 +04:00
|
|
|
|
2015-04-25 11:51:48 +03:00
|
|
|
onBeginUpdateBatch() {},
|
|
|
|
onEndUpdateBatch() {},
|
|
|
|
onItemVisited() {},
|
|
|
|
onItemAdded() {},
|
2006-10-19 06:24:30 +04:00
|
|
|
|
2015-04-25 11:51:48 +03:00
|
|
|
onItemChanged(id, property, isAnno, value, lastModified, itemType, parentId,
|
|
|
|
guid, parentGuid) {
|
|
|
|
if (itemType != Ci.nsINavBookmarksService.TYPE_FOLDER)
|
|
|
|
return;
|
2014-11-25 15:34:20 +03:00
|
|
|
|
2015-04-25 11:51:48 +03:00
|
|
|
this._promiseLivemarksMap().then(livemarksMap => {
|
|
|
|
if (livemarksMap.has(guid)) {
|
|
|
|
let livemark = livemarksMap.get(guid);
|
|
|
|
if (property == "title") {
|
|
|
|
livemark.title = value;
|
|
|
|
}
|
|
|
|
livemark.lastModified = lastModified;
|
2012-02-24 16:42:24 +04:00
|
|
|
}
|
2015-04-25 11:51:48 +03:00
|
|
|
});
|
2012-02-24 16:42:24 +04:00
|
|
|
},
|
2007-05-10 12:05:19 +04:00
|
|
|
|
2015-04-25 11:51:48 +03:00
|
|
|
onItemMoved(id, parentId, oldIndex, newParentId, newIndex, itemType, guid,
|
|
|
|
oldParentGuid, newParentGuid) {
|
|
|
|
if (itemType != Ci.nsINavBookmarksService.TYPE_FOLDER)
|
|
|
|
return;
|
|
|
|
|
|
|
|
this._promiseLivemarksMap().then(livemarksMap => {
|
|
|
|
if (livemarksMap.has(guid)) {
|
|
|
|
let livemark = livemarksMap.get(guid);
|
|
|
|
livemark.parentId = newParentId;
|
|
|
|
livemark.parentGuid = newParentGuid;
|
|
|
|
livemark.index = newIndex;
|
|
|
|
}
|
|
|
|
});
|
2006-10-19 06:24:30 +04:00
|
|
|
},
|
|
|
|
|
2015-04-25 11:51:48 +03:00
|
|
|
onItemRemoved(id, parentId, index, itemType, uri, guid, parentGuid) {
|
|
|
|
if (itemType != Ci.nsINavBookmarksService.TYPE_FOLDER)
|
|
|
|
return;
|
|
|
|
|
|
|
|
this._promiseLivemarksMap().then(livemarksMap => {
|
|
|
|
if (livemarksMap.has(guid)) {
|
|
|
|
let livemark = livemarksMap.get(guid);
|
|
|
|
livemark.terminate();
|
|
|
|
livemarksMap.delete(guid);
|
|
|
|
}
|
|
|
|
});
|
2006-10-19 06:24:30 +04:00
|
|
|
},
|
|
|
|
|
2016-11-03 21:42:06 +03:00
|
|
|
// nsINavHistoryObserver
|
2011-06-15 03:54:33 +04:00
|
|
|
|
2015-04-25 11:51:48 +03:00
|
|
|
onPageChanged() {},
|
|
|
|
onTitleChanged() {},
|
|
|
|
onDeleteVisits() {},
|
|
|
|
|
2015-03-28 15:33:00 +03:00
|
|
|
onClearHistory() {
|
2015-04-25 11:51:48 +03:00
|
|
|
this._promiseLivemarksMap().then(livemarksMap => {
|
|
|
|
for (let livemark of livemarksMap.values()) {
|
|
|
|
livemark.updateURIVisitedStatus(null, false);
|
|
|
|
}
|
|
|
|
});
|
2015-03-28 15:33:00 +03:00
|
|
|
},
|
2007-08-03 00:19:44 +04:00
|
|
|
|
2015-04-25 11:51:48 +03:00
|
|
|
onDeleteURI(aURI) {
|
|
|
|
this._promiseLivemarksMap().then(livemarksMap => {
|
|
|
|
for (let livemark of livemarksMap.values()) {
|
|
|
|
livemark.updateURIVisitedStatus(aURI, false);
|
|
|
|
}
|
|
|
|
});
|
2012-02-24 16:42:24 +04:00
|
|
|
},
|
2008-02-23 12:38:39 +03:00
|
|
|
|
2015-04-25 11:51:48 +03:00
|
|
|
onVisit(aURI) {
|
|
|
|
this._promiseLivemarksMap().then(livemarksMap => {
|
|
|
|
for (let livemark of livemarksMap.values()) {
|
|
|
|
livemark.updateURIVisitedStatus(aURI, true);
|
|
|
|
}
|
|
|
|
});
|
2006-10-19 06:24:30 +04:00
|
|
|
},
|
|
|
|
|
2016-11-03 21:42:06 +03:00
|
|
|
// nsISupports
|
2012-02-07 13:17:42 +04:00
|
|
|
|
2009-11-06 18:27:51 +03:00
|
|
|
classID: Components.ID("{dca61eb5-c7cd-4df1-b0fb-d0722baba251}"),
|
|
|
|
|
2012-02-07 13:17:42 +04:00
|
|
|
_xpcom_factory: XPCOMUtils.generateSingletonFactory(LivemarkService),
|
|
|
|
|
2009-11-06 18:27:51 +03:00
|
|
|
QueryInterface: XPCOMUtils.generateQI([
|
2013-02-28 01:12:49 +04:00
|
|
|
Ci.mozIAsyncLivemarks
|
2009-11-06 18:27:51 +03:00
|
|
|
, Ci.nsINavBookmarkObserver
|
2012-02-24 16:42:24 +04:00
|
|
|
, Ci.nsINavHistoryObserver
|
2009-11-06 18:27:51 +03:00
|
|
|
, Ci.nsIObserver
|
2012-02-24 16:42:24 +04:00
|
|
|
, Ci.nsISupportsWeakReference
|
2009-11-06 18:27:51 +03:00
|
|
|
])
|
2006-10-19 06:24:30 +04:00
|
|
|
};
|
|
|
|
|
2016-11-03 21:42:06 +03:00
|
|
|
// Livemark
|
2012-02-24 16:42:24 +04:00
|
|
|
|
2011-06-15 03:54:33 +04:00
|
|
|
/**
|
|
|
|
* Object used internally to represent a livemark.
|
|
|
|
*
|
2012-02-24 16:42:24 +04:00
|
|
|
* @param aLivemarkInfo
|
|
|
|
* Object containing information on the livemark. If the livemark is
|
|
|
|
* not included in the object, a new livemark will be created.
|
2011-06-15 03:54:33 +04:00
|
|
|
*
|
|
|
|
* @note terminate() must be invoked before getting rid of this object.
|
|
|
|
*/
|
2016-12-31 05:47:25 +03:00
|
|
|
function Livemark(aLivemarkInfo) {
|
2015-04-25 11:51:48 +03:00
|
|
|
this.id = aLivemarkInfo.id;
|
|
|
|
this.guid = aLivemarkInfo.guid;
|
|
|
|
this.feedURI = aLivemarkInfo.feedURI;
|
|
|
|
this.siteURI = aLivemarkInfo.siteURI || null;
|
2012-02-24 16:42:24 +04:00
|
|
|
this.title = aLivemarkInfo.title;
|
|
|
|
this.parentId = aLivemarkInfo.parentId;
|
2015-04-25 11:51:48 +03:00
|
|
|
this.parentGuid = aLivemarkInfo.parentGuid;
|
2012-02-24 16:42:24 +04:00
|
|
|
this.index = aLivemarkInfo.index;
|
2015-04-25 11:51:48 +03:00
|
|
|
this.dateAdded = aLivemarkInfo.dateAdded;
|
|
|
|
this.lastModified = aLivemarkInfo.lastModified;
|
2012-02-24 16:42:24 +04:00
|
|
|
|
|
|
|
this._status = Ci.mozILivemark.STATUS_READY;
|
|
|
|
|
|
|
|
// Hash of resultObservers, hashed by container.
|
2012-07-24 18:20:57 +04:00
|
|
|
this._resultObservers = new Map();
|
2012-02-24 16:42:24 +04:00
|
|
|
|
|
|
|
// Sorted array of objects representing livemark children in the form
|
|
|
|
// { uri, title, visited }.
|
|
|
|
this._children = [];
|
|
|
|
|
|
|
|
// Keeps a separate array of nodes for each requesting container, hashed by
|
|
|
|
// the container itself.
|
2012-07-24 18:20:57 +04:00
|
|
|
this._nodes = new Map();
|
2012-02-24 16:42:24 +04:00
|
|
|
|
|
|
|
this.loadGroup = null;
|
|
|
|
this.expireTime = 0;
|
2006-10-19 06:24:30 +04:00
|
|
|
}
|
|
|
|
|
2011-06-15 03:54:33 +04:00
|
|
|
Livemark.prototype = {
|
2015-09-24 14:32:23 +03:00
|
|
|
get status() {
|
|
|
|
return this._status;
|
|
|
|
},
|
2012-02-24 16:42:24 +04:00
|
|
|
set status(val) {
|
|
|
|
if (this._status != val) {
|
|
|
|
this._status = val;
|
|
|
|
this._invalidateRegisteredContainers();
|
|
|
|
}
|
|
|
|
return this._status;
|
|
|
|
},
|
2011-06-15 03:54:33 +04:00
|
|
|
|
2016-10-30 21:44:11 +03:00
|
|
|
writeFeedURI(aFeedURI, aSource) {
|
2012-02-24 16:42:24 +04:00
|
|
|
PlacesUtils.annotations
|
2015-04-25 11:51:48 +03:00
|
|
|
.setItemAnnotation(this.id, PlacesUtils.LMANNO_FEEDURI,
|
|
|
|
aFeedURI.spec,
|
2016-10-30 21:44:11 +03:00
|
|
|
0, PlacesUtils.annotations.EXPIRE_NEVER,
|
|
|
|
aSource);
|
2012-02-24 16:42:24 +04:00
|
|
|
this.feedURI = aFeedURI;
|
2011-06-15 03:54:33 +04:00
|
|
|
},
|
2007-06-01 04:44:43 +04:00
|
|
|
|
2016-10-30 21:44:11 +03:00
|
|
|
writeSiteURI(aSiteURI, aSource) {
|
2011-06-15 03:54:33 +04:00
|
|
|
if (!aSiteURI) {
|
2012-02-24 16:42:24 +04:00
|
|
|
PlacesUtils.annotations.removeItemAnnotation(this.id,
|
2016-10-30 21:44:11 +03:00
|
|
|
PlacesUtils.LMANNO_SITEURI,
|
|
|
|
aSource)
|
2012-02-24 16:42:24 +04:00
|
|
|
this.siteURI = null;
|
2011-06-15 03:54:33 +04:00
|
|
|
return;
|
2007-06-01 04:44:43 +04:00
|
|
|
}
|
|
|
|
|
2011-06-15 03:54:33 +04:00
|
|
|
// Security check the site URI against the feed URI principal.
|
2015-04-25 11:51:48 +03:00
|
|
|
let secMan = Services.scriptSecurityManager;
|
2016-05-24 13:01:34 +03:00
|
|
|
let feedPrincipal = secMan.createCodebasePrincipal(this.feedURI, {});
|
2011-06-15 03:54:33 +04:00
|
|
|
try {
|
2015-04-25 11:51:48 +03:00
|
|
|
secMan.checkLoadURIWithPrincipal(feedPrincipal, aSiteURI,
|
|
|
|
Ci.nsIScriptSecurityManager.DISALLOW_INHERIT_PRINCIPAL);
|
2016-12-31 05:47:25 +03:00
|
|
|
} catch (ex) {
|
2011-06-15 03:54:33 +04:00
|
|
|
return;
|
2008-01-06 07:45:01 +03:00
|
|
|
}
|
2007-09-17 05:46:27 +04:00
|
|
|
|
2015-04-25 11:51:48 +03:00
|
|
|
PlacesUtils.annotations
|
|
|
|
.setItemAnnotation(this.id, PlacesUtils.LMANNO_SITEURI,
|
|
|
|
aSiteURI.spec,
|
2016-10-30 21:44:11 +03:00
|
|
|
0, PlacesUtils.annotations.EXPIRE_NEVER,
|
|
|
|
aSource);
|
2012-02-24 16:42:24 +04:00
|
|
|
this.siteURI = aSiteURI;
|
2011-06-15 03:54:33 +04:00
|
|
|
},
|
2012-02-24 16:42:24 +04:00
|
|
|
|
2011-06-15 03:54:33 +04:00
|
|
|
/**
|
|
|
|
* Tries to updates the livemark if needed.
|
|
|
|
* The update process is asynchronous.
|
|
|
|
*
|
|
|
|
* @param [optional] aForceUpdate
|
|
|
|
* If true will try to update the livemark even if its contents have
|
|
|
|
* not yet expired.
|
|
|
|
*/
|
2015-04-25 11:51:48 +03:00
|
|
|
updateChildren(aForceUpdate) {
|
2012-02-24 16:42:24 +04:00
|
|
|
// Check if the livemark is already updating.
|
|
|
|
if (this.status == Ci.mozILivemark.STATUS_LOADING)
|
|
|
|
return;
|
2011-06-15 03:54:33 +04:00
|
|
|
|
2012-02-24 16:42:24 +04:00
|
|
|
// Check the TTL/expiration on this, to check if there is no need to update
|
|
|
|
// this livemark.
|
|
|
|
if (!aForceUpdate && this.children.length && this.expireTime > Date.now())
|
|
|
|
return;
|
2011-06-15 03:54:33 +04:00
|
|
|
|
2012-02-24 16:42:24 +04:00
|
|
|
this.status = Ci.mozILivemark.STATUS_LOADING;
|
2007-06-01 04:44:43 +04:00
|
|
|
|
2012-03-08 14:14:59 +04:00
|
|
|
// Setting the status notifies observers that may remove the livemark.
|
|
|
|
if (this._terminated)
|
|
|
|
return;
|
|
|
|
|
2011-06-15 03:54:33 +04:00
|
|
|
try {
|
|
|
|
// Create a load group for the request. This will allow us to
|
|
|
|
// automatically keep track of redirects, so we can always
|
|
|
|
// cancel the channel.
|
2012-02-24 16:42:24 +04:00
|
|
|
let loadgroup = Cc["@mozilla.org/network/load-group;1"].
|
|
|
|
createInstance(Ci.nsILoadGroup);
|
2016-03-18 20:58:58 +03:00
|
|
|
// Creating a CodeBasePrincipal and using it as the loadingPrincipal
|
|
|
|
// is *not* desired and is only tolerated within this file.
|
|
|
|
// TODO: Find the right OriginAttributes and pass something other
|
|
|
|
// than {} to .createCodeBasePrincipal().
|
2015-04-25 11:51:48 +03:00
|
|
|
let channel = NetUtil.newChannel({
|
2016-03-18 20:58:58 +03:00
|
|
|
uri: this.feedURI,
|
2015-08-18 10:01:42 +03:00
|
|
|
loadingPrincipal: Services.scriptSecurityManager.createCodebasePrincipal(this.feedURI, {}),
|
2016-04-14 12:20:47 +03:00
|
|
|
securityFlags: Ci.nsILoadInfo.SEC_ALLOW_CROSS_ORIGIN_DATA_IS_NULL,
|
2015-08-06 21:17:24 +03:00
|
|
|
contentPolicyType: Ci.nsIContentPolicy.TYPE_INTERNAL_XMLHTTPREQUEST
|
2015-04-25 11:51:48 +03:00
|
|
|
}).QueryInterface(Ci.nsIHttpChannel);
|
2011-06-15 03:54:33 +04:00
|
|
|
channel.loadGroup = loadgroup;
|
|
|
|
channel.loadFlags |= Ci.nsIRequest.LOAD_BACKGROUND |
|
2012-03-09 14:14:40 +04:00
|
|
|
Ci.nsIRequest.LOAD_BYPASS_CACHE;
|
2011-06-15 03:54:33 +04:00
|
|
|
channel.requestMethod = "GET";
|
|
|
|
channel.setRequestHeader("X-Moz", "livebookmarks", false);
|
|
|
|
|
|
|
|
// Stream the result to the feed parser with this listener
|
|
|
|
let listener = new LivemarkLoadListener(this);
|
|
|
|
channel.notificationCallbacks = listener;
|
2016-03-18 20:58:58 +03:00
|
|
|
channel.asyncOpen2(listener);
|
2012-02-24 16:42:24 +04:00
|
|
|
|
|
|
|
this.loadGroup = loadgroup;
|
2016-12-31 05:47:25 +03:00
|
|
|
} catch (ex) {
|
2012-02-24 16:42:24 +04:00
|
|
|
this.status = Ci.mozILivemark.STATUS_FAILED;
|
2007-06-01 04:44:43 +04:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2015-04-25 11:51:48 +03:00
|
|
|
reload(aForceUpdate) {
|
2012-02-24 16:42:24 +04:00
|
|
|
this.updateChildren(aForceUpdate);
|
|
|
|
},
|
|
|
|
|
2015-09-24 14:32:23 +03:00
|
|
|
get children() {
|
|
|
|
return this._children;
|
|
|
|
},
|
2012-02-24 16:42:24 +04:00
|
|
|
set children(val) {
|
|
|
|
this._children = val;
|
|
|
|
|
|
|
|
// Discard the previous cached nodes, new ones should be generated.
|
2015-04-25 11:51:48 +03:00
|
|
|
for (let container of this._resultObservers.keys()) {
|
2012-02-24 16:42:24 +04:00
|
|
|
this._nodes.delete(container);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Update visited status for each entry.
|
2015-04-25 11:51:48 +03:00
|
|
|
for (let child of this._children) {
|
|
|
|
asyncHistory.isURIVisited(child.uri, (aURI, aIsVisited) => {
|
|
|
|
this.updateURIVisitedStatus(aURI, aIsVisited);
|
|
|
|
});
|
2012-02-24 16:42:24 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
return this._children;
|
|
|
|
},
|
|
|
|
|
2015-04-25 11:51:48 +03:00
|
|
|
_isURIVisited(aURI) {
|
|
|
|
return this.children.some(child => child.uri.equals(aURI) && child.visited);
|
2012-02-24 16:42:24 +04:00
|
|
|
},
|
|
|
|
|
2015-04-25 11:51:48 +03:00
|
|
|
getNodesForContainer(aContainerNode) {
|
2012-02-24 16:42:24 +04:00
|
|
|
if (this._nodes.has(aContainerNode)) {
|
|
|
|
return this._nodes.get(aContainerNode);
|
|
|
|
}
|
|
|
|
|
|
|
|
let livemark = this;
|
|
|
|
let nodes = [];
|
|
|
|
let now = Date.now() * 1000;
|
2015-04-25 11:51:48 +03:00
|
|
|
for (let child of this.children) {
|
2012-02-24 16:42:24 +04:00
|
|
|
let node = {
|
|
|
|
// The QueryInterface is needed cause aContainerNode is a jsval.
|
|
|
|
// This is required to avoid issues with scriptable wrappers that would
|
|
|
|
// not allow the view to correctly set expandos.
|
2015-09-24 14:32:23 +03:00
|
|
|
get parent() {
|
|
|
|
return aContainerNode.QueryInterface(Ci.nsINavHistoryContainerResultNode);
|
|
|
|
},
|
|
|
|
get parentResult() {
|
|
|
|
return this.parent.parentResult;
|
|
|
|
},
|
|
|
|
get uri() {
|
2016-12-22 14:03:28 +03:00
|
|
|
return child.uri.spec;
|
2015-09-24 14:32:23 +03:00
|
|
|
},
|
|
|
|
get type() {
|
|
|
|
return Ci.nsINavHistoryResultNode.RESULT_TYPE_URI;
|
|
|
|
},
|
|
|
|
get title() {
|
2016-12-22 14:03:28 +03:00
|
|
|
return child.title;
|
2015-09-24 14:32:23 +03:00
|
|
|
},
|
|
|
|
get accessCount() {
|
|
|
|
return Number(livemark._isURIVisited(NetUtil.newURI(this.uri)));
|
|
|
|
},
|
|
|
|
get time() {
|
|
|
|
return 0;
|
|
|
|
},
|
|
|
|
get icon() {
|
|
|
|
return "";
|
|
|
|
},
|
|
|
|
get indentLevel() {
|
|
|
|
return this.parent.indentLevel + 1;
|
|
|
|
},
|
|
|
|
get bookmarkIndex() {
|
|
|
|
return -1;
|
|
|
|
},
|
|
|
|
get itemId() {
|
|
|
|
return -1;
|
|
|
|
},
|
|
|
|
get dateAdded() {
|
|
|
|
return now;
|
|
|
|
},
|
|
|
|
get lastModified() {
|
|
|
|
return now;
|
|
|
|
},
|
|
|
|
get tags() {
|
|
|
|
return PlacesUtils.tagging.getTagsForURI(NetUtil.newURI(this.uri)).join(", ");
|
|
|
|
},
|
2012-02-24 16:42:24 +04:00
|
|
|
QueryInterface: XPCOMUtils.generateQI([Ci.nsINavHistoryResultNode])
|
|
|
|
};
|
|
|
|
nodes.push(node);
|
|
|
|
}
|
|
|
|
this._nodes.set(aContainerNode, nodes);
|
|
|
|
return nodes;
|
|
|
|
},
|
|
|
|
|
2015-04-25 11:51:48 +03:00
|
|
|
registerForUpdates(aContainerNode, aResultObserver) {
|
2012-02-24 16:42:24 +04:00
|
|
|
this._resultObservers.set(aContainerNode, aResultObserver);
|
|
|
|
},
|
|
|
|
|
2015-04-25 11:51:48 +03:00
|
|
|
unregisterForUpdates(aContainerNode) {
|
2012-02-24 16:42:24 +04:00
|
|
|
this._resultObservers.delete(aContainerNode);
|
|
|
|
this._nodes.delete(aContainerNode);
|
|
|
|
},
|
|
|
|
|
2015-04-25 11:51:48 +03:00
|
|
|
_invalidateRegisteredContainers() {
|
|
|
|
for (let [ container, observer ] of this._resultObservers) {
|
2012-02-24 16:42:24 +04:00
|
|
|
observer.invalidateContainer(container);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2015-03-28 15:33:00 +03:00
|
|
|
/**
|
|
|
|
* Updates the visited status of nodes observing this livemark.
|
|
|
|
*
|
|
|
|
* @param aURI
|
|
|
|
* If provided will update nodes having the given uri,
|
|
|
|
* otherwise any node.
|
|
|
|
* @param aVisitedStatus
|
|
|
|
* Whether the nodes should be set as visited.
|
|
|
|
*/
|
2015-04-25 11:51:48 +03:00
|
|
|
updateURIVisitedStatus(aURI, aVisitedStatus) {
|
|
|
|
for (let child of this.children) {
|
|
|
|
if (!aURI || child.uri.equals(aURI)) {
|
|
|
|
child.visited = aVisitedStatus;
|
2012-02-24 16:42:24 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-25 11:51:48 +03:00
|
|
|
for (let [ container, observer ] of this._resultObservers) {
|
2012-02-24 16:42:24 +04:00
|
|
|
if (this._nodes.has(container)) {
|
|
|
|
let nodes = this._nodes.get(container);
|
2015-04-25 11:51:48 +03:00
|
|
|
for (let node of nodes) {
|
2015-03-28 15:33:00 +03:00
|
|
|
if (!aURI || node.uri == aURI.spec) {
|
2017-04-14 19:29:12 +03:00
|
|
|
Services.tm.dispatchToMainThread(() => {
|
2016-12-22 14:03:28 +03:00
|
|
|
observer.nodeHistoryDetailsChanged(node, 0, aVisitedStatus);
|
2017-04-14 19:29:12 +03:00
|
|
|
});
|
2012-02-24 16:42:24 +04:00
|
|
|
}
|
|
|
|
}
|
2011-06-15 03:54:33 +04:00
|
|
|
}
|
2012-02-24 16:42:24 +04:00
|
|
|
}
|
2011-06-15 03:54:33 +04:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Terminates the livemark entry, cancelling any ongoing load.
|
|
|
|
* Must be invoked before destroying the entry.
|
|
|
|
*/
|
2015-04-25 11:51:48 +03:00
|
|
|
terminate() {
|
2012-03-08 14:14:59 +04:00
|
|
|
// Avoid handling any updateChildren request from now on.
|
|
|
|
this._terminated = true;
|
2011-06-15 03:54:33 +04:00
|
|
|
this.abort();
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Aborts the livemark loading if needed.
|
|
|
|
*/
|
2015-04-25 11:51:48 +03:00
|
|
|
abort() {
|
2012-02-24 16:42:24 +04:00
|
|
|
this.status = Ci.mozILivemark.STATUS_FAILED;
|
2011-06-15 03:54:33 +04:00
|
|
|
if (this.loadGroup) {
|
|
|
|
this.loadGroup.cancel(Cr.NS_BINDING_ABORTED);
|
|
|
|
this.loadGroup = null;
|
|
|
|
}
|
|
|
|
},
|
2012-02-24 16:42:24 +04:00
|
|
|
|
|
|
|
QueryInterface: XPCOMUtils.generateQI([
|
|
|
|
Ci.mozILivemark
|
|
|
|
])
|
2011-06-15 03:54:33 +04:00
|
|
|
}
|
|
|
|
|
2016-11-03 21:42:06 +03:00
|
|
|
// LivemarkLoadListener
|
2012-02-24 16:42:24 +04:00
|
|
|
|
2011-06-15 03:54:33 +04:00
|
|
|
/**
|
|
|
|
* Object used internally to handle loading a livemark's contents.
|
|
|
|
*
|
|
|
|
* @param aLivemark
|
|
|
|
* The Livemark that is loading.
|
|
|
|
*/
|
2015-04-25 11:51:48 +03:00
|
|
|
function LivemarkLoadListener(aLivemark) {
|
2011-06-15 03:54:33 +04:00
|
|
|
this._livemark = aLivemark;
|
|
|
|
this._processor = null;
|
|
|
|
this._isAborted = false;
|
2012-02-24 16:42:24 +04:00
|
|
|
this._ttl = EXPIRE_TIME_MS;
|
2011-06-15 03:54:33 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
LivemarkLoadListener.prototype = {
|
2015-04-25 11:51:48 +03:00
|
|
|
abort(aException) {
|
2012-02-24 16:42:24 +04:00
|
|
|
if (!this._isAborted) {
|
|
|
|
this._isAborted = true;
|
|
|
|
this._livemark.abort();
|
|
|
|
this._setResourceTTL(ONERROR_EXPIRE_TIME_MS);
|
|
|
|
}
|
2011-06-15 03:54:33 +04:00
|
|
|
},
|
|
|
|
|
|
|
|
// nsIFeedResultListener
|
2015-04-25 11:51:48 +03:00
|
|
|
handleResult(aResult) {
|
2006-10-19 06:24:30 +04:00
|
|
|
if (this._isAborted) {
|
|
|
|
return;
|
|
|
|
}
|
2011-06-15 03:54:33 +04:00
|
|
|
|
2007-06-01 04:44:43 +04:00
|
|
|
try {
|
2011-06-15 03:54:33 +04:00
|
|
|
// We need this to make sure the item links are safe
|
2012-07-21 11:29:40 +04:00
|
|
|
let feedPrincipal =
|
2015-04-25 11:51:48 +03:00
|
|
|
Services.scriptSecurityManager
|
2016-05-24 13:01:34 +03:00
|
|
|
.createCodebasePrincipal(this._livemark.feedURI, {});
|
2011-06-15 03:54:33 +04:00
|
|
|
|
|
|
|
// Enforce well-formedness because the existing code does
|
|
|
|
if (!aResult || !aResult.doc || aResult.bozo) {
|
2012-02-24 16:42:24 +04:00
|
|
|
throw new Components.Exception("", Cr.NS_ERROR_FAILURE);
|
2011-06-15 03:54:33 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
let feed = aResult.doc.QueryInterface(Ci.nsIFeed);
|
2011-06-15 03:54:35 +04:00
|
|
|
let siteURI = this._livemark.siteURI;
|
|
|
|
if (feed.link && (!siteURI || !feed.link.equals(siteURI))) {
|
2012-02-24 16:42:24 +04:00
|
|
|
siteURI = feed.link;
|
|
|
|
this._livemark.writeSiteURI(siteURI);
|
2011-06-15 03:54:33 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Insert feed items.
|
|
|
|
let livemarkChildren = [];
|
|
|
|
for (let i = 0; i < feed.items.length; ++i) {
|
|
|
|
let entry = feed.items.queryElementAt(i, Ci.nsIFeedEntry);
|
2012-02-24 16:42:24 +04:00
|
|
|
let uri = entry.link || siteURI;
|
|
|
|
if (!uri) {
|
2011-06-15 03:54:33 +04:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
2015-04-25 11:51:48 +03:00
|
|
|
Services.scriptSecurityManager
|
|
|
|
.checkLoadURIWithPrincipal(feedPrincipal, uri,
|
|
|
|
Ci.nsIScriptSecurityManager.DISALLOW_INHERIT_PRINCIPAL);
|
2016-12-31 05:47:25 +03:00
|
|
|
} catch (ex) {
|
2011-06-15 03:54:33 +04:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
let title = entry.title ? entry.title.plainText() : "";
|
2016-12-30 02:34:54 +03:00
|
|
|
livemarkChildren.push({ uri, title, visited: false });
|
2011-06-15 03:54:33 +04:00
|
|
|
}
|
|
|
|
|
2012-02-24 16:42:24 +04:00
|
|
|
this._livemark.children = livemarkChildren;
|
2016-12-31 05:47:25 +03:00
|
|
|
} catch (ex) {
|
2012-02-24 16:42:24 +04:00
|
|
|
this.abort(ex);
|
2016-12-31 05:47:25 +03:00
|
|
|
} finally {
|
2006-10-19 06:24:30 +04:00
|
|
|
this._processor.listener = null;
|
|
|
|
this._processor = null;
|
|
|
|
}
|
|
|
|
},
|
2007-12-14 09:04:49 +03:00
|
|
|
|
2015-04-25 11:51:48 +03:00
|
|
|
onDataAvailable(aRequest, aContext, aInputStream, aSourceOffset, aCount) {
|
2011-06-15 03:54:33 +04:00
|
|
|
if (this._processor) {
|
2009-05-02 09:17:07 +04:00
|
|
|
this._processor.onDataAvailable(aRequest, aContext, aInputStream,
|
|
|
|
aSourceOffset, aCount);
|
2011-06-15 03:54:33 +04:00
|
|
|
}
|
2006-10-19 06:24:30 +04:00
|
|
|
},
|
2007-12-14 09:04:49 +03:00
|
|
|
|
2015-04-25 11:51:48 +03:00
|
|
|
onStartRequest(aRequest, aContext) {
|
2011-06-15 03:54:33 +04:00
|
|
|
if (this._isAborted) {
|
2015-04-25 11:51:48 +03:00
|
|
|
throw new Components.Exception("", Cr.NS_ERROR_UNEXPECTED);
|
2011-06-15 03:54:33 +04:00
|
|
|
}
|
2006-10-19 06:24:30 +04:00
|
|
|
|
2011-06-15 03:54:33 +04:00
|
|
|
let channel = aRequest.QueryInterface(Ci.nsIChannel);
|
2009-07-07 14:41:23 +04:00
|
|
|
try {
|
2012-02-24 16:42:24 +04:00
|
|
|
// Parse feed data as it comes in
|
|
|
|
this._processor = Cc["@mozilla.org/feed-processor;1"].
|
|
|
|
createInstance(Ci.nsIFeedProcessor);
|
|
|
|
this._processor.listener = this;
|
|
|
|
this._processor.parseAsync(null, channel.URI);
|
2009-07-07 14:41:23 +04:00
|
|
|
this._processor.onStartRequest(aRequest, aContext);
|
2016-12-31 05:47:25 +03:00
|
|
|
} catch (ex) {
|
2009-07-07 14:41:23 +04:00
|
|
|
Components.utils.reportError("Livemark Service: feed processor received an invalid channel for " + channel.URI.spec);
|
2012-02-24 16:42:24 +04:00
|
|
|
this.abort(ex);
|
2009-07-07 14:41:23 +04:00
|
|
|
}
|
2006-10-19 06:24:30 +04:00
|
|
|
},
|
2007-12-14 09:04:49 +03:00
|
|
|
|
2015-04-25 11:51:48 +03:00
|
|
|
onStopRequest(aRequest, aContext, aStatus) {
|
2008-03-07 21:35:29 +03:00
|
|
|
if (!Components.isSuccessCode(aStatus)) {
|
2011-06-15 03:54:33 +04:00
|
|
|
this.abort();
|
2006-10-19 06:24:30 +04:00
|
|
|
return;
|
|
|
|
}
|
2011-06-15 03:54:33 +04:00
|
|
|
|
|
|
|
// Set an expiration on the livemark, to reloading the data in future.
|
2007-12-14 09:04:49 +03:00
|
|
|
try {
|
2011-06-15 03:54:33 +04:00
|
|
|
if (this._processor) {
|
2009-05-02 09:17:07 +04:00
|
|
|
this._processor.onStopRequest(aRequest, aContext, aStatus);
|
2011-06-15 03:54:33 +04:00
|
|
|
}
|
2006-10-19 06:24:30 +04:00
|
|
|
|
|
|
|
// Calculate a new ttl
|
2011-06-15 03:54:33 +04:00
|
|
|
let channel = aRequest.QueryInterface(Ci.nsICachingChannel);
|
2006-10-19 06:24:30 +04:00
|
|
|
if (channel) {
|
2013-09-20 13:11:25 +04:00
|
|
|
let entryInfo = channel.cacheToken.QueryInterface(Ci.nsICacheEntry);
|
2006-10-19 06:24:30 +04:00
|
|
|
if (entryInfo) {
|
2013-09-20 13:11:25 +04:00
|
|
|
// nsICacheEntry returns value as seconds.
|
2011-06-15 03:54:33 +04:00
|
|
|
let expireTime = entryInfo.expirationTime * 1000;
|
|
|
|
let nowTime = Date.now();
|
2012-02-24 16:42:24 +04:00
|
|
|
// Note, expireTime can be 0, see bug 383538.
|
2008-03-07 21:35:29 +03:00
|
|
|
if (expireTime > nowTime) {
|
2012-02-24 16:42:24 +04:00
|
|
|
this._setResourceTTL(Math.max((expireTime - nowTime),
|
|
|
|
EXPIRE_TIME_MS));
|
2007-06-12 19:56:36 +04:00
|
|
|
return;
|
2006-10-19 06:24:30 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2012-02-24 16:42:24 +04:00
|
|
|
this._setResourceTTL(EXPIRE_TIME_MS);
|
2016-12-31 05:47:25 +03:00
|
|
|
} catch (ex) {
|
2012-02-24 16:42:24 +04:00
|
|
|
this.abort(ex);
|
2016-12-31 05:47:25 +03:00
|
|
|
} finally {
|
2012-02-24 16:42:24 +04:00
|
|
|
if (this._livemark.status == Ci.mozILivemark.STATUS_LOADING) {
|
|
|
|
this._livemark.status = Ci.mozILivemark.STATUS_READY;
|
|
|
|
}
|
2011-06-15 03:54:33 +04:00
|
|
|
this._livemark.locked = false;
|
|
|
|
this._livemark.loadGroup = null;
|
|
|
|
}
|
2006-10-19 06:24:30 +04:00
|
|
|
},
|
|
|
|
|
2015-04-25 11:51:48 +03:00
|
|
|
_setResourceTTL(aMilliseconds) {
|
2011-06-15 03:54:33 +04:00
|
|
|
this._livemark.expireTime = Date.now() + aMilliseconds;
|
2006-10-19 06:24:30 +04:00
|
|
|
},
|
2007-12-14 09:04:49 +03:00
|
|
|
|
2009-11-06 18:27:51 +03:00
|
|
|
// nsIInterfaceRequestor
|
2015-04-25 11:51:48 +03:00
|
|
|
getInterface(aIID) {
|
2008-03-07 21:35:29 +03:00
|
|
|
return this.QueryInterface(aIID);
|
2008-01-15 18:06:34 +03:00
|
|
|
},
|
|
|
|
|
2006-10-19 06:24:30 +04:00
|
|
|
// nsISupports
|
2009-11-06 18:27:51 +03:00
|
|
|
QueryInterface: XPCOMUtils.generateQI([
|
|
|
|
Ci.nsIFeedResultListener
|
|
|
|
, Ci.nsIStreamListener
|
|
|
|
, Ci.nsIRequestObserver
|
|
|
|
, Ci.nsIInterfaceRequestor
|
|
|
|
])
|
|
|
|
}
|
2006-10-19 06:24:30 +04:00
|
|
|
|
2012-10-31 20:13:28 +04:00
|
|
|
this.NSGetFactory = XPCOMUtils.generateNSGetFactory([LivemarkService]);
|