Bug 463459 - Use a separate pref instead of empty restrict/match values to specify defaults. r=dietrich, a1.9.1=beltzner
authorEdward Lee <edward.lee@engineering.uiuc.edu>
Wed, 10 Dec 2008 10:58:43 -0600
changeset 22574 5ce74cb333cc0c4ca47ba41922ae87b2d86683c1
parent 22573 cda0ae16a8a8ebf85d77dd807316e86552aa18ee
child 22575 8602d55fe61f712704ed239102da390994906304
push idunknown
push userunknown
push dateunknown
reviewersdietrich
bugs463459
milestone1.9.2a1pre
Bug 463459 - Use a separate pref instead of empty restrict/match values to specify defaults. r=dietrich, a1.9.1=beltzner
browser/app/profile/firefox.js
toolkit/components/places/src/nsNavHistory.cpp
toolkit/components/places/src/nsNavHistory.h
toolkit/components/places/src/nsNavHistoryAutoComplete.cpp
toolkit/components/places/tests/autocomplete/test_special_search.js
--- a/browser/app/profile/firefox.js
+++ b/browser/app/profile/firefox.js
@@ -239,16 +239,24 @@ pref("browser.urlbar.search.timeout", 10
 // the search to visited history, bookmarked, tagged pages; or force a match on
 // just the title text or url.
 pref("browser.urlbar.restrict.history", "^");
 pref("browser.urlbar.restrict.bookmark", "*");
 pref("browser.urlbar.restrict.tag", "+");
 pref("browser.urlbar.match.title", "#");
 pref("browser.urlbar.match.url", "@");
 
+// The default behavior for the urlbar can be configured to use any combination
+// of the restrict or match filters with each additional filter restricting
+// more (intersection). Add the following values to set the behavior as the
+// default: 1: history, 2: bookmark, 4: tag, 8: title, 16: url
+// E.g., 0 = show all results (no filtering), 1 = only visited pages in history,
+// 2 = only bookmarks, 3 = visited bookmarks, 1+16 = history matching in the url
+pref("browser.urlbar.default.behavior", 0);
+
 // Number of milliseconds to wait for the http headers (and thus
 // the Content-Disposition filename) before giving up and falling back to 
 // picking a filename without that info in hand so that the user sees some
 // feedback from their action.
 pref("browser.download.saveLinkAsFilenameTimeout", 1000);
 
 pref("browser.download.useDownloadDir", true);
 pref("browser.download.folderList", 0);
--- a/toolkit/components/places/src/nsNavHistory.cpp
+++ b/toolkit/components/places/src/nsNavHistory.cpp
@@ -119,16 +119,17 @@
 #define PREF_BROWSER_HISTORY_EXPIRE_DAYS_MAX    "history_expire_days"
 #define PREF_BROWSER_HISTORY_EXPIRE_SITES       "history_expire_sites"
 #define PREF_AUTOCOMPLETE_ONLY_TYPED            "urlbar.matchOnlyTyped"
 #define PREF_AUTOCOMPLETE_MATCH_BEHAVIOR        "urlbar.matchBehavior"
 #define PREF_AUTOCOMPLETE_SEARCH_SOURCES        "urlbar.search.sources"
 #define PREF_AUTOCOMPLETE_FILTER_JAVASCRIPT     "urlbar.filter.javascript"
 #define PREF_AUTOCOMPLETE_ENABLED               "urlbar.autocomplete.enabled"
 #define PREF_AUTOCOMPLETE_MAX_RICH_RESULTS      "urlbar.maxRichResults"
+#define PREF_AUTOCOMPLETE_DEFAULT_BEHAVIOR      "urlbar.default.behavior"
 #define PREF_AUTOCOMPLETE_RESTRICT_HISTORY      "urlbar.restrict.history"
 #define PREF_AUTOCOMPLETE_RESTRICT_BOOKMARK     "urlbar.restrict.bookmark"
 #define PREF_AUTOCOMPLETE_RESTRICT_TAG          "urlbar.restrict.tag"
 #define PREF_AUTOCOMPLETE_MATCH_TITLE           "urlbar.match.title"
 #define PREF_AUTOCOMPLETE_MATCH_URL             "urlbar.match.url"
 #define PREF_AUTOCOMPLETE_SEARCH_CHUNK_SIZE     "urlbar.search.chunkSize"
 #define PREF_AUTOCOMPLETE_SEARCH_TIMEOUT        "urlbar.search.timeout"
 #define PREF_DB_CACHE_PERCENTAGE                "history_cache_percentage"
@@ -315,16 +316,22 @@ const PRInt32 nsNavHistory::kGetInfoInde
 const PRInt32 nsNavHistory::kAutoCompleteIndex_URL = 0;
 const PRInt32 nsNavHistory::kAutoCompleteIndex_Title = 1;
 const PRInt32 nsNavHistory::kAutoCompleteIndex_FaviconURL = 2;
 const PRInt32 nsNavHistory::kAutoCompleteIndex_ParentId = 3;
 const PRInt32 nsNavHistory::kAutoCompleteIndex_BookmarkTitle = 4;
 const PRInt32 nsNavHistory::kAutoCompleteIndex_Tags = 5;
 const PRInt32 nsNavHistory::kAutoCompleteIndex_VisitCount = 6;
 
+const PRInt32 nsNavHistory::kAutoCompleteBehaviorHistory = 1 << 0;
+const PRInt32 nsNavHistory::kAutoCompleteBehaviorBookmark = 1 << 1;
+const PRInt32 nsNavHistory::kAutoCompleteBehaviorTag = 1 << 2;
+const PRInt32 nsNavHistory::kAutoCompleteBehaviorTitle = 1 << 3;
+const PRInt32 nsNavHistory::kAutoCompleteBehaviorUrl = 1 << 4;
+
 static const char* gQuitApplicationMessage = "quit-application";
 static const char* gXpcomShutdown = "xpcom-shutdown";
 static const char* gAutoCompleteFeedback = "autocomplete-will-enter-text";
 
 // annotation names
 const char nsNavHistory::kAnnotationPreviousEncoding[] = "history/encoding";
 
 // code borrowed from mozilla/xpfe/components/history/src/nsGlobalHistory.cpp
@@ -367,21 +374,18 @@ nsNavHistory::nsNavHistory() : mBatchLev
                                mAutoCompleteMaxResults(25),
                                mAutoCompleteRestrictHistory(NS_LITERAL_STRING("^")),
                                mAutoCompleteRestrictBookmark(NS_LITERAL_STRING("*")),
                                mAutoCompleteRestrictTag(NS_LITERAL_STRING("+")),
                                mAutoCompleteMatchTitle(NS_LITERAL_STRING("#")),
                                mAutoCompleteMatchUrl(NS_LITERAL_STRING("@")),
                                mAutoCompleteSearchChunkSize(100),
                                mAutoCompleteSearchTimeout(100),
-                               mRestrictHistory(PR_FALSE),
-                               mRestrictBookmark(PR_FALSE),
-                               mRestrictTag(PR_FALSE),
-                               mMatchTitle(PR_FALSE),
-                               mMatchUrl(PR_FALSE),
+                               mAutoCompleteDefaultBehavior(0),
+                               mAutoCompleteCurrentBehavior(0),
                                mPreviousChunkOffset(-1),
                                mAutoCompleteFinishedSearch(PR_FALSE),
                                mExpireDaysMin(0),
                                mExpireDaysMax(0),
                                mExpireSites(0),
                                mNumVisitsForFrecency(10),
                                mTagsFolder(-1),
                                mInPrivateBrowsing(PRIVATEBROWSING_NOTINITED),
@@ -497,16 +501,17 @@ nsNavHistory::Init()
 
   nsCOMPtr<nsIPrefBranch2> pbi = do_QueryInterface(mPrefBranch);
   if (pbi) {
     pbi->AddObserver(PREF_AUTOCOMPLETE_ONLY_TYPED, this, PR_FALSE);
     pbi->AddObserver(PREF_AUTOCOMPLETE_MATCH_BEHAVIOR, this, PR_FALSE);
     pbi->AddObserver(PREF_AUTOCOMPLETE_SEARCH_SOURCES, this, PR_FALSE);
     pbi->AddObserver(PREF_AUTOCOMPLETE_FILTER_JAVASCRIPT, this, PR_FALSE);
     pbi->AddObserver(PREF_AUTOCOMPLETE_MAX_RICH_RESULTS, this, PR_FALSE);
+    pbi->AddObserver(PREF_AUTOCOMPLETE_DEFAULT_BEHAVIOR, this, PR_FALSE);
     pbi->AddObserver(PREF_AUTOCOMPLETE_RESTRICT_HISTORY, this, PR_FALSE);
     pbi->AddObserver(PREF_AUTOCOMPLETE_RESTRICT_BOOKMARK, this, PR_FALSE);
     pbi->AddObserver(PREF_AUTOCOMPLETE_RESTRICT_TAG, this, PR_FALSE);
     pbi->AddObserver(PREF_AUTOCOMPLETE_MATCH_TITLE, this, PR_FALSE);
     pbi->AddObserver(PREF_AUTOCOMPLETE_MATCH_URL, this, PR_FALSE);
     pbi->AddObserver(PREF_AUTOCOMPLETE_SEARCH_CHUNK_SIZE, this, PR_FALSE);
     pbi->AddObserver(PREF_AUTOCOMPLETE_SEARCH_TIMEOUT, this, PR_FALSE);
     pbi->AddObserver(PREF_BROWSER_HISTORY_EXPIRE_DAYS_MAX, this, PR_FALSE);
@@ -2063,16 +2068,18 @@ nsNavHistory::LoadPrefs(PRBool aInitiali
   mPrefBranch->GetBoolPref(PREF_AUTOCOMPLETE_FILTER_JAVASCRIPT,
                            &mAutoCompleteFilterJavascript);
   mPrefBranch->GetIntPref(PREF_AUTOCOMPLETE_MAX_RICH_RESULTS,
                           &mAutoCompleteMaxResults);
   mPrefBranch->GetIntPref(PREF_AUTOCOMPLETE_SEARCH_CHUNK_SIZE,
                           &mAutoCompleteSearchChunkSize);
   mPrefBranch->GetIntPref(PREF_AUTOCOMPLETE_SEARCH_TIMEOUT,
                           &mAutoCompleteSearchTimeout);
+  mPrefBranch->GetIntPref(PREF_AUTOCOMPLETE_DEFAULT_BEHAVIOR,
+                          &mAutoCompleteDefaultBehavior);
   nsXPIDLCString prefStr;
   mPrefBranch->GetCharPref(PREF_AUTOCOMPLETE_RESTRICT_HISTORY,
                            getter_Copies(prefStr));
   mAutoCompleteRestrictHistory = NS_ConvertUTF8toUTF16(prefStr);
   mPrefBranch->GetCharPref(PREF_AUTOCOMPLETE_RESTRICT_BOOKMARK,
                            getter_Copies(prefStr));
   mAutoCompleteRestrictBookmark = NS_ConvertUTF8toUTF16(prefStr);
   mPrefBranch->GetCharPref(PREF_AUTOCOMPLETE_RESTRICT_TAG,
--- a/toolkit/components/places/src/nsNavHistory.h
+++ b/toolkit/components/places/src/nsNavHistory.h
@@ -732,21 +732,24 @@ protected:
   nsString mAutoCompleteRestrictBookmark;
   nsString mAutoCompleteRestrictTag;
   nsString mAutoCompleteMatchTitle;
   nsString mAutoCompleteMatchUrl;
   PRInt32 mAutoCompleteSearchChunkSize;
   PRInt32 mAutoCompleteSearchTimeout;
   nsCOMPtr<nsITimer> mAutoCompleteTimer;
 
-  PRBool mRestrictHistory;
-  PRBool mRestrictBookmark;
-  PRBool mRestrictTag;
-  PRBool mMatchTitle;
-  PRBool mMatchUrl;
+  static const PRInt32 kAutoCompleteBehaviorHistory;
+  static const PRInt32 kAutoCompleteBehaviorBookmark;
+  static const PRInt32 kAutoCompleteBehaviorTag;
+  static const PRInt32 kAutoCompleteBehaviorTitle;
+  static const PRInt32 kAutoCompleteBehaviorUrl;
+
+  PRInt32 mAutoCompleteDefaultBehavior; // kAutoCompleteBehavior* bitmap
+  PRInt32 mAutoCompleteCurrentBehavior; // kAutoCompleteBehavior* bitmap
 
   // Original search string for case-sensitive usage
   nsString mOrigSearchString;
   // Search string and tokens for case-insensitive matching
   nsString mCurrentSearchString;
   nsStringArray mCurrentSearchTokens;
   void GenerateSearchTokens();
   void AddSearchToken(nsAutoString &aToken);
--- a/toolkit/components/places/src/nsNavHistoryAutoComplete.cpp
+++ b/toolkit/components/places/src/nsNavHistoryAutoComplete.cpp
@@ -78,16 +78,22 @@
 #include "nsNavBookmarks.h"
 #include "nsPrintfCString.h"
 #include "nsILivemarkService.h"
 #include "mozIStoragePendingStatement.h"
 
 #define NS_AUTOCOMPLETESIMPLERESULT_CONTRACTID \
   "@mozilla.org/autocomplete/simple-result;1"
 
+// Helpers to get and set fields in the mAutoCompleteCurrentBehavior bitmap
+#define GET_BEHAVIOR(aBitName) \
+  (mAutoCompleteCurrentBehavior & kAutoCompleteBehavior##aBitName)
+#define SET_BEHAVIOR(aBitName) \
+  mAutoCompleteCurrentBehavior |= kAutoCompleteBehavior##aBitName
+
 // Helper to get a particular column with a desired name from the bookmark and
 // tags table based on if we want to include tags or not
 #define SQL_STR_FRAGMENT_GET_BOOK_TAG(name, column, comparison, getMostRecent) \
   NS_LITERAL_CSTRING(", (" \
   "SELECT " column " " \
   "FROM moz_bookmarks b " \
   "JOIN moz_bookmarks t ON t.id = b.parent AND t.parent " comparison " ?1 " \
   "WHERE b.type = ") + nsPrintfCString("%d", \
@@ -787,58 +793,54 @@ nsNavHistory::AddSearchToken(nsAutoStrin
   aToken.Trim("\r\n\t\b");
   if (!aToken.IsEmpty())
     mCurrentSearchTokens.AppendString(aToken);
 }
 
 void
 nsNavHistory::ProcessTokensForSpecialSearch()
 {
-  // If any of the special searches are empty, automatically use it
-  mRestrictHistory = mAutoCompleteRestrictHistory.IsEmpty();
-  mRestrictBookmark = mAutoCompleteRestrictBookmark.IsEmpty();
-  mRestrictTag = mAutoCompleteRestrictTag.IsEmpty();
-  mMatchTitle = mAutoCompleteMatchTitle.IsEmpty();
-  mMatchUrl = mAutoCompleteMatchUrl.IsEmpty();
+  // Start with the default behavior
+  mAutoCompleteCurrentBehavior = mAutoCompleteDefaultBehavior;
 
   // If we're searching only one of history or bookmark, we can use filters
   if (mAutoCompleteSearchSources == SEARCH_HISTORY)
-    mRestrictHistory = PR_TRUE;
+    SET_BEHAVIOR(History);
   else if (mAutoCompleteSearchSources == SEARCH_BOOKMARK)
-    mRestrictBookmark = PR_TRUE;
+    SET_BEHAVIOR(Bookmark);
   // SEARCH_BOTH doesn't require any filtering
 
   // Determine which special searches to apply
   for (PRInt32 i = mCurrentSearchTokens.Count(); --i >= 0; ) {
     PRBool needToRemove = PR_TRUE;
     const nsString *token = mCurrentSearchTokens.StringAt(i);
 
     if (token->Equals(mAutoCompleteRestrictHistory))
-      mRestrictHistory = PR_TRUE;
+      SET_BEHAVIOR(History);
     else if (token->Equals(mAutoCompleteRestrictBookmark))
-      mRestrictBookmark = PR_TRUE;
+      SET_BEHAVIOR(Bookmark);
     else if (token->Equals(mAutoCompleteRestrictTag))
-      mRestrictTag = PR_TRUE;
+      SET_BEHAVIOR(Tag);
     else if (token->Equals(mAutoCompleteMatchTitle))
-      mMatchTitle = PR_TRUE;
+      SET_BEHAVIOR(Title);
     else if (token->Equals(mAutoCompleteMatchUrl))
-      mMatchUrl = PR_TRUE;
+      SET_BEHAVIOR(Url);
     else
       needToRemove = PR_FALSE;
 
     // Remove the token if it's special search token
     if (needToRemove)
       (void)mCurrentSearchTokens.RemoveStringAt(i);
   }
 
   // We can use optimized queries for restricts, so check for the most
   // restrictive query first
-  mDBCurrentQuery = mRestrictTag ? GetDBAutoCompleteTagsQuery() :
-    mRestrictBookmark ? GetDBAutoCompleteStarQuery() :
-    mRestrictHistory ? GetDBAutoCompleteHistoryQuery() :
+  mDBCurrentQuery = GET_BEHAVIOR(Tag) ? GetDBAutoCompleteTagsQuery() :
+    GET_BEHAVIOR(Bookmark) ? GetDBAutoCompleteStarQuery() :
+    GET_BEHAVIOR(History) ? GetDBAutoCompleteHistoryQuery() :
     static_cast<mozIStorageStatement *>(mDBAutoCompleteQuery);
 }
 
 nsresult
 nsNavHistory::AutoCompleteKeywordSearch()
 {
   mozStorageStatementScoper scope(mDBKeywordQuery);
 
@@ -1021,43 +1023,43 @@ nsNavHistory::AutoCompleteProcessSearch(
           // If we get any results, there's potentially another chunk to proces
           if (aHasMoreResults)
             *aHasMoreResults = PR_TRUE;
 
           // Keep track of if we've matched all the filter requirements such as
           // only history items, only bookmarks, only tags. If a given restrict
           // is active, make sure a corresponding condition is *not* true. If
           // any are violated, matchAll will be false.
-          PRBool matchAll = !((mRestrictHistory && visitCount == 0) ||
-            (mRestrictBookmark && !parentId) ||
-            (mRestrictTag && entryTags.IsEmpty()));
+          PRBool matchAll = !((GET_BEHAVIOR(History) && visitCount == 0) ||
+                              (GET_BEHAVIOR(Bookmark) && !parentId) ||
+                              (GET_BEHAVIOR(Tag) && entryTags.IsEmpty()));
 
           // Unescape the url to search for unescaped terms
           nsString entryURL = FixupURIText(escapedEntryURL);
 
           // Determine if every token matches either the bookmark title, tags,
           // page title, or page url
           for (PRInt32 i = 0; i < mCurrentSearchTokens.Count() && matchAll; i++) {
             const nsString *token = mCurrentSearchTokens.StringAt(i);
 
             // Check if the tags match the search term
             PRBool matchTags = (*tokenMatchesTarget)(*token, entryTags);
             // Check if the title matches the search term
             PRBool matchTitle = (*tokenMatchesTarget)(*token, title);
 
             // Make sure we match something in the title or tags if we have to
             matchAll = matchTags || matchTitle;
-            if (mMatchTitle && !matchAll)
+            if (GET_BEHAVIOR(Title) && !matchAll)
               break;
 
             // Check if the url matches the search term
             PRBool matchUrl = (*tokenMatchesTarget)(*token, entryURL);
             // If we don't match the url when we have to, reset matchAll to
             // false; otherwise keep track that we did match the current search
-            if (mMatchUrl && !matchUrl)
+            if (GET_BEHAVIOR(Url) && !matchUrl)
               matchAll = PR_FALSE;
             else
               matchAll |= matchUrl;
           }
 
           // Skip if we don't match all terms in the bookmark, tag, title or url
           if (!matchAll)
             continue;
--- a/toolkit/components/places/tests/autocomplete/test_special_search.js
+++ b/toolkit/components/places/tests/autocomplete/test_special_search.js
@@ -130,20 +130,26 @@ let gTests = [
    "foo * +", [8,9,10,11]],
   ["18: foo # @ -> in title, in url",
    "foo # @", [3,7,10,11]],
   ["19: foo # + -> in title, is tag",
    "foo # +", [8,9,10,11]],
   ["20: foo @ + -> in url, is tag",
    "foo @ +", [10,11]],
 
-  // Test default usage by making the special search empty
+  // Test default usage by setting certain bits of default.behavior to 1
   ["21: foo -> default history",
-   "foo", [1,2,3,5,10], function() makeDefault("restrict.history")],
+   "foo", [1,2,3,5,10], function() makeDefault(1)],
   ["22: foo -> default history, is star",
-   "foo", [5,10], function() makeDefault("restrict.bookmark")],
+   "foo", [5,10], function() makeDefault(3)],
   ["23: foo -> default history, is star, in url",
-   "foo", [10], function() makeDefault("match.url")],
+   "foo", [10], function() makeDefault(19)],
+
+  // Change the default to be less restrictive to make sure we find more
+  ["24: foo -> default is star, in url",
+   "foo", [6,7,10,11], function() makeDefault(18)],
+  ["25: foo -> default in url",
+   "foo", [2,3,6,7,10,11], function() makeDefault(16)],
 ];
 
-function makeDefault(pref) {
-  prefs.setCharPref("browser.urlbar." + pref, "");
+function makeDefault(aDefault) {
+  prefs.setIntPref("browser.urlbar.default.behavior", aDefault);
 }