| 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/session_model_associator.h" | 5 #include "chrome/browser/sync/glue/session_model_associator.h" |
| 6 | 6 |
| 7 #include <algorithm> | 7 #include <algorithm> |
| 8 #include <utility> | 8 #include <utility> |
| 9 | 9 |
| 10 #include "base/logging.h" | 10 #include "base/logging.h" |
| 11 #include "base/tracked.h" | 11 #include "base/tracked.h" |
| 12 #include "chrome/browser/extensions/extension_tab_helper.h" | 12 #include "chrome/browser/extensions/extension_tab_helper.h" |
| 13 #include "chrome/browser/profiles/profile.h" | 13 #include "chrome/browser/profiles/profile.h" |
| 14 #include "chrome/browser/sessions/restore_tab_helper.h" | 14 #include "chrome/browser/sessions/restore_tab_helper.h" |
| 15 #include "chrome/browser/sessions/session_service_factory.h" | 15 #include "chrome/browser/sessions/session_service_factory.h" |
| 16 #include "chrome/browser/sync/api/sync_error.h" |
| 16 #include "chrome/browser/sync/profile_sync_service.h" | 17 #include "chrome/browser/sync/profile_sync_service.h" |
| 17 #include "chrome/browser/sync/syncable/syncable.h" | 18 #include "chrome/browser/sync/syncable/syncable.h" |
| 18 #include "chrome/browser/tabs/tab_strip_model.h" | 19 #include "chrome/browser/tabs/tab_strip_model.h" |
| 19 #include "chrome/browser/ui/browser_list.h" | 20 #include "chrome/browser/ui/browser_list.h" |
| 20 #include "chrome/browser/ui/browser_window.h" | 21 #include "chrome/browser/ui/browser_window.h" |
| 21 #include "chrome/browser/ui/tab_contents/tab_contents_wrapper.h" | 22 #include "chrome/browser/ui/tab_contents/tab_contents_wrapper.h" |
| 22 #include "chrome/common/chrome_notification_types.h" | 23 #include "chrome/common/chrome_notification_types.h" |
| 23 #include "chrome/common/extensions/extension.h" | 24 #include "chrome/common/extensions/extension.h" |
| 24 #include "chrome/common/url_constants.h" | 25 #include "chrome/common/url_constants.h" |
| 25 #include "content/browser/tab_contents/navigation_controller.h" | 26 #include "content/browser/tab_contents/navigation_controller.h" |
| (...skipping 372 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 398 sync_pb::TabNavigation_PageTransition_TYPED); | 399 sync_pb::TabNavigation_PageTransition_TYPED); |
| 399 } | 400 } |
| 400 } | 401 } |
| 401 | 402 |
| 402 void SessionModelAssociator::Disassociate(int64 sync_id) { | 403 void SessionModelAssociator::Disassociate(int64 sync_id) { |
| 403 DCHECK(CalledOnValidThread()); | 404 DCHECK(CalledOnValidThread()); |
| 404 NOTIMPLEMENTED(); | 405 NOTIMPLEMENTED(); |
| 405 // TODO(zea): we will need this once we support deleting foreign sessions. | 406 // TODO(zea): we will need this once we support deleting foreign sessions. |
| 406 } | 407 } |
| 407 | 408 |
| 408 bool SessionModelAssociator::AssociateModels() { | 409 bool SessionModelAssociator::AssociateModels(SyncError* error) { |
| 409 DCHECK(CalledOnValidThread()); | 410 DCHECK(CalledOnValidThread()); |
| 410 | 411 |
| 411 // Ensure that we disassociated properly, otherwise memory might leak. | 412 // Ensure that we disassociated properly, otherwise memory might leak. |
| 412 DCHECK(synced_session_tracker_.empty()); | 413 DCHECK(synced_session_tracker_.empty()); |
| 413 DCHECK_EQ(0U, tab_pool_.capacity()); | 414 DCHECK_EQ(0U, tab_pool_.capacity()); |
| 414 | 415 |
| 415 local_session_syncid_ = sync_api::kInvalidId; | 416 local_session_syncid_ = sync_api::kInvalidId; |
| 416 | 417 |
| 417 // Read any available foreign sessions and load any session data we may have. | 418 // Read any available foreign sessions and load any session data we may have. |
| 418 // If we don't have any local session data in the db, create a header node. | 419 // If we don't have any local session data in the db, create a header node. |
| 419 { | 420 { |
| 420 sync_api::WriteTransaction trans(FROM_HERE, sync_service_->GetUserShare()); | 421 sync_api::WriteTransaction trans(FROM_HERE, sync_service_->GetUserShare()); |
| 421 | 422 |
| 422 sync_api::ReadNode root(&trans); | 423 sync_api::ReadNode root(&trans); |
| 423 if (!root.InitByTagLookup(kSessionsTag)) { | 424 if (!root.InitByTagLookup(kSessionsTag)) { |
| 424 LOG(ERROR) << kNoSessionsFolderError; | 425 error->Reset(FROM_HERE, kNoSessionsFolderError, model_type()); |
| 425 return false; | 426 return false; |
| 426 } | 427 } |
| 427 | 428 |
| 428 // Make sure we have a machine tag. | 429 // Make sure we have a machine tag. |
| 429 if (current_machine_tag_.empty()) | 430 if (current_machine_tag_.empty()) |
| 430 InitializeCurrentMachineTag(&trans); | 431 InitializeCurrentMachineTag(&trans); |
| 431 synced_session_tracker_.SetLocalSessionTag(current_machine_tag_); | 432 synced_session_tracker_.SetLocalSessionTag(current_machine_tag_); |
| 432 UpdateAssociationsFromSyncModel(root, &trans); | 433 UpdateAssociationsFromSyncModel(root, &trans); |
| 433 | 434 |
| 434 if (local_session_syncid_ == sync_api::kInvalidId) { | 435 if (local_session_syncid_ == sync_api::kInvalidId) { |
| 435 // The sync db didn't have a header node for us, we need to create one. | 436 // The sync db didn't have a header node for us, we need to create one. |
| 436 sync_api::WriteNode write_node(&trans); | 437 sync_api::WriteNode write_node(&trans); |
| 437 if (!write_node.InitUniqueByCreation(syncable::SESSIONS, root, | 438 if (!write_node.InitUniqueByCreation(syncable::SESSIONS, root, |
| 438 current_machine_tag_)) { | 439 current_machine_tag_)) { |
| 439 LOG(ERROR) << "Failed to create sessions header sync node."; | 440 error->Reset(FROM_HERE, |
| 441 "Failed to create sessions header sync node.", |
| 442 model_type()); |
| 440 return false; | 443 return false; |
| 441 } | 444 } |
| 442 write_node.SetTitle(UTF8ToWide(current_machine_tag_)); | 445 write_node.SetTitle(UTF8ToWide(current_machine_tag_)); |
| 443 local_session_syncid_ = write_node.GetId(); | 446 local_session_syncid_ = write_node.GetId(); |
| 444 } | 447 } |
| 445 } | 448 } |
| 446 | 449 |
| 447 // Check if anything has changed on the client side. | 450 // Check if anything has changed on the client side. |
| 448 UpdateSyncModelDataFromClient(); | 451 UpdateSyncModelDataFromClient(); |
| 449 | 452 |
| 450 VLOG(1) << "Session models associated."; | 453 VLOG(1) << "Session models associated."; |
| 451 | 454 |
| 452 return true; | 455 return true; |
| 453 } | 456 } |
| 454 | 457 |
| 455 bool SessionModelAssociator::DisassociateModels() { | 458 bool SessionModelAssociator::DisassociateModels(SyncError* error) { |
| 456 DCHECK(CalledOnValidThread()); | 459 DCHECK(CalledOnValidThread()); |
| 457 synced_session_tracker_.clear(); | 460 synced_session_tracker_.clear(); |
| 458 tab_map_.clear(); | 461 tab_map_.clear(); |
| 459 tab_pool_.clear(); | 462 tab_pool_.clear(); |
| 460 local_session_syncid_ = sync_api::kInvalidId; | 463 local_session_syncid_ = sync_api::kInvalidId; |
| 461 | 464 |
| 462 // There is no local model stored with which to disassociate, just notify | 465 // There is no local model stored with which to disassociate, just notify |
| 463 // foreign session handlers. | 466 // foreign session handlers. |
| 464 NotificationService::current()->Notify( | 467 NotificationService::current()->Notify( |
| 465 chrome::NOTIFICATION_FOREIGN_SESSION_DISABLED, | 468 chrome::NOTIFICATION_FOREIGN_SESSION_DISABLED, |
| (...skipping 586 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1052 bool SessionModelAssociator::CryptoReadyIfNecessary() { | 1055 bool SessionModelAssociator::CryptoReadyIfNecessary() { |
| 1053 // We only access the cryptographer while holding a transaction. | 1056 // We only access the cryptographer while holding a transaction. |
| 1054 sync_api::ReadTransaction trans(FROM_HERE, sync_service_->GetUserShare()); | 1057 sync_api::ReadTransaction trans(FROM_HERE, sync_service_->GetUserShare()); |
| 1055 syncable::ModelTypeSet encrypted_types; | 1058 syncable::ModelTypeSet encrypted_types; |
| 1056 encrypted_types = sync_api::GetEncryptedTypes(&trans); | 1059 encrypted_types = sync_api::GetEncryptedTypes(&trans); |
| 1057 return encrypted_types.count(syncable::SESSIONS) == 0 || | 1060 return encrypted_types.count(syncable::SESSIONS) == 0 || |
| 1058 sync_service_->IsCryptographerReady(&trans); | 1061 sync_service_->IsCryptographerReady(&trans); |
| 1059 } | 1062 } |
| 1060 | 1063 |
| 1061 } // namespace browser_sync | 1064 } // namespace browser_sync |
| OLD | NEW |