Coverage Report

Created: 2018-09-25 14:53

/src/mozilla-central/dom/media/doctor/DDLifetimes.h
Line
Count
Source (jump to first uncovered line)
1
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2
/* vim:set ts=2 sw=2 sts=2 et cindent: */
3
/* This Source Code Form is subject to the terms of the Mozilla Public
4
 * License, v. 2.0. If a copy of the MPL was not distributed with this
5
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6
7
#ifndef DDLifetimes_h_
8
#define DDLifetimes_h_
9
10
#include "DDLifetime.h"
11
#include "DDLoggedTypeTraits.h"
12
#include "nsClassHashtable.h"
13
#include "nsTArray.h"
14
15
namespace mozilla {
16
17
// Managed list of lifetimes.
18
class DDLifetimes
19
{
20
public:
21
  // DDLifetime for a given aObject, that exists at the aIndex time;
22
  // otherwise nullptr.
23
  DDLifetime* FindLifetime(const DDLogObject& aObject,
24
                           const DDMessageIndex& aIndex);
25
26
  // DDLifetime for a given aObject, that exists at the aIndex time;
27
  // otherwise nullptr.
28
  const DDLifetime* FindLifetime(const DDLogObject& aObject,
29
                                 const DDMessageIndex& aIndex) const;
30
31
  // Create a lifetime with the given object and construction index&time.
32
  DDLifetime& CreateLifetime(const DDLogObject& aObject,
33
                             DDMessageIndex aIndex,
34
                             const DDTimeStamp& aConstructionTimeStamp);
35
36
  // Remove an existing lifetime (assumed to just have been found by
37
  // FindLifetime()).
38
  void RemoveLifetime(const DDLifetime* aLifetime);
39
40
  // Remove all lifetimes associated with the given HTMLMediaElement.
41
  void RemoveLifetimesFor(const dom::HTMLMediaElement* aMediaElement);
42
43
  // Remove all lifetimes.
44
  void Clear();
45
46
  // Visit all lifetimes associated with an HTMLMediaElement and run
47
  // `aF(const DDLifetime&)` on each one.
48
  // If aOnlyHTMLMediaElement is true, only run aF once of that element.
49
  template<typename F>
50
  void Visit(const dom::HTMLMediaElement* aMediaElement,
51
             F&& aF,
52
             bool aOnlyHTMLMediaElement = false) const
53
0
  {
54
0
    for (auto iter = mLifetimes.ConstIter(); !iter.Done(); iter.Next()) {
55
0
      for (const DDLifetime& lifetime : *iter.UserData()) {
56
0
        if (lifetime.mMediaElement == aMediaElement) {
57
0
          if (aOnlyHTMLMediaElement) {
58
0
            if (lifetime.mObject.Pointer() == aMediaElement &&
59
0
                lifetime.mObject.TypeName() ==
60
0
                  DDLoggedTypeTraits<dom::HTMLMediaElement>::Name()) {
61
0
              aF(lifetime);
62
0
              break;
63
0
            }
64
0
            continue;
65
0
          }
66
0
          static_assert(IsSame<decltype(aF(lifetime)), void>::value, "");
67
0
          aF(lifetime);
68
0
        }
69
0
      }
70
0
    }
71
0
  }
Unexecuted instantiation: Unified_cpp_dom_media_doctor0.cpp:void mozilla::DDLifetimes::Visit<mozilla::DDMediaLogs::FulfillPromises()::$_4>(mozilla::dom::HTMLMediaElement const*, mozilla::DDMediaLogs::FulfillPromises()::$_4&&, bool) const
Unexecuted instantiation: Unified_cpp_dom_media_doctor0.cpp:void mozilla::DDLifetimes::Visit<mozilla::DDMediaLogs::CleanUpLogs()::$_6>(mozilla::dom::HTMLMediaElement const*, mozilla::DDMediaLogs::CleanUpLogs()::$_6&&, bool) const
72
73
  // Visit all lifetimes associated with an HTMLMediaElement and run
74
  // `aF(const DDLifetime&)` on each one.
75
  // If aF() returns false, the loop continues.
76
  // If aF() returns true, the loop stops, and true is returned immediately.
77
  // If all aF() calls have returned false, false is returned at the end.
78
  template<typename F>
79
  bool VisitBreakable(const dom::HTMLMediaElement* aMediaElement, F&& aF) const
80
0
  {
81
0
    for (auto iter = mLifetimes.ConstIter(); !iter.Done(); iter.Next()) {
82
0
      for (const DDLifetime& lifetime : *iter.UserData()) {
83
0
        if (lifetime.mMediaElement == aMediaElement) {
84
0
          static_assert(IsSame<decltype(aF(lifetime)), bool>::value, "");
85
0
          if (aF(lifetime)) {
86
0
            return true;
87
0
          }
88
0
        }
89
0
      }
90
0
    }
91
0
    return false;
92
0
  }
93
94
  size_t SizeOfExcludingThis(MallocSizeOf aMallocSizeOf) const;
95
96
private:
97
  // Hashtable key to use for each DDLogObject.
98
  class DDLogObjectHashKey : public PLDHashEntryHdr
99
  {
100
  public:
101
    typedef const DDLogObject& KeyType;
102
    typedef const DDLogObject* KeyTypePointer;
103
104
    explicit DDLogObjectHashKey(KeyTypePointer aKey)
105
      : mValue(*aKey)
106
0
    {
107
0
    }
108
    DDLogObjectHashKey(const DDLogObjectHashKey& aToCopy)
109
      : mValue(aToCopy.mValue)
110
0
    {
111
0
    }
112
0
    ~DDLogObjectHashKey() {}
113
114
0
    KeyType GetKey() const { return mValue; }
115
0
    bool KeyEquals(KeyTypePointer aKey) const { return *aKey == mValue; }
116
117
0
    static KeyTypePointer KeyToPointer(KeyType aKey) { return &aKey; }
118
    static PLDHashNumber HashKey(KeyTypePointer aKey)
119
0
    {
120
0
      return HashBytes(aKey, sizeof(DDLogObject));
121
0
    }
122
    enum
123
    {
124
      ALLOW_MEMMOVE = true
125
    };
126
127
  private:
128
    const DDLogObject mValue;
129
  };
130
131
  // Array of all DDLifetimes for a given DDLogObject; they should be
132
  // distinguished by their construction&destruction times.
133
  using LifetimesForObject = nsTArray<DDLifetime>;
134
135
  // For each DDLogObject, we store an array of all objects that have used this
136
  // pointer and type.
137
  nsClassHashtable<DDLogObjectHashKey, LifetimesForObject> mLifetimes;
138
};
139
140
} // namespace mozilla
141
142
#endif // DDLifetimes_h_