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
3 // found in the LICENSE file.
4 
5 #ifndef COMPONENTS_SEARCH_ENGINES_TEMPLATE_URL_SERVICE_H_
6 #define COMPONENTS_SEARCH_ENGINES_TEMPLATE_URL_SERVICE_H_
7 
8 #include <stddef.h>
9 
10 #include <map>
11 #include <memory>
12 #include <set>
13 #include <string>
14 #include <utility>
15 #include <vector>
16 
17 #include "base/callback_list.h"
18 #include "base/gtest_prod_util.h"
19 #include "base/macros.h"
20 #include "base/memory/weak_ptr.h"
21 #include "base/observer_list.h"
22 #include "base/time/default_clock.h"
23 #include "build/build_config.h"
24 #include "components/keyed_service/core/keyed_service.h"
25 #include "components/prefs/pref_change_registrar.h"
26 #include "components/search_engines/default_search_manager.h"
27 #include "components/search_engines/keyword_web_data_service.h"
28 #include "components/search_engines/search_host_to_urls_map.h"
29 #include "components/search_engines/search_terms_data.h"
30 #include "components/search_engines/template_url.h"
31 #include "components/sync/model/sync_change.h"
32 #include "components/sync/model/syncable_service.h"
33 #include "components/webdata/common/web_data_service_consumer.h"
34 #if defined(OS_ANDROID)
35 #include "base/android/scoped_java_ref.h"
36 #endif
37 
38 class GURL;
39 class PrefService;
40 class TemplateURLServiceClient;
41 class TemplateURLServiceObserver;
42 struct TemplateURLData;
43 #if defined(OS_ANDROID)
44 class TemplateUrlServiceAndroid;
45 #endif
46 
47 namespace syncer {
48 class SyncData;
49 class SyncErrorFactory;
50 }
51 
52 namespace user_prefs {
53 class PrefRegistrySyncable;
54 }
55 
56 // TemplateURLService is the backend for keywords. It's used by
57 // KeywordAutocomplete.
58 //
59 // TemplateURLService stores a vector of TemplateURLs. The TemplateURLs are
60 // persisted to the database maintained by KeywordWebDataService.
61 // *ALL* mutations to the TemplateURLs must funnel through TemplateURLService.
62 // This allows TemplateURLService to notify listeners of changes as well as keep
63 // the database in sync.
64 //
65 // TemplateURLService does not load the vector of TemplateURLs in its
66 // constructor (except for testing). Use the Load method to trigger a load.
67 // When TemplateURLService has completed loading, observers are notified via
68 // OnTemplateURLServiceChanged, or by a callback registered prior to calling
69 // the Load method.
70 //
71 // TemplateURLService takes ownership of any TemplateURL passed to it. If there
72 // is a KeywordWebDataService, deletion is handled by KeywordWebDataService,
73 // otherwise TemplateURLService handles deletion.
74 
75 class TemplateURLService : public WebDataServiceConsumer,
76                            public KeyedService,
77                            public syncer::SyncableService {
78  public:
79   using QueryTerms = std::map<std::string, std::string>;
80   using TemplateURLVector = TemplateURL::TemplateURLVector;
81   using OwnedTemplateURLVector = TemplateURL::OwnedTemplateURLVector;
82   using SyncDataMap = std::map<std::string, syncer::SyncData>;
83   using Subscription = base::CallbackList<void(void)>::Subscription;
84 
85   // We may want to treat the keyword in a TemplateURL as being a different
86   // length than it actually is.  For example, for keywords that end in a
87   // registry, e.g., '.com', we want to consider the registry characters as not
88   // a meaningful part of the keyword and not penalize for the user not typing
89   // those.)
90   using TURLAndMeaningfulLength = std::pair<TemplateURL*, size_t>;
91   using TURLsAndMeaningfulLengths = std::vector<TURLAndMeaningfulLength>;
92 
93   // Struct used for initializing the data store with fake data.
94   // Each initializer is mapped to a TemplateURL.
95   struct Initializer {
96     const char* const keyword;
97     const char* const url;
98     const char* const content;
99   };
100 
101   struct URLVisitedDetails {
102     GURL url;
103     bool is_keyword_transition;
104   };
105 
106   TemplateURLService(
107       PrefService* prefs,
108       std::unique_ptr<SearchTermsData> search_terms_data,
109       const scoped_refptr<KeywordWebDataService>& web_data_service,
110       std::unique_ptr<TemplateURLServiceClient> client,
111       const base::RepeatingClosure& dsp_change_callback);
112   // The following is for testing.
113   TemplateURLService(const Initializer* initializers, const int count);
114   ~TemplateURLService() override;
115 
116   // Register Profile preferences in |registry|.
117   static void RegisterProfilePrefs(user_prefs::PrefRegistrySyncable* registry);
118 
119 #if defined(OS_ANDROID)
120   base::android::ScopedJavaLocalRef<jobject> GetJavaObject();
121 #endif
122 
123   // Returns true if there is no TemplateURL that conflicts with the
124   // keyword/url pair, or there is one but it can be replaced. If there is an
125   // existing keyword that can be replaced and template_url_to_replace is
126   // non-NULL, template_url_to_replace is set to the keyword to replace.
127   //
128   // |url| is the URL of the search query.  This is used to prevent auto-adding
129   // a keyword for hosts already associated with a manually-edited keyword.
130   bool CanAddAutogeneratedKeyword(const base::string16& keyword,
131                                   const GURL& url,
132                                   const TemplateURL** template_url_to_replace);
133 
134   // Returns whether the engine is a "pre-existing" engine, either from the
135   // prepopulate list or created by policy.
136   bool IsPrepopulatedOrCreatedByPolicy(const TemplateURL* template_url) const;
137 
138   // Returns whether |template_url| should be shown in the list of engines
139   // most likely to be selected as a default engine. This is meant to highlight
140   // the current default, as well as the other most likely choices of default
141   // engine, separately from a full list of all TemplateURLs (which might be
142   // very long).
143   bool ShowInDefaultList(const TemplateURL* template_url) const;
144 
145   // Adds to |matches| all TemplateURLs whose keywords begin with |prefix|,
146   // sorted shortest-keyword-first. If |supports_replacement_only| is true, only
147   // TemplateURLs that support replacement are returned. This method must be
148   // efficient, since it's run roughly once per omnibox keystroke.
149   void AddMatchingKeywords(const base::string16& prefix,
150                            bool supports_replacement_only,
151                            TURLsAndMeaningfulLengths* matches);
152 
153   // Looks up |keyword| and returns the element it maps to.  Returns NULL if
154   // the keyword was not found.
155   // The caller should not try to delete the returned pointer; the data store
156   // retains ownership of it.
157   TemplateURL* GetTemplateURLForKeyword(const base::string16& keyword);
158   const TemplateURL* GetTemplateURLForKeyword(
159       const base::string16& keyword) const;
160 
161   // Returns that TemplateURL with the specified GUID, or NULL if not found.
162   // The caller should not try to delete the returned pointer; the data store
163   // retains ownership of it.
164   TemplateURL* GetTemplateURLForGUID(const std::string& sync_guid);
165   const TemplateURL* GetTemplateURLForGUID(const std::string& sync_guid) const;
166 
167   // Returns the first TemplateURL found with a URL using the specified |host|,
168   // or NULL if there are no such TemplateURLs
169   TemplateURL* GetTemplateURLForHost(const std::string& host);
170   const TemplateURL* GetTemplateURLForHost(const std::string& host) const;
171 
172   // Adds a new TemplateURL to this model.
173   //
174   // This function guarantees that on return the model will not have two non-
175   // extension TemplateURLs with the same keyword.  If that means that it cannot
176   // add the provided argument, it will return null.  Otherwise it will return
177   // the raw pointer to the TemplateURL.
178   //
179   // Returns a raw pointer to |template_url| if the addition succeeded, or null
180   // on failure.  (Many callers need still need a raw pointer to the TemplateURL
181   // so they can access it later.)
182   TemplateURL* Add(std::unique_ptr<TemplateURL> template_url);
183 
184   // Like Add(), but overwrites the |template_url|'s values with the provided
185   // ones.
186   TemplateURL* AddWithOverrides(std::unique_ptr<TemplateURL> template_url,
187                                 const base::string16& short_name,
188                                 const base::string16& keyword,
189                                 const std::string& url);
190 
191   // Removes the keyword from the model. This deletes the supplied TemplateURL.
192   // This fails if the supplied template_url is the default search provider.
193   void Remove(const TemplateURL* template_url);
194 
195   // Removes any TemplateURL of the specified |type| associated with
196   // |extension_id|. Unlike with Remove(), this can be called when the
197   // TemplateURL in question is the current default search provider.
198   void RemoveExtensionControlledTURL(const std::string& extension_id,
199                                      TemplateURL::Type type);
200 
201   // Removes all auto-generated keywords that were created on or after the
202   // date passed in.
203   void RemoveAutoGeneratedSince(base::Time created_after);
204 
205   // Removes all auto-generated keywords that were created in the specified
206   // range.
207   void RemoveAutoGeneratedBetween(base::Time created_after,
208                                   base::Time created_before);
209 
210   // Removes all auto-generated keywords that were created in the specified
211   // range and match |url_filter|. If |url_filter| is_null(), deletes all
212   // auto-generated keywords in the range.
213   void RemoveAutoGeneratedForUrlsBetween(
214       const base::Callback<bool(const GURL&)>& url_filter,
215       base::Time created_after,
216       base::Time created_before);
217 
218   // Adds a TemplateURL for an extension with an omnibox keyword.
219   // Only 1 keyword is allowed for a given extension. If a keyword
220   // already exists for this extension, does nothing.
221   void RegisterOmniboxKeyword(const std::string& extension_id,
222                               const std::string& extension_name,
223                               const std::string& keyword,
224                               const std::string& template_url_string,
225                               const base::Time& extension_install_time);
226 
227   // Returns the set of URLs describing the keywords. The elements are owned
228   // by TemplateURLService and should not be deleted.
229   TemplateURLVector GetTemplateURLs();
230 
231   // Increment the usage count of a keyword.
232   // Called when a URL is loaded that was generated from a keyword.
233   void IncrementUsageCount(TemplateURL* url);
234 
235   // Resets the title, keyword and search url of the specified TemplateURL.
236   // The TemplateURL is marked as not replaceable.
237   void ResetTemplateURL(TemplateURL* url,
238                         const base::string16& title,
239                         const base::string16& keyword,
240                         const std::string& search_url);
241 
242   // Creates TemplateURL, populating it with data from Play API. If TemplateURL
243   // with matching keyword already exists then merges Play API data into it.
244   // Sets |created_from_play_api| flag.
245   TemplateURL* CreateOrUpdateTemplateURLFromPlayAPIData(
246       const base::string16& title,
247       const base::string16& keyword,
248       const std::string& search_url,
249       const std::string& suggestions_url,
250       const std::string& favicon_url);
251 
252   // Updates any search providers matching |potential_search_url| with the new
253   // favicon location |favicon_url|.
254   void UpdateProviderFavicons(const GURL& potential_search_url,
255                               const GURL& favicon_url);
256 
257   // Return true if the given |url| can be made the default. This returns false
258   // regardless of |url| if the default search provider is managed by policy or
259   // controlled by an extension.
260   bool CanMakeDefault(const TemplateURL* url) const;
261 
262   // Set the default search provider.  |url| may be null.
263   // This will assert if the default search is managed; the UI should not be
264   // invoking this method in that situation.
265   void SetUserSelectedDefaultSearchProvider(TemplateURL* url);
266 
267   // Returns the default search provider. If the TemplateURLService hasn't been
268   // loaded, the default search provider is pulled from preferences.
269   //
270   // NOTE: This may return null in certain circumstances such as:
271   //       1.) Unit test mode
272   //       2.) The default search engine is disabled by policy.
273   const TemplateURL* GetDefaultSearchProvider() const;
274 
275   // Returns the default search provider, ignoring any that were provided by an
276   // extension.
277   const TemplateURL* GetDefaultSearchProviderIgnoringExtensions() const;
278 
279   // Returns true if the |url| is a search results page from the default search
280   // provider.
281   bool IsSearchResultsPageFromDefaultSearchProvider(const GURL& url) const;
282 
283   // Returns true if the default search is managed through group policy.
is_default_search_managed()284   bool is_default_search_managed() const {
285     return default_search_provider_source_ == DefaultSearchManager::FROM_POLICY;
286   }
287 
288   // Returns true if the default search provider is controlled by an extension.
289   bool IsExtensionControlledDefaultSearch() const;
290 
291   // Returns the default search specified in the prepopulated data, if it
292   // exists.  If not, returns first URL in |template_urls_|, or NULL if that's
293   // empty. The returned object is owned by TemplateURLService and can be
294   // destroyed at any time so should be used right after the call.
295   TemplateURL* FindNewDefaultSearchProvider();
296 
297   // Performs the same actions that happen when the prepopulate data version is
298   // revved: all existing prepopulated entries are checked against the current
299   // prepopulate data, any now-extraneous safe_for_autoreplace() entries are
300   // removed, any existing engines are reset to the provided data (except for
301   // user-edited names or keywords), and any new prepopulated engines are
302   // added.
303   //
304   // After this, the default search engine is reset to the default entry in the
305   // prepopulate data.
306   void RepairPrepopulatedSearchEngines();
307 
308   // Observers used to listen for changes to the model.
309   // TemplateURLService does NOT delete the observers when deleted.
310   void AddObserver(TemplateURLServiceObserver* observer);
311   void RemoveObserver(TemplateURLServiceObserver* observer);
312 
313   // Loads the keywords. This has no effect if the keywords have already been
314   // loaded.
315   // Observers are notified when loading completes via the method
316   // OnTemplateURLServiceChanged.
317   void Load();
318 
319   // Registers a callback to be called when the service has loaded.
320   //
321   // If the service has already loaded, this function does nothing.
322   std::unique_ptr<Subscription> RegisterOnLoadedCallback(
323       const base::RepeatingClosure& callback);
324 
325 #if defined(UNIT_TEST)
set_loaded(bool value)326   void set_loaded(bool value) { loaded_ = value; }
327 
328   // Turns Load() into a no-op.
set_disable_load(bool value)329   void set_disable_load(bool value) { disable_load_ = value; }
330 #endif
331 
332   // Whether or not the keywords have been loaded.
loaded()333   bool loaded() { return loaded_; }
334 
335   // Notification that the keywords have been loaded.
336   // This is invoked from WebDataService, and should not be directly
337   // invoked.
338   void OnWebDataServiceRequestDone(
339       KeywordWebDataService::Handle h,
340       std::unique_ptr<WDTypedResult> result) override;
341 
342   // Returns the locale-direction-adjusted short name for the given keyword.
343   // Also sets the out param to indicate whether the keyword belongs to an
344   // Omnibox extension.
345   base::string16 GetKeywordShortName(
346       const base::string16& keyword,
347       bool* is_omnibox_api_extension_keyword) const;
348 
349   // Called by the history service when a URL is visited.
350   void OnHistoryURLVisited(const URLVisitedDetails& details);
351 
352   // KeyedService implementation.
353   void Shutdown() override;
354 
355   // syncer::SyncableService implementation.
356 
357   // Waits until keywords have been loaded.
358   void WaitUntilReadyToSync(base::OnceClosure done) override;
359 
360   // Returns all syncable TemplateURLs from this model as SyncData. This should
361   // include every search engine and no Extension keywords.
362   syncer::SyncDataList GetAllSyncData(syncer::ModelType type) const;
363   // Process new search engine changes from Sync, merging them into our local
364   // data. This may send notifications if local search engines are added,
365   // updated or removed.
366   base::Optional<syncer::ModelError> ProcessSyncChanges(
367       const base::Location& from_here,
368       const syncer::SyncChangeList& change_list) override;
369   // Merge initial search engine data from Sync and push any local changes up
370   // to Sync. This may send notifications if local search engines are added,
371   // updated or removed.
372   base::Optional<syncer::ModelError> MergeDataAndStartSyncing(
373       syncer::ModelType type,
374       const syncer::SyncDataList& initial_sync_data,
375       std::unique_ptr<syncer::SyncChangeProcessor> sync_processor,
376       std::unique_ptr<syncer::SyncErrorFactory> sync_error_factory) override;
377   void StopSyncing(syncer::ModelType type) override;
378 
379   // Processes a local TemplateURL change for Sync. |turl| is the TemplateURL
380   // that has been modified, and |type| is the Sync ChangeType that took place.
381   // This may send a new SyncChange to the cloud. If our model has not yet been
382   // associated with Sync, or if this is triggered by a Sync change, then this
383   // does nothing.
384   void ProcessTemplateURLChange(const base::Location& from_here,
385                                 const TemplateURL* turl,
386                                 syncer::SyncChange::SyncChangeType type);
387 
388   // Returns a SearchTermsData which can be used to call TemplateURL methods.
search_terms_data()389   const SearchTermsData& search_terms_data() const {
390     return *search_terms_data_;
391   }
392 
393   // Obtains a session token, regenerating if necessary.
394   std::string GetSessionToken();
395 
396   // Clears the session token. Should be called when the user clears browsing
397   // data.
398   void ClearSessionToken();
399 
400   // Returns a SyncData with a sync representation of the search engine data
401   // from |turl|.
402   static syncer::SyncData CreateSyncDataFromTemplateURL(
403       const TemplateURL& turl);
404 
405   // Creates a new heap-allocated TemplateURL* which is populated by overlaying
406   // |sync_data| atop |existing_turl|.  |existing_turl| may be NULL; if not it
407   // remains unmodified.  The caller owns the returned TemplateURL*.
408   //
409   // If the created TemplateURL is migrated in some way from out-of-date sync
410   // data, an appropriate SyncChange is added to |change_list|.  If the sync
411   // data is bad for some reason, an ACTION_DELETE change is added and the
412   // function returns NULL.
413   static std::unique_ptr<TemplateURL>
414   CreateTemplateURLFromTemplateURLAndSyncData(
415       TemplateURLServiceClient* client,
416       PrefService* prefs,
417       const SearchTermsData& search_terms_data,
418       const TemplateURL* existing_turl,
419       const syncer::SyncData& sync_data,
420       syncer::SyncChangeList* change_list);
421 
422   // Returns a map mapping Sync GUIDs to pointers to syncer::SyncData.
423   static SyncDataMap CreateGUIDToSyncDataMap(
424       const syncer::SyncDataList& sync_data);
425 
426 #if defined(UNIT_TEST)
set_clock(std::unique_ptr<base::Clock> clock)427   void set_clock(std::unique_ptr<base::Clock> clock) {
428     clock_ = std::move(clock);
429   }
430 #endif
431 
432  private:
433   FRIEND_TEST_ALL_PREFIXES(TemplateURLServiceTest, TestManagedDefaultSearch);
434   FRIEND_TEST_ALL_PREFIXES(TemplateURLServiceTest,
435                            UpdateKeywordSearchTermsForURL);
436   FRIEND_TEST_ALL_PREFIXES(TemplateURLServiceTest,
437                            DontUpdateKeywordSearchForNonReplaceable);
438   FRIEND_TEST_ALL_PREFIXES(TemplateURLServiceTest, ChangeGoogleBaseValue);
439   FRIEND_TEST_ALL_PREFIXES(TemplateURLServiceTest, MergeDeletesUnusedProviders);
440   FRIEND_TEST_ALL_PREFIXES(TemplateURLServiceTest, AddOmniboxExtensionKeyword);
441   FRIEND_TEST_ALL_PREFIXES(TemplateURLServiceTest, ExtensionsWithSameKeywords);
442   FRIEND_TEST_ALL_PREFIXES(TemplateURLServiceTest,
443                            CheckEnginesWithSameKeywords);
444   FRIEND_TEST_ALL_PREFIXES(TemplateURLServiceTest, LastVisitedTimeUpdate);
445   FRIEND_TEST_ALL_PREFIXES(TemplateURLServiceTest,
446                            RepairPrepopulatedSearchEngines);
447   FRIEND_TEST_ALL_PREFIXES(TemplateURLServiceSyncTest, UniquifyKeyword);
448   FRIEND_TEST_ALL_PREFIXES(TemplateURLServiceSyncTest,
449                            IsLocalTemplateURLBetter);
450   FRIEND_TEST_ALL_PREFIXES(TemplateURLServiceSyncTest,
451                            ResolveSyncKeywordConflict);
452   FRIEND_TEST_ALL_PREFIXES(TemplateURLServiceSyncTest, PreSyncDeletes);
453   FRIEND_TEST_ALL_PREFIXES(TemplateURLServiceSyncTest, MergeInSyncTemplateURL);
454   FRIEND_TEST_ALL_PREFIXES(LocationBarModelTest, GoogleBaseURL);
455   FRIEND_TEST_ALL_PREFIXES(TemplateURLServiceUnitTest, SessionToken);
456 
457   friend class InstantUnitTestBase;
458   friend class Scoper;
459   friend class TemplateURLServiceTestUtil;
460   friend class TemplateUrlServiceAndroid;
461 
462   using GUIDToTURL = std::map<std::string, TemplateURL*>;
463 
464   // A mapping from keywords to the corresponding TemplateURLs and their
465   // meaningful keyword lengths.  This is a multimap, so the system can
466   // efficiently tolerate multiple engines with the same keyword, like from
467   // extensions.  The values are not sorted from best to worst for each keyword,
468   // since multimaps don't sort on value. Users that want the best value for
469   // each key must traverse through all matching items, but we expect there to
470   // be below three values per key.
471   using KeywordToTURLAndMeaningfulLength =
472       std::multimap<base::string16, TURLAndMeaningfulLength>;
473   // Declaration of values to be used in an enumerated histogram to tally
474   // changes to the default search provider from various entry points. In
475   // particular, we use this to see what proportion of changes are from Sync
476   // entry points, to help spot erroneous Sync activity.
477   enum DefaultSearchChangeOrigin {
478     // Various known Sync entry points.
479     DSP_CHANGE_SYNC_PREF,
480     DSP_CHANGE_SYNC_ADD,
481     DSP_CHANGE_SYNC_DELETE,
482     DSP_CHANGE_SYNC_NOT_MANAGED,
483     // "Other" origins. We differentiate between Sync and not Sync so we know if
484     // certain changes were intentionally from the system, or possibly some
485     // unintentional change from when we were Syncing.
486     DSP_CHANGE_SYNC_UNINTENTIONAL,
487     // All changes that don't fall into another category; we can't reorder the
488     // list for clarity as this would screw up stat collection.
489     DSP_CHANGE_OTHER,
490     // Changed through "Profile Reset" feature.
491     DSP_CHANGE_PROFILE_RESET,
492     // Changed by an extension through the Override Settings API.
493     DSP_CHANGE_OVERRIDE_SETTINGS_EXTENSION,
494     // New DSP during database/prepopulate data load, which was not previously
495     // in the known engine set, and with no previous value in prefs.  The
496     // typical time to see this is during first run.
497     DSP_CHANGE_NEW_ENGINE_NO_PREFS,
498     // Boundary value.
499     DSP_CHANGE_MAX,
500   };
501 
502   // Helper functor for FindMatchingKeywords(), for finding the range of
503   // keywords which begin with a prefix.
504   class LessWithPrefix;
505 
506   // Used to defer notifications until the last Scoper is destroyed by leaving
507   // the scope of a code block.
508   class Scoper;
509 
510   void Init(const Initializer* initializers, int num_initializers);
511 
512   // Removes |template_url| from various internal maps
513   // (|keyword_to_turl_and_length_|, |guid_to_turl_|, |provider_map_|).
514   void RemoveFromMaps(const TemplateURL* template_url);
515 
516   // Adds |template_url| to various internal maps
517   // (|keyword_to_turl_and_length_|, |guid_to_turl_|, |provider_map_|) if
518   // appropriate.  (It might not be appropriate if, for instance,
519   // |template_url|'s keyword conflicts with the keyword of a custom search
520   // engine already existing in the maps that is not allowed to be replaced.)
521   void AddToMaps(TemplateURL* template_url);
522 
523   // Helper function for adding an element to |keyword_to_turl_and_length_|.
524   void AddToMap(TemplateURL* template_url);
525 
526   // Sets the keywords. This is used once the keywords have been loaded.
527   // This does NOT notify the delegate or the database.
528   void SetTemplateURLs(std::unique_ptr<OwnedTemplateURLVector> urls);
529 
530   // Transitions to the loaded state.
531   void ChangeToLoadedState();
532 
533   // Applies a DSE change and reports metrics if appropriate.
534   void ApplyDefaultSearchChange(const TemplateURLData* new_dse_data,
535                                 DefaultSearchManager::Source source);
536 
537   // Applies a DSE change. May be called at startup or after transitioning to
538   // the loaded state. Returns true if a change actually occurred.
539   bool ApplyDefaultSearchChangeNoMetrics(const TemplateURLData* new_dse_data,
540                                          DefaultSearchManager::Source source);
541 
542   // Returns false if there is a TemplateURL that has a search url with the
543   // specified host and that TemplateURL has been manually modified.
544   bool CanAddAutogeneratedKeywordForHost(const std::string& host) const;
545 
546   // Returns true if the TemplateURL is replaceable. This doesn't look at the
547   // uniqueness of the keyword or host and is intended to be called after those
548   // checks have been done. This returns true if the TemplateURL doesn't appear
549   // in the default list and is marked as safe_for_autoreplace.
550   bool CanReplace(const TemplateURL* t_url) const;
551 
552   // Like GetTemplateURLForKeyword(), but ignores extension-provided keywords.
553   TemplateURL* FindNonExtensionTemplateURLForKeyword(
554       const base::string16& keyword);
555 
556   // Updates the information in |existing_turl| using the information from
557   // |new_values|, but the ID for |existing_turl| is retained. Returns whether
558   // |existing_turl| was found in |template_urls_| and thus could be updated.
559   //
560   // NOTE: This should not be called with an extension keyword as there are no
561   // updates needed in that case.
562   bool Update(TemplateURL* existing_turl, const TemplateURL& new_values);
563 
564   // If the TemplateURL comes from a prepopulated URL available in the current
565   // country, update all its fields save for the keyword, short name and id so
566   // that they match the internal prepopulated URL. TemplateURLs not coming from
567   // a prepopulated URL are not modified.
568   static void UpdateTemplateURLIfPrepopulated(TemplateURL* existing_turl,
569                                               PrefService* prefs);
570 
571   // If the TemplateURL's sync GUID matches the kSyncedDefaultSearchProviderGUID
572   // preference it will be used to update the DSE in prefs.
573   // OnDefaultSearchChange may be triggered as a result.
574   void MaybeUpdateDSEViaPrefs(TemplateURL* synced_turl);
575 
576   // Iterates through the TemplateURLs to see if one matches the visited url.
577   // For each TemplateURL whose url matches the visited url
578   // SetKeywordSearchTermsForURL is invoked.
579   void UpdateKeywordSearchTermsForURL(const URLVisitedDetails& details);
580 
581   // Updates the last_visited time of |url| to the current time.
582   void UpdateTemplateURLVisitTime(TemplateURL* url);
583 
584   // If necessary, generates a visit for the site http:// + t_url.keyword().
585   void AddTabToSearchVisit(const TemplateURL& t_url);
586 
587   // Adds a new TemplateURL to this model.
588   //
589   // If |newly_adding| is false, we assume that this TemplateURL was already
590   // part of the model in the past, and therefore we don't need to do things
591   // like assign it an ID or notify sync.
592   //
593   // This function guarantees that on return the model will not have two non-
594   // extension TemplateURLs with the same keyword.  If that means that it cannot
595   // add the provided argument, it will return null.  Otherwise it will return
596   // the raw pointer to the TemplateURL.
597   //
598   // Returns a raw pointer to |template_url| if the addition succeeded, or null
599   // on failure.  (Many callers need still need a raw pointer to the TemplateURL
600   // so they can access it later.)
601   TemplateURL* Add(std::unique_ptr<TemplateURL> template_url,
602                    bool newly_adding);
603 
604   // Updates |template_urls| so that the only "created by policy" entry is
605   // |default_from_prefs|. |default_from_prefs| may be NULL if there is no
606   // policy-defined DSE in effect.
607   void UpdateProvidersCreatedByPolicy(
608       OwnedTemplateURLVector* template_urls,
609       const TemplateURLData* default_from_prefs);
610 
611   // Resets the sync GUID of the specified TemplateURL and persists the change
612   // to the database. This does not notify observers.
613   void ResetTemplateURLGUID(TemplateURL* url, const std::string& guid);
614 
615   // Attempts to generate a unique keyword for |turl| based on its original
616   // keyword. If its keyword is already unique, that is returned. Otherwise, it
617   // tries to return the autogenerated keyword if that is unique to the Service,
618   // and finally it repeatedly appends special characters to the keyword until
619   // it is unique to the Service. If |force| is true, then this will only
620   // execute the special character appending functionality.
621   base::string16 UniquifyKeyword(const TemplateURL& turl, bool force);
622 
623   // Returns true iff |local_turl| is considered "better" than |sync_turl| for
624   // the purposes of resolving conflicts. |local_turl| must be a TemplateURL
625   // known to the local model (though it may already be synced), and |sync_turl|
626   // is a new TemplateURL known to Sync but not yet known to the local model.
627   // The criteria for if |local_turl| is better than |sync_turl| is whether any
628   // of the following are true:
629   //  * |local_turl|'s last_modified timestamp is newer than sync_turl.
630   //  * |local_turl| is created by policy.
631   //  * |prefer_local_default| is true and |local_turl| is the local default
632   //    search provider
633   //
634   // TODO(tommycli): Consolidate into using
635   // TemplateURL::IsBetterThanEngineWithConflictingKeyword. Likely we will
636   // eliminate the |prefer_local_default| mechanism.
637   bool IsLocalTemplateURLBetter(const TemplateURL* local_turl,
638                                 const TemplateURL* sync_turl,
639                                 bool prefer_local_default = true) const;
640 
641   // Given two synced TemplateURLs with a conflicting keyword, one of which
642   // needs to be added to or updated in the local model (|unapplied_sync_turl|)
643   // and one which is already known to the local model (|applied_sync_turl|),
644   // prepares the local model so that |unapplied_sync_turl| can be added to it,
645   // or applied as an update to an existing TemplateURL.
646   // Since both entries are known to Sync and one of their keywords will change,
647   // an ACTION_UPDATE will be appended to |change_list| to reflect this change.
648   // Note that |applied_sync_turl| must not be an extension keyword.
649   void ResolveSyncKeywordConflict(TemplateURL* unapplied_sync_turl,
650                                   TemplateURL* applied_sync_turl,
651                                   syncer::SyncChangeList* change_list);
652 
653   // Adds |sync_turl| into the local model, possibly removing or updating a
654   // local TemplateURL to make room for it. This expects |sync_turl| to be a new
655   // entry from Sync, not currently known to the local model. |sync_data| should
656   // be a SyncDataMap where the contents are entries initially known to Sync
657   // during MergeDataAndStartSyncing.
658   // Any necessary updates to Sync will be appended to |change_list|. This can
659   // include updates on local TemplateURLs, if they are found in |sync_data|.
660   // |initial_data| should be a SyncDataMap of the entries known to the local
661   // model during MergeDataAndStartSyncing. If |sync_turl| replaces a local
662   // entry, that entry is removed from |initial_data| to prevent it from being
663   // sent up to Sync.
664   // |merge_result| tracks the changes made to the local model. Added/modified/
665   // deleted are updated depending on how the |sync_turl| is merged in.
666   // This should only be called from MergeDataAndStartSyncing.
667   void MergeInSyncTemplateURL(TemplateURL* sync_turl,
668                               const SyncDataMap& sync_data,
669                               syncer::SyncChangeList* change_list,
670                               SyncDataMap* local_data);
671 
672   // Goes through a vector of TemplateURLs and ensure that both the in-memory
673   // and database copies have valid sync_guids. This is to fix crbug.com/102038,
674   // where old entries were being pushed to Sync without a sync_guid.
675   void PatchMissingSyncGUIDs(OwnedTemplateURLVector* template_urls);
676 
677   void OnSyncedDefaultSearchProviderGUIDChanged();
678 
679   // Adds to |matches| all TemplateURLs stored in |keyword_to_turl_and_length|
680   // whose keywords begin with |prefix|, sorted shortest-keyword-first.  If
681   // |supports_replacement_only| is true, only TemplateURLs that support
682   // replacement are returned.
683   template <typename Container>
684   void AddMatchingKeywordsHelper(
685       const Container& keyword_to_turl_and_length,
686       const base::string16& prefix,
687       bool supports_replacement_only,
688       TURLsAndMeaningfulLengths* matches);
689 
690   // Returns the TemplateURL corresponding to |prepopulated_id|, if any.
691   TemplateURL* FindPrepopulatedTemplateURL(int prepopulated_id);
692 
693   // Returns the TemplateURL associated with |extension_id|, if any.
694   TemplateURL* FindTemplateURLForExtension(const std::string& extension_id,
695                                            TemplateURL::Type type);
696 
697   // Finds any NORMAL_CONTROLLED_BY_EXTENSION engine that matches |data| and
698   // wants to be default. Returns nullptr if not found.
699   TemplateURL* FindMatchingDefaultExtensionTemplateURL(
700       const TemplateURLData& data);
701 
702   // ---------- Browser state related members ---------------------------------
703   PrefService* prefs_ = nullptr;
704 
705   std::unique_ptr<SearchTermsData> search_terms_data_ =
706       std::make_unique<SearchTermsData>();
707 
708   // ---------- Dependencies on other components ------------------------------
709   // Service used to store entries.
710   scoped_refptr<KeywordWebDataService> web_data_service_ = nullptr;
711 
712   std::unique_ptr<TemplateURLServiceClient> client_;
713 
714   // This closure is run when the default search provider is set to Google.
715   base::RepeatingClosure dsp_change_callback_;
716 
717   PrefChangeRegistrar pref_change_registrar_;
718 
719   // Mapping from keyword to the TemplateURL.
720   KeywordToTURLAndMeaningfulLength keyword_to_turl_and_length_;
721 
722   // Mapping from Sync GUIDs to the TemplateURL.
723   GUIDToTURL guid_to_turl_;
724 
725   OwnedTemplateURLVector template_urls_;
726 
727   base::ObserverList<TemplateURLServiceObserver> model_observers_;
728 
729   // Maps from host to set of TemplateURLs whose search url host is host.
730   std::unique_ptr<SearchHostToURLsMap> provider_map_ =
731       std::make_unique<SearchHostToURLsMap>();
732 
733   // Whether the keywords have been loaded.
734   bool loaded_ = false;
735 
736   // Set when the web data service fails to load properly.  This prevents
737   // further communication with sync or writing to prefs, so we don't persist
738   // inconsistent state data anywhere.
739   bool load_failed_ = false;
740 
741   // Whether Load() is disabled. True only in testing contexts.
742   bool disable_load_ = false;
743 
744   // If non-zero, we're waiting on a load.
745   KeywordWebDataService::Handle load_handle_ = 0;
746 
747   // All visits that occurred before we finished loading. Once loaded
748   // UpdateKeywordSearchTermsForURL is invoked for each element of the vector.
749   std::vector<URLVisitedDetails> visits_to_add_;
750 
751   // Once loaded, the default search provider.  This is a pointer to a
752   // TemplateURL owned by |template_urls_|.
753   TemplateURL* default_search_provider_ = nullptr;
754 
755   // A temporary location for the DSE until Web Data has been loaded and it can
756   // be merged into |template_urls_|.
757   std::unique_ptr<TemplateURL> initial_default_search_provider_;
758 
759   // Source of the default search provider.
760   DefaultSearchManager::Source default_search_provider_source_;
761 
762   // ID assigned to next TemplateURL added to this model. This is an ever
763   // increasing integer that is initialized from the database.
764   TemplateURLID next_id_ = kInvalidTemplateURLID + 1;
765 
766   // Used to retrieve the current time, in base::Time units.
767   std::unique_ptr<base::Clock> clock_ = std::make_unique<base::DefaultClock>();
768 
769   // Do we have an active association between the TemplateURLs and sync models?
770   // Set in MergeDataAndStartSyncing, reset in StopSyncing. While this is not
771   // set, we ignore any local search engine changes (when we start syncing we
772   // will look up the most recent values anyways).
773   bool models_associated_ = false;
774 
775   // Whether we're currently processing changes from the syncer. While this is
776   // true, we ignore any local search engine changes, since we triggered them.
777   bool processing_syncer_changes_ = false;
778 
779   // We never want reentrancy while applying a default search engine change.
780   // This can happen when deleting keyword conflicts. crbug.com/1031506
781   bool applying_default_search_engine_change_ = false;
782 
783   // Sync's syncer::SyncChange handler. We push all our changes through this.
784   std::unique_ptr<syncer::SyncChangeProcessor> sync_processor_;
785 
786   // Sync's error handler. We use it to create a sync error.
787   std::unique_ptr<syncer::SyncErrorFactory> sync_error_factory_;
788 
789   // A set of sync GUIDs denoting TemplateURLs that have been removed from this
790   // model or the underlying KeywordWebDataService prior to
791   // MergeDataAndStartSyncing.
792   // This set is used to determine what entries from the server we want to
793   // ignore locally and return a delete command for.
794   std::set<std::string> pre_sync_deletes_;
795 
796   // This is used to log the origin of changes to the default search provider.
797   // We set this value to increasingly specific values when we know what is the
798   // cause/origin of a default search change.
799   DefaultSearchChangeOrigin dsp_change_origin_ = DSP_CHANGE_OTHER;
800 
801   // Stores a list of callbacks to be run after TemplateURLService has loaded.
802   base::CallbackList<void(void)> on_loaded_callbacks_;
803 
804   // Similar to |on_loaded_callbacks_| but used for WaitUntilReadyToSync().
805   base::OnceClosure on_loaded_callback_for_sync_;
806 
807   // Helper class to manage the default search engine.
808   DefaultSearchManager default_search_manager_;
809 
810   // This tracks how many Scoper handles exist. When the number of handles drops
811   // to zero, a notification is made to observers if
812   // |model_mutated_notification_pending_| is true.
813   int outstanding_scoper_handles_ = 0;
814 
815   // Used to track if a notification is necessary due to the model being
816   // mutated. The outermost Scoper handles, can be used to defer notifications,
817   // but if no model mutation occurs, the deferred notification can be skipped.
818   bool model_mutated_notification_pending_ = false;
819 
820   // Session token management.
821   std::string current_token_;
822   base::TimeTicks token_expiration_time_;
823 
824 #if defined(OS_ANDROID)
825   // Manage and fetch the java object that wraps this TemplateURLService on
826   // android.
827   std::unique_ptr<TemplateUrlServiceAndroid> template_url_service_android_;
828 #endif
829 
830   DISALLOW_COPY_AND_ASSIGN(TemplateURLService);
831 };
832 
833 #endif  // COMPONENTS_SEARCH_ENGINES_TEMPLATE_URL_SERVICE_H_
834