| OLD | NEW |
| 1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2006-2008 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/template_url_model.h" | 5 #include "chrome/browser/template_url_model.h" |
| 6 | 6 |
| 7 #include <algorithm> | 7 #include <algorithm> |
| 8 | 8 |
| 9 #include "base/logging.h" | 9 #include "base/logging.h" |
| 10 #include "base/string_util.h" | 10 #include "base/string_util.h" |
| (...skipping 14 matching lines...) Expand all Loading... |
| 25 #include "net/base/net_util.h" | 25 #include "net/base/net_util.h" |
| 26 #include "unicode/rbbi.h" | 26 #include "unicode/rbbi.h" |
| 27 #include "unicode/uchar.h" | 27 #include "unicode/uchar.h" |
| 28 | 28 |
| 29 using base::Time; | 29 using base::Time; |
| 30 | 30 |
| 31 // String in the URL that is replaced by the search term. | 31 // String in the URL that is replaced by the search term. |
| 32 static const wchar_t kSearchTermParameter[] = L"{searchTerms}"; | 32 static const wchar_t kSearchTermParameter[] = L"{searchTerms}"; |
| 33 | 33 |
| 34 // String in Initializer that is replaced with kSearchTermParameter. | 34 // String in Initializer that is replaced with kSearchTermParameter. |
| 35 static const wchar_t kTemplateParameter[](L"%s"); | 35 static const wchar_t kTemplateParameter[] = L"%s"; |
| 36 | 36 |
| 37 // Term used when generating a search url. Use something obscure so that on | 37 // Term used when generating a search url. Use something obscure so that on |
| 38 // the rare case the term replaces the URL it's unlikely another keyword would | 38 // the rare case the term replaces the URL it's unlikely another keyword would |
| 39 // have the same url. | 39 // have the same url. |
| 40 static const wchar_t kReplacementTerm[] = L"blah.blah.blah.blah.blah"; | 40 static const wchar_t kReplacementTerm[] = L"blah.blah.blah.blah.blah"; |
| 41 | 41 |
| 42 class TemplateURLModel::LessWithPrefix { | 42 class TemplateURLModel::LessWithPrefix { |
| 43 public: | 43 public: |
| 44 // We want to find the set of keywords that begin with a prefix. The STL | 44 // We want to find the set of keywords that begin with a prefix. The STL |
| 45 // algorithms will return the set of elements that are "equal to" the | 45 // algorithms will return the set of elements that are "equal to" the |
| (...skipping 120 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 166 // properly. See http://b/issue?id=1205573. | 166 // properly. See http://b/issue?id=1205573. |
| 167 return net::StripWWW(UTF8ToWide(url.host())); | 167 return net::StripWWW(UTF8ToWide(url.host())); |
| 168 } | 168 } |
| 169 | 169 |
| 170 // static | 170 // static |
| 171 std::wstring TemplateURLModel::CleanUserInputKeyword( | 171 std::wstring TemplateURLModel::CleanUserInputKeyword( |
| 172 const std::wstring& keyword) { | 172 const std::wstring& keyword) { |
| 173 // Remove the scheme. | 173 // Remove the scheme. |
| 174 std::wstring result(l10n_util::ToLower(keyword)); | 174 std::wstring result(l10n_util::ToLower(keyword)); |
| 175 url_parse::Component scheme_component; | 175 url_parse::Component scheme_component; |
| 176 if (url_parse::ExtractScheme(keyword.c_str(), | 176 if (url_parse::ExtractScheme(WideToUTF8(keyword).c_str(), |
| 177 static_cast<int>(keyword.length()), | 177 static_cast<int>(keyword.length()), |
| 178 &scheme_component)) { | 178 &scheme_component)) { |
| 179 // Include trailing ':'. | 179 // Include trailing ':'. |
| 180 result.erase(0, scheme_component.end() + 1); | 180 result.erase(0, scheme_component.end() + 1); |
| 181 // Many schemes usually have "//" after them, so strip it too. | 181 // Many schemes usually have "//" after them, so strip it too. |
| 182 const std::wstring after_scheme(L"//"); | 182 const std::wstring after_scheme(L"//"); |
| 183 if (result.compare(0, after_scheme.length(), after_scheme) == 0) | 183 if (result.compare(0, after_scheme.length(), after_scheme) == 0) |
| 184 result.erase(0, after_scheme.length()); | 184 result.erase(0, after_scheme.length()); |
| 185 } | 185 } |
| 186 | 186 |
| 187 // Remove leading "www.". | 187 // Remove leading "www.". |
| 188 result = net::StripWWW(result); | 188 result = net::StripWWW(result); |
| 189 | 189 |
| 190 // Remove trailing "/". | 190 // Remove trailing "/". |
| 191 return (result.length() > 0 && result[result.length() - 1] == L'/') ? | 191 return (result.length() > 0 && result[result.length() - 1] == L'/') ? |
| 192 result.substr(0, result.length() - 1) : result; | 192 result.substr(0, result.length() - 1) : result; |
| 193 } | 193 } |
| 194 | 194 |
| 195 // static | 195 // static |
| 196 GURL TemplateURLModel::GenerateSearchURL(const TemplateURL* t_url) { | 196 GURL TemplateURLModel::GenerateSearchURL(const TemplateURL* t_url) { |
| 197 DCHECK(t_url); | 197 DCHECK(t_url); |
| 198 const TemplateURLRef* search_ref = t_url->url(); | 198 const TemplateURLRef* search_ref = t_url->url(); |
| 199 if (!search_ref || !search_ref->IsValid()) | 199 if (!search_ref || !search_ref->IsValid()) |
| 200 return GURL(); | 200 return GURL(); |
| 201 | 201 |
| 202 if (!search_ref->SupportsReplacement()) | 202 if (!search_ref->SupportsReplacement()) |
| 203 return GURL(search_ref->url()); | 203 return GURL(WideToUTF8(search_ref->url())); |
| 204 | 204 |
| 205 return GURL(search_ref->ReplaceSearchTerms(*t_url, kReplacementTerm, | 205 return search_ref->ReplaceSearchTerms( |
| 206 TemplateURLRef::NO_SUGGESTIONS_AVAILABLE, std::wstring())); | 206 *t_url, |
| 207 kReplacementTerm, |
| 208 TemplateURLRef::NO_SUGGESTIONS_AVAILABLE, std::wstring()); |
| 207 } | 209 } |
| 208 | 210 |
| 209 bool TemplateURLModel::CanReplaceKeyword( | 211 bool TemplateURLModel::CanReplaceKeyword( |
| 210 const std::wstring& keyword, | 212 const std::wstring& keyword, |
| 211 const std::wstring& url, | 213 const std::wstring& url, |
| 212 const TemplateURL** template_url_to_replace) { | 214 const TemplateURL** template_url_to_replace) { |
| 213 DCHECK(!keyword.empty()); // This should only be called for non-empty | 215 DCHECK(!keyword.empty()); // This should only be called for non-empty |
| 214 // keywords. If we need to support empty kewords | 216 // keywords. If we need to support empty kewords |
| 215 // the code needs to change slightly. | 217 // the code needs to change slightly. |
| 216 const TemplateURL* existing_url = GetTemplateURLForKeyword(keyword); | 218 const TemplateURL* existing_url = GetTemplateURLForKeyword(keyword); |
| 217 if (existing_url) { | 219 if (existing_url) { |
| 218 // We already have a TemplateURL for this keyword. Only allow it to be | 220 // We already have a TemplateURL for this keyword. Only allow it to be |
| 219 // replaced if the TemplateURL can be replaced. | 221 // replaced if the TemplateURL can be replaced. |
| 220 if (template_url_to_replace) | 222 if (template_url_to_replace) |
| 221 *template_url_to_replace = existing_url; | 223 *template_url_to_replace = existing_url; |
| 222 return CanReplace(existing_url); | 224 return CanReplace(existing_url); |
| 223 } | 225 } |
| 224 | 226 |
| 225 // We don't have a TemplateURL with keyword. Only allow a new one if there | 227 // We don't have a TemplateURL with keyword. Only allow a new one if there |
| 226 // isn't a TemplateURL for the specified host, or there is one but it can | 228 // isn't a TemplateURL for the specified host, or there is one but it can |
| 227 // be replaced. We do this to ensure that if the user assigns a different | 229 // be replaced. We do this to ensure that if the user assigns a different |
| 228 // keyword to a generated TemplateURL, we won't regenerate another keyword for | 230 // keyword to a generated TemplateURL, we won't regenerate another keyword for |
| 229 // the same host. | 231 // the same host. |
| 230 GURL gurl(url); | 232 GURL gurl(WideToUTF8(url)); |
| 231 if (gurl.is_valid() && !gurl.host().empty()) | 233 if (gurl.is_valid() && !gurl.host().empty()) |
| 232 return CanReplaceKeywordForHost(gurl.host(), template_url_to_replace); | 234 return CanReplaceKeywordForHost(gurl.host(), template_url_to_replace); |
| 233 return true; | 235 return true; |
| 234 } | 236 } |
| 235 | 237 |
| 236 void TemplateURLModel::FindMatchingKeywords( | 238 void TemplateURLModel::FindMatchingKeywords( |
| 237 const std::wstring& prefix, | 239 const std::wstring& prefix, |
| 238 bool support_replacement_only, | 240 bool support_replacement_only, |
| 239 std::vector<std::wstring>* matches) const { | 241 std::vector<std::wstring>* matches) const { |
| 240 // Sanity check args. | 242 // Sanity check args. |
| (...skipping 727 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 968 AddToMaps(t_url); | 970 AddToMaps(t_url); |
| 969 something_changed = true; | 971 something_changed = true; |
| 970 } | 972 } |
| 971 } | 973 } |
| 972 | 974 |
| 973 if (something_changed && loaded_) { | 975 if (something_changed && loaded_) { |
| 974 FOR_EACH_OBSERVER(TemplateURLModelObserver, model_observers_, | 976 FOR_EACH_OBSERVER(TemplateURLModelObserver, model_observers_, |
| 975 OnTemplateURLModelChanged()); | 977 OnTemplateURLModelChanged()); |
| 976 } | 978 } |
| 977 } | 979 } |
| OLD | NEW |