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

Side by Side Diff: chrome/browser/search_engines/template_url_parser.cc

Issue 6291003: Revert 71485 - Remove wstring from TemplateURL and friends.... (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src/
Patch Set: Created 9 years, 11 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) 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/search_engines/template_url_parser.h" 5 #include "chrome/browser/search_engines/template_url_parser.h"
6 6
7 #include <algorithm> 7 #include <algorithm>
8 #include <map> 8 #include <map>
9 #include <vector> 9 #include <vector>
10 10
11 #include "base/logging.h" 11 #include "base/logging.h"
(...skipping 119 matching lines...) Expand 10 before | Expand all | Expand 10 after
131 // We only expect PARAM nodes under the Url node 131 // We only expect PARAM nodes under the Url node
132 if (elements_.size() == 3 && elements_[0] == OPEN_SEARCH_DESCRIPTION && 132 if (elements_.size() == 3 && elements_[0] == OPEN_SEARCH_DESCRIPTION &&
133 elements_[1] == URL && elements_[2] == PARAM) 133 elements_[1] == URL && elements_[2] == PARAM)
134 return PARAM; 134 return PARAM;
135 135
136 return UNKNOWN; 136 return UNKNOWN;
137 } 137 }
138 138
139 TemplateURL* template_url() { return url_; } 139 TemplateURL* template_url() { return url_; }
140 140
141 void AddImageRef(const std::string& type, int width, int height) { 141 void AddImageRef(const std::wstring& type, int width, int height) {
142 if (width > 0 && height > 0) 142 if (width > 0 && height > 0)
143 current_image_.reset(new TemplateURL::ImageRef(type, width, height)); 143 current_image_.reset(new TemplateURL::ImageRef(type, width, height));
144 } 144 }
145 145
146 void EndImage() { 146 void EndImage() {
147 current_image_.reset(); 147 current_image_.reset();
148 } 148 }
149 149
150 void SetImageURL(const GURL& url) { 150 void SetImageURL(const std::wstring& url) {
151 if (current_image_.get()) { 151 if (current_image_.get()) {
152 current_image_->url = url; 152 current_image_->url = GURL(WideToUTF8(url));
153 url_->add_image_ref(*current_image_); 153 url_->add_image_ref(*current_image_);
154 current_image_.reset(); 154 current_image_.reset();
155 } 155 }
156 } 156 }
157 157
158 void ResetString() { 158 void ResetString() {
159 string_.clear(); 159 string_.clear();
160 } 160 }
161 161
162 void AppendString(const string16& string) { 162 void AppendString(const std::wstring& string) {
163 string_ += string; 163 string_ += string;
164 } 164 }
165 165
166 const string16& GetString() { 166 const std::wstring& GetString() {
167 return string_; 167 return string_;
168 } 168 }
169 169
170 void ResetExtraParams() { 170 void ResetExtraParams() {
171 extra_params_.clear(); 171 extra_params_.clear();
172 } 172 }
173 173
174 void AddExtraParams(const std::string& key, const std::string& value) { 174 void AddExtraParams(const std::string& key, const std::string& value) {
175 if (parameter_filter_ && !parameter_filter_->KeepParameter(key, value)) 175 if (parameter_filter_ && !parameter_filter_->KeepParameter(key, value))
176 return; 176 return;
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after
226 226
227 // Key is UTF8 encoded. 227 // Key is UTF8 encoded.
228 static std::map<std::string, ElementType>* kElementNameToElementTypeMap; 228 static std::map<std::string, ElementType>* kElementNameToElementTypeMap;
229 // TemplateURL supplied to Read method. It's owned by the caller, so we 229 // TemplateURL supplied to Read method. It's owned by the caller, so we
230 // don't need to free it. 230 // don't need to free it.
231 TemplateURL* url_; 231 TemplateURL* url_;
232 std::vector<ElementType> elements_; 232 std::vector<ElementType> elements_;
233 scoped_ptr<TemplateURL::ImageRef> current_image_; 233 scoped_ptr<TemplateURL::ImageRef> current_image_;
234 234
235 // Character content for the current element. 235 // Character content for the current element.
236 string16 string_; 236 std::wstring string_;
237 237
238 TemplateURLParser::ParameterFilter* parameter_filter_; 238 TemplateURLParser::ParameterFilter* parameter_filter_;
239 239
240 // The list of parameters parsed in the Param nodes of a Url node. 240 // The list of parameters parsed in the Param nodes of a Url node.
241 std::vector<Param> extra_params_; 241 std::vector<Param> extra_params_;
242 242
243 // The HTTP methods used. 243 // The HTTP methods used.
244 Method method_; 244 Method method_;
245 Method suggestion_method_; 245 Method suggestion_method_;
246 246
247 // If true, we are currently parsing a suggest URL, otherwise it is an HTML 247 // If true, we are currently parsing a suggest URL, otherwise it is an HTML
248 // search. Note that we don't need a stack as Url nodes cannot be nested. 248 // search. Note that we don't need a stack as Url nodes cannot be nested.
249 bool is_suggest_url_; 249 bool is_suggest_url_;
250 250
251 // Whether we should derive the image from the URL (when images are data 251 // Whether we should derive the image from the URL (when images are data
252 // URLs). 252 // URLs).
253 bool derive_image_from_url_; 253 bool derive_image_from_url_;
254 254
255 DISALLOW_COPY_AND_ASSIGN(ParsingContext); 255 DISALLOW_COPY_AND_ASSIGN(ParsingContext);
256 }; 256 };
257 257
258 // static 258 // static
259 std::map<std::string, ParsingContext::ElementType>* 259 std::map<std::string, ParsingContext::ElementType>*
260 ParsingContext::kElementNameToElementTypeMap = NULL; 260 ParsingContext::kElementNameToElementTypeMap = NULL;
261 261
262 string16 XMLCharToUTF16(const xmlChar* value, int length) { 262 std::wstring XMLCharToWide(const xmlChar* value) {
263 return UTF8ToUTF16(std::string((const char*)value, length)); 263 return UTF8ToWide(std::string((const char*)value));
264 }
265
266 std::wstring XMLCharToWide(const xmlChar* value, int length) {
267 return UTF8ToWide(std::string((const char*)value, length));
264 } 268 }
265 269
266 std::string XMLCharToString(const xmlChar* value) { 270 std::string XMLCharToString(const xmlChar* value) {
267 return std::string((const char*)value); 271 return std::string((const char*)value);
268 } 272 }
269 273
270 // Returns true if input_encoding contains a valid input encoding string. This 274 // Returns true if input_encoding contains a valid input encoding string. This
271 // doesn't verify that we have a valid encoding for the string, just that the 275 // doesn't verify that we have a valid encoding for the string, just that the
272 // string contains characters that constitute a valid input encoding. 276 // string contains characters that constitute a valid input encoding.
273 bool IsValidEncodingString(const std::string& input_encoding) { 277 bool IsValidEncodingString(const std::string& input_encoding) {
(...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after
333 } 337 }
334 } 338 }
335 339
336 void ParseImage(const xmlChar** atts, ParsingContext* context) { 340 void ParseImage(const xmlChar** atts, ParsingContext* context) {
337 if (!atts) 341 if (!atts)
338 return; 342 return;
339 343
340 const xmlChar** attributes = atts; 344 const xmlChar** attributes = atts;
341 int width = 0; 345 int width = 0;
342 int height = 0; 346 int height = 0;
343 std::string type; 347 std::wstring type;
344 while (*attributes) { 348 while (*attributes) {
345 std::string name(XMLCharToString(*attributes)); 349 std::string name(XMLCharToString(*attributes));
346 const xmlChar* value = attributes[1]; 350 const xmlChar* value = attributes[1];
347 if (name == kImageTypeAttribute) { 351 if (name == kImageTypeAttribute) {
348 type = XMLCharToString(value); 352 type = XMLCharToWide(value);
349 } else if (name == kImageWidthAttribute) { 353 } else if (name == kImageWidthAttribute) {
350 base::StringToInt(XMLCharToString(value), &width); 354 base::StringToInt(XMLCharToString(value), &width);
351 } else if (name == kImageHeightAttribute) { 355 } else if (name == kImageHeightAttribute) {
352 base::StringToInt(XMLCharToString(value), &height); 356 base::StringToInt(XMLCharToString(value), &height);
353 } 357 }
354 attributes += 2; 358 attributes += 2;
355 } 359 }
356 if (width > 0 && height > 0 && !type.empty()) { 360 if (width > 0 && height > 0 && !type.empty()) {
357 // Valid Image URL. 361 // Valid Image URL.
358 context->AddImageRef(type, width, height); 362 context->AddImageRef(type, width, height);
359 } 363 }
360 } 364 }
361 365
362 void ParseParam(const xmlChar** atts, ParsingContext* context) { 366 void ParseParam(const xmlChar** atts, ParsingContext* context) {
363 if (!atts) 367 if (!atts)
364 return; 368 return;
365 369
366 const xmlChar** attributes = atts; 370 const xmlChar** attributes = atts;
371 std::wstring type;
367 std::string key, value; 372 std::string key, value;
368 while (*attributes) { 373 while (*attributes) {
369 std::string name(XMLCharToString(*attributes)); 374 std::string name(XMLCharToString(*attributes));
370 const xmlChar* val = attributes[1]; 375 const xmlChar* val = attributes[1];
371 if (name == kParamNameAttribute) { 376 if (name == kParamNameAttribute) {
372 key = XMLCharToString(val); 377 key = XMLCharToString(val);
373 } else if (name == kParamValueAttribute) { 378 } else if (name == kParamValueAttribute) {
374 value = XMLCharToString(val); 379 value = XMLCharToString(val);
375 } 380 }
376 attributes += 2; 381 attributes += 2;
(...skipping 98 matching lines...) Expand 10 before | Expand all | Expand 10 after
475 void EndElementImpl(void *ctx, const xmlChar *name) { 480 void EndElementImpl(void *ctx, const xmlChar *name) {
476 ParsingContext* context = reinterpret_cast<ParsingContext*>(ctx); 481 ParsingContext* context = reinterpret_cast<ParsingContext*>(ctx);
477 switch (context->GetKnownType()) { 482 switch (context->GetKnownType()) {
478 case ParsingContext::SHORT_NAME: 483 case ParsingContext::SHORT_NAME:
479 context->template_url()->set_short_name(context->GetString()); 484 context->template_url()->set_short_name(context->GetString());
480 break; 485 break;
481 case ParsingContext::DESCRIPTION: 486 case ParsingContext::DESCRIPTION:
482 context->template_url()->set_description(context->GetString()); 487 context->template_url()->set_description(context->GetString());
483 break; 488 break;
484 case ParsingContext::IMAGE: { 489 case ParsingContext::IMAGE: {
485 GURL image_url(UTF16ToUTF8(context->GetString())); 490 GURL image_url(WideToUTF8(context->GetString()));
486 if (image_url.SchemeIs(chrome::kDataScheme)) { 491 if (image_url.SchemeIs(chrome::kDataScheme)) {
487 // TODO (jcampan): bug 1169256: when dealing with data URL, we need to 492 // TODO (jcampan): bug 1169256: when dealing with data URL, we need to
488 // decode the data URL in the renderer. For now, we'll just point to the 493 // decode the data URL in the renderer. For now, we'll just point to the
489 // fav icon from the URL. 494 // fav icon from the URL.
490 context->set_derive_image_from_url(true); 495 context->set_derive_image_from_url(true);
491 } else { 496 } else {
492 context->SetImageURL(image_url); 497 context->SetImageURL(context->GetString());
493 } 498 }
494 context->EndImage(); 499 context->EndImage();
495 break; 500 break;
496 } 501 }
497 case ParsingContext::LANGUAGE: 502 case ParsingContext::LANGUAGE:
498 context->template_url()->add_language(context->GetString()); 503 context->template_url()->add_language(context->GetString());
499 break; 504 break;
500 case ParsingContext::INPUT_ENCODING: { 505 case ParsingContext::INPUT_ENCODING: {
501 std::string input_encoding = UTF16ToASCII(context->GetString()); 506 std::string input_encoding = WideToASCII(context->GetString());
502 if (IsValidEncodingString(input_encoding)) 507 if (IsValidEncodingString(input_encoding))
503 context->template_url()->add_input_encoding(input_encoding); 508 context->template_url()->add_input_encoding(input_encoding);
504 break; 509 break;
505 } 510 }
506 case ParsingContext::URL: 511 case ParsingContext::URL:
507 ProcessURLParams(context); 512 ProcessURLParams(context);
508 break; 513 break;
509 default: 514 default:
510 break; 515 break;
511 } 516 }
512 context->ResetString(); 517 context->ResetString();
513 context->PopElement(); 518 context->PopElement();
514 } 519 }
515 520
516 void CharactersImpl(void *ctx, const xmlChar *ch, int len) { 521 void CharactersImpl(void *ctx, const xmlChar *ch, int len) {
517 ParsingContext* context = reinterpret_cast<ParsingContext*>(ctx); 522 ParsingContext* context = reinterpret_cast<ParsingContext*>(ctx);
518 context->AppendString(XMLCharToUTF16(ch, len)); 523 context->AppendString(XMLCharToWide(ch, len));
519 } 524 }
520 525
521 // Returns true if the ref is null, or the url wrapped by ref is 526 // Returns true if the ref is null, or the url wrapped by ref is
522 // valid with a spec of http/https. 527 // valid with a spec of http/https.
523 bool IsHTTPRef(const TemplateURLRef* ref) { 528 bool IsHTTPRef(const TemplateURLRef* ref) {
524 if (ref == NULL) 529 if (ref == NULL)
525 return true; 530 return true;
526 GURL url(ref->url()); 531 GURL url(ref->url());
527 return (url.is_valid() && (url.SchemeIs(chrome::kHttpScheme) || 532 return (url.is_valid() && (url.SchemeIs(chrome::kHttpScheme) ||
528 url.SchemeIs(chrome::kHttpsScheme))); 533 url.SchemeIs(chrome::kHttpsScheme)));
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after
578 return false; 583 return false;
579 if (context.suggestion_method() == ParsingContext::POST) 584 if (context.suggestion_method() == ParsingContext::POST)
580 url->SetSuggestionsURL("", 0, 0); 585 url->SetSuggestionsURL("", 0, 0);
581 586
582 if (!url->short_name().empty() && !url->description().empty()) { 587 if (!url->short_name().empty() && !url->description().empty()) {
583 // So far so good, make sure the urls are http. 588 // So far so good, make sure the urls are http.
584 return IsLegal(url); 589 return IsLegal(url);
585 } 590 }
586 return false; 591 return false;
587 } 592 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698