blob: 5c46984343fd05119f05bcf272556dadb0e7f838 [file] [log] [blame]
[email protected]bc7d1ba2013-04-11 05:54:191// Copyright 2013 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
lukasza8acc4eb2015-07-20 20:57:205#ifndef COMPONENTS_DRIVE_DRIVE_NOTIFICATION_MANAGER_H_
6#define COMPONENTS_DRIVE_DRIVE_NOTIFICATION_MANAGER_H_
[email protected]bc7d1ba2013-04-11 05:54:197
Sam McNallye4bd01c2018-10-17 23:51:118#include <stdint.h>
9
10#include <map>
Stuart Langley80845862018-09-05 00:47:2911#include <memory>
Stuart Langley664e2292018-08-31 04:58:2812#include <set>
13#include <string>
14
avibc5337b2015-12-25 23:16:3315#include "base/macros.h"
[email protected]983efa802013-05-02 23:39:0916#include "base/memory/weak_ptr.h"
[email protected]bc7d1ba2013-04-11 05:54:1917#include "base/observer_list.h"
Stuart Langley80845862018-09-05 00:47:2918#include "base/sequence_checker.h"
19#include "base/time/default_tick_clock.h"
[email protected]41a17c52013-06-28 00:27:5320#include "base/timer/timer.h"
lukasza8acc4eb2015-07-20 20:57:2021#include "components/drive/drive_notification_observer.h"
knn062cdbb2015-06-26 18:18:4222#include "components/invalidation/public/invalidation_handler.h"
Maksim Moskvitin1817bfa2020-03-03 11:01:1623#include "components/invalidation/public/invalidation_util.h"
[email protected]95003d522014-03-13 20:22:3124#include "components/keyed_service/core/keyed_service.h"
[email protected]bc7d1ba2013-04-11 05:54:1925
[email protected]a5ec28b2013-09-03 12:36:4826namespace invalidation {
27class InvalidationService;
maxbogue0a379452016-09-22 21:35:0528} // namespace invalidation
[email protected]a5ec28b2013-09-03 12:36:4829
[email protected]e50af7652013-06-20 06:39:3130namespace drive {
[email protected]bc7d1ba2013-04-11 05:54:1931
32// Informs observers when they should check Google Drive for updates.
33// Conditions under which updates should be searched:
34// 1. XMPP invalidation is received from Google Drive.
[email protected]bc7d1ba2013-04-11 05:54:1935// 2. Polling timer counts down.
[email protected]95003d522014-03-13 20:22:3136class DriveNotificationManager : public KeyedService,
37 public syncer::InvalidationHandler {
[email protected]bc7d1ba2013-04-11 05:54:1938 public:
Stuart Langley80845862018-09-05 00:47:2939 // |clock| can be injected for testing.
[email protected]a5ec28b2013-09-03 12:36:4840 explicit DriveNotificationManager(
Stuart Langley80845862018-09-05 00:47:2941 invalidation::InvalidationService* invalidation_service,
42 const base::TickClock* clock = base::DefaultTickClock::GetInstance());
dchenga8d07742014-10-21 11:27:1343 ~DriveNotificationManager() override;
[email protected]bc7d1ba2013-04-11 05:54:1944
[email protected]95003d522014-03-13 20:22:3145 // KeyedService override.
dchenga8d07742014-10-21 11:27:1346 void Shutdown() override;
[email protected]bc7d1ba2013-04-11 05:54:1947
[email protected]71a84dec2013-04-17 03:26:1148 // syncer::InvalidationHandler implementation.
dchenga8d07742014-10-21 11:27:1349 void OnInvalidatorStateChange(syncer::InvalidatorState state) override;
50 void OnIncomingInvalidation(
Maksim Moskvitin1817bfa2020-03-03 11:01:1651 const syncer::TopicInvalidationMap& invalidation_map) override;
dchenga8d07742014-10-21 11:27:1352 std::string GetOwnerName() const override;
Sam McNally1305d3b62019-05-17 21:43:1653 bool IsPublicTopic(const syncer::Topic& topic) const override;
[email protected]bc7d1ba2013-04-11 05:54:1954
55 void AddObserver(DriveNotificationObserver* observer);
56 void RemoveObserver(DriveNotificationObserver* observer);
57
Stuart Langley664e2292018-08-31 04:58:2858 // There has been a change in the users team drives, and as a result we need
59 // to update which objects we receive invalidations for.
60 void UpdateTeamDriveIds(const std::set<std::string>& added_team_drive_ids,
61 const std::set<std::string>& removed_team_drive_ids);
62
Austin Tankiang6f52aa02020-09-30 14:44:3663 // Unsubscribe from invalidations from all team drives.
64 void ClearTeamDriveIds();
65
[email protected]a9a0c762013-05-07 07:01:3066 // True when XMPP notification is currently enabled.
67 bool push_notification_enabled() const {
68 return push_notification_enabled_;
69 }
70
71 // True when XMPP notification has been registered.
72 bool push_notification_registered() const {
73 return push_notification_registered_;
74 }
[email protected]0fa48442013-04-26 05:16:2375
Sam McNally4f4e4292018-10-19 03:50:2976 const std::set<std::string>& team_drive_ids_for_test() const {
77 return team_drive_ids_;
78 }
79
Sam McNally6e8c5502018-10-19 04:05:5780 const base::ObserverList<DriveNotificationObserver>::Unchecked&
81 observers_for_test() {
82 return observers_;
83 }
84
[email protected]bc7d1ba2013-04-11 05:54:1985 private:
[email protected]983efa802013-05-02 23:39:0986 enum NotificationSource {
87 NOTIFICATION_XMPP,
88 NOTIFICATION_POLLING,
89 };
90
91 // Restarts the polling timer. Used for polling-based notification.
92 void RestartPollingTimer();
93
Stuart Langley6477fb062018-11-15 03:03:3694 // Restarts the batch notification timer. Used for batching together XMPP
95 // notifications so we can smooth out the traffic on the drive backends.
96 void RestartBatchTimer();
97
[email protected]983efa802013-05-02 23:39:0998 // Notifies the observers that it's time to check for updates.
99 // |source| indicates where the notification comes from.
Stuart Langley664e2292018-08-31 04:58:28100 void NotifyObserversToUpdate(NotificationSource source,
Sam McNallye4bd01c2018-10-17 23:51:11101 std::map<std::string, int64_t> invalidations);
[email protected]983efa802013-05-02 23:39:09102
103 // Registers for Google Drive invalidation notifications through XMPP.
[email protected]71a84dec2013-04-17 03:26:11104 void RegisterDriveNotifications();
[email protected]71a84dec2013-04-17 03:26:11105
Stuart Langley664e2292018-08-31 04:58:28106 // Updates the list of notifications that we're expecting
107 void UpdateRegisteredDriveNotifications();
108
Stuart Langley80845862018-09-05 00:47:29109 // Dispatches batched invalidations to observers.
110 void OnBatchTimerExpired();
111
[email protected]983efa802013-05-02 23:39:09112 // Returns a string representation of NotificationSource.
113 static std::string NotificationSourceToString(NotificationSource source);
114
Maksim Moskvitin1817bfa2020-03-03 11:01:16115 syncer::Topic GetDriveInvalidationTopic() const;
116 syncer::Topic GetTeamDriveInvalidationTopic(
Sam McNallya037ff92019-05-16 15:38:32117 const std::string& team_drive_id) const;
Maksim Moskvitin1817bfa2020-03-03 11:01:16118 std::string ExtractTeamDriveId(base::StringPiece topic_name) const;
Sam McNallya037ff92019-05-16 15:38:32119
[email protected]a5ec28b2013-09-03 12:36:48120 invalidation::InvalidationService* invalidation_service_;
Trent Apteda250ec3ab2018-08-19 08:52:19121 base::ObserverList<DriveNotificationObserver>::Unchecked observers_;
[email protected]bc7d1ba2013-04-11 05:54:19122
[email protected]71a84dec2013-04-17 03:26:11123 // True when Drive File Sync Service is registered for Drive notifications.
124 bool push_notification_registered_;
[email protected]35225612013-05-07 06:45:40125 // True if the XMPP-based push notification is currently enabled.
[email protected]71a84dec2013-04-17 03:26:11126 bool push_notification_enabled_;
[email protected]35225612013-05-07 06:45:40127 // True once observers are notified for the first time.
128 bool observers_notified_;
[email protected]71a84dec2013-04-17 03:26:11129
Stuart Langley664e2292018-08-31 04:58:28130 // This is the set of team drive id's we're receiving notifications for.
131 std::set<std::string> team_drive_ids_;
132
[email protected]983efa802013-05-02 23:39:09133 // The timer is used for polling based notification. XMPP should usually be
134 // used but notification is done per polling when XMPP is not working.
tzik32768d52018-07-10 01:08:32135 base::OneShotTimer polling_timer_;
[email protected]983efa802013-05-02 23:39:09136
Stuart Langley80845862018-09-05 00:47:29137 // This timer is used to batch together invalidations. The invalidation
138 // service can send many invalidations for the same id in rapid succession,
139 // batching them together and removing duplicates is an optimzation.
Stuart Langley6477fb062018-11-15 03:03:36140 base::OneShotTimer batch_timer_;
Stuart Langley80845862018-09-05 00:47:29141
142 // The batch of invalidation id's that we've seen from the invaliation
143 // service, will be reset when when send the invalidations to the observers.
Sam McNallye4bd01c2018-10-17 23:51:11144 std::map<std::string, int64_t> invalidated_change_ids_;
Stuart Langley80845862018-09-05 00:47:29145
146 SEQUENCE_CHECKER(sequence_checker_);
147
[email protected]983efa802013-05-02 23:39:09148 // Note: This should remain the last member so it'll be destroyed and
149 // invalidate its weak pointers before any other members are destroyed.
Jeremy Roman5c341f6d2019-07-15 15:56:10150 base::WeakPtrFactory<DriveNotificationManager> weak_ptr_factory_{this};
[email protected]983efa802013-05-02 23:39:09151
[email protected]bc7d1ba2013-04-11 05:54:19152 DISALLOW_COPY_AND_ASSIGN(DriveNotificationManager);
153};
154
[email protected]e50af7652013-06-20 06:39:31155} // namespace drive
[email protected]bc7d1ba2013-04-11 05:54:19156
lukasza8acc4eb2015-07-20 20:57:20157#endif // COMPONENTS_DRIVE_DRIVE_NOTIFICATION_MANAGER_H_