Chromium Code Reviews| 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/search_engines/template_url_service.h" | 5 #include "chrome/browser/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 857 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 868 ++i) { | 868 ++i) { |
| 869 AddNoNotify(new TemplateURL(profile_, *i), true); | 869 AddNoNotify(new TemplateURL(profile_, *i), true); |
| 870 } | 870 } |
| 871 | 871 |
| 872 base::AutoReset<DefaultSearchChangeOrigin> change_origin( | 872 base::AutoReset<DefaultSearchChangeOrigin> change_origin( |
| 873 &dsp_change_origin_, DSP_CHANGE_PROFILE_RESET); | 873 &dsp_change_origin_, DSP_CHANGE_PROFILE_RESET); |
| 874 | 874 |
| 875 default_search_manager_.ClearUserSelectedDefaultSearchEngine(); | 875 default_search_manager_.ClearUserSelectedDefaultSearchEngine(); |
| 876 | 876 |
| 877 if (!default_search_provider_) { | 877 if (!default_search_provider_) { |
| 878 // If the default search provider came from a user pref we would have been | |
| 879 // notified of the new (fallback-provided) value in | |
| 880 // ClearUserSelectedDefaultSearchEngine() above. Since we are here, the | |
| 881 // value was presumably originally a fallback value (which may have been | |
| 882 // repaired). | |
| 878 DefaultSearchManager::Source source; | 883 DefaultSearchManager::Source source; |
| 879 const TemplateURLData* new_dse = | 884 const TemplateURLData* new_dse = |
| 880 default_search_manager_.GetDefaultSearchEngine(&source); | 885 default_search_manager_.GetDefaultSearchEngine(&source); |
| 881 // ApplyDefaultSearchChange will notify observers once it is done. | 886 // ApplyDefaultSearchChange will notify observers once it is done. |
| 882 ApplyDefaultSearchChange(new_dse, source); | 887 ApplyDefaultSearchChange(new_dse, source); |
| 883 } else { | 888 } else { |
| 884 NotifyObservers(); | 889 NotifyObservers(); |
| 885 } | 890 } |
| 886 } | 891 } |
| 887 | 892 |
| (...skipping 775 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1663 urls->clear(); | 1668 urls->clear(); |
| 1664 } | 1669 } |
| 1665 | 1670 |
| 1666 void TemplateURLService::ChangeToLoadedState() { | 1671 void TemplateURLService::ChangeToLoadedState() { |
| 1667 DCHECK(!loaded_); | 1672 DCHECK(!loaded_); |
| 1668 | 1673 |
| 1669 UIThreadSearchTermsData search_terms_data(profile_); | 1674 UIThreadSearchTermsData search_terms_data(profile_); |
| 1670 provider_map_->Init(template_urls_, search_terms_data); | 1675 provider_map_->Init(template_urls_, search_terms_data); |
| 1671 loaded_ = true; | 1676 loaded_ = true; |
| 1672 | 1677 |
| 1673 // This will cause a call to NotifyObservers(). | 1678 { |
| 1674 ApplyDefaultSearchChange(initial_default_search_provider_ ? | 1679 base::AutoReset<DefaultSearchChangeOrigin> change_origin( |
| 1675 &initial_default_search_provider_->data() : NULL, | 1680 &dsp_change_origin_, DSP_CHANGE_INITIAL_LOAD); |
|
Peter Kasting
2014/05/21 19:38:41
Can't we just move the |loaded_| assignment statem
erikwright (departed)
2014/05/21 20:38:48
ApplyDefaultSearchChange behaves differently befor
Peter Kasting
2014/05/21 20:45:13
My primary concern is really that you're adding a
erikwright (departed)
2014/05/21 21:18:01
I agree with deprecating the old values (in a sepa
| |
| 1676 default_search_provider_source_); | 1681 // This will cause a call to NotifyObservers(). |
| 1682 ApplyDefaultSearchChange( | |
| 1683 initial_default_search_provider_ ? | |
| 1684 &initial_default_search_provider_->data() : NULL, | |
| 1685 default_search_provider_source_); | |
| 1686 } | |
| 1687 | |
| 1677 initial_default_search_provider_.reset(); | 1688 initial_default_search_provider_.reset(); |
| 1678 on_loaded_callbacks_.Notify(); | 1689 on_loaded_callbacks_.Notify(); |
| 1679 } | 1690 } |
| 1680 | 1691 |
| 1681 bool TemplateURLService::CanReplaceKeywordForHost( | 1692 bool TemplateURLService::CanReplaceKeywordForHost( |
| 1682 const std::string& host, | 1693 const std::string& host, |
| 1683 TemplateURL** to_replace) { | 1694 TemplateURL** to_replace) { |
| 1684 DCHECK(!to_replace || !*to_replace); | 1695 DCHECK(!to_replace || !*to_replace); |
| 1685 const TemplateURLSet* urls = provider_map_->GetURLsForHost(host); | 1696 const TemplateURLSet* urls = provider_map_->GetURLsForHost(host); |
| 1686 if (!urls) | 1697 if (!urls) |
| (...skipping 260 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1947 } | 1958 } |
| 1948 | 1959 |
| 1949 // Prevent recursion if we update the value stored in default_search_manager_. | 1960 // Prevent recursion if we update the value stored in default_search_manager_. |
| 1950 // Note that we exclude the case of data == NULL because that could cause a | 1961 // Note that we exclude the case of data == NULL because that could cause a |
| 1951 // false positive for recursion when the initial_default_search_provider_ is | 1962 // false positive for recursion when the initial_default_search_provider_ is |
| 1952 // NULL due to policy. We'll never actually get recursion with data == NULL. | 1963 // NULL due to policy. We'll never actually get recursion with data == NULL. |
| 1953 if (source == default_search_provider_source_ && data != NULL && | 1964 if (source == default_search_provider_source_ && data != NULL && |
| 1954 TemplateURL::MatchesData(default_search_provider_, data)) | 1965 TemplateURL::MatchesData(default_search_provider_, data)) |
| 1955 return; | 1966 return; |
| 1956 | 1967 |
| 1957 UMA_HISTOGRAM_ENUMERATION("Search.DefaultSearchChangeOrigin", | 1968 // Use void* to prevent a potential UAF - this is only for pointer comparison. |
| 1958 dsp_change_origin_, DSP_CHANGE_MAX); | 1969 void* previous_default_search_engine = default_search_provider_; |
|
Peter Kasting
2014/05/21 19:38:41
If you're only doing a pointer comparison, you sho
erikwright (departed)
2014/05/21 20:38:48
This pointer could be invalidated in a variety of
Peter Kasting
2014/05/21 20:45:13
Right, I understand that.
| |
| 1959 | 1970 |
| 1960 WebDataService::KeywordBatchModeScoper keyword_scoper(service_.get()); | 1971 WebDataService::KeywordBatchModeScoper keyword_scoper(service_.get()); |
| 1961 if (default_search_provider_source_ == DefaultSearchManager::FROM_POLICY || | 1972 if (default_search_provider_source_ == DefaultSearchManager::FROM_POLICY || |
| 1962 source == DefaultSearchManager::FROM_POLICY) { | 1973 source == DefaultSearchManager::FROM_POLICY) { |
| 1963 // We do this both to remove any no-longer-applicable policy-defined DSE as | 1974 // We do this both to remove any no-longer-applicable policy-defined DSE as |
| 1964 // well as to add the new one, if appropriate. | 1975 // well as to add the new one, if appropriate. |
| 1965 UpdateProvidersCreatedByPolicy( | 1976 UpdateProvidersCreatedByPolicy( |
| 1966 &template_urls_, | 1977 &template_urls_, |
| 1967 source == DefaultSearchManager::FROM_POLICY ? data : NULL); | 1978 source == DefaultSearchManager::FROM_POLICY ? data : NULL); |
| 1968 } | 1979 } |
| 1969 | 1980 |
| 1970 if (!data) { | 1981 if (!data) { |
| 1971 default_search_provider_ = NULL; | 1982 default_search_provider_ = NULL; |
| 1972 default_search_provider_source_ = source; | 1983 } else if (source == DefaultSearchManager::FROM_EXTENSION) { |
| 1973 NotifyObservers(); | |
| 1974 return; | |
| 1975 } | |
| 1976 | |
| 1977 if (source == DefaultSearchManager::FROM_EXTENSION) { | |
| 1978 default_search_provider_ = FindMatchingExtensionTemplateURL( | 1984 default_search_provider_ = FindMatchingExtensionTemplateURL( |
| 1979 *data, TemplateURL::NORMAL_CONTROLLED_BY_EXTENSION); | 1985 *data, TemplateURL::NORMAL_CONTROLLED_BY_EXTENSION); |
| 1980 } | 1986 } else if (source == DefaultSearchManager::FROM_FALLBACK) { |
| 1981 | |
| 1982 if (source == DefaultSearchManager::FROM_FALLBACK) { | |
| 1983 default_search_provider_ = | 1987 default_search_provider_ = |
| 1984 FindPrepopulatedTemplateURL(data->prepopulate_id); | 1988 FindPrepopulatedTemplateURL(data->prepopulate_id); |
| 1985 if (default_search_provider_) { | 1989 if (default_search_provider_) { |
| 1986 TemplateURLData update_data(*data); | 1990 TemplateURLData update_data(*data); |
| 1987 update_data.sync_guid = default_search_provider_->sync_guid(); | 1991 update_data.sync_guid = default_search_provider_->sync_guid(); |
| 1988 if (!default_search_provider_->safe_for_autoreplace()) { | 1992 if (!default_search_provider_->safe_for_autoreplace()) { |
| 1989 update_data.safe_for_autoreplace = false; | 1993 update_data.safe_for_autoreplace = false; |
| 1990 update_data.SetKeyword(default_search_provider_->keyword()); | 1994 update_data.SetKeyword(default_search_provider_->keyword()); |
| 1991 update_data.short_name = default_search_provider_->short_name(); | 1995 update_data.short_name = default_search_provider_->short_name(); |
| 1992 } | 1996 } |
| 1993 UIThreadSearchTermsData search_terms_data( | 1997 UIThreadSearchTermsData search_terms_data( |
| 1994 default_search_provider_->profile()); | 1998 default_search_provider_->profile()); |
| 1995 UpdateNoNotify(default_search_provider_, | 1999 UpdateNoNotify(default_search_provider_, |
| 1996 TemplateURL(profile_, update_data), | 2000 TemplateURL(profile_, update_data), |
| 1997 search_terms_data); | 2001 search_terms_data); |
| 1998 } else { | 2002 } else { |
| 1999 // Normally the prepopulated fallback should be present in | 2003 // Normally the prepopulated fallback should be present in |
| 2000 // |template_urls_|, but in a few cases it might not be: | 2004 // |template_urls_|, but in a few cases it might not be: |
| 2001 // (1) Tests that initialize the TemplateURLService in peculiar ways. | 2005 // (1) Tests that initialize the TemplateURLService in peculiar ways. |
| 2002 // (2) If the user deleted the pre-populated default and we subsequently | 2006 // (2) If the user deleted the pre-populated default and we subsequently |
| 2003 // lost their user-selected value. | 2007 // lost their user-selected value. |
| 2004 TemplateURL* new_dse = new TemplateURL(profile_, *data); | 2008 TemplateURL* new_dse = new TemplateURL(profile_, *data); |
| 2005 if (AddNoNotify(new_dse, true)) | 2009 if (AddNoNotify(new_dse, true)) |
| 2006 default_search_provider_ = new_dse; | 2010 default_search_provider_ = new_dse; |
| 2007 } | 2011 } |
| 2008 } | 2012 } else if (source == DefaultSearchManager::FROM_USER) { |
| 2009 if (source == DefaultSearchManager::FROM_USER) { | |
| 2010 default_search_provider_ = GetTemplateURLForGUID(data->sync_guid); | 2013 default_search_provider_ = GetTemplateURLForGUID(data->sync_guid); |
| 2011 if (!default_search_provider_ && data->prepopulate_id) { | 2014 if (!default_search_provider_ && data->prepopulate_id) { |
| 2012 default_search_provider_ = | 2015 default_search_provider_ = |
| 2013 FindPrepopulatedTemplateURL(data->prepopulate_id); | 2016 FindPrepopulatedTemplateURL(data->prepopulate_id); |
| 2014 } | 2017 } |
| 2015 TemplateURLData new_data(*data); | 2018 TemplateURLData new_data(*data); |
| 2016 new_data.show_in_default_list = true; | 2019 new_data.show_in_default_list = true; |
| 2017 if (default_search_provider_) { | 2020 if (default_search_provider_) { |
| 2018 UIThreadSearchTermsData search_terms_data( | 2021 UIThreadSearchTermsData search_terms_data( |
| 2019 default_search_provider_->profile()); | 2022 default_search_provider_->profile()); |
| (...skipping 20 matching lines...) Expand all Loading... | |
| 2040 default_search_provider_->HasGoogleBaseURLs()) { | 2043 default_search_provider_->HasGoogleBaseURLs()) { |
| 2041 if (profile_) | 2044 if (profile_) |
| 2042 GoogleURLTracker::RequestServerCheck(profile_, false); | 2045 GoogleURLTracker::RequestServerCheck(profile_, false); |
| 2043 #if defined(ENABLE_RLZ) | 2046 #if defined(ENABLE_RLZ) |
| 2044 RLZTracker::RecordProductEvent(rlz_lib::CHROME, | 2047 RLZTracker::RecordProductEvent(rlz_lib::CHROME, |
| 2045 RLZTracker::CHROME_OMNIBOX, | 2048 RLZTracker::CHROME_OMNIBOX, |
| 2046 rlz_lib::SET_TO_GOOGLE); | 2049 rlz_lib::SET_TO_GOOGLE); |
| 2047 #endif | 2050 #endif |
| 2048 } | 2051 } |
| 2049 | 2052 |
| 2050 NotifyObservers(); | 2053 if (default_search_provider_ != previous_default_search_engine) { |
| 2054 if (dsp_change_origin_ != DSP_CHANGE_INITIAL_LOAD) { | |
| 2055 UMA_HISTOGRAM_ENUMERATION("Search.DefaultSearchChangeOrigin", | |
| 2056 dsp_change_origin_, | |
| 2057 DSP_CHANGE_MAX); | |
| 2058 } | |
| 2059 NotifyObservers(); | |
| 2060 } | |
| 2051 } | 2061 } |
| 2052 | 2062 |
| 2053 bool TemplateURLService::AddNoNotify(TemplateURL* template_url, | 2063 bool TemplateURLService::AddNoNotify(TemplateURL* template_url, |
| 2054 bool newly_adding) { | 2064 bool newly_adding) { |
| 2055 DCHECK(template_url); | 2065 DCHECK(template_url); |
| 2056 | 2066 |
| 2057 if (newly_adding) { | 2067 if (newly_adding) { |
| 2058 DCHECK_EQ(kInvalidTemplateURLID, template_url->id()); | 2068 DCHECK_EQ(kInvalidTemplateURLID, template_url->id()); |
| 2059 DCHECK(std::find(template_urls_.begin(), template_urls_.end(), | 2069 DCHECK(std::find(template_urls_.begin(), template_urls_.end(), |
| 2060 template_url) == template_urls_.end()); | 2070 template_url) == template_urls_.end()); |
| (...skipping 434 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 2495 | 2505 |
| 2496 if (most_recently_intalled_default) { | 2506 if (most_recently_intalled_default) { |
| 2497 base::AutoReset<DefaultSearchChangeOrigin> change_origin( | 2507 base::AutoReset<DefaultSearchChangeOrigin> change_origin( |
| 2498 &dsp_change_origin_, DSP_CHANGE_OVERRIDE_SETTINGS_EXTENSION); | 2508 &dsp_change_origin_, DSP_CHANGE_OVERRIDE_SETTINGS_EXTENSION); |
| 2499 default_search_manager_.SetExtensionControlledDefaultSearchEngine( | 2509 default_search_manager_.SetExtensionControlledDefaultSearchEngine( |
| 2500 most_recently_intalled_default->data()); | 2510 most_recently_intalled_default->data()); |
| 2501 } else { | 2511 } else { |
| 2502 default_search_manager_.ClearExtensionControlledDefaultSearchEngine(); | 2512 default_search_manager_.ClearExtensionControlledDefaultSearchEngine(); |
| 2503 } | 2513 } |
| 2504 } | 2514 } |
| OLD | NEW |