Chromium Code Reviews| 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 (template_url->type() == TemplateURL::NORMAL_CONTROLLED_BY_EXTENSION && | |
| 500 ext_dse) { | |
|
vasilii
2016/12/06 19:16:42
Shouldn't it be
if (ext_dse) {
DCHECK_EQ(Templat
Alexander Yashkin
2016/12/09 08:19:52
Agree, fixed.
| |
| 501 DCHECK(info->wants_to_be_default_engine); | |
| 502 // They must be similar at least. | |
| 503 DCHECK(ext_dse->data().keyword() == template_url->data().keyword()); | |
|
vasilii
2016/12/06 19:16:42
Use DCHECK_EQ
Alexander Yashkin
2016/12/09 08:19:52
Fixed
| |
| 504 DCHECK(ext_dse->data().short_name() == template_url->data().short_name()); | |
| 505 DCHECK(ext_dse->data().url() == template_url->data().url()); | |
| 506 return ext_dse; | |
| 507 } | |
| 508 | |
| 494 template_url->extension_info_.swap(info); | 509 template_url->extension_info_.swap(info); |
| 495 | 510 |
| 496 KeywordWebDataService::BatchModeScoper scoper(web_data_service_.get()); | 511 KeywordWebDataService::BatchModeScoper scoper(web_data_service_.get()); |
| 497 TemplateURL* template_url_ptr = AddNoNotify(std::move(template_url), true); | 512 TemplateURL* template_url_ptr = AddNoNotify(std::move(template_url), true); |
| 498 if (template_url_ptr) { | 513 if (template_url_ptr) { |
| 499 if (template_url_ptr->extension_info_->wants_to_be_default_engine) { | |
| 500 UpdateExtensionDefaultSearchEngine(); | |
| 501 } | |
| 502 NotifyObservers(); | 514 NotifyObservers(); |
| 503 } | 515 } |
| 504 | 516 |
| 505 return template_url_ptr; | 517 return template_url_ptr; |
| 506 } | 518 } |
| 507 | 519 |
| 508 void TemplateURLService::Remove(TemplateURL* template_url) { | 520 void TemplateURLService::Remove(TemplateURL* template_url) { |
| 509 RemoveNoNotify(template_url); | 521 RemoveNoNotify(template_url); |
| 510 NotifyObservers(); | 522 NotifyObservers(); |
| 511 } | 523 } |
| 512 | 524 |
| 513 void TemplateURLService::RemoveExtensionControlledTURL( | 525 void TemplateURLService::RemoveExtensionControlledTURL( |
| 514 const std::string& extension_id, | 526 const std::string& extension_id, |
| 515 TemplateURL::Type type) { | 527 TemplateURL::Type type) { |
| 516 DCHECK(loaded_); | 528 DCHECK(loaded_); |
| 517 TemplateURL* url = FindTemplateURLForExtension(extension_id, type); | 529 TemplateURL* url = FindTemplateURLForExtension(extension_id, type); |
| 518 if (!url) | 530 if (!url) |
| 519 return; | 531 return; |
| 520 // NULL this out so that we can call RemoveNoNotify. | 532 // NULL this out so that we can call RemoveNoNotify. |
| 521 // UpdateExtensionDefaultSearchEngine will cause it to be reset. | |
| 522 if (default_search_provider_ == url) | 533 if (default_search_provider_ == url) |
| 523 default_search_provider_ = nullptr; | 534 default_search_provider_ = nullptr; |
| 524 KeywordWebDataService::BatchModeScoper scoper(web_data_service_.get()); | 535 KeywordWebDataService::BatchModeScoper scoper(web_data_service_.get()); |
| 525 RemoveNoNotify(url); | 536 RemoveNoNotify(url); |
| 526 UpdateExtensionDefaultSearchEngine(); | |
| 527 NotifyObservers(); | 537 NotifyObservers(); |
| 528 } | 538 } |
| 529 | 539 |
| 530 void TemplateURLService::RemoveAutoGeneratedSince(base::Time created_after) { | 540 void TemplateURLService::RemoveAutoGeneratedSince(base::Time created_after) { |
| 531 RemoveAutoGeneratedBetween(created_after, base::Time()); | 541 RemoveAutoGeneratedBetween(created_after, base::Time()); |
| 532 } | 542 } |
| 533 | 543 |
| 534 void TemplateURLService::RemoveAutoGeneratedBetween(base::Time created_after, | 544 void TemplateURLService::RemoveAutoGeneratedBetween(base::Time created_after, |
| 535 base::Time created_before) { | 545 base::Time created_before) { |
| 536 RemoveAutoGeneratedForUrlsBetween(base::Callback<bool(const GURL&)>(), | 546 RemoveAutoGeneratedForUrlsBetween(base::Callback<bool(const GURL&)>(), |
| (...skipping 1400 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1937 UpdateProvidersCreatedByPolicy( | 1947 UpdateProvidersCreatedByPolicy( |
| 1938 &template_urls_, | 1948 &template_urls_, |
| 1939 source == DefaultSearchManager::FROM_POLICY ? data : nullptr); | 1949 source == DefaultSearchManager::FROM_POLICY ? data : nullptr); |
| 1940 } | 1950 } |
| 1941 | 1951 |
| 1942 if (!data) { | 1952 if (!data) { |
| 1943 default_search_provider_ = nullptr; | 1953 default_search_provider_ = nullptr; |
| 1944 } else if (source == DefaultSearchManager::FROM_EXTENSION) { | 1954 } else if (source == DefaultSearchManager::FROM_EXTENSION) { |
| 1945 default_search_provider_ = FindMatchingExtensionTemplateURL( | 1955 default_search_provider_ = FindMatchingExtensionTemplateURL( |
| 1946 *data, TemplateURL::NORMAL_CONTROLLED_BY_EXTENSION); | 1956 *data, TemplateURL::NORMAL_CONTROLLED_BY_EXTENSION); |
| 1957 if (!default_search_provider_) { | |
| 1958 // This is possible if extension has installed DSE, writing extension | |
| 1959 // controlled pref and extension service is not yet loaded. Create | |
| 1960 // extension template url from preference value and add it. Later when | |
| 1961 // extension will load, it will again register its DSE in template url | |
| 1962 // service. | |
| 1963 | |
| 1964 // Get id of extension that controls preference. | |
| 1965 std::string ext_id = client_->GetExtensionControllingDSEPref(); | |
|
vasilii
2016/12/06 19:16:42
I'm not sure about the whole flow. That call makes
Alexander Yashkin
2016/12/09 08:19:52
I described earlier the flow in comments to templa
vasilii
2016/12/13 17:16:39
ExtensionService::NotifyExtensionLoaded is called
Alexander Yashkin
2016/12/14 19:25:21
Regarding the point that TemplateURLService is cre
vasilii
2016/12/15 13:41:57
It's hardly possible that this will change in the
Alexander Yashkin
2016/12/15 15:47:10
Calling AddExtensionControlledTURL even before Tem
Peter Kasting
2016/12/16 08:41:06
Maybe no one thought of it? If you're asking if i
Alexander Yashkin
2016/12/16 08:45:50
Ok, I'll try to implement it.
vasilii
2016/12/16 10:22:20
I didn't implement it this way in the beginning be
Alexander Yashkin
2016/12/17 20:02:28
Updated, enable extensions search engines registra
| |
| 1966 DCHECK(!ext_id.empty()); | |
| 1967 auto ext_turl = base::MakeUnique<TemplateURL>( | |
| 1968 *data, TemplateURL::NORMAL_CONTROLLED_BY_EXTENSION); | |
| 1969 ext_turl->extension_info_ = | |
| 1970 base::MakeUnique<TemplateURL::AssociatedExtensionInfo>(ext_id); | |
| 1971 ext_turl->extension_info_->wants_to_be_default_engine = true; | |
| 1972 default_search_provider_ = AddNoNotify(std::move(ext_turl), true); | |
| 1973 } | |
| 1947 } else if (source == DefaultSearchManager::FROM_FALLBACK) { | 1974 } else if (source == DefaultSearchManager::FROM_FALLBACK) { |
| 1948 default_search_provider_ = | 1975 default_search_provider_ = |
| 1949 FindPrepopulatedTemplateURL(data->prepopulate_id); | 1976 FindPrepopulatedTemplateURL(data->prepopulate_id); |
| 1950 if (default_search_provider_) { | 1977 if (default_search_provider_) { |
| 1951 TemplateURLData update_data(*data); | 1978 TemplateURLData update_data(*data); |
| 1952 update_data.sync_guid = default_search_provider_->sync_guid(); | 1979 update_data.sync_guid = default_search_provider_->sync_guid(); |
| 1953 if (!default_search_provider_->safe_for_autoreplace()) { | 1980 if (!default_search_provider_->safe_for_autoreplace()) { |
| 1954 update_data.safe_for_autoreplace = false; | 1981 update_data.safe_for_autoreplace = false; |
| 1955 update_data.SetKeyword(default_search_provider_->keyword()); | 1982 update_data.SetKeyword(default_search_provider_->keyword()); |
| 1956 update_data.SetShortName(default_search_provider_->short_name()); | 1983 update_data.SetShortName(default_search_provider_->short_name()); |
| (...skipping 501 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 2458 const TemplateURLData& data, | 2485 const TemplateURLData& data, |
| 2459 TemplateURL::Type type) { | 2486 TemplateURL::Type type) { |
| 2460 DCHECK_NE(TemplateURL::NORMAL, type); | 2487 DCHECK_NE(TemplateURL::NORMAL, type); |
| 2461 for (const auto& turl : template_urls_) { | 2488 for (const auto& turl : template_urls_) { |
| 2462 if (turl->type() == type && | 2489 if (turl->type() == type && |
| 2463 TemplateURL::MatchesData(turl.get(), &data, search_terms_data())) | 2490 TemplateURL::MatchesData(turl.get(), &data, search_terms_data())) |
| 2464 return turl.get(); | 2491 return turl.get(); |
| 2465 } | 2492 } |
| 2466 return nullptr; | 2493 return nullptr; |
| 2467 } | 2494 } |
| 2468 | |
| 2469 void TemplateURLService::UpdateExtensionDefaultSearchEngine() { | |
| 2470 TemplateURL* most_recently_intalled_default = nullptr; | |
| 2471 for (const auto& turl : template_urls_) { | |
| 2472 if ((turl->type() == TemplateURL::NORMAL_CONTROLLED_BY_EXTENSION) && | |
| 2473 turl->extension_info_->wants_to_be_default_engine && | |
| 2474 turl->SupportsReplacement(search_terms_data()) && | |
| 2475 (!most_recently_intalled_default || | |
| 2476 (most_recently_intalled_default->extension_info_->install_time < | |
| 2477 turl->extension_info_->install_time))) | |
| 2478 most_recently_intalled_default = turl.get(); | |
| 2479 } | |
| 2480 | |
| 2481 if (most_recently_intalled_default) { | |
| 2482 base::AutoReset<DefaultSearchChangeOrigin> change_origin( | |
| 2483 &dsp_change_origin_, DSP_CHANGE_OVERRIDE_SETTINGS_EXTENSION); | |
| 2484 default_search_manager_.SetExtensionControlledDefaultSearchEngine( | |
| 2485 most_recently_intalled_default->data()); | |
| 2486 } else { | |
| 2487 default_search_manager_.ClearExtensionControlledDefaultSearchEngine(); | |
| 2488 } | |
| 2489 } | |
| OLD | NEW |