Chromium Code Reviews| Index: components/search_engines/template_url_service.cc |
| diff --git a/components/search_engines/template_url_service.cc b/components/search_engines/template_url_service.cc |
| index 4d7a28455a200cfcd4ac1674cdd7b72b758b6b31..4ba8cbf9bda68bf3fd2c8b3c69beb919a38fa73b 100644 |
| --- a/components/search_engines/template_url_service.cc |
| +++ b/components/search_engines/template_url_service.cc |
| @@ -489,16 +489,28 @@ TemplateURL* TemplateURLService::AddExtensionControlledTURL( |
| DCHECK_EQ(kInvalidTemplateURLID, template_url->id()); |
| DCHECK(info); |
| DCHECK_NE(TemplateURL::NORMAL, template_url->type()); |
| - DCHECK( |
| - !FindTemplateURLForExtension(info->extension_id, template_url->type())); |
| + // It is possible that extension template url is already created for extension |
| + // that is currently overriding default search, before extension itself is |
| + // loaded. This is the case when extension overrides DSE in preferences and |
| + // extension template url is created when TemplateUrlService changes to loaded |
| + // state but before extensions are loaded. |
| + TemplateURL* ext_dse = |
| + FindTemplateURLForExtension(info->extension_id, template_url->type()); |
| + if (template_url->type() == TemplateURL::NORMAL_CONTROLLED_BY_EXTENSION && |
| + 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.
|
| + DCHECK(info->wants_to_be_default_engine); |
| + // They must be similar at least. |
| + 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
|
| + DCHECK(ext_dse->data().short_name() == template_url->data().short_name()); |
| + DCHECK(ext_dse->data().url() == template_url->data().url()); |
| + return ext_dse; |
| + } |
| + |
| template_url->extension_info_.swap(info); |
| KeywordWebDataService::BatchModeScoper scoper(web_data_service_.get()); |
| TemplateURL* template_url_ptr = AddNoNotify(std::move(template_url), true); |
| if (template_url_ptr) { |
| - if (template_url_ptr->extension_info_->wants_to_be_default_engine) { |
| - UpdateExtensionDefaultSearchEngine(); |
| - } |
| NotifyObservers(); |
| } |
| @@ -518,12 +530,10 @@ void TemplateURLService::RemoveExtensionControlledTURL( |
| if (!url) |
| return; |
| // NULL this out so that we can call RemoveNoNotify. |
| - // UpdateExtensionDefaultSearchEngine will cause it to be reset. |
| if (default_search_provider_ == url) |
| default_search_provider_ = nullptr; |
| KeywordWebDataService::BatchModeScoper scoper(web_data_service_.get()); |
| RemoveNoNotify(url); |
| - UpdateExtensionDefaultSearchEngine(); |
| NotifyObservers(); |
| } |
| @@ -1944,6 +1954,23 @@ bool TemplateURLService::ApplyDefaultSearchChangeNoMetrics( |
| } else if (source == DefaultSearchManager::FROM_EXTENSION) { |
| default_search_provider_ = FindMatchingExtensionTemplateURL( |
| *data, TemplateURL::NORMAL_CONTROLLED_BY_EXTENSION); |
| + if (!default_search_provider_) { |
| + // This is possible if extension has installed DSE, writing extension |
| + // controlled pref and extension service is not yet loaded. Create |
| + // extension template url from preference value and add it. Later when |
| + // extension will load, it will again register its DSE in template url |
| + // service. |
| + |
| + // Get id of extension that controls preference. |
| + 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
|
| + DCHECK(!ext_id.empty()); |
| + auto ext_turl = base::MakeUnique<TemplateURL>( |
| + *data, TemplateURL::NORMAL_CONTROLLED_BY_EXTENSION); |
| + ext_turl->extension_info_ = |
| + base::MakeUnique<TemplateURL::AssociatedExtensionInfo>(ext_id); |
| + ext_turl->extension_info_->wants_to_be_default_engine = true; |
| + default_search_provider_ = AddNoNotify(std::move(ext_turl), true); |
| + } |
| } else if (source == DefaultSearchManager::FROM_FALLBACK) { |
| default_search_provider_ = |
| FindPrepopulatedTemplateURL(data->prepopulate_id); |
| @@ -2465,25 +2492,3 @@ TemplateURL* TemplateURLService::FindMatchingExtensionTemplateURL( |
| } |
| return nullptr; |
| } |
| - |
| -void TemplateURLService::UpdateExtensionDefaultSearchEngine() { |
| - TemplateURL* most_recently_intalled_default = nullptr; |
| - for (const auto& turl : template_urls_) { |
| - if ((turl->type() == TemplateURL::NORMAL_CONTROLLED_BY_EXTENSION) && |
| - turl->extension_info_->wants_to_be_default_engine && |
| - turl->SupportsReplacement(search_terms_data()) && |
| - (!most_recently_intalled_default || |
| - (most_recently_intalled_default->extension_info_->install_time < |
| - turl->extension_info_->install_time))) |
| - most_recently_intalled_default = turl.get(); |
| - } |
| - |
| - if (most_recently_intalled_default) { |
| - base::AutoReset<DefaultSearchChangeOrigin> change_origin( |
| - &dsp_change_origin_, DSP_CHANGE_OVERRIDE_SETTINGS_EXTENSION); |
| - default_search_manager_.SetExtensionControlledDefaultSearchEngine( |
| - most_recently_intalled_default->data()); |
| - } else { |
| - default_search_manager_.ClearExtensionControlledDefaultSearchEngine(); |
| - } |
| -} |