OLD | NEW |
| (Empty) |
1 // Copyright (c) 2012 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 #include "chrome/browser/autocomplete/shortcuts_provider_shortcut.h" | |
5 | |
6 #include "base/string_number_conversions.h" | |
7 #include "base/string_util.h" | |
8 #include "base/time.h" | |
9 #include "base/utf_string_conversions.h" | |
10 #include "chrome/browser/autocomplete/shortcuts_provider.h" | |
11 | |
12 namespace { | |
13 | |
14 // Takes Match classification vector and removes all matched positions, | |
15 // compacting repetitions if necessary. | |
16 void StripMatchMarkersFromClassifications(ACMatchClassifications* matches) { | |
17 DCHECK(matches); | |
18 ACMatchClassifications unmatched; | |
19 for (ACMatchClassifications::iterator i = matches->begin(); | |
20 i != matches->end(); ++i) { | |
21 shortcuts_provider::AddLastMatchIfNeeded(&unmatched, i->offset, | |
22 i->style & ~ACMatchClassification::MATCH); | |
23 } | |
24 matches->swap(unmatched); | |
25 } | |
26 | |
27 } // namespace | |
28 | |
29 namespace shortcuts_provider { | |
30 | |
31 Shortcut::Shortcut(const string16& text, | |
32 const GURL& url, | |
33 const string16& contents, | |
34 const ACMatchClassifications& in_contents_class, | |
35 const string16& description, | |
36 const ACMatchClassifications& in_description_class) | |
37 : text(text), | |
38 url(url), | |
39 contents(contents), | |
40 contents_class(in_contents_class), | |
41 description(description), | |
42 description_class(in_description_class), | |
43 last_access_time(base::Time::Now()), | |
44 number_of_hits(1) { | |
45 StripMatchMarkersFromClassifications(&contents_class); | |
46 StripMatchMarkersFromClassifications(&description_class); | |
47 } | |
48 | |
49 Shortcut::Shortcut(const std::string& id, | |
50 const string16& text, | |
51 const string16& url, | |
52 const string16& contents, | |
53 const string16& contents_class, | |
54 const string16& description, | |
55 const string16& description_class, | |
56 int64 time_of_last_access, | |
57 int number_of_hits) | |
58 : id(id), | |
59 text(text), | |
60 url(url), | |
61 contents(contents), | |
62 contents_class(SpansFromString(contents_class)), | |
63 description(description), | |
64 description_class(SpansFromString(description_class)), | |
65 last_access_time(base::Time::FromInternalValue(time_of_last_access)), | |
66 number_of_hits(1) {} | |
67 | |
68 Shortcut::Shortcut() | |
69 : last_access_time(base::Time::Now()), | |
70 number_of_hits(0) {} | |
71 | |
72 Shortcut::~Shortcut() {} | |
73 | |
74 string16 Shortcut::contents_class_as_str() const { | |
75 return SpansToString(contents_class); | |
76 } | |
77 | |
78 string16 Shortcut::description_class_as_str() const { | |
79 return SpansToString(description_class); | |
80 } | |
81 | |
82 string16 SpansToString(const ACMatchClassifications& value) { | |
83 string16 spans; | |
84 string16 comma(ASCIIToUTF16(",")); | |
85 for (size_t i = 0; i < value.size(); ++i) { | |
86 if (i) | |
87 spans.append(comma); | |
88 spans.append(base::IntToString16(value[i].offset)); | |
89 spans.append(comma); | |
90 spans.append(base::IntToString16(value[i].style)); | |
91 } | |
92 return spans; | |
93 } | |
94 | |
95 ACMatchClassifications SpansFromString(const string16& value) { | |
96 ACMatchClassifications spans; | |
97 std::vector<string16> tokens; | |
98 Tokenize(value, ASCIIToUTF16(","), &tokens); | |
99 // The number of tokens should be even. | |
100 if ((tokens.size() & 1) == 1) { | |
101 NOTREACHED(); | |
102 return spans; | |
103 } | |
104 for (size_t i = 0; i < tokens.size(); i += 2) { | |
105 int span_start = 0; | |
106 int span_type = ACMatchClassification::NONE; | |
107 if (!base::StringToInt(tokens[i], &span_start) || | |
108 !base::StringToInt(tokens[i + 1], &span_type)) { | |
109 NOTREACHED(); | |
110 return spans; | |
111 } | |
112 spans.push_back(ACMatchClassification(span_start, span_type)); | |
113 } | |
114 return spans; | |
115 } | |
116 | |
117 // Adds match at the end if and only if its style is different from the last | |
118 // match. | |
119 void AddLastMatchIfNeeded(ACMatchClassifications* matches, | |
120 size_t position, | |
121 int style) { | |
122 DCHECK(matches); | |
123 if (matches->empty() || matches->back().style != style) { | |
124 if (!matches->empty()) | |
125 DCHECK_GT(position, matches->back().offset); | |
126 matches->push_back(ACMatchClassification(position, style)); | |
127 } | |
128 } | |
129 | |
130 } // namespace shortcuts_provider | |
OLD | NEW |