2014-05-18 07:05:46 +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 "GMPPlatform.h"
|
2014-08-19 12:56:33 +04:00
|
|
|
#include "GMPStorageChild.h"
|
2014-08-05 11:56:05 +04:00
|
|
|
#include "GMPTimerChild.h"
|
2014-05-18 07:05:46 +04:00
|
|
|
#include "mozilla/Monitor.h"
|
2014-08-05 11:56:05 +04:00
|
|
|
#include "GMPChild.h"
|
2017-02-14 01:09:38 +03:00
|
|
|
#include "mozilla/Mutex.h"
|
|
|
|
#include "base/thread.h"
|
2017-02-27 00:42:59 +03:00
|
|
|
#include "base/time.h"
|
2017-02-14 01:09:38 +03:00
|
|
|
#include "mozilla/ReentrantMonitor.h"
|
|
|
|
|
2014-08-05 11:56:05 +04:00
|
|
|
#include <ctime>
|
2014-05-18 07:05:46 +04:00
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
namespace gmp {
|
|
|
|
|
|
|
|
static MessageLoop* sMainLoop = nullptr;
|
2014-08-05 11:56:05 +04:00
|
|
|
static GMPChild* sChild = nullptr;
|
2014-05-18 07:05:46 +04:00
|
|
|
|
2014-08-11 06:18:53 +04:00
|
|
|
static bool
|
|
|
|
IsOnChildMainThread()
|
|
|
|
{
|
|
|
|
return sMainLoop && sMainLoop == MessageLoop::current();
|
|
|
|
}
|
|
|
|
|
2014-05-18 07:05:46 +04:00
|
|
|
// We just need a refcounted wrapper for GMPTask objects.
|
2016-05-03 05:37:24 +03:00
|
|
|
class GMPRunnable final
|
2014-05-18 07:05:46 +04:00
|
|
|
{
|
|
|
|
public:
|
2016-05-03 05:37:24 +03:00
|
|
|
NS_INLINE_DECL_THREADSAFE_REFCOUNTING(GMPRunnable)
|
2014-05-18 07:05:46 +04:00
|
|
|
|
2016-05-03 05:37:24 +03:00
|
|
|
explicit GMPRunnable(GMPTask* aTask)
|
2014-05-18 07:05:46 +04:00
|
|
|
: mTask(aTask)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(mTask);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Run()
|
|
|
|
{
|
|
|
|
mTask->Run();
|
2014-07-11 07:35:56 +04:00
|
|
|
mTask->Destroy();
|
2014-05-18 07:05:46 +04:00
|
|
|
mTask = nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2016-05-03 05:37:24 +03:00
|
|
|
~GMPRunnable()
|
2014-05-18 07:05:46 +04:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2014-07-11 07:35:56 +04:00
|
|
|
GMPTask* mTask;
|
2014-05-18 07:05:46 +04:00
|
|
|
};
|
|
|
|
|
2016-05-03 05:37:24 +03:00
|
|
|
class GMPSyncRunnable final
|
2014-05-18 07:05:46 +04:00
|
|
|
{
|
|
|
|
public:
|
2016-05-03 05:37:24 +03:00
|
|
|
NS_INLINE_DECL_THREADSAFE_REFCOUNTING(GMPSyncRunnable)
|
2014-05-18 07:05:46 +04:00
|
|
|
|
2016-05-03 05:37:24 +03:00
|
|
|
GMPSyncRunnable(GMPTask* aTask, MessageLoop* aMessageLoop)
|
2014-05-18 07:05:46 +04:00
|
|
|
: mDone(false)
|
|
|
|
, mTask(aTask)
|
|
|
|
, mMessageLoop(aMessageLoop)
|
|
|
|
, mMonitor("GMPSyncRunnable")
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(mTask);
|
|
|
|
MOZ_ASSERT(mMessageLoop);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Post()
|
|
|
|
{
|
|
|
|
// We assert here for two reasons.
|
|
|
|
// 1) Nobody should be blocking the main thread.
|
|
|
|
// 2) This prevents deadlocks when doing sync calls to main which if the
|
|
|
|
// main thread tries to do a sync call back to the calling thread.
|
2014-08-11 06:18:53 +04:00
|
|
|
MOZ_ASSERT(!IsOnChildMainThread());
|
2014-05-18 07:05:46 +04:00
|
|
|
|
2017-06-12 22:34:10 +03:00
|
|
|
mMessageLoop->PostTask(NewRunnableMethod(
|
|
|
|
"gmp::GMPSyncRunnable::Run", this, &GMPSyncRunnable::Run));
|
2014-05-18 07:05:46 +04:00
|
|
|
MonitorAutoLock lock(mMonitor);
|
|
|
|
while (!mDone) {
|
|
|
|
lock.Wait();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Run()
|
|
|
|
{
|
|
|
|
mTask->Run();
|
2014-07-11 07:35:56 +04:00
|
|
|
mTask->Destroy();
|
2014-05-18 07:05:46 +04:00
|
|
|
mTask = nullptr;
|
|
|
|
MonitorAutoLock lock(mMonitor);
|
|
|
|
mDone = true;
|
|
|
|
lock.Notify();
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2016-05-03 05:37:24 +03:00
|
|
|
~GMPSyncRunnable()
|
2014-05-18 07:05:46 +04:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
bool mDone;
|
2014-07-11 07:35:56 +04:00
|
|
|
GMPTask* mTask;
|
2014-05-18 07:05:46 +04:00
|
|
|
MessageLoop* mMessageLoop;
|
|
|
|
Monitor mMonitor;
|
|
|
|
};
|
|
|
|
|
2017-02-14 01:09:38 +03:00
|
|
|
class GMPThreadImpl : public GMPThread
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
GMPThreadImpl();
|
|
|
|
virtual ~GMPThreadImpl();
|
|
|
|
|
|
|
|
// GMPThread
|
|
|
|
void Post(GMPTask* aTask) override;
|
|
|
|
void Join() override;
|
|
|
|
|
|
|
|
private:
|
|
|
|
Mutex mMutex;
|
|
|
|
base::Thread mThread;
|
|
|
|
};
|
|
|
|
|
2014-05-18 07:05:46 +04:00
|
|
|
GMPErr
|
|
|
|
CreateThread(GMPThread** aThread)
|
|
|
|
{
|
|
|
|
if (!aThread) {
|
|
|
|
return GMPGenericErr;
|
|
|
|
}
|
|
|
|
|
|
|
|
*aThread = new GMPThreadImpl();
|
|
|
|
|
|
|
|
return GMPNoErr;
|
|
|
|
}
|
|
|
|
|
|
|
|
GMPErr
|
|
|
|
RunOnMainThread(GMPTask* aTask)
|
|
|
|
{
|
|
|
|
if (!aTask || !sMainLoop) {
|
|
|
|
return GMPGenericErr;
|
|
|
|
}
|
|
|
|
|
2016-05-03 05:37:24 +03:00
|
|
|
RefPtr<GMPRunnable> r = new GMPRunnable(aTask);
|
2017-06-12 22:34:10 +03:00
|
|
|
sMainLoop->PostTask(
|
|
|
|
NewRunnableMethod("gmp::GMPRunnable::Run", r, &GMPRunnable::Run));
|
2014-05-18 07:05:46 +04:00
|
|
|
|
|
|
|
return GMPNoErr;
|
|
|
|
}
|
|
|
|
|
|
|
|
GMPErr
|
|
|
|
SyncRunOnMainThread(GMPTask* aTask)
|
|
|
|
{
|
2014-08-11 06:18:53 +04:00
|
|
|
if (!aTask || !sMainLoop || IsOnChildMainThread()) {
|
2014-05-18 07:05:46 +04:00
|
|
|
return GMPGenericErr;
|
|
|
|
}
|
|
|
|
|
2016-05-03 05:37:24 +03:00
|
|
|
RefPtr<GMPSyncRunnable> r = new GMPSyncRunnable(aTask, sMainLoop);
|
2014-05-18 07:05:46 +04:00
|
|
|
|
|
|
|
r->Post();
|
|
|
|
|
|
|
|
return GMPNoErr;
|
|
|
|
}
|
|
|
|
|
2017-02-14 01:09:38 +03:00
|
|
|
class GMPMutexImpl : public GMPMutex
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
GMPMutexImpl();
|
|
|
|
virtual ~GMPMutexImpl();
|
|
|
|
|
|
|
|
// GMPMutex
|
|
|
|
void Acquire() override;
|
|
|
|
void Release() override;
|
|
|
|
void Destroy() override;
|
|
|
|
|
|
|
|
private:
|
|
|
|
ReentrantMonitor mMonitor;
|
|
|
|
};
|
|
|
|
|
2014-05-18 07:05:46 +04:00
|
|
|
GMPErr
|
|
|
|
CreateMutex(GMPMutex** aMutex)
|
|
|
|
{
|
|
|
|
if (!aMutex) {
|
|
|
|
return GMPGenericErr;
|
|
|
|
}
|
|
|
|
|
|
|
|
*aMutex = new GMPMutexImpl();
|
|
|
|
|
|
|
|
return GMPNoErr;
|
|
|
|
}
|
|
|
|
|
2014-08-19 12:56:33 +04:00
|
|
|
GMPErr
|
|
|
|
CreateRecord(const char* aRecordName,
|
|
|
|
uint32_t aRecordNameSize,
|
|
|
|
GMPRecord** aOutRecord,
|
|
|
|
GMPRecordClient* aClient)
|
|
|
|
{
|
2015-08-11 07:05:19 +03:00
|
|
|
if (aRecordNameSize > GMP_MAX_RECORD_NAME_SIZE ||
|
|
|
|
aRecordNameSize == 0) {
|
|
|
|
NS_WARNING("GMP tried to CreateRecord with too long or 0 record name");
|
2014-08-19 12:56:33 +04:00
|
|
|
return GMPGenericErr;
|
|
|
|
}
|
|
|
|
GMPStorageChild* storage = sChild->GetGMPStorage();
|
|
|
|
if (!storage) {
|
|
|
|
return GMPGenericErr;
|
|
|
|
}
|
|
|
|
MOZ_ASSERT(storage);
|
|
|
|
return storage->CreateRecord(nsDependentCString(aRecordName, aRecordNameSize),
|
|
|
|
aOutRecord,
|
|
|
|
aClient);
|
|
|
|
}
|
|
|
|
|
2014-08-05 11:56:05 +04:00
|
|
|
GMPErr
|
|
|
|
SetTimerOnMainThread(GMPTask* aTask, int64_t aTimeoutMS)
|
|
|
|
{
|
2014-08-11 06:18:53 +04:00
|
|
|
if (!aTask || !sMainLoop || !IsOnChildMainThread()) {
|
|
|
|
return GMPGenericErr;
|
|
|
|
}
|
2014-08-05 11:56:05 +04:00
|
|
|
GMPTimerChild* timers = sChild->GetGMPTimers();
|
|
|
|
NS_ENSURE_TRUE(timers, GMPGenericErr);
|
|
|
|
return timers->SetTimer(aTask, aTimeoutMS);
|
|
|
|
}
|
|
|
|
|
|
|
|
GMPErr
|
|
|
|
GetClock(GMPTimestamp* aOutTime)
|
|
|
|
{
|
2017-02-27 00:42:59 +03:00
|
|
|
if (!aOutTime) {
|
|
|
|
return GMPGenericErr;
|
|
|
|
}
|
|
|
|
*aOutTime = base::Time::Now().ToDoubleT() * 1000.0;
|
|
|
|
return GMPNoErr;
|
2014-08-05 11:56:05 +04:00
|
|
|
}
|
|
|
|
|
2014-05-18 07:05:46 +04:00
|
|
|
void
|
2014-08-05 11:56:05 +04:00
|
|
|
InitPlatformAPI(GMPPlatformAPI& aPlatformAPI, GMPChild* aChild)
|
2014-05-18 07:05:46 +04:00
|
|
|
{
|
|
|
|
if (!sMainLoop) {
|
|
|
|
sMainLoop = MessageLoop::current();
|
|
|
|
}
|
2014-08-05 11:56:05 +04:00
|
|
|
if (!sChild) {
|
|
|
|
sChild = aChild;
|
|
|
|
}
|
2014-05-18 07:05:46 +04:00
|
|
|
|
|
|
|
aPlatformAPI.version = 0;
|
|
|
|
aPlatformAPI.createthread = &CreateThread;
|
|
|
|
aPlatformAPI.runonmainthread = &RunOnMainThread;
|
|
|
|
aPlatformAPI.syncrunonmainthread = &SyncRunOnMainThread;
|
|
|
|
aPlatformAPI.createmutex = &CreateMutex;
|
2014-08-19 12:56:33 +04:00
|
|
|
aPlatformAPI.createrecord = &CreateRecord;
|
2014-08-05 11:56:05 +04:00
|
|
|
aPlatformAPI.settimer = &SetTimerOnMainThread;
|
|
|
|
aPlatformAPI.getcurrenttime = &GetClock;
|
2014-05-18 07:05:46 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
GMPThreadImpl::GMPThreadImpl()
|
|
|
|
: mMutex("GMPThreadImpl"),
|
|
|
|
mThread("GMPThread")
|
|
|
|
{
|
2014-08-05 11:56:04 +04:00
|
|
|
MOZ_COUNT_CTOR(GMPThread);
|
2014-05-18 07:05:46 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
GMPThreadImpl::~GMPThreadImpl()
|
|
|
|
{
|
2014-08-05 11:56:04 +04:00
|
|
|
MOZ_COUNT_DTOR(GMPThread);
|
2014-05-18 07:05:46 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
GMPThreadImpl::Post(GMPTask* aTask)
|
|
|
|
{
|
|
|
|
MutexAutoLock lock(mMutex);
|
|
|
|
|
|
|
|
if (!mThread.IsRunning()) {
|
|
|
|
bool started = mThread.Start();
|
|
|
|
if (!started) {
|
|
|
|
NS_WARNING("Unable to start GMPThread!");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-03 05:37:24 +03:00
|
|
|
RefPtr<GMPRunnable> r = new GMPRunnable(aTask);
|
2017-06-12 22:34:10 +03:00
|
|
|
mThread.message_loop()->PostTask(
|
|
|
|
NewRunnableMethod("gmp::GMPRunnable::Run", r.get(), &GMPRunnable::Run));
|
2014-05-18 07:05:46 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
GMPThreadImpl::Join()
|
|
|
|
{
|
2014-08-05 11:56:04 +04:00
|
|
|
{
|
|
|
|
MutexAutoLock lock(mMutex);
|
|
|
|
if (mThread.IsRunning()) {
|
|
|
|
mThread.Stop();
|
|
|
|
}
|
2014-05-18 07:05:46 +04:00
|
|
|
}
|
2014-08-05 11:56:04 +04:00
|
|
|
delete this;
|
2014-05-18 07:05:46 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
GMPMutexImpl::GMPMutexImpl()
|
2014-09-16 12:15:14 +04:00
|
|
|
: mMonitor("gmp-mutex")
|
2014-05-18 07:05:46 +04:00
|
|
|
{
|
2014-08-05 11:56:04 +04:00
|
|
|
MOZ_COUNT_CTOR(GMPMutexImpl);
|
2014-05-18 07:05:46 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
GMPMutexImpl::~GMPMutexImpl()
|
|
|
|
{
|
2014-08-05 11:56:04 +04:00
|
|
|
MOZ_COUNT_DTOR(GMPMutexImpl);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
GMPMutexImpl::Destroy()
|
|
|
|
{
|
|
|
|
delete this;
|
2014-05-18 07:05:46 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
GMPMutexImpl::Acquire()
|
|
|
|
{
|
2014-09-16 12:38:21 +04:00
|
|
|
mMonitor.Enter();
|
2014-05-18 07:05:46 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
GMPMutexImpl::Release()
|
|
|
|
{
|
2014-09-16 12:38:21 +04:00
|
|
|
mMonitor.Exit();
|
2014-05-18 07:05:46 +04:00
|
|
|
}
|
|
|
|
|
2017-02-14 01:09:38 +03:00
|
|
|
GMPTask*
|
|
|
|
NewGMPTask(std::function<void()>&& aFunction)
|
|
|
|
{
|
|
|
|
class Task : public GMPTask
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
explicit Task(std::function<void()>&& aFunction)
|
|
|
|
: mFunction(Move(aFunction))
|
|
|
|
{
|
|
|
|
}
|
|
|
|
void Destroy() override
|
|
|
|
{
|
|
|
|
delete this;
|
|
|
|
}
|
|
|
|
~Task() override
|
|
|
|
{
|
|
|
|
}
|
|
|
|
void Run() override
|
|
|
|
{
|
|
|
|
mFunction();
|
|
|
|
}
|
|
|
|
private:
|
|
|
|
std::function<void()> mFunction;
|
|
|
|
};
|
|
|
|
return new Task(Move(aFunction));
|
|
|
|
}
|
|
|
|
|
2014-05-18 07:05:46 +04:00
|
|
|
} // namespace gmp
|
|
|
|
} // namespace mozilla
|