2008-06-26 14:49:01 -07:00
|
|
|
/* ***** BEGIN LICENSE BLOCK *****
|
|
|
|
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
|
|
|
|
*
|
|
|
|
* The contents of this file are subject to the Mozilla Public License Version
|
|
|
|
* 1.1 (the "License"); you may not use this file except in compliance with
|
|
|
|
* the License. You may obtain a copy of the License at
|
|
|
|
* http://www.mozilla.org/MPL/
|
|
|
|
*
|
|
|
|
* Software distributed under the License is distributed on an "AS IS" basis,
|
|
|
|
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
|
|
|
|
* for the specific language governing rights and limitations under the
|
|
|
|
* License.
|
|
|
|
*
|
2008-12-09 12:26:14 -08:00
|
|
|
* The Original Code is Weave
|
2008-06-26 14:49:01 -07:00
|
|
|
*
|
|
|
|
* The Initial Developer of the Original Code is Mozilla.
|
|
|
|
* Portions created by the Initial Developer are Copyright (C) 2008
|
|
|
|
* the Initial Developer. All Rights Reserved.
|
|
|
|
*
|
|
|
|
* Contributor(s):
|
|
|
|
* Dan Mills <thunder@mozilla.com>
|
|
|
|
*
|
|
|
|
* Alternatively, the contents of this file may be used under the terms of
|
|
|
|
* either the GNU General Public License Version 2 or later (the "GPL"), or
|
|
|
|
* the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
|
|
|
|
* in which case the provisions of the GPL or the LGPL are applicable instead
|
|
|
|
* of those above. If you wish to allow use of your version of this file only
|
|
|
|
* under the terms of either the GPL or the LGPL, and not to allow others to
|
|
|
|
* use your version of this file under the terms of the MPL, indicate your
|
|
|
|
* decision by deleting the provisions above and replace them with the notice
|
|
|
|
* and other provisions required by the GPL or the LGPL. If you do not delete
|
|
|
|
* the provisions above, a recipient may use your version of this file under
|
|
|
|
* the terms of any one of the MPL, the GPL or the LGPL.
|
|
|
|
*
|
|
|
|
* ***** END LICENSE BLOCK ***** */
|
|
|
|
|
2008-06-03 13:56:16 -07:00
|
|
|
const EXPORTED_SYMBOLS = ['HistoryEngine'];
|
|
|
|
|
|
|
|
const Cc = Components.classes;
|
|
|
|
const Ci = Components.interfaces;
|
|
|
|
const Cu = Components.utils;
|
|
|
|
|
2009-08-21 14:29:37 -07:00
|
|
|
const GUID_ANNO = "weave/guid";
|
2008-12-17 02:32:00 -08:00
|
|
|
|
2009-08-21 14:29:37 -07:00
|
|
|
Cu.import("resource://gre/modules/XPCOMUtils.jsm");
|
2010-06-16 14:30:08 -07:00
|
|
|
Cu.import("resource://services-sync/engines.js");
|
|
|
|
Cu.import("resource://services-sync/stores.js");
|
|
|
|
Cu.import("resource://services-sync/trackers.js");
|
|
|
|
Cu.import("resource://services-sync/type_records/history.js");
|
|
|
|
Cu.import("resource://services-sync/util.js");
|
2008-06-11 10:40:24 -07:00
|
|
|
|
2009-08-21 14:29:37 -07:00
|
|
|
// Create some helper functions to handle GUIDs
|
|
|
|
function setGUID(uri, guid) {
|
|
|
|
if (arguments.length == 1)
|
|
|
|
guid = Utils.makeGUID();
|
|
|
|
Utils.anno(uri, GUID_ANNO, guid, "WITH_HISTORY");
|
|
|
|
return guid;
|
|
|
|
}
|
2009-09-10 20:04:34 -07:00
|
|
|
function GUIDForUri(uri, create) {
|
2009-08-21 14:29:37 -07:00
|
|
|
try {
|
|
|
|
// Use the existing GUID if it exists
|
|
|
|
return Utils.anno(uri, GUID_ANNO);
|
|
|
|
}
|
|
|
|
catch (ex) {
|
|
|
|
// Give the uri a GUID if it doesn't have one
|
2009-09-10 20:04:34 -07:00
|
|
|
if (create)
|
|
|
|
return setGUID(uri);
|
2009-08-21 14:29:37 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-12-09 12:26:14 -08:00
|
|
|
function HistoryEngine() {
|
2010-02-11 15:29:15 -08:00
|
|
|
SyncEngine.call(this, "History");
|
2008-06-03 13:56:16 -07:00
|
|
|
}
|
|
|
|
HistoryEngine.prototype = {
|
2008-12-09 12:26:14 -08:00
|
|
|
__proto__: SyncEngine.prototype,
|
2009-04-13 12:54:31 -07:00
|
|
|
_recordObj: HistoryRec,
|
2009-01-06 13:54:18 -08:00
|
|
|
_storeObj: HistoryStore,
|
2009-01-15 14:05:50 -08:00
|
|
|
_trackerObj: HistoryTracker,
|
2009-01-20 13:13:31 -08:00
|
|
|
|
2009-08-31 16:28:00 -07:00
|
|
|
_sync: Utils.batchSync("History", SyncEngine),
|
2009-07-30 11:52:26 -07:00
|
|
|
|
2009-08-31 17:27:30 -07:00
|
|
|
_findDupe: function _findDupe(item) {
|
|
|
|
return GUIDForUri(item.histUri);
|
2009-01-15 14:05:50 -08:00
|
|
|
}
|
2008-06-03 13:56:16 -07:00
|
|
|
};
|
|
|
|
|
2010-02-11 15:29:15 -08:00
|
|
|
function HistoryStore(name) {
|
|
|
|
Store.call(this, name);
|
2008-06-03 13:56:16 -07:00
|
|
|
}
|
|
|
|
HistoryStore.prototype = {
|
2008-12-09 12:26:14 -08:00
|
|
|
__proto__: Store.prototype,
|
2008-06-03 13:56:16 -07:00
|
|
|
|
|
|
|
get _hsvc() {
|
2008-12-09 12:26:14 -08:00
|
|
|
let hsvc = Cc["@mozilla.org/browser/nav-history-service;1"].
|
|
|
|
getService(Ci.nsINavHistoryService);
|
|
|
|
hsvc.QueryInterface(Ci.nsIGlobalHistory2);
|
|
|
|
hsvc.QueryInterface(Ci.nsIBrowserHistory);
|
2008-12-10 00:57:27 -08:00
|
|
|
hsvc.QueryInterface(Ci.nsPIPlacesDatabase);
|
2008-12-09 12:26:14 -08:00
|
|
|
this.__defineGetter__("_hsvc", function() hsvc);
|
|
|
|
return hsvc;
|
2008-06-03 13:56:16 -07:00
|
|
|
},
|
|
|
|
|
2008-12-10 00:57:27 -08:00
|
|
|
get _db() {
|
2008-12-26 21:50:07 -08:00
|
|
|
return this._hsvc.DBConnection;
|
2008-12-10 00:57:27 -08:00
|
|
|
},
|
|
|
|
|
|
|
|
get _visitStm() {
|
2008-12-19 17:00:12 -08:00
|
|
|
this._log.trace("Creating SQL statement: _visitStm");
|
2009-08-21 14:29:30 -07:00
|
|
|
let stm = this._db.createStatement(
|
|
|
|
"SELECT visit_type type, visit_date date " +
|
2010-06-29 11:18:21 -07:00
|
|
|
"FROM moz_historyvisits_view " +
|
2009-08-21 14:29:30 -07:00
|
|
|
"WHERE place_id = (" +
|
|
|
|
"SELECT id " +
|
2010-06-29 11:18:21 -07:00
|
|
|
"FROM moz_places_view " +
|
2009-08-21 14:29:30 -07:00
|
|
|
"WHERE url = :url) " +
|
|
|
|
"ORDER BY date DESC LIMIT 10");
|
2008-12-19 17:00:12 -08:00
|
|
|
this.__defineGetter__("_visitStm", function() stm);
|
2008-12-17 16:04:03 -08:00
|
|
|
return stm;
|
2008-12-10 00:57:27 -08:00
|
|
|
},
|
|
|
|
|
|
|
|
get _urlStm() {
|
2008-12-19 17:00:12 -08:00
|
|
|
this._log.trace("Creating SQL statement: _urlStm");
|
|
|
|
let stm = this._db.createStatement(
|
2009-10-16 16:18:53 -07:00
|
|
|
"SELECT url, title, frecency " +
|
2010-06-29 11:18:21 -07:00
|
|
|
"FROM moz_places_view " +
|
2009-08-21 14:29:30 -07:00
|
|
|
"WHERE id = (" +
|
|
|
|
"SELECT place_id " +
|
|
|
|
"FROM moz_annos " +
|
|
|
|
"WHERE content = :guid AND anno_attribute_id = (" +
|
|
|
|
"SELECT id " +
|
|
|
|
"FROM moz_anno_attributes " +
|
2009-08-21 14:29:37 -07:00
|
|
|
"WHERE name = '" + GUID_ANNO + "'))");
|
2009-08-21 14:29:30 -07:00
|
|
|
this.__defineGetter__("_urlStm", function() stm);
|
2008-12-17 16:04:03 -08:00
|
|
|
return stm;
|
2008-06-03 13:56:16 -07:00
|
|
|
},
|
|
|
|
|
2010-05-13 11:44:19 -07:00
|
|
|
get _allUrlStm() {
|
|
|
|
this._log.trace("Creating SQL statement: _allUrlStm");
|
|
|
|
let stm = this._db.createStatement(
|
|
|
|
"SELECT url " +
|
2010-06-29 11:18:21 -07:00
|
|
|
"FROM moz_places_view " +
|
2010-05-13 11:44:19 -07:00
|
|
|
"WHERE last_visit_date > :cutoff_date " +
|
|
|
|
"ORDER BY frecency DESC " +
|
2010-06-02 15:18:01 -07:00
|
|
|
"LIMIT :max_results");
|
2010-05-13 11:44:19 -07:00
|
|
|
this.__defineGetter__("_allUrlStm", function() stm);
|
|
|
|
return stm;
|
|
|
|
},
|
|
|
|
|
2008-12-10 00:57:27 -08:00
|
|
|
// See bug 320831 for why we use SQL here
|
2008-12-17 16:04:03 -08:00
|
|
|
_getVisits: function HistStore__getVisits(uri) {
|
2010-04-29 14:36:15 -07:00
|
|
|
this._visitStm.params.url = uri;
|
2010-06-09 09:35:01 -07:00
|
|
|
return Utils.queryAsync(this._visitStm, ["date", "type"]);
|
2008-06-03 13:56:16 -07:00
|
|
|
},
|
|
|
|
|
2008-12-10 00:57:27 -08:00
|
|
|
// See bug 468732 for why we use SQL here
|
2008-12-19 17:00:12 -08:00
|
|
|
_findURLByGUID: function HistStore__findURLByGUID(guid) {
|
2010-04-29 14:36:15 -07:00
|
|
|
this._urlStm.params.guid = guid;
|
2010-06-09 09:35:01 -07:00
|
|
|
return Utils.queryAsync(this._urlStm, ["url", "title", "frecency"])[0];
|
2008-12-17 02:32:00 -08:00
|
|
|
},
|
|
|
|
|
|
|
|
changeItemID: function HStore_changeItemID(oldID, newID) {
|
2009-08-21 14:29:37 -07:00
|
|
|
setGUID(this._findURLByGUID(oldID).url, newID);
|
2008-08-07 20:00:35 -07:00
|
|
|
},
|
2008-12-10 00:57:27 -08:00
|
|
|
|
2009-01-03 01:04:17 -08:00
|
|
|
|
|
|
|
getAllIDs: function HistStore_getAllIDs() {
|
2010-05-13 11:44:19 -07:00
|
|
|
// Only get places visited within the last 30 days (30*24*60*60*1000ms)
|
|
|
|
this._allUrlStm.params.cutoff_date = (Date.now() - 2592000000) * 1000;
|
2010-06-02 15:18:01 -07:00
|
|
|
this._allUrlStm.params.max_results = 5000;
|
|
|
|
|
2010-06-09 09:35:01 -07:00
|
|
|
let urls = Utils.queryAsync(this._allUrlStm, "url");
|
|
|
|
return urls.reduce(function(ids, item) {
|
|
|
|
ids[GUIDForUri(item.url, true)] = item.url;
|
|
|
|
return ids;
|
|
|
|
}, {});
|
2008-12-12 13:55:58 -08:00
|
|
|
},
|
|
|
|
|
2008-12-17 16:04:03 -08:00
|
|
|
create: function HistStore_create(record) {
|
2009-09-10 20:04:34 -07:00
|
|
|
// Add the url and set the GUID
|
2009-01-20 13:13:31 -08:00
|
|
|
this.update(record);
|
2009-09-10 20:04:34 -07:00
|
|
|
setGUID(record.histUri, record.id);
|
2008-12-17 16:04:03 -08:00
|
|
|
},
|
|
|
|
|
|
|
|
remove: function HistStore_remove(record) {
|
2009-11-18 11:47:25 -08:00
|
|
|
let page = this._findURLByGUID(record.id)
|
|
|
|
if (page == null) {
|
|
|
|
this._log.debug("Page already removed: " + record.id);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
let uri = Utils.makeURI(page.url);
|
|
|
|
Svc.History.removePage(uri);
|
|
|
|
this._log.trace("Removed page: " + [record.id, page.url, page.title]);
|
2008-12-17 16:04:03 -08:00
|
|
|
},
|
|
|
|
|
|
|
|
update: function HistStore_update(record) {
|
2009-04-13 14:39:29 -07:00
|
|
|
this._log.trace(" -> processing history entry: " + record.histUri);
|
2009-01-20 13:13:31 -08:00
|
|
|
|
2009-04-13 14:39:29 -07:00
|
|
|
let uri = Utils.makeURI(record.histUri);
|
2009-05-11 16:11:04 -07:00
|
|
|
if (!uri) {
|
|
|
|
this._log.warn("Attempted to process invalid URI, skipping");
|
|
|
|
throw "invalid URI in record";
|
|
|
|
}
|
2009-01-20 13:13:31 -08:00
|
|
|
let curvisits = [];
|
|
|
|
if (this.urlExists(uri))
|
2009-04-13 14:39:29 -07:00
|
|
|
curvisits = this._getVisits(record.histUri);
|
2009-01-20 13:13:31 -08:00
|
|
|
|
2009-09-10 21:27:47 -07:00
|
|
|
// Add visits if there's no local visit with the same date
|
|
|
|
for each (let {date, type} in record.visits)
|
|
|
|
if (curvisits.every(function(cur) cur.date != date))
|
|
|
|
Svc.History.addVisit(uri, date, null, type, type == 5 || type == 6, 0);
|
|
|
|
|
2009-04-13 14:39:29 -07:00
|
|
|
this._hsvc.setPageTitle(uri, record.title);
|
2008-12-17 16:04:03 -08:00
|
|
|
},
|
|
|
|
|
2009-01-03 01:04:17 -08:00
|
|
|
itemExists: function HistStore_itemExists(id) {
|
|
|
|
if (this._findURLByGUID(id))
|
|
|
|
return true;
|
|
|
|
return false;
|
|
|
|
},
|
2008-12-10 00:57:27 -08:00
|
|
|
|
2009-01-20 13:13:31 -08:00
|
|
|
urlExists: function HistStore_urlExists(url) {
|
|
|
|
if (typeof(url) == "string")
|
|
|
|
url = Utils.makeURI(url);
|
2009-05-11 16:11:04 -07:00
|
|
|
// Don't call isVisited on a null URL to work around crasher bug 492442.
|
|
|
|
return url ? this._hsvc.isVisited(url) : false;
|
2009-01-20 13:13:31 -08:00
|
|
|
},
|
|
|
|
|
2010-03-05 14:46:48 -08:00
|
|
|
createRecord: function createRecord(guid) {
|
2009-01-03 01:04:17 -08:00
|
|
|
let foo = this._findURLByGUID(guid);
|
|
|
|
let record = new HistoryRec();
|
|
|
|
if (foo) {
|
|
|
|
record.histUri = foo.url;
|
|
|
|
record.title = foo.title;
|
2009-10-16 16:18:53 -07:00
|
|
|
record.sortindex = foo.frecency;
|
2009-01-03 01:04:17 -08:00
|
|
|
record.visits = this._getVisits(record.histUri);
|
2008-06-03 13:56:16 -07:00
|
|
|
}
|
2009-04-03 10:38:47 -07:00
|
|
|
else
|
|
|
|
record.deleted = true;
|
2010-03-05 14:43:11 -08:00
|
|
|
|
2009-01-03 01:04:17 -08:00
|
|
|
return record;
|
2008-06-03 13:56:16 -07:00
|
|
|
},
|
|
|
|
|
2008-12-10 00:57:27 -08:00
|
|
|
wipe: function HistStore_wipe() {
|
|
|
|
this._hsvc.removeAllPages();
|
2008-06-03 13:56:16 -07:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2010-02-11 15:29:15 -08:00
|
|
|
function HistoryTracker(name) {
|
|
|
|
Tracker.call(this, name);
|
2010-02-11 15:25:31 -08:00
|
|
|
Svc.History.addObserver(this, false);
|
2008-06-03 13:56:16 -07:00
|
|
|
}
|
|
|
|
HistoryTracker.prototype = {
|
2008-12-09 12:26:14 -08:00
|
|
|
__proto__: Tracker.prototype,
|
2008-06-03 13:56:16 -07:00
|
|
|
|
2009-11-18 11:47:25 -08:00
|
|
|
QueryInterface: XPCOMUtils.generateQI([
|
|
|
|
Ci.nsINavHistoryObserver,
|
|
|
|
Ci.nsINavHistoryObserver_MOZILLA_1_9_1_ADDITIONS
|
|
|
|
]),
|
2008-12-17 02:32:00 -08:00
|
|
|
|
2008-12-12 13:55:58 -08:00
|
|
|
onBeginUpdateBatch: function HT_onBeginUpdateBatch() {},
|
|
|
|
onEndUpdateBatch: function HT_onEndUpdateBatch() {},
|
|
|
|
onPageChanged: function HT_onPageChanged() {},
|
|
|
|
onTitleChanged: function HT_onTitleChanged() {},
|
2008-06-03 13:56:16 -07:00
|
|
|
|
|
|
|
/* Every add or remove is worth 1 point.
|
2008-12-12 13:55:58 -08:00
|
|
|
* Clearing the whole history is worth 50 points (see below)
|
2008-06-03 13:56:16 -07:00
|
|
|
*/
|
2008-12-12 13:55:58 -08:00
|
|
|
_upScore: function BMT__upScore() {
|
2009-11-09 09:57:58 -08:00
|
|
|
this.score += 1;
|
2008-06-03 13:56:16 -07:00
|
|
|
},
|
2008-12-12 13:55:58 -08:00
|
|
|
|
|
|
|
onVisit: function HT_onVisit(uri, vid, time, session, referrer, trans) {
|
2009-02-02 11:43:06 -08:00
|
|
|
if (this.ignoreAll)
|
|
|
|
return;
|
2008-12-17 02:32:00 -08:00
|
|
|
this._log.trace("onVisit: " + uri.spec);
|
2009-09-10 20:04:34 -07:00
|
|
|
if (this.addChangedID(GUIDForUri(uri, true)))
|
2009-01-13 15:55:35 -08:00
|
|
|
this._upScore();
|
2008-12-12 13:55:58 -08:00
|
|
|
},
|
2010-04-29 16:42:39 -07:00
|
|
|
onDeleteVisits: function onDeleteVisits() {
|
|
|
|
},
|
2008-06-03 13:56:16 -07:00
|
|
|
onPageExpired: function HT_onPageExpired(uri, time, entry) {
|
|
|
|
},
|
2009-11-18 11:47:25 -08:00
|
|
|
onBeforeDeleteURI: function onBeforeDeleteURI(uri) {
|
|
|
|
if (this.ignoreAll)
|
|
|
|
return;
|
|
|
|
this._log.trace("onBeforeDeleteURI: " + uri.spec);
|
|
|
|
if (this.addChangedID(GUIDForUri(uri, true)))
|
|
|
|
this._upScore();
|
|
|
|
},
|
2008-06-03 13:56:16 -07:00
|
|
|
onDeleteURI: function HT_onDeleteURI(uri) {
|
|
|
|
},
|
|
|
|
onClearHistory: function HT_onClearHistory() {
|
2008-12-12 13:55:58 -08:00
|
|
|
this._log.trace("onClearHistory");
|
2009-11-09 09:57:58 -08:00
|
|
|
this.score += 500;
|
2008-06-03 13:56:16 -07:00
|
|
|
}
|
2008-12-09 12:26:14 -08:00
|
|
|
};
|