File: push_messaging_refresher.cc

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 (123 lines) | stat: -rw-r--r-- 4,700 bytes parent folder | download | duplicates (3)
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
// 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.

#include "chrome/browser/push_messaging/push_messaging_refresher.h"

#include "base/feature_list.h"
#include "base/functional/bind.h"
#include "base/functional/callback_helpers.h"
#include "base/observer_list.h"
#include "base/task/single_thread_task_runner.h"
#include "chrome/browser/push_messaging/push_messaging_app_identifier.h"
#include "chrome/browser/push_messaging/push_messaging_constants.h"
#include "chrome/browser/push_messaging/push_messaging_service_impl.h"
#include "chrome/browser/push_messaging/push_messaging_utils.h"
#include "chrome/common/pref_names.h"
#include "content/public/browser/browser_context.h"
#include "content/public/browser/push_messaging_service.h"
#include "content/public/common/content_features.h"
#include "third_party/blink/public/mojom/push_messaging/push_messaging_status.mojom.h"
#include "url/gurl.h"

PushMessagingRefresher::PushMessagingRefresher() = default;

PushMessagingRefresher::~PushMessagingRefresher() = default;

size_t PushMessagingRefresher::GetCount() const {
  return old_subscriptions_.size();
}

void PushMessagingRefresher::Refresh(
    PushMessagingAppIdentifier old_app_identifier,
    const std::string& new_app_id,
    const std::string& sender_id) {
  RefreshObject refresh_object = {old_app_identifier, sender_id,
                                  false /* is_valid */};
  // Insert is as current started refresh
  old_subscriptions_.emplace(new_app_id, refresh_object);
  refresh_map_.emplace(old_app_identifier.app_id(), new_app_id);
  // TODO(viviy): Save old_subscription in a seperate map in preferences, so
  // that in case of a browser shutdown the subscription is remembered.
  // Unsubscribe on next startup.
}

void PushMessagingRefresher::OnSubscriptionUpdated(
    const std::string& new_app_id) {
  RefreshInfo::iterator result = old_subscriptions_.find(new_app_id);

  if (result == old_subscriptions_.end())
    return;

  // Schedule a unsubscription event for the old subscription
  base::SingleThreadTaskRunner::GetCurrentDefault()->PostDelayedTask(
      FROM_HERE,
      base::BindOnce(&PushMessagingRefresher::NotifyOnOldSubscriptionExpired,
                     weak_factory_.GetWeakPtr(),
                     result->second.old_identifier.app_id(),
                     result->second.sender_id),
      kPushSubscriptionRefreshTimeDelta);
}

void PushMessagingRefresher::NotifyOnOldSubscriptionExpired(
    const std::string& old_app_id,
    const std::string& sender_id) {
  for (Observer& obs : observers_)
    obs.OnOldSubscriptionExpired(old_app_id, sender_id);
}

void PushMessagingRefresher::OnUnsubscribed(const std::string& old_app_id) {
  auto found_new_app_id = refresh_map_.find(old_app_id);
  // Already unsubscribed
  if (found_new_app_id == refresh_map_.end())
    return;

  std::string new_app_id = found_new_app_id->second;
  refresh_map_.erase(found_new_app_id);

  RefreshInfo::iterator result = old_subscriptions_.find(new_app_id);
  CHECK(result != old_subscriptions_.end());

  PushMessagingAppIdentifier old_identifier = result->second.old_identifier;
  old_subscriptions_.erase(result);

  for (Observer& obs : observers_)
    obs.OnRefreshFinished(old_identifier);
}

void PushMessagingRefresher::GotMessageFrom(const std::string& app_id) {
  RefreshInfo::iterator result = old_subscriptions_.find(app_id);
  // If a message arrives that is part of the refresh, expire the old
  // subscription immediately
  if (result != old_subscriptions_.end() && !result->second.is_valid) {
    NotifyOnOldSubscriptionExpired(result->second.old_identifier.app_id(),
                                   result->second.sender_id);
    result->second.is_valid = true;
  }
}

std::optional<PushMessagingAppIdentifier>
PushMessagingRefresher::FindActiveAppIdentifier(const std::string& app_id) {
  std::optional<PushMessagingAppIdentifier> app_identifier;
  RefreshMap::iterator refresh_map_it = refresh_map_.find(app_id);
  if (refresh_map_it != refresh_map_.end()) {
    RefreshInfo::iterator result =
        old_subscriptions_.find(refresh_map_it->second);
    if (result != old_subscriptions_.end() && !result->second.is_valid) {
      app_identifier = result->second.old_identifier;
    }
  }
  return app_identifier;
}

base::WeakPtr<PushMessagingRefresher> PushMessagingRefresher::GetWeakPtr() {
  return weak_factory_.GetWeakPtr();
}

void PushMessagingRefresher::AddObserver(Observer* observer) {
  observers_.AddObserver(observer);
}

void PushMessagingRefresher::RemoveObserver(Observer* observer) {
  observers_.RemoveObserver(observer);
}