1 // Copyright (c) 2012 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.
5 #ifndef CHROME_BROWSER_SYNC_GLUE_PASSWORD_MODEL_ASSOCIATOR_H_
6 #define CHROME_BROWSER_SYNC_GLUE_PASSWORD_MODEL_ASSOCIATOR_H_
12 #include "base/basictypes.h"
13 #include "base/compiler_specific.h"
14 #include "base/synchronization/lock.h"
15 #include "chrome/browser/history/history_types.h"
16 #include "chrome/browser/sync/glue/data_type_error_handler.h"
17 #include "chrome/browser/sync/glue/model_associator.h"
18 #include "sync/protocol/password_specifics.pb.h"
21 class ProfileSyncService
;
33 class WriteTransaction
;
36 namespace browser_sync
{
38 extern const char kPasswordTag
[];
40 // Contains all model association related logic:
41 // * Algorithm to associate password model and sync model.
42 // * Persisting model associations and loading them back.
43 // We do not check if we have local data before this runs; we always
45 class PasswordModelAssociator
46 : public PerDataTypeAssociatorInterface
<std::string
, std::string
> {
48 typedef std::vector
<autofill::PasswordForm
> PasswordVector
;
50 static syncer::ModelType
model_type() { return syncer::PASSWORDS
; }
51 PasswordModelAssociator(ProfileSyncService
* sync_service
,
52 PasswordStore
* password_store
,
53 DataTypeErrorHandler
* error_handler
);
54 virtual ~PasswordModelAssociator();
56 // PerDataTypeAssociatorInterface implementation.
58 // Iterates through the sync model looking for matched pairs of items.
59 virtual syncer::SyncError
AssociateModels(
60 syncer::SyncMergeResult
* local_merge_result
,
61 syncer::SyncMergeResult
* syncer_merge_result
) OVERRIDE
;
63 // Delete all password nodes.
64 bool DeleteAllNodes(syncer::WriteTransaction
* trans
);
66 // Clears all associations.
67 virtual syncer::SyncError
DisassociateModels() OVERRIDE
;
69 // The has_nodes out param is true if the sync model has nodes other
70 // than the permanent tagged nodes.
71 virtual bool SyncModelHasUserCreatedNodes(bool* has_nodes
) OVERRIDE
;
73 // See ModelAssociator interface.
74 virtual void AbortAssociation() OVERRIDE
;
76 // See ModelAssociator interface.
77 virtual bool CryptoReadyIfNecessary() OVERRIDE
;
80 virtual const std::string
* GetChromeNodeFromSyncId(int64 sync_id
) OVERRIDE
;
83 virtual bool InitSyncNodeFromChromeId(const std::string
& node_id
,
84 syncer::BaseNode
* sync_node
) OVERRIDE
;
86 // Returns the sync id for the given password name, or syncer::kInvalidId
87 // if the password name is not associated to any sync id.
88 virtual int64
GetSyncIdFromChromeId(const std::string
& node_id
) OVERRIDE
;
90 // Associates the given password name with the given sync id.
91 virtual void Associate(const std::string
* node
, int64 sync_id
) OVERRIDE
;
93 // Remove the association that corresponds to the given sync id.
94 virtual void Disassociate(int64 sync_id
) OVERRIDE
;
96 // Returns whether a node with the given permanent tag was found and update
97 // |sync_id| with that node's id.
98 virtual bool GetSyncIdForTaggedNode(const std::string
& tag
, int64
* sync_id
);
100 syncer::SyncError
WriteToPasswordStore(const PasswordVector
* new_passwords
,
101 const PasswordVector
* updated_passwords
,
102 const PasswordVector
* deleted_passwords
);
104 static std::string
MakeTag(const autofill::PasswordForm
& password
);
105 static std::string
MakeTag(const sync_pb::PasswordSpecificsData
& password
);
106 static std::string
MakeTag(const std::string
& origin_url
,
107 const std::string
& username_element
,
108 const std::string
& username_value
,
109 const std::string
& password_element
,
110 const std::string
& signon_realm
);
112 static void CopyPassword(const sync_pb::PasswordSpecificsData
& password
,
113 autofill::PasswordForm
* new_password
);
115 static bool MergePasswords(const sync_pb::PasswordSpecificsData
& password
,
116 const autofill::PasswordForm
& password_form
,
117 autofill::PasswordForm
* new_password
);
118 static void WriteToSyncNode(const autofill::PasswordForm
& password_form
,
119 syncer::WriteNode
* node
);
122 typedef std::map
<std::string
, int64
> PasswordToSyncIdMap
;
123 typedef std::map
<int64
, std::string
> SyncIdToPasswordMap
;
125 ProfileSyncService
* sync_service_
;
126 scoped_refptr
<PasswordStore
> password_store_
;
127 int64 password_node_id_
;
129 // Set true by AbortAssociation.
130 bool abort_association_requested_
;
131 base::Lock association_lock_
;
133 base::MessageLoop
* expected_loop_
;
135 PasswordToSyncIdMap id_map_
;
136 SyncIdToPasswordMap id_map_inverse_
;
137 DataTypeErrorHandler
* error_handler_
;
139 DISALLOW_COPY_AND_ASSIGN(PasswordModelAssociator
);
142 } // namespace browser_sync
144 #endif // CHROME_BROWSER_SYNC_GLUE_PASSWORD_MODEL_ASSOCIATOR_H_