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 712 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
723 DefaultSearchManager::Source source; | 723 DefaultSearchManager::Source source; |
724 const TemplateURLData* new_dse = | 724 const TemplateURLData* new_dse = |
725 default_search_manager_.GetDefaultSearchEngine(&source); | 725 default_search_manager_.GetDefaultSearchEngine(&source); |
726 // ApplyDefaultSearchChange will notify observers once it is done. | 726 // ApplyDefaultSearchChange will notify observers once it is done. |
727 ApplyDefaultSearchChange(new_dse, source); | 727 ApplyDefaultSearchChange(new_dse, source); |
728 } else { | 728 } else { |
729 NotifyObservers(); | 729 NotifyObservers(); |
730 } | 730 } |
731 } | 731 } |
732 | 732 |
| 733 void TemplateURLService::UpdateTemplateURLVisitTime(TemplateURL* url) { |
| 734 TemplateURLData data(url->data()); |
| 735 data.last_visited = clock_->Now(); |
| 736 Update(url, TemplateURL(data)); |
| 737 } |
| 738 |
733 void TemplateURLService::AddObserver(TemplateURLServiceObserver* observer) { | 739 void TemplateURLService::AddObserver(TemplateURLServiceObserver* observer) { |
734 model_observers_.AddObserver(observer); | 740 model_observers_.AddObserver(observer); |
735 } | 741 } |
736 | 742 |
737 void TemplateURLService::RemoveObserver(TemplateURLServiceObserver* observer) { | 743 void TemplateURLService::RemoveObserver(TemplateURLServiceObserver* observer) { |
738 model_observers_.RemoveObserver(observer); | 744 model_observers_.RemoveObserver(observer); |
739 } | 745 } |
740 | 746 |
741 void TemplateURLService::Load() { | 747 void TemplateURLService::Load() { |
742 if (loaded_ || load_handle_ || disable_load_) | 748 if (loaded_ || load_handle_ || disable_load_) |
(...skipping 249 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
992 // unexpectedly swapped to something else. The user can safely delete | 998 // unexpectedly swapped to something else. The user can safely delete |
993 // the extra entry again later, if they choose. Most users who do not | 999 // the extra entry again later, if they choose. Most users who do not |
994 // look at the search engines UI will not notice this. | 1000 // look at the search engines UI will not notice this. |
995 // Note that we append a special character to the end of the keyword in | 1001 // Note that we append a special character to the end of the keyword in |
996 // an attempt to avoid a ping-poinging situation where receiving clients | 1002 // an attempt to avoid a ping-poinging situation where receiving clients |
997 // may try to continually delete the resurrected entry. | 1003 // may try to continually delete the resurrected entry. |
998 base::string16 updated_keyword = UniquifyKeyword(*existing_turl, true); | 1004 base::string16 updated_keyword = UniquifyKeyword(*existing_turl, true); |
999 TemplateURLData data(existing_turl->data()); | 1005 TemplateURLData data(existing_turl->data()); |
1000 data.SetKeyword(updated_keyword); | 1006 data.SetKeyword(updated_keyword); |
1001 TemplateURL new_turl(data); | 1007 TemplateURL new_turl(data); |
1002 if (UpdateNoNotify(existing_turl, new_turl)) | 1008 Update(existing_turl, new_turl); |
1003 NotifyObservers(); | |
1004 | 1009 |
1005 syncer::SyncData sync_data = CreateSyncDataFromTemplateURL(new_turl); | 1010 syncer::SyncData sync_data = CreateSyncDataFromTemplateURL(new_turl); |
1006 new_changes.push_back(syncer::SyncChange(FROM_HERE, | 1011 new_changes.push_back(syncer::SyncChange(FROM_HERE, |
1007 syncer::SyncChange::ACTION_ADD, | 1012 syncer::SyncChange::ACTION_ADD, |
1008 sync_data)); | 1013 sync_data)); |
1009 // Ignore the delete attempt. This means we never end up resetting the | 1014 // Ignore the delete attempt. This means we never end up resetting the |
1010 // default search provider due to an ACTION_DELETE from sync. | 1015 // default search provider due to an ACTION_DELETE from sync. |
1011 continue; | 1016 continue; |
1012 } | 1017 } |
1013 | 1018 |
(...skipping 27 matching lines...) Expand all Loading... |
1041 FROM_HERE, | 1046 FROM_HERE, |
1042 "ProcessSyncChanges failed on ChangeType ACTION_UPDATE"); | 1047 "ProcessSyncChanges failed on ChangeType ACTION_UPDATE"); |
1043 continue; | 1048 continue; |
1044 } | 1049 } |
1045 if (existing_keyword_turl && (existing_keyword_turl != existing_turl)) { | 1050 if (existing_keyword_turl && (existing_keyword_turl != existing_turl)) { |
1046 // Resolve any conflicts with other entries so we can safely update the | 1051 // Resolve any conflicts with other entries so we can safely update the |
1047 // keyword. | 1052 // keyword. |
1048 ResolveSyncKeywordConflict(turl.get(), existing_keyword_turl, | 1053 ResolveSyncKeywordConflict(turl.get(), existing_keyword_turl, |
1049 &new_changes); | 1054 &new_changes); |
1050 } | 1055 } |
1051 if (UpdateNoNotify(existing_turl, *turl)) { | 1056 if (Update(existing_turl, *turl)) |
1052 NotifyObservers(); | |
1053 MaybeUpdateDSEAfterSync(existing_turl); | 1057 MaybeUpdateDSEAfterSync(existing_turl); |
1054 } | |
1055 } else { | 1058 } else { |
1056 // We've unexpectedly received an ACTION_INVALID. | 1059 // We've unexpectedly received an ACTION_INVALID. |
1057 error = sync_error_factory_->CreateAndUploadError( | 1060 error = sync_error_factory_->CreateAndUploadError( |
1058 FROM_HERE, | 1061 FROM_HERE, |
1059 "ProcessSyncChanges received an ACTION_INVALID"); | 1062 "ProcessSyncChanges received an ACTION_INVALID"); |
1060 } | 1063 } |
1061 } | 1064 } |
1062 | 1065 |
1063 // If something went wrong, we want to prematurely exit to avoid pushing | 1066 // If something went wrong, we want to prematurely exit to avoid pushing |
1064 // inconsistent data to Sync. We return the last error we received. | 1067 // inconsistent data to Sync. We return the last error we received. |
(...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1141 DCHECK(IsFromSync(local_turl, sync_data_map)); | 1144 DCHECK(IsFromSync(local_turl, sync_data_map)); |
1142 // This local search engine is already synced. If the timestamp differs | 1145 // This local search engine is already synced. If the timestamp differs |
1143 // from Sync, we need to update locally or to the cloud. Note that if the | 1146 // from Sync, we need to update locally or to the cloud. Note that if the |
1144 // timestamps are equal, we touch neither. | 1147 // timestamps are equal, we touch neither. |
1145 if (sync_turl->last_modified() > local_turl->last_modified()) { | 1148 if (sync_turl->last_modified() > local_turl->last_modified()) { |
1146 // We've received an update from Sync. We should replace all synced | 1149 // We've received an update from Sync. We should replace all synced |
1147 // fields in the local TemplateURL. Note that this includes the | 1150 // fields in the local TemplateURL. Note that this includes the |
1148 // TemplateURLID and the TemplateURL may have to be reparsed. This | 1151 // TemplateURLID and the TemplateURL may have to be reparsed. This |
1149 // also makes the local data's last_modified timestamp equal to Sync's, | 1152 // also makes the local data's last_modified timestamp equal to Sync's, |
1150 // avoiding an Update on the next MergeData call. | 1153 // avoiding an Update on the next MergeData call. |
1151 if (UpdateNoNotify(local_turl, *sync_turl)) | 1154 Update(local_turl, *sync_turl); |
1152 NotifyObservers(); | |
1153 merge_result.set_num_items_modified( | 1155 merge_result.set_num_items_modified( |
1154 merge_result.num_items_modified() + 1); | 1156 merge_result.num_items_modified() + 1); |
1155 } else if (sync_turl->last_modified() < local_turl->last_modified()) { | 1157 } else if (sync_turl->last_modified() < local_turl->last_modified()) { |
1156 // Otherwise, we know we have newer data, so update Sync with our | 1158 // Otherwise, we know we have newer data, so update Sync with our |
1157 // data fields. | 1159 // data fields. |
1158 new_changes.push_back( | 1160 new_changes.push_back( |
1159 syncer::SyncChange(FROM_HERE, | 1161 syncer::SyncChange(FROM_HERE, |
1160 syncer::SyncChange::ACTION_UPDATE, | 1162 syncer::SyncChange::ACTION_UPDATE, |
1161 local_data_map[local_turl->sync_guid()])); | 1163 local_data_map[local_turl->sync_guid()])); |
1162 } | 1164 } |
(...skipping 572 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1735 FROM_HERE, existing_turl, syncer::SyncChange::ACTION_UPDATE); | 1737 FROM_HERE, existing_turl, syncer::SyncChange::ACTION_UPDATE); |
1736 | 1738 |
1737 if (default_search_provider_ == existing_turl && | 1739 if (default_search_provider_ == existing_turl && |
1738 default_search_provider_source_ == DefaultSearchManager::FROM_USER) { | 1740 default_search_provider_source_ == DefaultSearchManager::FROM_USER) { |
1739 default_search_manager_.SetUserSelectedDefaultSearchEngine( | 1741 default_search_manager_.SetUserSelectedDefaultSearchEngine( |
1740 default_search_provider_->data()); | 1742 default_search_provider_->data()); |
1741 } | 1743 } |
1742 return true; | 1744 return true; |
1743 } | 1745 } |
1744 | 1746 |
| 1747 bool TemplateURLService::Update(TemplateURL* existing_turl, |
| 1748 const TemplateURL& new_values) { |
| 1749 const bool updated = UpdateNoNotify(existing_turl, new_values); |
| 1750 if (updated) |
| 1751 NotifyObservers(); |
| 1752 return updated; |
| 1753 } |
| 1754 |
1745 // static | 1755 // static |
1746 void TemplateURLService::UpdateTemplateURLIfPrepopulated( | 1756 void TemplateURLService::UpdateTemplateURLIfPrepopulated( |
1747 TemplateURL* template_url, | 1757 TemplateURL* template_url, |
1748 PrefService* prefs) { | 1758 PrefService* prefs) { |
1749 int prepopulate_id = template_url->prepopulate_id(); | 1759 int prepopulate_id = template_url->prepopulate_id(); |
1750 if (template_url->prepopulate_id() == 0) | 1760 if (template_url->prepopulate_id() == 0) |
1751 return; | 1761 return; |
1752 | 1762 |
1753 size_t default_search_index; | 1763 size_t default_search_index; |
1754 std::vector<std::unique_ptr<TemplateURLData>> prepopulated_urls = | 1764 std::vector<std::unique_ptr<TemplateURLData>> prepopulated_urls = |
(...skipping 20 matching lines...) Expand all Loading... |
1775 void TemplateURLService::UpdateKeywordSearchTermsForURL( | 1785 void TemplateURLService::UpdateKeywordSearchTermsForURL( |
1776 const URLVisitedDetails& details) { | 1786 const URLVisitedDetails& details) { |
1777 if (!details.url.is_valid()) | 1787 if (!details.url.is_valid()) |
1778 return; | 1788 return; |
1779 | 1789 |
1780 const TemplateURLSet* urls_for_host = | 1790 const TemplateURLSet* urls_for_host = |
1781 provider_map_->GetURLsForHost(details.url.host()); | 1791 provider_map_->GetURLsForHost(details.url.host()); |
1782 if (!urls_for_host) | 1792 if (!urls_for_host) |
1783 return; | 1793 return; |
1784 | 1794 |
| 1795 TemplateURL* visited_url = nullptr; |
1785 for (TemplateURLSet::const_iterator i = urls_for_host->begin(); | 1796 for (TemplateURLSet::const_iterator i = urls_for_host->begin(); |
1786 i != urls_for_host->end(); ++i) { | 1797 i != urls_for_host->end(); ++i) { |
1787 base::string16 search_terms; | 1798 base::string16 search_terms; |
1788 if ((*i)->ExtractSearchTermsFromURL(details.url, search_terms_data(), | 1799 if ((*i)->ExtractSearchTermsFromURL(details.url, search_terms_data(), |
1789 &search_terms) && | 1800 &search_terms) && |
1790 !search_terms.empty()) { | 1801 !search_terms.empty()) { |
1791 if (details.is_keyword_transition) { | 1802 if (details.is_keyword_transition) { |
1792 // The visit is the result of the user entering a keyword, generate a | 1803 // The visit is the result of the user entering a keyword, generate a |
1793 // KEYWORD_GENERATED visit for the KEYWORD so that the keyword typed | 1804 // KEYWORD_GENERATED visit for the KEYWORD so that the keyword typed |
1794 // count is boosted. | 1805 // count is boosted. |
1795 AddTabToSearchVisit(**i); | 1806 AddTabToSearchVisit(**i); |
1796 } | 1807 } |
1797 if (client_) { | 1808 if (client_) { |
1798 client_->SetKeywordSearchTermsForURL( | 1809 client_->SetKeywordSearchTermsForURL( |
1799 details.url, (*i)->id(), search_terms); | 1810 details.url, (*i)->id(), search_terms); |
1800 } | 1811 } |
| 1812 // Caches the matched TemplateURL so its last_visited could be updated |
| 1813 // later after iteration. |
| 1814 // Note: UpdateNoNotify() will replace the entry from the container of |
| 1815 // this iterator, so update here directly will cause an error about it. |
| 1816 visited_url = *i; |
1801 } | 1817 } |
1802 } | 1818 } |
| 1819 if (visited_url) |
| 1820 UpdateTemplateURLVisitTime(visited_url); |
1803 } | 1821 } |
1804 | 1822 |
1805 void TemplateURLService::AddTabToSearchVisit(const TemplateURL& t_url) { | 1823 void TemplateURLService::AddTabToSearchVisit(const TemplateURL& t_url) { |
1806 // Only add visits for entries the user hasn't modified. If the user modified | 1824 // Only add visits for entries the user hasn't modified. If the user modified |
1807 // the entry the keyword may no longer correspond to the host name. It may be | 1825 // the entry the keyword may no longer correspond to the host name. It may be |
1808 // possible to do something more sophisticated here, but it's so rare as to | 1826 // possible to do something more sophisticated here, but it's so rare as to |
1809 // not be worth it. | 1827 // not be worth it. |
1810 if (!t_url.safe_for_autoreplace()) | 1828 if (!t_url.safe_for_autoreplace()) |
1811 return; | 1829 return; |
1812 | 1830 |
(...skipping 436 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2249 base::string16 new_keyword = UniquifyKeyword(*loser, false); | 2267 base::string16 new_keyword = UniquifyKeyword(*loser, false); |
2250 DCHECK(!GetTemplateURLForKeyword(new_keyword)); | 2268 DCHECK(!GetTemplateURLForKeyword(new_keyword)); |
2251 if (applied_turl_is_better) { | 2269 if (applied_turl_is_better) { |
2252 // Just set the keyword of |unapplied_sync_turl|. The caller is responsible | 2270 // Just set the keyword of |unapplied_sync_turl|. The caller is responsible |
2253 // for adding or updating unapplied_sync_turl in the local model. | 2271 // for adding or updating unapplied_sync_turl in the local model. |
2254 unapplied_sync_turl->data_.SetKeyword(new_keyword); | 2272 unapplied_sync_turl->data_.SetKeyword(new_keyword); |
2255 } else { | 2273 } else { |
2256 // Update |applied_sync_turl| in the local model with the new keyword. | 2274 // Update |applied_sync_turl| in the local model with the new keyword. |
2257 TemplateURLData data(applied_sync_turl->data()); | 2275 TemplateURLData data(applied_sync_turl->data()); |
2258 data.SetKeyword(new_keyword); | 2276 data.SetKeyword(new_keyword); |
2259 if (UpdateNoNotify(applied_sync_turl, TemplateURL(data))) | 2277 Update(applied_sync_turl, TemplateURL(data)); |
2260 NotifyObservers(); | |
2261 } | 2278 } |
2262 // The losing TemplateURL should have their keyword updated. Send a change to | 2279 // The losing TemplateURL should have their keyword updated. Send a change to |
2263 // the server to reflect this change. | 2280 // the server to reflect this change. |
2264 syncer::SyncData sync_data = CreateSyncDataFromTemplateURL(*loser); | 2281 syncer::SyncData sync_data = CreateSyncDataFromTemplateURL(*loser); |
2265 change_list->push_back(syncer::SyncChange(FROM_HERE, | 2282 change_list->push_back(syncer::SyncChange(FROM_HERE, |
2266 syncer::SyncChange::ACTION_UPDATE, | 2283 syncer::SyncChange::ACTION_UPDATE, |
2267 sync_data)); | 2284 sync_data)); |
2268 } | 2285 } |
2269 | 2286 |
2270 void TemplateURLService::MergeInSyncTemplateURL( | 2287 void TemplateURLService::MergeInSyncTemplateURL( |
(...skipping 209 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2480 | 2497 |
2481 if (most_recently_intalled_default) { | 2498 if (most_recently_intalled_default) { |
2482 base::AutoReset<DefaultSearchChangeOrigin> change_origin( | 2499 base::AutoReset<DefaultSearchChangeOrigin> change_origin( |
2483 &dsp_change_origin_, DSP_CHANGE_OVERRIDE_SETTINGS_EXTENSION); | 2500 &dsp_change_origin_, DSP_CHANGE_OVERRIDE_SETTINGS_EXTENSION); |
2484 default_search_manager_.SetExtensionControlledDefaultSearchEngine( | 2501 default_search_manager_.SetExtensionControlledDefaultSearchEngine( |
2485 most_recently_intalled_default->data()); | 2502 most_recently_intalled_default->data()); |
2486 } else { | 2503 } else { |
2487 default_search_manager_.ClearExtensionControlledDefaultSearchEngine(); | 2504 default_search_manager_.ClearExtensionControlledDefaultSearchEngine(); |
2488 } | 2505 } |
2489 } | 2506 } |
OLD | NEW |