Index: chrome/browser/search_engines/template_url_service.h |
=================================================================== |
--- chrome/browser/search_engines/template_url_service.h (revision 95804) |
+++ chrome/browser/search_engines/template_url_service.h (working copy) |
@@ -17,6 +17,9 @@ |
#include "chrome/browser/profiles/profile_keyed_service.h" |
#include "chrome/browser/search_engines/search_host_to_urls_map.h" |
#include "chrome/browser/search_engines/template_url_id.h" |
+#include "chrome/browser/sync/api/sync_change.h" |
+#include "chrome/browser/sync/api/syncable_service.h" |
+#include "chrome/browser/sync/protocol/search_engine_specifics.pb.h" |
#include "chrome/browser/webdata/web_data_service.h" |
#include "content/common/notification_observer.h" |
#include "content/common/notification_registrar.h" |
@@ -28,6 +31,7 @@ |
class PrefSetObserver; |
class SearchHostToURLsMap; |
class SearchTermsData; |
+class SyncData; |
class TemplateURLServiceObserver; |
class TemplateURLRef; |
@@ -35,6 +39,8 @@ |
struct URLVisitedDetails; |
} |
+typedef std::map<std::string, SyncData> SyncDataMap; |
Peter Kasting
2011/08/09 21:31:04
Nit: Should this typedef be inside the class? See
SteveT
2011/08/10 15:15:12
Done.
|
+ |
// TemplateURLService is the backend for keywords. It's used by |
// KeywordAutocomplete. |
// |
@@ -58,7 +64,8 @@ |
class TemplateURLService : public WebDataServiceConsumer, |
public ProfileKeyedService, |
- public NotificationObserver { |
+ public NotificationObserver, |
+ public SyncableService { |
public: |
typedef std::map<std::string, std::string> QueryTerms; |
typedef std::vector<const TemplateURL*> TemplateURLVector; |
@@ -124,6 +131,12 @@ |
// retains ownership of it. |
const TemplateURL* GetTemplateURLForKeyword(const string16& keyword) const; |
+ // Looks up |sync_guid| and returns the element it maps to. Returns NULL if |
+ // the guid was not found. |
+ // The caller should not try to delete the returned pointer; the data store |
+ // retains ownership of it. |
+ const TemplateURL* GetTemplateURLForGUID(const std::string& sync_guid) const; |
+ |
// Returns the first TemplateURL found with a URL using the specified |host|, |
// or NULL if there are no such TemplateURLs |
const TemplateURL* GetTemplateURLForHost(const std::string& host) const; |
@@ -175,6 +188,14 @@ |
const string16& keyword, |
const std::string& search_url); |
+ // Resets the title, keyword, search url and sync GUID of the specified |
+ // TemplateURL. The TemplateURL is marked as not replaceable. |
+ void ResetTemplateURL(const TemplateURL* url, |
Peter Kasting
2011/08/09 21:31:04
By adding this, we've effectively created a "defau
SteveT
2011/08/10 15:15:12
You're right - the correct thing to do here is to
|
+ const string16& title, |
+ const string16& keyword, |
+ const std::string& search_url, |
+ const std::string& guid); |
+ |
// Return true if the given |url| can be made the default. |
bool CanMakeDefault(const TemplateURL* url); |
@@ -233,6 +254,34 @@ |
const NotificationSource& source, |
const NotificationDetails& details); |
+ // SyncableService implementation. |
+ |
+ // Returns all syncable TemplateURLs from this model as SyncData. This should |
+ // include every search engine and no Extension keywords. |
+ virtual SyncDataList GetAllSyncData(syncable::ModelType type) const OVERRIDE; |
+ // Process new search engine changes from Sync, merging them into our local |
+ // data. This may send notifications if local search engines are added, |
+ // updated or removed. |
+ virtual SyncError ProcessSyncChanges( |
+ const tracked_objects::Location& from_here, |
+ const SyncChangeList& change_list) OVERRIDE; |
+ // Merge initial search engine data from Sync and push any local changes up |
+ // to Sync. This may send notifications if local search engines are added, |
+ // updated or removed. |
+ virtual SyncError MergeDataAndStartSyncing( |
+ syncable::ModelType type, |
+ const SyncDataList& initial_sync_data, |
+ SyncChangeProcessor* sync_processor) OVERRIDE; |
+ virtual void StopSyncing(syncable::ModelType type) OVERRIDE; |
+ |
+ // Processes a local TemplateURL change for Sync. |turl| is the TemplateURL |
+ // that has been modified, and |type| is the Sync ChangeType that took place. |
+ // This may send a new SyncChange to the cloud. If our model has not yet been |
+ // associated with Sync, or if this is triggered by a Sync change, then this |
+ // does nothing. |
+ void ProcessTemplateURLChange(const TemplateURL* turl, |
+ SyncChange::SyncChangeType type); |
+ |
Profile* profile() const { return profile_; } |
void SetSearchEngineDialogSlot(int slot) { |
@@ -246,6 +295,52 @@ |
// Registers the preferences used to save a TemplateURL to prefs. |
static void RegisterUserPrefs(PrefService* prefs); |
+ // Fills |sync_data| with a sync representation of the search engine data from |
+ // |turl|. Returns true if successful, false otherwise. |
Peter Kasting
2011/08/09 21:31:04
Nit: You can probably just say "Returns whether cr
SteveT
2011/08/10 15:15:12
Changed the wording. Actually this currently alway
Peter Kasting
2011/08/10 17:39:11
Yeah, I'd do that; it's easy to change the signatu
|
+ static bool CreateSearchEngineSyncData(const TemplateURL& turl, |
+ SyncData* sync_data); |
+ |
+ // Read a TemplateURL object from SyncData containing SearchEngineSpecifics. |
+ // This does the opposite of CreateSearchEngineSyncData. The caller owns the |
+ // returned TemplateURL*. |
+ static TemplateURL* ReadSearchEngineSyncData(const SyncData& sync_data); |
Peter Kasting
2011/08/09 21:31:04
Nit: These two functions are paired, but one retur
SteveT
2011/08/10 15:15:12
Actually, since our version of CreateSyncData (abo
|
+ |
+ // Attempts to generate a unique keyword for |turl| based on its original |
+ // keyword. If its keyword is already unique, that is returned. Otherwise, it |
+ // tries to return the autogenerated keyword if that is unique to the Service, |
+ // and finally it repeatedly appends special characters to the keyword until |
+ // it is unique to the Service. |
+ string16 UniquifyKeyword(const TemplateURL& turl) const; |
+ |
+ // Given a TemplateURL from Sync, resolves any keyword conflicts by checking |
+ // the local keywords and uniquifying either the cloud keyword or a |
+ // conflicting local keyword (whichever is older). If the cloud TURL is |
+ // changed, then an appropriate SyncChange is appended to |change_list|. If |
+ // a local TURL is changed, the service is updated with the new keyword. If |
+ // there was no conflict to begin with, this does nothing. In the case of tied |
+ // last_modified dates, |sync_turl| wins. Returns true iff there was a |
+ // conflict. |
+ bool ResolveSyncKeywordConflict(TemplateURL* sync_turl, |
+ SyncChangeList& change_list); |
+ |
+ // Returns a TemplateURL from the service that has the same keyword and search |
+ // URL as |sync_turl|, if it exists. |
+ const TemplateURL* FindDuplicateOfSyncTemplateURL( |
+ const TemplateURL& sync_turl); |
+ |
+ // Given a TemplateURL from the cloud and a local matching duplicate found by |
+ // FindDuplcateOfSyncTemplateURL, merges the two. If |sync_url| is newer, this |
+ // replaces |local_url| with |sync_url| using the service's Remove and Add. |
+ // If |local_url| is newer, this copies the GUID from |sync_url| over to |
+ // |local_url| and adds an update to change_list to notify the server of the |
+ // change. |
+ void MergeSyncAndLocalURLDuplicates(TemplateURL* sync_url, |
+ TemplateURL* local_url, |
+ SyncChangeList& change_list); |
+ |
+ // Returns a map mapping Sync GUIDs to pointers to SyncData. |
+ static SyncDataMap CreateGUIDToSyncDataMap(const SyncDataList& sync_data); |
+ |
#if defined(UNIT_TEST) |
// Set a different time provider function, such as |
// base::MockTimeProvider::StaticNow, when testing calls to base::Time::Now. |
@@ -275,6 +370,7 @@ |
friend class TemplateURLServiceTestUtil; |
typedef std::map<string16, const TemplateURL*> KeywordToTemplateMap; |
+ typedef std::map<std::string, const TemplateURL*> GUIDToTemplateMap; |
// Helper functor for FindMatchingKeywords(), for finding the range of |
// keywords which begin with a prefix. |
@@ -400,6 +496,9 @@ |
// Mapping from keyword to the TemplateURL. |
KeywordToTemplateMap keyword_to_template_map_; |
+ // Mapping from Sync GUIDs to the TemplateURL. |
+ GUIDToTemplateMap guid_to_template_map_; |
+ |
TemplateURLVector template_urls_; |
ObserverList<TemplateURLServiceObserver> model_observers_; |
@@ -457,6 +556,19 @@ |
// Function returning current time in base::Time units. |
TimeProvider* time_provider_; |
+ // Do we have an active association between the TemplateURLs and sync models? |
+ // Set when start syncing, reset in StopSyncing. While this is not set, we |
Peter Kasting
2011/08/09 21:31:04
Nit: "Set when start syncing" isn't grammatical (m
SteveT
2011/08/10 15:15:12
Done.
|
+ // ignore any local search engine changes (when we start syncing we will look |
+ // up the most recent values anyways). |
+ bool models_associated_; |
+ |
+ // Whether we're currently processing changes from the syncer. While this is |
+ // true, we ignore any local search engine changes, since we triggered them. |
+ bool processing_syncer_changes_; |
+ |
+ // Sync's SyncChange handler. We push all our changes through this. |
+ SyncChangeProcessor* sync_processor_; |
+ |
DISALLOW_COPY_AND_ASSIGN(TemplateURLService); |
}; |