GMPMemoryStorage.cpp (2554B)
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 "GMPLog.h" 7 #include "GMPStorage.h" 8 #include "nsClassHashtable.h" 9 10 namespace mozilla::gmp { 11 12 #define LOG(msg, ...) \ 13 MOZ_LOG(GetGMPLog(), LogLevel::Debug, \ 14 ("GMPMemoryStorage=%p, " msg, this, ##__VA_ARGS__)) 15 16 class GMPMemoryStorage : public GMPStorage { 17 public: 18 GMPMemoryStorage(const nsACString& aNodeId, const nsAString& aGMPName) { 19 LOG("Created GMPMemoryStorage, nodeId=%s, gmpName=%s", 20 aNodeId.BeginReading(), NS_ConvertUTF16toUTF8(aGMPName).get()); 21 } 22 ~GMPMemoryStorage() { LOG("Destroyed GMPMemoryStorage"); } 23 24 GMPErr Open(const nsACString& aRecordName) override { 25 MOZ_ASSERT(!IsOpen(aRecordName)); 26 27 Record* record = mRecords.GetOrInsertNew(aRecordName); 28 record->mIsOpen = true; 29 return GMPNoErr; 30 } 31 32 bool IsOpen(const nsACString& aRecordName) const override { 33 const Record* record = mRecords.Get(aRecordName); 34 if (!record) { 35 return false; 36 } 37 return record->mIsOpen; 38 } 39 40 GMPErr Read(const nsACString& aRecordName, 41 nsTArray<uint8_t>& aOutBytes) override { 42 const Record* record = mRecords.Get(aRecordName); 43 if (!record) { 44 return GMPGenericErr; 45 } 46 aOutBytes = record->mData.Clone(); 47 return GMPNoErr; 48 } 49 50 GMPErr Write(const nsACString& aRecordName, 51 const nsTArray<uint8_t>& aBytes) override { 52 Record* record = nullptr; 53 if (!mRecords.Get(aRecordName, &record)) { 54 return GMPClosedErr; 55 } 56 record->mData = aBytes.Clone(); 57 return GMPNoErr; 58 } 59 60 void Close(const nsACString& aRecordName) override { 61 Record* record = nullptr; 62 if (!mRecords.Get(aRecordName, &record)) { 63 return; 64 } 65 if (!record->mData.Length()) { 66 // Record is empty, delete. 67 mRecords.Remove(aRecordName); 68 } else { 69 record->mIsOpen = false; 70 } 71 } 72 73 private: 74 struct Record { 75 nsTArray<uint8_t> mData; 76 bool mIsOpen = false; 77 }; 78 79 nsClassHashtable<nsCStringHashKey, Record> mRecords; 80 }; 81 82 already_AddRefed<GMPStorage> CreateGMPMemoryStorage(const nsACString& aNodeId, 83 const nsAString& aGMPName) { 84 return RefPtr<GMPStorage>(new GMPMemoryStorage(aNodeId, aGMPName)).forget(); 85 } 86 87 #undef LOG 88 89 } // namespace mozilla::gmp