| 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 471 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 482 } | 482 } |
| 483 | 483 |
| 484 TemplateURL* TemplateURLService::AddExtensionControlledTURL( | 484 TemplateURL* TemplateURLService::AddExtensionControlledTURL( |
| 485 std::unique_ptr<TemplateURL> template_url, | 485 std::unique_ptr<TemplateURL> template_url, |
| 486 std::unique_ptr<TemplateURL::AssociatedExtensionInfo> info) { | 486 std::unique_ptr<TemplateURL::AssociatedExtensionInfo> info) { |
| 487 DCHECK(loaded_); | 487 DCHECK(loaded_); |
| 488 DCHECK(template_url); | 488 DCHECK(template_url); |
| 489 DCHECK_EQ(kInvalidTemplateURLID, template_url->id()); | 489 DCHECK_EQ(kInvalidTemplateURLID, template_url->id()); |
| 490 DCHECK(info); | 490 DCHECK(info); |
| 491 DCHECK_NE(TemplateURL::NORMAL, template_url->type()); | 491 DCHECK_NE(TemplateURL::NORMAL, template_url->type()); |
| 492 DCHECK( | 492 // It is possible that extension template url is already created for extension |
| 493 !FindTemplateURLForExtension(info->extension_id, template_url->type())); | 493 // that is currently overriding default search, before extension itself is |
| 494 // loaded. This is the case when extension overrides DSE in preferences and |
| 495 // extension template url is created when TemplateUrlService changes to loaded |
| 496 // state but before extensions are loaded. |
| 497 TemplateURL* ext_dse = |
| 498 FindTemplateURLForExtension(info->extension_id, template_url->type()); |
| 499 if (ext_dse) { |
| 500 DCHECK_EQ(TemplateURL::NORMAL_CONTROLLED_BY_EXTENSION, |
| 501 template_url->type()); |
| 502 DCHECK(info->wants_to_be_default_engine); |
| 503 // They must be similar at least. |
| 504 DCHECK_EQ(template_url->data().keyword(), ext_dse->data().keyword()); |
| 505 DCHECK_EQ(template_url->data().short_name(), ext_dse->data().short_name()); |
| 506 DCHECK_EQ(template_url->data().url(), ext_dse->data().url()); |
| 507 return ext_dse; |
| 508 } |
| 509 |
| 494 template_url->extension_info_.swap(info); | 510 template_url->extension_info_.swap(info); |
| 495 | 511 |
| 496 KeywordWebDataService::BatchModeScoper scoper(web_data_service_.get()); | 512 KeywordWebDataService::BatchModeScoper scoper(web_data_service_.get()); |
| 497 TemplateURL* template_url_ptr = AddNoNotify(std::move(template_url), true); | 513 TemplateURL* template_url_ptr = AddNoNotify(std::move(template_url), true); |
| 498 if (template_url_ptr) { | 514 if (template_url_ptr) { |
| 499 if (template_url_ptr->extension_info_->wants_to_be_default_engine) { | |
| 500 UpdateExtensionDefaultSearchEngine(); | |
| 501 } | |
| 502 NotifyObservers(); | 515 NotifyObservers(); |
| 503 } | 516 } |
| 504 | 517 |
| 505 return template_url_ptr; | 518 return template_url_ptr; |
| 506 } | 519 } |
| 507 | 520 |
| 508 void TemplateURLService::Remove(TemplateURL* template_url) { | 521 void TemplateURLService::Remove(TemplateURL* template_url) { |
| 509 RemoveNoNotify(template_url); | 522 RemoveNoNotify(template_url); |
| 510 NotifyObservers(); | 523 NotifyObservers(); |
| 511 } | 524 } |
| 512 | 525 |
| 513 void TemplateURLService::RemoveExtensionControlledTURL( | 526 void TemplateURLService::RemoveExtensionControlledTURL( |
| 514 const std::string& extension_id, | 527 const std::string& extension_id, |
| 515 TemplateURL::Type type) { | 528 TemplateURL::Type type) { |
| 516 DCHECK(loaded_); | 529 DCHECK(loaded_); |
| 517 TemplateURL* url = FindTemplateURLForExtension(extension_id, type); | 530 TemplateURL* url = FindTemplateURLForExtension(extension_id, type); |
| 518 if (!url) | 531 if (!url) |
| 519 return; | 532 return; |
| 520 // NULL this out so that we can call RemoveNoNotify. | 533 // NULL this out so that we can call RemoveNoNotify. |
| 521 // UpdateExtensionDefaultSearchEngine will cause it to be reset. | |
| 522 if (default_search_provider_ == url) | 534 if (default_search_provider_ == url) |
| 523 default_search_provider_ = nullptr; | 535 default_search_provider_ = nullptr; |
| 524 KeywordWebDataService::BatchModeScoper scoper(web_data_service_.get()); | 536 KeywordWebDataService::BatchModeScoper scoper(web_data_service_.get()); |
| 525 RemoveNoNotify(url); | 537 RemoveNoNotify(url); |
| 526 UpdateExtensionDefaultSearchEngine(); | |
| 527 NotifyObservers(); | 538 NotifyObservers(); |
| 528 } | 539 } |
| 529 | 540 |
| 530 void TemplateURLService::RemoveAutoGeneratedSince(base::Time created_after) { | 541 void TemplateURLService::RemoveAutoGeneratedSince(base::Time created_after) { |
| 531 RemoveAutoGeneratedBetween(created_after, base::Time()); | 542 RemoveAutoGeneratedBetween(created_after, base::Time()); |
| 532 } | 543 } |
| 533 | 544 |
| 534 void TemplateURLService::RemoveAutoGeneratedBetween(base::Time created_after, | 545 void TemplateURLService::RemoveAutoGeneratedBetween(base::Time created_after, |
| 535 base::Time created_before) { | 546 base::Time created_before) { |
| 536 RemoveAutoGeneratedForUrlsBetween(base::Callback<bool(const GURL&)>(), | 547 RemoveAutoGeneratedForUrlsBetween(base::Callback<bool(const GURL&)>(), |
| (...skipping 1418 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1955 UpdateProvidersCreatedByPolicy( | 1966 UpdateProvidersCreatedByPolicy( |
| 1956 &template_urls_, | 1967 &template_urls_, |
| 1957 source == DefaultSearchManager::FROM_POLICY ? data : nullptr); | 1968 source == DefaultSearchManager::FROM_POLICY ? data : nullptr); |
| 1958 } | 1969 } |
| 1959 | 1970 |
| 1960 if (!data) { | 1971 if (!data) { |
| 1961 default_search_provider_ = nullptr; | 1972 default_search_provider_ = nullptr; |
| 1962 } else if (source == DefaultSearchManager::FROM_EXTENSION) { | 1973 } else if (source == DefaultSearchManager::FROM_EXTENSION) { |
| 1963 default_search_provider_ = FindMatchingExtensionTemplateURL( | 1974 default_search_provider_ = FindMatchingExtensionTemplateURL( |
| 1964 *data, TemplateURL::NORMAL_CONTROLLED_BY_EXTENSION); | 1975 *data, TemplateURL::NORMAL_CONTROLLED_BY_EXTENSION); |
| 1976 if (!default_search_provider_) { |
| 1977 // This is possible if extension has installed DSE, writing extension |
| 1978 // controlled pref and extension service is not yet loaded. Create |
| 1979 // extension template url from preference value and add it. Later when |
| 1980 // extension will load, it will again register its DSE in template url |
| 1981 // service. |
| 1982 |
| 1983 // Get id of extension that controls preference. |
| 1984 std::string ext_id = client_->GetExtensionControllingDSEPref(); |
| 1985 DCHECK(!ext_id.empty()); |
| 1986 auto ext_turl = base::MakeUnique<TemplateURL>( |
| 1987 *data, TemplateURL::NORMAL_CONTROLLED_BY_EXTENSION); |
| 1988 ext_turl->extension_info_ = |
| 1989 base::MakeUnique<TemplateURL::AssociatedExtensionInfo>(ext_id); |
| 1990 ext_turl->extension_info_->wants_to_be_default_engine = true; |
| 1991 default_search_provider_ = AddNoNotify(std::move(ext_turl), true); |
| 1992 } |
| 1965 } else if (source == DefaultSearchManager::FROM_FALLBACK) { | 1993 } else if (source == DefaultSearchManager::FROM_FALLBACK) { |
| 1966 default_search_provider_ = | 1994 default_search_provider_ = |
| 1967 FindPrepopulatedTemplateURL(data->prepopulate_id); | 1995 FindPrepopulatedTemplateURL(data->prepopulate_id); |
| 1968 if (default_search_provider_) { | 1996 if (default_search_provider_) { |
| 1969 TemplateURLData update_data(*data); | 1997 TemplateURLData update_data(*data); |
| 1970 update_data.sync_guid = default_search_provider_->sync_guid(); | 1998 update_data.sync_guid = default_search_provider_->sync_guid(); |
| 1971 if (!default_search_provider_->safe_for_autoreplace()) { | 1999 if (!default_search_provider_->safe_for_autoreplace()) { |
| 1972 update_data.safe_for_autoreplace = false; | 2000 update_data.safe_for_autoreplace = false; |
| 1973 update_data.SetKeyword(default_search_provider_->keyword()); | 2001 update_data.SetKeyword(default_search_provider_->keyword()); |
| 1974 update_data.SetShortName(default_search_provider_->short_name()); | 2002 update_data.SetShortName(default_search_provider_->short_name()); |
| (...skipping 500 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 2475 const TemplateURLData& data, | 2503 const TemplateURLData& data, |
| 2476 TemplateURL::Type type) { | 2504 TemplateURL::Type type) { |
| 2477 DCHECK_NE(TemplateURL::NORMAL, type); | 2505 DCHECK_NE(TemplateURL::NORMAL, type); |
| 2478 for (const auto& turl : template_urls_) { | 2506 for (const auto& turl : template_urls_) { |
| 2479 if (turl->type() == type && | 2507 if (turl->type() == type && |
| 2480 TemplateURL::MatchesData(turl.get(), &data, search_terms_data())) | 2508 TemplateURL::MatchesData(turl.get(), &data, search_terms_data())) |
| 2481 return turl.get(); | 2509 return turl.get(); |
| 2482 } | 2510 } |
| 2483 return nullptr; | 2511 return nullptr; |
| 2484 } | 2512 } |
| 2485 | |
| 2486 void TemplateURLService::UpdateExtensionDefaultSearchEngine() { | |
| 2487 TemplateURL* most_recently_intalled_default = nullptr; | |
| 2488 for (const auto& turl : template_urls_) { | |
| 2489 if ((turl->type() == TemplateURL::NORMAL_CONTROLLED_BY_EXTENSION) && | |
| 2490 turl->extension_info_->wants_to_be_default_engine && | |
| 2491 turl->SupportsReplacement(search_terms_data()) && | |
| 2492 (!most_recently_intalled_default || | |
| 2493 (most_recently_intalled_default->extension_info_->install_time < | |
| 2494 turl->extension_info_->install_time))) | |
| 2495 most_recently_intalled_default = turl.get(); | |
| 2496 } | |
| 2497 | |
| 2498 if (most_recently_intalled_default) { | |
| 2499 base::AutoReset<DefaultSearchChangeOrigin> change_origin( | |
| 2500 &dsp_change_origin_, DSP_CHANGE_OVERRIDE_SETTINGS_EXTENSION); | |
| 2501 default_search_manager_.SetExtensionControlledDefaultSearchEngine( | |
| 2502 most_recently_intalled_default->data()); | |
| 2503 } else { | |
| 2504 default_search_manager_.ClearExtensionControlledDefaultSearchEngine(); | |
| 2505 } | |
| 2506 } | |
| OLD | NEW |