summaryrefslogtreecommitdiffstats
path: root/chrome/browser/sync/notifier/registration_manager.cc
blob: ccadac4c553ef4fc325d5256b7669980b2e8b246 (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
// 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/notifier/registration_manager.h"

#include <string>

#include "chrome/browser/sync/notifier/invalidation_util.h"
#include "chrome/browser/sync/syncable/model_type.h"
#include "google/cacheinvalidation/invalidation-client.h"

namespace sync_notifier {

RegistrationManager::RegistrationManager(
    invalidation::InvalidationClient* invalidation_client)
    : invalidation_client_(invalidation_client) {
  DCHECK(invalidation_client_);
}

RegistrationManager::~RegistrationManager() {
  DCHECK(non_thread_safe_.CalledOnValidThread());
}

bool RegistrationManager::RegisterType(syncable::ModelType model_type) {
  DCHECK(non_thread_safe_.CalledOnValidThread());
  invalidation::ObjectId object_id;
  if (!RealModelTypeToObjectId(model_type, &object_id)) {
    LOG(ERROR) << "Invalid model type: " << model_type;
    return false;
  }
  RegistrationStatusMap::iterator it =
      registration_status_.insert(
          std::make_pair(model_type, UNREGISTERED)).first;
  if (it->second == UNREGISTERED) {
    RegisterObject(object_id, it);
  }
  return true;
}

bool RegistrationManager::IsRegistered(
    syncable::ModelType model_type) const {
  DCHECK(non_thread_safe_.CalledOnValidThread());
  RegistrationStatusMap::const_iterator it =
      registration_status_.find(model_type);
  if (it == registration_status_.end()) {
    return false;
  }
  return it->second == REGISTERED;
}

void RegistrationManager::MarkRegistrationLost(
    syncable::ModelType model_type) {
  DCHECK(non_thread_safe_.CalledOnValidThread());
  invalidation::ObjectId object_id;
  if (!RealModelTypeToObjectId(model_type, &object_id)) {
    LOG(ERROR) << "Invalid model type: " << model_type;
    return;
  }
  RegistrationStatusMap::iterator it =
      registration_status_.find(model_type);
  if (it == registration_status_.end()) {
    LOG(ERROR) << "Unknown model type: " << model_type;
    return;
  }
  it->second = UNREGISTERED;
  RegisterObject(object_id, it);
}

void RegistrationManager::MarkAllRegistrationsLost() {
  DCHECK(non_thread_safe_.CalledOnValidThread());
  for (RegistrationStatusMap::iterator it =
           registration_status_.begin();
       it != registration_status_.end(); ++it) {
    invalidation::ObjectId object_id;
    if (!RealModelTypeToObjectId(it->first, &object_id)) {
      LOG(DFATAL) << "Invalid model type: " << it->first;
      continue;
    }
    it->second = UNREGISTERED;
    RegisterObject(object_id, it);
  }
}

void RegistrationManager::RegisterObject(
    const invalidation::ObjectId& object_id,
    RegistrationStatusMap::iterator it) {
  DCHECK_EQ(it->second, UNREGISTERED);
  invalidation_client_->Register(
      object_id,
      invalidation::NewPermanentCallback(
          this, &RegistrationManager::OnRegister));
  it->second = PENDING;
}

void RegistrationManager::OnRegister(
    const invalidation::RegistrationUpdateResult& result) {
  DCHECK(non_thread_safe_.CalledOnValidThread());
  LOG(INFO) << "OnRegister: " << RegistrationUpdateResultToString(result);
  if (result.operation().type() !=
      invalidation::RegistrationUpdate::REGISTER) {
    LOG(ERROR) << "Got non-register result";
    return;
  }
  syncable::ModelType model_type;
  if (!ObjectIdToRealModelType(result.operation().object_id(),
                               &model_type)) {
    LOG(ERROR) << "Could not get model type";
    return;
  }
  RegistrationStatusMap::iterator it =
      registration_status_.find(model_type);
  if (it == registration_status_.end()) {
    LOG(ERROR) << "Unknown model type: " << model_type;
    return;
  }
  invalidation::Status::Code code = result.status().code();
  switch (code) {
    case invalidation::Status::SUCCESS:
      it->second = REGISTERED;
      break;
    case invalidation::Status::TRANSIENT_FAILURE:
      // TODO(akalin): Add retry logic.  For now, just fall through.
    default:
      // Treat everything else as a permanent failure.
      LOG(ERROR) << "Registration failed with code: " << code;
      break;
  }
}

}  // namespace sync_notifier