| OLD | NEW |
| 1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 "base/utf_string_conversions.h" | 8 #include "base/utf_string_conversions.h" |
| 9 #include "chrome/browser/metrics/user_metrics.h" | 9 #include "chrome/browser/metrics/user_metrics.h" |
| 10 #include "chrome/browser/net/url_fixer_upper.h" | 10 #include "chrome/browser/net/url_fixer_upper.h" |
| 11 #include "chrome/browser/profiles/profile.h" | 11 #include "chrome/browser/profiles/profile.h" |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 44 // placeholder. | 44 // placeholder. |
| 45 if (template_url_ == | 45 if (template_url_ == |
| 46 profile_->GetTemplateURLModel()->GetDefaultSearchProvider()) | 46 profile_->GetTemplateURLModel()->GetDefaultSearchProvider()) |
| 47 return false; | 47 return false; |
| 48 return GURL(url).is_valid(); | 48 return GURL(url).is_valid(); |
| 49 } | 49 } |
| 50 | 50 |
| 51 // If the url has a search term, replace it with a random string and make | 51 // If the url has a search term, replace it with a random string and make |
| 52 // sure the resulting URL is valid. We don't check the validity of the url | 52 // sure the resulting URL is valid. We don't check the validity of the url |
| 53 // with the search term as that is not necessarily valid. | 53 // with the search term as that is not necessarily valid. |
| 54 return GURL(template_ref.ReplaceSearchTerms(TemplateURL(), L"a", | 54 return GURL(template_ref.ReplaceSearchTerms(TemplateURL(), ASCIIToUTF16("a"), |
| 55 TemplateURLRef::NO_SUGGESTIONS_AVAILABLE, std::wstring())).is_valid(); | 55 TemplateURLRef::NO_SUGGESTIONS_AVAILABLE, string16())).is_valid(); |
| 56 } | 56 } |
| 57 | 57 |
| 58 bool EditSearchEngineController::IsKeywordValid( | 58 bool EditSearchEngineController::IsKeywordValid( |
| 59 const string16& keyword_input) const { | 59 const string16& keyword_input) const { |
| 60 string16 keyword_input_trimmed(CollapseWhitespace(keyword_input, true)); | 60 string16 keyword_input_trimmed(CollapseWhitespace(keyword_input, true)); |
| 61 if (keyword_input_trimmed.empty()) | 61 if (keyword_input_trimmed.empty()) |
| 62 return false; // Do not allow empty keyword. | 62 return false; // Do not allow empty keyword. |
| 63 const TemplateURL* turl_with_keyword = | 63 const TemplateURL* turl_with_keyword = |
| 64 profile_->GetTemplateURLModel()->GetTemplateURLForKeyword( | 64 profile_->GetTemplateURLModel()->GetTemplateURLForKeyword( |
| 65 UTF16ToWideHack(keyword_input_trimmed)); | 65 keyword_input_trimmed); |
| 66 return (turl_with_keyword == NULL || turl_with_keyword == template_url_); | 66 return (turl_with_keyword == NULL || turl_with_keyword == template_url_); |
| 67 } | 67 } |
| 68 | 68 |
| 69 void EditSearchEngineController::AcceptAddOrEdit( | 69 void EditSearchEngineController::AcceptAddOrEdit( |
| 70 const string16& title_input, | 70 const string16& title_input, |
| 71 const string16& keyword_input, | 71 const string16& keyword_input, |
| 72 const std::string& url_input) { | 72 const std::string& url_input) { |
| 73 std::string url_string = GetFixedUpURL(url_input); | 73 std::string url_string = GetFixedUpURL(url_input); |
| 74 DCHECK(!url_string.empty()); | 74 DCHECK(!url_string.empty()); |
| 75 | 75 |
| 76 const TemplateURL* existing = | 76 const TemplateURL* existing = |
| 77 profile_->GetTemplateURLModel()->GetTemplateURLForKeyword( | 77 profile_->GetTemplateURLModel()->GetTemplateURLForKeyword( |
| 78 UTF16ToWideHack(keyword_input)); | 78 keyword_input); |
| 79 if (existing && | 79 if (existing && |
| 80 (!edit_keyword_delegate_ || existing != template_url_)) { | 80 (!edit_keyword_delegate_ || existing != template_url_)) { |
| 81 // An entry may have been added with the same keyword string while the | 81 // An entry may have been added with the same keyword string while the |
| 82 // user edited the dialog, either automatically or by the user (if we're | 82 // user edited the dialog, either automatically or by the user (if we're |
| 83 // confirming a JS addition, they could have the Options dialog open at the | 83 // confirming a JS addition, they could have the Options dialog open at the |
| 84 // same time). If so, just ignore this add. | 84 // same time). If so, just ignore this add. |
| 85 // TODO(pamg): Really, we should modify the entry so this later one | 85 // TODO(pamg): Really, we should modify the entry so this later one |
| 86 // overwrites it. But we don't expect this case to be common. | 86 // overwrites it. But we don't expect this case to be common. |
| 87 CleanUpCancelledAdd(); | 87 CleanUpCancelledAdd(); |
| 88 return; | 88 return; |
| 89 } | 89 } |
| 90 | 90 |
| 91 if (!edit_keyword_delegate_) { | 91 if (!edit_keyword_delegate_) { |
| 92 // Confiming an entry we got from JS. We have a template_url_, but it | 92 // Confiming an entry we got from JS. We have a template_url_, but it |
| 93 // hasn't yet been added to the model. | 93 // hasn't yet been added to the model. |
| 94 DCHECK(template_url_); | 94 DCHECK(template_url_); |
| 95 // const_cast is ugly, but this is the same thing the TemplateURLModel | 95 // const_cast is ugly, but this is the same thing the TemplateURLModel |
| 96 // does in a similar situation (updating an existing TemplateURL with | 96 // does in a similar situation (updating an existing TemplateURL with |
| 97 // data from a new one). | 97 // data from a new one). |
| 98 TemplateURL* modifiable_url = const_cast<TemplateURL*>(template_url_); | 98 TemplateURL* modifiable_url = const_cast<TemplateURL*>(template_url_); |
| 99 modifiable_url->set_short_name(UTF16ToWideHack(title_input)); | 99 modifiable_url->set_short_name(title_input); |
| 100 modifiable_url->set_keyword(UTF16ToWideHack(keyword_input)); | 100 modifiable_url->set_keyword(keyword_input); |
| 101 modifiable_url->SetURL(url_string, 0, 0); | 101 modifiable_url->SetURL(url_string, 0, 0); |
| 102 // TemplateURLModel takes ownership of template_url_. | 102 // TemplateURLModel takes ownership of template_url_. |
| 103 profile_->GetTemplateURLModel()->Add(modifiable_url); | 103 profile_->GetTemplateURLModel()->Add(modifiable_url); |
| 104 UserMetrics::RecordAction(UserMetricsAction("KeywordEditor_AddKeywordJS"), | 104 UserMetrics::RecordAction(UserMetricsAction("KeywordEditor_AddKeywordJS"), |
| 105 profile_); | 105 profile_); |
| 106 } else { | 106 } else { |
| 107 // Adding or modifying an entry via the Delegate. | 107 // Adding or modifying an entry via the Delegate. |
| 108 edit_keyword_delegate_->OnEditedKeyword(template_url_, | 108 edit_keyword_delegate_->OnEditedKeyword(template_url_, |
| 109 title_input, | 109 title_input, |
| 110 keyword_input, | 110 keyword_input, |
| 111 url_string); | 111 url_string); |
| 112 } | 112 } |
| 113 } | 113 } |
| 114 | 114 |
| 115 void EditSearchEngineController::CleanUpCancelledAdd() { | 115 void EditSearchEngineController::CleanUpCancelledAdd() { |
| 116 if (!edit_keyword_delegate_ && template_url_) { | 116 if (!edit_keyword_delegate_ && template_url_) { |
| 117 // When we have no Delegate, we know that the template_url_ hasn't yet been | 117 // When we have no Delegate, we know that the template_url_ hasn't yet been |
| 118 // added to the model, so we need to clean it up. | 118 // added to the model, so we need to clean it up. |
| 119 delete template_url_; | 119 delete template_url_; |
| 120 template_url_ = NULL; | 120 template_url_ = NULL; |
| 121 } | 121 } |
| 122 } | 122 } |
| 123 | 123 |
| 124 std::string EditSearchEngineController::GetFixedUpURL( | 124 std::string EditSearchEngineController::GetFixedUpURL( |
| 125 const std::string& url_input) const { | 125 const std::string& url_input) const { |
| 126 std::string url; | 126 std::string url; |
| 127 TrimWhitespace(TemplateURLRef::DisplayURLToURLRef(UTF8ToWide(url_input)), | 127 TrimWhitespace(TemplateURLRef::DisplayURLToURLRef(UTF8ToUTF16(url_input)), |
| 128 TRIM_ALL, &url); | 128 TRIM_ALL, &url); |
| 129 if (url.empty()) | 129 if (url.empty()) |
| 130 return url; | 130 return url; |
| 131 | 131 |
| 132 // Parse the string as a URL to determine the scheme. If we need to, add the | 132 // Parse the string as a URL to determine the scheme. If we need to, add the |
| 133 // scheme. As the scheme may be expanded (as happens with {google:baseURL}) | 133 // scheme. As the scheme may be expanded (as happens with {google:baseURL}) |
| 134 // we need to replace the search terms before testing for the scheme. | 134 // we need to replace the search terms before testing for the scheme. |
| 135 TemplateURL t_url; | 135 TemplateURL t_url; |
| 136 t_url.SetURL(url, 0, 0); | 136 t_url.SetURL(url, 0, 0); |
| 137 std::string expanded_url = | 137 std::string expanded_url = |
| 138 t_url.url()->ReplaceSearchTerms(t_url, L"x", 0, std::wstring()); | 138 t_url.url()->ReplaceSearchTerms(t_url, ASCIIToUTF16("x"), 0, string16()); |
| 139 url_parse::Parsed parts; | 139 url_parse::Parsed parts; |
| 140 std::string scheme( | 140 std::string scheme( |
| 141 URLFixerUpper::SegmentURL(expanded_url, &parts)); | 141 URLFixerUpper::SegmentURL(expanded_url, &parts)); |
| 142 if (!parts.scheme.is_valid()) { | 142 if (!parts.scheme.is_valid()) { |
| 143 scheme.append("://"); | 143 scheme.append("://"); |
| 144 url.insert(0, scheme); | 144 url.insert(0, scheme); |
| 145 } | 145 } |
| 146 | 146 |
| 147 return url; | 147 return url; |
| 148 } | 148 } |
| 149 | 149 |
| OLD | NEW |