summaryrefslogtreecommitdiffstats
path: root/chrome/browser/sync/glue/session_change_processor.cc
blob: 8ea64b183a6ca2633ca0a8b3f12ef43914156f1b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
// Copyright (c) 2010 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chrome/browser/sync/glue/session_change_processor.h"

#include <sstream>
#include <string>

#include "base/logging.h"
#include "chrome/browser/chrome_thread.h"
#include "chrome/browser/sync/engine/syncapi.h"
#include "chrome/browser/sync/glue/session_model_associator.h"
#include "chrome/common/notification_details.h"
#include "chrome/common/notification_source.h"

namespace browser_sync {

SessionChangeProcessor::SessionChangeProcessor(
    UnrecoverableErrorHandler* error_handler,
    SessionModelAssociator* session_model_associator)
    : ChangeProcessor(error_handler),
      session_model_associator_(session_model_associator),
      profile_(NULL) {
  DCHECK(ChromeThread::CurrentlyOn(ChromeThread::UI));
  DCHECK(error_handler);
  DCHECK(session_model_associator_);
}

SessionChangeProcessor::~SessionChangeProcessor() {
  DCHECK(ChromeThread::CurrentlyOn(ChromeThread::UI));
}

void SessionChangeProcessor::Observe(NotificationType type,
                                     const NotificationSource& source,
                                     const NotificationDetails& details) {
  DCHECK(ChromeThread::CurrentlyOn(ChromeThread::UI));
  DCHECK(running());
  DCHECK(profile_);
  switch (type.value) {
    case NotificationType::SESSION_SERVICE_SAVED: {
      std::string tag = session_model_associator_->GetCurrentMachineTag();
      DCHECK_EQ(Source<Profile>(source).ptr(), profile_);
      LOG(INFO) << "Got change notification of type " << type.value
                << " for session " << tag;
      session_model_associator_->UpdateSyncModelDataFromClient();
      break;
    }
    default:
      LOG(DFATAL) << "Received unexpected notification of type "
                  << type.value;
      break;
  }
}

void SessionChangeProcessor::ApplyChangesFromSyncModel(
    const sync_api::BaseTransaction* trans,
    const sync_api::SyncManager::ChangeRecord* changes,
    int change_count) {
  DCHECK(ChromeThread::CurrentlyOn(ChromeThread::UI));
  if (!running()) {
    return;
  }
  for (int i = 0; i < change_count; ++i) {
    const sync_api::SyncManager::ChangeRecord& change = changes[i];
    switch (change.action) {
      case sync_api::SyncManager::ChangeRecord::ACTION_ADD:
        UpdateModel(trans, change, true);
        break;
      case sync_api::SyncManager::ChangeRecord::ACTION_UPDATE:
        UpdateModel(trans, change, true);
        break;
      case sync_api::SyncManager::ChangeRecord::ACTION_DELETE:
        UpdateModel(trans, change, false);
        break;
    }
  }
}

void SessionChangeProcessor::UpdateModel(const sync_api::BaseTransaction* trans,
    const sync_api::SyncManager::ChangeRecord& change, bool associate) {
  sync_api::ReadNode node(trans);
  if (!node.InitByIdLookup(change.id)) {
    std::stringstream error;
    error << "Session node lookup failed for change " << change.id
      << " of action type " << change.action;
    error_handler()->OnUnrecoverableError(FROM_HERE, error.str());
    return;
  }
  DCHECK_EQ(node.GetModelType(), syncable::SESSIONS);
  StopObserving();
  if (associate) {
    session_model_associator_->Associate(
      (const sync_pb::SessionSpecifics *) NULL, node.GetId());
  } else {
    session_model_associator_->Disassociate(node.GetId());
  }
  StartObserving();
}

void SessionChangeProcessor::StartImpl(Profile* profile) {
  DCHECK(ChromeThread::CurrentlyOn(ChromeThread::UI));
  DCHECK(profile);
  DCHECK(profile_ == NULL);
  profile_ = profile;
  StartObserving();
}

void SessionChangeProcessor::StopImpl() {
  DCHECK(ChromeThread::CurrentlyOn(ChromeThread::UI));
  StopObserving();
  profile_ = NULL;
}

void SessionChangeProcessor::StartObserving() {
  DCHECK(ChromeThread::CurrentlyOn(ChromeThread::UI));
  DCHECK(profile_);
  LOG(INFO) << "Observing SESSION_SERVICE_SAVED";
  notification_registrar_.Add(
      this, NotificationType::SESSION_SERVICE_SAVED,
      Source<Profile>(profile_));
}

void SessionChangeProcessor::StopObserving() {
  DCHECK(ChromeThread::CurrentlyOn(ChromeThread::UI));
  DCHECK(profile_);
  LOG(INFO) << "removing observation of all notifications";
  notification_registrar_.RemoveAll();
}

}  // namespace browser_sync