all_status.h revision 731df977c0511bca2206b5f333555b1205ff1f43
1// Copyright (c) 2006-2009 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// The AllStatus object watches various sync engine components and aggregates
6// the status of all of them into one place.
7
8#ifndef CHROME_BROWSER_SYNC_ENGINE_ALL_STATUS_H_
9#define CHROME_BROWSER_SYNC_ENGINE_ALL_STATUS_H_
10#pragma once
11
12#include <map>
13
14#include "base/lock.h"
15#include "base/scoped_ptr.h"
16#include "chrome/browser/sync/engine/syncer_types.h"
17
18namespace browser_sync {
19
20class ScopedStatusLock;
21class ServerConnectionManager;
22class Syncer;
23class SyncerThread;
24struct AuthWatcherEvent;
25struct ServerConnectionEvent;
26
27class AllStatus : public SyncEngineEventListener {
28  friend class ScopedStatusLock;
29 public:
30  // Status of the entire sync process distilled into a single enum.
31  enum SyncStatus {
32    // Can't connect to server, but there are no pending changes in
33    // our local dataase.
34    OFFLINE,
35    // Can't connect to server, and there are pending changes in our
36    // local cache.
37    OFFLINE_UNSYNCED,
38    // Connected and syncing.
39    SYNCING,
40    // Connected, no pending changes.
41    READY,
42    // Internal sync error.
43    CONFLICT,
44    // Can't connect to server, and we haven't completed the initial
45    // sync yet.  So there's nothing we can do but wait for the server.
46    OFFLINE_UNUSABLE,
47    // For array sizing, etc.
48    ICON_STATUS_COUNT
49  };
50
51  struct Status {
52    SyncStatus icon;
53    int unsynced_count;
54    int conflicting_count;
55    bool syncing;
56    bool authenticated;  // Successfully authenticated via gaia
57    // True if we have received at least one good reply from the server.
58    bool server_up;
59    bool server_reachable;
60    // True after a client has done a first sync.
61    bool initial_sync_ended;
62    // True if any syncer is stuck.
63    bool syncer_stuck;
64    // True if any syncer is stopped because of server issues.
65    bool server_broken;
66    // True only if the notification listener has subscribed.
67    bool notifications_enabled;
68    // Notifications counters updated by the actions in synapi.
69    int notifications_received;
70    int notifications_sent;
71    // The max number of consecutive errors from any component.
72    int max_consecutive_errors;
73    bool disk_full;
74
75    // Contains current transfer item meta handle
76    int64 current_item_meta_handle;
77    // The next two values will be equal if all updates have been received.
78    // total updates available.
79    int64 updates_available;
80    // total updates received.
81    int64 updates_received;
82  };
83
84  AllStatus();
85  ~AllStatus();
86
87  void HandleServerConnectionEvent(const ServerConnectionEvent& event);
88
89  void HandleAuthWatcherEvent(const AuthWatcherEvent& event);
90
91  virtual void OnSyncEngineEvent(const SyncEngineEvent& event);
92
93  // Returns a string description of the SyncStatus (currently just the ascii
94  // version of the enum). Will LOG(FATAL) if the status us out of range.
95  static const char* GetSyncStatusString(SyncStatus status);
96
97  Status status() const;
98
99  void SetNotificationsEnabled(bool notifications_enabled);
100
101  void IncrementNotificationsSent();
102
103  void IncrementNotificationsReceived();
104
105 protected:
106  // Examines syncer to calculate syncing and the unsynced count,
107  // and returns a Status with new values.
108  Status CalcSyncing(const SyncEngineEvent& event) const;
109  Status CreateBlankStatus() const;
110
111  // Examines status to see what has changed, updates old_status in place.
112  void CalcStatusChanges();
113
114  Status status_;
115
116  mutable Lock mutex_;  // Protects all data members.
117  DISALLOW_COPY_AND_ASSIGN(AllStatus);
118};
119
120class ScopedStatusLock {
121 public:
122  explicit ScopedStatusLock(AllStatus* allstatus);
123  ~ScopedStatusLock();
124 protected:
125  AllStatus* allstatus_;
126};
127
128}  // namespace browser_sync
129
130#endif  // CHROME_BROWSER_SYNC_ENGINE_ALL_STATUS_H_
131