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

Side by Side 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 unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #ifndef CHROME_BROWSER_SEARCH_ENGINES_TEMPLATE_URL_SERVICE_H_ 5 #ifndef CHROME_BROWSER_SEARCH_ENGINES_TEMPLATE_URL_SERVICE_H_
6 #define CHROME_BROWSER_SEARCH_ENGINES_TEMPLATE_URL_SERVICE_H_ 6 #define CHROME_BROWSER_SEARCH_ENGINES_TEMPLATE_URL_SERVICE_H_
7 #pragma once 7 #pragma once
8 8
9 #include <map> 9 #include <map>
10 #include <set> 10 #include <set>
11 #include <string> 11 #include <string>
12 #include <vector> 12 #include <vector>
13 13
14 #include "base/gtest_prod_util.h" 14 #include "base/gtest_prod_util.h"
15 #include "base/memory/scoped_ptr.h" 15 #include "base/memory/scoped_ptr.h"
16 #include "base/observer_list.h" 16 #include "base/observer_list.h"
17 #include "chrome/browser/profiles/profile_keyed_service.h" 17 #include "chrome/browser/profiles/profile_keyed_service.h"
18 #include "chrome/browser/search_engines/search_host_to_urls_map.h" 18 #include "chrome/browser/search_engines/search_host_to_urls_map.h"
19 #include "chrome/browser/search_engines/template_url_id.h" 19 #include "chrome/browser/search_engines/template_url_id.h"
20 #include "chrome/browser/sync/api/sync_change.h"
21 #include "chrome/browser/sync/api/syncable_service.h"
22 #include "chrome/browser/sync/protocol/search_engine_specifics.pb.h"
20 #include "chrome/browser/webdata/web_data_service.h" 23 #include "chrome/browser/webdata/web_data_service.h"
21 #include "content/common/notification_observer.h" 24 #include "content/common/notification_observer.h"
22 #include "content/common/notification_registrar.h" 25 #include "content/common/notification_registrar.h"
23 26
24 class GURL; 27 class GURL;
25 class Extension; 28 class Extension;
26 class PrefService; 29 class PrefService;
27 class Profile; 30 class Profile;
28 class PrefSetObserver; 31 class PrefSetObserver;
29 class SearchHostToURLsMap; 32 class SearchHostToURLsMap;
30 class SearchTermsData; 33 class SearchTermsData;
34 class SyncData;
31 class TemplateURLServiceObserver; 35 class TemplateURLServiceObserver;
32 class TemplateURLRef; 36 class TemplateURLRef;
33 37
34 namespace history { 38 namespace history {
35 struct URLVisitedDetails; 39 struct URLVisitedDetails;
36 } 40 }
37 41
38 // TemplateURLService is the backend for keywords. It's used by 42 // TemplateURLService is the backend for keywords. It's used by
39 // KeywordAutocomplete. 43 // KeywordAutocomplete.
40 // 44 //
(...skipping 10 matching lines...) Expand all
51 // When TemplateURLService has completed loading, observers are notified via 55 // When TemplateURLService has completed loading, observers are notified via
52 // OnTemplateURLServiceChanged as well as the TEMPLATE_URL_SERVICE_LOADED 56 // OnTemplateURLServiceChanged as well as the TEMPLATE_URL_SERVICE_LOADED
53 // notification message. 57 // notification message.
54 // 58 //
55 // TemplateURLService takes ownership of any TemplateURL passed to it. If there 59 // TemplateURLService takes ownership of any TemplateURL passed to it. If there
56 // is a WebDataService, deletion is handled by WebDataService, otherwise 60 // is a WebDataService, deletion is handled by WebDataService, otherwise
57 // TemplateURLService handles deletion. 61 // TemplateURLService handles deletion.
58 62
59 class TemplateURLService : public WebDataServiceConsumer, 63 class TemplateURLService : public WebDataServiceConsumer,
60 public ProfileKeyedService, 64 public ProfileKeyedService,
61 public NotificationObserver { 65 public NotificationObserver,
66 public SyncableService {
62 public: 67 public:
63 typedef std::map<std::string, std::string> QueryTerms; 68 typedef std::map<std::string, std::string> QueryTerms;
64 typedef std::vector<const TemplateURL*> TemplateURLVector; 69 typedef std::vector<const TemplateURL*> TemplateURLVector;
65 // Type for a static function pointer that acts as a time source. 70 // Type for a static function pointer that acts as a time source.
66 typedef base::Time(TimeProvider)(); 71 typedef base::Time(TimeProvider)();
72 typedef std::map<std::string, SyncData> SyncDataMap;
67 73
68 // Struct used for initializing the data store with fake data. 74 // Struct used for initializing the data store with fake data.
69 // Each initializer is mapped to a TemplateURL. 75 // Each initializer is mapped to a TemplateURL.
70 struct Initializer { 76 struct Initializer {
71 const char* const keyword; 77 const char* const keyword;
72 const char* const url; 78 const char* const url;
73 const char* const content; 79 const char* const content;
74 }; 80 };
75 81
76 explicit TemplateURLService(Profile* profile); 82 explicit TemplateURLService(Profile* profile);
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after
117 void FindMatchingKeywords(const string16& prefix, 123 void FindMatchingKeywords(const string16& prefix,
118 bool support_replacement_only, 124 bool support_replacement_only,
119 std::vector<string16>* matches) const; 125 std::vector<string16>* matches) const;
120 126
121 // Looks up |keyword| and returns the element it maps to. Returns NULL if 127 // Looks up |keyword| and returns the element it maps to. Returns NULL if
122 // the keyword was not found. 128 // the keyword was not found.
123 // The caller should not try to delete the returned pointer; the data store 129 // The caller should not try to delete the returned pointer; the data store
124 // retains ownership of it. 130 // retains ownership of it.
125 const TemplateURL* GetTemplateURLForKeyword(const string16& keyword) const; 131 const TemplateURL* GetTemplateURLForKeyword(const string16& keyword) const;
126 132
133 // Looks up |sync_guid| and returns the element it maps to. Returns NULL if
134 // the guid was not found.
135 // The caller should not try to delete the returned pointer; the data store
136 // retains ownership of it.
137 const TemplateURL* GetTemplateURLForGUID(const std::string& sync_guid) const;
138
127 // Returns the first TemplateURL found with a URL using the specified |host|, 139 // Returns the first TemplateURL found with a URL using the specified |host|,
128 // or NULL if there are no such TemplateURLs 140 // or NULL if there are no such TemplateURLs
129 const TemplateURL* GetTemplateURLForHost(const std::string& host) const; 141 const TemplateURL* GetTemplateURLForHost(const std::string& host) const;
130 142
131 // Adds a new TemplateURL to this model. TemplateURLService will own the 143 // Adds a new TemplateURL to this model. TemplateURLService will own the
132 // reference, and delete it when the TemplateURL is removed. 144 // reference, and delete it when the TemplateURL is removed.
133 void Add(TemplateURL* template_url); 145 void Add(TemplateURL* template_url);
134 146
135 // Removes the keyword from the model. This deletes the supplied TemplateURL. 147 // Removes the keyword from the model. This deletes the supplied TemplateURL.
136 // This fails if the supplied template_url is the default search provider. 148 // This fails if the supplied template_url is the default search provider.
(...skipping 24 matching lines...) Expand all
161 const Extension* extension) const; 173 const Extension* extension) const;
162 174
163 // Returns the set of URLs describing the keywords. The elements are owned 175 // Returns the set of URLs describing the keywords. The elements are owned
164 // by TemplateURLService and should not be deleted. 176 // by TemplateURLService and should not be deleted.
165 TemplateURLVector GetTemplateURLs() const; 177 TemplateURLVector GetTemplateURLs() const;
166 178
167 // Increment the usage count of a keyword. 179 // Increment the usage count of a keyword.
168 // Called when a URL is loaded that was generated from a keyword. 180 // Called when a URL is loaded that was generated from a keyword.
169 void IncrementUsageCount(const TemplateURL* url); 181 void IncrementUsageCount(const TemplateURL* url);
170 182
171 // Resets the title, keyword and search url of the specified TemplateURL. 183 // Resets the title, keyword, search url and sync GUID of the specified
172 // The TemplateURL is marked as not replaceable. 184 // 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.
185 // empty, this will not reset the GUID.
173 void ResetTemplateURL(const TemplateURL* url, 186 void ResetTemplateURL(const TemplateURL* url,
174 const string16& title, 187 const string16& title,
175 const string16& keyword, 188 const string16& keyword,
176 const std::string& search_url); 189 const std::string& search_url);
177 190
191 // 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.
192 // the database. This does not notify observers.
193 void ResetTemplateURLGUID(const TemplateURL* url, const std::string& guid);
194
178 // Return true if the given |url| can be made the default. 195 // Return true if the given |url| can be made the default.
179 bool CanMakeDefault(const TemplateURL* url); 196 bool CanMakeDefault(const TemplateURL* url);
180 197
181 // Set the default search provider. |url| may be null. 198 // Set the default search provider. |url| may be null.
182 // This will assert if the default search is managed; the UI should not be 199 // This will assert if the default search is managed; the UI should not be
183 // invoking this method in that situation. 200 // invoking this method in that situation.
184 void SetDefaultSearchProvider(const TemplateURL* url); 201 void SetDefaultSearchProvider(const TemplateURL* url);
185 202
186 // Returns the default search provider. If the TemplateURLService hasn't been 203 // Returns the default search provider. If the TemplateURLService hasn't been
187 // loaded, the default search provider is pulled from preferences. 204 // loaded, the default search provider is pulled from preferences.
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after
226 // notification types: 243 // notification types:
227 // . NOTIFY_HISTORY_URL_VISITED: adds keyword search terms if the visit 244 // . NOTIFY_HISTORY_URL_VISITED: adds keyword search terms if the visit
228 // corresponds to a keyword. 245 // corresponds to a keyword.
229 // . NOTIFY_GOOGLE_URL_UPDATED: updates mapping for any keywords containing 246 // . NOTIFY_GOOGLE_URL_UPDATED: updates mapping for any keywords containing
230 // a google base url replacement term. 247 // a google base url replacement term.
231 // . PREF_CHANGED: checks whether the default search engine has changed. 248 // . PREF_CHANGED: checks whether the default search engine has changed.
232 virtual void Observe(int type, 249 virtual void Observe(int type,
233 const NotificationSource& source, 250 const NotificationSource& source,
234 const NotificationDetails& details); 251 const NotificationDetails& details);
235 252
253 // SyncableService implementation.
254
255 // Returns all syncable TemplateURLs from this model as SyncData. This should
256 // include every search engine and no Extension keywords.
257 virtual SyncDataList GetAllSyncData(syncable::ModelType type) const OVERRIDE;
258 // Process new search engine changes from Sync, merging them into our local
259 // data. This may send notifications if local search engines are added,
260 // updated or removed.
261 virtual SyncError ProcessSyncChanges(
262 const tracked_objects::Location& from_here,
263 const SyncChangeList& change_list) OVERRIDE;
264 // Merge initial search engine data from Sync and push any local changes up
265 // to Sync. This may send notifications if local search engines are added,
266 // updated or removed.
267 virtual SyncError MergeDataAndStartSyncing(
268 syncable::ModelType type,
269 const SyncDataList& initial_sync_data,
270 SyncChangeProcessor* sync_processor) OVERRIDE;
271 virtual void StopSyncing(syncable::ModelType type) OVERRIDE;
272
273 // Processes a local TemplateURL change for Sync. |turl| is the TemplateURL
274 // that has been modified, and |type| is the Sync ChangeType that took place.
275 // This may send a new SyncChange to the cloud. If our model has not yet been
276 // associated with Sync, or if this is triggered by a Sync change, then this
277 // does nothing.
278 void ProcessTemplateURLChange(const TemplateURL* turl,
279 SyncChange::SyncChangeType type);
280
236 Profile* profile() const { return profile_; } 281 Profile* profile() const { return profile_; }
237 282
238 void SetSearchEngineDialogSlot(int slot) { 283 void SetSearchEngineDialogSlot(int slot) {
239 search_engine_dialog_chosen_slot_ = slot; 284 search_engine_dialog_chosen_slot_ = slot;
240 } 285 }
241 286
242 int GetSearchEngineDialogSlot() const { 287 int GetSearchEngineDialogSlot() const {
243 return search_engine_dialog_chosen_slot_; 288 return search_engine_dialog_chosen_slot_;
244 } 289 }
245 290
246 // Registers the preferences used to save a TemplateURL to prefs. 291 // Registers the preferences used to save a TemplateURL to prefs.
247 static void RegisterUserPrefs(PrefService* prefs); 292 static void RegisterUserPrefs(PrefService* prefs);
248 293
294 // Returns a SyncData with a sync representation of the search engine data
295 // from |turl|.
296 static SyncData CreateSyncDataFromTemplateURL(const TemplateURL& turl);
297
298 // Returns a heap-allocated TemplateURL, populated by |sync_data|'s fields.
299 // This does the opposite of CreateSyncDataFromTemplateURL. The caller owns
300 // the returned TemplateURL*.
301 static TemplateURL* CreateTemplateURLFromSyncData(const SyncData& sync_data);
302
303 // Attempts to generate a unique keyword for |turl| based on its original
304 // keyword. If its keyword is already unique, that is returned. Otherwise, it
305 // tries to return the autogenerated keyword if that is unique to the Service,
306 // and finally it repeatedly appends special characters to the keyword until
307 // it is unique to the Service.
308 string16 UniquifyKeyword(const TemplateURL& turl) const;
309
310 // Given a TemplateURL from Sync, resolves any keyword conflicts by checking
311 // the local keywords and uniquifying either the cloud keyword or a
312 // conflicting local keyword (whichever is older). If the cloud TURL is
313 // changed, then an appropriate SyncChange is appended to |change_list|. If
314 // a local TURL is changed, the service is updated with the new keyword. If
315 // there was no conflict to begin with, this does nothing. In the case of tied
316 // last_modified dates, |sync_turl| wins. Returns true iff there was a
317 // conflict.
318 bool ResolveSyncKeywordConflict(TemplateURL* sync_turl,
319 SyncChangeList& change_list);
320
321 // Returns a TemplateURL from the service that has the same keyword and search
322 // URL as |sync_turl|, if it exists.
323 const TemplateURL* FindDuplicateOfSyncTemplateURL(
324 const TemplateURL& sync_turl);
325
326 // Given a TemplateURL from the cloud and a local matching duplicate found by
327 // FindDuplcateOfSyncTemplateURL, merges the two. If |sync_url| is newer, this
328 // replaces |local_url| with |sync_url| using the service's Remove and Add.
329 // If |local_url| is newer, this copies the GUID from |sync_url| over to
330 // |local_url| and adds an update to change_list to notify the server of the
331 // change.
332 void MergeSyncAndLocalURLDuplicates(TemplateURL* sync_url,
333 TemplateURL* local_url,
334 SyncChangeList& change_list);
335
336 // Returns a map mapping Sync GUIDs to pointers to SyncData.
337 static SyncDataMap CreateGUIDToSyncDataMap(const SyncDataList& sync_data);
338
249 #if defined(UNIT_TEST) 339 #if defined(UNIT_TEST)
250 // Set a different time provider function, such as 340 // Set a different time provider function, such as
251 // base::MockTimeProvider::StaticNow, when testing calls to base::Time::Now. 341 // base::MockTimeProvider::StaticNow, when testing calls to base::Time::Now.
252 void set_time_provider(TimeProvider* time_provider) { 342 void set_time_provider(TimeProvider* time_provider) {
253 time_provider_ = time_provider; 343 time_provider_ = time_provider;
254 } 344 }
255 #endif 345 #endif
256 346
257 protected: 347 protected:
258 // Cover method for the method of the same name on the HistoryService. 348 // Cover method for the method of the same name on the HistoryService.
259 // url is the one that was visited with the given search terms. 349 // url is the one that was visited with the given search terms.
260 // 350 //
261 // This exists and is virtual for testing. 351 // This exists and is virtual for testing.
262 virtual void SetKeywordSearchTermsForURL(const TemplateURL* t_url, 352 virtual void SetKeywordSearchTermsForURL(const TemplateURL* t_url,
263 const GURL& url, 353 const GURL& url,
264 const string16& term); 354 const string16& term);
265 355
266 private: 356 private:
267 FRIEND_TEST_ALL_PREFIXES(TemplateURLServiceTest, BuildQueryTerms); 357 FRIEND_TEST_ALL_PREFIXES(TemplateURLServiceTest, BuildQueryTerms);
268 FRIEND_TEST_ALL_PREFIXES(TemplateURLServiceTest, TestManagedDefaultSearch); 358 FRIEND_TEST_ALL_PREFIXES(TemplateURLServiceTest, TestManagedDefaultSearch);
269 FRIEND_TEST_ALL_PREFIXES(TemplateURLServiceTest, 359 FRIEND_TEST_ALL_PREFIXES(TemplateURLServiceTest,
270 UpdateKeywordSearchTermsForURL); 360 UpdateKeywordSearchTermsForURL);
271 FRIEND_TEST_ALL_PREFIXES(TemplateURLServiceTest, 361 FRIEND_TEST_ALL_PREFIXES(TemplateURLServiceTest,
272 DontUpdateKeywordSearchForNonReplaceable); 362 DontUpdateKeywordSearchForNonReplaceable);
273 FRIEND_TEST_ALL_PREFIXES(TemplateURLServiceTest, ChangeGoogleBaseValue); 363 FRIEND_TEST_ALL_PREFIXES(TemplateURLServiceTest, ChangeGoogleBaseValue);
274 FRIEND_TEST_ALL_PREFIXES(TemplateURLServiceTest, MergeDeletesUnusedProviders); 364 FRIEND_TEST_ALL_PREFIXES(TemplateURLServiceTest, MergeDeletesUnusedProviders);
275 friend class TemplateURLServiceTestUtil; 365 friend class TemplateURLServiceTestUtil;
276 366
277 typedef std::map<string16, const TemplateURL*> KeywordToTemplateMap; 367 typedef std::map<string16, const TemplateURL*> KeywordToTemplateMap;
368 typedef std::map<std::string, const TemplateURL*> GUIDToTemplateMap;
278 369
279 // Helper functor for FindMatchingKeywords(), for finding the range of 370 // Helper functor for FindMatchingKeywords(), for finding the range of
280 // keywords which begin with a prefix. 371 // keywords which begin with a prefix.
281 class LessWithPrefix; 372 class LessWithPrefix;
282 373
283 void Init(const Initializer* initializers, int num_initializers); 374 void Init(const Initializer* initializers, int num_initializers);
284 375
285 void RemoveFromMaps(const TemplateURL* template_url); 376 void RemoveFromMaps(const TemplateURL* template_url);
286 377
287 // Removes the supplied template_url from the keyword maps. This searches 378 // Removes the supplied template_url from the keyword maps. This searches
(...skipping 105 matching lines...) Expand 10 before | Expand all | Expand 10 after
393 void RemoveProvidersCreatedByPolicy( 484 void RemoveProvidersCreatedByPolicy(
394 std::vector<TemplateURL*>* template_urls, 485 std::vector<TemplateURL*>* template_urls,
395 const TemplateURL** default_search_provider, 486 const TemplateURL** default_search_provider,
396 const TemplateURL* default_from_prefs); 487 const TemplateURL* default_from_prefs);
397 488
398 NotificationRegistrar registrar_; 489 NotificationRegistrar registrar_;
399 490
400 // Mapping from keyword to the TemplateURL. 491 // Mapping from keyword to the TemplateURL.
401 KeywordToTemplateMap keyword_to_template_map_; 492 KeywordToTemplateMap keyword_to_template_map_;
402 493
494 // Mapping from Sync GUIDs to the TemplateURL.
495 GUIDToTemplateMap guid_to_template_map_;
496
403 TemplateURLVector template_urls_; 497 TemplateURLVector template_urls_;
404 498
405 ObserverList<TemplateURLServiceObserver> model_observers_; 499 ObserverList<TemplateURLServiceObserver> model_observers_;
406 500
407 // Maps from host to set of TemplateURLs whose search url host is host. 501 // Maps from host to set of TemplateURLs whose search url host is host.
408 SearchHostToURLsMap provider_map_; 502 SearchHostToURLsMap provider_map_;
409 503
410 // Used to obtain the WebDataService. 504 // Used to obtain the WebDataService.
411 // When Load is invoked, if we haven't yet loaded, the WebDataService is 505 // When Load is invoked, if we haven't yet loaded, the WebDataService is
412 // obtained from the Profile. This allows us to lazily access the database. 506 // obtained from the Profile. This allows us to lazily access the database.
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after
450 // ID assigned to next TemplateURL added to this model. This is an ever 544 // ID assigned to next TemplateURL added to this model. This is an ever
451 // increasing integer that is initialized from the database. 545 // increasing integer that is initialized from the database.
452 TemplateURLID next_id_; 546 TemplateURLID next_id_;
453 547
454 // List of extension IDs waiting for Load to have keywords registered. 548 // List of extension IDs waiting for Load to have keywords registered.
455 std::vector<std::string> pending_extension_ids_; 549 std::vector<std::string> pending_extension_ids_;
456 550
457 // Function returning current time in base::Time units. 551 // Function returning current time in base::Time units.
458 TimeProvider* time_provider_; 552 TimeProvider* time_provider_;
459 553
554 // Do we have an active association between the TemplateURLs and sync models?
555 // Set in MergeDataAndStartSyncing, reset in StopSyncing. While this is not
556 // set, we ignore any local search engine changes (when we start syncing we
557 // will look up the most recent values anyways).
558 bool models_associated_;
559
560 // Whether we're currently processing changes from the syncer. While this is
561 // true, we ignore any local search engine changes, since we triggered them.
562 bool processing_syncer_changes_;
563
564 // Sync's SyncChange handler. We push all our changes through this.
565 SyncChangeProcessor* sync_processor_;
566
460 DISALLOW_COPY_AND_ASSIGN(TemplateURLService); 567 DISALLOW_COPY_AND_ASSIGN(TemplateURLService);
461 }; 568 };
462 569
463 #endif // CHROME_BROWSER_SEARCH_ENGINES_TEMPLATE_URL_SERVICE_H_ 570 #endif // CHROME_BROWSER_SEARCH_ENGINES_TEMPLATE_URL_SERVICE_H_
OLDNEW
« 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