Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(513)

Side by Side Diff: chrome/browser/ui/title_prefix_matcher.cc

Issue 6783015: Improvements to tab title prefix eliding as per email discussions. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src/
Patch Set: '' Created 9 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "chrome/browser/ui/title_prefix_matcher.h" 5 #include "chrome/browser/ui/title_prefix_matcher.h"
6 6
7 #include "base/hash_tables.h" 7 #include "base/hash_tables.h"
8 #include "base/i18n/break_iterator.h" 8 #include "base/i18n/break_iterator.h"
9 #include "base/logging.h" 9 #include "base/logging.h"
10 #include "base/utf_string_conversions.h"
10 11
11 namespace { 12 namespace {
12 // We use this value to identify that we have already seen the title associated 13 // We use this value to identify that we have already seen the title associated
13 // to this value in the duplicate_titles hash_set, ans marked it as a duplicate. 14 // to this value in the duplicate_titles hash_set, ans marked it as a duplicate.
14 const size_t kPreviouslySeenIndex = 0xFFFFFFFF; 15 const size_t kPreviouslySeenIndex = 0xFFFFFFFF;
15 } 16 }
16 17
17 TitlePrefixMatcher::TitleInfo::TitleInfo(const string16* title, 18 // static
18 int caller_value) 19 const int TitlePrefixMatcher::kCommonCharsToShow = 4;
20 const size_t TitlePrefixMatcher::kMinElidingLength =
21 TitlePrefixMatcher::kCommonCharsToShow + 3;
22
23 TitlePrefixMatcher::TitleInfo::TitleInfo(
24 const string16* title, const GURL& url, int caller_value)
19 : title(title), 25 : title(title),
26 url(url),
20 prefix_length(0), 27 prefix_length(0),
21 caller_value(caller_value) { 28 caller_value(caller_value) {
22 DCHECK(title != NULL); 29 DCHECK(title != NULL);
23 } 30 }
24 31
32 TitlePrefixMatcher::TitleInfo::~TitleInfo() {
33 }
34
25 // static 35 // static
26 void TitlePrefixMatcher::CalculatePrefixLengths( 36 void TitlePrefixMatcher::CalculatePrefixLengths(
27 std::vector<TitleInfo>* title_infos) { 37 std::vector<TitleInfo>* title_infos) {
28 DCHECK(title_infos != NULL); 38 DCHECK(title_infos != NULL);
29 // This set will contain the indexes of the TitleInfo objects in the vector 39 // This set will contain the indexes of the TitleInfo objects in the vector
30 // that have a duplicate. 40 // that have a duplicate.
31 base::hash_set<size_t> duplicate_titles; 41 base::hash_set<size_t> duplicate_titles;
32 // This map is used to identify duplicates by remembering the vector indexes 42 // This map is used to identify duplicates by remembering the vector indexes
33 // we have seen with a given title string. The vector index is set to 43 // we have seen with a given title string. The vector index is set to
34 // kPreviouslySeenIndex once we identified duplicates and placed their 44 // kPreviouslySeenIndex once we identified duplicates and placed their
35 // indices in duplicate_titles. 45 // indices in duplicate_titles.
36 base::hash_map<string16, size_t> existing_title; 46 base::hash_map<string16, size_t> existing_title;
37 // We identify if there are identical titles upfront, 47 // We identify if there are identical titles upfront,
38 // because we don't want to remove prefixes for those at all. 48 // because we don't want to remove prefixes for those at all.
39 // We do it as a separate pass so that we don't need to remove 49 // We do it as a separate pass so that we don't need to remove
40 // previously parsed titles when we find a duplicate title later on. 50 // previously parsed titles when we find a duplicate title later on.
41 for (size_t i = 0; i < title_infos->size(); ++i) { 51 for (size_t i = 0; i < title_infos->size(); ++i) {
42 // We use pairs to test existence and insert in one shot. 52 // We use pairs to test existence and insert in one shot.
43 std::pair<base::hash_map<string16, size_t>::iterator, bool> insert_result = 53 std::pair<base::hash_map<string16, size_t>::iterator, bool> insert_result =
44 existing_title.insert(std::make_pair(*title_infos->at(i).title, i)); 54 existing_title.insert(std::make_pair(*(*title_infos)[i].title, i));
45 if (!insert_result.second) { 55 if (!insert_result.second) {
46 // insert_result.second is false when we insert a duplicate in the set. 56 // insert_result.second is false when we insert a duplicate in the set.
47 // insert_result.first is a map iterator and thus 57 // insert_result.first is a map iterator and thus
48 // insert_result.first->first is the string title key of the map. 58 // insert_result.first->first is the string title key of the map.
49 DCHECK(*title_infos->at(i).title == insert_result.first->first); 59 DCHECK_EQ(*(*title_infos)[i].title, insert_result.first->first);
50 duplicate_titles.insert(i); 60 duplicate_titles.insert(i);
51 // insert_result.first->second is the value of the title index and if it's 61 // insert_result.first->second is the value of the title index and if it's
52 // not kPreviouslySeenIndex yet, we must remember it as a duplicate too. 62 // not kPreviouslySeenIndex yet, we must remember it as a duplicate too.
53 if (insert_result.first->second != kPreviouslySeenIndex) { 63 if (insert_result.first->second != kPreviouslySeenIndex) {
54 duplicate_titles.insert(insert_result.first->second); 64 duplicate_titles.insert(insert_result.first->second);
55 insert_result.first->second = kPreviouslySeenIndex; 65 insert_result.first->second = kPreviouslySeenIndex;
56 } 66 }
57 } 67 }
58 } 68 }
59 69
60 // This next loop accumulates all the potential prefixes, 70 // This next loop accumulates all the potential prefixes,
61 // and remember on which titles we saw them. 71 // and remember on which titles we saw them.
62 base::hash_map<string16, std::vector<size_t> > prefixes; 72 base::hash_map<string16, std::vector<size_t> > prefixes;
63 for (size_t i = 0; i < title_infos->size(); ++i) { 73 for (size_t i = 0; i < title_infos->size(); ++i) {
64 // Duplicate titles are not to be included in this process. 74 // Duplicate titles are not to be included in this process.
65 if (duplicate_titles.find(i) != duplicate_titles.end()) 75 if (duplicate_titles.find(i) != duplicate_titles.end())
66 continue; 76 continue;
67 const string16* title = title_infos->at(i).title; 77 const TitleInfo& title_info = (*title_infos)[i];
78 const string16* title = title_info.title;
79 // We prefix the hostname at the beginning, so that we only group
80 // titles that are from the same hostname.
81 string16 hostname = ASCIIToUTF16(title_info.url.host());
68 // We only create prefixes at word boundaries. 82 // We only create prefixes at word boundaries.
69 base::BreakIterator iter(title, base::BreakIterator::BREAK_WORD); 83 base::BreakIterator iter(title, base::BreakIterator::BREAK_WORD);
70 // We ignore this title if we can't break it into words, or if it only 84 // We ignore this title if we can't break it into words, or if it only
71 // contains a single word. 85 // contains a single word.
72 if (!iter.Init() || !iter.Advance()) 86 if (!iter.Init() || !iter.Advance())
73 continue; 87 continue;
74 // We continue advancing even though we already advanced to the first 88 // We continue advancing even though we already advanced to the first
75 // word above, so that we can use iter.prev() to identify the end of the 89 // word above, so that we can use iter.prev() to identify the end of the
76 // previous word and more easily ignore the last word while iterating. 90 // previous word and more easily ignore the last word while iterating.
77 while (iter.Advance()) { 91 while (iter.Advance()) {
78 if (iter.IsWord()) 92 if (iter.IsWord())
79 prefixes[title->substr(0, iter.prev())].push_back(i); 93 prefixes[hostname + title->substr(0, iter.prev())].push_back(i);
80 } 94 }
81 } 95 }
82 96
83 // Now we parse the map to find common prefixes 97 // Now we parse the map to find common prefixes
84 // and keep the largest per title. 98 // and keep the largest per title.
85 for (base::hash_map<string16, std::vector<size_t> >::iterator iter = 99 for (base::hash_map<string16, std::vector<size_t> >::iterator iter =
86 prefixes.begin(); iter != prefixes.end(); ++iter) { 100 prefixes.begin(); iter != prefixes.end(); ++iter) {
87 // iter->first is the prefix string, iter->second is a vector of indices. 101 // iter->first is the prefix string, iter->second is a vector of indices.
88 if (iter->second.size() > 1) { 102 if (iter->second.size() > 1) {
89 size_t prefix_length = iter->first.size(); 103 // We need to subtract the hostname size since we added it to the prefix.
104 const TitleInfo& first_title_info = (*title_infos)[iter->second[0]];
105 DCHECK_GE(iter->first.size(), first_title_info.url.host().size());
106 size_t prefix_length = iter->first.size() -
107 first_title_info.url.host().size();
90 for (size_t i = 0; i < iter->second.size(); ++i){ 108 for (size_t i = 0; i < iter->second.size(); ++i){
91 if (title_infos->at(iter->second[i]).prefix_length < prefix_length) 109 TitleInfo& title_info = (*title_infos)[iter->second[i]];
92 title_infos->at(iter->second[i]).prefix_length = prefix_length; 110 DCHECK_EQ(first_title_info.url.host(), title_info.url.host());
111 if (title_info.prefix_length < prefix_length)
112 title_info.prefix_length = prefix_length;
93 } 113 }
94 } 114 }
95 } 115 }
96 } 116 }
OLDNEW
« no previous file with comments | « chrome/browser/ui/title_prefix_matcher.h ('k') | chrome/browser/ui/title_prefix_matcher_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698