| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 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/internal_api/write_node.h" | 5 #include "chrome/browser/sync/internal_api/write_node.h" |
| 6 | 6 |
| 7 #include "base/json/json_writer.h" | |
| 8 #include "base/utf_string_conversions.h" | 7 #include "base/utf_string_conversions.h" |
| 9 #include "base/values.h" | 8 #include "base/values.h" |
| 10 #include "chrome/browser/sync/engine/nigori_util.h" | 9 #include "chrome/browser/sync/internal_api/syncapi_internal.h" |
| 11 #include "chrome/browser/sync/engine/syncapi_internal.h" | |
| 12 #include "chrome/browser/sync/internal_api/base_transaction.h" | 10 #include "chrome/browser/sync/internal_api/base_transaction.h" |
| 13 #include "chrome/browser/sync/internal_api/write_transaction.h" | 11 #include "chrome/browser/sync/internal_api/write_transaction.h" |
| 14 #include "chrome/browser/sync/syncable/syncable.h" | 12 #include "sync/engine/nigori_util.h" |
| 15 #include "chrome/browser/sync/util/cryptographer.h" | |
| 16 #include "sync/protocol/app_specifics.pb.h" | 13 #include "sync/protocol/app_specifics.pb.h" |
| 17 #include "sync/protocol/autofill_specifics.pb.h" | 14 #include "sync/protocol/autofill_specifics.pb.h" |
| 18 #include "sync/protocol/bookmark_specifics.pb.h" | 15 #include "sync/protocol/bookmark_specifics.pb.h" |
| 19 #include "sync/protocol/extension_specifics.pb.h" | 16 #include "sync/protocol/extension_specifics.pb.h" |
| 20 #include "sync/protocol/password_specifics.pb.h" | 17 #include "sync/protocol/password_specifics.pb.h" |
| 21 #include "sync/protocol/session_specifics.pb.h" | 18 #include "sync/protocol/session_specifics.pb.h" |
| 22 #include "sync/protocol/theme_specifics.pb.h" | 19 #include "sync/protocol/theme_specifics.pb.h" |
| 23 #include "sync/protocol/typed_url_specifics.pb.h" | 20 #include "sync/protocol/typed_url_specifics.pb.h" |
| 21 #include "sync/syncable/syncable.h" |
| 22 #include "sync/util/cryptographer.h" |
| 24 | 23 |
| 25 using browser_sync::Cryptographer; | 24 using browser_sync::Cryptographer; |
| 26 using std::string; | 25 using std::string; |
| 27 using std::vector; | 26 using std::vector; |
| 28 using syncable::kEncryptedString; | 27 using syncable::kEncryptedString; |
| 29 using syncable::SPECIFICS; | 28 using syncable::SPECIFICS; |
| 30 | 29 |
| 31 namespace sync_api { | 30 namespace sync_api { |
| 32 | 31 |
| 33 static const char kDefaultNameForNewNodes[] = " "; | 32 static const char kDefaultNameForNewNodes[] = " "; |
| 34 | 33 |
| 35 bool WriteNode::UpdateEntryWithEncryption( | |
| 36 browser_sync::Cryptographer* cryptographer, | |
| 37 const sync_pb::EntitySpecifics& new_specifics, | |
| 38 syncable::MutableEntry* entry) { | |
| 39 syncable::ModelType type = syncable::GetModelTypeFromSpecifics(new_specifics); | |
| 40 DCHECK_GE(type, syncable::FIRST_REAL_MODEL_TYPE); | |
| 41 const sync_pb::EntitySpecifics& old_specifics = entry->Get(SPECIFICS); | |
| 42 const syncable::ModelTypeSet encrypted_types = | |
| 43 cryptographer->GetEncryptedTypes(); | |
| 44 // It's possible the nigori lost the set of encrypted types. If the current | |
| 45 // specifics are already encrypted, we want to ensure we continue encrypting. | |
| 46 bool was_encrypted = old_specifics.has_encrypted(); | |
| 47 sync_pb::EntitySpecifics generated_specifics; | |
| 48 if (new_specifics.has_encrypted()) { | |
| 49 NOTREACHED() << "New specifics already has an encrypted blob."; | |
| 50 return false; | |
| 51 } | |
| 52 if ((!SpecificsNeedsEncryption(encrypted_types, new_specifics) && | |
| 53 !was_encrypted) || | |
| 54 !cryptographer->is_initialized()) { | |
| 55 // No encryption required or we are unable to encrypt. | |
| 56 generated_specifics.CopyFrom(new_specifics); | |
| 57 } else { | |
| 58 // Encrypt new_specifics into generated_specifics. | |
| 59 if (VLOG_IS_ON(2)) { | |
| 60 scoped_ptr<DictionaryValue> value(entry->ToValue()); | |
| 61 std::string info; | |
| 62 base::JSONWriter::Write(value.get(), true, &info); | |
| 63 DVLOG(2) << "Encrypting specifics of type " | |
| 64 << syncable::ModelTypeToString(type) | |
| 65 << " with content: " | |
| 66 << info; | |
| 67 } | |
| 68 // Only copy over the old specifics if it is of the right type and already | |
| 69 // encrypted. The first time we encrypt a node we start from scratch, hence | |
| 70 // removing all the unencrypted data, but from then on we only want to | |
| 71 // update the node if the data changes or the encryption key changes. | |
| 72 if (syncable::GetModelTypeFromSpecifics(old_specifics) == type && | |
| 73 was_encrypted) { | |
| 74 generated_specifics.CopyFrom(old_specifics); | |
| 75 } else { | |
| 76 syncable::AddDefaultFieldValue(type, &generated_specifics); | |
| 77 } | |
| 78 // Does not change anything if underlying encrypted blob was already up | |
| 79 // to date and encrypted with the default key. | |
| 80 if (!cryptographer->Encrypt(new_specifics, | |
| 81 generated_specifics.mutable_encrypted())) { | |
| 82 NOTREACHED() << "Could not encrypt data for node of type " | |
| 83 << syncable::ModelTypeToString(type); | |
| 84 return false; | |
| 85 } | |
| 86 } | |
| 87 | |
| 88 // It's possible this entry was encrypted but didn't properly overwrite the | |
| 89 // non_unique_name (see crbug.com/96314). | |
| 90 bool encrypted_without_overwriting_name = (was_encrypted && | |
| 91 entry->Get(syncable::NON_UNIQUE_NAME) != kEncryptedString); | |
| 92 | |
| 93 // If we're encrypted but the name wasn't overwritten properly we still want | |
| 94 // to rewrite the entry, irrespective of whether the specifics match. | |
| 95 if (!encrypted_without_overwriting_name && | |
| 96 old_specifics.SerializeAsString() == | |
| 97 generated_specifics.SerializeAsString()) { | |
| 98 DVLOG(2) << "Specifics of type " << syncable::ModelTypeToString(type) | |
| 99 << " already match, dropping change."; | |
| 100 return true; | |
| 101 } | |
| 102 | |
| 103 if (generated_specifics.has_encrypted()) { | |
| 104 // Overwrite the possibly sensitive non-specifics data. | |
| 105 entry->Put(syncable::NON_UNIQUE_NAME, kEncryptedString); | |
| 106 // For bookmarks we actually put bogus data into the unencrypted specifics, | |
| 107 // else the server will try to do it for us. | |
| 108 if (type == syncable::BOOKMARKS) { | |
| 109 sync_pb::BookmarkSpecifics* bookmark_specifics = | |
| 110 generated_specifics.mutable_bookmark(); | |
| 111 if (!entry->Get(syncable::IS_DIR)) | |
| 112 bookmark_specifics->set_url(kEncryptedString); | |
| 113 bookmark_specifics->set_title(kEncryptedString); | |
| 114 } | |
| 115 } | |
| 116 entry->Put(syncable::SPECIFICS, generated_specifics); | |
| 117 DVLOG(1) << "Overwriting specifics of type " | |
| 118 << syncable::ModelTypeToString(type) | |
| 119 << " and marking for syncing."; | |
| 120 syncable::MarkForSyncing(entry); | |
| 121 return true; | |
| 122 } | |
| 123 | |
| 124 void WriteNode::SetIsFolder(bool folder) { | 34 void WriteNode::SetIsFolder(bool folder) { |
| 125 if (entry_->Get(syncable::IS_DIR) == folder) | 35 if (entry_->Get(syncable::IS_DIR) == folder) |
| 126 return; // Skip redundant changes. | 36 return; // Skip redundant changes. |
| 127 | 37 |
| 128 entry_->Put(syncable::IS_DIR, folder); | 38 entry_->Put(syncable::IS_DIR, folder); |
| 129 MarkForSyncing(); | 39 MarkForSyncing(); |
| 130 } | 40 } |
| 131 | 41 |
| 132 void WriteNode::SetTitle(const std::wstring& title) { | 42 void WriteNode::SetTitle(const std::wstring& title) { |
| 133 DCHECK_NE(GetModelType(), syncable::UNSPECIFIED); | 43 DCHECK_NE(GetModelType(), syncable::UNSPECIFIED); |
| (...skipping 442 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 576 sync_pb::BookmarkSpecifics new_value = GetBookmarkSpecifics(); | 486 sync_pb::BookmarkSpecifics new_value = GetBookmarkSpecifics(); |
| 577 new_value.set_favicon(bytes.empty() ? NULL : &bytes[0], bytes.size()); | 487 new_value.set_favicon(bytes.empty() ? NULL : &bytes[0], bytes.size()); |
| 578 SetBookmarkSpecifics(new_value); | 488 SetBookmarkSpecifics(new_value); |
| 579 } | 489 } |
| 580 | 490 |
| 581 void WriteNode::MarkForSyncing() { | 491 void WriteNode::MarkForSyncing() { |
| 582 syncable::MarkForSyncing(entry_); | 492 syncable::MarkForSyncing(entry_); |
| 583 } | 493 } |
| 584 | 494 |
| 585 } // namespace sync_api | 495 } // namespace sync_api |
| OLD | NEW |