| Index: chrome/browser/prefs/pref_model_associator.h
|
| diff --git a/chrome/browser/prefs/pref_model_associator.h b/chrome/browser/prefs/pref_model_associator.h
|
| new file mode 100644
|
| index 0000000000000000000000000000000000000000..922856930818b8a2d671351bc233c15146252c0b
|
| --- /dev/null
|
| +++ b/chrome/browser/prefs/pref_model_associator.h
|
| @@ -0,0 +1,171 @@
|
| +// Copyright (c) 2011 The Chromium Authors. All rights reserved.
|
| +// Use of this source code is governed by a BSD-style license that can be
|
| +// found in the LICENSE file.
|
| +
|
| +#ifndef CHROME_BROWSER_PREFS_PREF_MODEL_ASSOCIATOR_H_
|
| +#define CHROME_BROWSER_PREFS_PREF_MODEL_ASSOCIATOR_H_
|
| +#pragma once
|
| +
|
| +#include <map>
|
| +#include <set>
|
| +#include <string>
|
| +
|
| +#include "base/basictypes.h"
|
| +#include "base/compiler_specific.h"
|
| +#include "base/threading/non_thread_safe.h"
|
| +#include "chrome/browser/prefs/pref_service.h"
|
| +#include "chrome/browser/sync/glue/model_associator.h"
|
| +#include "chrome/browser/sync/unrecoverable_error_handler.h"
|
| +#include "content/common/notification_observer.h"
|
| +#include "content/common/notification_registrar.h"
|
| +
|
| +class Profile;
|
| +class ProfileSyncService;
|
| +class Value;
|
| +
|
| +namespace sync_api {
|
| + class WriteNode;
|
| + class WriteTransaction;
|
| +}
|
| +
|
| +namespace browser_sync {
|
| + class ChangeProcessor;
|
| + class GenericChangeProcessor;
|
| +}
|
| +
|
| +static const char kPreferencesTag[] = "google_chrome_preferences";
|
| +
|
| +// Contains all model association related logic:
|
| +// * Algorithm to associate preferences model and sync model.
|
| +// TODO(zea): Rewrite to use change processor instead of transactions.
|
| +class PrefModelAssociator
|
| + : public browser_sync::NewAssociatorInterface,
|
| + public NotificationObserver,
|
| + public base::NonThreadSafe {
|
| + public:
|
| + explicit PrefModelAssociator(PrefService* pref_service);
|
| +
|
| + // NewAssociatorInterface implementation.
|
| + virtual bool AssociateModels() OVERRIDE;
|
| + virtual bool DisassociateModels() OVERRIDE;
|
| + virtual bool SyncModelHasUserCreatedNodes(bool* has_nodes) OVERRIDE;
|
| + virtual void AbortAssociation() OVERRIDE; // Not implemented.
|
| + virtual bool CryptoReadyIfNecessary() OVERRIDE;
|
| + virtual syncable::ModelType model_type() const OVERRIDE;
|
| + virtual void set_change_processor(
|
| + browser_sync::ChangeProcessor* processor) OVERRIDE;
|
| + virtual browser_sync::ChangeProcessor* change_processor() OVERRIDE;
|
| + virtual void ApplyChangesFromSync(
|
| + const sync_api::BaseTransaction* trans,
|
| + const sync_api::SyncManager::ChangeRecord* changes,
|
| + int change_count) OVERRIDE;
|
| +
|
| + // NotificationObserver implementation.
|
| + virtual void Observe(NotificationType type,
|
| + const NotificationSource& source,
|
| + const NotificationDetails& details) OVERRIDE;
|
| +
|
| + // Returns the list of preference names that should be monitored for changes.
|
| + // Only preferences that are registered will be in this list.
|
| + std::set<std::string> synced_preferences();
|
| +
|
| + // Register a preference with the specified name for syncing. We do not care
|
| + // about the type at registration time, but when changes arrive from the
|
| + // syncer, we check if they can be applied and if not drop them.
|
| + virtual void RegisterPref(const char* name);
|
| +
|
| + // Returns true if the specified preference is registered for syncing.
|
| + virtual bool IsPrefRegistered(const char* name);
|
| +
|
| + // Process a local preference change.
|
| + virtual void ProcessPrefChange(const std::string& name);
|
| +
|
| + // Merges the value of local_pref into the supplied server_value and returns
|
| + // the result (caller takes ownership). If there is a conflict, the server
|
| + // value always takes precedence. Note that only certain preferences will
|
| + // actually be merged, all others will return a copy of the server value. See
|
| + // the method's implementation for details.
|
| + static Value* MergePreference(const PrefService::Preference& local_pref,
|
| + const Value& server_value);
|
| +
|
| + // Writes the value of pref into the specified node. Returns true
|
| + // upon success.
|
| + static bool WritePreferenceToNode(const std::string& name,
|
| + const Value& value,
|
| + sync_api::WriteNode* node);
|
| +
|
| + // Extract preference value and name from sync specifics.
|
| + Value* ReadPreferenceSpecifics(
|
| + const sync_pb::PreferenceSpecifics& specifics,
|
| + std::string* name);
|
| +
|
| + // Returns the sync id for the given preference name, or sync_api::kInvalidId
|
| + // if the preference name is not associated to any sync id.
|
| + int64 GetSyncIdFromChromeId(const std::string& node_id);
|
| + protected:
|
| + friend class ProfileSyncServicePreferenceTest;
|
| +
|
| + // For testing.
|
| + PrefModelAssociator();
|
| +
|
| + // Protected since w're refcounted.
|
| + virtual ~PrefModelAssociator();
|
| +
|
| + // Create an association for a given preference. A sync node is created if
|
| + // necessary and the value is read from or written to the node as appropriate.
|
| + bool InitPrefNodeAndAssociate(sync_api::WriteTransaction* trans,
|
| + const sync_api::BaseNode& root,
|
| + const PrefService::Preference* pref);
|
| +
|
| + // Associates the given preference name with the given sync id.
|
| + void Associate(const PrefService::Preference* node, int64 sync_id);
|
| +
|
| + // Remove the association that corresponds to the given sync id.
|
| + void Disassociate(int64 sync_id);
|
| +
|
| + // Returns whether a node with the given permanent tag was found and update
|
| + // |sync_id| with that node's id.
|
| + bool GetSyncIdForTaggedNode(const std::string& tag, int64* sync_id);
|
| +
|
| + // Perform any additional operations that need to happen after a preference
|
| + // has been updated.
|
| + void AfterUpdateOperations(const std::string& pref_name);
|
| +
|
| + typedef std::map<std::string, int64> PreferenceNameToSyncIdMap;
|
| + typedef std::map<int64, std::string> SyncIdToPreferenceNameMap;
|
| +
|
| + static Value* MergeListValues(const Value& from_value, const Value& to_value);
|
| + static Value* MergeDictionaryValues(const Value& from_value,
|
| + const Value& to_value);
|
| +
|
| + PrefService* pref_service_;
|
| + ProfileSyncService* sync_service_;
|
| + int64 preferences_node_id_;
|
| +
|
| + // Do we have an active association between the preferences and sync models?
|
| + // Set by AssociateModels, reset by DisassociateModels.
|
| + bool models_associated_;
|
| +
|
| + // Whether we're currently processing changes from the syncer. While this is
|
| + // true, we ignore any pref changes, since we triggered them.
|
| + bool processing_syncer_changes_;
|
| +
|
| + // Whether we're currently processing local syncapi changes. While this is
|
| + // true, we ignore any pref changes, since we triggered them.
|
| + bool processing_syncapi_changes_;
|
| +
|
| + // The Registrar used to register ForeignSessionHandler for notifications.
|
| + NotificationRegistrar registrar_;
|
| +
|
| + PreferenceNameToSyncIdMap id_map_;
|
| + SyncIdToPreferenceNameMap id_map_inverse_;
|
| + std::set<std::string> synced_preferences_;
|
| +
|
| + // TODO(zea): Get rid of this and use one owned by the PSS.
|
| + // We create, but don't own this (will be destroyed by data type controller).
|
| + browser_sync::GenericChangeProcessor* change_processor_;
|
| +
|
| + DISALLOW_COPY_AND_ASSIGN(PrefModelAssociator);
|
| +};
|
| +
|
| +#endif // CHROME_BROWSER_PREFS_PREF_MODEL_ASSOCIATOR_H_
|
|
|