2008-06-27 01:49:01 +04: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 23:26:14 +03:00
|
|
|
* The Original Code is Weave
|
2008-06-27 01:49:01 +04: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-04 00:56:16 +04:00
|
|
|
const EXPORTED_SYMBOLS = ['HistoryEngine'];
|
|
|
|
|
|
|
|
const Cc = Components.classes;
|
|
|
|
const Ci = Components.interfaces;
|
|
|
|
const Cu = Components.utils;
|
|
|
|
|
2009-08-22 01:29:37 +04:00
|
|
|
const GUID_ANNO = "weave/guid";
|
2008-12-17 13:32:00 +03:00
|
|
|
|
2009-08-22 01:29:37 +04:00
|
|
|
Cu.import("resource://gre/modules/XPCOMUtils.jsm");
|
2010-06-17 01:30:08 +04: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 21:40:24 +04:00
|
|
|
|
2009-08-22 01:29:37 +04: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-11 07:04:34 +04:00
|
|
|
function GUIDForUri(uri, create) {
|
2009-08-22 01:29:37 +04: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-11 07:04:34 +04:00
|
|
|
if (create)
|
|
|
|
return setGUID(uri);
|
2009-08-22 01:29:37 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-12-09 23:26:14 +03:00
|
|
|
function HistoryEngine() {
|
2010-02-12 02:29:15 +03:00
|
|
|
SyncEngine.call(this, "History");
|
2008-06-04 00:56:16 +04:00
|
|
|
}
|
|
|
|
HistoryEngine.prototype = {
|
2008-12-09 23:26:14 +03:00
|
|
|
__proto__: SyncEngine.prototype,
|
2009-04-13 23:54:31 +04:00
|
|
|
_recordObj: HistoryRec,
|
2009-01-07 00:54:18 +03:00
|
|
|
_storeObj: HistoryStore,
|
2009-01-16 01:05:50 +03:00
|
|
|
_trackerObj: HistoryTracker,
|
2009-01-21 00:13:31 +03:00
|
|
|
|
2009-09-01 03:28:00 +04:00
|
|
|
_sync: Utils.batchSync("History", SyncEngine),
|
2009-07-30 22:52:26 +04:00
|
|
|
|
2009-09-01 04:27:30 +04:00
|
|
|
_findDupe: function _findDupe(item) {
|
|
|
|
return GUIDForUri(item.histUri);
|
2009-01-16 01:05:50 +03:00
|
|
|
}
|
2008-06-04 00:56:16 +04:00
|
|
|
};
|
|
|
|
|
2010-02-12 02:29:15 +03:00
|
|
|
function HistoryStore(name) {
|
|
|
|
Store.call(this, name);
|
2010-08-04 16:59:34 +04:00
|
|
|
|
|
|
|
// Explicitly nullify our references to our cached services so we don't leak
|
2010-08-04 19:07:50 +04:00
|
|
|
Svc.Obs.add("places-shutdown", function() {
|
2010-08-04 16:59:34 +04:00
|
|
|
for each([query, stmt] in Iterator(this._stmts))
|
|
|
|
stmt.finalize();
|
|
|
|
this.__hsvc = null;
|
|
|
|
this._stmts = [];
|
|
|
|
}, this);
|
2008-06-04 00:56:16 +04:00
|
|
|
}
|
|
|
|
HistoryStore.prototype = {
|
2008-12-09 23:26:14 +03:00
|
|
|
__proto__: Store.prototype,
|
2008-06-04 00:56:16 +04:00
|
|
|
|
2010-08-04 16:59:34 +04:00
|
|
|
__hsvc: null,
|
2008-06-04 00:56:16 +04:00
|
|
|
get _hsvc() {
|
2010-08-04 16:59:34 +04:00
|
|
|
if (!this.__hsvc)
|
|
|
|
this.__hsvc = Cc["@mozilla.org/browser/nav-history-service;1"].
|
|
|
|
getService(Ci.nsINavHistoryService).
|
|
|
|
QueryInterface(Ci.nsIGlobalHistory2).
|
|
|
|
QueryInterface(Ci.nsIBrowserHistory).
|
|
|
|
QueryInterface(Ci.nsPIPlacesDatabase);
|
|
|
|
return this.__hsvc;
|
2008-06-04 00:56:16 +04:00
|
|
|
},
|
|
|
|
|
2008-12-10 11:57:27 +03:00
|
|
|
get _db() {
|
2008-12-27 08:50:07 +03:00
|
|
|
return this._hsvc.DBConnection;
|
2008-12-10 11:57:27 +03:00
|
|
|
},
|
|
|
|
|
2010-08-04 16:59:34 +04:00
|
|
|
_stmts: [],
|
|
|
|
_getStmt: function(query) {
|
|
|
|
if (query in this._stmts)
|
|
|
|
return this._stmts[query];
|
|
|
|
|
|
|
|
this._log.trace("Creating SQL statement: " + query);
|
|
|
|
return this._stmts[query] = this._db.createStatement(query);
|
|
|
|
},
|
|
|
|
|
2008-12-10 11:57:27 +03:00
|
|
|
get _visitStm() {
|
2010-08-04 16:59:34 +04:00
|
|
|
return this._getStmt(
|
2009-08-22 01:29:30 +04:00
|
|
|
"SELECT visit_type type, visit_date date " +
|
2010-06-29 22:18:21 +04:00
|
|
|
"FROM moz_historyvisits_view " +
|
2009-08-22 01:29:30 +04:00
|
|
|
"WHERE place_id = (" +
|
|
|
|
"SELECT id " +
|
2010-06-29 22:18:21 +04:00
|
|
|
"FROM moz_places_view " +
|
2009-08-22 01:29:30 +04:00
|
|
|
"WHERE url = :url) " +
|
|
|
|
"ORDER BY date DESC LIMIT 10");
|
2008-12-10 11:57:27 +03:00
|
|
|
},
|
|
|
|
|
|
|
|
get _urlStm() {
|
2010-08-04 16:59:34 +04:00
|
|
|
return this._getStmt(
|
2009-10-17 03:18:53 +04:00
|
|
|
"SELECT url, title, frecency " +
|
2010-06-29 22:18:21 +04:00
|
|
|
"FROM moz_places_view " +
|
2009-08-22 01:29:30 +04:00
|
|
|
"WHERE id = (" +
|
|
|
|
"SELECT place_id " +
|
|
|
|
"FROM moz_annos " +
|
|
|
|
"WHERE content = :guid AND anno_attribute_id = (" +
|
|
|
|
"SELECT id " +
|
|
|
|
"FROM moz_anno_attributes " +
|
2009-08-22 01:29:37 +04:00
|
|
|
"WHERE name = '" + GUID_ANNO + "'))");
|
2008-06-04 00:56:16 +04:00
|
|
|
},
|
|
|
|
|
2010-05-13 22:44:19 +04:00
|
|
|
get _allUrlStm() {
|
2010-08-04 16:59:34 +04:00
|
|
|
return this._getStmt(
|
2010-05-13 22:44:19 +04:00
|
|
|
"SELECT url " +
|
2010-06-29 22:18:21 +04:00
|
|
|
"FROM moz_places_view " +
|
2010-05-13 22:44:19 +04:00
|
|
|
"WHERE last_visit_date > :cutoff_date " +
|
|
|
|
"ORDER BY frecency DESC " +
|
2010-06-03 02:18:01 +04:00
|
|
|
"LIMIT :max_results");
|
2010-05-13 22:44:19 +04:00
|
|
|
},
|
|
|
|
|
2008-12-10 11:57:27 +03:00
|
|
|
// See bug 320831 for why we use SQL here
|
2008-12-18 03:04:03 +03:00
|
|
|
_getVisits: function HistStore__getVisits(uri) {
|
2010-04-30 01:36:15 +04:00
|
|
|
this._visitStm.params.url = uri;
|
2010-06-09 20:35:01 +04:00
|
|
|
return Utils.queryAsync(this._visitStm, ["date", "type"]);
|
2008-06-04 00:56:16 +04:00
|
|
|
},
|
|
|
|
|
2008-12-10 11:57:27 +03:00
|
|
|
// See bug 468732 for why we use SQL here
|
2008-12-20 04:00:12 +03:00
|
|
|
_findURLByGUID: function HistStore__findURLByGUID(guid) {
|
2010-04-30 01:36:15 +04:00
|
|
|
this._urlStm.params.guid = guid;
|
2010-06-09 20:35:01 +04:00
|
|
|
return Utils.queryAsync(this._urlStm, ["url", "title", "frecency"])[0];
|
2008-12-17 13:32:00 +03:00
|
|
|
},
|
|
|
|
|
|
|
|
changeItemID: function HStore_changeItemID(oldID, newID) {
|
2009-08-22 01:29:37 +04:00
|
|
|
setGUID(this._findURLByGUID(oldID).url, newID);
|
2008-08-08 07:00:35 +04:00
|
|
|
},
|
2008-12-10 11:57:27 +03:00
|
|
|
|
2009-01-03 12:04:17 +03:00
|
|
|
|
|
|
|
getAllIDs: function HistStore_getAllIDs() {
|
2010-05-13 22:44:19 +04: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-03 02:18:01 +04:00
|
|
|
this._allUrlStm.params.max_results = 5000;
|
|
|
|
|
2010-06-09 20:35:01 +04: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-13 00:55:58 +03:00
|
|
|
},
|
|
|
|
|
2008-12-18 03:04:03 +03:00
|
|
|
create: function HistStore_create(record) {
|
2009-09-11 07:04:34 +04:00
|
|
|
// Add the url and set the GUID
|
2009-01-21 00:13:31 +03:00
|
|
|
this.update(record);
|
2009-09-11 07:04:34 +04:00
|
|
|
setGUID(record.histUri, record.id);
|
2008-12-18 03:04:03 +03:00
|
|
|
},
|
|
|
|
|
|
|
|
remove: function HistStore_remove(record) {
|
2009-11-18 22:47:25 +03: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-18 03:04:03 +03:00
|
|
|
},
|
|
|
|
|
|
|
|
update: function HistStore_update(record) {
|
2009-04-14 01:39:29 +04:00
|
|
|
this._log.trace(" -> processing history entry: " + record.histUri);
|
2009-01-21 00:13:31 +03:00
|
|
|
|
2009-04-14 01:39:29 +04:00
|
|
|
let uri = Utils.makeURI(record.histUri);
|
2009-05-12 03:11:04 +04:00
|
|
|
if (!uri) {
|
|
|
|
this._log.warn("Attempted to process invalid URI, skipping");
|
|
|
|
throw "invalid URI in record";
|
|
|
|
}
|
2009-01-21 00:13:31 +03:00
|
|
|
let curvisits = [];
|
|
|
|
if (this.urlExists(uri))
|
2009-04-14 01:39:29 +04:00
|
|
|
curvisits = this._getVisits(record.histUri);
|
2009-01-21 00:13:31 +03:00
|
|
|
|
2009-09-11 08:27:47 +04: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-14 01:39:29 +04:00
|
|
|
this._hsvc.setPageTitle(uri, record.title);
|
2008-12-18 03:04:03 +03:00
|
|
|
},
|
|
|
|
|
2009-01-03 12:04:17 +03:00
|
|
|
itemExists: function HistStore_itemExists(id) {
|
|
|
|
if (this._findURLByGUID(id))
|
|
|
|
return true;
|
|
|
|
return false;
|
|
|
|
},
|
2008-12-10 11:57:27 +03:00
|
|
|
|
2009-01-21 00:13:31 +03:00
|
|
|
urlExists: function HistStore_urlExists(url) {
|
|
|
|
if (typeof(url) == "string")
|
|
|
|
url = Utils.makeURI(url);
|
2009-05-12 03:11:04 +04:00
|
|
|
// Don't call isVisited on a null URL to work around crasher bug 492442.
|
|
|
|
return url ? this._hsvc.isVisited(url) : false;
|
2009-01-21 00:13:31 +03:00
|
|
|
},
|
|
|
|
|
2010-03-06 01:46:48 +03:00
|
|
|
createRecord: function createRecord(guid) {
|
2009-01-03 12:04:17 +03:00
|
|
|
let foo = this._findURLByGUID(guid);
|
|
|
|
let record = new HistoryRec();
|
|
|
|
if (foo) {
|
|
|
|
record.histUri = foo.url;
|
|
|
|
record.title = foo.title;
|
2009-10-17 03:18:53 +04:00
|
|
|
record.sortindex = foo.frecency;
|
2009-01-03 12:04:17 +03:00
|
|
|
record.visits = this._getVisits(record.histUri);
|
2008-06-04 00:56:16 +04:00
|
|
|
}
|
2009-04-03 21:38:47 +04:00
|
|
|
else
|
|
|
|
record.deleted = true;
|
2010-03-06 01:43:11 +03:00
|
|
|
|
2009-01-03 12:04:17 +03:00
|
|
|
return record;
|
2008-06-04 00:56:16 +04:00
|
|
|
},
|
|
|
|
|
2008-12-10 11:57:27 +03:00
|
|
|
wipe: function HistStore_wipe() {
|
|
|
|
this._hsvc.removeAllPages();
|
2008-06-04 00:56:16 +04:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2010-02-12 02:29:15 +03:00
|
|
|
function HistoryTracker(name) {
|
|
|
|
Tracker.call(this, name);
|
2010-08-04 23:07:53 +04:00
|
|
|
Svc.History.addObserver(this, true);
|
2008-06-04 00:56:16 +04:00
|
|
|
}
|
|
|
|
HistoryTracker.prototype = {
|
2008-12-09 23:26:14 +03:00
|
|
|
__proto__: Tracker.prototype,
|
2008-06-04 00:56:16 +04:00
|
|
|
|
2009-11-18 22:47:25 +03:00
|
|
|
QueryInterface: XPCOMUtils.generateQI([
|
|
|
|
Ci.nsINavHistoryObserver,
|
2010-08-04 23:07:53 +04:00
|
|
|
Ci.nsINavHistoryObserver_MOZILLA_1_9_1_ADDITIONS,
|
|
|
|
Ci.nsISupportsWeakReference
|
2009-11-18 22:47:25 +03:00
|
|
|
]),
|
2008-12-17 13:32:00 +03:00
|
|
|
|
2008-12-13 00:55:58 +03:00
|
|
|
onBeginUpdateBatch: function HT_onBeginUpdateBatch() {},
|
|
|
|
onEndUpdateBatch: function HT_onEndUpdateBatch() {},
|
|
|
|
onPageChanged: function HT_onPageChanged() {},
|
|
|
|
onTitleChanged: function HT_onTitleChanged() {},
|
2008-06-04 00:56:16 +04:00
|
|
|
|
|
|
|
/* Every add or remove is worth 1 point.
|
2008-12-13 00:55:58 +03:00
|
|
|
* Clearing the whole history is worth 50 points (see below)
|
2008-06-04 00:56:16 +04:00
|
|
|
*/
|
2008-12-13 00:55:58 +03:00
|
|
|
_upScore: function BMT__upScore() {
|
2009-11-09 20:57:58 +03:00
|
|
|
this.score += 1;
|
2008-06-04 00:56:16 +04:00
|
|
|
},
|
2008-12-13 00:55:58 +03:00
|
|
|
|
|
|
|
onVisit: function HT_onVisit(uri, vid, time, session, referrer, trans) {
|
2009-02-02 22:43:06 +03:00
|
|
|
if (this.ignoreAll)
|
|
|
|
return;
|
2008-12-17 13:32:00 +03:00
|
|
|
this._log.trace("onVisit: " + uri.spec);
|
2009-09-11 07:04:34 +04:00
|
|
|
if (this.addChangedID(GUIDForUri(uri, true)))
|
2009-01-14 02:55:35 +03:00
|
|
|
this._upScore();
|
2008-12-13 00:55:58 +03:00
|
|
|
},
|
2010-04-30 03:42:39 +04:00
|
|
|
onDeleteVisits: function onDeleteVisits() {
|
|
|
|
},
|
2008-06-04 00:56:16 +04:00
|
|
|
onPageExpired: function HT_onPageExpired(uri, time, entry) {
|
|
|
|
},
|
2009-11-18 22:47:25 +03: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-04 00:56:16 +04:00
|
|
|
onDeleteURI: function HT_onDeleteURI(uri) {
|
|
|
|
},
|
|
|
|
onClearHistory: function HT_onClearHistory() {
|
2008-12-13 00:55:58 +03:00
|
|
|
this._log.trace("onClearHistory");
|
2009-11-09 20:57:58 +03:00
|
|
|
this.score += 500;
|
2008-06-04 00:56:16 +04:00
|
|
|
}
|
2008-12-09 23:26:14 +03:00
|
|
|
};
|