summaryrefslogtreecommitdiff
path: root/chromium/third_party/blink/renderer/modules/geolocation/geolocation_watchers.cc
blob: 1e7a3b0ce1c19c104ae09f856e35078c07dda00d (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
78
79
80
81
// Copyright 2014 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 "third_party/blink/renderer/modules/geolocation/geolocation_watchers.h"

#include "third_party/blink/renderer/modules/geolocation/geo_notifier.h"
#include "third_party/blink/renderer/platform/wtf/assertions.h"

namespace blink {

void GeolocationWatchers::Trace(Visitor* visitor) const {
  visitor->Trace(id_to_notifier_map_);
  visitor->Trace(notifier_to_id_map_);
}

bool GeolocationWatchers::Add(int id, GeoNotifier* notifier) {
  DCHECK_GT(id, 0);
  if (!id_to_notifier_map_.insert(id, notifier).is_new_entry)
    return false;
  DCHECK(!notifier->IsTimerActive());
  notifier_to_id_map_.Set(notifier, id);
  return true;
}

GeoNotifier* GeolocationWatchers::Find(int id) const {
  DCHECK_GT(id, 0);
  IdToNotifierMap::const_iterator iter = id_to_notifier_map_.find(id);
  if (iter == id_to_notifier_map_.end())
    return nullptr;
  return iter->value;
}

void GeolocationWatchers::Remove(int id) {
  DCHECK_GT(id, 0);
  IdToNotifierMap::iterator iter = id_to_notifier_map_.find(id);
  if (iter == id_to_notifier_map_.end())
    return;
  DCHECK(!iter->value->IsTimerActive());
  notifier_to_id_map_.erase(iter->value);
  id_to_notifier_map_.erase(iter);
}

void GeolocationWatchers::Remove(GeoNotifier* notifier) {
  NotifierToIdMap::iterator iter = notifier_to_id_map_.find(notifier);
  if (iter == notifier_to_id_map_.end())
    return;
  DCHECK(!notifier->IsTimerActive());
  id_to_notifier_map_.erase(iter->value);
  notifier_to_id_map_.erase(iter);
}

bool GeolocationWatchers::Contains(GeoNotifier* notifier) const {
  return notifier_to_id_map_.Contains(notifier);
}

void GeolocationWatchers::Clear() {
#if DCHECK_IS_ON()
  for (const auto& notifier : Notifiers()) {
    DCHECK(!notifier->IsTimerActive());
  }
#endif
  id_to_notifier_map_.clear();
  notifier_to_id_map_.clear();
}

bool GeolocationWatchers::IsEmpty() const {
  return id_to_notifier_map_.IsEmpty();
}

void GeolocationWatchers::Swap(GeolocationWatchers& other) {
  swap(id_to_notifier_map_, other.id_to_notifier_map_);
  swap(notifier_to_id_map_, other.notifier_to_id_map_);
}

void GeolocationWatchers::CopyNotifiersToVector(
    HeapVector<Member<GeoNotifier>>& vector) const {
  CopyValuesToVector(id_to_notifier_map_, vector);
}

}  // namespace blink