Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(73)

Side by Side Diff: components/search_engines/template_url_service.cc

Issue 2347973002: Enable Chrome to tweak search engines for some locales (Closed)
Patch Set: compile Created 4 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 171 matching lines...) Expand 10 before | Expand all | Expand 10 after
182 // The meaningful keyword length is the length of any portion before the 182 // The meaningful keyword length is the length of any portion before the
183 // registry ("co.uk") and its preceding dot. 183 // registry ("co.uk") and its preceding dot.
184 return keyword.length() - (registry_length ? (registry_length + 1) : 0); 184 return keyword.length() - (registry_length ? (registry_length + 1) : 0);
185 } 185 }
186 186
187 bool Contains(TemplateURLService::OwnedTemplateURLVector* template_urls, 187 bool Contains(TemplateURLService::OwnedTemplateURLVector* template_urls,
188 TemplateURL* turl) { 188 TemplateURL* turl) {
189 return FindTemplateURL(template_urls, turl) != template_urls->end(); 189 return FindTemplateURL(template_urls, turl) != template_urls->end();
190 } 190 }
191 191
192 bool IsCreatedByExtension(TemplateURL* template_url) {
193 return template_url->type() ==
194 TemplateURL::NORMAL_CONTROLLED_BY_EXTENSION ||
195 template_url->type() == TemplateURL::OMNIBOX_API_EXTENSION;
196 }
197
192 } // namespace 198 } // namespace
193 199
194 // TemplateURLService::LessWithPrefix ----------------------------------------- 200 // TemplateURLService::LessWithPrefix -----------------------------------------
195 201
196 class TemplateURLService::LessWithPrefix { 202 class TemplateURLService::LessWithPrefix {
197 public: 203 public:
198 // We want to find the set of keywords that begin with a prefix. The STL 204 // We want to find the set of keywords that begin with a prefix. The STL
199 // algorithms will return the set of elements that are "equal to" the 205 // algorithms will return the set of elements that are "equal to" the
200 // prefix, where "equal(x, y)" means "!(cmp(x, y) || cmp(y, x))". When 206 // prefix, where "equal(x, y)" means "!(cmp(x, y) || cmp(y, x))". When
201 // cmp() is the typical std::less<>, this results in lexicographic equality; 207 // cmp() is the typical std::less<>, this results in lexicographic equality;
(...skipping 262 matching lines...) Expand 10 before | Expand all | Expand 10 after
464 return Add(std::move(template_url)); 470 return Add(std::move(template_url));
465 } 471 }
466 472
467 TemplateURL* TemplateURLService::AddExtensionControlledTURL( 473 TemplateURL* TemplateURLService::AddExtensionControlledTURL(
468 std::unique_ptr<TemplateURL> template_url, 474 std::unique_ptr<TemplateURL> template_url,
469 std::unique_ptr<TemplateURL::AssociatedExtensionInfo> info) { 475 std::unique_ptr<TemplateURL::AssociatedExtensionInfo> info) {
470 DCHECK(loaded_); 476 DCHECK(loaded_);
471 DCHECK(template_url); 477 DCHECK(template_url);
472 DCHECK_EQ(kInvalidTemplateURLID, template_url->id()); 478 DCHECK_EQ(kInvalidTemplateURLID, template_url->id());
473 DCHECK(info); 479 DCHECK(info);
474 DCHECK_NE(TemplateURL::NORMAL, info->type); 480 DCHECK_NE(TemplateURL::NORMAL, template_url->type());
475 DCHECK_EQ(info->wants_to_be_default_engine, 481 DCHECK_EQ(info->wants_to_be_default_engine,
476 template_url->show_in_default_list()); 482 template_url->show_in_default_list());
477 DCHECK(!FindTemplateURLForExtension(info->extension_id, info->type)); 483 DCHECK(
484 !FindTemplateURLForExtension(info->extension_id, template_url->type()));
478 template_url->extension_info_.swap(info); 485 template_url->extension_info_.swap(info);
479 486
480 KeywordWebDataService::BatchModeScoper scoper(web_data_service_.get()); 487 KeywordWebDataService::BatchModeScoper scoper(web_data_service_.get());
481 TemplateURL* template_url_ptr = AddNoNotify(std::move(template_url), true); 488 TemplateURL* template_url_ptr = AddNoNotify(std::move(template_url), true);
482 if (template_url_ptr) { 489 if (template_url_ptr) {
483 if (template_url_ptr->extension_info_->wants_to_be_default_engine) { 490 if (template_url_ptr->extension_info_->wants_to_be_default_engine) {
484 UpdateExtensionDefaultSearchEngine(); 491 UpdateExtensionDefaultSearchEngine();
485 } 492 }
486 NotifyObservers(); 493 NotifyObservers();
487 } 494 }
(...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after
554 561
555 if (FindTemplateURLForExtension(extension_id, 562 if (FindTemplateURLForExtension(extension_id,
556 TemplateURL::OMNIBOX_API_EXTENSION)) 563 TemplateURL::OMNIBOX_API_EXTENSION))
557 return; 564 return;
558 565
559 TemplateURLData data; 566 TemplateURLData data;
560 data.SetShortName(base::UTF8ToUTF16(extension_name)); 567 data.SetShortName(base::UTF8ToUTF16(extension_name));
561 data.SetKeyword(base::UTF8ToUTF16(keyword)); 568 data.SetKeyword(base::UTF8ToUTF16(keyword));
562 data.SetURL(template_url_string); 569 data.SetURL(template_url_string);
563 std::unique_ptr<TemplateURL::AssociatedExtensionInfo> info( 570 std::unique_ptr<TemplateURL::AssociatedExtensionInfo> info(
564 new TemplateURL::AssociatedExtensionInfo( 571 new TemplateURL::AssociatedExtensionInfo(extension_id));
565 TemplateURL::OMNIBOX_API_EXTENSION, extension_id)); 572 AddExtensionControlledTURL(
566 AddExtensionControlledTURL(base::MakeUnique<TemplateURL>(data), 573 base::MakeUnique<TemplateURL>(data, TemplateURL::OMNIBOX_API_EXTENSION),
567 std::move(info)); 574 std::move(info));
568 } 575 }
569 576
570 TemplateURLService::TemplateURLVector TemplateURLService::GetTemplateURLs() { 577 TemplateURLService::TemplateURLVector TemplateURLService::GetTemplateURLs() {
571 TemplateURLVector result; 578 TemplateURLVector result;
572 for (const auto& turl : template_urls_) 579 for (const auto& turl : template_urls_)
573 result.push_back(turl.get()); 580 result.push_back(turl.get());
574 return result; 581 return result;
575 } 582 }
576 583
577 void TemplateURLService::IncrementUsageCount(TemplateURL* url) { 584 void TemplateURLService::IncrementUsageCount(TemplateURL* url) {
578 DCHECK(url); 585 DCHECK(url);
579 // Extension-controlled search engines are not persisted. 586 // Extension-controlled search engines are not persisted.
580 if (url->GetType() != TemplateURL::NORMAL) 587 if (url->type() != TemplateURL::NORMAL)
581 return; 588 return;
582 if (!Contains(&template_urls_, url)) 589 if (!Contains(&template_urls_, url))
583 return; 590 return;
584 ++url->data_.usage_count; 591 ++url->data_.usage_count;
585 592
586 if (web_data_service_.get()) 593 if (web_data_service_.get())
587 web_data_service_->UpdateKeyword(url->data()); 594 web_data_service_->UpdateKeyword(url->data());
588 } 595 }
589 596
590 void TemplateURLService::ResetTemplateURL(TemplateURL* url, 597 void TemplateURLService::ResetTemplateURL(TemplateURL* url,
591 const base::string16& title, 598 const base::string16& title,
592 const base::string16& keyword, 599 const base::string16& keyword,
593 const std::string& search_url) { 600 const std::string& search_url) {
594 if (ResetTemplateURLNoNotify(url, title, keyword, search_url)) 601 if (ResetTemplateURLNoNotify(url, title, keyword, search_url))
595 NotifyObservers(); 602 NotifyObservers();
596 } 603 }
597 604
598 bool TemplateURLService::CanMakeDefault(const TemplateURL* url) { 605 bool TemplateURLService::CanMakeDefault(const TemplateURL* url) {
599 return 606 return
600 ((default_search_provider_source_ == DefaultSearchManager::FROM_USER) || 607 ((default_search_provider_source_ == DefaultSearchManager::FROM_USER) ||
601 (default_search_provider_source_ == 608 (default_search_provider_source_ ==
602 DefaultSearchManager::FROM_FALLBACK)) && 609 DefaultSearchManager::FROM_FALLBACK)) &&
603 (url != GetDefaultSearchProvider()) && 610 (url != GetDefaultSearchProvider()) &&
604 url->url_ref().SupportsReplacement(search_terms_data()) && 611 url->url_ref().SupportsReplacement(search_terms_data()) &&
605 (url->GetType() == TemplateURL::NORMAL); 612 (url->type() == TemplateURL::NORMAL);
606 } 613 }
607 614
608 void TemplateURLService::SetUserSelectedDefaultSearchProvider( 615 void TemplateURLService::SetUserSelectedDefaultSearchProvider(
609 TemplateURL* url) { 616 TemplateURL* url) {
610 // Omnibox keywords cannot be made default. Extension-controlled search 617 // Omnibox keywords cannot be made default. Extension-controlled search
611 // engines can be made default only by the extension itself because they 618 // engines can be made default only by the extension itself because they
612 // aren't persisted. 619 // aren't persisted.
613 DCHECK(!url || (url->GetType() == TemplateURL::NORMAL)); 620 DCHECK(!url || !IsCreatedByExtension(url));
614 if (load_failed_) { 621 if (load_failed_) {
615 // Skip the DefaultSearchManager, which will persist to user preferences. 622 // Skip the DefaultSearchManager, which will persist to user preferences.
616 if ((default_search_provider_source_ == DefaultSearchManager::FROM_USER) || 623 if ((default_search_provider_source_ == DefaultSearchManager::FROM_USER) ||
617 (default_search_provider_source_ == 624 (default_search_provider_source_ ==
618 DefaultSearchManager::FROM_FALLBACK)) { 625 DefaultSearchManager::FROM_FALLBACK)) {
619 ApplyDefaultSearchChange(url ? &url->data() : nullptr, 626 ApplyDefaultSearchChange(url ? &url->data() : nullptr,
620 DefaultSearchManager::FROM_USER); 627 DefaultSearchManager::FROM_USER);
621 } 628 }
622 } else { 629 } else {
623 // We rely on the DefaultSearchManager to call OnDefaultSearchChange if, in 630 // We rely on the DefaultSearchManager to call OnDefaultSearchChange if, in
(...skipping 236 matching lines...) Expand 10 before | Expand all | Expand 10 after
860 867
861 base::string16 TemplateURLService::GetKeywordShortName( 868 base::string16 TemplateURLService::GetKeywordShortName(
862 const base::string16& keyword, 869 const base::string16& keyword,
863 bool* is_omnibox_api_extension_keyword) { 870 bool* is_omnibox_api_extension_keyword) {
864 const TemplateURL* template_url = GetTemplateURLForKeyword(keyword); 871 const TemplateURL* template_url = GetTemplateURLForKeyword(keyword);
865 872
866 // TODO(sky): Once LocationBarView adds a listener to the TemplateURLService 873 // TODO(sky): Once LocationBarView adds a listener to the TemplateURLService
867 // to track changes to the model, this should become a DCHECK. 874 // to track changes to the model, this should become a DCHECK.
868 if (template_url) { 875 if (template_url) {
869 *is_omnibox_api_extension_keyword = 876 *is_omnibox_api_extension_keyword =
870 template_url->GetType() == TemplateURL::OMNIBOX_API_EXTENSION; 877 template_url->type() == TemplateURL::OMNIBOX_API_EXTENSION;
871 return template_url->AdjustedShortNameForLocaleDirection(); 878 return template_url->AdjustedShortNameForLocaleDirection();
872 } 879 }
873 *is_omnibox_api_extension_keyword = false; 880 *is_omnibox_api_extension_keyword = false;
874 return base::string16(); 881 return base::string16();
875 } 882 }
876 883
877 void TemplateURLService::OnHistoryURLVisited(const URLVisitedDetails& details) { 884 void TemplateURLService::OnHistoryURLVisited(const URLVisitedDetails& details) {
878 if (!loaded_) 885 if (!loaded_)
879 visits_to_add_.push_back(details); 886 visits_to_add_.push_back(details);
880 else 887 else
(...skipping 16 matching lines...) Expand all
897 syncer::SyncDataList TemplateURLService::GetAllSyncData( 904 syncer::SyncDataList TemplateURLService::GetAllSyncData(
898 syncer::ModelType type) const { 905 syncer::ModelType type) const {
899 DCHECK_EQ(syncer::SEARCH_ENGINES, type); 906 DCHECK_EQ(syncer::SEARCH_ENGINES, type);
900 907
901 syncer::SyncDataList current_data; 908 syncer::SyncDataList current_data;
902 for (const auto& turl : template_urls_) { 909 for (const auto& turl : template_urls_) {
903 // We don't sync keywords managed by policy. 910 // We don't sync keywords managed by policy.
904 if (turl->created_by_policy()) 911 if (turl->created_by_policy())
905 continue; 912 continue;
906 // We don't sync extension-controlled search engines. 913 // We don't sync extension-controlled search engines.
907 if (turl->GetType() != TemplateURL::NORMAL) 914 if (turl->type() != TemplateURL::NORMAL)
908 continue; 915 continue;
909 current_data.push_back(CreateSyncDataFromTemplateURL(*turl)); 916 current_data.push_back(CreateSyncDataFromTemplateURL(*turl));
910 } 917 }
911 918
912 return current_data; 919 return current_data;
913 } 920 }
914 921
915 syncer::SyncError TemplateURLService::ProcessSyncChanges( 922 syncer::SyncError TemplateURLService::ProcessSyncChanges(
916 const tracked_objects::Location& from_here, 923 const tracked_objects::Location& from_here,
917 const syncer::SyncChangeList& change_list) { 924 const syncer::SyncChangeList& change_list) {
(...skipping 285 matching lines...) Expand 10 before | Expand all | Expand 10 after
1203 return; // Not syncing. 1210 return; // Not syncing.
1204 1211
1205 if (processing_syncer_changes_) 1212 if (processing_syncer_changes_)
1206 return; // These are changes originating from us. Ignore. 1213 return; // These are changes originating from us. Ignore.
1207 1214
1208 // Avoid syncing keywords managed by policy. 1215 // Avoid syncing keywords managed by policy.
1209 if (turl->created_by_policy()) 1216 if (turl->created_by_policy())
1210 return; 1217 return;
1211 1218
1212 // Avoid syncing extension-controlled search engines. 1219 // Avoid syncing extension-controlled search engines.
1213 if (turl->GetType() == TemplateURL::NORMAL_CONTROLLED_BY_EXTENSION) 1220 if (turl->type() == TemplateURL::NORMAL_CONTROLLED_BY_EXTENSION)
1214 return; 1221 return;
1215 1222
1216 syncer::SyncChangeList changes; 1223 syncer::SyncChangeList changes;
1217 1224
1218 syncer::SyncData sync_data = CreateSyncDataFromTemplateURL(*turl); 1225 syncer::SyncData sync_data = CreateSyncDataFromTemplateURL(*turl);
1219 changes.push_back(syncer::SyncChange(from_here, 1226 changes.push_back(syncer::SyncChange(from_here,
1220 type, 1227 type,
1221 sync_data)); 1228 sync_data));
1222 1229
1223 sync_processor_->ProcessSyncChanges(FROM_HERE, changes); 1230 sync_processor_->ProcessSyncChanges(FROM_HERE, changes);
(...skipping 120 matching lines...) Expand 10 before | Expand all | Expand 10 after
1344 UpdateTemplateURLIfPrepopulated(turl.get(), prefs); 1351 UpdateTemplateURLIfPrepopulated(turl.get(), prefs);
1345 1352
1346 // We used to sync keywords associated with omnibox extensions, but no longer 1353 // We used to sync keywords associated with omnibox extensions, but no longer
1347 // want to. However, if we delete these keywords from sync, we'll break any 1354 // want to. However, if we delete these keywords from sync, we'll break any
1348 // synced old versions of Chrome which were relying on them. Instead, for now 1355 // synced old versions of Chrome which were relying on them. Instead, for now
1349 // we simply ignore these. 1356 // we simply ignore these.
1350 // TODO(vasilii): After a few Chrome versions, change this to go ahead and 1357 // TODO(vasilii): After a few Chrome versions, change this to go ahead and
1351 // delete these from sync. 1358 // delete these from sync.
1352 DCHECK(client); 1359 DCHECK(client);
1353 client->RestoreExtensionInfoIfNecessary(turl.get()); 1360 client->RestoreExtensionInfoIfNecessary(turl.get());
1354 if (turl->GetType() == TemplateURL::OMNIBOX_API_EXTENSION) 1361 if (turl->type() == TemplateURL::OMNIBOX_API_EXTENSION)
1355 return nullptr; 1362 return nullptr;
1356 1363
1357 DCHECK_EQ(TemplateURL::NORMAL, turl->GetType()); 1364 DCHECK_EQ(TemplateURL::NORMAL, turl->type());
1358 if (reset_keyword || deduped) { 1365 if (reset_keyword || deduped) {
1359 if (reset_keyword) 1366 if (reset_keyword)
1360 turl->ResetKeywordIfNecessary(search_terms_data, true); 1367 turl->ResetKeywordIfNecessary(search_terms_data, true);
1361 syncer::SyncData sync_data = CreateSyncDataFromTemplateURL(*turl); 1368 syncer::SyncData sync_data = CreateSyncDataFromTemplateURL(*turl);
1362 change_list->push_back(syncer::SyncChange(FROM_HERE, 1369 change_list->push_back(syncer::SyncChange(FROM_HERE,
1363 syncer::SyncChange::ACTION_UPDATE, 1370 syncer::SyncChange::ACTION_UPDATE,
1364 sync_data)); 1371 sync_data));
1365 } else if (turl->IsGoogleSearchURLWithReplaceableKeyword(search_terms_data)) { 1372 } else if (turl->IsGoogleSearchURLWithReplaceableKeyword(search_terms_data)) {
1366 if (!existing_turl) { 1373 if (!existing_turl) {
1367 // We're adding a new TemplateURL that uses the Google base URL, so set 1374 // We're adding a new TemplateURL that uses the Google base URL, so set
(...skipping 90 matching lines...) Expand 10 before | Expand all | Expand 10 after
1458 // extension keywords and how they can coexist with non-extension keywords. 1465 // extension keywords and how they can coexist with non-extension keywords.
1459 // In the case of more than one extension, we use the most recently 1466 // In the case of more than one extension, we use the most recently
1460 // installed (which will be the most recently added, which will have the 1467 // installed (which will be the most recently added, which will have the
1461 // highest ID). 1468 // highest ID).
1462 TemplateURL* best_fallback = nullptr; 1469 TemplateURL* best_fallback = nullptr;
1463 for (const auto& turl : template_urls_) { 1470 for (const auto& turl : template_urls_) {
1464 // This next statement relies on the fact that there can only be one 1471 // This next statement relies on the fact that there can only be one
1465 // non-Omnibox API TemplateURL with a given keyword. 1472 // non-Omnibox API TemplateURL with a given keyword.
1466 if ((turl.get() != template_url) && (turl->keyword() == keyword) && 1473 if ((turl.get() != template_url) && (turl->keyword() == keyword) &&
1467 (!best_fallback || 1474 (!best_fallback ||
1468 (best_fallback->GetType() != TemplateURL::OMNIBOX_API_EXTENSION) || 1475 (best_fallback->type() != TemplateURL::OMNIBOX_API_EXTENSION) ||
1469 ((turl->GetType() == TemplateURL::OMNIBOX_API_EXTENSION) && 1476 ((turl->type() == TemplateURL::OMNIBOX_API_EXTENSION) &&
1470 (turl->id() > best_fallback->id())))) 1477 (turl->id() > best_fallback->id()))))
1471 best_fallback = turl.get(); 1478 best_fallback = turl.get();
1472 } 1479 }
1473 RemoveFromDomainMap(template_url); 1480 RemoveFromDomainMap(template_url);
1474 if (best_fallback) { 1481 if (best_fallback) {
1475 AddToMap(best_fallback); 1482 AddToMap(best_fallback);
1476 AddToDomainMap(best_fallback); 1483 AddToDomainMap(best_fallback);
1477 } else { 1484 } else {
1478 keyword_to_turl_and_length_.erase(keyword); 1485 keyword_to_turl_and_length_.erase(keyword);
1479 } 1486 }
1480 } 1487 }
1481 1488
1482 if (template_url->GetType() == TemplateURL::OMNIBOX_API_EXTENSION) 1489 if (template_url->type() == TemplateURL::OMNIBOX_API_EXTENSION)
1483 return; 1490 return;
1484 1491
1485 if (!template_url->sync_guid().empty()) 1492 if (!template_url->sync_guid().empty())
1486 guid_to_turl_.erase(template_url->sync_guid()); 1493 guid_to_turl_.erase(template_url->sync_guid());
1487 // |provider_map_| is only initialized after loading has completed. 1494 // |provider_map_| is only initialized after loading has completed.
1488 if (loaded_) { 1495 if (loaded_) {
1489 provider_map_->Remove(template_url); 1496 provider_map_->Remove(template_url);
1490 } 1497 }
1491 } 1498 }
1492 1499
1493 void TemplateURLService::AddToMaps(TemplateURL* template_url) { 1500 void TemplateURLService::AddToMaps(TemplateURL* template_url) {
1494 bool template_url_is_omnibox_api = 1501 bool template_url_is_omnibox_api =
1495 template_url->GetType() == TemplateURL::OMNIBOX_API_EXTENSION; 1502 template_url->type() == TemplateURL::OMNIBOX_API_EXTENSION;
1496 const base::string16& keyword = template_url->keyword(); 1503 const base::string16& keyword = template_url->keyword();
1497 KeywordToTURLAndMeaningfulLength::const_iterator i = 1504 KeywordToTURLAndMeaningfulLength::const_iterator i =
1498 keyword_to_turl_and_length_.find(keyword); 1505 keyword_to_turl_and_length_.find(keyword);
1499 if (i == keyword_to_turl_and_length_.end()) { 1506 if (i == keyword_to_turl_and_length_.end()) {
1500 AddToMap(template_url); 1507 AddToMap(template_url);
1501 AddToDomainMap(template_url); 1508 AddToDomainMap(template_url);
1502 } else { 1509 } else {
1503 const TemplateURL* existing_url = i->second.first; 1510 const TemplateURL* existing_url = i->second.first;
1504 // We should only have overlapping keywords when at least one comes from 1511 // We should only have overlapping keywords when at least one comes from
1505 // an extension. In that case, the ranking order is: 1512 // an extension. In that case, the ranking order is:
1506 // Manually-modified keywords > extension keywords > replaceable keywords 1513 // Manually-modified keywords > extension keywords > replaceable keywords
1507 // When there are multiple extensions, the last-added wins. 1514 // When there are multiple extensions, the last-added wins.
1508 bool existing_url_is_omnibox_api = 1515 bool existing_url_is_omnibox_api =
1509 existing_url->GetType() == TemplateURL::OMNIBOX_API_EXTENSION; 1516 existing_url->type() == TemplateURL::OMNIBOX_API_EXTENSION;
1510 DCHECK(existing_url_is_omnibox_api || template_url_is_omnibox_api); 1517 DCHECK(existing_url_is_omnibox_api || template_url_is_omnibox_api);
1511 if (existing_url_is_omnibox_api ? 1518 if (existing_url_is_omnibox_api ?
1512 !CanReplace(template_url) : CanReplace(existing_url)) { 1519 !CanReplace(template_url) : CanReplace(existing_url)) {
1513 RemoveFromDomainMap(existing_url); 1520 RemoveFromDomainMap(existing_url);
1514 AddToMap(template_url); 1521 AddToMap(template_url);
1515 AddToDomainMap(template_url); 1522 AddToDomainMap(template_url);
1516 } 1523 }
1517 } 1524 }
1518 1525
1519 if (template_url_is_omnibox_api) 1526 if (template_url_is_omnibox_api)
(...skipping 113 matching lines...) Expand 10 before | Expand all | Expand 10 after
1633 } 1640 }
1634 1641
1635 bool TemplateURLService::CanReplace(const TemplateURL* t_url) { 1642 bool TemplateURLService::CanReplace(const TemplateURL* t_url) {
1636 return (t_url != default_search_provider_ && !t_url->show_in_default_list() && 1643 return (t_url != default_search_provider_ && !t_url->show_in_default_list() &&
1637 t_url->safe_for_autoreplace()); 1644 t_url->safe_for_autoreplace());
1638 } 1645 }
1639 1646
1640 TemplateURL* TemplateURLService::FindNonExtensionTemplateURLForKeyword( 1647 TemplateURL* TemplateURLService::FindNonExtensionTemplateURLForKeyword(
1641 const base::string16& keyword) { 1648 const base::string16& keyword) {
1642 TemplateURL* keyword_turl = GetTemplateURLForKeyword(keyword); 1649 TemplateURL* keyword_turl = GetTemplateURLForKeyword(keyword);
1643 if (!keyword_turl || (keyword_turl->GetType() == TemplateURL::NORMAL)) 1650 if (!keyword_turl || (keyword_turl->type() == TemplateURL::NORMAL))
1644 return keyword_turl; 1651 return keyword_turl;
1645 // The extension keyword in the model may be hiding a replaceable 1652 // The extension keyword in the model may be hiding a replaceable
1646 // non-extension keyword. Look for it. 1653 // non-extension keyword. Look for it.
1647 for (const auto& turl : template_urls_) { 1654 for (const auto& turl : template_urls_) {
1648 if ((turl->GetType() == TemplateURL::NORMAL) && 1655 if ((turl->type() == TemplateURL::NORMAL) &&
1649 (turl->keyword() == keyword)) 1656 (turl->keyword() == keyword))
1650 return turl.get(); 1657 return turl.get();
1651 } 1658 }
1652 return nullptr; 1659 return nullptr;
1653 } 1660 }
1654 1661
1655 bool TemplateURLService::UpdateNoNotify(TemplateURL* existing_turl, 1662 bool TemplateURLService::UpdateNoNotify(TemplateURL* existing_turl,
1656 const TemplateURL& new_values) { 1663 const TemplateURL& new_values) {
1657 DCHECK(existing_turl); 1664 DCHECK(existing_turl);
1658 if (!Contains(&template_urls_, existing_turl)) 1665 if (!Contains(&template_urls_, existing_turl))
1659 return false; 1666 return false;
1660 1667
1661 DCHECK_NE(TemplateURL::OMNIBOX_API_EXTENSION, existing_turl->GetType()); 1668 DCHECK_NE(TemplateURL::OMNIBOX_API_EXTENSION, existing_turl->type());
1662 1669
1663 base::string16 old_keyword(existing_turl->keyword()); 1670 base::string16 old_keyword(existing_turl->keyword());
1664 keyword_to_turl_and_length_.erase(old_keyword); 1671 keyword_to_turl_and_length_.erase(old_keyword);
1665 RemoveFromDomainMap(existing_turl); 1672 RemoveFromDomainMap(existing_turl);
1666 if (!existing_turl->sync_guid().empty()) 1673 if (!existing_turl->sync_guid().empty())
1667 guid_to_turl_.erase(existing_turl->sync_guid()); 1674 guid_to_turl_.erase(existing_turl->sync_guid());
1668 1675
1669 // |provider_map_| is only initialized after loading has completed. 1676 // |provider_map_| is only initialized after loading has completed.
1670 if (loaded_) 1677 if (loaded_)
1671 provider_map_->Remove(existing_turl); 1678 provider_map_->Remove(existing_turl);
(...skipping 15 matching lines...) Expand all
1687 } else { 1694 } else {
1688 // We can theoretically reach here in two cases: 1695 // We can theoretically reach here in two cases:
1689 // * There is an existing extension keyword and sync brings in a rename of 1696 // * There is an existing extension keyword and sync brings in a rename of
1690 // a non-extension keyword to match. In this case we just need to pick 1697 // a non-extension keyword to match. In this case we just need to pick
1691 // which keyword has priority to update the keyword map. 1698 // which keyword has priority to update the keyword map.
1692 // * Autogeneration of the keyword for a Google default search provider 1699 // * Autogeneration of the keyword for a Google default search provider
1693 // at load time causes it to conflict with an existing keyword. In this 1700 // at load time causes it to conflict with an existing keyword. In this
1694 // case we delete the existing keyword if it's replaceable, or else undo 1701 // case we delete the existing keyword if it's replaceable, or else undo
1695 // the change in keyword for |existing_turl|. 1702 // the change in keyword for |existing_turl|.
1696 TemplateURL* existing_keyword_turl = i->second.first; 1703 TemplateURL* existing_keyword_turl = i->second.first;
1697 if (existing_keyword_turl->GetType() != TemplateURL::NORMAL) { 1704 if (existing_keyword_turl->type() != TemplateURL::NORMAL) {
1698 if (!CanReplace(existing_turl)) { 1705 if (!CanReplace(existing_turl)) {
1699 AddToMap(existing_turl); 1706 AddToMap(existing_turl);
1700 AddToDomainMap(existing_turl); 1707 AddToDomainMap(existing_turl);
1701 } 1708 }
1702 } else { 1709 } else {
1703 if (CanReplace(existing_keyword_turl)) { 1710 if (CanReplace(existing_keyword_turl)) {
1704 RemoveNoNotify(existing_keyword_turl); 1711 RemoveNoNotify(existing_keyword_turl);
1705 } else { 1712 } else {
1706 existing_turl->data_.SetKeyword(old_keyword); 1713 existing_turl->data_.SetKeyword(old_keyword);
1707 AddToMap(existing_turl); 1714 AddToMap(existing_turl);
(...skipping 299 matching lines...) Expand 10 before | Expand all | Expand 10 after
2007 2014
2008 // Check whether |template_url|'s keyword conflicts with any already in the 2015 // Check whether |template_url|'s keyword conflicts with any already in the
2009 // model. Note that we can reach here during the loading phase while 2016 // model. Note that we can reach here during the loading phase while
2010 // processing the template URLs from the web data service. In this case, 2017 // processing the template URLs from the web data service. In this case,
2011 // GetTemplateURLForKeyword() will look not only at what's already in the 2018 // GetTemplateURLForKeyword() will look not only at what's already in the
2012 // model, but at the |initial_default_search_provider_|. Since this engine 2019 // model, but at the |initial_default_search_provider_|. Since this engine
2013 // will presumably also be present in the web data, we need to double-check 2020 // will presumably also be present in the web data, we need to double-check
2014 // that any "pre-existing" entries we find are actually coming from 2021 // that any "pre-existing" entries we find are actually coming from
2015 // |template_urls_|, lest we detect a "conflict" between the 2022 // |template_urls_|, lest we detect a "conflict" between the
2016 // |initial_default_search_provider_| and the web data version of itself. 2023 // |initial_default_search_provider_| and the web data version of itself.
2017 if (template_url->GetType() != TemplateURL::OMNIBOX_API_EXTENSION && 2024 if (template_url->type() != TemplateURL::OMNIBOX_API_EXTENSION &&
2018 existing_keyword_turl && 2025 existing_keyword_turl &&
2019 existing_keyword_turl->GetType() != TemplateURL::OMNIBOX_API_EXTENSION && 2026 existing_keyword_turl->type() != TemplateURL::OMNIBOX_API_EXTENSION &&
2020 Contains(&template_urls_, existing_keyword_turl)) { 2027 Contains(&template_urls_, existing_keyword_turl)) {
2021 DCHECK_NE(existing_keyword_turl, template_url.get()); 2028 DCHECK_NE(existing_keyword_turl, template_url.get());
2022 // Only replace one of the TemplateURLs if they are either both extensions, 2029 // Only replace one of the TemplateURLs if they are either both extensions,
2023 // or both not extensions. 2030 // or both not extensions.
2024 bool are_same_type = existing_keyword_turl->GetType() == 2031 bool are_same_type = IsCreatedByExtension(existing_keyword_turl) ==
2025 template_url->GetType(); 2032 IsCreatedByExtension(template_url.get());
2026 if (CanReplace(existing_keyword_turl) && are_same_type) { 2033 if (CanReplace(existing_keyword_turl) && are_same_type) {
2027 RemoveNoNotify(existing_keyword_turl); 2034 RemoveNoNotify(existing_keyword_turl);
2028 } else if (CanReplace(template_url.get()) && are_same_type) { 2035 } else if (CanReplace(template_url.get()) && are_same_type) {
2029 return nullptr; 2036 return nullptr;
2030 } else { 2037 } else {
2031 base::string16 new_keyword = 2038 base::string16 new_keyword =
2032 UniquifyKeyword(*existing_keyword_turl, false); 2039 UniquifyKeyword(*existing_keyword_turl, false);
2033 ResetTemplateURLNoNotify(existing_keyword_turl, 2040 ResetTemplateURLNoNotify(existing_keyword_turl,
2034 existing_keyword_turl->short_name(), new_keyword, 2041 existing_keyword_turl->short_name(), new_keyword,
2035 existing_keyword_turl->url()); 2042 existing_keyword_turl->url());
2036 } 2043 }
2037 } 2044 }
2038 TemplateURL* template_url_ptr = template_url.get(); 2045 TemplateURL* template_url_ptr = template_url.get();
2039 template_urls_.push_back(std::move(template_url)); 2046 template_urls_.push_back(std::move(template_url));
2040 AddToMaps(template_url_ptr); 2047 AddToMaps(template_url_ptr);
2041 2048
2042 if (newly_adding && (template_url_ptr->GetType() == TemplateURL::NORMAL)) { 2049 if (newly_adding && (template_url_ptr->type() == TemplateURL::NORMAL)) {
2043 if (web_data_service_.get()) 2050 if (web_data_service_.get())
2044 web_data_service_->AddKeyword(template_url_ptr->data()); 2051 web_data_service_->AddKeyword(template_url_ptr->data());
2045 2052
2046 // Inform sync of the addition. Note that this will assign a GUID to 2053 // Inform sync of the addition. Note that this will assign a GUID to
2047 // template_url and add it to the guid_to_turl_. 2054 // template_url and add it to the guid_to_turl_.
2048 ProcessTemplateURLChange(FROM_HERE, template_url_ptr, 2055 ProcessTemplateURLChange(FROM_HERE, template_url_ptr,
2049 syncer::SyncChange::ACTION_ADD); 2056 syncer::SyncChange::ACTION_ADD);
2050 } 2057 }
2051 2058
2052 return template_url_ptr; 2059 return template_url_ptr;
2053 } 2060 }
2054 2061
2055 void TemplateURLService::RemoveNoNotify(TemplateURL* template_url) { 2062 void TemplateURLService::RemoveNoNotify(TemplateURL* template_url) {
2056 DCHECK(template_url != default_search_provider_); 2063 DCHECK(template_url != default_search_provider_);
2057 2064
2058 auto i = FindTemplateURL(&template_urls_, template_url); 2065 auto i = FindTemplateURL(&template_urls_, template_url);
2059 if (i == template_urls_.end()) 2066 if (i == template_urls_.end())
2060 return; 2067 return;
2061 2068
2062 RemoveFromMaps(template_url); 2069 RemoveFromMaps(template_url);
2063 2070
2064 // Remove it from the vector containing all TemplateURLs. 2071 // Remove it from the vector containing all TemplateURLs.
2065 std::unique_ptr<TemplateURL> scoped_turl = std::move(*i); 2072 std::unique_ptr<TemplateURL> scoped_turl = std::move(*i);
2066 template_urls_.erase(i); 2073 template_urls_.erase(i);
2067 2074
2068 if (template_url->GetType() == TemplateURL::NORMAL) { 2075 if (template_url->type() == TemplateURL::NORMAL) {
2069 if (web_data_service_.get()) 2076 if (web_data_service_.get())
2070 web_data_service_->RemoveKeyword(template_url->id()); 2077 web_data_service_->RemoveKeyword(template_url->id());
2071 2078
2072 // Inform sync of the deletion. 2079 // Inform sync of the deletion.
2073 ProcessTemplateURLChange(FROM_HERE, 2080 ProcessTemplateURLChange(FROM_HERE,
2074 template_url, 2081 template_url,
2075 syncer::SyncChange::ACTION_DELETE); 2082 syncer::SyncChange::ACTION_DELETE);
2076 2083
2077 UMA_HISTOGRAM_ENUMERATION(kDeleteSyncedEngineHistogramName, 2084 UMA_HISTOGRAM_ENUMERATION(kDeleteSyncedEngineHistogramName,
2078 DELETE_ENGINE_USER_ACTION, DELETE_ENGINE_MAX); 2085 DELETE_ENGINE_USER_ACTION, DELETE_ENGINE_MAX);
(...skipping 99 matching lines...) Expand 10 before | Expand all | Expand 10 after
2178 bool force) { 2185 bool force) {
2179 if (!force) { 2186 if (!force) {
2180 // Already unique. 2187 // Already unique.
2181 if (!GetTemplateURLForKeyword(turl.keyword())) 2188 if (!GetTemplateURLForKeyword(turl.keyword()))
2182 return turl.keyword(); 2189 return turl.keyword();
2183 2190
2184 // First, try to return the generated keyword for the TemplateURL (except 2191 // First, try to return the generated keyword for the TemplateURL (except
2185 // for extensions, as their keywords are not associated with their URLs). 2192 // for extensions, as their keywords are not associated with their URLs).
2186 GURL gurl(turl.url()); 2193 GURL gurl(turl.url());
2187 if (gurl.is_valid() && 2194 if (gurl.is_valid() &&
2188 (turl.GetType() != TemplateURL::OMNIBOX_API_EXTENSION)) { 2195 (turl.type() != TemplateURL::OMNIBOX_API_EXTENSION)) {
2189 base::string16 keyword_candidate = TemplateURL::GenerateKeyword(gurl); 2196 base::string16 keyword_candidate = TemplateURL::GenerateKeyword(gurl);
2190 if (!GetTemplateURLForKeyword(keyword_candidate)) 2197 if (!GetTemplateURLForKeyword(keyword_candidate))
2191 return keyword_candidate; 2198 return keyword_candidate;
2192 } 2199 }
2193 } 2200 }
2194 2201
2195 // We try to uniquify the keyword by appending a special character to the end. 2202 // We try to uniquify the keyword by appending a special character to the end.
2196 // This is a best-effort approach where we try to preserve the original 2203 // This is a best-effort approach where we try to preserve the original
2197 // keyword and let the user do what they will after our attempt. 2204 // keyword and let the user do what they will after our attempt.
2198 base::string16 keyword_candidate(turl.keyword()); 2205 base::string16 keyword_candidate(turl.keyword());
(...skipping 15 matching lines...) Expand all
2214 2221
2215 void TemplateURLService::ResolveSyncKeywordConflict( 2222 void TemplateURLService::ResolveSyncKeywordConflict(
2216 TemplateURL* unapplied_sync_turl, 2223 TemplateURL* unapplied_sync_turl,
2217 TemplateURL* applied_sync_turl, 2224 TemplateURL* applied_sync_turl,
2218 syncer::SyncChangeList* change_list) { 2225 syncer::SyncChangeList* change_list) {
2219 DCHECK(loaded_); 2226 DCHECK(loaded_);
2220 DCHECK(unapplied_sync_turl); 2227 DCHECK(unapplied_sync_turl);
2221 DCHECK(applied_sync_turl); 2228 DCHECK(applied_sync_turl);
2222 DCHECK(change_list); 2229 DCHECK(change_list);
2223 DCHECK_EQ(applied_sync_turl->keyword(), unapplied_sync_turl->keyword()); 2230 DCHECK_EQ(applied_sync_turl->keyword(), unapplied_sync_turl->keyword());
2224 DCHECK_EQ(TemplateURL::NORMAL, applied_sync_turl->GetType()); 2231 DCHECK_EQ(TemplateURL::NORMAL, applied_sync_turl->type());
2225 2232
2226 // Both |unapplied_sync_turl| and |applied_sync_turl| are known to Sync, so 2233 // Both |unapplied_sync_turl| and |applied_sync_turl| are known to Sync, so
2227 // don't delete either of them. Instead, determine which is "better" and 2234 // don't delete either of them. Instead, determine which is "better" and
2228 // uniquify the other one, sending an update to the server for the updated 2235 // uniquify the other one, sending an update to the server for the updated
2229 // entry. 2236 // entry.
2230 const bool applied_turl_is_better = 2237 const bool applied_turl_is_better =
2231 IsLocalTemplateURLBetter(applied_sync_turl, unapplied_sync_turl); 2238 IsLocalTemplateURLBetter(applied_sync_turl, unapplied_sync_turl);
2232 TemplateURL* loser = applied_turl_is_better ? 2239 TemplateURL* loser = applied_turl_is_better ?
2233 unapplied_sync_turl : applied_sync_turl; 2240 unapplied_sync_turl : applied_sync_turl;
2234 base::string16 new_keyword = UniquifyKeyword(*loser, false); 2241 base::string16 new_keyword = UniquifyKeyword(*loser, false);
(...skipping 124 matching lines...) Expand 10 before | Expand all | Expand 10 after
2359 merge_result->set_num_items_added(merge_result->num_items_added() + 1); 2366 merge_result->set_num_items_added(merge_result->num_items_added() + 1);
2360 } 2367 }
2361 } 2368 }
2362 2369
2363 void TemplateURLService::PatchMissingSyncGUIDs( 2370 void TemplateURLService::PatchMissingSyncGUIDs(
2364 OwnedTemplateURLVector* template_urls) { 2371 OwnedTemplateURLVector* template_urls) {
2365 DCHECK(template_urls); 2372 DCHECK(template_urls);
2366 for (auto& template_url : *template_urls) { 2373 for (auto& template_url : *template_urls) {
2367 DCHECK(template_url); 2374 DCHECK(template_url);
2368 if (template_url->sync_guid().empty() && 2375 if (template_url->sync_guid().empty() &&
2369 (template_url->GetType() == TemplateURL::NORMAL)) { 2376 (template_url->type() == TemplateURL::NORMAL)) {
2370 template_url->data_.sync_guid = base::GenerateGUID(); 2377 template_url->data_.sync_guid = base::GenerateGUID();
2371 if (web_data_service_.get()) 2378 if (web_data_service_.get())
2372 web_data_service_->UpdateKeyword(template_url->data()); 2379 web_data_service_->UpdateKeyword(template_url->data());
2373 } 2380 }
2374 } 2381 }
2375 } 2382 }
2376 2383
2377 void TemplateURLService::OnSyncedDefaultSearchProviderGUIDChanged() { 2384 void TemplateURLService::OnSyncedDefaultSearchProviderGUIDChanged() {
2378 base::AutoReset<DefaultSearchChangeOrigin> change_origin( 2385 base::AutoReset<DefaultSearchChangeOrigin> change_origin(
2379 &dsp_change_origin_, DSP_CHANGE_SYNC_PREF); 2386 &dsp_change_origin_, DSP_CHANGE_SYNC_PREF);
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after
2426 return turl.get(); 2433 return turl.get();
2427 } 2434 }
2428 return nullptr; 2435 return nullptr;
2429 } 2436 }
2430 2437
2431 TemplateURL* TemplateURLService::FindTemplateURLForExtension( 2438 TemplateURL* TemplateURLService::FindTemplateURLForExtension(
2432 const std::string& extension_id, 2439 const std::string& extension_id,
2433 TemplateURL::Type type) { 2440 TemplateURL::Type type) {
2434 DCHECK_NE(TemplateURL::NORMAL, type); 2441 DCHECK_NE(TemplateURL::NORMAL, type);
2435 for (const auto& turl : template_urls_) { 2442 for (const auto& turl : template_urls_) {
2436 if (turl->GetType() == type && turl->GetExtensionId() == extension_id) 2443 if (turl->type() == type && turl->GetExtensionId() == extension_id)
2437 return turl.get(); 2444 return turl.get();
2438 } 2445 }
2439 return nullptr; 2446 return nullptr;
2440 } 2447 }
2441 2448
2442 TemplateURL* TemplateURLService::FindMatchingExtensionTemplateURL( 2449 TemplateURL* TemplateURLService::FindMatchingExtensionTemplateURL(
2443 const TemplateURLData& data, 2450 const TemplateURLData& data,
2444 TemplateURL::Type type) { 2451 TemplateURL::Type type) {
2445 DCHECK_NE(TemplateURL::NORMAL, type); 2452 DCHECK_NE(TemplateURL::NORMAL, type);
2446 for (const auto& turl : template_urls_) { 2453 for (const auto& turl : template_urls_) {
2447 if (turl->GetType() == type && 2454 if (turl->type() == type &&
2448 TemplateURL::MatchesData(turl.get(), &data, search_terms_data())) 2455 TemplateURL::MatchesData(turl.get(), &data, search_terms_data()))
2449 return turl.get(); 2456 return turl.get();
2450 } 2457 }
2451 return nullptr; 2458 return nullptr;
2452 } 2459 }
2453 2460
2454 void TemplateURLService::UpdateExtensionDefaultSearchEngine() { 2461 void TemplateURLService::UpdateExtensionDefaultSearchEngine() {
2455 TemplateURL* most_recently_intalled_default = nullptr; 2462 TemplateURL* most_recently_intalled_default = nullptr;
2456 for (const auto& turl : template_urls_) { 2463 for (const auto& turl : template_urls_) {
2457 if ((turl->GetType() == TemplateURL::NORMAL_CONTROLLED_BY_EXTENSION) && 2464 if ((turl->type() == TemplateURL::NORMAL_CONTROLLED_BY_EXTENSION) &&
2458 turl->extension_info_->wants_to_be_default_engine && 2465 turl->extension_info_->wants_to_be_default_engine &&
2459 turl->SupportsReplacement(search_terms_data()) && 2466 turl->SupportsReplacement(search_terms_data()) &&
2460 (!most_recently_intalled_default || 2467 (!most_recently_intalled_default ||
2461 (most_recently_intalled_default->extension_info_->install_time < 2468 (most_recently_intalled_default->extension_info_->install_time <
2462 turl->extension_info_->install_time))) 2469 turl->extension_info_->install_time)))
2463 most_recently_intalled_default = turl.get(); 2470 most_recently_intalled_default = turl.get();
2464 } 2471 }
2465 2472
2466 if (most_recently_intalled_default) { 2473 if (most_recently_intalled_default) {
2467 base::AutoReset<DefaultSearchChangeOrigin> change_origin( 2474 base::AutoReset<DefaultSearchChangeOrigin> change_origin(
2468 &dsp_change_origin_, DSP_CHANGE_OVERRIDE_SETTINGS_EXTENSION); 2475 &dsp_change_origin_, DSP_CHANGE_OVERRIDE_SETTINGS_EXTENSION);
2469 default_search_manager_.SetExtensionControlledDefaultSearchEngine( 2476 default_search_manager_.SetExtensionControlledDefaultSearchEngine(
2470 most_recently_intalled_default->data()); 2477 most_recently_intalled_default->data());
2471 } else { 2478 } else {
2472 default_search_manager_.ClearExtensionControlledDefaultSearchEngine(); 2479 default_search_manager_.ClearExtensionControlledDefaultSearchEngine();
2473 } 2480 }
2474 } 2481 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698