OLD | NEW |
1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 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/extensions/extension_omnibox_api.h" | 5 #include "chrome/browser/extensions/extension_omnibox_api.h" |
6 | 6 |
7 #include "base/json/json_writer.h" | 7 #include "base/json/json_writer.h" |
8 #include "base/string_util.h" | 8 #include "base/string_util.h" |
9 #include "base/values.h" | 9 #include "base/values.h" |
10 #include "chrome/browser/extensions/extension_event_router.h" | 10 #include "chrome/browser/extensions/extension_event_router.h" |
(...skipping 108 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
119 | 119 |
120 return true; | 120 return true; |
121 } | 121 } |
122 | 122 |
123 ExtensionOmniboxSuggestion::ExtensionOmniboxSuggestion() {} | 123 ExtensionOmniboxSuggestion::ExtensionOmniboxSuggestion() {} |
124 | 124 |
125 ExtensionOmniboxSuggestion::~ExtensionOmniboxSuggestion() {} | 125 ExtensionOmniboxSuggestion::~ExtensionOmniboxSuggestion() {} |
126 | 126 |
127 bool ExtensionOmniboxSuggestion::ReadStylesFromValue( | 127 bool ExtensionOmniboxSuggestion::ReadStylesFromValue( |
128 const ListValue& styles_value) { | 128 const ListValue& styles_value) { |
129 // Start with a NONE style covering the entire range. As we iterate over the | |
130 // styles, bisect or overwrite the running style list with each new style. | |
131 description_styles.clear(); | 129 description_styles.clear(); |
132 description_styles.push_back( | 130 |
133 ACMatchClassification(0, ACMatchClassification::NONE)); | 131 // Step 1: Build a vector of styles, 1 per character of description text. |
| 132 std::vector<int> styles; |
| 133 styles.resize(description.length()); // sets all styles to 0 |
134 | 134 |
135 for (size_t i = 0; i < styles_value.GetSize(); ++i) { | 135 for (size_t i = 0; i < styles_value.GetSize(); ++i) { |
136 DictionaryValue* style; | 136 DictionaryValue* style; |
137 std::string type; | 137 std::string type; |
138 int offset; | 138 int offset; |
139 int length; | 139 int length; |
140 if (!styles_value.GetDictionary(i, &style)) | 140 if (!styles_value.GetDictionary(i, &style)) |
141 return false; | 141 return false; |
142 if (!style->GetString(kDescriptionStylesType, &type)) | 142 if (!style->GetString(kDescriptionStylesType, &type)) |
143 return false; | 143 return false; |
144 if (!style->GetInteger(kDescriptionStylesOffset, &offset)) | 144 if (!style->GetInteger(kDescriptionStylesOffset, &offset)) |
145 return false; | 145 return false; |
146 if (!style->GetInteger(kDescriptionStylesLength, &length)) | 146 if (!style->GetInteger(kDescriptionStylesLength, &length) || length < 0) |
147 return false; | 147 length = description.length(); |
| 148 |
| 149 if (offset < 0) |
| 150 offset = std::max(0, static_cast<int>(description.length()) + offset); |
148 | 151 |
149 int type_class = | 152 int type_class = |
150 (type == "url") ? ACMatchClassification::URL : | 153 (type == "url") ? ACMatchClassification::URL : |
151 (type == "match") ? ACMatchClassification::MATCH : | 154 (type == "match") ? ACMatchClassification::MATCH : |
152 (type == "dim") ? ACMatchClassification::DIM : -1; | 155 (type == "dim") ? ACMatchClassification::DIM : -1; |
153 if (type_class == -1) | 156 if (type_class == -1) |
154 return false; | 157 return false; |
155 | 158 |
156 InsertNewStyle(type_class, offset, length); | 159 for (int j = offset; |
| 160 j < offset + length && j < static_cast<int>(styles.size()); ++j) |
| 161 styles[j] |= type_class; |
| 162 } |
| 163 |
| 164 // Step 2: Convert the vector into continous runs of common styles. |
| 165 for (size_t i = 0; i < styles.size(); ++i) { |
| 166 if (i == 0 || styles[i] != styles[i-1]) |
| 167 description_styles.push_back(ACMatchClassification(i, styles[i])); |
157 } | 168 } |
158 | 169 |
159 return true; | 170 return true; |
160 } | 171 } |
161 | 172 |
162 void ExtensionOmniboxSuggestion::InsertNewStyle(int type, | |
163 size_t offset, | |
164 size_t length) { | |
165 // Find the first and last existing styles that this new style overlaps. Those | |
166 // will have to be removed (if they completely overlap), or bisected. | |
167 size_t start = 0, end = 0; | |
168 while (end < description_styles.size()) { | |
169 if (start < description_styles.size() && | |
170 description_styles[start].offset < offset) | |
171 ++start; | |
172 if (description_styles[end].offset <= offset + length) { | |
173 ++end; | |
174 } else { | |
175 break; | |
176 } | |
177 } | |
178 | |
179 DCHECK_GT(end, 0u); | |
180 DCHECK(start == description_styles.size() || | |
181 description_styles[start].offset >= offset); | |
182 DCHECK(end == description_styles.size() || | |
183 description_styles[end].offset > offset + length); | |
184 | |
185 // The last style in the overlapping range will come after our new style. | |
186 int last_style = description_styles[end - 1].style; | |
187 | |
188 // Remove all overlapping styles. | |
189 if (start < end) { | |
190 description_styles.erase(description_styles.begin() + start, | |
191 description_styles.begin() + end); | |
192 } | |
193 | |
194 // Insert our new style. | |
195 description_styles.insert(description_styles.begin() + start, | |
196 ACMatchClassification(offset, type)); | |
197 if (offset + length < description.length()) { | |
198 description_styles.insert(description_styles.begin() + start + 1, | |
199 ACMatchClassification(offset + length, | |
200 last_style)); | |
201 } | |
202 } | |
203 | |
204 ExtensionOmniboxSuggestions::ExtensionOmniboxSuggestions() : request_id(0) {} | 173 ExtensionOmniboxSuggestions::ExtensionOmniboxSuggestions() : request_id(0) {} |
205 | 174 |
206 ExtensionOmniboxSuggestions::~ExtensionOmniboxSuggestions() {} | 175 ExtensionOmniboxSuggestions::~ExtensionOmniboxSuggestions() {} |
OLD | NEW |