push_notification_manager.h 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. // Copyright 2021 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_OPTIMIZATION_GUIDE_CORE_PUSH_NOTIFICATION_MANAGER_H_
  5. #define COMPONENTS_OPTIMIZATION_GUIDE_CORE_PUSH_NOTIFICATION_MANAGER_H_
  6. #include <string>
  7. #include "base/callback.h"
  8. #include "base/containers/flat_set.h"
  9. #include "base/observer_list.h"
  10. #include "base/observer_list_types.h"
  11. #include "components/optimization_guide/proto/hints.pb.h"
  12. #include "components/optimization_guide/proto/push_notification.pb.h"
  13. namespace optimization_guide {
  14. // An interface for classes that implement the handling of push hint
  15. // notifications. The primary task of this interface is to remove any hints from
  16. // persistent storage that are invalidated by a more recent hint being pushed.
  17. class PushNotificationManager {
  18. public:
  19. // Receives actions from the PushNotificationManager to execute when a new
  20. // pushed hint is being processed. The Delegate should be implemented by the
  21. // owner of the PushNotificationManager.
  22. class Delegate {
  23. public:
  24. // Corresponds to a batch of hints being processed, typically from a cache.
  25. virtual void RemoveFetchedEntriesByHintKeys(
  26. base::OnceClosure on_success,
  27. proto::KeyRepresentation key_representation,
  28. const base::flat_set<std::string>& hint_keys) = 0;
  29. };
  30. // Observer interface to process HintNotificationPayload.payload. Subclasses
  31. // should parse the optimization_guide::proto::Any to a specific proto type.
  32. class Observer : public base::CheckedObserver {
  33. public:
  34. // Processes the HintNotificationPayload.payload.
  35. virtual void OnNotificationPayload(
  36. proto::OptimizationType optimization_type,
  37. const proto::Any& payload) = 0;
  38. };
  39. PushNotificationManager(const PushNotificationManager&) = delete;
  40. PushNotificationManager& operator=(const PushNotificationManager&) = delete;
  41. PushNotificationManager();
  42. virtual ~PushNotificationManager();
  43. // Sets |this|'s delegate.
  44. void SetDelegate(Delegate* delegate);
  45. // Informs |this| that the delegate is ready to process pushed hints.
  46. virtual void OnDelegateReady();
  47. // Called when a new push notification arrives.
  48. virtual void OnNewPushNotification(
  49. const proto::HintNotificationPayload& notification);
  50. // Adds an observer to handle payload in HintNotificationPayload.payload.
  51. virtual void AddObserver(Observer* observer);
  52. // Removes an observer that handles HintNotificationPayload.payload.
  53. virtual void RemoveObserver(Observer* observer);
  54. protected:
  55. // Owns |this|
  56. raw_ptr<PushNotificationManager::Delegate> delegate_ = nullptr;
  57. private:
  58. friend class PushNotificationManagerUnitTest;
  59. void DispatchPayload(const proto::HintNotificationPayload& notification);
  60. // Observers to handle the custom payload.
  61. base::ObserverList<PushNotificationManager::Observer> observers_;
  62. };
  63. } // namespace optimization_guide
  64. #endif // COMPONENTS_OPTIMIZATION_GUIDE_CORE_PUSH_NOTIFICATION_MANAGER_H_