summaryrefslogtreecommitdiff
path: root/chromium/content/browser/sms/sms_queue.cc
blob: cd1bdd8704ec6cd1d56cfc9880eff5f16bd3f77c (plain)
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
// Copyright 2019 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "content/browser/sms/sms_queue.h"

#include "base/callback.h"
#include "base/metrics/histogram_macros.h"
#include "base/optional.h"

namespace content {

SmsQueue::SmsQueue() = default;
SmsQueue::~SmsQueue() = default;

void SmsQueue::Push(const url::Origin& origin, Subscriber* subscriber) {
  subscribers_[origin].AddObserver(subscriber);
  // We expect that in most cases there should be only one pending origin and in
  // rare cases there may be a few more (<10).
  UMA_HISTOGRAM_EXACT_LINEAR("Blink.Sms.PendingOriginCount",
                             subscribers_.size(), 10);
}

SmsQueue::Subscriber* SmsQueue::Pop(const url::Origin& origin) {
  auto it = subscribers_.find(origin);
  if (it == subscribers_.end())
    return nullptr;
  base::ObserverList<Subscriber>& subscribers = it->second;

  Subscriber& subscriber = *(subscribers.begin());
  Remove(origin, &subscriber);
  return &subscriber;
}

void SmsQueue::Remove(const url::Origin& origin, Subscriber* subscriber) {
  auto it = subscribers_.find(origin);
  if (it == subscribers_.end())
    return;
  base::ObserverList<Subscriber>& queue = it->second;
  queue.RemoveObserver(subscriber);

  if (queue.begin() == queue.end())
    subscribers_.erase(it);
}

bool SmsQueue::HasSubscribers() {
  return !subscribers_.empty();
}

bool SmsQueue::HasSubscriber(const url::Origin& origin,
                             const Subscriber* subscriber) {
  return (subscribers_.find(origin) != subscribers_.end()) &&
         subscribers_[origin].HasObserver(subscriber);
}

// Currently we cannot extract the origin information upon failure because it's
// not visible to the service. If we have a single origin in the queue we simply
// assume failure belongs to that origin. This assumption should hold for vast
// majority of cases given that a single pending origin is the most likely
// scenario (measured in UMA histogram  |Blink.Sms.PendingOriginCount|). However
// if there is more than one origin waiting we do not pass up the error to avoid
// over-counting failures. Similar to the success case, we only notify the first
// subscriber with that origin.
bool SmsQueue::NotifyFailure(FailureType failure_type) {
  // TODO(crbug.com/1138454): We should improve the infrastructure to be able to
  // handle failed requests when there are multiple pending origins
  // simultaneously.
  if (subscribers_.size() != 1)
    return false;

  const url::Origin& implied_origin = subscribers_.begin()->first;
  Subscriber* subscriber = Pop(implied_origin);
  subscriber->OnFailure(failure_type);
  return true;
}

}  // namespace content