1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "sync/sessions/directory_type_debug_info_emitter.h"
6
7 #include "sync/internal_api/public/sessions/status_counters.h"
8 #include "sync/internal_api/public/sessions/type_debug_info_observer.h"
9 #include "sync/syncable/entry.h"
10 #include "sync/syncable/syncable_read_transaction.h"
11
12 namespace syncer {
13
DirectoryTypeDebugInfoEmitter(syncable::Directory * directory,syncer::ModelType type,ObserverList<TypeDebugInfoObserver> * observers)14 DirectoryTypeDebugInfoEmitter::DirectoryTypeDebugInfoEmitter(
15 syncable::Directory* directory,
16 syncer::ModelType type,
17 ObserverList<TypeDebugInfoObserver>* observers)
18 : directory_(directory),
19 type_(type),
20 type_debug_info_observers_(observers) {}
21
DirectoryTypeDebugInfoEmitter(ModelType type,ObserverList<TypeDebugInfoObserver> * observers)22 DirectoryTypeDebugInfoEmitter::DirectoryTypeDebugInfoEmitter(
23 ModelType type,
24 ObserverList<TypeDebugInfoObserver>* observers)
25 : directory_(NULL),
26 type_(type),
27 type_debug_info_observers_(observers) {}
28
~DirectoryTypeDebugInfoEmitter()29 DirectoryTypeDebugInfoEmitter::~DirectoryTypeDebugInfoEmitter() {}
30
GetAllNodes()31 scoped_ptr<base::ListValue> DirectoryTypeDebugInfoEmitter::GetAllNodes() {
32 syncable::ReadTransaction trans(FROM_HERE, directory_);
33 scoped_ptr<base::ListValue> nodes(
34 directory_->GetNodeDetailsForType(&trans, type_));
35 return nodes.Pass();
36 }
37
GetCommitCounters() const38 const CommitCounters& DirectoryTypeDebugInfoEmitter::GetCommitCounters() const {
39 return commit_counters_;
40 }
41
GetMutableCommitCounters()42 CommitCounters* DirectoryTypeDebugInfoEmitter::GetMutableCommitCounters() {
43 return &commit_counters_;
44 }
45
EmitCommitCountersUpdate()46 void DirectoryTypeDebugInfoEmitter::EmitCommitCountersUpdate() {
47 FOR_EACH_OBSERVER(TypeDebugInfoObserver, (*type_debug_info_observers_),
48 OnCommitCountersUpdated(type_, commit_counters_));
49 }
50
GetUpdateCounters() const51 const UpdateCounters& DirectoryTypeDebugInfoEmitter::GetUpdateCounters() const {
52 return update_counters_;
53 }
54
GetMutableUpdateCounters()55 UpdateCounters* DirectoryTypeDebugInfoEmitter::GetMutableUpdateCounters() {
56 return &update_counters_;
57 }
58
EmitUpdateCountersUpdate()59 void DirectoryTypeDebugInfoEmitter::EmitUpdateCountersUpdate() {
60 FOR_EACH_OBSERVER(TypeDebugInfoObserver, (*type_debug_info_observers_),
61 OnUpdateCountersUpdated(type_, update_counters_));
62 }
63
EmitStatusCountersUpdate()64 void DirectoryTypeDebugInfoEmitter::EmitStatusCountersUpdate() {
65 // This is expensive. Avoid running this code unless about:sync is open.
66 if (!type_debug_info_observers_->might_have_observers())
67 return;
68
69 syncable::ReadTransaction trans(FROM_HERE, directory_);
70 std::vector<int64> result;
71 directory_->GetMetaHandlesOfType(&trans, type_, &result);
72
73 StatusCounters counters;
74 counters.num_entries_and_tombstones = result.size();
75
76 for (std::vector<int64>::const_iterator it = result.begin();
77 it != result.end(); ++it) {
78 syncable::Entry e(&trans, syncable::GET_BY_HANDLE, *it);
79 if (!e.GetIsDel()) {
80 counters.num_entries++;
81 }
82 }
83
84 FOR_EACH_OBSERVER(TypeDebugInfoObserver, (*type_debug_info_observers_),
85 OnStatusCountersUpdated(type_, counters));
86 }
87
88 } // namespace syncer
89