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 1417 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1954 UpdateProvidersCreatedByPolicy( | 1965 UpdateProvidersCreatedByPolicy( |
1955 &template_urls_, | 1966 &template_urls_, |
1956 source == DefaultSearchManager::FROM_POLICY ? data : nullptr); | 1967 source == DefaultSearchManager::FROM_POLICY ? data : nullptr); |
1957 } | 1968 } |
1958 | 1969 |
1959 if (!data) { | 1970 if (!data) { |
1960 default_search_provider_ = nullptr; | 1971 default_search_provider_ = nullptr; |
1961 } else if (source == DefaultSearchManager::FROM_EXTENSION) { | 1972 } else if (source == DefaultSearchManager::FROM_EXTENSION) { |
1962 default_search_provider_ = FindMatchingExtensionTemplateURL( | 1973 default_search_provider_ = FindMatchingExtensionTemplateURL( |
1963 *data, TemplateURL::NORMAL_CONTROLLED_BY_EXTENSION); | 1974 *data, TemplateURL::NORMAL_CONTROLLED_BY_EXTENSION); |
| 1975 if (!default_search_provider_) { |
| 1976 // This is possible if extension has installed DSE, writing extension |
| 1977 // controlled pref and extension service is not yet loaded. Create |
| 1978 // extension template url from preference value and add it. Later when |
| 1979 // extension will load, it will again register its DSE in template url |
| 1980 // service. |
| 1981 |
| 1982 // Get id of extension that controls preference. |
| 1983 std::string ext_id = client_->GetExtensionControllingDSEPref(); |
| 1984 DCHECK(!ext_id.empty()); |
| 1985 auto ext_turl = base::MakeUnique<TemplateURL>( |
| 1986 *data, TemplateURL::NORMAL_CONTROLLED_BY_EXTENSION); |
| 1987 ext_turl->extension_info_ = |
| 1988 base::MakeUnique<TemplateURL::AssociatedExtensionInfo>(ext_id); |
| 1989 ext_turl->extension_info_->wants_to_be_default_engine = true; |
| 1990 default_search_provider_ = AddNoNotify(std::move(ext_turl), true); |
| 1991 } |
1964 } else if (source == DefaultSearchManager::FROM_FALLBACK) { | 1992 } else if (source == DefaultSearchManager::FROM_FALLBACK) { |
1965 default_search_provider_ = | 1993 default_search_provider_ = |
1966 FindPrepopulatedTemplateURL(data->prepopulate_id); | 1994 FindPrepopulatedTemplateURL(data->prepopulate_id); |
1967 if (default_search_provider_) { | 1995 if (default_search_provider_) { |
1968 TemplateURLData update_data(*data); | 1996 TemplateURLData update_data(*data); |
1969 update_data.sync_guid = default_search_provider_->sync_guid(); | 1997 update_data.sync_guid = default_search_provider_->sync_guid(); |
1970 if (!default_search_provider_->safe_for_autoreplace()) { | 1998 if (!default_search_provider_->safe_for_autoreplace()) { |
1971 update_data.safe_for_autoreplace = false; | 1999 update_data.safe_for_autoreplace = false; |
1972 update_data.SetKeyword(default_search_provider_->keyword()); | 2000 update_data.SetKeyword(default_search_provider_->keyword()); |
1973 update_data.SetShortName(default_search_provider_->short_name()); | 2001 update_data.SetShortName(default_search_provider_->short_name()); |
(...skipping 500 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2474 const TemplateURLData& data, | 2502 const TemplateURLData& data, |
2475 TemplateURL::Type type) { | 2503 TemplateURL::Type type) { |
2476 DCHECK_NE(TemplateURL::NORMAL, type); | 2504 DCHECK_NE(TemplateURL::NORMAL, type); |
2477 for (const auto& turl : template_urls_) { | 2505 for (const auto& turl : template_urls_) { |
2478 if (turl->type() == type && | 2506 if (turl->type() == type && |
2479 TemplateURL::MatchesData(turl.get(), &data, search_terms_data())) | 2507 TemplateURL::MatchesData(turl.get(), &data, search_terms_data())) |
2480 return turl.get(); | 2508 return turl.get(); |
2481 } | 2509 } |
2482 return nullptr; | 2510 return nullptr; |
2483 } | 2511 } |
2484 | |
2485 void TemplateURLService::UpdateExtensionDefaultSearchEngine() { | |
2486 TemplateURL* most_recently_intalled_default = nullptr; | |
2487 for (const auto& turl : template_urls_) { | |
2488 if ((turl->type() == TemplateURL::NORMAL_CONTROLLED_BY_EXTENSION) && | |
2489 turl->extension_info_->wants_to_be_default_engine && | |
2490 turl->SupportsReplacement(search_terms_data()) && | |
2491 (!most_recently_intalled_default || | |
2492 (most_recently_intalled_default->extension_info_->install_time < | |
2493 turl->extension_info_->install_time))) | |
2494 most_recently_intalled_default = turl.get(); | |
2495 } | |
2496 | |
2497 if (most_recently_intalled_default) { | |
2498 base::AutoReset<DefaultSearchChangeOrigin> change_origin( | |
2499 &dsp_change_origin_, DSP_CHANGE_OVERRIDE_SETTINGS_EXTENSION); | |
2500 default_search_manager_.SetExtensionControlledDefaultSearchEngine( | |
2501 most_recently_intalled_default->data()); | |
2502 } else { | |
2503 default_search_manager_.ClearExtensionControlledDefaultSearchEngine(); | |
2504 } | |
2505 } | |
OLD | NEW |