Coverage Report

Created: 2018-09-25 14:53

/src/mozilla-central/toolkit/components/url-classifier/ChunkSet.h
Line
Count
Source (jump to first uncovered line)
1
//* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2
/* This Source Code Form is subject to the terms of the Mozilla Public
3
 * License, v. 2.0. If a copy of the MPL was not distributed with this
4
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
5
6
#ifndef ChunkSet_h__
7
#define ChunkSet_h__
8
9
#include "Entries.h"
10
#include "nsString.h"
11
#include "nsTArray.h"
12
13
namespace mozilla {
14
namespace safebrowsing {
15
16
/**
17
 * Store the chunk numbers as an array of ranges of uint32_t.
18
 * We need chunk numbers in order to ask for incremental updates from the
19
 * server.
20
 */
21
class ChunkSet {
22
public:
23
  nsresult Serialize(nsACString& aStr);
24
  nsresult Set(uint32_t aChunk);
25
  bool Has(uint32_t chunk) const;
26
  nsresult Merge(const ChunkSet& aOther);
27
  uint32_t Length() const;
28
  nsresult Remove(const ChunkSet& aOther);
29
  void Clear();
30
31
  nsresult Write(nsIOutputStream* aOut) const;
32
  nsresult Read(nsIInputStream* aIn, uint32_t aNumElements);
33
34
private:
35
  class Range {
36
  public:
37
0
    Range(uint32_t aBegin, uint32_t aEnd) : mBegin(aBegin), mEnd(aEnd) {}
38
39
    uint32_t Length() const;
40
    nsresult Remove(const Range& aRange, ChunkSet& aRemainderSet) const;
41
    bool FoldLeft(const Range& aRange);
42
43
0
    bool operator==(const Range& rhs) const {
44
0
      return mBegin == rhs.mBegin;
45
0
    }
46
0
    bool operator<(const Range& rhs) const {
47
0
      return mBegin < rhs.mBegin;
48
0
    }
49
50
0
    uint32_t Begin() const {
51
0
      return mBegin;
52
0
    }
53
0
    void Begin(const uint32_t aBegin) {
54
0
      mBegin = aBegin;
55
0
    }
56
0
    uint32_t End() const {
57
0
      return mEnd;
58
0
    }
59
0
    void End(const uint32_t aEnd) {
60
0
      mEnd = aEnd;
61
0
    }
62
63
0
    bool Contains(const Range& aRange) const {
64
0
      return mBegin <= aRange.mBegin && aRange.mEnd <= mEnd;
65
0
    }
66
0
    bool Precedes(const Range& aRange) const {
67
0
      return mEnd + 1 == aRange.mBegin;
68
0
    }
69
70
    struct IntersectionComparator {
71
0
      int operator()(const Range& aRange) const {
72
0
        if (aRange.mBegin > mTarget.mEnd) {
73
0
          return -1;
74
0
        }
75
0
        if (mTarget.mBegin > aRange.mEnd) {
76
0
          return 1;
77
0
        }
78
0
        return 0;
79
0
      }
80
81
0
      explicit IntersectionComparator(const Range& aTarget) : mTarget(aTarget){}
82
      const Range& mTarget;
83
    };
84
85
  private:
86
    uint32_t mBegin;
87
    uint32_t mEnd;
88
  };
89
90
  static const size_t IO_BUFFER_SIZE = 1024;
91
  FallibleTArray<Range> mRanges;
92
93
  bool HasSubrange(const Range& aSubrange) const;
94
};
95
96
} // namespace safebrowsing
97
} // namespace mozilla
98
99
#endif