fix for bug 71903 (search history broken), bug 82277 (fix string allocation in mIgnorePrefixes), bug 83490 (non-allocating accessor for autocomplete), bug 85364 (switching to nsAutoString)

sr=hewitt
r=ben
a=asa
This commit is contained in:
alecf%netscape.com 2001-06-12 23:59:01 +00:00
Родитель c4496bd87a
Коммит 3fb26b9f6c
9 изменённых файлов: 180 добавлений и 132 удалений

Просмотреть файл

@ -77,7 +77,7 @@ PRBool nsAbAutoCompleteSession::ItsADuplicate(PRUnichar* fullAddrStr, nsIAutoCom
{
nsCOMPtr<nsISupports> item;
nsCOMPtr<nsIAutoCompleteItem> resultItem;
nsXPIDLString valueStr;
nsAutoString valueStr;
for (rv = enumerator->First(); NS_SUCCEEDED(rv); rv = enumerator->Next())
{
@ -87,11 +87,11 @@ PRBool nsAbAutoCompleteSession::ItsADuplicate(PRUnichar* fullAddrStr, nsIAutoCom
resultItem = do_QueryInterface(item, &rv);
if (NS_SUCCEEDED(rv))
{
rv = resultItem->GetValue(getter_Copies(valueStr));
if (NS_SUCCEEDED(rv) && valueStr && ((const PRUnichar*)valueStr)[0] != 0)
rv = resultItem->GetValue(valueStr);
if (NS_SUCCEEDED(rv) && !valueStr.IsEmpty())
{
if (nsCRT::strcasecmp(fullAddrStr, valueStr) == 0)
return PR_TRUE;
if (nsCRT::strcasecmp(fullAddrStr, valueStr.get())==0)
return PR_TRUE;
}
}
}
@ -177,7 +177,7 @@ void nsAbAutoCompleteSession::AddToResult(const PRUnichar* pNickNameStr, const P
newItem->SetParam(param);
NS_IF_RELEASE(param);
newItem->SetValue(fullAddrStr);
newItem->SetValue(nsDependentString(fullAddrStr));
nsCOMPtr<nsISupportsArray> array;
rv = results->GetItems(getter_AddRefs(array));
if (NS_SUCCEEDED(rv))

Просмотреть файл

@ -1,4 +1,4 @@
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
/* -*- Mode: IDL; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
*
* The contents of this file are subject to the Netscape Public
* License Version 1.1 (the "License"); you may not use this file
@ -27,23 +27,56 @@
[scriptable, uuid(88DCFA80-062F-11d4-a449-B36A1A94C0FC)]
interface nsIAutoCompleteItem : nsISupports {
attribute wstring value; // the result value
attribute wstring comment; // an extra comment that will be displayed next to the value but that
// will not be part of the value itself
attribute string className; // class name used to define some style through css like the colors, an
// icon url, etc...
attribute nsISupports param; // parameter use by the search engin
/**
* the result value
* using AString to avoid excess allocations
*/
attribute AString value;
/**
* an extra comment that will be
* displayed next to the value but that
* will not be part of the value
* itself
*/
attribute wstring comment;
/**
* class name used to define some style through
* css like the colors, an icon url, etc...
*/
attribute string className;
/**
* parameter use by the search engine
*/
attribute nsISupports param;
};
[scriptable, uuid(88DCFA81-062F-11d4-a449-B36A1A94C0FC)]
interface nsIAutoCompleteResults : nsISupports {
attribute wstring searchString; // the original search string
attribute nsISupportsArray items; // Array of result items (nsIAutoCompleteItem)
attribute long defaultItemIndex; // Index (0 base) of the default item that will be preselected and displayed
/**
* the original search string
*/
attribute wstring searchString;
/**
* Array of result items (nsIAutoCompleteItem)
*/
attribute nsISupportsArray items;
attribute nsISupports param; // param use by the the search engine
/**
* Index (0 base) of the default item that
* will be preselected and displayed
*/
attribute long defaultItemIndex;
/**
* param use by the the search engine
*/
attribute nsISupports param;
};
%{ C++

Просмотреть файл

@ -1,4 +1,4 @@
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
*
* The contents of this file are subject to the Netscape Public
* License Version 1.1 (the "License"); you may not use this file
@ -41,16 +41,15 @@ nsAutoCompleteItem::~nsAutoCompleteItem()
{
}
NS_IMETHODIMP nsAutoCompleteItem::GetValue(PRUnichar * *aValue)
NS_IMETHODIMP nsAutoCompleteItem::GetValue(nsAWritableString& aValue)
{
if (!aValue) return NS_ERROR_NULL_POINTER;
*aValue = mValue.ToNewUnicode();
aValue.Assign(mValue);
return NS_OK;
}
NS_IMETHODIMP nsAutoCompleteItem::SetValue(const PRUnichar * aValue)
NS_IMETHODIMP nsAutoCompleteItem::SetValue(const nsAReadableString& aValue)
{
mValue = aValue;
mValue.Assign(aValue);
return NS_OK;
}

Просмотреть файл

@ -1,26 +0,0 @@
#!nmake
#
# The contents of this file are subject to the Netscape 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/NPL/
#
# 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.
#
# The Original Code is mozilla.org code.
#
# The Initial Developer of the Original Code is Netscape
# Communications Corporation. Portions created by Netscape are
# Copyright (C) 1998 Netscape Communications Corporation. All
# Rights Reserved.
#
# Contributor(s):
DEPTH=..\..\..
DIRS=public src resources
include <$(DEPTH)\config\rules.mak>

Просмотреть файл

@ -36,7 +36,7 @@ function find()
var match = document.getElementById("matchList");
var method = document.getElementById("methodList");
var field = document.getElementById("searchField");
var searchURI = "find:datasource=rdf:history"
var searchURI = "find:datasource=history"
searchURI += "&match=" + match.selectedItem.value;
searchURI += "&method=" + method.selectedItem.value;
searchURI += "&text=" + escape(field.value);

Просмотреть файл

@ -48,8 +48,8 @@
<hbox autostretch="never" valign="middle">
<menulist id="matchList">
<menupopup>
<menuitem value="http://home.netscape.com/NC-rdf#Name" label="&search.name.label;"/>
<menuitem value="http://home.netscape.com/NC-rdf#URL" label="&search.url.label;"/>
<menuitem value="Name" label="&search.name.label;"/>
<menuitem value="URL" label="&search.url.label;"/>
</menupopup>
</menulist>
<menulist id="methodList">

Просмотреть файл

@ -47,6 +47,7 @@
#include "nsIDirectoryService.h"
#include "nsAppDirectoryServiceDefs.h"
#include "nsString.h"
#include "nsReadableUtils.h"
#include "nsXPIDLString.h"
#include "plhash.h"
#include "plstr.h"
@ -462,24 +463,12 @@ nsGlobalHistory::nsGlobalHistory()
// commonly used prefixes that should be chopped off all
// history and input urls before comparison
mIgnorePrefixes = new nsVoidArray(5);
nsString* temp;
temp = new nsString(NS_LITERAL_STRING("http://www."));
mIgnorePrefixes->ReplaceElementAt(NS_STATIC_CAST(void*, temp), 0);
temp = new nsString(NS_LITERAL_STRING("http://"));
mIgnorePrefixes->ReplaceElementAt(NS_STATIC_CAST(void*, temp), 1);
temp = new nsString(NS_LITERAL_STRING("www."));
mIgnorePrefixes->ReplaceElementAt(NS_STATIC_CAST(void*, temp), 2);
temp = new nsString(NS_LITERAL_STRING("https://www."));
mIgnorePrefixes->ReplaceElementAt(NS_STATIC_CAST(void*, temp), 3);
temp = new nsString(NS_LITERAL_STRING("https://"));
mIgnorePrefixes->ReplaceElementAt(NS_STATIC_CAST(void*, temp), 4);
mIgnorePrefixes.AppendString(NS_LITERAL_STRING("http://www."));
mIgnorePrefixes.AppendString(NS_LITERAL_STRING("http://"));
mIgnorePrefixes.AppendString(NS_LITERAL_STRING("www."));
mIgnorePrefixes.AppendString(NS_LITERAL_STRING("https://www."));
mIgnorePrefixes.AppendString(NS_LITERAL_STRING("https://"));
}
nsGlobalHistory::~nsGlobalHistory()
@ -516,12 +505,6 @@ nsGlobalHistory::~nsGlobalHistory()
if (mExpireNowTimer)
mExpireNowTimer->Cancel();
for(PRInt32 i = 0; i < mIgnorePrefixes->Count(); ++i) {
nsString* entry = NS_STATIC_CAST(nsString*, mIgnorePrefixes->ElementAt(i));
delete entry;
}
delete mIgnorePrefixes;
}
@ -733,8 +716,18 @@ nsGlobalHistory::SetRowValue(nsIMdbRow *aRow, mdb_column aCol,
mdb_err err;
PRInt32 len = (nsCRT::strlen(aValue) * sizeof(PRUnichar));
// eventually turn this on when we're confident in mork's abilitiy
// to handle yarn forms properly
#if 0
NS_ConvertUCS2toUTF8 utf8Value(aValue);
printf("Storing utf8 value %s\n", utf8Value.get());
mdbYarn yarn = { (void *)utf8Value.get(), utf8Value.Length(), utf8Value.Length(), 0, 1, nsnull };
#else
mdbYarn yarn = { (void *)aValue, len, len, 0, 0, nsnull };
#endif
err = aRow->AddColumn(mEnv, aCol, &yarn);
if (err != 0) return NS_ERROR_FAILURE;
return NS_OK;
@ -883,7 +876,7 @@ nsGlobalHistory::SetPageTitle(const char *aURL, const PRUnichar *aTitle)
nsCOMPtr<nsIRDFLiteral> oldname;
if (!oldtitle.IsEmpty()) {
rv = gRDFService->GetLiteral(oldtitle.GetUnicode(), getter_AddRefs(oldname));
rv = gRDFService->GetLiteral(oldtitle.get(), getter_AddRefs(oldname));
if (NS_FAILED(rv)) return rv;
}
@ -1525,7 +1518,7 @@ nsGlobalHistory::GetTarget(nsIRDFResource* aSource,
if (NS_FAILED(rv)) return rv;
nsCOMPtr<nsIRDFLiteral> name;
rv = gRDFService->GetLiteral(title.GetUnicode(), getter_AddRefs(name));
rv = gRDFService->GetLiteral(title.get(), getter_AddRefs(name));
if (NS_FAILED(rv)) return rv;
return CallQueryInterface(name, aTarget);
@ -2368,10 +2361,11 @@ nsGlobalHistory::CheckHostnameEntries()
while (row) {
#if 0
rv = GetRowValue(row, kToken_URLColumn, url);
if (NS_FAILED(rv)) break;
rv = urlObj->SetSpec(url);
rv = urlObj->SetSpec(url.get());
if (NS_FAILED(rv)) break;
rv = urlObj->GetHost(getter_Copies(hostname));
@ -2379,6 +2373,17 @@ nsGlobalHistory::CheckHostnameEntries()
SetRowValue(row, kToken_HostnameColumn, hostname);
#endif
// to be turned on when we're confident in mork's ability
// to handle yarn forms properly
#if 0
nsAutoString title;
rv = GetRowValue(row, kToken_NameColumn, title);
// reencode back into UTF8
if (NS_SUCCEEDED(rv))
SetRowValue(row, kToken_NameColumn, title.get());
#endif
cursor->NextRow(mEnv, getter_Acquires(row), &pos);
}
@ -3342,22 +3347,67 @@ nsGlobalHistory::RowMatches(nsIMdbRow *aRow,
mdbYarn yarn;
aRow->AliasCellYarn(mEnv, property_column, &yarn);
if (term->method.Equals("is")) {
// is the cell in unicode or not? Hmm...let's assume so?
nsDependentCString rowVal((const char *)yarn.mYarn_Buf, yarn.mYarn_Fill);
nsDependentCString rowVal((const char *)yarn.mYarn_Buf, yarn.mYarn_Fill);
if (NS_ConvertUCS2toUTF8(term->text) != rowVal)
// set up some iterators
nsACString::const_iterator start, end;
rowVal.BeginReading(start);
rowVal.EndReading(end);
// is the cell in unicode or not? Hmm...let's assume so?
NS_ConvertUCS2toUTF8 utf8Value(term->text);
if (term->method.Equals("is")) {
if (utf8Value != rowVal)
return PR_FALSE;
}
else if (term->method.Equals("isnot")) {
if (utf8Value == rowVal)
return PR_FALSE;
}
else if (term->method.Equals("contains")) {
if (!FindInReadable(utf8Value, start, end))
return PR_FALSE;
}
else if (term->method.Equals("doesntcontain")) {
if (FindInReadable(utf8Value, start, end))
return PR_FALSE;
}
else if (term->method.Equals("startswith")) {
// need to make sure that the found string is
// at the beginning of the string
nsACString::const_iterator real_start = start;
if (!(FindInReadable(utf8Value, start, end) &&
real_start == start))
return PR_FALSE;
}
else if (term->method.Equals("endswith")) {
// need to make sure that the found string ends
// at the end of the string
nsACString::const_iterator real_end = end;
if (!(RFindInReadable(utf8Value, start, end) &&
real_end == end))
return PR_FALSE;
}
else {
NS_WARNING("Unrecognized search method in SearchEnumerator::RowMatches");
// don't handle other match types like isgreater/etc yet
// don't handle other match types like isgreater/etc yet,
// so assume the match failed and bail
return PR_FALSE;
}
}
}
// we've gone through each term and didn't bail, so they must have
// all matched!
return PR_TRUE;
}
@ -3435,11 +3485,11 @@ nsGlobalHistory::AutoCompleteEnumerator::~AutoCompleteEnumerator()
PRBool
nsGlobalHistory::AutoCompleteEnumerator::IsResult(nsIMdbRow* aRow)
{
nsCString url;
nsCAutoString url;
mHistory->GetRowValue(aRow, mURLColumn, url);
nsString url2;
url2.AssignWithConversion(url);
nsAutoString url2;
url2.AssignWithConversion(url.get());
PRBool result = mHistory->AutoCompleteCompare(url2, mSelectValue);
return result;
@ -3448,7 +3498,7 @@ nsGlobalHistory::AutoCompleteEnumerator::IsResult(nsIMdbRow* aRow)
nsresult
nsGlobalHistory::AutoCompleteEnumerator::ConvertToISupports(nsIMdbRow* aRow, nsISupports** aResult)
{
nsCString url;
nsCAutoString url;
mHistory->GetRowValue(aRow, mURLColumn, url);
nsAutoString comments;
mHistory->GetRowValue(aRow, mCommentColumn, comments);
@ -3456,9 +3506,7 @@ nsGlobalHistory::AutoCompleteEnumerator::ConvertToISupports(nsIMdbRow* aRow, nsI
nsCOMPtr<nsIAutoCompleteItem> newItem(do_CreateInstance(NS_AUTOCOMPLETEITEM_CONTRACTID));
NS_ENSURE_TRUE(newItem, NS_ERROR_FAILURE);
PRUnichar* urlstr = url.ToNewUnicode();
newItem->SetValue(urlstr);
nsMemory::Free(urlstr);
newItem->SetValue(NS_ConvertASCIItoUCS2(url.get()));
newItem->SetComment(comments.get());
@ -3591,12 +3639,13 @@ nsGlobalHistory::AutoCompleteSearch(const nsAReadableString& aSearchString,
for (PRUint32 i = 0; i < count; ++i) {
nsCOMPtr<nsIAutoCompleteItem> item;
prevResultItems->GetElementAt(i, getter_AddRefs(item));
// make a copy of the value because AutoCompleteCompare
// is destructive
nsAutoString url;
item->GetValue(url);
nsXPIDLString url;
item->GetValue(getter_Copies(url));
nsDependentString urlstr(url);
if (AutoCompleteCompare(urlstr, aSearchString))
if (AutoCompleteCompare(url, aSearchString))
resultItems->AppendElement(item);
}
} else {
@ -3612,18 +3661,24 @@ nsGlobalHistory::AutoCompleteSearch(const nsAReadableString& aSearchString,
// store hits in an auto array initially
nsAutoVoidArray array;
nsISupports* entry; // not using nsCOMPtr here to avoid time spent refcounting
// while passing these around between the 3 arrays
// step through the enumerator
// not using nsCOMPtr here to avoid time spent
// refcounting while passing these around between the 3 arrays
nsISupports* entry;
// step through the enumerator to get the items into 'array'
// because we don't know how many items there will be
PRBool hasMore;
while (PR_TRUE) {
enumerator->HasMoreElements(&hasMore);
if (!hasMore) break;
// addref's each entry as it enters 'array'
enumerator->GetNext(&entry);
array.AppendElement(entry);
}
// turn auto array into flat array for quick sort
// turn auto array into flat array for quick sort, now that we
// know how many items there are
PRUint32 count = array.Count();
nsIAutoCompleteItem** items = new nsIAutoCompleteItem*[count];
PRUint32 i;
@ -3631,7 +3686,8 @@ nsGlobalHistory::AutoCompleteSearch(const nsAReadableString& aSearchString,
items[i] = (nsIAutoCompleteItem*)array.ElementAt(i);
// sort it
NS_QuickSort(items, count, sizeof(nsIAutoCompleteItem*), AutoCompleteSortComparison, nsnull);
NS_QuickSort(items, count, sizeof(nsIAutoCompleteItem*),
AutoCompleteSortComparison, nsnull);
// place the sorted array into the autocomplete results
for (i = 0; i < count; ++i) {
@ -3652,8 +3708,8 @@ nsGlobalHistory::AutoCompleteCutPrefix(nsAWritableString& aURL)
// This comparison is case-sensitive. Therefore, it assumes that aUserURL is a
// potential URL whose host name is in all lower case.
PRInt32 idx = 0;
for (PRInt32 i = 0; i < mIgnorePrefixes->Count(); ++i) {
nsString* string = NS_STATIC_CAST(nsString*, mIgnorePrefixes->ElementAt(i));
for (PRInt32 i = 0; i < mIgnorePrefixes.Count(); ++i) {
nsString* string = mIgnorePrefixes.StringAt(i);
if (Substring(aURL, 0, string->Length()).Equals(*string)) {
idx = string->Length();
break;
@ -3703,20 +3759,11 @@ AutoCompleteSortComparison(const void *v1, const void *v2, void *unused)
nsIAutoCompleteItem *item1 = *(nsIAutoCompleteItem**) v1;
nsIAutoCompleteItem *item2 = *(nsIAutoCompleteItem**) v2;
nsXPIDLString s1;
item1->GetValue(getter_Copies(s1));
nsXPIDLString s2;
item2->GetValue(getter_Copies(s2));
nsAutoString s1;
item1->GetValue(s1);
nsAutoString s2;
item2->GetValue(s2);
if (!s1) {
if (!s2)
return 0;
else
return -1;
} else if (!s2) {
return 1;
} else {
return nsCRT::strcmp(s1, s2);
}
return nsCRT::strcmp(s1.get(), s2.get());
}

Просмотреть файл

@ -176,7 +176,7 @@ protected:
//
// autocomplete stuff
//
nsVoidArray* mIgnorePrefixes;
nsStringArray mIgnorePrefixes;
nsresult AutoCompleteSearch(const nsAReadableString& aSearchString,
nsIAutoCompleteResults* aPrevResults,

Просмотреть файл

@ -22,13 +22,13 @@
*/
// Local Includes
#include "nsString.h"
#include "nsUrlbarHistory.h"
// Helper Classes
#include "nsXPIDLString.h"
// Interfaces Needed
#include "nsString.h"
#include "nsIAutoCompleteResults.h"
#include "nsISimpleEnumerator.h"
#include "nsIPref.h"
@ -356,18 +356,13 @@ nsUrlbarHistory::SearchPreviousResults(const PRUnichar *searchStr, nsIAutoComple
if (NS_FAILED(rv))
return NS_ERROR_FAILURE;
PRUnichar * itemValue=nsnull;
resultItem->GetValue(&itemValue);
nsAutoString itemAutoStr(itemValue);
nsAutoString itemValue;
resultItem->GetValue(itemValue);
//printf("SearchPreviousResults::Comparing %s with %s \n", searchAutoStr.ToNewCString(), itemAutoStr.ToNewCString());
if (!itemValue)
if (itemValue.IsEmpty())
continue;
if (nsCRT::strncasecmp(searchStr, itemValue, searchStrLen) == 0)
{
Recycle(itemValue);
if (nsCRT::strncasecmp(searchStr, itemValue.get(), searchStrLen) == 0)
continue;
}
}
return NS_OK;
@ -519,7 +514,7 @@ nsUrlbarHistory::SearchCache(const PRUnichar* searchStr, nsIAutoCompleteResults*
nsCOMPtr<nsIAutoCompleteItem> newItem(do_CreateInstance(NS_AUTOCOMPLETEITEM_CONTRACTID));
NS_ENSURE_TRUE(newItem, NS_ERROR_FAILURE);
newItem->SetValue(match);
newItem->SetValue(nsDependentString(match));
nsCOMPtr<nsISupportsArray> array;
rv = results->GetItems(getter_AddRefs(array));
if (NS_SUCCEEDED(rv))
@ -611,11 +606,11 @@ nsUrlbarHistory::CheckItemAvailability(const PRUnichar * aItem, nsIAutoCompleteR
if (NS_FAILED(rv))
return NS_ERROR_FAILURE;
nsXPIDLString itemValue;
resultItem->GetValue(getter_Copies(itemValue));
nsAutoString itemValue;
resultItem->GetValue(itemValue);
// Using nsIURI to do comparisons didn't quite work out.
// So use nsCRT methods
if (nsCRT::strcasecmp(itemValue, aItem) == 0)
if (nsCRT::strcasecmp(itemValue.get(), aItem) == 0)
{
//printf("In CheckItemAvailability. Item already found\n");
*aResult = PR_TRUE;
@ -675,7 +670,7 @@ nsUrlbarHistory::VerifyAndCreateEntry(const PRUnichar * aSearchItem, PRUnichar *
//Create an AutoComplete Item
nsCOMPtr<nsIAutoCompleteItem> newItem(do_CreateInstance(NS_AUTOCOMPLETEITEM_CONTRACTID));
NS_ENSURE_TRUE(newItem, NS_ERROR_FAILURE);
newItem->SetValue(hostName.GetUnicode());
newItem->SetValue(hostName);
nsCOMPtr<nsISupportsArray> array;
rv = aResultArray->GetItems(getter_AddRefs(array));
// Always insert the host entry at the top of the array