Bug 394520 Switch urlbar history to sqlite r=jag

git-svn-id: svn://10.0.0.236/trunk@233769 18797224-902f-48f8-a5cc-f745e15eee43
This commit is contained in:
neil%parkwaycc.co.uk 2007-09-02 22:42:14 +00:00
parent 7f54748a50
commit 8863d0702a
3 changed files with 91 additions and 113 deletions

View File

@ -41,11 +41,6 @@
const MAX_HISTORY_MENU_ITEMS = 15;
const MAX_URLBAR_HISTORY_MENU_ITEMS = 30;
const MAX_URLBAR_HISTORY_ITEMS = 100;
var gRDF = null;
var gRDFC = null;
var gGlobalHistory = null;
var gURIFixup = null;
var gLocalStore = null;
function FillHistoryMenu(aParent, aMenu)
{
@ -115,46 +110,35 @@ function executeUrlBarHistoryCommand( aTarget )
function createUBHistoryMenu( aParent )
{
if (!gRDF)
gRDF = Components.classes["@mozilla.org/rdf/rdf-service;1"]
.getService(Components.interfaces.nsIRDFService);
while (aParent.hasChildNodes())
aParent.removeChild(aParent.lastChild);
if (!gLocalStore)
gLocalStore = gRDF.GetDataSource("rdf:local-store");
if (gLocalStore) {
if (!gRDFC)
gRDFC = Components.classes["@mozilla.org/rdf/container-utils;1"]
.getService(Components.interfaces.nsIRDFContainerUtils);
var entries = gRDFC.MakeSeq(gLocalStore, gRDF.GetResource("nc:urlbar-history")).GetElements();
var i = MAX_URLBAR_HISTORY_MENU_ITEMS;
// Delete any old menu items only if there are legitimate
// urls to display, otherwise we want to display the
// '(Nothing Available)' item.
deleteHistoryItems(aParent);
if (!entries.hasMoreElements()) {
//Create the "Nothing Available" Menu item and disable it.
var na = gNavigatorBundle.getString("nothingAvailable");
createMenuItem(aParent, "nothing_available", na);
aParent.firstChild.setAttribute("disabled", "true");
}
while (entries.hasMoreElements() && (i-- > 0)) {
var entry = entries.getNext();
if (entry) {
try {
entry = entry.QueryInterface(Components.interfaces.nsIRDFLiteral);
} catch(ex) {
// XXXbar not an nsIRDFLiteral for some reason. see 90337.
continue;
}
var url = entry.Value;
createMenuItem(aParent, i, url);
var file = Components.classes["@mozilla.org/file/directory_service;1"]
.getService(Components.interfaces.nsIProperties)
.get("ProfD", Components.interfaces.nsIFile);
file.append("urlbarhistory.sqlite");
if (file.exists()) {
var connection = Components.classes["@mozilla.org/storage/service;1"]
.getService(Components.interfaces.mozIStorageService)
.openDatabase(file);
try {
if (connection.tableExists("urlbarhistory")) {
var statement = connection.createStatement(
"SELECT url FROM urlbarhistory ORDER BY ROWID DESC");
while (statement.executeStep())
aParent.appendChild(document.createElement("menuitem"))
.setAttribute("label", statement.getString(0));
statement.reset();
return;
}
} finally {
connection.close();
}
}
//Create the "Nothing Available" Menu item and disable it.
var na = aParent.appendChild(document.createElement("menuitem"));
na.setAttribute("label", gNavigatorBundle.getString("nothingAvailable"));
na.setAttribute("disabled", "true");
}
function createMenuItem( aParent, aIndex, aLabel)

View File

@ -185,6 +185,9 @@
return false;
}
var gGlobalHistory = null;
var gURIFixup = null;
function middleMousePaste( event )
{
var url = readFromClipboard();
@ -245,10 +248,6 @@
if (aUrlToAdd.search(/[\x00-\x1F]/) != -1) // don't store bad URLs
return;
if (!gRDF)
gRDF = Components.classes["@mozilla.org/rdf/rdf-service;1"]
.getService(Components.interfaces.nsIRDFService);
if (!gGlobalHistory)
gGlobalHistory = Components.classes["@mozilla.org/browser/global-history;2"]
.getService(Components.interfaces.nsIBrowserHistory);
@ -256,44 +255,6 @@
if (!gURIFixup)
gURIFixup = Components.classes["@mozilla.org/docshell/urifixup;1"]
.getService(Components.interfaces.nsIURIFixup);
if (!gLocalStore)
gLocalStore = gRDF.GetDataSource("rdf:local-store");
if (!gRDFC)
gRDFC = Components.classes["@mozilla.org/rdf/container-utils;1"]
.getService(Components.interfaces.nsIRDFContainerUtils);
var entries = gRDFC.MakeSeq(gLocalStore, gRDF.GetResource("nc:urlbar-history"));
if (!entries)
return;
var elements = entries.GetElements();
if (!elements)
return;
var index = 0;
var urlToCompare = aUrlToAdd.toUpperCase();
while(elements.hasMoreElements()) {
var entry = elements.getNext();
if (!entry) continue;
index ++;
try {
entry = entry.QueryInterface(Components.interfaces.nsIRDFLiteral);
} catch(ex) {
// XXXbar not an nsIRDFLiteral for some reason. see 90337.
continue;
}
if (urlToCompare == entry.Value.toUpperCase()) {
// URL already present in the database
// Remove it from its current position.
// It is inserted to the top after the while loop.
entries.RemoveElementAt(index, true);
break;
}
} // while
// Otherwise, we've got a new URL in town. Add it!
try {
var url = getShortcutOrURI(aUrlToAdd);
@ -304,16 +265,38 @@
catch(ex) {
}
// Put the value as it was typed by the user in to RDF
// Insert it to the beginning of the list.
var entryToAdd = gRDF.GetLiteral(aUrlToAdd);
entries.InsertElementAt(entryToAdd, 1, true);
// Open or create the urlbar history database.
var file = Components.classes["@mozilla.org/file/directory_service;1"]
.getService(Components.interfaces.nsIProperties)
.get("ProfD", Components.interfaces.nsIFile);
file.append("urlbarhistory.sqlite");
var connection = Components.classes["@mozilla.org/storage/service;1"]
.getService(Components.interfaces.mozIStorageService)
.openDatabase(file);
connection.beginTransaction();
if (!connection.tableExists("urlbarhistory"))
connection.createTable("urlbarhistory", "url TEXT");
// If the URL is already present in the database then remove it from
// its current position. It is then reinserted at the top of the list.
var statement = connection.createStatement(
"DELETE FROM urlbarhistory WHERE LOWER(url) = LOWER(?1)");
statement.bindStringParameter(0, aUrlToAdd);
statement.execute();
// Put the value as it was typed by the user in to urlbar history
statement = connection.createStatement(
"INSERT INTO urlbarhistory (url) VALUES (?1)");
statement.bindStringParameter(0, aUrlToAdd);
statement.execute();
// Remove any expired history items so that we don't let
// this grow without bound.
for (index = entries.GetCount(); index > MAX_URLBAR_HISTORY_ITEMS; --index) {
entries.RemoveElementAt(index, true);
} // for
connection.executeSimpleSQL(
"DELETE FROM urlbarhistory WHERE ROWID NOT IN " +
"(SELECT ROWID FROM urlbarhistory ORDER BY ROWID DESC LIMIT 30)");
connection.commitTransaction();
connection.close();
}
function makeURLAbsolute(base, url)

View File

@ -54,18 +54,23 @@
{
var urlbarHistButton = document.getElementById("ClearUrlBarHistoryButton");
try {
var RDF = Components.classes["@mozilla.org/rdf/rdf-service;1"]
.getService(Components.interfaces.nsIRDFService);
var localStore = RDF.GetDataSource("rdf:local-store");
var RDFC = Components.classes["@mozilla.org/rdf/container-utils;1"]
.getService(Components.interfaces.nsIRDFContainerUtils);
var urlBarHist = RDFC.MakeSeq(localStore, RDF.GetResource("nc:urlbar-history"));
var isBtnLocked = parent.hPrefWindow.getPrefIsLocked(urlbarHistButton.getAttribute("prefstring"));
var pref = Components.classes["@mozilla.org/preferences-service;1"]
.getService(Components.interfaces.nsIPrefBranch);
var lastUrl = pref.getComplexValue("general.open_location.last_url",
Components.interfaces.nsISupportsString).data;
urlbarHistButton.disabled = ( urlBarHist.GetCount() == 0 && !lastUrl ) || isBtnLocked ;
var isBtnDisabled = parent.hPrefWindow.getPrefIsLocked(urlbarHistButton.getAttribute("prefstring"));
if (!isBtnDisabled && !parent.hPrefWindow.getPref("string", "general.open_location.last_url")) {
var file = Components.classes["@mozilla.org/file/directory_service;1"]
.getService(Components.interfaces.nsIProperties)
.get("ProfD", Components.interfaces.nsIFile);
file.append("urlbarhistory.sqlite");
if (!file.exists())
isBtnDisabled = true;
else {
var connection = Components.classes["@mozilla.org/storage/service;1"]
.getService(Components.interfaces.mozIStorageService)
.openDatabase(file);
isBtnDisabled = !connection.tableExists("urlbarhistory");
connection.close();
}
}
urlbarHistButton.disabled = isBtnDisabled;
}
catch(ex) {
}
@ -87,17 +92,23 @@
function prefClearUrlbarHistory()
{
var pref = Components.classes["@mozilla.org/preferences-service;1"]
.getService(Components.interfaces.nsIPrefBranch);
pref.setCharPref("general.open_location.last_url", "");
var RDF = Components.classes["@mozilla.org/rdf/rdf-service;1"]
.getService(Components.interfaces.nsIRDFService);
var localStore = RDF.GetDataSource("rdf:local-store");
var RDFC = Components.classes["@mozilla.org/rdf/container-utils;1"]
.getService(Components.interfaces.nsIRDFContainerUtils);
var urlBarHist = RDFC.MakeSeq(localStore, RDF.GetResource("nc:urlbar-history"));
for (var i = urlBarHist.GetCount(); i > 0; --i)
urlBarHist.RemoveElementAt(i, true);
parent.hPrefWindow.setPref("string", "general.open_location.last_url", "");
var file = Components.classes["@mozilla.org/file/directory_service;1"]
.getService(Components.interfaces.nsIProperties)
.get("ProfD", Components.interfaces.nsIFile);
file.append("urlbarhistory.sqlite");
if (file.exists()) {
try {
file.remove(false);
} catch (e) {
var connection = Components.classes["@mozilla.org/storage/service;1"]
.getService(Components.interfaces.mozIStorageService)
.openDatabase(file);
if (connection.tableExists("urlbarhistory"))
connection.executeSimpleSQL("DROP TABLE urlbarhistory");
connection.close();
}
}
}
]]>
</script>