| 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/glue/model_associator.h" |
| 18 #include "chrome/browser/sync/unrecoverable_error_handler.h" |
| 19 #include "content/common/notification_observer.h" |
| 20 #include "content/common/notification_registrar.h" |
| 21 |
| 22 class Profile; |
| 23 class ProfileSyncService; |
| 24 class Value; |
| 25 |
| 26 namespace sync_api { |
| 27 class WriteNode; |
| 28 class WriteTransaction; |
| 29 } |
| 30 |
| 31 namespace browser_sync { |
| 32 class ChangeProcessor; |
| 33 class GenericChangeProcessor; |
| 34 } |
| 35 |
| 36 static const char kPreferencesTag[] = "google_chrome_preferences"; |
| 37 |
| 38 // Contains all model association related logic: |
| 39 // * Algorithm to associate preferences model and sync model. |
| 40 // TODO(zea): Rewrite to use change processor instead of transactions. |
| 41 class PrefModelAssociator |
| 42 : public browser_sync::NewAssociatorInterface, |
| 43 public NotificationObserver, |
| 44 public base::NonThreadSafe { |
| 45 public: |
| 46 explicit PrefModelAssociator(PrefService* pref_service); |
| 47 |
| 48 // NewAssociatorInterface 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 syncable::ModelType model_type() const OVERRIDE; |
| 55 virtual void set_change_processor( |
| 56 browser_sync::ChangeProcessor* processor) OVERRIDE; |
| 57 virtual browser_sync::ChangeProcessor* change_processor() OVERRIDE; |
| 58 virtual void ApplyChangesFromSync( |
| 59 const sync_api::BaseTransaction* trans, |
| 60 const sync_api::SyncManager::ChangeRecord* changes, |
| 61 int change_count) OVERRIDE; |
| 62 |
| 63 // NotificationObserver implementation. |
| 64 virtual void Observe(NotificationType type, |
| 65 const NotificationSource& source, |
| 66 const NotificationDetails& details) OVERRIDE; |
| 67 |
| 68 // Returns the list of preference names that should be monitored for changes. |
| 69 // Only preferences that are registered will be in this list. |
| 70 std::set<std::string> synced_preferences(); |
| 71 |
| 72 // Register a preference with the specified name for syncing. We do not care |
| 73 // about the type at registration time, but when changes arrive from the |
| 74 // syncer, we check if they can be applied and if not drop them. |
| 75 virtual void RegisterPref(const char* name); |
| 76 |
| 77 // Returns true if the specified preference is registered for syncing. |
| 78 virtual bool IsPrefRegistered(const char* name); |
| 79 |
| 80 // Process a local preference change. |
| 81 virtual void ProcessPrefChange(const std::string& name); |
| 82 |
| 83 // Merges the value of local_pref into the supplied server_value and returns |
| 84 // the result (caller takes ownership). If there is a conflict, the server |
| 85 // value always takes precedence. Note that only certain preferences will |
| 86 // actually be merged, all others will return a copy of the server value. See |
| 87 // the method's implementation for details. |
| 88 static Value* MergePreference(const PrefService::Preference& local_pref, |
| 89 const Value& server_value); |
| 90 |
| 91 // Writes the value of pref into the specified node. Returns true |
| 92 // upon success. |
| 93 static bool WritePreferenceToNode(const std::string& name, |
| 94 const Value& value, |
| 95 sync_api::WriteNode* node); |
| 96 |
| 97 // Extract preference value and name from sync specifics. |
| 98 Value* ReadPreferenceSpecifics( |
| 99 const sync_pb::PreferenceSpecifics& specifics, |
| 100 std::string* name); |
| 101 |
| 102 // Returns the sync id for the given preference name, or sync_api::kInvalidId |
| 103 // if the preference name is not associated to any sync id. |
| 104 int64 GetSyncIdFromChromeId(const std::string& node_id); |
| 105 protected: |
| 106 friend class ProfileSyncServicePreferenceTest; |
| 107 |
| 108 // For testing. |
| 109 PrefModelAssociator(); |
| 110 |
| 111 // Protected since w're refcounted. |
| 112 virtual ~PrefModelAssociator(); |
| 113 |
| 114 // Create an association for a given preference. A sync node is created if |
| 115 // necessary and the value is read from or written to the node as appropriate. |
| 116 bool InitPrefNodeAndAssociate(sync_api::WriteTransaction* trans, |
| 117 const sync_api::BaseNode& root, |
| 118 const PrefService::Preference* pref); |
| 119 |
| 120 // Associates the given preference name with the given sync id. |
| 121 void Associate(const PrefService::Preference* node, int64 sync_id); |
| 122 |
| 123 // Remove the association that corresponds to the given sync id. |
| 124 void Disassociate(int64 sync_id); |
| 125 |
| 126 // Returns whether a node with the given permanent tag was found and update |
| 127 // |sync_id| with that node's id. |
| 128 bool GetSyncIdForTaggedNode(const std::string& tag, int64* sync_id); |
| 129 |
| 130 // Perform any additional operations that need to happen after a preference |
| 131 // has been updated. |
| 132 void AfterUpdateOperations(const std::string& pref_name); |
| 133 |
| 134 typedef std::map<std::string, int64> PreferenceNameToSyncIdMap; |
| 135 typedef std::map<int64, std::string> SyncIdToPreferenceNameMap; |
| 136 |
| 137 static Value* MergeListValues(const Value& from_value, const Value& to_value); |
| 138 static Value* MergeDictionaryValues(const Value& from_value, |
| 139 const Value& to_value); |
| 140 |
| 141 PrefService* pref_service_; |
| 142 ProfileSyncService* sync_service_; |
| 143 int64 preferences_node_id_; |
| 144 |
| 145 // Do we have an active association between the preferences and sync models? |
| 146 // Set by AssociateModels, reset by DisassociateModels. |
| 147 bool models_associated_; |
| 148 |
| 149 // Whether we're currently processing changes from the syncer. While this is |
| 150 // true, we ignore any pref changes, since we triggered them. |
| 151 bool processing_syncer_changes_; |
| 152 |
| 153 // Whether we're currently processing local syncapi changes. While this is |
| 154 // true, we ignore any pref changes, since we triggered them. |
| 155 bool processing_syncapi_changes_; |
| 156 |
| 157 // The Registrar used to register ForeignSessionHandler for notifications. |
| 158 NotificationRegistrar registrar_; |
| 159 |
| 160 PreferenceNameToSyncIdMap id_map_; |
| 161 SyncIdToPreferenceNameMap id_map_inverse_; |
| 162 std::set<std::string> synced_preferences_; |
| 163 |
| 164 // TODO(zea): Get rid of this and use one owned by the PSS. |
| 165 // We create, but don't own this (will be destroyed by data type controller). |
| 166 browser_sync::GenericChangeProcessor* change_processor_; |
| 167 |
| 168 DISALLOW_COPY_AND_ASSIGN(PrefModelAssociator); |
| 169 }; |
| 170 |
| 171 #endif // CHROME_BROWSER_PREFS_PREF_MODEL_ASSOCIATOR_H_ |
| OLD | NEW |