Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #ifndef CHROME_BROWSER_PREFS_PREF_MODEL_ASSOCIATOR_H_ | |
| 6 #define CHROME_BROWSER_PREFS_PREF_MODEL_ASSOCIATOR_H_ | |
| 7 #pragma once | |
| 8 | |
| 9 #include <map> | |
| 10 #include <set> | |
| 11 #include <string> | |
| 12 | |
| 13 #include "base/basictypes.h" | |
| 14 #include "base/compiler_specific.h" | |
| 15 #include "base/threading/non_thread_safe.h" | |
| 16 #include "chrome/browser/prefs/pref_service.h" | |
| 17 #include "chrome/browser/sync/syncable_service.h" | |
| 18 #include "chrome/browser/sync/unrecoverable_error_handler.h" | |
| 19 | |
| 20 class Profile; | |
| 21 class ProfileSyncService; | |
| 22 class Value; | |
| 23 | |
| 24 namespace sync_api { | |
| 25 class WriteNode; | |
| 26 class WriteTransaction; | |
| 27 } | |
| 28 | |
| 29 namespace browser_sync { | |
| 30 class ChangeProcessor; | |
| 31 class GenericChangeProcessor; | |
| 32 } | |
| 33 | |
| 34 static const char kPreferencesTag[] = "google_chrome_preferences"; | |
|
tim (not reviewing)
2011/05/05 17:45:42
probably shouldn't have this in the global ns, at
Nicolas Zea
2011/05/06 00:26:43
Done.
| |
| 35 | |
| 36 // Contains all model association related logic: | |
| 37 // * Algorithm to associate preferences model and sync model. | |
| 38 // TODO(sync): Rewrite to use change processor instead of transactions. | |
| 39 // TODO(sync): Merge this into PrefService. We don't actually need the id_map | |
| 40 // mapping since the sync node tags are the same as the pref names. | |
| 41 class PrefModelAssociator | |
| 42 : public SyncableService, | |
| 43 public base::NonThreadSafe { | |
| 44 public: | |
| 45 explicit PrefModelAssociator(PrefService* pref_service); | |
| 46 virtual ~PrefModelAssociator(); | |
| 47 | |
| 48 // SyncableService implementation. | |
| 49 virtual bool AssociateModels() OVERRIDE; | |
| 50 virtual bool DisassociateModels() OVERRIDE; | |
| 51 virtual bool SyncModelHasUserCreatedNodes(bool* has_nodes) OVERRIDE; | |
| 52 virtual void AbortAssociation() OVERRIDE; // Not implemented. | |
| 53 virtual bool CryptoReadyIfNecessary() OVERRIDE; | |
| 54 virtual void ApplyChangesFromSync( | |
| 55 const sync_api::BaseTransaction* trans, | |
| 56 const sync_api::SyncManager::ChangeRecord* changes, | |
| 57 int change_count) OVERRIDE; | |
| 58 virtual void SetupSync( | |
| 59 ProfileSyncService* sync_service, | |
| 60 browser_sync::GenericChangeProcessor* change_processor) OVERRIDE; | |
| 61 | |
| 62 // Returns the list of preference names that should be monitored for changes. | |
| 63 // Only preferences that are registered will be in this list. | |
| 64 std::set<std::string> synced_preferences() const; | |
| 65 | |
| 66 // Register a preference with the specified name for syncing. We do not care | |
| 67 // about the type at registration time, but when changes arrive from the | |
| 68 // syncer, we check if they can be applied and if not drop them. | |
| 69 virtual void RegisterPref(const char* name); | |
| 70 | |
| 71 // Returns true if the specified preference is registered for syncing. | |
| 72 virtual bool IsPrefRegistered(const char* name); | |
| 73 | |
| 74 // Process a local preference change. | |
| 75 virtual void ProcessPrefChange(const std::string& name); | |
| 76 | |
| 77 // Merges the value of local_pref into the supplied server_value and returns | |
| 78 // the result (caller takes ownership). If there is a conflict, the server | |
| 79 // value always takes precedence. Note that only certain preferences will | |
| 80 // actually be merged, all others will return a copy of the server value. See | |
| 81 // the method's implementation for details. | |
| 82 static Value* MergePreference(const PrefService::Preference& local_pref, | |
| 83 const Value& server_value); | |
| 84 | |
| 85 // Writes the value of pref into the specified node. Returns true | |
| 86 // upon success. | |
| 87 static bool WritePreferenceToNode(const std::string& name, | |
| 88 const Value& value, | |
| 89 sync_api::WriteNode* node); | |
| 90 | |
| 91 // Extract preference value and name from sync specifics. | |
| 92 Value* ReadPreferenceSpecifics( | |
| 93 const sync_pb::PreferenceSpecifics& specifics, | |
| 94 std::string* name); | |
| 95 | |
| 96 // Returns the sync id for the given preference name, or sync_api::kInvalidId | |
| 97 // if the preference name is not associated to any sync id. | |
| 98 int64 GetSyncIdFromChromeId(const std::string& node_id); | |
| 99 protected: | |
| 100 friend class ProfileSyncServicePreferenceTest; | |
| 101 | |
| 102 // For testing. | |
| 103 PrefModelAssociator(); | |
| 104 | |
| 105 // Create an association for a given preference. A sync node is created if | |
| 106 // necessary and the value is read from or written to the node as appropriate. | |
| 107 bool InitPrefNodeAndAssociate(sync_api::WriteTransaction* trans, | |
| 108 const sync_api::BaseNode& root, | |
| 109 const PrefService::Preference* pref); | |
| 110 | |
| 111 // Associates the given preference name with the given sync id. | |
| 112 void Associate(const PrefService::Preference* node, int64 sync_id); | |
| 113 | |
| 114 // Remove the association that corresponds to the given sync id. | |
| 115 void Disassociate(int64 sync_id); | |
| 116 | |
| 117 // Returns whether a node with the given permanent tag was found and update | |
| 118 // |sync_id| with that node's id. | |
| 119 bool GetSyncIdForTaggedNode(const std::string& tag, int64* sync_id); | |
| 120 | |
| 121 // Perform any additional operations that need to happen after a preference | |
| 122 // has been updated. | |
| 123 void SendUpdateNotificationsIfNecessary(const std::string& pref_name); | |
| 124 | |
| 125 typedef std::map<std::string, int64> PreferenceNameToSyncIdMap; | |
| 126 typedef std::map<int64, std::string> SyncIdToPreferenceNameMap; | |
| 127 | |
| 128 static Value* MergeListValues(const Value& from_value, const Value& to_value); | |
| 129 static Value* MergeDictionaryValues(const Value& from_value, | |
| 130 const Value& to_value); | |
| 131 | |
| 132 PrefService* pref_service_; | |
| 133 ProfileSyncService* sync_service_; | |
| 134 | |
| 135 // Do we have an active association between the preferences and sync models? | |
| 136 // Set by AssociateModels, reset by DisassociateModels. | |
| 137 bool models_associated_; | |
| 138 | |
| 139 // Whether we're currently processing changes from the syncer. While this is | |
| 140 // true, we ignore any pref changes, since we triggered them. | |
| 141 bool processing_syncer_changes_; | |
|
tim (not reviewing)
2011/05/05 17:45:42
think we decided these could be combined after all
Nicolas Zea
2011/05/06 00:26:43
Done.
| |
| 142 | |
| 143 // Whether we're currently processing local syncapi changes. While this is | |
| 144 // true, we ignore any pref changes, since we triggered them. | |
| 145 bool processing_syncapi_changes_; | |
| 146 | |
| 147 PreferenceNameToSyncIdMap id_map_; | |
| 148 SyncIdToPreferenceNameMap id_map_inverse_; | |
| 149 std::set<std::string> synced_preferences_; | |
| 150 | |
| 151 // TODO(zea): Get rid of this and use one owned by the PSS. | |
| 152 // We create, but don't own this (will be destroyed by data type controller). | |
| 153 browser_sync::GenericChangeProcessor* change_processor_; | |
| 154 | |
| 155 DISALLOW_COPY_AND_ASSIGN(PrefModelAssociator); | |
| 156 }; | |
| 157 | |
| 158 #endif // CHROME_BROWSER_PREFS_PREF_MODEL_ASSOCIATOR_H_ | |
| OLD | NEW |