| 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/search_engines/edit_search_engine_controller.h" | 5 #include "chrome/browser/search_engines/edit_search_engine_controller.h" |
| 6 | 6 |
| 7 #include "base/string_util.h" | 7 #include "base/string_util.h" |
| 8 #include "chrome/browser/metrics/user_metrics.h" | 8 #include "chrome/browser/metrics/user_metrics.h" |
| 9 #include "chrome/browser/net/url_fixer_upper.h" | 9 #include "chrome/browser/net/url_fixer_upper.h" |
| 10 #include "chrome/browser/profile.h" | 10 #include "chrome/browser/profile.h" |
| 11 #include "chrome/browser/search_engines/template_url.h" | 11 #include "chrome/browser/search_engines/template_url.h" |
| 12 #include "chrome/browser/search_engines/template_url_model.h" | 12 #include "chrome/browser/search_engines/template_url_model.h" |
| 13 #include "googleurl/src/gurl.h" | 13 #include "googleurl/src/gurl.h" |
| 14 | 14 |
| 15 EditSearchEngineController::EditSearchEngineController( | 15 EditSearchEngineController::EditSearchEngineController( |
| 16 const TemplateURL* template_url, | 16 const TemplateURL* template_url, |
| 17 EditSearchEngineControllerDelegate* edit_keyword_delegate, | 17 EditSearchEngineControllerDelegate* edit_keyword_delegate, |
| 18 Profile* profile) | 18 Profile* profile) |
| 19 : template_url_(template_url), | 19 : template_url_(template_url), |
| 20 edit_keyword_delegate_(edit_keyword_delegate), | 20 edit_keyword_delegate_(edit_keyword_delegate), |
| 21 profile_(profile) { | 21 profile_(profile) { |
| 22 DCHECK(profile_); | 22 DCHECK(profile_); |
| 23 } | 23 } |
| 24 | 24 |
| 25 bool EditSearchEngineController::IsTitleValid( | 25 bool EditSearchEngineController::IsTitleValid( |
| 26 const std::wstring& title_input) const { | 26 const std::wstring& title_input) const { |
| 27 return !title_input.empty(); | 27 return !CollapseWhitespace(title_input, true).empty(); |
| 28 } | 28 } |
| 29 | 29 |
| 30 bool EditSearchEngineController::IsURLValid( | 30 bool EditSearchEngineController::IsURLValid( |
| 31 const std::wstring& url_input) const { | 31 const std::wstring& url_input) const { |
| 32 std::wstring url = GetFixedUpURL(url_input); | 32 std::wstring url = GetFixedUpURL(url_input); |
| 33 if (url.empty()) | 33 if (url.empty()) |
| 34 return false; | 34 return false; |
| 35 | 35 |
| 36 // Use TemplateURLRef to extract the search placeholder. | 36 // Use TemplateURLRef to extract the search placeholder. |
| 37 TemplateURLRef template_ref(url, 0, 0); | 37 TemplateURLRef template_ref(url, 0, 0); |
| 38 if (!template_ref.IsValid()) | 38 if (!template_ref.IsValid()) |
| 39 return false; | 39 return false; |
| 40 | 40 |
| 41 if (!template_ref.SupportsReplacement()) | 41 if (!template_ref.SupportsReplacement()) |
| 42 return GURL(WideToUTF16Hack(url)).is_valid(); | 42 return GURL(WideToUTF16Hack(url)).is_valid(); |
| 43 | 43 |
| 44 // If the url has a search term, replace it with a random string and make | 44 // If the url has a search term, replace it with a random string and make |
| 45 // sure the resulting URL is valid. We don't check the validity of the url | 45 // sure the resulting URL is valid. We don't check the validity of the url |
| 46 // with the search term as that is not necessarily valid. | 46 // with the search term as that is not necessarily valid. |
| 47 return GURL(WideToUTF8(template_ref.ReplaceSearchTerms(TemplateURL(), L"a", | 47 return GURL(WideToUTF8(template_ref.ReplaceSearchTerms(TemplateURL(), L"a", |
| 48 TemplateURLRef::NO_SUGGESTIONS_AVAILABLE, std::wstring()))).is_valid(); | 48 TemplateURLRef::NO_SUGGESTIONS_AVAILABLE, std::wstring()))).is_valid(); |
| 49 } | 49 } |
| 50 | 50 |
| 51 bool EditSearchEngineController::IsKeywordValid( | 51 bool EditSearchEngineController::IsKeywordValid( |
| 52 const std::wstring& keyword_input) const { | 52 const std::wstring& keyword_input) const { |
| 53 if (keyword_input.empty()) | 53 std::wstring keyword_input_trimmed(CollapseWhitespace(keyword_input, true)); |
| 54 return true; // Always allow no keyword. | 54 if (keyword_input_trimmed.empty()) |
| 55 return false; // Do not allow empty keyword. |
| 55 const TemplateURL* turl_with_keyword = | 56 const TemplateURL* turl_with_keyword = |
| 56 profile_->GetTemplateURLModel()->GetTemplateURLForKeyword(keyword_input); | 57 profile_->GetTemplateURLModel()->GetTemplateURLForKeyword( |
| 58 keyword_input_trimmed); |
| 57 return (turl_with_keyword == NULL || turl_with_keyword == template_url_); | 59 return (turl_with_keyword == NULL || turl_with_keyword == template_url_); |
| 58 } | 60 } |
| 59 | 61 |
| 60 void EditSearchEngineController::AcceptAddOrEdit( | 62 void EditSearchEngineController::AcceptAddOrEdit( |
| 61 const std::wstring& title_input, | 63 const std::wstring& title_input, |
| 62 const std::wstring& keyword_input, | 64 const std::wstring& keyword_input, |
| 63 const std::wstring& url_input) { | 65 const std::wstring& url_input) { |
| 64 std::wstring url_string = GetFixedUpURL(url_input); | 66 std::wstring url_string = GetFixedUpURL(url_input); |
| 65 DCHECK(!url_string.empty()); | 67 DCHECK(!url_string.empty()); |
| 66 | 68 |
| (...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 129 std::string scheme( | 131 std::string scheme( |
| 130 URLFixerUpper::SegmentURL(WideToUTF8(expanded_url), &parts)); | 132 URLFixerUpper::SegmentURL(WideToUTF8(expanded_url), &parts)); |
| 131 if(!parts.scheme.is_valid()) { | 133 if(!parts.scheme.is_valid()) { |
| 132 scheme.append("://"); | 134 scheme.append("://"); |
| 133 url.insert(0, UTF8ToWide(scheme)); | 135 url.insert(0, UTF8ToWide(scheme)); |
| 134 } | 136 } |
| 135 | 137 |
| 136 return url; | 138 return url; |
| 137 } | 139 } |
| 138 | 140 |
| OLD | NEW |