Home | History | Annotate | Download | only in search_engines
      1 // Copyright (c) 2012 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 CHROME_BROWSER_SEARCH_ENGINES_TEMPLATE_URL_SERVICE_H_
      6 #define CHROME_BROWSER_SEARCH_ENGINES_TEMPLATE_URL_SERVICE_H_
      7 
      8 #include <list>
      9 #include <map>
     10 #include <set>
     11 #include <string>
     12 #include <vector>
     13 
     14 #include "base/callback_list.h"
     15 #include "base/gtest_prod_util.h"
     16 #include "base/memory/scoped_ptr.h"
     17 #include "base/observer_list.h"
     18 #include "base/prefs/pref_change_registrar.h"
     19 #include "chrome/browser/search_engines/default_search_manager.h"
     20 #include "chrome/browser/webdata/web_data_service.h"
     21 #include "components/google/core/browser/google_url_tracker.h"
     22 #include "components/keyed_service/core/keyed_service.h"
     23 #include "components/search_engines/template_url_id.h"
     24 #include "content/public/browser/notification_observer.h"
     25 #include "content/public/browser/notification_registrar.h"
     26 #include "sync/api/sync_change.h"
     27 #include "sync/api/syncable_service.h"
     28 
     29 class GURL;
     30 class PrefService;
     31 class Profile;
     32 class SearchHostToURLsMap;
     33 class SearchTermsData;
     34 class TemplateURL;
     35 struct TemplateURLData;
     36 class TemplateURLServiceObserver;
     37 
     38 namespace syncer {
     39 class SyncData;
     40 class SyncErrorFactory;
     41 }
     42 
     43 namespace history {
     44 struct URLVisitedDetails;
     45 }
     46 
     47 // TemplateURLService is the backend for keywords. It's used by
     48 // KeywordAutocomplete.
     49 //
     50 // TemplateURLService stores a vector of TemplateURLs. The TemplateURLs are
     51 // persisted to the database maintained by WebDataService. *ALL* mutations
     52 // to the TemplateURLs must funnel through TemplateURLService. This allows
     53 // TemplateURLService to notify listeners of changes as well as keep the
     54 // database in sync.
     55 //
     56 // There is a TemplateURLService per Profile.
     57 //
     58 // TemplateURLService does not load the vector of TemplateURLs in its
     59 // constructor (except for testing). Use the Load method to trigger a load.
     60 // When TemplateURLService has completed loading, observers are notified via
     61 // OnTemplateURLServiceChanged, or by a callback registered prior to calling
     62 // the Load method.
     63 //
     64 // TemplateURLService takes ownership of any TemplateURL passed to it. If there
     65 // is a WebDataService, deletion is handled by WebDataService, otherwise
     66 // TemplateURLService handles deletion.
     67 
     68 class TemplateURLService : public WebDataServiceConsumer,
     69                            public KeyedService,
     70                            public content::NotificationObserver,
     71                            public syncer::SyncableService {
     72  public:
     73   typedef std::map<std::string, std::string> QueryTerms;
     74   typedef std::vector<TemplateURL*> TemplateURLVector;
     75   // Type for a static function pointer that acts as a time source.
     76   typedef base::Time(TimeProvider)();
     77   typedef std::map<std::string, syncer::SyncData> SyncDataMap;
     78   typedef base::CallbackList<void(void)>::Subscription Subscription;
     79 
     80   // Struct used for initializing the data store with fake data.
     81   // Each initializer is mapped to a TemplateURL.
     82   struct Initializer {
     83     const char* const keyword;
     84     const char* const url;
     85     const char* const content;
     86   };
     87 
     88   // Struct describes a search engine added by an extension.
     89   struct ExtensionKeyword {
     90     ExtensionKeyword(const std::string& id,
     91                      const std::string& name,
     92                      const std::string& keyword);
     93     ~ExtensionKeyword();
     94 
     95     std::string extension_id;
     96     std::string extension_name;
     97     std::string extension_keyword;
     98   };
     99 
    100   explicit TemplateURLService(Profile* profile);
    101   // The following is for testing.
    102   TemplateURLService(const Initializer* initializers, const int count);
    103   virtual ~TemplateURLService();
    104 
    105   // Creates a TemplateURLData that was previously saved to |prefs| via
    106   // SaveDefaultSearchProviderToPrefs or set via policy.
    107   // Returns true if successful, false otherwise.
    108   // If the user or the policy has opted for no default search, this
    109   // returns true but default_provider is set to NULL.
    110   // |*is_managed| specifies whether the default is managed via policy.
    111   static bool LoadDefaultSearchProviderFromPrefs(
    112       PrefService* prefs,
    113       scoped_ptr<TemplateURLData>* default_provider_data,
    114       bool* is_managed);
    115 
    116   // Removes any unnecessary characters from a user input keyword.
    117   // This removes the leading scheme, "www." and any trailing slash.
    118   static base::string16 CleanUserInputKeyword(const base::string16& keyword);
    119 
    120   // Saves enough of url to |prefs| so that it can be loaded from preferences on
    121   // start up.
    122   static void SaveDefaultSearchProviderToPrefs(const TemplateURL* url,
    123                                                PrefService* prefs);
    124 
    125   // Returns true if there is no TemplateURL that conflicts with the
    126   // keyword/url pair, or there is one but it can be replaced. If there is an
    127   // existing keyword that can be replaced and template_url_to_replace is
    128   // non-NULL, template_url_to_replace is set to the keyword to replace.
    129   //
    130   // url gives the url of the search query. The url is used to avoid generating
    131   // a TemplateURL for an existing TemplateURL that shares the same host.
    132   bool CanReplaceKeyword(const base::string16& keyword,
    133                          const GURL& url,
    134                          TemplateURL** template_url_to_replace);
    135 
    136   // Returns (in |matches|) all TemplateURLs whose keywords begin with |prefix|,
    137   // sorted shortest keyword-first. If |support_replacement_only| is true, only
    138   // TemplateURLs that support replacement are returned.
    139   void FindMatchingKeywords(const base::string16& prefix,
    140                             bool support_replacement_only,
    141                             TemplateURLVector* matches);
    142 
    143   // Looks up |keyword| and returns the element it maps to.  Returns NULL if
    144   // the keyword was not found.
    145   // The caller should not try to delete the returned pointer; the data store
    146   // retains ownership of it.
    147   TemplateURL* GetTemplateURLForKeyword(const base::string16& keyword);
    148 
    149   // Returns that TemplateURL with the specified GUID, or NULL if not found.
    150   // The caller should not try to delete the returned pointer; the data store
    151   // retains ownership of it.
    152   TemplateURL* GetTemplateURLForGUID(const std::string& sync_guid);
    153 
    154   // Returns the first TemplateURL found with a URL using the specified |host|,
    155   // or NULL if there are no such TemplateURLs
    156   TemplateURL* GetTemplateURLForHost(const std::string& host);
    157 
    158   // Takes ownership of |template_url| and adds it to this model.  For obvious
    159   // reasons, it is illegal to Add() the same |template_url| pointer twice.
    160   // Returns true if the Add is successful.
    161   bool Add(TemplateURL* template_url);
    162 
    163   // Like Add(), but overwrites the |template_url|'s values with the provided
    164   // ones.
    165   void AddWithOverrides(TemplateURL* template_url,
    166                         const base::string16& short_name,
    167                         const base::string16& keyword,
    168                         const std::string& url);
    169 
    170   // Add the search engine of type NORMAL_CONTROLLED_BY_EXTENSION.
    171   void AddExtensionControlledTURL(TemplateURL* template_url,
    172                                   scoped_ptr<AssociatedExtensionInfo> info);
    173 
    174   // Removes the keyword from the model. This deletes the supplied TemplateURL.
    175   // This fails if the supplied template_url is the default search provider.
    176   void Remove(TemplateURL* template_url);
    177 
    178   // Removes any TemplateURL of type NORMAL_CONTROLLED_BY_EXTENSION associated
    179   // with |extension_id|. Unlike with Remove(), this can be called when the
    180   // TemplateURL in question is the current default search provider.
    181   void RemoveExtensionControlledTURL(const std::string& extension_id);
    182 
    183   // Removes all auto-generated keywords that were created on or after the
    184   // date passed in.
    185   void RemoveAutoGeneratedSince(base::Time created_after);
    186 
    187   // Removes all auto-generated keywords that were created in the specified
    188   // range.
    189   void RemoveAutoGeneratedBetween(base::Time created_after,
    190                                   base::Time created_before);
    191 
    192   // Removes all auto-generated keywords that were created in the specified
    193   // range for a specified |origin|. If |origin| is empty, deletes all
    194   // auto-generated keywords in the range.
    195   void RemoveAutoGeneratedForOriginBetween(const GURL& origin,
    196                                            base::Time created_after,
    197                                            base::Time created_before);
    198 
    199   // Adds a TemplateURL for an extension with an omnibox keyword.
    200   // Only 1 keyword is allowed for a given extension. If a keyword
    201   // already exists for this extension, does nothing.
    202   void RegisterOmniboxKeyword(const std::string& extension_id,
    203                               const std::string& extension_name,
    204                               const std::string& keyword);
    205 
    206   // Removes the TemplateURL containing the keyword for the extension with the
    207   // given ID, if any.
    208   void UnregisterOmniboxKeyword(const std::string& extension_id);
    209 
    210   // Returns the set of URLs describing the keywords. The elements are owned
    211   // by TemplateURLService and should not be deleted.
    212   TemplateURLVector GetTemplateURLs();
    213 
    214   // Increment the usage count of a keyword.
    215   // Called when a URL is loaded that was generated from a keyword.
    216   void IncrementUsageCount(TemplateURL* url);
    217 
    218   // Resets the title, keyword and search url of the specified TemplateURL.
    219   // The TemplateURL is marked as not replaceable.
    220   void ResetTemplateURL(TemplateURL* url,
    221                         const base::string16& title,
    222                         const base::string16& keyword,
    223                         const std::string& search_url);
    224 
    225   // Return true if the given |url| can be made the default. This returns false
    226   // regardless of |url| if the default search provider is managed by policy or
    227   // controlled by an extension.
    228   bool CanMakeDefault(const TemplateURL* url);
    229 
    230   // Set the default search provider.  |url| may be null.
    231   // This will assert if the default search is managed; the UI should not be
    232   // invoking this method in that situation.
    233   void SetUserSelectedDefaultSearchProvider(TemplateURL* url);
    234 
    235   // Returns the default search provider. If the TemplateURLService hasn't been
    236   // loaded, the default search provider is pulled from preferences.
    237   //
    238   // NOTE: At least in unittest mode, this may return NULL.
    239   TemplateURL* GetDefaultSearchProvider();
    240 
    241   // Returns true if the |url| is a search results page from the default search
    242   // provider.
    243   bool IsSearchResultsPageFromDefaultSearchProvider(const GURL& url);
    244 
    245   // Returns true if the default search is managed through group policy.
    246   bool is_default_search_managed() const {
    247     return default_search_provider_source_ == DefaultSearchManager::FROM_POLICY;
    248   }
    249 
    250   // Returns true if the default search provider is controlled by an extension.
    251   bool IsExtensionControlledDefaultSearch();
    252 
    253   // Returns the default search specified in the prepopulated data, if it
    254   // exists.  If not, returns first URL in |template_urls_|, or NULL if that's
    255   // empty. The returned object is owned by TemplateURLService and can be
    256   // destroyed at any time so should be used right after the call.
    257   TemplateURL* FindNewDefaultSearchProvider();
    258 
    259   // Performs the same actions that happen when the prepopulate data version is
    260   // revved: all existing prepopulated entries are checked against the current
    261   // prepopulate data, any now-extraneous safe_for_autoreplace() entries are
    262   // removed, any existing engines are reset to the provided data (except for
    263   // user-edited names or keywords), and any new prepopulated engines are
    264   // added.
    265   //
    266   // After this, the default search engine is reset to the default entry in the
    267   // prepopulate data.
    268   void RepairPrepopulatedSearchEngines();
    269 
    270   // Observers used to listen for changes to the model.
    271   // TemplateURLService does NOT delete the observers when deleted.
    272   void AddObserver(TemplateURLServiceObserver* observer);
    273   void RemoveObserver(TemplateURLServiceObserver* observer);
    274 
    275   // Loads the keywords. This has no effect if the keywords have already been
    276   // loaded.
    277   // Observers are notified when loading completes via the method
    278   // OnTemplateURLServiceChanged.
    279   void Load();
    280 
    281   // Registers a callback to be called when the service has loaded.
    282   //
    283   // If the service has already loaded, this function does nothing.
    284   scoped_ptr<Subscription> RegisterOnLoadedCallback(
    285       const base::Closure& callback);
    286 
    287 #if defined(UNIT_TEST)
    288   void set_loaded(bool value) { loaded_ = value; }
    289 #endif
    290 
    291   // Whether or not the keywords have been loaded.
    292   bool loaded() { return loaded_; }
    293 
    294   // Notification that the keywords have been loaded.
    295   // This is invoked from WebDataService, and should not be directly
    296   // invoked.
    297   virtual void OnWebDataServiceRequestDone(
    298       WebDataService::Handle h,
    299       const WDTypedResult* result) OVERRIDE;
    300 
    301   // Returns the locale-direction-adjusted short name for the given keyword.
    302   // Also sets the out param to indicate whether the keyword belongs to an
    303   // Omnibox extension.
    304   base::string16 GetKeywordShortName(const base::string16& keyword,
    305                                      bool* is_omnibox_api_extension_keyword);
    306 
    307   // content::NotificationObserver implementation.
    308   virtual void Observe(int type,
    309                        const content::NotificationSource& source,
    310                        const content::NotificationDetails& details) OVERRIDE;
    311 
    312   // KeyedService implementation.
    313   virtual void Shutdown() OVERRIDE;
    314 
    315   // syncer::SyncableService implementation.
    316 
    317   // Returns all syncable TemplateURLs from this model as SyncData. This should
    318   // include every search engine and no Extension keywords.
    319   virtual syncer::SyncDataList GetAllSyncData(
    320       syncer::ModelType type) const OVERRIDE;
    321   // Process new search engine changes from Sync, merging them into our local
    322   // data. This may send notifications if local search engines are added,
    323   // updated or removed.
    324   virtual syncer::SyncError ProcessSyncChanges(
    325       const tracked_objects::Location& from_here,
    326       const syncer::SyncChangeList& change_list) OVERRIDE;
    327   // Merge initial search engine data from Sync and push any local changes up
    328   // to Sync. This may send notifications if local search engines are added,
    329   // updated or removed.
    330   virtual syncer::SyncMergeResult MergeDataAndStartSyncing(
    331       syncer::ModelType type,
    332       const syncer::SyncDataList& initial_sync_data,
    333       scoped_ptr<syncer::SyncChangeProcessor> sync_processor,
    334       scoped_ptr<syncer::SyncErrorFactory> sync_error_factory) OVERRIDE;
    335   virtual void StopSyncing(syncer::ModelType type) OVERRIDE;
    336 
    337   // Processes a local TemplateURL change for Sync. |turl| is the TemplateURL
    338   // that has been modified, and |type| is the Sync ChangeType that took place.
    339   // This may send a new SyncChange to the cloud. If our model has not yet been
    340   // associated with Sync, or if this is triggered by a Sync change, then this
    341   // does nothing.
    342   void ProcessTemplateURLChange(const tracked_objects::Location& from_here,
    343                                 const TemplateURL* turl,
    344                                 syncer::SyncChange::SyncChangeType type);
    345 
    346   // DEPRECATED: Profile will be removed from this class. crbug.com/371535
    347   Profile* profile() const { return profile_; }
    348 
    349   // Returns a SearchTermsData which can be used to call TemplateURL methods.
    350   // Note: Prefer using this method to instantiating UIThreadSearchTermsData on
    351   // your own, especially when your code hasn't depended on Profile, to avoid
    352   // adding a new dependency on Profile.
    353   const SearchTermsData& search_terms_data() const {
    354     return *search_terms_data_;
    355   }
    356 
    357   // Returns a SyncData with a sync representation of the search engine data
    358   // from |turl|.
    359   static syncer::SyncData CreateSyncDataFromTemplateURL(
    360       const TemplateURL& turl);
    361 
    362   // Creates a new heap-allocated TemplateURL* which is populated by overlaying
    363   // |sync_data| atop |existing_turl|.  |existing_turl| may be NULL; if not it
    364   // remains unmodified.  The caller owns the returned TemplateURL*.
    365   //
    366   // If the created TemplateURL is migrated in some way from out-of-date sync
    367   // data, an appropriate SyncChange is added to |change_list|.  If the sync
    368   // data is bad for some reason, an ACTION_DELETE change is added and the
    369   // function returns NULL.
    370   static TemplateURL* CreateTemplateURLFromTemplateURLAndSyncData(
    371       Profile* profile,
    372       TemplateURL* existing_turl,
    373       const syncer::SyncData& sync_data,
    374       syncer::SyncChangeList* change_list);
    375 
    376   // Returns a map mapping Sync GUIDs to pointers to syncer::SyncData.
    377   static SyncDataMap CreateGUIDToSyncDataMap(
    378       const syncer::SyncDataList& sync_data);
    379 
    380 #if defined(UNIT_TEST)
    381   // Sets a different time provider function, such as
    382   // base::MockTimeProvider::StaticNow, for testing calls to base::Time::Now.
    383   void set_time_provider(TimeProvider* time_provider) {
    384     time_provider_ = time_provider;
    385   }
    386 #endif
    387 
    388  protected:
    389   // Cover method for the method of the same name on the HistoryService.
    390   // url is the one that was visited with the given search terms.
    391   //
    392   // This exists and is virtual for testing.
    393   virtual void SetKeywordSearchTermsForURL(const TemplateURL* t_url,
    394                                            const GURL& url,
    395                                            const base::string16& term);
    396 
    397  private:
    398   FRIEND_TEST_ALL_PREFIXES(TemplateURLServiceTest, TestManagedDefaultSearch);
    399   FRIEND_TEST_ALL_PREFIXES(TemplateURLServiceTest,
    400                            UpdateKeywordSearchTermsForURL);
    401   FRIEND_TEST_ALL_PREFIXES(TemplateURLServiceTest,
    402                            DontUpdateKeywordSearchForNonReplaceable);
    403   FRIEND_TEST_ALL_PREFIXES(TemplateURLServiceTest, ChangeGoogleBaseValue);
    404   FRIEND_TEST_ALL_PREFIXES(TemplateURLServiceTest, MergeDeletesUnusedProviders);
    405   FRIEND_TEST_ALL_PREFIXES(TemplateURLServiceSyncTest, UniquifyKeyword);
    406   FRIEND_TEST_ALL_PREFIXES(TemplateURLServiceSyncTest,
    407                            IsLocalTemplateURLBetter);
    408   FRIEND_TEST_ALL_PREFIXES(TemplateURLServiceSyncTest,
    409                            ResolveSyncKeywordConflict);
    410   FRIEND_TEST_ALL_PREFIXES(TemplateURLServiceSyncTest, PreSyncDeletes);
    411   FRIEND_TEST_ALL_PREFIXES(TemplateURLServiceSyncTest, MergeInSyncTemplateURL);
    412 
    413   friend class InstantUnitTestBase;
    414   friend class TemplateURLServiceTestUtilBase;
    415 
    416   typedef std::map<base::string16, TemplateURL*> KeywordToTemplateMap;
    417   typedef std::map<std::string, TemplateURL*> GUIDToTemplateMap;
    418 
    419   // Declaration of values to be used in an enumerated histogram to tally
    420   // changes to the default search provider from various entry points. In
    421   // particular, we use this to see what proportion of changes are from Sync
    422   // entry points, to help spot erroneous Sync activity.
    423   enum DefaultSearchChangeOrigin {
    424     // Various known Sync entry points.
    425     DSP_CHANGE_SYNC_PREF,
    426     DSP_CHANGE_SYNC_ADD,
    427     DSP_CHANGE_SYNC_DELETE,
    428     DSP_CHANGE_SYNC_NOT_MANAGED,
    429     // "Other" origins. We differentiate between Sync and not Sync so we know if
    430     // certain changes were intentionally from the system, or possibly some
    431     // unintentional change from when we were Syncing.
    432     DSP_CHANGE_SYNC_UNINTENTIONAL,
    433     // All changes that don't fall into another category; we can't reorder the
    434     // list for clarity as this would screw up stat collection.
    435     DSP_CHANGE_OTHER,
    436     // Changed through "Profile Reset" feature.
    437     DSP_CHANGE_PROFILE_RESET,
    438     // Changed by an extension through the Override Settings API.
    439     DSP_CHANGE_OVERRIDE_SETTINGS_EXTENSION,
    440     // New DSP during database/prepopulate data load, which was not previously
    441     // in the known engine set, and with no previous value in prefs.  The
    442     // typical time to see this is during first run.
    443     DSP_CHANGE_NEW_ENGINE_NO_PREFS,
    444     // Boundary value.
    445     DSP_CHANGE_MAX,
    446   };
    447 
    448   // Helper functor for FindMatchingKeywords(), for finding the range of
    449   // keywords which begin with a prefix.
    450   class LessWithPrefix;
    451 
    452   void Init(const Initializer* initializers, int num_initializers);
    453 
    454   void RemoveFromMaps(TemplateURL* template_url);
    455 
    456   void AddToMaps(TemplateURL* template_url);
    457 
    458   // Sets the keywords. This is used once the keywords have been loaded.
    459   // This does NOT notify the delegate or the database.
    460   //
    461   // This transfers ownership of the elements in |urls| to |this|, and may
    462   // delete some elements, so it's not safe for callers to access any elements
    463   // after calling; to reinforce this, this function clears |urls| on exit.
    464   void SetTemplateURLs(TemplateURLVector* urls);
    465 
    466   // Transitions to the loaded state.
    467   void ChangeToLoadedState();
    468 
    469   // Callback that is called when the Google URL is updated.
    470   void OnGoogleURLUpdated(GURL old_url, GURL new_url);
    471 
    472   // Called by DefaultSearchManager when the effective default search engine has
    473   // changed.
    474   void OnDefaultSearchChange(const TemplateURLData* new_dse_data,
    475                              DefaultSearchManager::Source source);
    476 
    477   // Applies a DSE change and reports metrics if appropriate.
    478   void ApplyDefaultSearchChange(const TemplateURLData* new_dse_data,
    479                                 DefaultSearchManager::Source source);
    480 
    481 
    482   // Applies a DSE change. May be called at startup or after transitioning to
    483   // the loaded state. Returns true if a change actually occurred.
    484   bool ApplyDefaultSearchChangeNoMetrics(const TemplateURLData* new_dse_data,
    485                                          DefaultSearchManager::Source source);
    486 
    487   // Returns true if there is no TemplateURL that has a search url with the
    488   // specified host, or the only TemplateURLs matching the specified host can
    489   // be replaced.
    490   bool CanReplaceKeywordForHost(const std::string& host,
    491                                 TemplateURL** to_replace);
    492 
    493   // Returns true if the TemplateURL is replaceable. This doesn't look at the
    494   // uniqueness of the keyword or host and is intended to be called after those
    495   // checks have been done. This returns true if the TemplateURL doesn't appear
    496   // in the default list and is marked as safe_for_autoreplace.
    497   bool CanReplace(const TemplateURL* t_url);
    498 
    499   // Like GetTemplateURLForKeyword(), but ignores extension-provided keywords.
    500   TemplateURL* FindNonExtensionTemplateURLForKeyword(
    501       const base::string16& keyword);
    502 
    503   // Updates the information in |existing_turl| using the information from
    504   // |new_values|, but the ID for |existing_turl| is retained.  Notifying
    505   // observers is the responsibility of the caller.  Returns whether
    506   // |existing_turl| was found in |template_urls_| and thus could be updated.
    507   // |old_search_terms_data| is passed to SearchHostToURLsMap::Remove().
    508   //
    509   // NOTE: This should not be called with an extension keyword as there are no
    510   // updates needed in that case.
    511   bool UpdateNoNotify(TemplateURL* existing_turl,
    512                       const TemplateURL& new_values,
    513                       const SearchTermsData& old_search_terms_data);
    514 
    515   // If the TemplateURL comes from a prepopulated URL available in the current
    516   // country, update all its fields save for the keyword, short name and id so
    517   // that they match the internal prepopulated URL. TemplateURLs not coming from
    518   // a prepopulated URL are not modified.
    519   static void UpdateTemplateURLIfPrepopulated(TemplateURL* existing_turl,
    520                                               Profile* profile);
    521 
    522   // If the TemplateURL's sync GUID matches the kSyncedDefaultSearchProviderGUID
    523   // preference it will be used to update the DSE in memory and as persisted in
    524   // preferences.
    525   void MaybeUpdateDSEAfterSync(TemplateURL* synced_turl);
    526 
    527   // Returns the preferences we use.
    528   PrefService* GetPrefs();
    529 
    530   // Iterates through the TemplateURLs to see if one matches the visited url.
    531   // For each TemplateURL whose url matches the visited url
    532   // SetKeywordSearchTermsForURL is invoked.
    533   void UpdateKeywordSearchTermsForURL(
    534       const history::URLVisitedDetails& details);
    535 
    536   // If necessary, generates a visit for the site http:// + t_url.keyword().
    537   void AddTabToSearchVisit(const TemplateURL& t_url);
    538 
    539   // Invoked when the Google base URL has changed. Updates the mapping for all
    540   // TemplateURLs that have a replacement term of {google:baseURL} or
    541   // {google:baseSuggestURL}.
    542   void GoogleBaseURLChanged(const GURL& old_base_url);
    543 
    544   // Adds a new TemplateURL to this model. TemplateURLService will own the
    545   // reference, and delete it when the TemplateURL is removed.
    546   // If |newly_adding| is false, we assume that this TemplateURL was already
    547   // part of the model in the past, and therefore we don't need to do things
    548   // like assign it an ID or notify sync.
    549   // This function guarantees that on return the model will not have two
    550   // non-extension TemplateURLs with the same keyword.  If that means that it
    551   // cannot add the provided argument, it will delete it and return false.
    552   // Caller is responsible for notifying observers if this function returns
    553   // true.
    554   bool AddNoNotify(TemplateURL* template_url, bool newly_adding);
    555 
    556   // Removes the keyword from the model. This deletes the supplied TemplateURL.
    557   // This fails if the supplied template_url is the default search provider.
    558   // Caller is responsible for notifying observers.
    559   void RemoveNoNotify(TemplateURL* template_url);
    560 
    561   // Like ResetTemplateURL(), but instead of notifying observers, returns
    562   // whether anything has changed.
    563   bool ResetTemplateURLNoNotify(TemplateURL* url,
    564                                 const base::string16& title,
    565                                 const base::string16& keyword,
    566                                 const std::string& search_url);
    567 
    568   // Notify the observers that the model has changed.  This is done only if the
    569   // model is loaded.
    570   void NotifyObservers();
    571 
    572   // Updates |template_urls| so that the only "created by policy" entry is
    573   // |default_from_prefs|. |default_from_prefs| may be NULL if there is no
    574   // policy-defined DSE in effect.
    575   void UpdateProvidersCreatedByPolicy(
    576       TemplateURLVector* template_urls,
    577       const TemplateURLData* default_from_prefs);
    578 
    579   // Resets the sync GUID of the specified TemplateURL and persists the change
    580   // to the database. This does not notify observers.
    581   void ResetTemplateURLGUID(TemplateURL* url, const std::string& guid);
    582 
    583   // Attempts to generate a unique keyword for |turl| based on its original
    584   // keyword. If its keyword is already unique, that is returned. Otherwise, it
    585   // tries to return the autogenerated keyword if that is unique to the Service,
    586   // and finally it repeatedly appends special characters to the keyword until
    587   // it is unique to the Service. If |force| is true, then this will only
    588   // execute the special character appending functionality.
    589   base::string16 UniquifyKeyword(const TemplateURL& turl, bool force);
    590 
    591   // Returns true iff |local_turl| is considered "better" than |sync_turl| for
    592   // the purposes of resolving conflicts. |local_turl| must be a TemplateURL
    593   // known to the local model (though it may already be synced), and |sync_turl|
    594   // is a new TemplateURL known to Sync but not yet known to the local model.
    595   // The criteria for if |local_turl| is better than |sync_turl| is whether any
    596   // of the following are true:
    597   //  * |local_turl|'s last_modified timestamp is newer than sync_turl.
    598   //  * |local_turl| is created by policy.
    599   //  * |local_turl| is the local default search provider.
    600   bool IsLocalTemplateURLBetter(const TemplateURL* local_turl,
    601                                 const TemplateURL* sync_turl);
    602 
    603   // Given two synced TemplateURLs with a conflicting keyword, one of which
    604   // needs to be added to or updated in the local model (|unapplied_sync_turl|)
    605   // and one which is already known to the local model (|applied_sync_turl|),
    606   // prepares the local model so that |unapplied_sync_turl| can be added to it,
    607   // or applied as an update to an existing TemplateURL.
    608   // Since both entries are known to Sync and one of their keywords will change,
    609   // an ACTION_UPDATE will be appended to |change_list| to reflect this change.
    610   // Note that |applied_sync_turl| must not be an extension keyword.
    611   void ResolveSyncKeywordConflict(TemplateURL* unapplied_sync_turl,
    612                                   TemplateURL* applied_sync_turl,
    613                                   syncer::SyncChangeList* change_list);
    614 
    615   // Adds |sync_turl| into the local model, possibly removing or updating a
    616   // local TemplateURL to make room for it. This expects |sync_turl| to be a new
    617   // entry from Sync, not currently known to the local model. |sync_data| should
    618   // be a SyncDataMap where the contents are entries initially known to Sync
    619   // during MergeDataAndStartSyncing.
    620   // Any necessary updates to Sync will be appended to |change_list|. This can
    621   // include updates on local TemplateURLs, if they are found in |sync_data|.
    622   // |initial_data| should be a SyncDataMap of the entries known to the local
    623   // model during MergeDataAndStartSyncing. If |sync_turl| replaces a local
    624   // entry, that entry is removed from |initial_data| to prevent it from being
    625   // sent up to Sync.
    626   // |merge_result| tracks the changes made to the local model. Added/modified/
    627   // deleted are updated depending on how the |sync_turl| is merged in.
    628   // This should only be called from MergeDataAndStartSyncing.
    629   void MergeInSyncTemplateURL(TemplateURL* sync_turl,
    630                               const SyncDataMap& sync_data,
    631                               syncer::SyncChangeList* change_list,
    632                               SyncDataMap* local_data,
    633                               syncer::SyncMergeResult* merge_result);
    634 
    635   // Goes through a vector of TemplateURLs and ensure that both the in-memory
    636   // and database copies have valid sync_guids. This is to fix crbug.com/102038,
    637   // where old entries were being pushed to Sync without a sync_guid.
    638   void PatchMissingSyncGUIDs(TemplateURLVector* template_urls);
    639 
    640   void OnSyncedDefaultSearchProviderGUIDChanged();
    641 
    642   // Adds |template_urls| to |template_urls_|.
    643   //
    644   // This transfers ownership of the elements in |template_urls| to |this|, and
    645   // may delete some elements, so it's not safe for callers to access any
    646   // elements after calling; to reinforce this, this function clears
    647   // |template_urls| on exit.
    648   void AddTemplateURLs(TemplateURLVector* template_urls);
    649 
    650   // Returns a new TemplateURL for the given extension.
    651   TemplateURL* CreateTemplateURLForExtension(
    652       const ExtensionKeyword& extension_keyword);
    653 
    654   // Returns the TemplateURL corresponding to |prepopulated_id|, if any.
    655   TemplateURL* FindPrepopulatedTemplateURL(int prepopulated_id);
    656 
    657   // Returns the TemplateURL associated with |extension_id|, if any.
    658   TemplateURL* FindTemplateURLForExtension(const std::string& extension_id,
    659                                            TemplateURL::Type type);
    660 
    661   // Finds the extension-supplied TemplateURL that matches |data|, if any.
    662   TemplateURL* FindMatchingExtensionTemplateURL(const TemplateURLData& data,
    663                                                 TemplateURL::Type type);
    664 
    665   // Finds the most recently-installed NORMAL_CONTROLLED_BY_EXTENSION engine
    666   // that supports replacement and wants to be default, if any. Notifies the
    667   // DefaultSearchManager, which might change the effective default search
    668   // engine.
    669   void UpdateExtensionDefaultSearchEngine();
    670 
    671   content::NotificationRegistrar notification_registrar_;
    672   PrefChangeRegistrar pref_change_registrar_;
    673 
    674   // Mapping from keyword to the TemplateURL.
    675   KeywordToTemplateMap keyword_to_template_map_;
    676 
    677   // Mapping from Sync GUIDs to the TemplateURL.
    678   GUIDToTemplateMap guid_to_template_map_;
    679 
    680   TemplateURLVector template_urls_;
    681 
    682   ObserverList<TemplateURLServiceObserver> model_observers_;
    683 
    684   // Maps from host to set of TemplateURLs whose search url host is host.
    685   // NOTE: This is always non-NULL; we use a scoped_ptr<> to avoid circular
    686   // header dependencies.
    687   scoped_ptr<SearchHostToURLsMap> provider_map_;
    688 
    689   // Used to obtain the WebDataService.
    690   // When Load is invoked, if we haven't yet loaded, the WebDataService is
    691   // obtained from the Profile. This allows us to lazily access the database.
    692   Profile* profile_;
    693 
    694   scoped_ptr<SearchTermsData> search_terms_data_;
    695 
    696   // Whether the keywords have been loaded.
    697   bool loaded_;
    698 
    699   // Set when the web data service fails to load properly.  This prevents
    700   // further communication with sync or writing to prefs, so we don't persist
    701   // inconsistent state data anywhere.
    702   bool load_failed_;
    703 
    704   // If non-zero, we're waiting on a load.
    705   WebDataService::Handle load_handle_;
    706 
    707   // Service used to store entries.
    708   scoped_refptr<WebDataService> service_;
    709 
    710   // All visits that occurred before we finished loading. Once loaded
    711   // UpdateKeywordSearchTermsForURL is invoked for each element of the vector.
    712   std::vector<history::URLVisitedDetails> visits_to_add_;
    713 
    714   // Once loaded, the default search provider.  This is a pointer to a
    715   // TemplateURL owned by |template_urls_|.
    716   TemplateURL* default_search_provider_;
    717 
    718   // A temporary location for the DSE until Web Data has been loaded and it can
    719   // be merged into |template_urls_|.
    720   scoped_ptr<TemplateURL> initial_default_search_provider_;
    721 
    722   // Source of the default search provider.
    723   DefaultSearchManager::Source default_search_provider_source_;
    724 
    725   // ID assigned to next TemplateURL added to this model. This is an ever
    726   // increasing integer that is initialized from the database.
    727   TemplateURLID next_id_;
    728 
    729   // Function returning current time in base::Time units.
    730   TimeProvider* time_provider_;
    731 
    732   // Do we have an active association between the TemplateURLs and sync models?
    733   // Set in MergeDataAndStartSyncing, reset in StopSyncing. While this is not
    734   // set, we ignore any local search engine changes (when we start syncing we
    735   // will look up the most recent values anyways).
    736   bool models_associated_;
    737 
    738   // Whether we're currently processing changes from the syncer. While this is
    739   // true, we ignore any local search engine changes, since we triggered them.
    740   bool processing_syncer_changes_;
    741 
    742   // Sync's syncer::SyncChange handler. We push all our changes through this.
    743   scoped_ptr<syncer::SyncChangeProcessor> sync_processor_;
    744 
    745   // Sync's error handler. We use it to create a sync error.
    746   scoped_ptr<syncer::SyncErrorFactory> sync_error_factory_;
    747 
    748   // A set of sync GUIDs denoting TemplateURLs that have been removed from this
    749   // model or the underlying WebDataService prior to MergeDataAndStartSyncing.
    750   // This set is used to determine what entries from the server we want to
    751   // ignore locally and return a delete command for.
    752   std::set<std::string> pre_sync_deletes_;
    753 
    754   // This is used to log the origin of changes to the default search provider.
    755   // We set this value to increasingly specific values when we know what is the
    756   // cause/origin of a default search change.
    757   DefaultSearchChangeOrigin dsp_change_origin_;
    758 
    759   // Stores a list of callbacks to be run after TemplateURLService has loaded.
    760   base::CallbackList<void(void)> on_loaded_callbacks_;
    761 
    762   // Helper class to manage the default search engine.
    763   DefaultSearchManager default_search_manager_;
    764 
    765   scoped_ptr<GoogleURLTracker::Subscription> google_url_updated_subscription_;
    766 
    767   DISALLOW_COPY_AND_ASSIGN(TemplateURLService);
    768 };
    769 
    770 #endif  // CHROME_BROWSER_SEARCH_ENGINES_TEMPLATE_URL_SERVICE_H_
    771