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

Side by Side Diff: components/sync/driver/glue/sync_backend_host_impl.cc

Issue 2663783002: [Sync] Split encryption state and logic out of PSS and SBHI. (Closed)
Patch Set: Tweak comment. Created 3 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
OLDNEW
1 // Copyright 2013 The Chromium Authors. All rights reserved. 1 // Copyright 2013 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 "components/sync/driver/glue/sync_backend_host_impl.h" 5 #include "components/sync/driver/glue/sync_backend_host_impl.h"
6 6
7 #include <utility> 7 #include <utility>
8 8
9 #include "base/bind.h" 9 #include "base/bind.h"
10 #include "base/command_line.h" 10 #include "base/command_line.h"
(...skipping 88 matching lines...) Expand 10 before | Expand all | Expand 10 after
99 SDVLOG(1) << "SyncBackendHostImpl::StartSyncingWithServer called."; 99 SDVLOG(1) << "SyncBackendHostImpl::StartSyncingWithServer called.";
100 100
101 sync_task_runner_->PostTask( 101 sync_task_runner_->PostTask(
102 FROM_HERE, base::Bind(&SyncBackendHostCore::DoStartSyncing, core_, 102 FROM_HERE, base::Bind(&SyncBackendHostCore::DoStartSyncing, core_,
103 sync_prefs_->GetLastPollTime())); 103 sync_prefs_->GetLastPollTime()));
104 } 104 }
105 105
106 void SyncBackendHostImpl::SetEncryptionPassphrase(const std::string& passphrase, 106 void SyncBackendHostImpl::SetEncryptionPassphrase(const std::string& passphrase,
107 bool is_explicit) { 107 bool is_explicit) {
108 DCHECK(thread_checker_.CalledOnValidThread()); 108 DCHECK(thread_checker_.CalledOnValidThread());
109
110 if (!IsNigoriEnabled()) {
111 NOTREACHED() << "SetEncryptionPassphrase must never be called when nigori"
112 " is disabled.";
113 return;
114 }
115
116 // We should never be called with an empty passphrase.
117 DCHECK(!passphrase.empty());
118
119 // SetEncryptionPassphrase should never be called if we are currently
120 // encrypted with an explicit passphrase.
121 DCHECK(cached_passphrase_type_ == PassphraseType::KEYSTORE_PASSPHRASE ||
122 cached_passphrase_type_ == PassphraseType::IMPLICIT_PASSPHRASE);
123
124 // Post an encryption task on the syncer thread.
125 sync_task_runner_->PostTask( 109 sync_task_runner_->PostTask(
126 FROM_HERE, base::Bind(&SyncBackendHostCore::DoSetEncryptionPassphrase, 110 FROM_HERE, base::Bind(&SyncBackendHostCore::DoSetEncryptionPassphrase,
127 core_, passphrase, is_explicit)); 111 core_, passphrase, is_explicit));
128 } 112 }
129 113
130 bool SyncBackendHostImpl::SetDecryptionPassphrase( 114 void SyncBackendHostImpl::SetDecryptionPassphrase(
131 const std::string& passphrase) { 115 const std::string& passphrase) {
132 DCHECK(thread_checker_.CalledOnValidThread()); 116 DCHECK(thread_checker_.CalledOnValidThread());
133
134 if (!IsNigoriEnabled()) {
135 NOTREACHED() << "SetDecryptionPassphrase must never be called when nigori"
136 " is disabled.";
137 return false;
138 }
139
140 // We should never be called with an empty passphrase.
141 DCHECK(!passphrase.empty());
142
143 // This should only be called when we have cached pending keys.
144 DCHECK(cached_pending_keys_.has_blob());
145
146 // Check the passphrase that was provided against our local cache of the
147 // cryptographer's pending keys. If this was unsuccessful, the UI layer can
148 // immediately call OnPassphraseRequired without showing the user a spinner.
149 if (!CheckPassphraseAgainstCachedPendingKeys(passphrase))
150 return false;
151
152 // Post a decryption task on the syncer thread.
153 sync_task_runner_->PostTask( 117 sync_task_runner_->PostTask(
154 FROM_HERE, base::Bind(&SyncBackendHostCore::DoSetDecryptionPassphrase, 118 FROM_HERE, base::Bind(&SyncBackendHostCore::DoSetDecryptionPassphrase,
155 core_, passphrase)); 119 core_, passphrase));
156
157 // Since we were able to decrypt the cached pending keys with the passphrase
158 // provided, we immediately alert the UI layer that the passphrase was
159 // accepted. This will avoid the situation where a user enters a passphrase,
160 // clicks OK, immediately reopens the advanced settings dialog, and gets an
161 // unnecessary prompt for a passphrase.
162 // Note: It is not guaranteed that the passphrase will be accepted by the
163 // syncer thread, since we could receive a new nigori node while the task is
164 // pending. This scenario is a valid race, and SetDecryptionPassphrase can
165 // trigger a new OnPassphraseRequired if it needs to.
166 NotifyPassphraseAccepted();
167 return true;
168 } 120 }
169 121
170 void SyncBackendHostImpl::StopSyncingForShutdown() { 122 void SyncBackendHostImpl::StopSyncingForShutdown() {
171 DCHECK(thread_checker_.CalledOnValidThread()); 123 DCHECK(thread_checker_.CalledOnValidThread());
172 124
173 // Stop getting messages from the sync thread. 125 // Stop getting messages from the sync thread.
174 weak_ptr_factory_.InvalidateWeakPtrs(); 126 weak_ptr_factory_.InvalidateWeakPtrs();
175 // Immediately stop sending messages to the host. 127 // Immediately stop sending messages to the host.
176 host_ = nullptr; 128 host_ = nullptr;
177 129
(...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after
260 SyncBackendHostImpl::Status SyncBackendHostImpl::GetDetailedStatus() { 212 SyncBackendHostImpl::Status SyncBackendHostImpl::GetDetailedStatus() {
261 DCHECK(initialized()); 213 DCHECK(initialized());
262 return core_->sync_manager()->GetDetailedStatus(); 214 return core_->sync_manager()->GetDetailedStatus();
263 } 215 }
264 216
265 bool SyncBackendHostImpl::HasUnsyncedItems() const { 217 bool SyncBackendHostImpl::HasUnsyncedItems() const {
266 DCHECK(initialized()); 218 DCHECK(initialized());
267 return core_->sync_manager()->HasUnsyncedItems(); 219 return core_->sync_manager()->HasUnsyncedItems();
268 } 220 }
269 221
270 bool SyncBackendHostImpl::IsNigoriEnabled() const {
271 return registrar_ && registrar_->IsNigoriEnabled();
272 }
273
274 PassphraseType SyncBackendHostImpl::GetPassphraseType() const {
275 return cached_passphrase_type_;
276 }
277
278 base::Time SyncBackendHostImpl::GetExplicitPassphraseTime() const {
279 return cached_explicit_passphrase_time_;
280 }
281
282 bool SyncBackendHostImpl::IsCryptographerReady( 222 bool SyncBackendHostImpl::IsCryptographerReady(
283 const BaseTransaction* trans) const { 223 const BaseTransaction* trans) const {
284 return initialized() && trans->GetCryptographer() && 224 return initialized() && trans->GetCryptographer() &&
285 trans->GetCryptographer()->is_ready(); 225 trans->GetCryptographer()->is_ready();
286 } 226 }
287 227
288 void SyncBackendHostImpl::GetModelSafeRoutingInfo( 228 void SyncBackendHostImpl::GetModelSafeRoutingInfo(
289 ModelSafeRoutingInfo* out) const { 229 ModelSafeRoutingInfo* out) const {
290 if (initialized()) { 230 if (initialized()) {
291 CHECK(registrar_); 231 CHECK(registrar_);
(...skipping 107 matching lines...) Expand 10 before | Expand all | Expand 10 after
399 host_->OnSyncCycleCompleted(snapshot); 339 host_->OnSyncCycleCompleted(snapshot);
400 } 340 }
401 } 341 }
402 342
403 void SyncBackendHostImpl::RetryConfigurationOnFrontendLoop( 343 void SyncBackendHostImpl::RetryConfigurationOnFrontendLoop(
404 const base::Closure& retry_callback) { 344 const base::Closure& retry_callback) {
405 SDVLOG(1) << "Failed to complete configuration, informing of retry."; 345 SDVLOG(1) << "Failed to complete configuration, informing of retry.";
406 retry_callback.Run(); 346 retry_callback.Run();
407 } 347 }
408 348
409 void SyncBackendHostImpl::PersistEncryptionBootstrapToken(
410 const std::string& token,
411 BootstrapTokenType token_type) {
412 CHECK(sync_prefs_.get());
413 if (token_type == PASSPHRASE_BOOTSTRAP_TOKEN)
414 sync_prefs_->SetEncryptionBootstrapToken(token);
415 else
416 sync_prefs_->SetKeystoreEncryptionBootstrapToken(token);
417 }
418
419 void SyncBackendHostImpl::HandleActionableErrorEventOnFrontendLoop( 349 void SyncBackendHostImpl::HandleActionableErrorEventOnFrontendLoop(
420 const SyncProtocolError& sync_error) { 350 const SyncProtocolError& sync_error) {
421 DCHECK(thread_checker_.CalledOnValidThread()); 351 DCHECK(thread_checker_.CalledOnValidThread());
422 host_->OnActionableError(sync_error); 352 host_->OnActionableError(sync_error);
423 } 353 }
424 354
425 void SyncBackendHostImpl::HandleMigrationRequestedOnFrontendLoop( 355 void SyncBackendHostImpl::HandleMigrationRequestedOnFrontendLoop(
426 ModelTypeSet types) { 356 ModelTypeSet types) {
427 DCHECK(thread_checker_.CalledOnValidThread()); 357 DCHECK(thread_checker_.CalledOnValidThread());
428 host_->OnMigrationNeededForTypes(types); 358 host_->OnMigrationNeededForTypes(types);
429 } 359 }
430 360
431 void SyncBackendHostImpl::OnInvalidatorStateChange(InvalidatorState state) { 361 void SyncBackendHostImpl::OnInvalidatorStateChange(InvalidatorState state) {
432 sync_task_runner_->PostTask( 362 sync_task_runner_->PostTask(
433 FROM_HERE, base::Bind(&SyncBackendHostCore::DoOnInvalidatorStateChange, 363 FROM_HERE, base::Bind(&SyncBackendHostCore::DoOnInvalidatorStateChange,
434 core_, state)); 364 core_, state));
435 } 365 }
436 366
437 void SyncBackendHostImpl::OnIncomingInvalidation( 367 void SyncBackendHostImpl::OnIncomingInvalidation(
438 const ObjectIdInvalidationMap& invalidation_map) { 368 const ObjectIdInvalidationMap& invalidation_map) {
439 sync_task_runner_->PostTask( 369 sync_task_runner_->PostTask(
440 FROM_HERE, base::Bind(&SyncBackendHostCore::DoOnIncomingInvalidation, 370 FROM_HERE, base::Bind(&SyncBackendHostCore::DoOnIncomingInvalidation,
441 core_, invalidation_map)); 371 core_, invalidation_map));
442 } 372 }
443 373
444 std::string SyncBackendHostImpl::GetOwnerName() const { 374 std::string SyncBackendHostImpl::GetOwnerName() const {
445 return "SyncBackendHostImpl"; 375 return "SyncBackendHostImpl";
446 } 376 }
447 377
448 bool SyncBackendHostImpl::CheckPassphraseAgainstCachedPendingKeys(
449 const std::string& passphrase) const {
450 DCHECK(cached_pending_keys_.has_blob());
451 DCHECK(!passphrase.empty());
452 Nigori nigori;
453 nigori.InitByDerivation("localhost", "dummy", passphrase);
454 std::string plaintext;
455 bool result = nigori.Decrypt(cached_pending_keys_.blob(), &plaintext);
456 DVLOG_IF(1, result) << "Passphrase failed to decrypt pending keys.";
457 return result;
458 }
459
460 void SyncBackendHostImpl::NotifyPassphraseRequired(
461 PassphraseRequiredReason reason,
462 sync_pb::EncryptedData pending_keys) {
463 DCHECK(thread_checker_.CalledOnValidThread());
464
465 // Update our cache of the cryptographer's pending keys.
466 cached_pending_keys_ = pending_keys;
467
468 host_->OnPassphraseRequired(reason, pending_keys);
469 }
470
471 void SyncBackendHostImpl::NotifyPassphraseAccepted() {
472 DCHECK(thread_checker_.CalledOnValidThread());
473 // Clear our cache of the cryptographer's pending keys.
474 cached_pending_keys_.clear_blob();
475 host_->OnPassphraseAccepted();
476 }
477
478 void SyncBackendHostImpl::NotifyEncryptedTypesChanged(
479 ModelTypeSet encrypted_types,
480 bool encrypt_everything) {
481 DCHECK(thread_checker_.CalledOnValidThread());
482 host_->OnEncryptedTypesChanged(encrypted_types, encrypt_everything);
483 }
484
485 void SyncBackendHostImpl::NotifyEncryptionComplete() {
486 DCHECK(thread_checker_.CalledOnValidThread());
487 host_->OnEncryptionComplete();
488 }
489
490 void SyncBackendHostImpl::HandlePassphraseTypeChangedOnFrontendLoop(
491 PassphraseType type,
492 base::Time explicit_passphrase_time) {
493 DCHECK(thread_checker_.CalledOnValidThread());
494 DVLOG(1) << "Passphrase type changed to " << PassphraseTypeToString(type);
495 cached_passphrase_type_ = type;
496 cached_explicit_passphrase_time_ = explicit_passphrase_time;
497 }
498
499 void SyncBackendHostImpl::HandleLocalSetPassphraseEncryptionOnFrontendLoop(
500 const SyncEncryptionHandler::NigoriState& nigori_state) {
501 DCHECK(thread_checker_.CalledOnValidThread());
502 host_->OnLocalSetPassphraseEncryption(nigori_state);
503 }
504
505 void SyncBackendHostImpl::HandleConnectionStatusChangeOnFrontendLoop( 378 void SyncBackendHostImpl::HandleConnectionStatusChangeOnFrontendLoop(
506 ConnectionStatus status) { 379 ConnectionStatus status) {
507 DCHECK(thread_checker_.CalledOnValidThread()); 380 DCHECK(thread_checker_.CalledOnValidThread());
508 DVLOG(1) << "Connection status changed: " << ConnectionStatusToString(status); 381 DVLOG(1) << "Connection status changed: " << ConnectionStatusToString(status);
509 host_->OnConnectionStatusChange(status); 382 host_->OnConnectionStatusChange(status);
510 } 383 }
511 384
512 void SyncBackendHostImpl::HandleProtocolEventOnFrontendLoop( 385 void SyncBackendHostImpl::HandleProtocolEventOnFrontendLoop(
513 std::unique_ptr<ProtocolEvent> event) { 386 std::unique_ptr<ProtocolEvent> event) {
514 DCHECK(thread_checker_.CalledOnValidThread()); 387 DCHECK(thread_checker_.CalledOnValidThread());
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after
569 const SyncManager::ClearServerDataCallback& frontend_callback) { 442 const SyncManager::ClearServerDataCallback& frontend_callback) {
570 DCHECK(thread_checker_.CalledOnValidThread()); 443 DCHECK(thread_checker_.CalledOnValidThread());
571 frontend_callback.Run(); 444 frontend_callback.Run();
572 } 445 }
573 446
574 } // namespace syncer 447 } // namespace syncer
575 448
576 #undef SDVLOG 449 #undef SDVLOG
577 450
578 #undef SLOG 451 #undef SLOG
OLDNEW
« no previous file with comments | « components/sync/driver/glue/sync_backend_host_impl.h ('k') | components/sync/driver/glue/sync_backend_host_impl_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698