| OLD | NEW |
| 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/glue/session_change_processor.h" | 5 #include "chrome/browser/sync/glue/session_change_processor.h" |
| 6 | 6 |
| 7 #include <sstream> | 7 #include <sstream> |
| 8 #include <string> | 8 #include <string> |
| 9 #include <vector> |
| 9 | 10 |
| 10 #include "base/logging.h" | 11 #include "base/logging.h" |
| 12 #include "base/scoped_vector.h" |
| 11 #include "chrome/browser/browser_thread.h" | 13 #include "chrome/browser/browser_thread.h" |
| 12 #include "chrome/browser/sync/engine/syncapi.h" | 14 #include "chrome/browser/sync/engine/syncapi.h" |
| 13 #include "chrome/browser/sync/glue/session_model_associator.h" | 15 #include "chrome/browser/sync/glue/session_model_associator.h" |
| 14 #include "chrome/common/notification_details.h" | 16 #include "chrome/common/notification_details.h" |
| 17 #include "chrome/common/notification_service.h" |
| 15 #include "chrome/common/notification_source.h" | 18 #include "chrome/common/notification_source.h" |
| 16 | 19 |
| 17 namespace browser_sync { | 20 namespace browser_sync { |
| 18 | 21 |
| 19 SessionChangeProcessor::SessionChangeProcessor( | 22 SessionChangeProcessor::SessionChangeProcessor( |
| 20 UnrecoverableErrorHandler* error_handler, | 23 UnrecoverableErrorHandler* error_handler, |
| 21 SessionModelAssociator* session_model_associator) | 24 SessionModelAssociator* session_model_associator) |
| 22 : ChangeProcessor(error_handler), | 25 : ChangeProcessor(error_handler), |
| 23 session_model_associator_(session_model_associator), | 26 session_model_associator_(session_model_associator), |
| 24 profile_(NULL) { | 27 profile_(NULL) { |
| 25 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 28 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 26 DCHECK(error_handler); | 29 DCHECK(error_handler); |
| 27 DCHECK(session_model_associator_); | 30 DCHECK(session_model_associator_); |
| 28 } | 31 } |
| 29 | 32 |
| 30 SessionChangeProcessor::~SessionChangeProcessor() { | 33 SessionChangeProcessor::~SessionChangeProcessor() { |
| 31 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 34 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 32 } | 35 } |
| 33 | 36 |
| 34 void SessionChangeProcessor::Observe(NotificationType type, | 37 void SessionChangeProcessor::Observe(NotificationType type, |
| 35 const NotificationSource& source, | 38 const NotificationSource& source, |
| 36 const NotificationDetails& details) { | 39 const NotificationDetails& details) { |
| 37 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 40 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 38 DCHECK(running()); | 41 DCHECK(running()); |
| 39 DCHECK(profile_); | 42 DCHECK(profile_); |
| 40 switch (type.value) { | 43 switch (type.value) { |
| 41 case NotificationType::SESSION_SERVICE_SAVED: { | 44 case NotificationType::SESSION_SERVICE_SAVED: { |
| 42 std::string tag = session_model_associator_->GetCurrentMachineTag(); | 45 std::string tag = session_model_associator_->GetCurrentMachineTag(); |
| 43 DCHECK_EQ(Source<Profile>(source).ptr(), profile_); | 46 DCHECK_EQ(Source<Profile>(source).ptr(), profile_); |
| 44 LOG(INFO) << "Got change notification of type " << type.value | |
| 45 << " for session " << tag; | |
| 46 session_model_associator_->UpdateSyncModelDataFromClient(); | 47 session_model_associator_->UpdateSyncModelDataFromClient(); |
| 47 break; | 48 break; |
| 48 } | 49 } |
| 49 default: | 50 default: |
| 50 LOG(DFATAL) << "Received unexpected notification of type " | 51 LOG(DFATAL) << "Received unexpected notification of type " |
| 51 << type.value; | 52 << type.value; |
| 52 break; | 53 break; |
| 53 } | 54 } |
| 54 } | 55 } |
| 55 | 56 |
| 56 void SessionChangeProcessor::ApplyChangesFromSyncModel( | 57 void SessionChangeProcessor::ApplyChangesFromSyncModel( |
| 57 const sync_api::BaseTransaction* trans, | 58 const sync_api::BaseTransaction* trans, |
| 58 const sync_api::SyncManager::ChangeRecord* changes, | 59 const sync_api::SyncManager::ChangeRecord* changes, |
| 59 int change_count) { | 60 int change_count) { |
| 60 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 61 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 61 if (!running()) { | 62 if (!running()) { |
| 62 return; | 63 return; |
| 63 } | 64 } |
| 64 for (int i = 0; i < change_count; ++i) { | |
| 65 const sync_api::SyncManager::ChangeRecord& change = changes[i]; | |
| 66 switch (change.action) { | |
| 67 case sync_api::SyncManager::ChangeRecord::ACTION_ADD: | |
| 68 UpdateModel(trans, change, true); | |
| 69 break; | |
| 70 case sync_api::SyncManager::ChangeRecord::ACTION_UPDATE: | |
| 71 UpdateModel(trans, change, true); | |
| 72 break; | |
| 73 case sync_api::SyncManager::ChangeRecord::ACTION_DELETE: | |
| 74 UpdateModel(trans, change, false); | |
| 75 break; | |
| 76 } | |
| 77 } | |
| 78 } | |
| 79 | 65 |
| 80 void SessionChangeProcessor::UpdateModel(const sync_api::BaseTransaction* trans, | |
| 81 const sync_api::SyncManager::ChangeRecord& change, bool associate) { | |
| 82 sync_api::ReadNode node(trans); | |
| 83 if (!node.InitByIdLookup(change.id)) { | |
| 84 std::stringstream error; | |
| 85 error << "Session node lookup failed for change " << change.id | |
| 86 << " of action type " << change.action; | |
| 87 error_handler()->OnUnrecoverableError(FROM_HERE, error.str()); | |
| 88 return; | |
| 89 } | |
| 90 DCHECK_EQ(node.GetModelType(), syncable::SESSIONS); | |
| 91 StopObserving(); | 66 StopObserving(); |
| 92 if (associate) { | 67 |
| 93 session_model_associator_->Associate( | 68 // This currently ignores the tracked changes and rebuilds the sessions from |
| 94 (const sync_pb::SessionSpecifics *) NULL, node.GetId()); | 69 // all the session sync nodes. |
| 95 } else { | 70 // TODO(zea): Make use of |changes| to adjust only modified sessions. |
| 96 session_model_associator_->Disassociate(node.GetId()); | 71 session_model_associator_->UpdateFromSyncModel(trans); |
| 97 } | 72 |
| 73 // Notify foreign session handlers that there are new sessions. |
| 74 NotificationService::current()->Notify( |
| 75 NotificationType::FOREIGN_SESSION_UPDATED, |
| 76 NotificationService::AllSources(), |
| 77 NotificationService::NoDetails()); |
| 78 |
| 98 StartObserving(); | 79 StartObserving(); |
| 99 } | 80 } |
| 100 | 81 |
| 101 void SessionChangeProcessor::StartImpl(Profile* profile) { | 82 void SessionChangeProcessor::StartImpl(Profile* profile) { |
| 102 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 83 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 103 DCHECK(profile); | 84 DCHECK(profile); |
| 104 DCHECK(profile_ == NULL); | 85 DCHECK(profile_ == NULL); |
| 105 profile_ = profile; | 86 profile_ = profile; |
| 106 StartObserving(); | 87 StartObserving(); |
| 107 } | 88 } |
| 108 | 89 |
| 109 void SessionChangeProcessor::StopImpl() { | 90 void SessionChangeProcessor::StopImpl() { |
| 110 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 91 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 111 StopObserving(); | 92 StopObserving(); |
| 112 profile_ = NULL; | 93 profile_ = NULL; |
| 113 } | 94 } |
| 114 | 95 |
| 115 void SessionChangeProcessor::StartObserving() { | 96 void SessionChangeProcessor::StartObserving() { |
| 116 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 97 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 117 DCHECK(profile_); | 98 DCHECK(profile_); |
| 118 LOG(INFO) << "Observing SESSION_SERVICE_SAVED"; | |
| 119 notification_registrar_.Add( | 99 notification_registrar_.Add( |
| 120 this, NotificationType::SESSION_SERVICE_SAVED, | 100 this, NotificationType::SESSION_SERVICE_SAVED, |
| 121 Source<Profile>(profile_)); | 101 Source<Profile>(profile_)); |
| 122 } | 102 } |
| 123 | 103 |
| 124 void SessionChangeProcessor::StopObserving() { | 104 void SessionChangeProcessor::StopObserving() { |
| 125 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 105 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 126 DCHECK(profile_); | 106 DCHECK(profile_); |
| 127 LOG(INFO) << "removing observation of all notifications"; | |
| 128 notification_registrar_.RemoveAll(); | 107 notification_registrar_.RemoveAll(); |
| 129 } | 108 } |
| 130 | 109 |
| 131 } // namespace browser_sync | 110 } // namespace browser_sync |
| 132 | |
| OLD | NEW |