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