| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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/ui/search_engines/edit_search_engine_controller.h" | 5 #include "chrome/browser/ui/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/net/url_fixer_upper.h" | 9 #include "chrome/browser/net/url_fixer_upper.h" |
| 10 #include "chrome/browser/profiles/profile.h" | 10 #include "chrome/browser/profiles/profile.h" |
| (...skipping 23 matching lines...) Expand all Loading... |
| 34 bool EditSearchEngineController::IsURLValid( | 34 bool EditSearchEngineController::IsURLValid( |
| 35 const std::string& url_input) const { | 35 const std::string& url_input) const { |
| 36 std::string url = GetFixedUpURL(url_input); | 36 std::string url = GetFixedUpURL(url_input); |
| 37 if (url.empty()) | 37 if (url.empty()) |
| 38 return false; | 38 return false; |
| 39 | 39 |
| 40 // Convert |url| to a TemplateURLRef so we can check its validity even if it | 40 // Convert |url| to a TemplateURLRef so we can check its validity even if it |
| 41 // contains replacement strings. We do this by constructing a dummy | 41 // contains replacement strings. We do this by constructing a dummy |
| 42 // TemplateURL owner because |template_url_| might be NULL and we can't call | 42 // TemplateURL owner because |template_url_| might be NULL and we can't call |
| 43 // TemplateURLRef::IsValid() when its owner is NULL. | 43 // TemplateURLRef::IsValid() when its owner is NULL. |
| 44 TemplateURL t_url; | 44 TemplateURLData data; |
| 45 t_url.SetURL(url); | 45 data.SetURL(url); |
| 46 TemplateURL t_url(data); |
| 46 const TemplateURLRef& template_ref = t_url.url_ref(); | 47 const TemplateURLRef& template_ref = t_url.url_ref(); |
| 47 if (!template_ref.IsValid()) | 48 if (!template_ref.IsValid()) |
| 48 return false; | 49 return false; |
| 49 | 50 |
| 50 // If this is going to be the default search engine, it must support | 51 // If this is going to be the default search engine, it must support |
| 51 // replacement. | 52 // replacement. |
| 52 if (!template_ref.SupportsReplacement() && | 53 if (!template_ref.SupportsReplacement() && |
| 53 (template_url_ == TemplateURLServiceFactory::GetForProfile(profile_)-> | 54 (template_url_ == TemplateURLServiceFactory::GetForProfile(profile_)-> |
| 54 GetDefaultSearchProvider())) | 55 GetDefaultSearchProvider())) |
| 55 return false; | 56 return false; |
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 90 // TODO(pamg): Really, we should modify the entry so this later one | 91 // TODO(pamg): Really, we should modify the entry so this later one |
| 91 // overwrites it. But we don't expect this case to be common. | 92 // overwrites it. But we don't expect this case to be common. |
| 92 CleanUpCancelledAdd(); | 93 CleanUpCancelledAdd(); |
| 93 return; | 94 return; |
| 94 } | 95 } |
| 95 | 96 |
| 96 if (!edit_keyword_delegate_) { | 97 if (!edit_keyword_delegate_) { |
| 97 // Confiming an entry we got from JS. We have a template_url_, but it | 98 // Confiming an entry we got from JS. We have a template_url_, but it |
| 98 // hasn't yet been added to the model. | 99 // hasn't yet been added to the model. |
| 99 DCHECK(template_url_); | 100 DCHECK(template_url_); |
| 100 // const_cast is ugly, but this is the same thing the TemplateURLService | |
| 101 // does in a similar situation (updating an existing TemplateURL with | |
| 102 // data from a new one). | |
| 103 TemplateURL* modifiable_url = const_cast<TemplateURL*>(template_url_); | |
| 104 modifiable_url->set_short_name(title_input); | |
| 105 modifiable_url->set_keyword(keyword_input); | |
| 106 modifiable_url->SetURL(url_string); | |
| 107 // TemplateURLService takes ownership of template_url_. | 101 // TemplateURLService takes ownership of template_url_. |
| 108 template_url_service->Add(modifiable_url); | 102 template_url_service->AddWithOverrides(template_url_, title_input, |
| 103 keyword_input, url_string); |
| 109 content::RecordAction(UserMetricsAction("KeywordEditor_AddKeywordJS")); | 104 content::RecordAction(UserMetricsAction("KeywordEditor_AddKeywordJS")); |
| 110 } else { | 105 } else { |
| 111 // Adding or modifying an entry via the Delegate. | 106 // Adding or modifying an entry via the Delegate. |
| 112 edit_keyword_delegate_->OnEditedKeyword(template_url_, title_input, | 107 edit_keyword_delegate_->OnEditedKeyword(template_url_, title_input, |
| 113 keyword_input, url_string); | 108 keyword_input, url_string); |
| 114 } | 109 } |
| 115 } | 110 } |
| 116 | 111 |
| 117 void EditSearchEngineController::CleanUpCancelledAdd() { | 112 void EditSearchEngineController::CleanUpCancelledAdd() { |
| 118 if (!edit_keyword_delegate_ && template_url_) { | 113 if (!edit_keyword_delegate_ && template_url_) { |
| 119 // When we have no Delegate, we know that the template_url_ hasn't yet been | 114 // When we have no Delegate, we know that the template_url_ hasn't yet been |
| 120 // added to the model, so we need to clean it up. | 115 // added to the model, so we need to clean it up. |
| 121 delete template_url_; | 116 delete template_url_; |
| 122 template_url_ = NULL; | 117 template_url_ = NULL; |
| 123 } | 118 } |
| 124 } | 119 } |
| 125 | 120 |
| 126 std::string EditSearchEngineController::GetFixedUpURL( | 121 std::string EditSearchEngineController::GetFixedUpURL( |
| 127 const std::string& url_input) const { | 122 const std::string& url_input) const { |
| 128 std::string url; | 123 std::string url; |
| 129 TrimWhitespace(TemplateURLRef::DisplayURLToURLRef(UTF8ToUTF16(url_input)), | 124 TrimWhitespace(TemplateURLRef::DisplayURLToURLRef(UTF8ToUTF16(url_input)), |
| 130 TRIM_ALL, &url); | 125 TRIM_ALL, &url); |
| 131 if (url.empty()) | 126 if (url.empty()) |
| 132 return url; | 127 return url; |
| 133 | 128 |
| 134 // Parse the string as a URL to determine the scheme. If we need to, add the | 129 // Parse the string as a URL to determine the scheme. If we need to, add the |
| 135 // scheme. As the scheme may be expanded (as happens with {google:baseURL}) | 130 // scheme. As the scheme may be expanded (as happens with {google:baseURL}) |
| 136 // we need to replace the search terms before testing for the scheme. | 131 // we need to replace the search terms before testing for the scheme. |
| 137 TemplateURL t_url; | 132 TemplateURLData data; |
| 138 t_url.SetURL(url); | 133 data.SetURL(url); |
| 134 TemplateURL t_url(data); |
| 139 std::string expanded_url(t_url.url_ref().ReplaceSearchTerms(ASCIIToUTF16("x"), | 135 std::string expanded_url(t_url.url_ref().ReplaceSearchTerms(ASCIIToUTF16("x"), |
| 140 TemplateURLRef::NO_SUGGESTIONS_AVAILABLE, string16())); | 136 TemplateURLRef::NO_SUGGESTIONS_AVAILABLE, string16())); |
| 141 url_parse::Parsed parts; | 137 url_parse::Parsed parts; |
| 142 std::string scheme(URLFixerUpper::SegmentURL(expanded_url, &parts)); | 138 std::string scheme(URLFixerUpper::SegmentURL(expanded_url, &parts)); |
| 143 if (!parts.scheme.is_valid()) | 139 if (!parts.scheme.is_valid()) |
| 144 url.insert(0, scheme + "://"); | 140 url.insert(0, scheme + "://"); |
| 145 | 141 |
| 146 return url; | 142 return url; |
| 147 } | 143 } |
| 148 | 144 |
| OLD | NEW |