OLD | NEW |
| (Empty) |
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 | |
3 // found in the LICENSE file. | |
4 | |
5 #ifndef CHROME_BROWSER_UI_TITLE_PREFIX_MATCHER_H_ | |
6 #define CHROME_BROWSER_UI_TITLE_PREFIX_MATCHER_H_ | |
7 #pragma once | |
8 | |
9 #include <vector> | |
10 | |
11 #include "base/string16.h" | |
12 #include "googleurl/src/gurl.h" | |
13 | |
14 // This class exposes a static method that receives a vector of TitleInfo | |
15 // objects so that it can find the length of the common prefixes among all | |
16 // the titles. It can be used for tab titles for example so that the common | |
17 // prefixes can be elided. | |
18 // First, the caller needs to fill a vector of TitleInfo objects with the titles | |
19 // for which they want to find the common prefix lengths. They can also provide | |
20 // an optional caller_value where the index of the tabs could be saved | |
21 // for example. This way the caller can remember which tab this title belongs | |
22 // to, if not all tabs are passed into the vector. | |
23 // When CalculatePrefixLengths returns, the TitleInfo objects in the vector | |
24 // are set with the prefix_length that is common between this title | |
25 // and at least one other. | |
26 // Note that the prefix_length is only calculated at word boundaries. | |
27 class TitlePrefixMatcher { | |
28 public: | |
29 struct TitleInfo { | |
30 TitleInfo(const string16* title, const GURL& url, int caller_value); | |
31 ~TitleInfo(); | |
32 // We assume the title string will be valid throughout the execution of | |
33 // the prefix lengths calculation, and so we use a pointer to avoid an | |
34 // unnecessary string copy. | |
35 const string16* title; | |
36 // We only look for common prefix when the URL has the same hostname. | |
37 GURL url; | |
38 // This contains the number of characters at the beginning of title that | |
39 // are common with other titles in the TitleInfo vector. | |
40 size_t prefix_length; | |
41 // Utility data space for the caller. Unused by CalculatePrefixLengths. | |
42 int caller_value; | |
43 }; | |
44 static void CalculatePrefixLengths(std::vector<TitleInfo>* title_infos); | |
45 | |
46 // We want to show the last few common chars of a page title in the tab, | |
47 // so we only do it if the common prefix is at least kMinElidingLength, | |
48 // otherwise, we could be replacing less characters than the ellipsis take. | |
49 static const int kCommonCharsToShow; | |
50 static const size_t kMinElidingLength; | |
51 | |
52 private: | |
53 DISALLOW_IMPLICIT_CONSTRUCTORS(TitlePrefixMatcher); | |
54 }; | |
55 | |
56 #endif // CHROME_BROWSER_UI_TITLE_PREFIX_MATCHER_H_ | |
OLD | NEW |