File: push_messaging_refresher.h

package info (click to toggle)
chromium 138.0.7204.157-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 6,071,864 kB
  • sloc: cpp: 34,936,859; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,953; asm: 946,768; xml: 739,967; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,806; php: 13,980; tcl: 13,166; yacc: 8,925; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (99 lines) | stat: -rw-r--r-- 3,829 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
// Copyright 2020 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef CHROME_BROWSER_PUSH_MESSAGING_PUSH_MESSAGING_REFRESHER_H_
#define CHROME_BROWSER_PUSH_MESSAGING_PUSH_MESSAGING_REFRESHER_H_

#include <map>
#include <optional>
#include <vector>

#include "base/memory/weak_ptr.h"
#include "base/observer_list.h"
#include "base/observer_list_types.h"
#include "chrome/browser/push_messaging/push_messaging_app_identifier.h"
#include "content/public/browser/push_messaging_service.h"
#include "third_party/blink/public/mojom/push_messaging/push_messaging.mojom-forward.h"

// This class enables push subscription refreshes as defined in the docs:
// https://w3c.github.io/push-api/#subscription-refreshes
// The idea is to keep the refresh information of both new and old subscription
// in memory during the refresh process to be still able to receive messages
// through the old subscription after it was replaced by the new subscription.
class PushMessagingRefresher {
 public:
  PushMessagingRefresher();

  PushMessagingRefresher(const PushMessagingRefresher&) = delete;
  PushMessagingRefresher& operator=(const PushMessagingRefresher&) = delete;

  ~PushMessagingRefresher();

  // Return number of objects that are currently being refreshed
  size_t GetCount() const;

  // Register a new refresh pair with relevant information.
  void Refresh(PushMessagingAppIdentifier old_app_identifier,
               const std::string& new_app_id,
               const std::string& sender_id);

  // The subscription with the new app id was updated, new messages arriving
  // through the new subscription should be accepted now.
  void OnSubscriptionUpdated(const std::string& new_app_id);

  // Unsubscribe event happened for the old subscription. It is deleted in
  // the RefreshMap and notify all observers that the refresh process for
  // |app_id| has finished
  void OnUnsubscribed(const std::string& app_id);

  // If a new message arrives through an |app_id| that is associated with a
  // refresh, the old subscription needs to be deactivated.
  void GotMessageFrom(const std::string& app_id);

  // If a subscription was refreshed, we accept the old subscription for
  // a moment after refresh
  std::optional<PushMessagingAppIdentifier> FindActiveAppIdentifier(
      const std::string& app_id);

  base::WeakPtr<PushMessagingRefresher> GetWeakPtr();

  // Observer for Refresh status updates
  class Observer : public base::CheckedObserver {
   public:
    virtual void OnOldSubscriptionExpired(const std::string& app_id,
                                          const std::string& sender_id) = 0;
    virtual void OnRefreshFinished(
        const PushMessagingAppIdentifier& app_identifier) = 0;
  };

  void AddObserver(Observer* observer);
  void RemoveObserver(Observer* observer);

 private:
  // A RefreshObject carries subscription information that is needed to receive
  // messages and to unsubscribe from the old subscription
  struct RefreshObject {
    PushMessagingAppIdentifier old_identifier;
    std::string sender_id;
    bool is_valid;
  };

  void NotifyOnOldSubscriptionExpired(const std::string& app_id,
                                      const std::string& sender_id);

  base::ObserverList<Observer> observers_;

  // Maps from new app id to the refresh information of the old subscription
  // that is needed to receive messages and unsubscribe
  using RefreshInfo = std::map<std::string, RefreshObject>;
  RefreshInfo old_subscriptions_;

  // Maps from old app id to new app id
  using RefreshMap = std::map<std::string, std::string>;
  RefreshMap refresh_map_;

  base::WeakPtrFactory<PushMessagingRefresher> weak_factory_{this};
};

#endif  // CHROME_BROWSER_PUSH_MESSAGING_PUSH_MESSAGING_REFRESHER_H_