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. 4 5 #ifndef CHROME_BROWSER_SYNC_GLUE_PASSWORD_MODEL_ASSOCIATOR_H_ 6 #define CHROME_BROWSER_SYNC_GLUE_PASSWORD_MODEL_ASSOCIATOR_H_ 7 8 #include <map> 9 #include <string> 10 #include <vector> 11 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" 19 20 class PasswordStore; 21 class ProfileSyncService; 22 23 namespace base { 24 class MessageLoop; 25 } 26 27 namespace content { 28 struct PasswordForm; 29 } 30 31 namespace syncer { 32 class WriteNode; 33 class WriteTransaction; 34 } 35 36 namespace browser_sync { 37 38 extern const char kPasswordTag[]; 39 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 44 // merge and sync. 45 class PasswordModelAssociator 46 : public PerDataTypeAssociatorInterface<std::string, std::string> { 47 public: 48 typedef std::vector<content::PasswordForm> PasswordVector; 49 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(); 55 56 // PerDataTypeAssociatorInterface implementation. 57 // 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; 62 63 // Delete all password nodes. 64 bool DeleteAllNodes(syncer::WriteTransaction* trans); 65 66 // Clears all associations. 67 virtual syncer::SyncError DisassociateModels() OVERRIDE; 68 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; 72 73 // See ModelAssociator interface. 74 virtual void AbortAssociation() OVERRIDE; 75 76 // See ModelAssociator interface. 77 virtual bool CryptoReadyIfNecessary() OVERRIDE; 78 79 // Not implemented. 80 virtual const std::string* GetChromeNodeFromSyncId(int64 sync_id) OVERRIDE; 81 82 // Not implemented. 83 virtual bool InitSyncNodeFromChromeId(const std::string& node_id, 84 syncer::BaseNode* sync_node) OVERRIDE; 85 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; 89 90 // Associates the given password name with the given sync id. 91 virtual void Associate(const std::string* node, int64 sync_id) OVERRIDE; 92 93 // Remove the association that corresponds to the given sync id. 94 virtual void Disassociate(int64 sync_id) OVERRIDE; 95 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); 99 100 syncer::SyncError WriteToPasswordStore(const PasswordVector* new_passwords, 101 const PasswordVector* updated_passwords, 102 const PasswordVector* deleted_passwords); 103 104 static std::string MakeTag(const content::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); 111 112 static void CopyPassword(const sync_pb::PasswordSpecificsData& password, 113 content::PasswordForm* new_password); 114 115 static bool MergePasswords(const sync_pb::PasswordSpecificsData& password, 116 const content::PasswordForm& password_form, 117 content::PasswordForm* new_password); 118 static void WriteToSyncNode(const content::PasswordForm& password_form, 119 syncer::WriteNode* node); 120 121 private: 122 typedef std::map<std::string, int64> PasswordToSyncIdMap; 123 typedef std::map<int64, std::string> SyncIdToPasswordMap; 124 125 ProfileSyncService* sync_service_; 126 PasswordStore* password_store_; 127 int64 password_node_id_; 128 129 // Set true by AbortAssociation. 130 bool abort_association_requested_; 131 base::Lock association_lock_; 132 133 base::MessageLoop* expected_loop_; 134 135 PasswordToSyncIdMap id_map_; 136 SyncIdToPasswordMap id_map_inverse_; 137 DataTypeErrorHandler* error_handler_; 138 139 DISALLOW_COPY_AND_ASSIGN(PasswordModelAssociator); 140 }; 141 142 } // namespace browser_sync 143 144 #endif // CHROME_BROWSER_SYNC_GLUE_PASSWORD_MODEL_ASSOCIATOR_H_ 145