drive_notification_manager.h 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. // 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. #ifndef COMPONENTS_DRIVE_DRIVE_NOTIFICATION_MANAGER_H_
  5. #define COMPONENTS_DRIVE_DRIVE_NOTIFICATION_MANAGER_H_
  6. #include <stdint.h>
  7. #include <map>
  8. #include <memory>
  9. #include <set>
  10. #include <string>
  11. #include "base/memory/raw_ptr.h"
  12. #include "base/memory/weak_ptr.h"
  13. #include "base/observer_list.h"
  14. #include "base/sequence_checker.h"
  15. #include "base/strings/string_piece_forward.h"
  16. #include "base/time/default_tick_clock.h"
  17. #include "base/timer/timer.h"
  18. #include "components/drive/drive_notification_observer.h"
  19. #include "components/invalidation/public/invalidation_handler.h"
  20. #include "components/invalidation/public/invalidation_util.h"
  21. #include "components/keyed_service/core/keyed_service.h"
  22. namespace invalidation {
  23. class InvalidationService;
  24. } // namespace invalidation
  25. namespace drive {
  26. // Informs observers when they should check Google Drive for updates.
  27. // Conditions under which updates should be searched:
  28. // 1. XMPP invalidation is received from Google Drive.
  29. // 2. Polling timer counts down.
  30. class DriveNotificationManager : public KeyedService,
  31. public invalidation::InvalidationHandler {
  32. public:
  33. // |clock| can be injected for testing.
  34. explicit DriveNotificationManager(
  35. invalidation::InvalidationService* invalidation_service,
  36. const base::TickClock* clock = base::DefaultTickClock::GetInstance());
  37. DriveNotificationManager(const DriveNotificationManager&) = delete;
  38. DriveNotificationManager& operator=(const DriveNotificationManager&) = delete;
  39. ~DriveNotificationManager() override;
  40. // KeyedService override.
  41. void Shutdown() override;
  42. // invalidation::InvalidationHandler implementation.
  43. void OnInvalidatorStateChange(invalidation::InvalidatorState state) override;
  44. void OnIncomingInvalidation(
  45. const invalidation::TopicInvalidationMap& invalidation_map) override;
  46. std::string GetOwnerName() const override;
  47. bool IsPublicTopic(const invalidation::Topic& topic) const override;
  48. void AddObserver(DriveNotificationObserver* observer);
  49. void RemoveObserver(DriveNotificationObserver* observer);
  50. // There has been a change in the users team drives, and as a result we need
  51. // to update which objects we receive invalidations for.
  52. void UpdateTeamDriveIds(const std::set<std::string>& added_team_drive_ids,
  53. const std::set<std::string>& removed_team_drive_ids);
  54. // Unsubscribe from invalidations from all team drives.
  55. void ClearTeamDriveIds();
  56. // True when XMPP notification is currently enabled.
  57. bool push_notification_enabled() const {
  58. return push_notification_enabled_;
  59. }
  60. // True when XMPP notification has been registered.
  61. bool push_notification_registered() const {
  62. return push_notification_registered_;
  63. }
  64. const std::set<std::string>& team_drive_ids_for_test() const {
  65. return team_drive_ids_;
  66. }
  67. const base::ObserverList<DriveNotificationObserver>::Unchecked&
  68. observers_for_test() {
  69. return observers_;
  70. }
  71. private:
  72. enum NotificationSource {
  73. NOTIFICATION_XMPP,
  74. NOTIFICATION_POLLING,
  75. };
  76. // Restarts the polling timer. Used for polling-based notification.
  77. void RestartPollingTimer();
  78. // Restarts the batch notification timer. Used for batching together XMPP
  79. // notifications so we can smooth out the traffic on the drive backends.
  80. void RestartBatchTimer();
  81. // Notifies the observers that it's time to check for updates.
  82. // |source| indicates where the notification comes from.
  83. void NotifyObserversToUpdate(NotificationSource source,
  84. std::map<std::string, int64_t> invalidations);
  85. // Registers for Google Drive invalidation notifications through XMPP.
  86. void RegisterDriveNotifications();
  87. // Updates the list of notifications that we're expecting
  88. void UpdateRegisteredDriveNotifications();
  89. // Dispatches batched invalidations to observers.
  90. void OnBatchTimerExpired();
  91. // Returns a string representation of NotificationSource.
  92. static std::string NotificationSourceToString(NotificationSource source);
  93. invalidation::Topic GetDriveInvalidationTopic() const;
  94. invalidation::Topic GetTeamDriveInvalidationTopic(
  95. const std::string& team_drive_id) const;
  96. std::string ExtractTeamDriveId(base::StringPiece topic_name) const;
  97. raw_ptr<invalidation::InvalidationService> invalidation_service_;
  98. base::ObserverList<DriveNotificationObserver>::Unchecked observers_;
  99. // True when Drive File Sync Service is registered for Drive notifications.
  100. bool push_notification_registered_;
  101. // True if the XMPP-based push notification is currently enabled.
  102. bool push_notification_enabled_;
  103. // True once observers are notified for the first time.
  104. bool observers_notified_;
  105. // This is the set of team drive id's we're receiving notifications for.
  106. std::set<std::string> team_drive_ids_;
  107. // The timer is used for polling based notification. XMPP should usually be
  108. // used but notification is done per polling when XMPP is not working.
  109. base::OneShotTimer polling_timer_;
  110. // This timer is used to batch together invalidations. The invalidation
  111. // service can send many invalidations for the same id in rapid succession,
  112. // batching them together and removing duplicates is an optimzation.
  113. base::OneShotTimer batch_timer_;
  114. // The batch of invalidation id's that we've seen from the invaliation
  115. // service, will be reset when when send the invalidations to the observers.
  116. std::map<std::string, int64_t> invalidated_change_ids_;
  117. SEQUENCE_CHECKER(sequence_checker_);
  118. // Note: This should remain the last member so it'll be destroyed and
  119. // invalidate its weak pointers before any other members are destroyed.
  120. base::WeakPtrFactory<DriveNotificationManager> weak_ptr_factory_{this};
  121. };
  122. } // namespace drive
  123. #endif // COMPONENTS_DRIVE_DRIVE_NOTIFICATION_MANAGER_H_