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 1997 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
2008 | 2008 |
2009 if (newly_adding) { | 2009 if (newly_adding) { |
2010 DCHECK_EQ(kInvalidTemplateURLID, template_url->id()); | 2010 DCHECK_EQ(kInvalidTemplateURLID, template_url->id()); |
2011 DCHECK(!Contains(&template_urls_, template_url.get())); | 2011 DCHECK(!Contains(&template_urls_, template_url.get())); |
2012 template_url->data_.id = ++next_id_; | 2012 template_url->data_.id = ++next_id_; |
2013 } | 2013 } |
2014 | 2014 |
2015 template_url->ResetKeywordIfNecessary(search_terms_data(), false); | 2015 template_url->ResetKeywordIfNecessary(search_terms_data(), false); |
2016 // Check whether |template_url|'s keyword conflicts with any already in the | 2016 // Check whether |template_url|'s keyword conflicts with any already in the |
2017 // model. | 2017 // model. |
2018 TemplateURL* existing_keyword_turl = | 2018 TemplateURL* existing_turl = |
2019 GetTemplateURLForKeyword(template_url->keyword()); | 2019 GetTemplateURLForKeyword(template_url->keyword()); |
2020 | 2020 |
2021 // Check whether |template_url|'s keyword conflicts with any already in the | 2021 // Check whether |template_url|'s keyword conflicts with any already in the |
2022 // model. Note that we can reach here during the loading phase while | 2022 // model. Note that we can reach here during the loading phase while |
2023 // processing the template URLs from the web data service. In this case, | 2023 // processing the template URLs from the web data service. In this case, |
2024 // GetTemplateURLForKeyword() will look not only at what's already in the | 2024 // GetTemplateURLForKeyword() will look not only at what's already in the |
2025 // model, but at the |initial_default_search_provider_|. Since this engine | 2025 // model, but at the |initial_default_search_provider_|. Since this engine |
2026 // will presumably also be present in the web data, we need to double-check | 2026 // will presumably also be present in the web data, we need to double-check |
2027 // that any "pre-existing" entries we find are actually coming from | 2027 // that any "pre-existing" entries we find are actually coming from |
2028 // |template_urls_|, lest we detect a "conflict" between the | 2028 // |template_urls_|, lest we detect a "conflict" between the |
2029 // |initial_default_search_provider_| and the web data version of itself. | 2029 // |initial_default_search_provider_| and the web data version of itself. |
2030 if (template_url->type() != TemplateURL::OMNIBOX_API_EXTENSION && | 2030 if (template_url->type() != TemplateURL::OMNIBOX_API_EXTENSION && |
2031 existing_keyword_turl && | 2031 existing_turl && |
2032 existing_keyword_turl->type() != TemplateURL::OMNIBOX_API_EXTENSION && | 2032 existing_turl->type() != TemplateURL::OMNIBOX_API_EXTENSION && |
2033 Contains(&template_urls_, existing_keyword_turl)) { | 2033 Contains(&template_urls_, existing_turl)) { |
2034 DCHECK_NE(existing_keyword_turl, template_url.get()); | 2034 DCHECK_NE(existing_turl, template_url.get()); |
2035 // Only replace one of the TemplateURLs if they are either both extensions, | 2035 // Only replace one of the TemplateURLs if they are either both extensions, |
2036 // or both not extensions. | 2036 // or both not extensions. |
2037 bool are_same_type = IsCreatedByExtension(existing_keyword_turl) == | 2037 bool are_same_type = IsCreatedByExtension(existing_turl) == |
2038 IsCreatedByExtension(template_url.get()); | 2038 IsCreatedByExtension(template_url.get()); |
2039 if (CanReplace(existing_keyword_turl) && are_same_type) { | 2039 if (CanReplace(existing_turl) && are_same_type) { |
2040 RemoveNoNotify(existing_keyword_turl); | 2040 RemoveNoNotify(existing_turl); |
2041 } else if (CanReplace(template_url.get()) && are_same_type) { | 2041 } else if (CanReplace(template_url.get()) && are_same_type) { |
2042 return nullptr; | 2042 return nullptr; |
2043 } else { | 2043 } else { |
2044 base::string16 new_keyword = | 2044 // Resolve keyword conflicts for engines that can not be replaced. |
2045 UniquifyKeyword(*existing_keyword_turl, false); | 2045 // Use following priority while resolving keyword conflicts: |
2046 ResetTemplateURLNoNotify(existing_keyword_turl, | 2046 // extension default search -> extension search -> normal engines. |
2047 existing_keyword_turl->short_name(), new_keyword, | 2047 // For engines of the same priority, the newly-added one wins. |
2048 existing_keyword_turl->url()); | 2048 // By extension we mean NORMAL_CONTROLLED_BY_EXTENSION type of engine, not |
2049 // the OMNIBOX_API_EXTENSION. | |
vasilii
2017/02/07 16:27:21
I'm confused by the algorithm because I thought we
Alexander Yashkin
2017/02/07 16:50:11
I have understood differently, especially Peter's
| |
2050 base::string16 new_keyword = UniquifyKeyword(*existing_turl, false); | |
2051 if (IsCreatedByExtension(existing_turl) && | |
2052 (!IsCreatedByExtension(template_url.get()) || | |
2053 (existing_turl->extension_info_->wants_to_be_default_engine && | |
2054 !template_url->extension_info_->wants_to_be_default_engine))) { | |
2055 // Replace keyword for added search engine. | |
2056 template_url->data_.SetKeyword(new_keyword); | |
2057 } else { | |
2058 // Replace keyword for existing engine. | |
2059 ResetTemplateURLNoNotify(existing_turl, existing_turl->short_name(), | |
2060 new_keyword, existing_turl->url()); | |
2061 } | |
2049 } | 2062 } |
2050 } | 2063 } |
2051 TemplateURL* template_url_ptr = template_url.get(); | 2064 TemplateURL* template_url_ptr = template_url.get(); |
2052 template_urls_.push_back(std::move(template_url)); | 2065 template_urls_.push_back(std::move(template_url)); |
2053 AddToMaps(template_url_ptr); | 2066 AddToMaps(template_url_ptr); |
2054 | 2067 |
2055 if (newly_adding && (template_url_ptr->type() == TemplateURL::NORMAL)) { | 2068 if (newly_adding && (template_url_ptr->type() == TemplateURL::NORMAL)) { |
2056 if (web_data_service_.get()) | 2069 if (web_data_service_.get()) |
2057 web_data_service_->AddKeyword(template_url_ptr->data()); | 2070 web_data_service_->AddKeyword(template_url_ptr->data()); |
2058 | 2071 |
(...skipping 418 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
2477 | 2490 |
2478 if (most_recently_intalled_default) { | 2491 if (most_recently_intalled_default) { |
2479 base::AutoReset<DefaultSearchChangeOrigin> change_origin( | 2492 base::AutoReset<DefaultSearchChangeOrigin> change_origin( |
2480 &dsp_change_origin_, DSP_CHANGE_OVERRIDE_SETTINGS_EXTENSION); | 2493 &dsp_change_origin_, DSP_CHANGE_OVERRIDE_SETTINGS_EXTENSION); |
2481 default_search_manager_.SetExtensionControlledDefaultSearchEngine( | 2494 default_search_manager_.SetExtensionControlledDefaultSearchEngine( |
2482 most_recently_intalled_default->data()); | 2495 most_recently_intalled_default->data()); |
2483 } else { | 2496 } else { |
2484 default_search_manager_.ClearExtensionControlledDefaultSearchEngine(); | 2497 default_search_manager_.ClearExtensionControlledDefaultSearchEngine(); |
2485 } | 2498 } |
2486 } | 2499 } |
OLD | NEW |