OLD | NEW |
1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2009 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/views/edit_keyword_controller.h" | 5 #include "chrome/browser/views/edit_keyword_controller.h" |
6 | 6 |
7 #include "app/l10n_util.h" | 7 #include "app/l10n_util.h" |
8 #include "app/resource_bundle.h" | 8 #include "app/resource_bundle.h" |
9 #include "base/string_util.h" | 9 #include "base/string_util.h" |
10 #include "chrome/browser/metrics/user_metrics.h" | 10 #include "chrome/browser/metrics/user_metrics.h" |
(...skipping 280 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
291 TemplateURLRef template_ref(url, 0, 0); | 291 TemplateURLRef template_ref(url, 0, 0); |
292 if (!template_ref.IsValid()) | 292 if (!template_ref.IsValid()) |
293 return false; | 293 return false; |
294 | 294 |
295 if (!template_ref.SupportsReplacement()) | 295 if (!template_ref.SupportsReplacement()) |
296 return GURL(url).is_valid(); | 296 return GURL(url).is_valid(); |
297 | 297 |
298 // If the url has a search term, replace it with a random string and make | 298 // If the url has a search term, replace it with a random string and make |
299 // sure the resulting URL is valid. We don't check the validity of the url | 299 // sure the resulting URL is valid. We don't check the validity of the url |
300 // with the search term as that is not necessarily valid. | 300 // with the search term as that is not necessarily valid. |
301 return template_ref.ReplaceSearchTerms(TemplateURL(), L"a", | 301 return GURL(WideToUTF8(template_ref.ReplaceSearchTerms(TemplateURL(), L"a", |
302 TemplateURLRef::NO_SUGGESTIONS_AVAILABLE, std::wstring()).is_valid(); | 302 TemplateURLRef::NO_SUGGESTIONS_AVAILABLE, std::wstring()))).is_valid(); |
303 } | 303 } |
304 | 304 |
305 std::wstring EditKeywordController::GetURL() const { | 305 std::wstring EditKeywordController::GetURL() const { |
306 std::wstring url; | 306 std::wstring url; |
307 TrimWhitespace(TemplateURLRef::DisplayURLToURLRef(url_tf_->text()), | 307 TrimWhitespace(TemplateURLRef::DisplayURLToURLRef(url_tf_->text()), |
308 TRIM_ALL, &url); | 308 TRIM_ALL, &url); |
309 if (url.empty()) | 309 if (url.empty()) |
310 return url; | 310 return url; |
311 | 311 |
312 // Parse the string as a URL to determine the scheme. If a scheme hasn't been | 312 // Parse the string as a URL to determine the scheme. If we need to, add the |
313 // specified, we add it. | 313 // scheme. As the scheme may be expanded (as happens with {google:baseURL}) |
| 314 // we need to replace the search terms before testing for the scheme. |
| 315 TemplateURL t_url; |
| 316 t_url.SetURL(url, 0, 0); |
| 317 std::wstring expanded_url = |
| 318 t_url.url()->ReplaceSearchTerms(t_url, L"x", 0, std::wstring()); |
314 url_parse::Parsed parts; | 319 url_parse::Parsed parts; |
315 std::wstring scheme(URLFixerUpper::SegmentURL(url, &parts)); | 320 std::string scheme( |
316 if (!parts.scheme.is_valid()) { | 321 URLFixerUpper::SegmentURL(WideToUTF8(expanded_url), &parts)); |
317 std::wstring fixed_scheme(scheme); | 322 if(!parts.scheme.is_valid()) { |
318 fixed_scheme.append(L"://"); | 323 scheme.append("://"); |
319 url.insert(0, fixed_scheme); | 324 url.insert(0, UTF8ToWide(scheme)); |
320 } | 325 } |
321 | 326 |
322 return url; | 327 return url; |
323 } | 328 } |
324 | 329 |
325 bool EditKeywordController::IsKeywordValid() const { | 330 bool EditKeywordController::IsKeywordValid() const { |
326 std::wstring keyword = keyword_tf_->text(); | 331 std::wstring keyword = keyword_tf_->text(); |
327 if (keyword.empty()) | 332 if (keyword.empty()) |
328 return true; // Always allow no keyword. | 333 return true; // Always allow no keyword. |
329 const TemplateURL* turl_with_keyword = | 334 const TemplateURL* turl_with_keyword = |
(...skipping 26 matching lines...) Expand all Loading... |
356 } | 361 } |
357 | 362 |
358 void EditKeywordController::CleanUpCancelledAdd() { | 363 void EditKeywordController::CleanUpCancelledAdd() { |
359 if (!keyword_editor_view_ && template_url_) { | 364 if (!keyword_editor_view_ && template_url_) { |
360 // When we have no KeywordEditorView, we know that the template_url_ | 365 // When we have no KeywordEditorView, we know that the template_url_ |
361 // hasn't yet been added to the model, so we need to clean it up. | 366 // hasn't yet been added to the model, so we need to clean it up. |
362 delete template_url_; | 367 delete template_url_; |
363 template_url_ = NULL; | 368 template_url_ = NULL; |
364 } | 369 } |
365 } | 370 } |
OLD | NEW |