OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/browser/sync/glue/password_model_associator.h" | 5 #include "chrome/browser/sync/glue/password_model_associator.h" |
6 | 6 |
7 #include <set> | 7 #include <set> |
8 | 8 |
9 #include "base/stl_util.h" | 9 #include "base/stl_util.h" |
10 #include "base/tracked.h" | 10 #include "base/tracked.h" |
11 #include "base/utf_string_conversions.h" | 11 #include "base/utf_string_conversions.h" |
12 #include "chrome/browser/password_manager/password_store.h" | 12 #include "chrome/browser/password_manager/password_store.h" |
13 #include "chrome/browser/sync/api/sync_error.h" | |
14 #include "chrome/browser/sync/engine/syncapi.h" | 13 #include "chrome/browser/sync/engine/syncapi.h" |
15 #include "chrome/browser/sync/profile_sync_service.h" | 14 #include "chrome/browser/sync/profile_sync_service.h" |
16 #include "chrome/browser/sync/protocol/password_specifics.pb.h" | 15 #include "chrome/browser/sync/protocol/password_specifics.pb.h" |
17 #include "net/base/escape.h" | 16 #include "net/base/escape.h" |
18 #include "webkit/glue/password_form.h" | 17 #include "webkit/glue/password_form.h" |
19 | 18 |
20 namespace browser_sync { | 19 namespace browser_sync { |
21 | 20 |
22 const char kPasswordTag[] = "google_chrome_passwords"; | 21 const char kPasswordTag[] = "google_chrome_passwords"; |
23 | 22 |
24 PasswordModelAssociator::PasswordModelAssociator( | 23 PasswordModelAssociator::PasswordModelAssociator( |
25 ProfileSyncService* sync_service, | 24 ProfileSyncService* sync_service, |
26 PasswordStore* password_store) | 25 PasswordStore* password_store) |
27 : sync_service_(sync_service), | 26 : sync_service_(sync_service), |
28 password_store_(password_store), | 27 password_store_(password_store), |
29 password_node_id_(sync_api::kInvalidId), | 28 password_node_id_(sync_api::kInvalidId), |
30 abort_association_pending_(false), | 29 abort_association_pending_(false), |
31 expected_loop_(MessageLoop::current()) { | 30 expected_loop_(MessageLoop::current()) { |
32 DCHECK(sync_service_); | 31 DCHECK(sync_service_); |
33 DCHECK(password_store_); | 32 DCHECK(password_store_); |
34 #if defined(OS_MACOSX) | 33 #if defined(OS_MACOSX) |
35 DCHECK(!BrowserThread::CurrentlyOn(BrowserThread::UI)); | 34 DCHECK(!BrowserThread::CurrentlyOn(BrowserThread::UI)); |
36 #else | 35 #else |
37 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::DB)); | 36 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::DB)); |
38 #endif | 37 #endif |
39 } | 38 } |
40 | 39 |
41 PasswordModelAssociator::~PasswordModelAssociator() {} | 40 PasswordModelAssociator::~PasswordModelAssociator() {} |
42 | 41 |
43 bool PasswordModelAssociator::AssociateModels(SyncError* error) { | 42 bool PasswordModelAssociator::AssociateModels() { |
44 DCHECK(expected_loop_ == MessageLoop::current()); | 43 DCHECK(expected_loop_ == MessageLoop::current()); |
45 { | 44 { |
46 base::AutoLock lock(abort_association_pending_lock_); | 45 base::AutoLock lock(abort_association_pending_lock_); |
47 abort_association_pending_ = false; | 46 abort_association_pending_ = false; |
48 } | 47 } |
49 | 48 |
50 // We must not be holding a transaction when we interact with the password | 49 // We must not be holding a transaction when we interact with the password |
51 // store, as it can post tasks to the UI thread which can itself be blocked | 50 // store, as it can post tasks to the UI thread which can itself be blocked |
52 // on our transaction, resulting in deadlock. (http://crbug.com/70658) | 51 // on our transaction, resulting in deadlock. (http://crbug.com/70658) |
53 std::vector<webkit_glue::PasswordForm*> passwords; | 52 std::vector<webkit_glue::PasswordForm*> passwords; |
54 if (!password_store_->FillAutofillableLogins(&passwords) || | 53 if (!password_store_->FillAutofillableLogins(&passwords) || |
55 !password_store_->FillBlacklistLogins(&passwords)) { | 54 !password_store_->FillBlacklistLogins(&passwords)) { |
56 STLDeleteElements(&passwords); | 55 STLDeleteElements(&passwords); |
57 error->Reset(FROM_HERE, | 56 LOG(ERROR) << "Could not get the password entries."; |
58 "Could not get the password entries.", | |
59 model_type()); | |
60 return false; | 57 return false; |
61 } | 58 } |
62 | 59 |
63 std::set<std::string> current_passwords; | 60 std::set<std::string> current_passwords; |
64 PasswordVector new_passwords; | 61 PasswordVector new_passwords; |
65 PasswordVector updated_passwords; | 62 PasswordVector updated_passwords; |
66 { | 63 { |
67 sync_api::WriteTransaction trans(FROM_HERE, sync_service_->GetUserShare()); | 64 sync_api::WriteTransaction trans(FROM_HERE, sync_service_->GetUserShare()); |
68 sync_api::ReadNode password_root(&trans); | 65 sync_api::ReadNode password_root(&trans); |
69 if (!password_root.InitByTagLookup(kPasswordTag)) { | 66 if (!password_root.InitByTagLookup(kPasswordTag)) { |
70 error->Reset(FROM_HERE, | 67 LOG(ERROR) << "Server did not create the top-level password node. We " |
71 "Server did not create the top-level password node. We " | 68 << "might be running against an out-of-date server."; |
72 "might be running against an out-of-date server.", | |
73 model_type()); | |
74 return false; | 69 return false; |
75 } | 70 } |
76 | 71 |
77 for (std::vector<webkit_glue::PasswordForm*>::iterator ix = | 72 for (std::vector<webkit_glue::PasswordForm*>::iterator ix = |
78 passwords.begin(); | 73 passwords.begin(); |
79 ix != passwords.end(); ++ix) { | 74 ix != passwords.end(); ++ix) { |
80 if (IsAbortPending()) { | 75 if (IsAbortPending()) |
81 error->Reset(FROM_HERE, "Abort pending", model_type()); | |
82 return false; | 76 return false; |
83 } | |
84 std::string tag = MakeTag(**ix); | 77 std::string tag = MakeTag(**ix); |
85 | 78 |
86 sync_api::ReadNode node(&trans); | 79 sync_api::ReadNode node(&trans); |
87 if (node.InitByClientTagLookup(syncable::PASSWORDS, tag)) { | 80 if (node.InitByClientTagLookup(syncable::PASSWORDS, tag)) { |
88 const sync_pb::PasswordSpecificsData& password = | 81 const sync_pb::PasswordSpecificsData& password = |
89 node.GetPasswordSpecifics(); | 82 node.GetPasswordSpecifics(); |
90 DCHECK_EQ(tag, MakeTag(password)); | 83 DCHECK_EQ(tag, MakeTag(password)); |
91 | 84 |
92 webkit_glue::PasswordForm new_password; | 85 webkit_glue::PasswordForm new_password; |
93 | 86 |
94 if (MergePasswords(password, **ix, &new_password)) { | 87 if (MergePasswords(password, **ix, &new_password)) { |
95 sync_api::WriteNode write_node(&trans); | 88 sync_api::WriteNode write_node(&trans); |
96 if (!write_node.InitByClientTagLookup(syncable::PASSWORDS, tag)) { | 89 if (!write_node.InitByClientTagLookup(syncable::PASSWORDS, tag)) { |
97 STLDeleteElements(&passwords); | 90 STLDeleteElements(&passwords); |
98 error->Reset(FROM_HERE, | 91 LOG(ERROR) << "Failed to edit password sync node."; |
99 "Failed to edit password sync node.", | |
100 model_type()); | |
101 return false; | 92 return false; |
102 } | 93 } |
103 WriteToSyncNode(new_password, &write_node); | 94 WriteToSyncNode(new_password, &write_node); |
104 updated_passwords.push_back(new_password); | 95 updated_passwords.push_back(new_password); |
105 } | 96 } |
106 | 97 |
107 Associate(&tag, node.GetId()); | 98 Associate(&tag, node.GetId()); |
108 } else { | 99 } else { |
109 sync_api::WriteNode node(&trans); | 100 sync_api::WriteNode node(&trans); |
110 if (!node.InitUniqueByCreation(syncable::PASSWORDS, | 101 if (!node.InitUniqueByCreation(syncable::PASSWORDS, |
111 password_root, tag)) { | 102 password_root, tag)) { |
112 STLDeleteElements(&passwords); | 103 STLDeleteElements(&passwords); |
113 error->Reset(FROM_HERE, | 104 LOG(ERROR) << "Failed to create password sync node."; |
114 "Failed to create password sync node.", | |
115 model_type()); | |
116 return false; | 105 return false; |
117 } | 106 } |
118 | 107 |
119 WriteToSyncNode(**ix, &node); | 108 WriteToSyncNode(**ix, &node); |
120 | 109 |
121 Associate(&tag, node.GetId()); | 110 Associate(&tag, node.GetId()); |
122 } | 111 } |
123 | 112 |
124 current_passwords.insert(tag); | 113 current_passwords.insert(tag); |
125 } | 114 } |
126 | 115 |
127 STLDeleteElements(&passwords); | 116 STLDeleteElements(&passwords); |
128 | 117 |
129 int64 sync_child_id = password_root.GetFirstChildId(); | 118 int64 sync_child_id = password_root.GetFirstChildId(); |
130 while (sync_child_id != sync_api::kInvalidId) { | 119 while (sync_child_id != sync_api::kInvalidId) { |
131 sync_api::ReadNode sync_child_node(&trans); | 120 sync_api::ReadNode sync_child_node(&trans); |
132 if (!sync_child_node.InitByIdLookup(sync_child_id)) { | 121 if (!sync_child_node.InitByIdLookup(sync_child_id)) { |
133 error->Reset(FROM_HERE, "Failed to fetch child node.", model_type()); | 122 LOG(ERROR) << "Failed to fetch child node."; |
134 return false; | 123 return false; |
135 } | 124 } |
136 const sync_pb::PasswordSpecificsData& password = | 125 const sync_pb::PasswordSpecificsData& password = |
137 sync_child_node.GetPasswordSpecifics(); | 126 sync_child_node.GetPasswordSpecifics(); |
138 std::string tag = MakeTag(password); | 127 std::string tag = MakeTag(password); |
139 | 128 |
140 // The password only exists on the server. Add it to the local | 129 // The password only exists on the server. Add it to the local |
141 // model. | 130 // model. |
142 if (current_passwords.find(tag) == current_passwords.end()) { | 131 if (current_passwords.find(tag) == current_passwords.end()) { |
143 webkit_glue::PasswordForm new_password; | 132 webkit_glue::PasswordForm new_password; |
144 | 133 |
145 CopyPassword(password, &new_password); | 134 CopyPassword(password, &new_password); |
146 Associate(&tag, sync_child_node.GetId()); | 135 Associate(&tag, sync_child_node.GetId()); |
147 new_passwords.push_back(new_password); | 136 new_passwords.push_back(new_password); |
148 } | 137 } |
149 | 138 |
150 sync_child_id = sync_child_node.GetSuccessorId(); | 139 sync_child_id = sync_child_node.GetSuccessorId(); |
151 } | 140 } |
152 } | 141 } |
153 | 142 |
154 // We must not be holding a transaction when we interact with the password | 143 // We must not be holding a transaction when we interact with the password |
155 // store, as it can post tasks to the UI thread which can itself be blocked | 144 // store, as it can post tasks to the UI thread which can itself be blocked |
156 // on our transaction, resulting in deadlock. (http://crbug.com/70658) | 145 // on our transaction, resulting in deadlock. (http://crbug.com/70658) |
157 if (!WriteToPasswordStore(&new_passwords, &updated_passwords, NULL)) { | 146 if (!WriteToPasswordStore(&new_passwords, &updated_passwords, NULL)) { |
158 error->Reset(FROM_HERE, "Failed to write passwords.", model_type()); | 147 LOG(ERROR) << "Failed to write passwords."; |
159 return false; | 148 return false; |
160 } | 149 } |
161 | 150 |
162 return true; | 151 return true; |
163 } | 152 } |
164 | 153 |
165 bool PasswordModelAssociator::DeleteAllNodes( | 154 bool PasswordModelAssociator::DeleteAllNodes( |
166 sync_api::WriteTransaction* trans) { | 155 sync_api::WriteTransaction* trans) { |
167 DCHECK(expected_loop_ == MessageLoop::current()); | 156 DCHECK(expected_loop_ == MessageLoop::current()); |
168 for (PasswordToSyncIdMap::iterator node_id = id_map_.begin(); | 157 for (PasswordToSyncIdMap::iterator node_id = id_map_.begin(); |
169 node_id != id_map_.end(); ++node_id) { | 158 node_id != id_map_.end(); ++node_id) { |
170 sync_api::WriteNode sync_node(trans); | 159 sync_api::WriteNode sync_node(trans); |
171 if (!sync_node.InitByIdLookup(node_id->second)) { | 160 if (!sync_node.InitByIdLookup(node_id->second)) { |
172 LOG(ERROR) << "Typed url node lookup failed."; | 161 LOG(ERROR) << "Typed url node lookup failed."; |
173 return false; | 162 return false; |
174 } | 163 } |
175 sync_node.Remove(); | 164 sync_node.Remove(); |
176 } | 165 } |
177 | 166 |
178 id_map_.clear(); | 167 id_map_.clear(); |
179 id_map_inverse_.clear(); | 168 id_map_inverse_.clear(); |
180 return true; | 169 return true; |
181 } | 170 } |
182 | 171 |
183 bool PasswordModelAssociator::DisassociateModels(SyncError* error) { | 172 bool PasswordModelAssociator::DisassociateModels() { |
184 id_map_.clear(); | 173 id_map_.clear(); |
185 id_map_inverse_.clear(); | 174 id_map_inverse_.clear(); |
186 return true; | 175 return true; |
187 } | 176 } |
188 | 177 |
189 bool PasswordModelAssociator::SyncModelHasUserCreatedNodes(bool* has_nodes) { | 178 bool PasswordModelAssociator::SyncModelHasUserCreatedNodes(bool* has_nodes) { |
190 DCHECK(has_nodes); | 179 DCHECK(has_nodes); |
191 *has_nodes = false; | 180 *has_nodes = false; |
192 int64 password_sync_id; | 181 int64 password_sync_id; |
193 if (!GetSyncIdForTaggedNode(kPasswordTag, &password_sync_id)) { | 182 if (!GetSyncIdForTaggedNode(kPasswordTag, &password_sync_id)) { |
(...skipping 224 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
418 const std::string& password_element, | 407 const std::string& password_element, |
419 const std::string& signon_realm) { | 408 const std::string& signon_realm) { |
420 return EscapePath(origin_url) + "|" + | 409 return EscapePath(origin_url) + "|" + |
421 EscapePath(username_element) + "|" + | 410 EscapePath(username_element) + "|" + |
422 EscapePath(username_value) + "|" + | 411 EscapePath(username_value) + "|" + |
423 EscapePath(password_element) + "|" + | 412 EscapePath(password_element) + "|" + |
424 EscapePath(signon_realm); | 413 EscapePath(signon_realm); |
425 } | 414 } |
426 | 415 |
427 } // namespace browser_sync | 416 } // namespace browser_sync |
OLD | NEW |