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

Unified Diff: chrome/browser/search_engines/template_url_service.h

Issue 7566036: Implement SyncableServices in TemplateURLService. Add related unittests. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src/
Patch Set: '' Created 9 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
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;
@@ -58,12 +62,14 @@
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;
// Type for a static function pointer that acts as a time source.
typedef base::Time(TimeProvider)();
+ typedef std::map<std::string, SyncData> SyncDataMap;
// Struct used for initializing the data store with fake data.
// Each initializer is mapped to a TemplateURL.
@@ -124,6 +130,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;
@@ -168,13 +180,18 @@
// Called when a URL is loaded that was generated from a keyword.
void IncrementUsageCount(const TemplateURL* url);
- // Resets the title, keyword and search url of the specified TemplateURL.
- // The TemplateURL is marked as not replaceable.
+ // Resets the title, keyword, search url and sync GUID of the specified
+ // TemplateURL. The TemplateURL is marked as not replaceable. If |guid| is
Peter Kasting 2011/08/10 17:42:50 Nit: This comment is out of date.
SteveT 2011/08/10 18:27:18 Done.
+ // empty, this will not reset the GUID.
void ResetTemplateURL(const TemplateURL* url,
const string16& title,
const string16& keyword,
const std::string& search_url);
+ // Resets the sync GUID of the specified TemplateURL and persist the change to
Peter Kasting 2011/08/10 17:42:50 Nit: persist -> persists
SteveT 2011/08/10 18:27:18 Done.
+ // the database. This does not notify observers.
+ void ResetTemplateURLGUID(const TemplateURL* url, const std::string& guid);
+
// Return true if the given |url| can be made the default.
bool CanMakeDefault(const TemplateURL* url);
@@ -233,6 +250,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 +291,51 @@
// Registers the preferences used to save a TemplateURL to prefs.
static void RegisterUserPrefs(PrefService* prefs);
+ // Returns a SyncData with a sync representation of the search engine data
+ // from |turl|.
+ static SyncData CreateSyncDataFromTemplateURL(const TemplateURL& turl);
+
+ // Returns a heap-allocated TemplateURL, populated by |sync_data|'s fields.
+ // This does the opposite of CreateSyncDataFromTemplateURL. The caller owns
+ // the returned TemplateURL*.
+ static TemplateURL* CreateTemplateURLFromSyncData(const SyncData& sync_data);
+
+ // 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 +365,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 +491,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 +551,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 in MergeDataAndStartSyncing, reset in StopSyncing. While this is not
+ // set, we 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);
};
« no previous file with comments | « chrome/browser/search_engines/template_url.cc ('k') | chrome/browser/search_engines/template_url_service.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698