1 // Copyright (c) 2012 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 COMPONENTS_SYNC_ENGINE_IMPL_ALL_STATUS_H_
6 #define COMPONENTS_SYNC_ENGINE_IMPL_ALL_STATUS_H_
7 
8 #include <map>
9 #include <string>
10 #include <vector>
11 
12 #include "base/compiler_specific.h"
13 #include "base/macros.h"
14 #include "base/sequence_checker.h"
15 #include "components/sync/base/model_type.h"
16 #include "components/sync/engine/sync_status.h"
17 #include "components/sync/engine_impl/sync_engine_event_listener.h"
18 
19 namespace syncer {
20 
21 class SyncStatusObserver;
22 struct SyncCycleEvent;
23 
24 // This class watches various sync engine components, updating its internal
25 // state upon change. It can send to observers a snapshot of this state as a
26 // SyncStatus object, aggregating all this data into one place.
27 //
28 // Most of this data ends up on the chrome://sync-internals page.  But the page
29 // is only 'pinged' to update itself at the end of a sync cycle.  A user could
30 // refresh manually, but unless their timing is excellent it's unlikely that a
31 // user will see any state in mid-sync cycle.  We have no plans to change this.
32 // However, we will continue to collect data and update state mid-sync-cycle in
33 // case we need to debug slow or stuck sync cycles.
34 class AllStatus : public SyncEngineEventListener {
35  public:
36   AllStatus();
37   ~AllStatus() override;
38 
39   // Adds an observer which will receive a call whenever this object changes.
40   // The |observer| gets notification after being added right away. |observer|
41   // must not be null and must outlive this object.
42   void AddObserver(SyncStatusObserver* observer);
43 
44   // SyncEngineEventListener implementation.
45   void OnSyncCycleEvent(const SyncCycleEvent& event) override;
46   void OnActionableError(const SyncProtocolError& error) override;
47   void OnRetryTimeChanged(base::Time retry_time) override;
48   void OnThrottledTypesChanged(ModelTypeSet throttled_types) override;
49   void OnBackedOffTypesChanged(ModelTypeSet backed_off_types) override;
50   void OnMigrationRequested(ModelTypeSet types) override;
51   void OnProtocolEvent(const ProtocolEvent& event) override;
52 
53   void SetNotificationsEnabled(bool notifications_enabled);
54 
55   void IncrementNotificationsReceived();
56 
57   void SetEncryptedTypes(ModelTypeSet types);
58   void SetCryptographerCanEncrypt(bool can_encrypt);
59   void SetCryptoHasPendingKeys(bool has_pending_keys);
60   void SetPassphraseType(PassphraseType type);
61   void SetHasKeystoreKey(bool has_keystore_key);
62   void SetKeystoreMigrationTime(const base::Time& migration_time);
63 
64   void SetSyncId(const std::string& sync_id);
65   void SetInvalidatorClientId(const std::string& invalidator_client_id);
66 
67   void SetLocalBackendFolder(const std::string& folder);
68 
69  protected:
70   // Examines syncer to calculate syncing and the unsynced count,
71   // and returns a Status with new values.
72   SyncStatus CalcSyncing(const SyncCycleEvent& event) const;
73   SyncStatus CreateBlankStatus() const;
74 
75   SyncStatus status_;
76 
77  private:
78   friend class ScopedStatusLock;
79 
80   // Notifies all observers about changing of status.
81   void NotifyStatusChanged();
82 
83   std::vector<SyncStatusObserver*> sync_status_observers_;
84 
85   SEQUENCE_CHECKER(sequence_checker_);
86 
87   DISALLOW_COPY_AND_ASSIGN(AllStatus);
88 };
89 
90 }  // namespace syncer
91 
92 #endif  // COMPONENTS_SYNC_ENGINE_IMPL_ALL_STATUS_H_
93