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