Coverage Report

Created: 2018-09-25 14:53

/src/mozilla-central/dom/media/gmp/GMPMemoryStorage.cpp
Line
Count
Source (jump to first uncovered line)
1
/* -*- Mode: C++; tab-width: 2; 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
#include "GMPStorage.h"
7
#include "nsClassHashtable.h"
8
9
namespace mozilla {
10
namespace gmp {
11
12
class GMPMemoryStorage : public GMPStorage {
13
public:
14
  GMPErr Open(const nsCString& aRecordName) override
15
0
  {
16
0
    MOZ_ASSERT(!IsOpen(aRecordName));
17
0
18
0
    Record* record = nullptr;
19
0
    if (!mRecords.Get(aRecordName, &record)) {
20
0
      record = new Record();
21
0
      mRecords.Put(aRecordName, record);
22
0
    }
23
0
    record->mIsOpen = true;
24
0
    return GMPNoErr;
25
0
  }
26
27
0
  bool IsOpen(const nsCString& aRecordName) const override {
28
0
    const Record* record = mRecords.Get(aRecordName);
29
0
    if (!record) {
30
0
      return false;
31
0
    }
32
0
    return record->mIsOpen;
33
0
  }
34
35
  GMPErr Read(const nsCString& aRecordName,
36
              nsTArray<uint8_t>& aOutBytes) override
37
0
  {
38
0
    const Record* record = mRecords.Get(aRecordName);
39
0
    if (!record) {
40
0
      return GMPGenericErr;
41
0
    }
42
0
    aOutBytes = record->mData;
43
0
    return GMPNoErr;
44
0
  }
45
46
  GMPErr Write(const nsCString& aRecordName,
47
               const nsTArray<uint8_t>& aBytes) override
48
0
  {
49
0
    Record* record = nullptr;
50
0
    if (!mRecords.Get(aRecordName, &record)) {
51
0
      return GMPClosedErr;
52
0
    }
53
0
    record->mData = aBytes;
54
0
    return GMPNoErr;
55
0
  }
56
57
  void Close(const nsCString& aRecordName) override
58
0
  {
59
0
    Record* record = nullptr;
60
0
    if (!mRecords.Get(aRecordName, &record)) {
61
0
      return;
62
0
    }
63
0
    if (!record->mData.Length()) {
64
0
      // Record is empty, delete.
65
0
      mRecords.Remove(aRecordName);
66
0
    } else {
67
0
      record->mIsOpen = false;
68
0
    }
69
0
  }
70
71
private:
72
73
  struct Record {
74
    nsTArray<uint8_t> mData;
75
    bool mIsOpen = false;
76
  };
77
78
  nsClassHashtable<nsCStringHashKey, Record> mRecords;
79
};
80
81
already_AddRefed<GMPStorage> CreateGMPMemoryStorage()
82
0
{
83
0
  return RefPtr<GMPStorage>(new GMPMemoryStorage()).forget();
84
0
}
85
86
} // namespace gmp
87
} // namespace mozilla