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

Side by Side Diff: chrome/browser/extensions/extension_omnibox_api.cc

Issue 5064001: More changes to extension omnibox API for styles. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 10 years, 1 month 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) 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
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) || offset < 0)
145 return false; 145 return false;
146 if (!style->GetInteger(kDescriptionStylesLength, &length)) 146 if (!style->GetInteger(kDescriptionStylesLength, &length) || length < 0)
asargent_no_longer_on_chrome 2010/11/16 19:05:02 It seems like passing a negative length value is l
Matt Perry 2010/11/16 22:26:44 The negative checks here are just me being paranoi
147 return false; 147 length = description.length();
148 148
149 int type_class = 149 int type_class =
150 (type == "url") ? ACMatchClassification::URL : 150 (type == "url") ? ACMatchClassification::URL :
151 (type == "match") ? ACMatchClassification::MATCH : 151 (type == "match") ? ACMatchClassification::MATCH :
152 (type == "dim") ? ACMatchClassification::DIM : -1; 152 (type == "dim") ? ACMatchClassification::DIM : -1;
153 if (type_class == -1) 153 if (type_class == -1)
154 return false; 154 return false;
155 155
156 InsertNewStyle(type_class, offset, length); 156 for (int j = offset;
157 j < offset + length && j < static_cast<int>(styles.size()); ++j)
158 styles[j] |= type_class;
159 }
160
161 // Step 2: Convert the vector into continous runs of common styles.
162 for (size_t i = 0; i < styles.size(); ++i) {
163 if (i == 0 || styles[i] != styles[i-1])
164 description_styles.push_back(ACMatchClassification(i, styles[i]));
157 } 165 }
158 166
159 return true; 167 return true;
160 } 168 }
161 169
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) {} 170 ExtensionOmniboxSuggestions::ExtensionOmniboxSuggestions() : request_id(0) {}
205 171
206 ExtensionOmniboxSuggestions::~ExtensionOmniboxSuggestions() {} 172 ExtensionOmniboxSuggestions::~ExtensionOmniboxSuggestions() {}
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698