Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(233)

Side by Side Diff: chrome/browser/sync/engine/syncapi.cc

Issue 6465005: [Sync] Initial support for encrypting any datatype (no UI hookup yet). (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Comments. Rest of unit tests. Created 9 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2010 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/engine/syncapi.h" 5 #include "chrome/browser/sync/engine/syncapi.h"
6 6
7 #include <algorithm>
7 #include <bitset> 8 #include <bitset>
8 #include <iomanip> 9 #include <iomanip>
9 #include <list> 10 #include <list>
11 #include <queue>
10 #include <string> 12 #include <string>
11 #include <vector> 13 #include <vector>
12 14
13 #include "base/base64.h" 15 #include "base/base64.h"
14 #include "base/logging.h" 16 #include "base/logging.h"
15 #include "base/message_loop.h" 17 #include "base/message_loop.h"
16 #include "base/scoped_ptr.h" 18 #include "base/scoped_ptr.h"
17 #include "base/sha1.h" 19 #include "base/sha1.h"
18 #include "base/string_number_conversions.h" 20 #include "base/string_number_conversions.h"
19 #include "base/string_util.h" 21 #include "base/string_util.h"
(...skipping 24 matching lines...) Expand all
44 #include "chrome/browser/sync/protocol/proto_value_conversions.h" 46 #include "chrome/browser/sync/protocol/proto_value_conversions.h"
45 #include "chrome/browser/sync/protocol/service_constants.h" 47 #include "chrome/browser/sync/protocol/service_constants.h"
46 #include "chrome/browser/sync/protocol/session_specifics.pb.h" 48 #include "chrome/browser/sync/protocol/session_specifics.pb.h"
47 #include "chrome/browser/sync/protocol/sync.pb.h" 49 #include "chrome/browser/sync/protocol/sync.pb.h"
48 #include "chrome/browser/sync/protocol/theme_specifics.pb.h" 50 #include "chrome/browser/sync/protocol/theme_specifics.pb.h"
49 #include "chrome/browser/sync/protocol/typed_url_specifics.pb.h" 51 #include "chrome/browser/sync/protocol/typed_url_specifics.pb.h"
50 #include "chrome/browser/sync/sessions/sync_session.h" 52 #include "chrome/browser/sync/sessions/sync_session.h"
51 #include "chrome/browser/sync/sessions/sync_session_context.h" 53 #include "chrome/browser/sync/sessions/sync_session_context.h"
52 #include "chrome/browser/sync/syncable/autofill_migration.h" 54 #include "chrome/browser/sync/syncable/autofill_migration.h"
53 #include "chrome/browser/sync/syncable/directory_manager.h" 55 #include "chrome/browser/sync/syncable/directory_manager.h"
56 #include "chrome/browser/sync/syncable/nigori_util.h"
54 #include "chrome/browser/sync/syncable/syncable.h" 57 #include "chrome/browser/sync/syncable/syncable.h"
55 #include "chrome/browser/sync/util/crypto_helpers.h" 58 #include "chrome/browser/sync/util/crypto_helpers.h"
56 #include "chrome/common/deprecated/event_sys.h" 59 #include "chrome/common/deprecated/event_sys.h"
57 #include "chrome/common/net/gaia/gaia_authenticator.h" 60 #include "chrome/common/net/gaia/gaia_authenticator.h"
58 #include "jingle/notifier/listener/mediator_thread_impl.h" 61 #include "jingle/notifier/listener/mediator_thread_impl.h"
59 #include "jingle/notifier/listener/notification_constants.h" 62 #include "jingle/notifier/listener/notification_constants.h"
60 #include "jingle/notifier/listener/talk_mediator.h" 63 #include "jingle/notifier/listener/talk_mediator.h"
61 #include "jingle/notifier/listener/talk_mediator_impl.h" 64 #include "jingle/notifier/listener/talk_mediator_impl.h"
62 #include "net/base/network_change_notifier.h" 65 #include "net/base/network_change_notifier.h"
63 66
(...skipping 134 matching lines...) Expand 10 before | Expand all | Expand 10 after
198 if (!crypto->Decrypt(encrypted, data.get())) 201 if (!crypto->Decrypt(encrypted, data.get()))
199 return NULL; 202 return NULL;
200 return data.release(); 203 return data.release();
201 } 204 }
202 205
203 bool BaseNode::DecryptIfNecessary(Entry* entry) { 206 bool BaseNode::DecryptIfNecessary(Entry* entry) {
204 if (GetIsFolder()) return true; // Ignore the top-level password folder. 207 if (GetIsFolder()) return true; // Ignore the top-level password folder.
205 const sync_pb::EntitySpecifics& specifics = 208 const sync_pb::EntitySpecifics& specifics =
206 entry->Get(syncable::SPECIFICS); 209 entry->Get(syncable::SPECIFICS);
207 if (specifics.HasExtension(sync_pb::password)) { 210 if (specifics.HasExtension(sync_pb::password)) {
211 // Passwords have their own legacy encryption structure.
208 scoped_ptr<sync_pb::PasswordSpecificsData> data(DecryptPasswordSpecifics( 212 scoped_ptr<sync_pb::PasswordSpecificsData> data(DecryptPasswordSpecifics(
209 specifics, GetTransaction()->GetCryptographer())); 213 specifics, GetTransaction()->GetCryptographer()));
210 if (!data.get()) 214 if (!data.get())
211 return false; 215 return false;
212 password_data_.swap(data); 216 password_data_.swap(data);
217 return true;
218 }
219
220 // We assume any node with the encrypted field set has encrypted data.
221 if (!specifics.has_encrypted())
222 return true;
223
224 const sync_pb::EncryptedData& encrypted =
225 specifics.encrypted();
226 std::string plaintext_data = GetTransaction()->GetCryptographer()->
227 DecryptToString(encrypted);
228 if (plaintext_data.length() == 0)
229 return false;
230 if (!unencrypted_data_.ParseFromString(plaintext_data)) {
231 LOG(ERROR) << "Failed to decrypt encrypted node of type " <<
232 syncable::ModelTypeToString(entry->GetModelType()) << ".";
233 return false;
213 } 234 }
214 return true; 235 return true;
215 } 236 }
216 237
238 const sync_pb::EntitySpecifics& BaseNode::GetUnencryptedSpecifics(
239 const syncable::Entry* entry) const {
240 const sync_pb::EntitySpecifics& specifics = entry->Get(SPECIFICS);
241 if (specifics.has_encrypted()) {
242 DCHECK(syncable::GetModelTypeFromSpecifics(unencrypted_data_) !=
243 syncable::UNSPECIFIED);
244 return unencrypted_data_;
245 } else {
246 DCHECK(syncable::GetModelTypeFromSpecifics(unencrypted_data_) ==
247 syncable::UNSPECIFIED);
248 return specifics;
249 }
250 }
251
217 int64 BaseNode::GetParentId() const { 252 int64 BaseNode::GetParentId() const {
218 return IdToMetahandle(GetTransaction()->GetWrappedTrans(), 253 return IdToMetahandle(GetTransaction()->GetWrappedTrans(),
219 GetEntry()->Get(syncable::PARENT_ID)); 254 GetEntry()->Get(syncable::PARENT_ID));
220 } 255 }
221 256
222 int64 BaseNode::GetId() const { 257 int64 BaseNode::GetId() const {
223 return GetEntry()->Get(syncable::META_HANDLE); 258 return GetEntry()->Get(syncable::META_HANDLE);
224 } 259 }
225 260
226 int64 BaseNode::GetModificationTime() const { 261 int64 BaseNode::GetModificationTime() const {
(...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after
308 output->assign(reinterpret_cast<const unsigned char*>(favicon.data()), 343 output->assign(reinterpret_cast<const unsigned char*>(favicon.data()),
309 reinterpret_cast<const unsigned char*>(favicon.data() + 344 reinterpret_cast<const unsigned char*>(favicon.data() +
310 favicon.length())); 345 favicon.length()));
311 } 346 }
312 347
313 int64 BaseNode::GetExternalId() const { 348 int64 BaseNode::GetExternalId() const {
314 return GetEntry()->Get(syncable::LOCAL_EXTERNAL_ID); 349 return GetEntry()->Get(syncable::LOCAL_EXTERNAL_ID);
315 } 350 }
316 351
317 const sync_pb::AppSpecifics& BaseNode::GetAppSpecifics() const { 352 const sync_pb::AppSpecifics& BaseNode::GetAppSpecifics() const {
318 DCHECK(GetModelType() == syncable::APPS); 353 DCHECK_EQ(syncable::APPS, GetModelType());
319 return GetEntry()->Get(SPECIFICS).GetExtension(sync_pb::app); 354 const sync_pb::EntitySpecifics& unencrypted =
355 GetUnencryptedSpecifics(GetEntry());
356 return unencrypted.GetExtension(sync_pb::app);
320 } 357 }
321 358
322 const sync_pb::AutofillSpecifics& BaseNode::GetAutofillSpecifics() const { 359 const sync_pb::AutofillSpecifics& BaseNode::GetAutofillSpecifics() const {
323 DCHECK(GetModelType() == syncable::AUTOFILL); 360 DCHECK_EQ(syncable::AUTOFILL, GetModelType());
324 return GetEntry()->Get(SPECIFICS).GetExtension(sync_pb::autofill); 361 const sync_pb::EntitySpecifics& unencrypted =
362 GetUnencryptedSpecifics(GetEntry());
363 return unencrypted.GetExtension(sync_pb::autofill);
325 } 364 }
326 365
327 const AutofillProfileSpecifics& BaseNode::GetAutofillProfileSpecifics() const { 366 const AutofillProfileSpecifics& BaseNode::GetAutofillProfileSpecifics() const {
328 DCHECK_EQ(GetModelType(), syncable::AUTOFILL_PROFILE); 367 DCHECK_EQ(GetModelType(), syncable::AUTOFILL_PROFILE);
329 return GetEntry()->Get(SPECIFICS).GetExtension(sync_pb::autofill_profile); 368 const sync_pb::EntitySpecifics& unencrypted =
369 GetUnencryptedSpecifics(GetEntry());
370 return unencrypted.GetExtension(sync_pb::autofill_profile);
330 } 371 }
331 372
332 const sync_pb::BookmarkSpecifics& BaseNode::GetBookmarkSpecifics() const { 373 const sync_pb::BookmarkSpecifics& BaseNode::GetBookmarkSpecifics() const {
333 DCHECK(GetModelType() == syncable::BOOKMARKS); 374 DCHECK_EQ(syncable::BOOKMARKS, GetModelType());
334 return GetEntry()->Get(SPECIFICS).GetExtension(sync_pb::bookmark); 375 const sync_pb::EntitySpecifics& unencrypted =
376 GetUnencryptedSpecifics(GetEntry());
377 return unencrypted.GetExtension(sync_pb::bookmark);
335 } 378 }
336 379
337 const sync_pb::NigoriSpecifics& BaseNode::GetNigoriSpecifics() const { 380 const sync_pb::NigoriSpecifics& BaseNode::GetNigoriSpecifics() const {
338 DCHECK(GetModelType() == syncable::NIGORI); 381 DCHECK_EQ(syncable::NIGORI, GetModelType());
339 return GetEntry()->Get(SPECIFICS).GetExtension(sync_pb::nigori); 382 const sync_pb::EntitySpecifics& unencrypted =
383 GetUnencryptedSpecifics(GetEntry());
384 return unencrypted.GetExtension(sync_pb::nigori);
340 } 385 }
341 386
342 const sync_pb::PasswordSpecificsData& BaseNode::GetPasswordSpecifics() const { 387 const sync_pb::PasswordSpecificsData& BaseNode::GetPasswordSpecifics() const {
343 DCHECK(GetModelType() == syncable::PASSWORDS); 388 DCHECK_EQ(syncable::PASSWORDS, GetModelType());
344 DCHECK(password_data_.get()); 389 DCHECK(password_data_.get());
345 return *password_data_; 390 return *password_data_;
346 } 391 }
347 392
348 const sync_pb::PreferenceSpecifics& BaseNode::GetPreferenceSpecifics() const { 393 const sync_pb::PreferenceSpecifics& BaseNode::GetPreferenceSpecifics() const {
349 DCHECK(GetModelType() == syncable::PREFERENCES); 394 DCHECK_EQ(syncable::PREFERENCES, GetModelType());
350 return GetEntry()->Get(SPECIFICS).GetExtension(sync_pb::preference); 395 const sync_pb::EntitySpecifics& unencrypted =
396 GetUnencryptedSpecifics(GetEntry());
397 return unencrypted.GetExtension(sync_pb::preference);
351 } 398 }
352 399
353 const sync_pb::ThemeSpecifics& BaseNode::GetThemeSpecifics() const { 400 const sync_pb::ThemeSpecifics& BaseNode::GetThemeSpecifics() const {
354 DCHECK(GetModelType() == syncable::THEMES); 401 DCHECK_EQ(syncable::THEMES, GetModelType());
355 return GetEntry()->Get(SPECIFICS).GetExtension(sync_pb::theme); 402 const sync_pb::EntitySpecifics& unencrypted =
403 GetUnencryptedSpecifics(GetEntry());
404 return unencrypted.GetExtension(sync_pb::theme);
356 } 405 }
357 406
358 const sync_pb::TypedUrlSpecifics& BaseNode::GetTypedUrlSpecifics() const { 407 const sync_pb::TypedUrlSpecifics& BaseNode::GetTypedUrlSpecifics() const {
359 DCHECK(GetModelType() == syncable::TYPED_URLS); 408 DCHECK_EQ(syncable::TYPED_URLS, GetModelType());
360 return GetEntry()->Get(SPECIFICS).GetExtension(sync_pb::typed_url); 409 const sync_pb::EntitySpecifics& unencrypted =
410 GetUnencryptedSpecifics(GetEntry());
411 return unencrypted.GetExtension(sync_pb::typed_url);
361 } 412 }
362 413
363 const sync_pb::ExtensionSpecifics& BaseNode::GetExtensionSpecifics() const { 414 const sync_pb::ExtensionSpecifics& BaseNode::GetExtensionSpecifics() const {
364 DCHECK(GetModelType() == syncable::EXTENSIONS); 415 DCHECK_EQ(syncable::EXTENSIONS, GetModelType());
365 return GetEntry()->Get(SPECIFICS).GetExtension(sync_pb::extension); 416 const sync_pb::EntitySpecifics& unencrypted =
417 GetUnencryptedSpecifics(GetEntry());
418 return unencrypted.GetExtension(sync_pb::extension);
366 } 419 }
367 420
368 const sync_pb::SessionSpecifics& BaseNode::GetSessionSpecifics() const { 421 const sync_pb::SessionSpecifics& BaseNode::GetSessionSpecifics() const {
369 DCHECK(GetModelType() == syncable::SESSIONS); 422 DCHECK_EQ(syncable::SESSIONS, GetModelType());
370 return GetEntry()->Get(SPECIFICS).GetExtension(sync_pb::session); 423 const sync_pb::EntitySpecifics& unencrypted =
424 GetUnencryptedSpecifics(GetEntry());
425 return unencrypted.GetExtension(sync_pb::session);
371 } 426 }
372 427
373 syncable::ModelType BaseNode::GetModelType() const { 428 syncable::ModelType BaseNode::GetModelType() const {
374 return GetEntry()->GetModelType(); 429 return GetEntry()->GetModelType();
375 } 430 }
376 431
377 //////////////////////////////////// 432 ////////////////////////////////////
378 // WriteNode member definitions 433 // WriteNode member definitions
434 void WriteNode::EncryptIfNecessary(sync_pb::EntitySpecifics* unencrypted) {
435 syncable::ModelType type = syncable::GetModelTypeFromSpecifics(*unencrypted);
436 DCHECK_NE(type, syncable::UNSPECIFIED);
437 DCHECK_NE(type, syncable::PASSWORDS); // Passwords use their own encryption.
438 DCHECK_NE(type, syncable::NIGORI); // Nigori is encrypted separately.
439
440 syncable::ModelTypeSet encrypted_types =
441 GetEncryptedDataTypesFromSyncer(GetTransaction()->GetWrappedTrans());
442 if (encrypted_types.count(type) == 0) {
443 // This datatype does not require encryption.
444 return;
445 }
446
447 if (unencrypted->has_encrypted()) {
448 // This specifics is already encrypted, our work is done.
449 LOG(WARNING) << "Attempted to encrypt an already encrypted entity"
450 << " specifics of type " << syncable::ModelTypeToString(type)
451 << ". Dropping.";
452 return;
453 }
454 sync_pb::EntitySpecifics encrypted;
455 syncable::AddDefaultExtensionValue(type, &encrypted);
456 VLOG(2) << "Encrypted specifics of type " << syncable::ModelTypeToString(type)
457 << " with content: " << unencrypted->SerializeAsString() << "\n";
458 if (!GetTransaction()->GetCryptographer()->Encrypt(
459 *unencrypted,
460 encrypted.mutable_encrypted())) {
461 LOG(ERROR) << "Could not encrypt data for node of type " <<
462 syncable::ModelTypeToString(type);
463 NOTREACHED();
464 }
465 unencrypted->CopyFrom(encrypted);
466 }
467
379 void WriteNode::SetIsFolder(bool folder) { 468 void WriteNode::SetIsFolder(bool folder) {
380 if (entry_->Get(syncable::IS_DIR) == folder) 469 if (entry_->Get(syncable::IS_DIR) == folder)
381 return; // Skip redundant changes. 470 return; // Skip redundant changes.
382 471
383 entry_->Put(syncable::IS_DIR, folder); 472 entry_->Put(syncable::IS_DIR, folder);
384 MarkForSyncing(); 473 MarkForSyncing();
385 } 474 }
386 475
387 void WriteNode::SetTitle(const std::wstring& title) { 476 void WriteNode::SetTitle(const std::wstring& title) {
388 std::string server_legal_name; 477 std::string server_legal_name;
389 SyncAPINameToServerName(title, &server_legal_name); 478 SyncAPINameToServerName(title, &server_legal_name);
390 479
391 string old_name = entry_->Get(syncable::NON_UNIQUE_NAME); 480 string old_name = entry_->Get(syncable::NON_UNIQUE_NAME);
392 481
393 if (server_legal_name == old_name) 482 if (server_legal_name == old_name)
394 return; // Skip redundant changes. 483 return; // Skip redundant changes.
395 484
396 entry_->Put(syncable::NON_UNIQUE_NAME, server_legal_name); 485 entry_->Put(syncable::NON_UNIQUE_NAME, server_legal_name);
397 MarkForSyncing(); 486 MarkForSyncing();
398 } 487 }
399 488
400 void WriteNode::SetURL(const GURL& url) { 489 void WriteNode::SetURL(const GURL& url) {
401 sync_pb::BookmarkSpecifics new_value = GetBookmarkSpecifics(); 490 sync_pb::BookmarkSpecifics new_value = GetBookmarkSpecifics();
402 new_value.set_url(url.spec()); 491 new_value.set_url(url.spec());
403 SetBookmarkSpecifics(new_value); 492 SetBookmarkSpecifics(new_value);
404 } 493 }
405 494
406 void WriteNode::SetAppSpecifics( 495 void WriteNode::SetAppSpecifics(
407 const sync_pb::AppSpecifics& new_value) { 496 const sync_pb::AppSpecifics& new_value) {
408 DCHECK(GetModelType() == syncable::APPS); 497 DCHECK_EQ(syncable::APPS, GetModelType());
409 PutAppSpecificsAndMarkForSyncing(new_value); 498 PutAppSpecificsAndMarkForSyncing(new_value);
410 } 499 }
411 500
412 void WriteNode::SetAutofillSpecifics( 501 void WriteNode::SetAutofillSpecifics(
413 const sync_pb::AutofillSpecifics& new_value) { 502 const sync_pb::AutofillSpecifics& new_value) {
414 DCHECK(GetModelType() == syncable::AUTOFILL); 503 DCHECK_EQ(syncable::AUTOFILL, GetModelType());
415 PutAutofillSpecificsAndMarkForSyncing(new_value); 504 PutAutofillSpecificsAndMarkForSyncing(new_value);
416 } 505 }
417 506
418 void WriteNode::PutAutofillSpecificsAndMarkForSyncing( 507 void WriteNode::PutAutofillSpecificsAndMarkForSyncing(
419 const sync_pb::AutofillSpecifics& new_value) { 508 const sync_pb::AutofillSpecifics& new_value) {
420 sync_pb::EntitySpecifics entity_specifics; 509 sync_pb::EntitySpecifics entity_specifics;
421 entity_specifics.MutableExtension(sync_pb::autofill)->CopyFrom(new_value); 510 entity_specifics.MutableExtension(sync_pb::autofill)->CopyFrom(new_value);
511 EncryptIfNecessary(&entity_specifics);
422 PutSpecificsAndMarkForSyncing(entity_specifics); 512 PutSpecificsAndMarkForSyncing(entity_specifics);
423 } 513 }
424 514
425 void WriteNode::SetAutofillProfileSpecifics( 515 void WriteNode::SetAutofillProfileSpecifics(
426 const sync_pb::AutofillProfileSpecifics& new_value) { 516 const sync_pb::AutofillProfileSpecifics& new_value) {
427 DCHECK_EQ(GetModelType(), syncable::AUTOFILL_PROFILE); 517 DCHECK_EQ(GetModelType(), syncable::AUTOFILL_PROFILE);
428 PutAutofillProfileSpecificsAndMarkForSyncing(new_value); 518 PutAutofillProfileSpecificsAndMarkForSyncing(new_value);
429 } 519 }
430 520
431 void WriteNode::PutAutofillProfileSpecificsAndMarkForSyncing( 521 void WriteNode::PutAutofillProfileSpecificsAndMarkForSyncing(
432 const sync_pb::AutofillProfileSpecifics& new_value) { 522 const sync_pb::AutofillProfileSpecifics& new_value) {
433 sync_pb::EntitySpecifics entity_specifics; 523 sync_pb::EntitySpecifics entity_specifics;
434 entity_specifics.MutableExtension(sync_pb::autofill_profile)->CopyFrom( 524 entity_specifics.MutableExtension(sync_pb::autofill_profile)->CopyFrom(
435 new_value); 525 new_value);
526 EncryptIfNecessary(&entity_specifics);
436 PutSpecificsAndMarkForSyncing(entity_specifics); 527 PutSpecificsAndMarkForSyncing(entity_specifics);
437 } 528 }
438 529
439 void WriteNode::SetBookmarkSpecifics( 530 void WriteNode::SetBookmarkSpecifics(
440 const sync_pb::BookmarkSpecifics& new_value) { 531 const sync_pb::BookmarkSpecifics& new_value) {
441 DCHECK(GetModelType() == syncable::BOOKMARKS); 532 DCHECK_EQ(syncable::BOOKMARKS, GetModelType());
442 PutBookmarkSpecificsAndMarkForSyncing(new_value); 533 PutBookmarkSpecificsAndMarkForSyncing(new_value);
443 } 534 }
444 535
445 void WriteNode::PutBookmarkSpecificsAndMarkForSyncing( 536 void WriteNode::PutBookmarkSpecificsAndMarkForSyncing(
446 const sync_pb::BookmarkSpecifics& new_value) { 537 const sync_pb::BookmarkSpecifics& new_value) {
447 sync_pb::EntitySpecifics entity_specifics; 538 sync_pb::EntitySpecifics entity_specifics;
448 entity_specifics.MutableExtension(sync_pb::bookmark)->CopyFrom(new_value); 539 entity_specifics.MutableExtension(sync_pb::bookmark)->CopyFrom(new_value);
540 EncryptIfNecessary(&entity_specifics);
449 PutSpecificsAndMarkForSyncing(entity_specifics); 541 PutSpecificsAndMarkForSyncing(entity_specifics);
450 } 542 }
451 543
452 void WriteNode::SetNigoriSpecifics( 544 void WriteNode::SetNigoriSpecifics(
453 const sync_pb::NigoriSpecifics& new_value) { 545 const sync_pb::NigoriSpecifics& new_value) {
454 DCHECK(GetModelType() == syncable::NIGORI); 546 DCHECK_EQ(syncable::NIGORI, GetModelType());
455 PutNigoriSpecificsAndMarkForSyncing(new_value); 547 PutNigoriSpecificsAndMarkForSyncing(new_value);
456 } 548 }
457 549
458 void WriteNode::PutNigoriSpecificsAndMarkForSyncing( 550 void WriteNode::PutNigoriSpecificsAndMarkForSyncing(
459 const sync_pb::NigoriSpecifics& new_value) { 551 const sync_pb::NigoriSpecifics& new_value) {
460 sync_pb::EntitySpecifics entity_specifics; 552 sync_pb::EntitySpecifics entity_specifics;
461 entity_specifics.MutableExtension(sync_pb::nigori)->CopyFrom(new_value); 553 entity_specifics.MutableExtension(sync_pb::nigori)->CopyFrom(new_value);
462 PutSpecificsAndMarkForSyncing(entity_specifics); 554 PutSpecificsAndMarkForSyncing(entity_specifics);
463 } 555 }
464 556
465 void WriteNode::SetPasswordSpecifics( 557 void WriteNode::SetPasswordSpecifics(
466 const sync_pb::PasswordSpecificsData& data) { 558 const sync_pb::PasswordSpecificsData& data) {
467 DCHECK(GetModelType() == syncable::PASSWORDS); 559 DCHECK_EQ(syncable::PASSWORDS, GetModelType());
468
469 sync_pb::PasswordSpecifics new_value; 560 sync_pb::PasswordSpecifics new_value;
470 if (!GetTransaction()->GetCryptographer()->Encrypt( 561 if (!GetTransaction()->GetCryptographer()->Encrypt(
471 data, 562 data,
472 new_value.mutable_encrypted())) { 563 new_value.mutable_encrypted())) {
473 NOTREACHED(); 564 NOTREACHED();
474 } 565 }
475
476 PutPasswordSpecificsAndMarkForSyncing(new_value); 566 PutPasswordSpecificsAndMarkForSyncing(new_value);
477 } 567 }
478 568
479 void WriteNode::SetPreferenceSpecifics( 569 void WriteNode::SetPreferenceSpecifics(
480 const sync_pb::PreferenceSpecifics& new_value) { 570 const sync_pb::PreferenceSpecifics& new_value) {
481 DCHECK(GetModelType() == syncable::PREFERENCES); 571 DCHECK_EQ(syncable::PREFERENCES, GetModelType());
482 PutPreferenceSpecificsAndMarkForSyncing(new_value); 572 PutPreferenceSpecificsAndMarkForSyncing(new_value);
483 } 573 }
484 574
485 void WriteNode::SetThemeSpecifics( 575 void WriteNode::SetThemeSpecifics(
486 const sync_pb::ThemeSpecifics& new_value) { 576 const sync_pb::ThemeSpecifics& new_value) {
487 DCHECK(GetModelType() == syncable::THEMES); 577 DCHECK_EQ(syncable::THEMES, GetModelType());
488 PutThemeSpecificsAndMarkForSyncing(new_value); 578 PutThemeSpecificsAndMarkForSyncing(new_value);
489 } 579 }
490 580
491 void WriteNode::SetSessionSpecifics( 581 void WriteNode::SetSessionSpecifics(
492 const sync_pb::SessionSpecifics& new_value) { 582 const sync_pb::SessionSpecifics& new_value) {
493 DCHECK(GetModelType() == syncable::SESSIONS); 583 DCHECK_EQ(syncable::SESSIONS, GetModelType());
494 PutSessionSpecificsAndMarkForSyncing(new_value); 584 PutSessionSpecificsAndMarkForSyncing(new_value);
495 } 585 }
496 586
587 void WriteNode::ResetFromSpecifics() {
588 sync_pb::EntitySpecifics new_data;
589 new_data.CopyFrom(GetUnencryptedSpecifics(GetEntry()));
590 EncryptIfNecessary(&new_data);
591 PutSpecificsAndMarkForSyncing(new_data);
592 }
497 593
498 void WriteNode::PutPasswordSpecificsAndMarkForSyncing( 594 void WriteNode::PutPasswordSpecificsAndMarkForSyncing(
499 const sync_pb::PasswordSpecifics& new_value) { 595 const sync_pb::PasswordSpecifics& new_value) {
500 sync_pb::EntitySpecifics entity_specifics; 596 sync_pb::EntitySpecifics entity_specifics;
501 entity_specifics.MutableExtension(sync_pb::password)->CopyFrom(new_value); 597 entity_specifics.MutableExtension(sync_pb::password)->CopyFrom(new_value);
502 PutSpecificsAndMarkForSyncing(entity_specifics); 598 PutSpecificsAndMarkForSyncing(entity_specifics);
503 } 599 }
504 600
505 void WriteNode::PutPreferenceSpecificsAndMarkForSyncing( 601 void WriteNode::PutPreferenceSpecificsAndMarkForSyncing(
506 const sync_pb::PreferenceSpecifics& new_value) { 602 const sync_pb::PreferenceSpecifics& new_value) {
507 sync_pb::EntitySpecifics entity_specifics; 603 sync_pb::EntitySpecifics entity_specifics;
508 entity_specifics.MutableExtension(sync_pb::preference)->CopyFrom(new_value); 604 entity_specifics.MutableExtension(sync_pb::preference)->CopyFrom(new_value);
605 EncryptIfNecessary(&entity_specifics);
509 PutSpecificsAndMarkForSyncing(entity_specifics); 606 PutSpecificsAndMarkForSyncing(entity_specifics);
510 } 607 }
511 608
512 void WriteNode::SetTypedUrlSpecifics( 609 void WriteNode::SetTypedUrlSpecifics(
513 const sync_pb::TypedUrlSpecifics& new_value) { 610 const sync_pb::TypedUrlSpecifics& new_value) {
514 DCHECK(GetModelType() == syncable::TYPED_URLS); 611 DCHECK_EQ(syncable::TYPED_URLS, GetModelType());
515 PutTypedUrlSpecificsAndMarkForSyncing(new_value); 612 PutTypedUrlSpecificsAndMarkForSyncing(new_value);
516 } 613 }
517 614
518 void WriteNode::SetExtensionSpecifics( 615 void WriteNode::SetExtensionSpecifics(
519 const sync_pb::ExtensionSpecifics& new_value) { 616 const sync_pb::ExtensionSpecifics& new_value) {
520 DCHECK(GetModelType() == syncable::EXTENSIONS); 617 DCHECK_EQ(syncable::EXTENSIONS, GetModelType());
521 PutExtensionSpecificsAndMarkForSyncing(new_value); 618 PutExtensionSpecificsAndMarkForSyncing(new_value);
522 } 619 }
523 620
524 void WriteNode::PutAppSpecificsAndMarkForSyncing( 621 void WriteNode::PutAppSpecificsAndMarkForSyncing(
525 const sync_pb::AppSpecifics& new_value) { 622 const sync_pb::AppSpecifics& new_value) {
526 sync_pb::EntitySpecifics entity_specifics; 623 sync_pb::EntitySpecifics entity_specifics;
527 entity_specifics.MutableExtension(sync_pb::app)->CopyFrom(new_value); 624 entity_specifics.MutableExtension(sync_pb::app)->CopyFrom(new_value);
625 EncryptIfNecessary(&entity_specifics);
528 PutSpecificsAndMarkForSyncing(entity_specifics); 626 PutSpecificsAndMarkForSyncing(entity_specifics);
529 } 627 }
530 628
531 void WriteNode::PutThemeSpecificsAndMarkForSyncing( 629 void WriteNode::PutThemeSpecificsAndMarkForSyncing(
532 const sync_pb::ThemeSpecifics& new_value) { 630 const sync_pb::ThemeSpecifics& new_value) {
533 sync_pb::EntitySpecifics entity_specifics; 631 sync_pb::EntitySpecifics entity_specifics;
534 entity_specifics.MutableExtension(sync_pb::theme)->CopyFrom(new_value); 632 entity_specifics.MutableExtension(sync_pb::theme)->CopyFrom(new_value);
633 EncryptIfNecessary(&entity_specifics);
535 PutSpecificsAndMarkForSyncing(entity_specifics); 634 PutSpecificsAndMarkForSyncing(entity_specifics);
536 } 635 }
537 636
538 void WriteNode::PutTypedUrlSpecificsAndMarkForSyncing( 637 void WriteNode::PutTypedUrlSpecificsAndMarkForSyncing(
539 const sync_pb::TypedUrlSpecifics& new_value) { 638 const sync_pb::TypedUrlSpecifics& new_value) {
540 sync_pb::EntitySpecifics entity_specifics; 639 sync_pb::EntitySpecifics entity_specifics;
541 entity_specifics.MutableExtension(sync_pb::typed_url)->CopyFrom(new_value); 640 entity_specifics.MutableExtension(sync_pb::typed_url)->CopyFrom(new_value);
641 EncryptIfNecessary(&entity_specifics);
542 PutSpecificsAndMarkForSyncing(entity_specifics); 642 PutSpecificsAndMarkForSyncing(entity_specifics);
543 } 643 }
544 644
545 void WriteNode::PutExtensionSpecificsAndMarkForSyncing( 645 void WriteNode::PutExtensionSpecificsAndMarkForSyncing(
546 const sync_pb::ExtensionSpecifics& new_value) { 646 const sync_pb::ExtensionSpecifics& new_value) {
547 sync_pb::EntitySpecifics entity_specifics; 647 sync_pb::EntitySpecifics entity_specifics;
548 entity_specifics.MutableExtension(sync_pb::extension)->CopyFrom(new_value); 648 entity_specifics.MutableExtension(sync_pb::extension)->CopyFrom(new_value);
649 EncryptIfNecessary(&entity_specifics);
549 PutSpecificsAndMarkForSyncing(entity_specifics); 650 PutSpecificsAndMarkForSyncing(entity_specifics);
550 } 651 }
551 652
552
553 void WriteNode::PutSessionSpecificsAndMarkForSyncing( 653 void WriteNode::PutSessionSpecificsAndMarkForSyncing(
554 const sync_pb::SessionSpecifics& new_value) { 654 const sync_pb::SessionSpecifics& new_value) {
555 sync_pb::EntitySpecifics entity_specifics; 655 sync_pb::EntitySpecifics entity_specifics;
556 entity_specifics.MutableExtension(sync_pb::session)->CopyFrom(new_value); 656 entity_specifics.MutableExtension(sync_pb::session)->CopyFrom(new_value);
657 EncryptIfNecessary(&entity_specifics);
557 PutSpecificsAndMarkForSyncing(entity_specifics); 658 PutSpecificsAndMarkForSyncing(entity_specifics);
558 } 659 }
559 660
560
561 void WriteNode::PutSpecificsAndMarkForSyncing( 661 void WriteNode::PutSpecificsAndMarkForSyncing(
562 const sync_pb::EntitySpecifics& specifics) { 662 const sync_pb::EntitySpecifics& specifics) {
563 // Skip redundant changes. 663 // Skip redundant changes.
564 if (specifics.SerializeAsString() == 664 if (specifics.SerializeAsString() ==
565 entry_->Get(SPECIFICS).SerializeAsString()) { 665 entry_->Get(SPECIFICS).SerializeAsString()) {
566 return; 666 return;
567 } 667 }
568 entry_->Put(SPECIFICS, specifics); 668 entry_->Put(SPECIFICS, specifics);
569 MarkForSyncing(); 669 MarkForSyncing();
570 } 670 }
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after
615 DCHECK(!entry_) << "Init called twice"; 715 DCHECK(!entry_) << "Init called twice";
616 if (tag.empty()) 716 if (tag.empty())
617 return false; 717 return false;
618 entry_ = new syncable::MutableEntry(transaction_->GetWrappedWriteTrans(), 718 entry_ = new syncable::MutableEntry(transaction_->GetWrappedWriteTrans(),
619 syncable::GET_BY_SERVER_TAG, tag); 719 syncable::GET_BY_SERVER_TAG, tag);
620 if (!entry_->good()) 720 if (!entry_->good())
621 return false; 721 return false;
622 if (entry_->Get(syncable::IS_DEL)) 722 if (entry_->Get(syncable::IS_DEL))
623 return false; 723 return false;
624 syncable::ModelType model_type = GetModelType(); 724 syncable::ModelType model_type = GetModelType();
625 DCHECK(model_type == syncable::NIGORI); 725 DCHECK_EQ(syncable::NIGORI, model_type);
626 return true; 726 return true;
627 } 727 }
628 728
629 void WriteNode::PutModelType(syncable::ModelType model_type) { 729 void WriteNode::PutModelType(syncable::ModelType model_type) {
630 // Set an empty specifics of the appropriate datatype. The presence 730 // Set an empty specifics of the appropriate datatype. The presence
631 // of the specific extension will identify the model type. 731 // of the specific extension will identify the model type.
632 DCHECK(GetModelType() == model_type || 732 DCHECK(GetModelType() == model_type ||
633 GetModelType() == syncable::UNSPECIFIED); // Immutable once set. 733 GetModelType() == syncable::UNSPECIFIED); // Immutable once set.
634 734
635 sync_pb::EntitySpecifics specifics; 735 sync_pb::EntitySpecifics specifics;
636 syncable::AddDefaultExtensionValue(model_type, &specifics); 736 syncable::AddDefaultExtensionValue(model_type, &specifics);
637 PutSpecificsAndMarkForSyncing(specifics); 737 PutSpecificsAndMarkForSyncing(specifics);
638 DCHECK(GetModelType() == model_type); 738 DCHECK_EQ(model_type, GetModelType());
639 } 739 }
640 740
641 // Create a new node with default properties, and bind this WriteNode to it. 741 // Create a new node with default properties, and bind this WriteNode to it.
642 // Return true on success. 742 // Return true on success.
643 bool WriteNode::InitByCreation(syncable::ModelType model_type, 743 bool WriteNode::InitByCreation(syncable::ModelType model_type,
644 const BaseNode& parent, 744 const BaseNode& parent,
645 const BaseNode* predecessor) { 745 const BaseNode* predecessor) {
646 DCHECK(!entry_) << "Init called twice"; 746 DCHECK(!entry_) << "Init called twice";
647 // |predecessor| must be a child of |parent| or NULL. 747 // |predecessor| must be a child of |parent| or NULL.
648 if (predecessor && predecessor->GetParentId() != parent.GetId()) { 748 if (predecessor && predecessor->GetParentId() != parent.GetId()) {
(...skipping 345 matching lines...) Expand 10 before | Expand all | Expand 10 after
994 1094
995 // Update tokens that we're using in Sync. Email must stay the same. 1095 // Update tokens that we're using in Sync. Email must stay the same.
996 void UpdateCredentials(const SyncCredentials& credentials); 1096 void UpdateCredentials(const SyncCredentials& credentials);
997 1097
998 // Tell the sync engine to start the syncing process. 1098 // Tell the sync engine to start the syncing process.
999 void StartSyncing(); 1099 void StartSyncing();
1000 1100
1001 // Whether or not the Nigori node is encrypted using an explicit passphrase. 1101 // Whether or not the Nigori node is encrypted using an explicit passphrase.
1002 bool IsUsingExplicitPassphrase(); 1102 bool IsUsingExplicitPassphrase();
1003 1103
1104 // Set the datatypes we want to encrypt and encrypt any nodes as necessary.
1105 void EncryptDataTypes(const syncable::ModelTypeSet& encrypted_types);
1106
1004 // Try to set the current passphrase to |passphrase|, and record whether 1107 // Try to set the current passphrase to |passphrase|, and record whether
1005 // it is an explicit passphrase or implicitly using gaia in the Nigori 1108 // it is an explicit passphrase or implicitly using gaia in the Nigori
1006 // node. 1109 // node.
1007 void SetPassphrase(const std::string& passphrase, bool is_explicit); 1110 void SetPassphrase(const std::string& passphrase, bool is_explicit);
1008 1111
1009 // Call periodically from a database-safe thread to persist recent changes 1112 // Call periodically from a database-safe thread to persist recent changes
1010 // to the syncapi model. 1113 // to the syncapi model.
1011 void SaveChanges(); 1114 void SaveChanges();
1012 1115
1013 // This listener is called upon completion of a syncable transaction, and 1116 // This listener is called upon completion of a syncable transaction, and
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after
1066 // See SyncManager::Shutdown for information. 1169 // See SyncManager::Shutdown for information.
1067 void Shutdown(); 1170 void Shutdown();
1068 1171
1069 // Whether we're initialized to the point of being able to accept changes 1172 // Whether we're initialized to the point of being able to accept changes
1070 // (and hence allow transaction creation). See initialized_ for details. 1173 // (and hence allow transaction creation). See initialized_ for details.
1071 bool initialized() const { 1174 bool initialized() const {
1072 base::AutoLock lock(initialized_mutex_); 1175 base::AutoLock lock(initialized_mutex_);
1073 return initialized_; 1176 return initialized_;
1074 } 1177 }
1075 1178
1179 // If this is a deletion for a password, sets the legacy
1180 // ExtraPasswordChangeRecordData field of |buffer|. Otherwise sets
1181 // |buffer|'s specifics field to contain the unencrypted data.
1076 void SetExtraChangeRecordData(int64 id, 1182 void SetExtraChangeRecordData(int64 id,
1077 syncable::ModelType type, 1183 syncable::ModelType type,
1078 ChangeReorderBuffer* buffer, 1184 ChangeReorderBuffer* buffer,
1079 Cryptographer* cryptographer, 1185 Cryptographer* cryptographer,
1080 const syncable::EntryKernel& original, 1186 const syncable::EntryKernel& original,
1081 bool existed_before, 1187 bool existed_before,
1082 bool exists_now); 1188 bool exists_now);
1083 1189
1084 // Called only by our NetworkChangeNotifier. 1190 // Called only by our NetworkChangeNotifier.
1085 virtual void OnIPAddressChanged(); 1191 virtual void OnIPAddressChanged();
(...skipping 105 matching lines...) Expand 10 before | Expand all | Expand 10 after
1191 return true; 1297 return true;
1192 if (a.ref(syncable::PARENT_ID) != b.Get(syncable::PARENT_ID)) 1298 if (a.ref(syncable::PARENT_ID) != b.Get(syncable::PARENT_ID))
1193 return true; 1299 return true;
1194 return false; 1300 return false;
1195 } 1301 }
1196 1302
1197 // Determine if any of the fields made visible to clients of the Sync API 1303 // Determine if any of the fields made visible to clients of the Sync API
1198 // differ between the versions of an entry stored in |a| and |b|. A return 1304 // differ between the versions of an entry stored in |a| and |b|. A return
1199 // value of false means that it should be OK to ignore this change. 1305 // value of false means that it should be OK to ignore this change.
1200 static bool VisiblePropertiesDiffer(const syncable::EntryKernel& a, 1306 static bool VisiblePropertiesDiffer(const syncable::EntryKernel& a,
1201 const syncable::Entry& b) { 1307 const syncable::Entry& b,
1308 Cryptographer* cryptographer) {
1202 syncable::ModelType model_type = b.GetModelType(); 1309 syncable::ModelType model_type = b.GetModelType();
1203 // Suppress updates to items that aren't tracked by any browser model. 1310 // Suppress updates to items that aren't tracked by any browser model.
1204 if (model_type == syncable::UNSPECIFIED || 1311 if (model_type == syncable::UNSPECIFIED ||
1205 model_type == syncable::TOP_LEVEL_FOLDER) { 1312 model_type == syncable::TOP_LEVEL_FOLDER) {
1206 return false; 1313 return false;
1207 } 1314 }
1208 if (a.ref(syncable::NON_UNIQUE_NAME) != b.Get(syncable::NON_UNIQUE_NAME)) 1315 if (a.ref(syncable::NON_UNIQUE_NAME) != b.Get(syncable::NON_UNIQUE_NAME))
1209 return true; 1316 return true;
1210 if (a.ref(syncable::IS_DIR) != b.Get(syncable::IS_DIR)) 1317 if (a.ref(syncable::IS_DIR) != b.Get(syncable::IS_DIR))
1211 return true; 1318 return true;
1212 if (a.ref(SPECIFICS).SerializeAsString() != 1319 // Check if data has changed (account for encryption).
1213 b.Get(SPECIFICS).SerializeAsString()) { 1320 std::string a_str, b_str;
1321 if (a.ref(SPECIFICS).has_encrypted()) {
1322 const sync_pb::EncryptedData& encrypted = a.ref(SPECIFICS).encrypted();
1323 a_str = cryptographer->DecryptToString(encrypted);
1324 } else {
1325 a_str = a.ref(SPECIFICS).SerializeAsString();
1326 }
1327 if (b.Get(SPECIFICS).has_encrypted()) {
1328 const sync_pb::EncryptedData& encrypted = b.Get(SPECIFICS).encrypted();
1329 b_str = cryptographer->DecryptToString(encrypted);
1330 } else {
1331 b_str = b.Get(SPECIFICS).SerializeAsString();
1332 }
1333 if (a_str != b_str) {
1214 return true; 1334 return true;
1215 } 1335 }
1216 if (VisiblePositionsDiffer(a, b)) 1336 if (VisiblePositionsDiffer(a, b))
1217 return true; 1337 return true;
1218 return false; 1338 return false;
1219 } 1339 }
1220 1340
1221 bool ChangeBuffersAreEmpty() { 1341 bool ChangeBuffersAreEmpty() {
1222 for (int i = 0; i < syncable::MODEL_TYPE_COUNT; ++i) { 1342 for (int i = 0; i < syncable::MODEL_TYPE_COUNT; ++i) {
1223 if (!change_buffers_[i].IsEmpty()) 1343 if (!change_buffers_[i].IsEmpty())
(...skipping 176 matching lines...) Expand 10 before | Expand all | Expand 10 after
1400 syncable::AutofillMigrationDebugInfo::PropertyToSet property_to_set, 1520 syncable::AutofillMigrationDebugInfo::PropertyToSet property_to_set,
1401 const syncable::AutofillMigrationDebugInfo& info) { 1521 const syncable::AutofillMigrationDebugInfo& info) {
1402 return data_->SetAutofillMigrationDebugInfo(property_to_set, info); 1522 return data_->SetAutofillMigrationDebugInfo(property_to_set, info);
1403 } 1523 }
1404 1524
1405 void SyncManager::SetPassphrase(const std::string& passphrase, 1525 void SyncManager::SetPassphrase(const std::string& passphrase,
1406 bool is_explicit) { 1526 bool is_explicit) {
1407 data_->SetPassphrase(passphrase, is_explicit); 1527 data_->SetPassphrase(passphrase, is_explicit);
1408 } 1528 }
1409 1529
1530 void SyncManager::EncryptDataTypes(
1531 const syncable::ModelTypeSet& encrypted_types) {
1532 data_->EncryptDataTypes(encrypted_types);
1533 }
1534
1410 bool SyncManager::IsUsingExplicitPassphrase() { 1535 bool SyncManager::IsUsingExplicitPassphrase() {
1411 return data_ && data_->IsUsingExplicitPassphrase(); 1536 return data_ && data_->IsUsingExplicitPassphrase();
1412 } 1537 }
1413 1538
1414 bool SyncManager::RequestPause() { 1539 bool SyncManager::RequestPause() {
1415 if (data_->syncer_thread()) 1540 if (data_->syncer_thread())
1416 return data_->syncer_thread()->RequestPause(); 1541 return data_->syncer_thread()->RequestPause();
1417 return false; 1542 return false;
1418 } 1543 }
1419 1544
(...skipping 85 matching lines...) Expand 10 before | Expand all | Expand 10 after
1505 NOTREACHED(); 1630 NOTREACHED();
1506 return; 1631 return;
1507 } 1632 }
1508 1633
1509 if (!lookup->initial_sync_ended_for_type(syncable::NIGORI)) 1634 if (!lookup->initial_sync_ended_for_type(syncable::NIGORI))
1510 return; 1635 return;
1511 1636
1512 Cryptographer* cryptographer = share_.dir_manager->cryptographer(); 1637 Cryptographer* cryptographer = share_.dir_manager->cryptographer();
1513 cryptographer->Bootstrap(restored_key_for_bootstrapping); 1638 cryptographer->Bootstrap(restored_key_for_bootstrapping);
1514 1639
1515 ReadTransaction trans(GetUserShare()); 1640 sync_pb::NigoriSpecifics nigori;
1516 ReadNode node(&trans); 1641 {
1517 if (!node.InitByTagLookup(kNigoriTag)) { 1642 ReadTransaction trans(GetUserShare());
1518 NOTREACHED(); 1643 ReadNode node(&trans);
1519 return; 1644 if (!node.InitByTagLookup(kNigoriTag)) {
1645 NOTREACHED();
1646 return;
1647 }
1648
1649 nigori.CopyFrom(node.GetNigoriSpecifics());
1650 if (!nigori.encrypted().blob().empty()) {
1651 if (cryptographer->CanDecrypt(nigori.encrypted())) {
1652 cryptographer->SetKeys(nigori.encrypted());
1653 } else {
1654 cryptographer->SetPendingKeys(nigori.encrypted());
1655 observer_->OnPassphraseRequired(true);
1656 }
1657 }
1520 } 1658 }
1521 1659
1522 const sync_pb::NigoriSpecifics& nigori = node.GetNigoriSpecifics(); 1660 // Refresh list of encrypted datatypes.
1523 if (!nigori.encrypted().blob().empty()) { 1661 syncable::ModelTypeSet encrypted_types =
1524 if (cryptographer->CanDecrypt(nigori.encrypted())) { 1662 syncable::GetEncryptedDataTypesFromNigori(nigori);
1525 cryptographer->SetKeys(nigori.encrypted()); 1663
1526 } else { 1664 // Ensure any datatypes that need encryption are encrypted.
1527 cryptographer->SetPendingKeys(nigori.encrypted()); 1665 EncryptDataTypes(encrypted_types);
1528 observer_->OnPassphraseRequired(true);
1529 }
1530 }
1531 } 1666 }
1532 1667
1533 void SyncManager::SyncInternal::StartSyncing() { 1668 void SyncManager::SyncInternal::StartSyncing() {
1534 if (syncer_thread()) // NULL during certain unittests. 1669 if (syncer_thread()) // NULL during certain unittests.
1535 syncer_thread()->Start(); // Start the syncer thread. This won't actually 1670 syncer_thread()->Start(); // Start the syncer thread. This won't actually
1536 // result in any syncing until at least the 1671 // result in any syncing until at least the
1537 // DirectoryManager broadcasts the OPENED event, 1672 // DirectoryManager broadcasts the OPENED event,
1538 // and a valid server connection is detected. 1673 // and a valid server connection is detected.
1539 } 1674 }
1540 1675
(...skipping 90 matching lines...) Expand 10 before | Expand all | Expand 10 after
1631 1766
1632 if (!setup_for_test_mode_) { 1767 if (!setup_for_test_mode_) {
1633 UpdateCredentials(credentials); 1768 UpdateCredentials(credentials);
1634 } 1769 }
1635 return true; 1770 return true;
1636 } 1771 }
1637 1772
1638 void SyncManager::SyncInternal::UpdateCredentials( 1773 void SyncManager::SyncInternal::UpdateCredentials(
1639 const SyncCredentials& credentials) { 1774 const SyncCredentials& credentials) {
1640 DCHECK_EQ(MessageLoop::current(), core_message_loop_); 1775 DCHECK_EQ(MessageLoop::current(), core_message_loop_);
1641 DCHECK(share_.name == credentials.email); 1776 DCHECK_EQ(credentials.email, share_.name);
1642 connection_manager()->set_auth_token(credentials.sync_token); 1777 connection_manager()->set_auth_token(credentials.sync_token);
1643 TalkMediatorLogin(credentials.email, credentials.sync_token); 1778 TalkMediatorLogin(credentials.email, credentials.sync_token);
1644 CheckServerReachable(); 1779 CheckServerReachable();
1645 sync_manager_->RequestNudge(); 1780 sync_manager_->RequestNudge();
1646 } 1781 }
1647 1782
1648 void SyncManager::SyncInternal::InitializeTalkMediator() { 1783 void SyncManager::SyncInternal::InitializeTalkMediator() {
1649 if (notifier_options_.notification_method == 1784 if (notifier_options_.notification_method ==
1650 notifier::NOTIFICATION_SERVER) { 1785 notifier::NOTIFICATION_SERVER) {
1651 syncable::ScopedDirLookup lookup(dir_manager(), username_for_share()); 1786 syncable::ScopedDirLookup lookup(dir_manager(), username_for_share());
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after
1706 observer_->OnPassphraseRequired(true); 1841 observer_->OnPassphraseRequired(true);
1707 return; 1842 return;
1708 } 1843 }
1709 1844
1710 // TODO(tim): If this is the first time the user has entered a passphrase 1845 // TODO(tim): If this is the first time the user has entered a passphrase
1711 // since the protocol changed to store passphrase preferences in the cloud, 1846 // since the protocol changed to store passphrase preferences in the cloud,
1712 // make sure we update this preference. See bug 62103. 1847 // make sure we update this preference. See bug 62103.
1713 if (is_explicit) 1848 if (is_explicit)
1714 SetUsingExplicitPassphrasePrefForMigration(); 1849 SetUsingExplicitPassphrasePrefForMigration();
1715 1850
1716 // Nudge the syncer so that passwords updates that were waiting for this 1851 // Nudge the syncer so that encrypted datatype updates that were waiting for
1717 // passphrase get applied as soon as possible. 1852 // this passphrase get applied as soon as possible.
1718 sync_manager_->RequestNudge(); 1853 sync_manager_->RequestNudge();
1719 } else { 1854 } else {
1720 WriteTransaction trans(GetUserShare()); 1855 WriteTransaction trans(GetUserShare());
1721 WriteNode node(&trans); 1856 WriteNode node(&trans);
1722 if (!node.InitByTagLookup(kNigoriTag)) { 1857 if (!node.InitByTagLookup(kNigoriTag)) {
1723 // TODO(albertb): Plumb an UnrecoverableError all the way back to the PSS. 1858 // TODO(albertb): Plumb an UnrecoverableError all the way back to the PSS.
1724 NOTREACHED(); 1859 NOTREACHED();
1725 return; 1860 return;
1726 } 1861 }
1727 1862
(...skipping 25 matching lines...) Expand all
1753 ReadNode node(&trans); 1888 ReadNode node(&trans);
1754 if (!node.InitByTagLookup(kNigoriTag)) { 1889 if (!node.InitByTagLookup(kNigoriTag)) {
1755 // TODO(albertb): Plumb an UnrecoverableError all the way back to the PSS. 1890 // TODO(albertb): Plumb an UnrecoverableError all the way back to the PSS.
1756 NOTREACHED(); 1891 NOTREACHED();
1757 return false; 1892 return false;
1758 } 1893 }
1759 1894
1760 return node.GetNigoriSpecifics().using_explicit_passphrase(); 1895 return node.GetNigoriSpecifics().using_explicit_passphrase();
1761 } 1896 }
1762 1897
1763 void SyncManager::SyncInternal::ReEncryptEverything(WriteTransaction* trans) { 1898 void SyncManager::SyncInternal::EncryptDataTypes(
1764 // TODO(tim): bug 59242. We shouldn't lookup by data type and instead use 1899 const syncable::ModelTypeSet& encrypted_types) {
1765 // a protocol flag or existence of an EncryptedData message, but for now, 1900 // Verify the encrypted types are all enabled.
1766 // encryption is on if-and-only-if the type is passwords, and we haven't 1901 ModelSafeRoutingInfo routes;
1767 // ironed out the protocol for generic encryption. 1902 registrar_->GetModelSafeRoutingInfo(&routes);
1768 static const char* passwords_tag = "google_chrome_passwords"; 1903 for (syncable::ModelTypeSet::iterator iter = encrypted_types.begin();
1769 ReadNode passwords_root(trans); 1904 iter != encrypted_types.end(); ++iter) {
1770 if (!passwords_root.InitByTagLookup(passwords_tag)) { 1905 if (routes.count(*iter) == 0){
1771 LOG(WARNING) << "No passwords to reencrypt."; 1906 LOG(ERROR) << "Attempted to encrypt non-enabled datatype "
1907 << syncable::ModelTypeToString(*iter);
tim (not reviewing) 2011/02/15 00:10:57 Does SyncBackendHost enforce that registrar can't
Nicolas Zea 2011/02/15 19:52:18 Good point. It's probably better to just remove an
1908 NOTREACHED();
1909 return;
1910 }
1911 }
1912 if (routes.count(syncable::PASSWORDS) > 0 &&
1913 encrypted_types.count(syncable::PASSWORDS) == 0) {
1914 LOG(ERROR) << "Attempted to set PASSWORDS datatype as unencrypted";
1915 NOTREACHED();
1916 return;
1917 }
1918 WriteTransaction trans(GetUserShare());
1919 WriteNode node(&trans);
1920 if (!node.InitByTagLookup(kNigoriTag)) {
1921 LOG(ERROR) << "Unable to set encrypted datatypes because Nigori node not "
1922 << "found.";
1923 NOTREACHED();
1772 return; 1924 return;
1773 } 1925 }
1774 1926
1775 int64 child_id = passwords_root.GetFirstChildId(); 1927 // Update the Nigori node set of encrypted datatypes so other machines notice.
1776 while (child_id != kInvalidId) { 1928 sync_pb::NigoriSpecifics nigori;
1777 WriteNode child(trans); 1929 nigori.CopyFrom(node.GetNigoriSpecifics());
1778 if (!child.InitByIdLookup(child_id)) { 1930 syncable::FillNigoriEncryptedTypes(encrypted_types, &nigori);
1931 node.SetNigoriSpecifics(nigori);
1932
1933 // TODO(zea): only reencrypt this datatype? ReEncrypting everything is a
1934 // safer approach, and should not impact anything that is already encrypted
1935 // (redundant changes are ignored).
1936 ReEncryptEverything(&trans);
1937 return;
1938 }
1939
1940 void SyncManager::SyncInternal::ReEncryptEverything(WriteTransaction* trans) {
1941 syncable::ModelTypeSet encrypted_types =
1942 GetEncryptedDataTypesFromSyncer(trans->GetWrappedTrans());
1943 std::string tag;
1944 for (syncable::ModelTypeSet::iterator iter = encrypted_types.begin();
1945 iter != encrypted_types.end(); ++iter) {
1946 if (*iter == syncable::PASSWORDS)
1947 continue; // Has special implementation below.
1948 ReadNode type_root(trans);
1949 tag = syncable::ModelTypeToRootTag(*iter);
1950 if (!type_root.InitByTagLookup(tag)) {
1779 NOTREACHED(); 1951 NOTREACHED();
1780 return; 1952 return;
1781 } 1953 }
1782 child.SetPasswordSpecifics(child.GetPasswordSpecifics()); 1954
1783 child_id = child.GetSuccessorId(); 1955 // Iterate through all children of this datatype.
1956 std::queue<int64> to_visit;
1957 int64 child_id = type_root.GetFirstChildId();
1958 to_visit.push(child_id);
1959 while (!to_visit.empty()) {
1960 child_id = to_visit.front();
1961 to_visit.pop();
1962 if (child_id == kInvalidId)
1963 continue;
1964
1965 WriteNode child(trans);
1966 if (!child.InitByIdLookup(child_id)) {
1967 NOTREACHED();
1968 return;
1969 }
1970 if (child.GetIsFolder()) {
1971 to_visit.push(child.GetFirstChildId());
1972 } else {
1973 // Rewrite the specifics of the node with encrypted data if necessary.
1974 child.ResetFromSpecifics();
1975 }
1976 to_visit.push(child.GetSuccessorId());
1977 }
1784 } 1978 }
1979
1980 if (encrypted_types.count(syncable::PASSWORDS) > 0) {
1981 // Passwords are encrypted with their own legacy scheme.
1982 ReadNode passwords_root(trans);
1983 std::string passwords_tag =
1984 syncable::ModelTypeToRootTag(syncable::PASSWORDS);
1985 if (!passwords_root.InitByTagLookup(passwords_tag)) {
1986 LOG(WARNING) << "No passwords to reencrypt.";
1987 return;
1988 }
1989
1990 int64 child_id = passwords_root.GetFirstChildId();
1991 while (child_id != kInvalidId) {
1992 WriteNode child(trans);
1993 if (!child.InitByIdLookup(child_id)) {
1994 NOTREACHED();
1995 return;
1996 }
1997 child.SetPasswordSpecifics(child.GetPasswordSpecifics());
1998 child_id = child.GetSuccessorId();
1999 }
2000 }
2001
2002 observer_->OnEncryptionComplete(encrypted_types);
1785 } 2003 }
1786 2004
1787 SyncManager::~SyncManager() { 2005 SyncManager::~SyncManager() {
1788 delete data_; 2006 delete data_;
1789 } 2007 }
1790 2008
1791 void SyncManager::SetObserver(Observer* observer) { 2009 void SyncManager::SetObserver(Observer* observer) {
1792 data_->set_observer(observer); 2010 data_->set_observer(observer);
1793 } 2011 }
1794 2012
(...skipping 219 matching lines...) Expand 10 before | Expand all | Expand 10 after
2014 nudge_delay, 2232 nudge_delay,
2015 SyncerThread::kLocal, 2233 SyncerThread::kLocal,
2016 model_types); 2234 model_types);
2017 } 2235 }
2018 } 2236 }
2019 2237
2020 void SyncManager::SyncInternal::SetExtraChangeRecordData(int64 id, 2238 void SyncManager::SyncInternal::SetExtraChangeRecordData(int64 id,
2021 syncable::ModelType type, ChangeReorderBuffer* buffer, 2239 syncable::ModelType type, ChangeReorderBuffer* buffer,
2022 Cryptographer* cryptographer, const syncable::EntryKernel& original, 2240 Cryptographer* cryptographer, const syncable::EntryKernel& original,
2023 bool existed_before, bool exists_now) { 2241 bool existed_before, bool exists_now) {
2024 // If this is a deletion, attach the entity specifics as extra data 2242 // If this is a deletion and the datatype was encrypted, we need to decrypt it
2025 // so that the delete can be processed. 2243 // and attach it to the buffer.
2026 if (!exists_now && existed_before) { 2244 if (!exists_now && existed_before) {
2027 buffer->SetSpecificsForId(id, original.ref(SPECIFICS)); 2245 sync_pb::EntitySpecifics original_specifics(original.ref(SPECIFICS));
2028 if (type == syncable::PASSWORDS) { 2246 if (type == syncable::PASSWORDS) {
2029 // Need to dig a bit deeper as passwords are encrypted. 2247 // Passwords must use their own legacy ExtraPasswordChangeRecordData.
2030 scoped_ptr<sync_pb::PasswordSpecificsData> data( 2248 scoped_ptr<sync_pb::PasswordSpecificsData> data(
2031 DecryptPasswordSpecifics(original.ref(SPECIFICS), cryptographer)); 2249 DecryptPasswordSpecifics(original_specifics, cryptographer));
2032 if (!data.get()) { 2250 if (!data.get()) {
2033 NOTREACHED(); 2251 NOTREACHED();
2034 return; 2252 return;
2035 } 2253 }
2036 buffer->SetExtraDataForId(id, new ExtraPasswordChangeRecordData(*data)); 2254 buffer->SetExtraDataForId(id, new ExtraPasswordChangeRecordData(*data));
2255 } else if (original_specifics.has_encrypted()) {
2256 // All other datatypes can just create a new unencrypted specifics and
2257 // attach it.
2258 const sync_pb::EncryptedData& encrypted = original_specifics.encrypted();
2259 if (!cryptographer->Decrypt(encrypted, &original_specifics)) {
2260 NOTREACHED();
2261 return;
2262 }
2037 } 2263 }
2264 buffer->SetSpecificsForId(id, original_specifics);
2038 } 2265 }
2039 } 2266 }
2040 2267
2041 void SyncManager::SyncInternal::HandleCalculateChangesChangeEventFromSyncer( 2268 void SyncManager::SyncInternal::HandleCalculateChangesChangeEventFromSyncer(
2042 const syncable::DirectoryChangeEvent& event) { 2269 const syncable::DirectoryChangeEvent& event) {
2043 // We only expect one notification per sync step, so change_buffers_ should 2270 // We only expect one notification per sync step, so change_buffers_ should
2044 // contain no pending entries. 2271 // contain no pending entries.
2045 DCHECK_EQ(event.todo, syncable::DirectoryChangeEvent::CALCULATE_CHANGES); 2272 DCHECK_EQ(event.todo, syncable::DirectoryChangeEvent::CALCULATE_CHANGES);
2046 DCHECK(event.writer == syncable::SYNCER || 2273 DCHECK(event.writer == syncable::SYNCER ||
2047 event.writer == syncable::UNITTEST); 2274 event.writer == syncable::UNITTEST);
(...skipping 10 matching lines...) Expand all
2058 2285
2059 // Omit items that aren't associated with a model. 2286 // Omit items that aren't associated with a model.
2060 syncable::ModelType type = e.GetModelType(); 2287 syncable::ModelType type = e.GetModelType();
2061 if (type == syncable::TOP_LEVEL_FOLDER || type == syncable::UNSPECIFIED) 2288 if (type == syncable::TOP_LEVEL_FOLDER || type == syncable::UNSPECIFIED)
2062 continue; 2289 continue;
2063 2290
2064 if (exists_now && !existed_before) 2291 if (exists_now && !existed_before)
2065 change_buffers_[type].PushAddedItem(id); 2292 change_buffers_[type].PushAddedItem(id);
2066 else if (!exists_now && existed_before) 2293 else if (!exists_now && existed_before)
2067 change_buffers_[type].PushDeletedItem(id); 2294 change_buffers_[type].PushDeletedItem(id);
2068 else if (exists_now && existed_before && VisiblePropertiesDiffer(*i, e)) 2295 else if (exists_now && existed_before &&
2296 VisiblePropertiesDiffer(*i, e, dir_manager()->cryptographer())) {
2069 change_buffers_[type].PushUpdatedItem(id, VisiblePositionsDiffer(*i, e)); 2297 change_buffers_[type].PushUpdatedItem(id, VisiblePositionsDiffer(*i, e));
2298 }
2070 2299
2071 SetExtraChangeRecordData(id, type, &change_buffers_[type], 2300 SetExtraChangeRecordData(id, type, &change_buffers_[type],
2072 dir_manager()->cryptographer(), *i, 2301 dir_manager()->cryptographer(), *i,
2073 existed_before, exists_now); 2302 existed_before, exists_now);
2074 } 2303 }
2075 } 2304 }
2076 2305
2077 SyncManager::Status SyncManager::SyncInternal::GetStatus() { 2306 SyncManager::Status SyncManager::SyncInternal::GetStatus() {
2078 return allstatus_.status(); 2307 return allstatus_.status();
2079 } 2308 }
2080 2309
2081 void SyncManager::SyncInternal::OnSyncEngineEvent( 2310 void SyncManager::SyncInternal::OnSyncEngineEvent(
2082 const SyncEngineEvent& event) { 2311 const SyncEngineEvent& event) {
2083 if (!observer_) 2312 if (!observer_)
2084 return; 2313 return;
2085 2314
2086 // Only send an event if this is due to a cycle ending and this cycle 2315 // Only send an event if this is due to a cycle ending and this cycle
2087 // concludes a canonical "sync" process; that is, based on what is known 2316 // concludes a canonical "sync" process; that is, based on what is known
2088 // locally we are "all happy" and up-to-date. There may be new changes on 2317 // locally we are "all happy" and up-to-date. There may be new changes on
2089 // the server, but we'll get them on a subsequent sync. 2318 // the server, but we'll get them on a subsequent sync.
2090 // 2319 //
2091 // Notifications are sent at the end of every sync cycle, regardless of 2320 // Notifications are sent at the end of every sync cycle, regardless of
2092 // whether we should sync again. 2321 // whether we should sync again.
2093 if (event.what_happened == SyncEngineEvent::SYNC_CYCLE_ENDED) { 2322 if (event.what_happened == SyncEngineEvent::SYNC_CYCLE_ENDED) {
2094 ModelSafeRoutingInfo enabled_types; 2323 ModelSafeRoutingInfo enabled_types;
2095 registrar_->GetModelSafeRoutingInfo(&enabled_types); 2324 registrar_->GetModelSafeRoutingInfo(&enabled_types);
2096 if (enabled_types.count(syncable::PASSWORDS) > 0) { 2325 {
2097 Cryptographer* cryptographer = 2326 // Check to see if we need to notify the frontend that we have newly
2098 GetUserShare()->dir_manager->cryptographer(); 2327 // encrypted types or that we require a passphrase.
2099 if (!cryptographer->is_ready() && !cryptographer->has_pending_keys()) { 2328 sync_api::ReadTransaction trans(GetUserShare());
2100 sync_api::ReadTransaction trans(GetUserShare()); 2329 sync_api::ReadNode node(&trans);
2101 sync_api::ReadNode node(&trans); 2330 if (!node.InitByTagLookup(kNigoriTag)) {
2102 if (!node.InitByTagLookup(kNigoriTag)) { 2331 DCHECK(!event.snapshot->is_share_usable);
2103 DCHECK(!event.snapshot->is_share_usable); 2332 return;
2104 return; 2333 }
2334 const sync_pb::NigoriSpecifics& nigori = node.GetNigoriSpecifics();
2335 syncable::ModelTypeSet encrypted_types =
2336 syncable::GetEncryptedDataTypesFromNigori(nigori);
2337 // If this is a first time sync with encryption, it's possible Passwords
2338 // hasn't been added to the encryption types list.
2339 if (enabled_types.count(syncable::PASSWORDS) > 0)
2340 encrypted_types.insert(syncable::PASSWORDS);
2341 if (encrypted_types.size() > 0) {
2342 Cryptographer* cryptographer =
2343 GetUserShare()->dir_manager->cryptographer();
2344 if (!cryptographer->is_ready() && !cryptographer->has_pending_keys()) {
2345 if (!nigori.encrypted().blob().empty()) {
2346 DCHECK(!cryptographer->CanDecrypt(nigori.encrypted()));
2347 cryptographer->SetPendingKeys(nigori.encrypted());
2348 }
2105 } 2349 }
2106 const sync_pb::NigoriSpecifics& nigori = node.GetNigoriSpecifics(); 2350
2107 if (!nigori.encrypted().blob().empty()) { 2351 // If we've completed a sync cycle and the cryptographer isn't ready
2108 DCHECK(!cryptographer->CanDecrypt(nigori.encrypted())); 2352 // yet, prompt the user for a passphrase.
2109 cryptographer->SetPendingKeys(nigori.encrypted()); 2353 if (cryptographer->has_pending_keys()) {
2354 observer_->OnPassphraseRequired(true);
2355 } else if (!cryptographer->is_ready()) {
2356 observer_->OnPassphraseRequired(false);
2357 } else {
2358 observer_->OnEncryptionComplete(encrypted_types);
2110 } 2359 }
2111 } 2360 }
2112
2113 // If we've completed a sync cycle and the cryptographer isn't ready yet,
2114 // prompt the user for a passphrase.
2115 if (cryptographer->has_pending_keys()) {
2116 observer_->OnPassphraseRequired(true);
2117 } else if (!cryptographer->is_ready()) {
2118 observer_->OnPassphraseRequired(false);
2119 }
2120 } 2361 }
2121 2362
2122 if (!initialized()) 2363 if (!initialized())
2123 return; 2364 return;
2124 2365
2125 if (!event.snapshot->has_more_to_sync) { 2366 if (!event.snapshot->has_more_to_sync) {
2126 observer_->OnSyncCycleCompleted(event.snapshot); 2367 observer_->OnSyncCycleCompleted(event.snapshot);
2127 } 2368 }
2128 2369
2129 if (notifier_options_.notification_method != 2370 if (notifier_options_.notification_method !=
(...skipping 330 matching lines...) Expand 10 before | Expand all | Expand 10 after
2460 2701
2461 void SyncManager::TriggerOnIncomingNotificationForTest( 2702 void SyncManager::TriggerOnIncomingNotificationForTest(
2462 const syncable::ModelTypeBitSet& model_types) { 2703 const syncable::ModelTypeBitSet& model_types) {
2463 IncomingNotificationData notification_data; 2704 IncomingNotificationData notification_data;
2464 notification_data.service_url = model_types.to_string(); 2705 notification_data.service_url = model_types.to_string();
2465 // Here we rely on the default notification method being SERVER. 2706 // Here we rely on the default notification method being SERVER.
2466 data_->OnIncomingNotification(notification_data); 2707 data_->OnIncomingNotification(notification_data);
2467 } 2708 }
2468 2709
2469 } // namespace sync_api 2710 } // namespace sync_api
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698