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#ifndef SYNC_SESSIONS_DIRECTORY_TYPE_DEBUG_INFO_EMITTER_H_
6#define SYNC_SESSIONS_DIRECTORY_TYPE_DEBUG_INFO_EMITTER_H_
7
8#include "base/macros.h"
9#include "base/memory/scoped_ptr.h"
10#include "base/observer_list.h"
11#include "base/values.h"
12#include "sync/base/sync_export.h"
13#include "sync/internal_api/public/sessions/commit_counters.h"
14#include "sync/internal_api/public/sessions/update_counters.h"
15#include "sync/syncable/directory.h"
16
17namespace syncer {
18
19class DirectoryCommitContributor;
20class TypeDebugInfoObserver;
21
22// Supports various kinds of debugging requests for a certain directory type.
23//
24// The GetAllNodes() function is used to help export a snapshot of all current
25// nodes to its caller.  The complexity required to manage the request mostly
26// lives elsewhere.
27//
28// The Emit*() functions send updates to registered TypeDebugInfoObservers.
29// The DirectoryTypeDebugInfoEmitter does not directly own that list; it is
30// managed by the ModelTypeRegistry.
31//
32// For Update and Commit counters, the job of keeping the counters up to date
33// is delegated to the UpdateHandler and CommitContributors.  For the Stats
34// counters, the emitter will use its type_ and directory_ members to fetch all
35// the required information on demand.
36class SYNC_EXPORT_PRIVATE DirectoryTypeDebugInfoEmitter {
37 public:
38  // Standard constructor for non-tests.
39  //
40  // The |directory| and |observers| arguments are not owned.  Both may be
41  // modified outside of this object and both are expected to outlive this
42  // object.
43  DirectoryTypeDebugInfoEmitter(
44      syncable::Directory* directory,
45      syncer::ModelType type,
46      ObserverList<TypeDebugInfoObserver>* observers);
47
48  // A simple constructor for tests.  Should not be used in real code.
49  DirectoryTypeDebugInfoEmitter(
50      ModelType type,
51      ObserverList<TypeDebugInfoObserver>* observers);
52
53  virtual ~DirectoryTypeDebugInfoEmitter();
54
55  // Returns a ListValue representation of all known nodes of this type.
56  scoped_ptr<base::ListValue> GetAllNodes();
57
58  // Returns a reference to the current commit counters.
59  const CommitCounters& GetCommitCounters() const;
60
61  // Allows others to mutate the commit counters.
62  CommitCounters* GetMutableCommitCounters();
63
64  // Triggerss a commit counters update to registered observers.
65  void EmitCommitCountersUpdate();
66
67  // Returns a reference to the current update counters.
68  const UpdateCounters& GetUpdateCounters() const;
69
70  // Allows others to mutate the update counters.
71  UpdateCounters* GetMutableUpdateCounters();
72
73  // Triggers an update counters update to registered observers.
74  void EmitUpdateCountersUpdate();
75
76  // Triggers a status counters update to registered observers.
77  void EmitStatusCountersUpdate();
78
79 private:
80  syncable::Directory* directory_;
81
82  const ModelType type_;
83
84  CommitCounters commit_counters_;
85  UpdateCounters update_counters_;
86
87  // Because there are so many emitters that come into and out of existence, it
88  // doesn't make sense to have them manage their own observer list.  They all
89  // share one observer list that is provided by their owner and which is
90  // guaranteed to outlive them.
91  ObserverList<TypeDebugInfoObserver>* type_debug_info_observers_;
92
93  DISALLOW_COPY_AND_ASSIGN(DirectoryTypeDebugInfoEmitter);
94};
95
96}  // namespace syncer
97
98#endif  // SYNC_SESSIONS_DIRECTORY_TYPE_DEBUG_INFO_EMITTER_H_
99