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 466 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
477 DCHECK(!url.empty()); | 477 DCHECK(!url.empty()); |
478 template_url->data_.SetShortName(short_name); | 478 template_url->data_.SetShortName(short_name); |
479 template_url->data_.SetKeyword(keyword); | 479 template_url->data_.SetKeyword(keyword); |
480 template_url->SetURL(url); | 480 template_url->SetURL(url); |
481 return Add(std::move(template_url)); | 481 return Add(std::move(template_url)); |
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_); | |
488 DCHECK(template_url); | 487 DCHECK(template_url); |
489 DCHECK_EQ(kInvalidTemplateURLID, template_url->id()); | 488 DCHECK_EQ(kInvalidTemplateURLID, template_url->id()); |
490 DCHECK(info); | 489 DCHECK(info); |
491 DCHECK_NE(TemplateURL::NORMAL, template_url->type()); | 490 DCHECK_NE(TemplateURL::NORMAL, template_url->type()); |
492 DCHECK( | 491 DCHECK( |
493 !FindTemplateURLForExtension(info->extension_id, template_url->type())); | 492 !FindTemplateURLForExtension(info->extension_id, template_url->type())); |
493 | |
494 template_url->extension_info_.swap(info); | 494 template_url->extension_info_.swap(info); |
495 | 495 |
496 KeywordWebDataService::BatchModeScoper scoper(web_data_service_.get()); | |
497 TemplateURL* template_url_ptr = AddNoNotify(std::move(template_url), true); | 496 TemplateURL* template_url_ptr = AddNoNotify(std::move(template_url), true); |
498 if (template_url_ptr) { | 497 if (template_url_ptr) { |
Peter Kasting
2016/12/22 20:49:04
Nit: No {}
Alexander Yashkin
2016/12/23 19:44:09
Done
| |
499 if (template_url_ptr->extension_info_->wants_to_be_default_engine) { | |
500 UpdateExtensionDefaultSearchEngine(); | |
501 } | |
502 NotifyObservers(); | 498 NotifyObservers(); |
503 } | 499 } |
504 | 500 |
505 return template_url_ptr; | 501 return template_url_ptr; |
506 } | 502 } |
507 | 503 |
508 void TemplateURLService::Remove(TemplateURL* template_url) { | 504 void TemplateURLService::Remove(TemplateURL* template_url) { |
509 RemoveNoNotify(template_url); | 505 RemoveNoNotify(template_url); |
510 NotifyObservers(); | 506 NotifyObservers(); |
511 } | 507 } |
512 | 508 |
513 void TemplateURLService::RemoveExtensionControlledTURL( | 509 void TemplateURLService::RemoveExtensionControlledTURL( |
514 const std::string& extension_id, | 510 const std::string& extension_id, |
515 TemplateURL::Type type) { | 511 TemplateURL::Type type) { |
516 DCHECK(loaded_); | 512 DCHECK(loaded_); |
517 TemplateURL* url = FindTemplateURLForExtension(extension_id, type); | 513 TemplateURL* url = FindTemplateURLForExtension(extension_id, type); |
518 if (!url) | 514 if (!url) |
519 return; | 515 return; |
520 // NULL this out so that we can call RemoveNoNotify. | 516 // NULL this out so that we can call RemoveNoNotify. |
521 // UpdateExtensionDefaultSearchEngine will cause it to be reset. | |
522 if (default_search_provider_ == url) | 517 if (default_search_provider_ == url) |
523 default_search_provider_ = nullptr; | 518 default_search_provider_ = nullptr; |
524 KeywordWebDataService::BatchModeScoper scoper(web_data_service_.get()); | 519 KeywordWebDataService::BatchModeScoper scoper(web_data_service_.get()); |
Peter Kasting
2016/12/22 20:49:04
I don't think we want this scoper anymore?
Alexander Yashkin
2016/12/23 19:44:09
Removed
| |
525 RemoveNoNotify(url); | 520 RemoveNoNotify(url); |
526 UpdateExtensionDefaultSearchEngine(); | |
527 NotifyObservers(); | 521 NotifyObservers(); |
528 } | 522 } |
529 | 523 |
530 void TemplateURLService::RemoveAutoGeneratedSince(base::Time created_after) { | 524 void TemplateURLService::RemoveAutoGeneratedSince(base::Time created_after) { |
531 RemoveAutoGeneratedBetween(created_after, base::Time()); | 525 RemoveAutoGeneratedBetween(created_after, base::Time()); |
532 } | 526 } |
533 | 527 |
534 void TemplateURLService::RemoveAutoGeneratedBetween(base::Time created_after, | 528 void TemplateURLService::RemoveAutoGeneratedBetween(base::Time created_after, |
535 base::Time created_before) { | 529 base::Time created_before) { |
536 RemoveAutoGeneratedForUrlsBetween(base::Callback<bool(const GURL&)>(), | 530 RemoveAutoGeneratedForUrlsBetween(base::Callback<bool(const GURL&)>(), |
(...skipping 1418 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1955 UpdateProvidersCreatedByPolicy( | 1949 UpdateProvidersCreatedByPolicy( |
1956 &template_urls_, | 1950 &template_urls_, |
1957 source == DefaultSearchManager::FROM_POLICY ? data : nullptr); | 1951 source == DefaultSearchManager::FROM_POLICY ? data : nullptr); |
1958 } | 1952 } |
1959 | 1953 |
1960 if (!data) { | 1954 if (!data) { |
1961 default_search_provider_ = nullptr; | 1955 default_search_provider_ = nullptr; |
1962 } else if (source == DefaultSearchManager::FROM_EXTENSION) { | 1956 } else if (source == DefaultSearchManager::FROM_EXTENSION) { |
1963 default_search_provider_ = FindMatchingExtensionTemplateURL( | 1957 default_search_provider_ = FindMatchingExtensionTemplateURL( |
1964 *data, TemplateURL::NORMAL_CONTROLLED_BY_EXTENSION); | 1958 *data, TemplateURL::NORMAL_CONTROLLED_BY_EXTENSION); |
1959 DCHECK(default_search_provider_); | |
1965 } else if (source == DefaultSearchManager::FROM_FALLBACK) { | 1960 } else if (source == DefaultSearchManager::FROM_FALLBACK) { |
1966 default_search_provider_ = | 1961 default_search_provider_ = |
1967 FindPrepopulatedTemplateURL(data->prepopulate_id); | 1962 FindPrepopulatedTemplateURL(data->prepopulate_id); |
1968 if (default_search_provider_) { | 1963 if (default_search_provider_) { |
1969 TemplateURLData update_data(*data); | 1964 TemplateURLData update_data(*data); |
1970 update_data.sync_guid = default_search_provider_->sync_guid(); | 1965 update_data.sync_guid = default_search_provider_->sync_guid(); |
1971 if (!default_search_provider_->safe_for_autoreplace()) { | 1966 if (!default_search_provider_->safe_for_autoreplace()) { |
1972 update_data.safe_for_autoreplace = false; | 1967 update_data.safe_for_autoreplace = false; |
1973 update_data.SetKeyword(default_search_provider_->keyword()); | 1968 update_data.SetKeyword(default_search_provider_->keyword()); |
1974 update_data.SetShortName(default_search_provider_->short_name()); | 1969 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, | 2470 const TemplateURLData& data, |
2476 TemplateURL::Type type) { | 2471 TemplateURL::Type type) { |
2477 DCHECK_NE(TemplateURL::NORMAL, type); | 2472 DCHECK_NE(TemplateURL::NORMAL, type); |
2478 for (const auto& turl : template_urls_) { | 2473 for (const auto& turl : template_urls_) { |
2479 if (turl->type() == type && | 2474 if (turl->type() == type && |
2480 TemplateURL::MatchesData(turl.get(), &data, search_terms_data())) | 2475 TemplateURL::MatchesData(turl.get(), &data, search_terms_data())) |
2481 return turl.get(); | 2476 return turl.get(); |
2482 } | 2477 } |
2483 return nullptr; | 2478 return nullptr; |
2484 } | 2479 } |
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 |