diff options
author | mad@chromium.org <mad@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2011-04-05 18:23:01 +0000 |
---|---|---|
committer | mad@chromium.org <mad@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2011-04-05 18:23:01 +0000 |
commit | 4e963b73e6be8d0b6676cf014bc9363863130ea6 (patch) | |
tree | 97a515b94c9ebc1fa02f3470fc75cdc73b700c76 /chrome/browser/ui/title_prefix_matcher.cc | |
parent | 97e780da17ba0e8933e690e33751e19c5fe1bbb9 (diff) | |
download | chromium_src-4e963b73e6be8d0b6676cf014bc9363863130ea6.zip chromium_src-4e963b73e6be8d0b6676cf014bc9363863130ea6.tar.gz chromium_src-4e963b73e6be8d0b6676cf014bc9363863130ea6.tar.bz2 |
Improvements to tab title prefix eliding as per email discussions.
- Don't look for common title prefixes between pages from different hosts.
- Bring down the minimal width of a tab title area from 10 to 6 chars.
- Don't elide last 4 chars of the common prefix, and also don't elide if there is less than 7 common characters.
BUG=None
TEST=Try it!
Review URL: http://codereview.chromium.org/6783015
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@80492 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'chrome/browser/ui/title_prefix_matcher.cc')
-rw-r--r-- | chrome/browser/ui/title_prefix_matcher.cc | 38 |
1 files changed, 29 insertions, 9 deletions
diff --git a/chrome/browser/ui/title_prefix_matcher.cc b/chrome/browser/ui/title_prefix_matcher.cc index 55e052c..95bdbfd 100644 --- a/chrome/browser/ui/title_prefix_matcher.cc +++ b/chrome/browser/ui/title_prefix_matcher.cc @@ -7,6 +7,7 @@ #include "base/hash_tables.h" #include "base/i18n/break_iterator.h" #include "base/logging.h" +#include "base/utf_string_conversions.h" namespace { // We use this value to identify that we have already seen the title associated @@ -14,14 +15,23 @@ namespace { const size_t kPreviouslySeenIndex = 0xFFFFFFFF; } -TitlePrefixMatcher::TitleInfo::TitleInfo(const string16* title, - int caller_value) +// static +const int TitlePrefixMatcher::kCommonCharsToShow = 4; +const size_t TitlePrefixMatcher::kMinElidingLength = + TitlePrefixMatcher::kCommonCharsToShow + 3; + +TitlePrefixMatcher::TitleInfo::TitleInfo( + const string16* title, const GURL& url, int caller_value) : title(title), + url(url), prefix_length(0), caller_value(caller_value) { DCHECK(title != NULL); } +TitlePrefixMatcher::TitleInfo::~TitleInfo() { +} + // static void TitlePrefixMatcher::CalculatePrefixLengths( std::vector<TitleInfo>* title_infos) { @@ -41,12 +51,12 @@ void TitlePrefixMatcher::CalculatePrefixLengths( for (size_t i = 0; i < title_infos->size(); ++i) { // We use pairs to test existence and insert in one shot. std::pair<base::hash_map<string16, size_t>::iterator, bool> insert_result = - existing_title.insert(std::make_pair(*title_infos->at(i).title, i)); + existing_title.insert(std::make_pair(*(*title_infos)[i].title, i)); if (!insert_result.second) { // insert_result.second is false when we insert a duplicate in the set. // insert_result.first is a map iterator and thus // insert_result.first->first is the string title key of the map. - DCHECK(*title_infos->at(i).title == insert_result.first->first); + DCHECK_EQ(*(*title_infos)[i].title, insert_result.first->first); duplicate_titles.insert(i); // insert_result.first->second is the value of the title index and if it's // not kPreviouslySeenIndex yet, we must remember it as a duplicate too. @@ -64,7 +74,11 @@ void TitlePrefixMatcher::CalculatePrefixLengths( // Duplicate titles are not to be included in this process. if (duplicate_titles.find(i) != duplicate_titles.end()) continue; - const string16* title = title_infos->at(i).title; + const TitleInfo& title_info = (*title_infos)[i]; + const string16* title = title_info.title; + // We prefix the hostname at the beginning, so that we only group + // titles that are from the same hostname. + string16 hostname = ASCIIToUTF16(title_info.url.host()); // We only create prefixes at word boundaries. base::BreakIterator iter(title, base::BreakIterator::BREAK_WORD); // We ignore this title if we can't break it into words, or if it only @@ -76,7 +90,7 @@ void TitlePrefixMatcher::CalculatePrefixLengths( // previous word and more easily ignore the last word while iterating. while (iter.Advance()) { if (iter.IsWord()) - prefixes[title->substr(0, iter.prev())].push_back(i); + prefixes[hostname + title->substr(0, iter.prev())].push_back(i); } } @@ -86,10 +100,16 @@ void TitlePrefixMatcher::CalculatePrefixLengths( prefixes.begin(); iter != prefixes.end(); ++iter) { // iter->first is the prefix string, iter->second is a vector of indices. if (iter->second.size() > 1) { - size_t prefix_length = iter->first.size(); + // We need to subtract the hostname size since we added it to the prefix. + const TitleInfo& first_title_info = (*title_infos)[iter->second[0]]; + DCHECK_GE(iter->first.size(), first_title_info.url.host().size()); + size_t prefix_length = iter->first.size() - + first_title_info.url.host().size(); for (size_t i = 0; i < iter->second.size(); ++i){ - if (title_infos->at(iter->second[i]).prefix_length < prefix_length) - title_infos->at(iter->second[i]).prefix_length = prefix_length; + TitleInfo& title_info = (*title_infos)[iter->second[i]]; + DCHECK_EQ(first_title_info.url.host(), title_info.url.host()); + if (title_info.prefix_length < prefix_length) + title_info.prefix_length = prefix_length; } } } |