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 434 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
445 DCHECK(!url.empty()); | 445 DCHECK(!url.empty()); |
446 template_url->data_.SetShortName(short_name); | 446 template_url->data_.SetShortName(short_name); |
447 template_url->data_.SetKeyword(keyword); | 447 template_url->data_.SetKeyword(keyword); |
448 template_url->SetURL(url); | 448 template_url->SetURL(url); |
449 return Add(std::move(template_url)); | 449 return Add(std::move(template_url)); |
450 } | 450 } |
451 | 451 |
452 TemplateURL* TemplateURLService::AddExtensionControlledTURL( | 452 TemplateURL* TemplateURLService::AddExtensionControlledTURL( |
453 std::unique_ptr<TemplateURL> template_url, | 453 std::unique_ptr<TemplateURL> template_url, |
454 std::unique_ptr<TemplateURL::AssociatedExtensionInfo> info) { | 454 std::unique_ptr<TemplateURL::AssociatedExtensionInfo> info) { |
455 DCHECK(loaded_); | |
456 DCHECK(template_url); | 455 DCHECK(template_url); |
457 DCHECK_EQ(kInvalidTemplateURLID, template_url->id()); | 456 DCHECK_EQ(kInvalidTemplateURLID, template_url->id()); |
458 DCHECK(info); | 457 DCHECK(info); |
459 DCHECK_NE(TemplateURL::NORMAL, template_url->type()); | 458 DCHECK_NE(TemplateURL::NORMAL, template_url->type()); |
460 DCHECK( | 459 DCHECK( |
461 !FindTemplateURLForExtension(info->extension_id, template_url->type())); | 460 !FindTemplateURLForExtension(info->extension_id, template_url->type())); |
| 461 |
462 template_url->extension_info_.swap(info); | 462 template_url->extension_info_.swap(info); |
463 | 463 |
464 KeywordWebDataService::BatchModeScoper scoper(web_data_service_.get()); | |
465 TemplateURL* template_url_ptr = AddNoNotify(std::move(template_url), true); | 464 TemplateURL* template_url_ptr = AddNoNotify(std::move(template_url), true); |
466 if (template_url_ptr) { | 465 if (template_url_ptr) |
467 if (template_url_ptr->extension_info_->wants_to_be_default_engine) { | |
468 UpdateExtensionDefaultSearchEngine(); | |
469 } | |
470 NotifyObservers(); | 466 NotifyObservers(); |
471 } | |
472 | 467 |
473 return template_url_ptr; | 468 return template_url_ptr; |
474 } | 469 } |
475 | 470 |
476 void TemplateURLService::Remove(TemplateURL* template_url) { | 471 void TemplateURLService::Remove(TemplateURL* template_url) { |
477 RemoveNoNotify(template_url); | 472 RemoveNoNotify(template_url); |
478 NotifyObservers(); | 473 NotifyObservers(); |
479 } | 474 } |
480 | 475 |
481 void TemplateURLService::RemoveExtensionControlledTURL( | 476 void TemplateURLService::RemoveExtensionControlledTURL( |
482 const std::string& extension_id, | 477 const std::string& extension_id, |
483 TemplateURL::Type type) { | 478 TemplateURL::Type type) { |
484 DCHECK(loaded_); | |
485 TemplateURL* url = FindTemplateURLForExtension(extension_id, type); | 479 TemplateURL* url = FindTemplateURLForExtension(extension_id, type); |
486 if (!url) | 480 if (!url) |
487 return; | 481 return; |
488 // NULL this out so that we can call RemoveNoNotify. | 482 // NULL this out so that we can call RemoveNoNotify. |
489 // UpdateExtensionDefaultSearchEngine will cause it to be reset. | |
490 if (default_search_provider_ == url) | 483 if (default_search_provider_ == url) |
491 default_search_provider_ = nullptr; | 484 default_search_provider_ = nullptr; |
492 KeywordWebDataService::BatchModeScoper scoper(web_data_service_.get()); | |
493 RemoveNoNotify(url); | 485 RemoveNoNotify(url); |
494 UpdateExtensionDefaultSearchEngine(); | |
495 NotifyObservers(); | 486 NotifyObservers(); |
496 } | 487 } |
497 | 488 |
498 void TemplateURLService::RemoveAutoGeneratedSince(base::Time created_after) { | 489 void TemplateURLService::RemoveAutoGeneratedSince(base::Time created_after) { |
499 RemoveAutoGeneratedBetween(created_after, base::Time()); | 490 RemoveAutoGeneratedBetween(created_after, base::Time()); |
500 } | 491 } |
501 | 492 |
502 void TemplateURLService::RemoveAutoGeneratedBetween(base::Time created_after, | 493 void TemplateURLService::RemoveAutoGeneratedBetween(base::Time created_after, |
503 base::Time created_before) { | 494 base::Time created_before) { |
504 RemoveAutoGeneratedForUrlsBetween(base::Callback<bool(const GURL&)>(), | 495 RemoveAutoGeneratedForUrlsBetween(base::Callback<bool(const GURL&)>(), |
(...skipping 1396 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1901 bool TemplateURLService::ApplyDefaultSearchChangeNoMetrics( | 1892 bool TemplateURLService::ApplyDefaultSearchChangeNoMetrics( |
1902 const TemplateURLData* data, | 1893 const TemplateURLData* data, |
1903 DefaultSearchManager::Source source) { | 1894 DefaultSearchManager::Source source) { |
1904 if (!loaded_) { | 1895 if (!loaded_) { |
1905 // Set |initial_default_search_provider_| from the preferences. This is | 1896 // Set |initial_default_search_provider_| from the preferences. This is |
1906 // mainly so we can hold ownership until we get to the point where the list | 1897 // mainly so we can hold ownership until we get to the point where the list |
1907 // of keywords from Web Data is the owner of everything including the | 1898 // of keywords from Web Data is the owner of everything including the |
1908 // default. | 1899 // default. |
1909 bool changed = TemplateURL::MatchesData( | 1900 bool changed = TemplateURL::MatchesData( |
1910 initial_default_search_provider_.get(), data, search_terms_data()); | 1901 initial_default_search_provider_.get(), data, search_terms_data()); |
| 1902 TemplateURL::Type initial_engine_type = |
| 1903 (source == DefaultSearchManager::FROM_EXTENSION) |
| 1904 ? TemplateURL::NORMAL_CONTROLLED_BY_EXTENSION |
| 1905 : TemplateURL::NORMAL; |
1911 initial_default_search_provider_ = | 1906 initial_default_search_provider_ = |
1912 data ? base::MakeUnique<TemplateURL>(*data) : nullptr; | 1907 data ? base::MakeUnique<TemplateURL>(*data, initial_engine_type) |
| 1908 : nullptr; |
1913 default_search_provider_source_ = source; | 1909 default_search_provider_source_ = source; |
1914 return changed; | 1910 return changed; |
1915 } | 1911 } |
1916 | 1912 |
1917 // Prevent recursion if we update the value stored in default_search_manager_. | 1913 // Prevent recursion if we update the value stored in default_search_manager_. |
1918 // Note that we exclude the case of data == NULL because that could cause a | 1914 // Note that we exclude the case of data == NULL because that could cause a |
1919 // false positive for recursion when the initial_default_search_provider_ is | 1915 // false positive for recursion when the initial_default_search_provider_ is |
1920 // NULL due to policy. We'll never actually get recursion with data == NULL. | 1916 // NULL due to policy. We'll never actually get recursion with data == NULL. |
1921 if (source == default_search_provider_source_ && data != nullptr && | 1917 if (source == default_search_provider_source_ && data != nullptr && |
1922 TemplateURL::MatchesData(default_search_provider_, data, | 1918 TemplateURL::MatchesData(default_search_provider_, data, |
(...skipping 10 matching lines...) Expand all Loading... |
1933 // We do this both to remove any no-longer-applicable policy-defined DSE as | 1929 // We do this both to remove any no-longer-applicable policy-defined DSE as |
1934 // well as to add the new one, if appropriate. | 1930 // well as to add the new one, if appropriate. |
1935 UpdateProvidersCreatedByPolicy( | 1931 UpdateProvidersCreatedByPolicy( |
1936 &template_urls_, | 1932 &template_urls_, |
1937 source == DefaultSearchManager::FROM_POLICY ? data : nullptr); | 1933 source == DefaultSearchManager::FROM_POLICY ? data : nullptr); |
1938 } | 1934 } |
1939 | 1935 |
1940 if (!data) { | 1936 if (!data) { |
1941 default_search_provider_ = nullptr; | 1937 default_search_provider_ = nullptr; |
1942 } else if (source == DefaultSearchManager::FROM_EXTENSION) { | 1938 } else if (source == DefaultSearchManager::FROM_EXTENSION) { |
1943 default_search_provider_ = FindMatchingExtensionTemplateURL( | 1939 // Extension controlled default TemplateURL must be stored in prefs along |
1944 *data, TemplateURL::NORMAL_CONTROLLED_BY_EXTENSION); | 1940 // with extension id. |
| 1941 default_search_provider_ = GetExtensionControlledDefaultTemplateURL(); |
| 1942 DCHECK(default_search_provider_); |
1945 } else if (source == DefaultSearchManager::FROM_FALLBACK) { | 1943 } else if (source == DefaultSearchManager::FROM_FALLBACK) { |
1946 default_search_provider_ = | 1944 default_search_provider_ = |
1947 FindPrepopulatedTemplateURL(data->prepopulate_id); | 1945 FindPrepopulatedTemplateURL(data->prepopulate_id); |
1948 if (default_search_provider_) { | 1946 if (default_search_provider_) { |
1949 TemplateURLData update_data(*data); | 1947 TemplateURLData update_data(*data); |
1950 update_data.sync_guid = default_search_provider_->sync_guid(); | 1948 update_data.sync_guid = default_search_provider_->sync_guid(); |
1951 if (!default_search_provider_->safe_for_autoreplace()) { | 1949 if (!default_search_provider_->safe_for_autoreplace()) { |
1952 update_data.safe_for_autoreplace = false; | 1950 update_data.safe_for_autoreplace = false; |
1953 update_data.SetKeyword(default_search_provider_->keyword()); | 1951 update_data.SetKeyword(default_search_provider_->keyword()); |
1954 update_data.SetShortName(default_search_provider_->short_name()); | 1952 update_data.SetShortName(default_search_provider_->short_name()); |
(...skipping 25 matching lines...) Expand all Loading... |
1980 std::unique_ptr<TemplateURL> new_dse_ptr = | 1978 std::unique_ptr<TemplateURL> new_dse_ptr = |
1981 base::MakeUnique<TemplateURL>(new_data); | 1979 base::MakeUnique<TemplateURL>(new_data); |
1982 TemplateURL* new_dse = new_dse_ptr.get(); | 1980 TemplateURL* new_dse = new_dse_ptr.get(); |
1983 if (AddNoNotify(std::move(new_dse_ptr), true)) | 1981 if (AddNoNotify(std::move(new_dse_ptr), true)) |
1984 default_search_provider_ = new_dse; | 1982 default_search_provider_ = new_dse; |
1985 } | 1983 } |
1986 if (default_search_provider_ && prefs_) { | 1984 if (default_search_provider_ && prefs_) { |
1987 prefs_->SetString(prefs::kSyncedDefaultSearchProviderGUID, | 1985 prefs_->SetString(prefs::kSyncedDefaultSearchProviderGUID, |
1988 default_search_provider_->sync_guid()); | 1986 default_search_provider_->sync_guid()); |
1989 } | 1987 } |
1990 | |
1991 } | 1988 } |
1992 | 1989 |
1993 default_search_provider_source_ = source; | 1990 default_search_provider_source_ = source; |
1994 | 1991 |
1995 bool changed = default_search_provider_ != previous_default_search_engine; | 1992 bool changed = default_search_provider_ != previous_default_search_engine; |
1996 if (changed) | 1993 if (changed) |
1997 RequestGoogleURLTrackerServerCheckIfNecessary(); | 1994 RequestGoogleURLTrackerServerCheckIfNecessary(); |
1998 | 1995 |
1999 NotifyObservers(); | 1996 NotifyObservers(); |
2000 | 1997 |
(...skipping 443 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2444 const std::string& extension_id, | 2441 const std::string& extension_id, |
2445 TemplateURL::Type type) { | 2442 TemplateURL::Type type) { |
2446 DCHECK_NE(TemplateURL::NORMAL, type); | 2443 DCHECK_NE(TemplateURL::NORMAL, type); |
2447 for (const auto& turl : template_urls_) { | 2444 for (const auto& turl : template_urls_) { |
2448 if (turl->type() == type && turl->GetExtensionId() == extension_id) | 2445 if (turl->type() == type && turl->GetExtensionId() == extension_id) |
2449 return turl.get(); | 2446 return turl.get(); |
2450 } | 2447 } |
2451 return nullptr; | 2448 return nullptr; |
2452 } | 2449 } |
2453 | 2450 |
2454 TemplateURL* TemplateURLService::FindMatchingExtensionTemplateURL( | 2451 TemplateURL* TemplateURLService::GetExtensionControlledDefaultTemplateURL() { |
2455 const TemplateURLData& data, | 2452 const base::DictionaryValue* data_dict = prefs_->GetDictionary( |
2456 TemplateURL::Type type) { | 2453 DefaultSearchManager::kDefaultSearchProviderDataPrefName); |
2457 DCHECK_NE(TemplateURL::NORMAL, type); | 2454 std::string extension_id; |
2458 for (const auto& turl : template_urls_) { | 2455 if (data_dict && |
2459 if (turl->type() == type && | 2456 data_dict->GetString(DefaultSearchManager::kExtensionID, &extension_id)) { |
2460 TemplateURL::MatchesData(turl.get(), &data, search_terms_data())) | 2457 DCHECK(!extension_id.empty()); |
2461 return turl.get(); | 2458 return FindTemplateURLForExtension( |
| 2459 extension_id, TemplateURL::NORMAL_CONTROLLED_BY_EXTENSION); |
2462 } | 2460 } |
2463 return nullptr; | 2461 return nullptr; |
2464 } | 2462 } |
2465 | |
2466 void TemplateURLService::UpdateExtensionDefaultSearchEngine() { | |
2467 TemplateURL* most_recently_intalled_default = nullptr; | |
2468 for (const auto& turl : template_urls_) { | |
2469 if ((turl->type() == TemplateURL::NORMAL_CONTROLLED_BY_EXTENSION) && | |
2470 turl->extension_info_->wants_to_be_default_engine && | |
2471 turl->SupportsReplacement(search_terms_data()) && | |
2472 (!most_recently_intalled_default || | |
2473 (most_recently_intalled_default->extension_info_->install_time < | |
2474 turl->extension_info_->install_time))) | |
2475 most_recently_intalled_default = turl.get(); | |
2476 } | |
2477 | |
2478 if (most_recently_intalled_default) { | |
2479 base::AutoReset<DefaultSearchChangeOrigin> change_origin( | |
2480 &dsp_change_origin_, DSP_CHANGE_OVERRIDE_SETTINGS_EXTENSION); | |
2481 default_search_manager_.SetExtensionControlledDefaultSearchEngine( | |
2482 most_recently_intalled_default->data()); | |
2483 } else { | |
2484 default_search_manager_.ClearExtensionControlledDefaultSearchEngine(); | |
2485 } | |
2486 } | |
OLD | NEW |