summaryrefslogtreecommitdiffstats
path: root/sync/sessions/directory_type_debug_info_emitter.cc
blob: 88049589c11f79ea73c30d45dc6d1a9b2ca3b5d7 (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
// Copyright 2014 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 "sync/sessions/directory_type_debug_info_emitter.h"

#include <stdint.h>

#include <vector>

#include "sync/internal_api/public/sessions/status_counters.h"
#include "sync/internal_api/public/sessions/type_debug_info_observer.h"
#include "sync/syncable/entry.h"
#include "sync/syncable/syncable_read_transaction.h"

namespace syncer {

DirectoryTypeDebugInfoEmitter::DirectoryTypeDebugInfoEmitter(
    syncable::Directory* directory,
    syncer::ModelType type,
    base::ObserverList<TypeDebugInfoObserver>* observers)
    : directory_(directory),
      type_(type),
      type_debug_info_observers_(observers) {
}

DirectoryTypeDebugInfoEmitter::DirectoryTypeDebugInfoEmitter(
    ModelType type,
    base::ObserverList<TypeDebugInfoObserver>* observers)
    : directory_(NULL), type_(type), type_debug_info_observers_(observers) {
}

DirectoryTypeDebugInfoEmitter::~DirectoryTypeDebugInfoEmitter() {}

scoped_ptr<base::ListValue> DirectoryTypeDebugInfoEmitter::GetAllNodes() {
  syncable::ReadTransaction trans(FROM_HERE, directory_);
  scoped_ptr<base::ListValue> nodes(
      directory_->GetNodeDetailsForType(&trans, type_));
  return nodes;
}

const CommitCounters& DirectoryTypeDebugInfoEmitter::GetCommitCounters() const {
  return commit_counters_;
}

CommitCounters* DirectoryTypeDebugInfoEmitter::GetMutableCommitCounters() {
  return &commit_counters_;
}

void DirectoryTypeDebugInfoEmitter::EmitCommitCountersUpdate() {
  FOR_EACH_OBSERVER(TypeDebugInfoObserver, (*type_debug_info_observers_),
                    OnCommitCountersUpdated(type_, commit_counters_));
}

const UpdateCounters& DirectoryTypeDebugInfoEmitter::GetUpdateCounters() const {
  return update_counters_;
}

UpdateCounters* DirectoryTypeDebugInfoEmitter::GetMutableUpdateCounters() {
  return &update_counters_;
}

void DirectoryTypeDebugInfoEmitter::EmitUpdateCountersUpdate() {
  FOR_EACH_OBSERVER(TypeDebugInfoObserver, (*type_debug_info_observers_),
                    OnUpdateCountersUpdated(type_, update_counters_));
}

void DirectoryTypeDebugInfoEmitter::EmitStatusCountersUpdate() {
  // This is expensive.  Avoid running this code unless about:sync is open.
  if (!type_debug_info_observers_->might_have_observers())
    return;

  syncable::ReadTransaction trans(FROM_HERE, directory_);
  std::vector<int64_t> result;
  directory_->GetMetaHandlesOfType(&trans, type_, &result);

  StatusCounters counters;
  counters.num_entries_and_tombstones = result.size();

  for (std::vector<int64_t>::const_iterator it = result.begin();
       it != result.end(); ++it) {
    syncable::Entry e(&trans, syncable::GET_BY_HANDLE, *it);
    if (!e.GetIsDel()) {
      counters.num_entries++;
    }
  }

  FOR_EACH_OBSERVER(TypeDebugInfoObserver, (*type_debug_info_observers_),
                    OnStatusCountersUpdated(type_, counters));
}

}  // namespace syncer