2014-08-19 12:56:33 +04:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
|
|
|
|
|
|
|
#include "GMPStorageChild.h"
|
|
|
|
#include "GMPChild.h"
|
|
|
|
#include "gmp-storage.h"
|
|
|
|
|
2014-11-28 02:34:00 +03:00
|
|
|
#define ON_GMP_THREAD() (mPlugin->GMPMessageLoop() == MessageLoop::current())
|
|
|
|
|
|
|
|
#define CALL_ON_GMP_THREAD(_func, ...) \
|
|
|
|
do { \
|
|
|
|
if (ON_GMP_THREAD()) { \
|
|
|
|
_func(__VA_ARGS__); \
|
|
|
|
} else { \
|
|
|
|
mPlugin->GMPMessageLoop()->PostTask( \
|
|
|
|
FROM_HERE, NewRunnableMethod(this, &GMPStorageChild::_func, ##__VA_ARGS__) \
|
|
|
|
); \
|
|
|
|
} \
|
|
|
|
} while(false)
|
|
|
|
|
|
|
|
static nsTArray<uint8_t>
|
|
|
|
ToArray(const uint8_t* aData, uint32_t aDataSize)
|
|
|
|
{
|
|
|
|
nsTArray<uint8_t> data;
|
|
|
|
data.AppendElements(aData, aDataSize);
|
|
|
|
return mozilla::Move(data);
|
|
|
|
}
|
|
|
|
|
2014-08-19 12:56:33 +04:00
|
|
|
namespace mozilla {
|
|
|
|
namespace gmp {
|
|
|
|
|
|
|
|
GMPRecordImpl::GMPRecordImpl(GMPStorageChild* aOwner,
|
|
|
|
const nsCString& aName,
|
|
|
|
GMPRecordClient* aClient)
|
|
|
|
: mName(aName)
|
|
|
|
, mClient(aClient)
|
|
|
|
, mOwner(aOwner)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
GMPErr
|
|
|
|
GMPRecordImpl::Open()
|
|
|
|
{
|
|
|
|
return mOwner->Open(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
GMPRecordImpl::OpenComplete(GMPErr aStatus)
|
|
|
|
{
|
|
|
|
mClient->OpenComplete(aStatus);
|
|
|
|
}
|
|
|
|
|
|
|
|
GMPErr
|
|
|
|
GMPRecordImpl::Read()
|
|
|
|
{
|
|
|
|
return mOwner->Read(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
GMPRecordImpl::ReadComplete(GMPErr aStatus,
|
|
|
|
const uint8_t* aBytes,
|
|
|
|
uint32_t aLength)
|
|
|
|
{
|
|
|
|
mClient->ReadComplete(aStatus, aBytes, aLength);
|
|
|
|
}
|
|
|
|
|
|
|
|
GMPErr
|
|
|
|
GMPRecordImpl::Write(const uint8_t* aData, uint32_t aDataSize)
|
|
|
|
{
|
|
|
|
return mOwner->Write(this, aData, aDataSize);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
GMPRecordImpl::WriteComplete(GMPErr aStatus)
|
|
|
|
{
|
|
|
|
mClient->WriteComplete(aStatus);
|
|
|
|
}
|
|
|
|
|
|
|
|
GMPErr
|
|
|
|
GMPRecordImpl::Close()
|
|
|
|
{
|
|
|
|
nsRefPtr<GMPRecordImpl> kungfuDeathGrip(this);
|
|
|
|
// Delete our self reference.
|
|
|
|
Release();
|
2014-11-28 02:34:00 +03:00
|
|
|
mOwner->Close(this->Name());
|
2014-08-19 12:56:33 +04:00
|
|
|
return GMPNoErr;
|
|
|
|
}
|
|
|
|
|
|
|
|
GMPStorageChild::GMPStorageChild(GMPChild* aPlugin)
|
2014-11-28 02:34:00 +03:00
|
|
|
: mMonitor("GMPStorageChild")
|
|
|
|
, mPlugin(aPlugin)
|
2014-08-19 12:56:33 +04:00
|
|
|
, mShutdown(false)
|
|
|
|
{
|
2014-11-28 02:34:00 +03:00
|
|
|
MOZ_ASSERT(ON_GMP_THREAD());
|
2014-08-19 12:56:33 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
GMPErr
|
|
|
|
GMPStorageChild::CreateRecord(const nsCString& aRecordName,
|
|
|
|
GMPRecord** aOutRecord,
|
|
|
|
GMPRecordClient* aClient)
|
|
|
|
{
|
2014-11-28 02:34:00 +03:00
|
|
|
MonitorAutoLock lock(mMonitor);
|
|
|
|
|
2014-08-19 12:56:33 +04:00
|
|
|
if (mShutdown) {
|
|
|
|
NS_WARNING("GMPStorage used after it's been shutdown!");
|
|
|
|
return GMPClosedErr;
|
|
|
|
}
|
2014-11-28 02:34:00 +03:00
|
|
|
|
2014-08-19 12:56:33 +04:00
|
|
|
MOZ_ASSERT(aRecordName.Length() && aOutRecord);
|
|
|
|
nsRefPtr<GMPRecordImpl> record(new GMPRecordImpl(this, aRecordName, aClient));
|
|
|
|
mRecords.Put(aRecordName, record); // Addrefs
|
|
|
|
|
|
|
|
// The GMPRecord holds a self reference until the GMP calls Close() on
|
|
|
|
// it. This means the object is always valid (even if neutered) while
|
|
|
|
// the GMP expects it to be.
|
|
|
|
record.forget(aOutRecord);
|
|
|
|
|
|
|
|
return GMPNoErr;
|
|
|
|
}
|
|
|
|
|
2014-11-28 02:34:00 +03:00
|
|
|
bool
|
|
|
|
GMPStorageChild::HasRecord(const nsCString& aRecordName)
|
|
|
|
{
|
|
|
|
mMonitor.AssertCurrentThreadOwns();
|
|
|
|
return mRecords.Contains(aRecordName);
|
|
|
|
}
|
|
|
|
|
|
|
|
already_AddRefed<GMPRecordImpl>
|
|
|
|
GMPStorageChild::GetRecord(const nsCString& aRecordName)
|
|
|
|
{
|
|
|
|
MonitorAutoLock lock(mMonitor);
|
|
|
|
nsRefPtr<GMPRecordImpl> record;
|
|
|
|
mRecords.Get(aRecordName, getter_AddRefs(record));
|
|
|
|
return record.forget();
|
|
|
|
}
|
|
|
|
|
2014-08-19 12:56:33 +04:00
|
|
|
GMPErr
|
|
|
|
GMPStorageChild::Open(GMPRecordImpl* aRecord)
|
|
|
|
{
|
2014-11-28 02:34:00 +03:00
|
|
|
MonitorAutoLock lock(mMonitor);
|
|
|
|
|
2014-08-19 12:56:33 +04:00
|
|
|
if (mShutdown) {
|
|
|
|
NS_WARNING("GMPStorage used after it's been shutdown!");
|
|
|
|
return GMPClosedErr;
|
|
|
|
}
|
2014-11-28 02:34:00 +03:00
|
|
|
|
|
|
|
if (!HasRecord(aRecord->Name())) {
|
|
|
|
// Trying to re-open a record that has already been closed.
|
2014-08-19 12:56:33 +04:00
|
|
|
return GMPClosedErr;
|
|
|
|
}
|
2014-11-28 02:34:00 +03:00
|
|
|
|
|
|
|
CALL_ON_GMP_THREAD(SendOpen, aRecord->Name());
|
|
|
|
|
2014-08-19 12:56:33 +04:00
|
|
|
return GMPNoErr;
|
|
|
|
}
|
|
|
|
|
|
|
|
GMPErr
|
|
|
|
GMPStorageChild::Read(GMPRecordImpl* aRecord)
|
|
|
|
{
|
2014-11-28 02:34:00 +03:00
|
|
|
MonitorAutoLock lock(mMonitor);
|
|
|
|
|
2014-08-19 12:56:33 +04:00
|
|
|
if (mShutdown) {
|
|
|
|
NS_WARNING("GMPStorage used after it's been shutdown!");
|
|
|
|
return GMPClosedErr;
|
|
|
|
}
|
2014-11-28 02:34:00 +03:00
|
|
|
|
|
|
|
if (!HasRecord(aRecord->Name())) {
|
|
|
|
// Record not opened.
|
2014-08-19 12:56:33 +04:00
|
|
|
return GMPClosedErr;
|
|
|
|
}
|
2014-11-28 02:34:00 +03:00
|
|
|
|
|
|
|
CALL_ON_GMP_THREAD(SendRead, aRecord->Name());
|
|
|
|
|
2014-08-19 12:56:33 +04:00
|
|
|
return GMPNoErr;
|
|
|
|
}
|
|
|
|
|
|
|
|
GMPErr
|
|
|
|
GMPStorageChild::Write(GMPRecordImpl* aRecord,
|
|
|
|
const uint8_t* aData,
|
|
|
|
uint32_t aDataSize)
|
|
|
|
{
|
2014-11-28 02:34:00 +03:00
|
|
|
if (aDataSize > GMP_MAX_RECORD_SIZE) {
|
|
|
|
return GMPQuotaExceededErr;
|
2014-08-19 12:56:33 +04:00
|
|
|
}
|
2014-11-28 02:34:00 +03:00
|
|
|
|
|
|
|
MonitorAutoLock lock(mMonitor);
|
|
|
|
|
2014-08-19 12:56:33 +04:00
|
|
|
if (mShutdown) {
|
|
|
|
NS_WARNING("GMPStorage used after it's been shutdown!");
|
|
|
|
return GMPClosedErr;
|
|
|
|
}
|
2014-11-28 02:34:00 +03:00
|
|
|
|
|
|
|
if (!HasRecord(aRecord->Name())) {
|
|
|
|
// Record not opened.
|
2014-08-19 12:56:33 +04:00
|
|
|
return GMPClosedErr;
|
|
|
|
}
|
2014-11-28 02:34:00 +03:00
|
|
|
|
|
|
|
CALL_ON_GMP_THREAD(SendWrite, aRecord->Name(), ToArray(aData, aDataSize));
|
|
|
|
|
2014-08-19 12:56:33 +04:00
|
|
|
return GMPNoErr;
|
|
|
|
}
|
|
|
|
|
|
|
|
GMPErr
|
2014-11-28 02:34:00 +03:00
|
|
|
GMPStorageChild::Close(const nsCString& aRecordName)
|
2014-08-19 12:56:33 +04:00
|
|
|
{
|
2014-11-28 02:34:00 +03:00
|
|
|
MonitorAutoLock lock(mMonitor);
|
|
|
|
|
|
|
|
if (!HasRecord(aRecordName)) {
|
2014-08-19 12:56:33 +04:00
|
|
|
// Already closed.
|
|
|
|
return GMPClosedErr;
|
|
|
|
}
|
|
|
|
|
2014-11-28 02:34:00 +03:00
|
|
|
mRecords.Remove(aRecordName);
|
2014-08-19 12:56:33 +04:00
|
|
|
|
2014-11-28 02:34:00 +03:00
|
|
|
if (!mShutdown) {
|
|
|
|
CALL_ON_GMP_THREAD(SendClose, aRecordName);
|
|
|
|
}
|
2014-08-19 12:56:33 +04:00
|
|
|
|
2014-11-28 02:34:00 +03:00
|
|
|
return GMPNoErr;
|
2014-08-19 12:56:33 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
GMPStorageChild::RecvOpenComplete(const nsCString& aRecordName,
|
|
|
|
const GMPErr& aStatus)
|
|
|
|
{
|
2014-11-28 02:34:00 +03:00
|
|
|
// We don't need a lock to read |mShutdown| since it is only changed in
|
|
|
|
// the GMP thread.
|
2014-08-19 12:56:33 +04:00
|
|
|
if (mShutdown) {
|
|
|
|
return true;
|
|
|
|
}
|
2014-11-28 02:34:00 +03:00
|
|
|
nsRefPtr<GMPRecordImpl> record = GetRecord(aRecordName);
|
|
|
|
if (!record) {
|
2014-08-19 12:56:33 +04:00
|
|
|
// Not fatal.
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
record->OpenComplete(aStatus);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
GMPStorageChild::RecvReadComplete(const nsCString& aRecordName,
|
|
|
|
const GMPErr& aStatus,
|
|
|
|
const InfallibleTArray<uint8_t>& aBytes)
|
|
|
|
{
|
|
|
|
if (mShutdown) {
|
|
|
|
return true;
|
|
|
|
}
|
2014-11-28 02:34:00 +03:00
|
|
|
nsRefPtr<GMPRecordImpl> record = GetRecord(aRecordName);
|
|
|
|
if (!record) {
|
2014-08-19 12:56:33 +04:00
|
|
|
// Not fatal.
|
|
|
|
return true;
|
|
|
|
}
|
2014-11-28 02:34:00 +03:00
|
|
|
record->ReadComplete(aStatus, aBytes.Elements(), aBytes.Length());
|
2014-08-19 12:56:33 +04:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
GMPStorageChild::RecvWriteComplete(const nsCString& aRecordName,
|
|
|
|
const GMPErr& aStatus)
|
|
|
|
{
|
|
|
|
if (mShutdown) {
|
|
|
|
return true;
|
|
|
|
}
|
2014-11-28 02:34:00 +03:00
|
|
|
nsRefPtr<GMPRecordImpl> record = GetRecord(aRecordName);
|
|
|
|
if (!record) {
|
2014-08-19 12:56:33 +04:00
|
|
|
// Not fatal.
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
record->WriteComplete(aStatus);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2014-11-21 02:25:12 +03:00
|
|
|
GMPErr
|
|
|
|
GMPStorageChild::EnumerateRecords(RecvGMPRecordIteratorPtr aRecvIteratorFunc,
|
|
|
|
void* aUserArg)
|
|
|
|
{
|
2014-11-28 02:34:00 +03:00
|
|
|
MonitorAutoLock lock(mMonitor);
|
|
|
|
|
2014-11-21 02:25:12 +03:00
|
|
|
if (mShutdown) {
|
|
|
|
NS_WARNING("GMPStorage used after it's been shutdown!");
|
|
|
|
return GMPClosedErr;
|
|
|
|
}
|
2014-11-28 02:34:00 +03:00
|
|
|
|
2014-11-21 02:25:12 +03:00
|
|
|
MOZ_ASSERT(aRecvIteratorFunc);
|
|
|
|
mPendingRecordIterators.push(RecordIteratorContext(aRecvIteratorFunc, aUserArg));
|
2014-11-28 02:34:00 +03:00
|
|
|
|
|
|
|
CALL_ON_GMP_THREAD(SendGetRecordNames);
|
|
|
|
|
2014-11-21 02:25:12 +03:00
|
|
|
return GMPNoErr;
|
|
|
|
}
|
|
|
|
|
|
|
|
class GMPRecordIteratorImpl : public GMPRecordIterator {
|
|
|
|
public:
|
2014-12-11 01:49:09 +03:00
|
|
|
explicit GMPRecordIteratorImpl(const InfallibleTArray<nsCString>& aRecordNames)
|
2014-11-21 02:25:12 +03:00
|
|
|
: mRecordNames(aRecordNames)
|
|
|
|
, mIndex(0)
|
|
|
|
{
|
|
|
|
mRecordNames.Sort();
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual GMPErr GetName(const char** aOutName, uint32_t* aOutNameLength) MOZ_OVERRIDE {
|
|
|
|
if (!aOutName || !aOutNameLength) {
|
|
|
|
return GMPInvalidArgErr;
|
|
|
|
}
|
|
|
|
if (mIndex == mRecordNames.Length()) {
|
|
|
|
return GMPEndOfEnumeration;
|
|
|
|
}
|
|
|
|
*aOutName = mRecordNames[mIndex].get();
|
|
|
|
*aOutNameLength = mRecordNames[mIndex].Length();
|
|
|
|
return GMPNoErr;
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual GMPErr NextRecord() MOZ_OVERRIDE {
|
|
|
|
if (mIndex < mRecordNames.Length()) {
|
|
|
|
mIndex++;
|
|
|
|
}
|
|
|
|
return (mIndex < mRecordNames.Length()) ? GMPNoErr
|
|
|
|
: GMPEndOfEnumeration;
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual void Close() MOZ_OVERRIDE {
|
|
|
|
delete this;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
nsTArray<nsCString> mRecordNames;
|
|
|
|
size_t mIndex;
|
|
|
|
};
|
|
|
|
|
|
|
|
bool
|
|
|
|
GMPStorageChild::RecvRecordNames(const InfallibleTArray<nsCString>& aRecordNames,
|
|
|
|
const GMPErr& aStatus)
|
|
|
|
{
|
2014-11-28 02:34:00 +03:00
|
|
|
RecordIteratorContext ctx;
|
|
|
|
{
|
|
|
|
MonitorAutoLock lock(mMonitor);
|
|
|
|
if (mShutdown || mPendingRecordIterators.empty()) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
ctx = mPendingRecordIterators.front();
|
|
|
|
mPendingRecordIterators.pop();
|
2014-11-21 02:25:12 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if (GMP_FAILED(aStatus)) {
|
|
|
|
ctx.mFunc(nullptr, ctx.mUserArg, aStatus);
|
|
|
|
} else {
|
|
|
|
ctx.mFunc(new GMPRecordIteratorImpl(aRecordNames), ctx.mUserArg, GMPNoErr);
|
|
|
|
}
|
2014-11-28 02:34:00 +03:00
|
|
|
|
2014-11-21 02:25:12 +03:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2014-08-19 12:56:33 +04:00
|
|
|
bool
|
|
|
|
GMPStorageChild::RecvShutdown()
|
|
|
|
{
|
|
|
|
// Block any new storage requests, and thus any messages back to the
|
|
|
|
// parent. We don't delete any objects here, as that may invalidate
|
|
|
|
// GMPRecord pointers held by the GMP.
|
2014-11-28 02:34:00 +03:00
|
|
|
MonitorAutoLock lock(mMonitor);
|
2014-08-19 12:56:33 +04:00
|
|
|
mShutdown = true;
|
2014-11-21 02:25:12 +03:00
|
|
|
while (!mPendingRecordIterators.empty()) {
|
|
|
|
mPendingRecordIterators.pop();
|
|
|
|
}
|
2014-08-19 12:56:33 +04:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace gmp
|
|
|
|
} // namespace mozilla
|
2014-11-28 02:34:00 +03:00
|
|
|
|
|
|
|
// avoid redefined macro in unified build
|
|
|
|
#undef ON_GMP_THREAD
|
|
|
|
#undef CALL_ON_GMP_THREAD
|