OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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 "components/search_engines/template_url_service.h" | 5 #include "components/search_engines/template_url_service.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 #include <utility> | 8 #include <utility> |
9 | 9 |
10 #include "base/auto_reset.h" | 10 #include "base/auto_reset.h" |
(...skipping 171 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
182 // The meaningful keyword length is the length of any portion before the | 182 // The meaningful keyword length is the length of any portion before the |
183 // registry ("co.uk") and its preceding dot. | 183 // registry ("co.uk") and its preceding dot. |
184 return keyword.length() - (registry_length ? (registry_length + 1) : 0); | 184 return keyword.length() - (registry_length ? (registry_length + 1) : 0); |
185 } | 185 } |
186 | 186 |
187 bool Contains(TemplateURLService::OwnedTemplateURLVector* template_urls, | 187 bool Contains(TemplateURLService::OwnedTemplateURLVector* template_urls, |
188 TemplateURL* turl) { | 188 TemplateURL* turl) { |
189 return FindTemplateURL(template_urls, turl) != template_urls->end(); | 189 return FindTemplateURL(template_urls, turl) != template_urls->end(); |
190 } | 190 } |
191 | 191 |
| 192 bool IsCreatedByExtension(TemplateURL* template_url) { |
| 193 return template_url->GetType() == |
| 194 TemplateURL::NORMAL_CONTROLLED_BY_EXTENSION || |
| 195 template_url->GetType() == TemplateURL::OMNIBOX_API_EXTENSION; |
| 196 } |
| 197 |
192 } // namespace | 198 } // namespace |
193 | 199 |
194 // TemplateURLService::LessWithPrefix ----------------------------------------- | 200 // TemplateURLService::LessWithPrefix ----------------------------------------- |
195 | 201 |
196 class TemplateURLService::LessWithPrefix { | 202 class TemplateURLService::LessWithPrefix { |
197 public: | 203 public: |
198 // We want to find the set of keywords that begin with a prefix. The STL | 204 // We want to find the set of keywords that begin with a prefix. The STL |
199 // algorithms will return the set of elements that are "equal to" the | 205 // algorithms will return the set of elements that are "equal to" the |
200 // prefix, where "equal(x, y)" means "!(cmp(x, y) || cmp(y, x))". When | 206 // prefix, where "equal(x, y)" means "!(cmp(x, y) || cmp(y, x))". When |
201 // cmp() is the typical std::less<>, this results in lexicographic equality; | 207 // cmp() is the typical std::less<>, this results in lexicographic equality; |
(...skipping 401 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
603 (url != GetDefaultSearchProvider()) && | 609 (url != GetDefaultSearchProvider()) && |
604 url->url_ref().SupportsReplacement(search_terms_data()) && | 610 url->url_ref().SupportsReplacement(search_terms_data()) && |
605 (url->GetType() == TemplateURL::NORMAL); | 611 (url->GetType() == TemplateURL::NORMAL); |
606 } | 612 } |
607 | 613 |
608 void TemplateURLService::SetUserSelectedDefaultSearchProvider( | 614 void TemplateURLService::SetUserSelectedDefaultSearchProvider( |
609 TemplateURL* url) { | 615 TemplateURL* url) { |
610 // Omnibox keywords cannot be made default. Extension-controlled search | 616 // Omnibox keywords cannot be made default. Extension-controlled search |
611 // engines can be made default only by the extension itself because they | 617 // engines can be made default only by the extension itself because they |
612 // aren't persisted. | 618 // aren't persisted. |
613 DCHECK(!url || (url->GetType() == TemplateURL::NORMAL)); | 619 DCHECK(!url || (!IsCreatedByExtension(url))); |
614 if (load_failed_) { | 620 if (load_failed_) { |
615 // Skip the DefaultSearchManager, which will persist to user preferences. | 621 // Skip the DefaultSearchManager, which will persist to user preferences. |
616 if ((default_search_provider_source_ == DefaultSearchManager::FROM_USER) || | 622 if ((default_search_provider_source_ == DefaultSearchManager::FROM_USER) || |
617 (default_search_provider_source_ == | 623 (default_search_provider_source_ == |
618 DefaultSearchManager::FROM_FALLBACK)) { | 624 DefaultSearchManager::FROM_FALLBACK)) { |
619 ApplyDefaultSearchChange(url ? &url->data() : nullptr, | 625 ApplyDefaultSearchChange(url ? &url->data() : nullptr, |
620 DefaultSearchManager::FROM_USER); | 626 DefaultSearchManager::FROM_USER); |
621 } | 627 } |
622 } else { | 628 } else { |
623 // We rely on the DefaultSearchManager to call OnDefaultSearchChange if, in | 629 // We rely on the DefaultSearchManager to call OnDefaultSearchChange if, in |
(...skipping 1390 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2014 // that any "pre-existing" entries we find are actually coming from | 2020 // that any "pre-existing" entries we find are actually coming from |
2015 // |template_urls_|, lest we detect a "conflict" between the | 2021 // |template_urls_|, lest we detect a "conflict" between the |
2016 // |initial_default_search_provider_| and the web data version of itself. | 2022 // |initial_default_search_provider_| and the web data version of itself. |
2017 if (template_url->GetType() != TemplateURL::OMNIBOX_API_EXTENSION && | 2023 if (template_url->GetType() != TemplateURL::OMNIBOX_API_EXTENSION && |
2018 existing_keyword_turl && | 2024 existing_keyword_turl && |
2019 existing_keyword_turl->GetType() != TemplateURL::OMNIBOX_API_EXTENSION && | 2025 existing_keyword_turl->GetType() != TemplateURL::OMNIBOX_API_EXTENSION && |
2020 Contains(&template_urls_, existing_keyword_turl)) { | 2026 Contains(&template_urls_, existing_keyword_turl)) { |
2021 DCHECK_NE(existing_keyword_turl, template_url.get()); | 2027 DCHECK_NE(existing_keyword_turl, template_url.get()); |
2022 // Only replace one of the TemplateURLs if they are either both extensions, | 2028 // Only replace one of the TemplateURLs if they are either both extensions, |
2023 // or both not extensions. | 2029 // or both not extensions. |
2024 bool are_same_type = existing_keyword_turl->GetType() == | 2030 bool are_same_type = IsCreatedByExtension(existing_keyword_turl) == |
2025 template_url->GetType(); | 2031 IsCreatedByExtension(template_url.get()); |
2026 if (CanReplace(existing_keyword_turl) && are_same_type) { | 2032 if (CanReplace(existing_keyword_turl) && are_same_type) { |
2027 RemoveNoNotify(existing_keyword_turl); | 2033 RemoveNoNotify(existing_keyword_turl); |
2028 } else if (CanReplace(template_url.get()) && are_same_type) { | 2034 } else if (CanReplace(template_url.get()) && are_same_type) { |
2029 return nullptr; | 2035 return nullptr; |
2030 } else { | 2036 } else { |
2031 base::string16 new_keyword = | 2037 base::string16 new_keyword = |
2032 UniquifyKeyword(*existing_keyword_turl, false); | 2038 UniquifyKeyword(*existing_keyword_turl, false); |
2033 ResetTemplateURLNoNotify(existing_keyword_turl, | 2039 ResetTemplateURLNoNotify(existing_keyword_turl, |
2034 existing_keyword_turl->short_name(), new_keyword, | 2040 existing_keyword_turl->short_name(), new_keyword, |
2035 existing_keyword_turl->url()); | 2041 existing_keyword_turl->url()); |
(...skipping 429 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2465 | 2471 |
2466 if (most_recently_intalled_default) { | 2472 if (most_recently_intalled_default) { |
2467 base::AutoReset<DefaultSearchChangeOrigin> change_origin( | 2473 base::AutoReset<DefaultSearchChangeOrigin> change_origin( |
2468 &dsp_change_origin_, DSP_CHANGE_OVERRIDE_SETTINGS_EXTENSION); | 2474 &dsp_change_origin_, DSP_CHANGE_OVERRIDE_SETTINGS_EXTENSION); |
2469 default_search_manager_.SetExtensionControlledDefaultSearchEngine( | 2475 default_search_manager_.SetExtensionControlledDefaultSearchEngine( |
2470 most_recently_intalled_default->data()); | 2476 most_recently_intalled_default->data()); |
2471 } else { | 2477 } else { |
2472 default_search_manager_.ClearExtensionControlledDefaultSearchEngine(); | 2478 default_search_manager_.ClearExtensionControlledDefaultSearchEngine(); |
2473 } | 2479 } |
2474 } | 2480 } |
OLD | NEW |