2014-11-14 11:26:24 +03:00
|
|
|
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
|
|
|
|
* vim: sw=4 ts=4 et :
|
|
|
|
* 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 "GMPLoader.h"
|
|
|
|
#include <stdio.h>
|
|
|
|
#include "mozilla/Attributes.h"
|
|
|
|
#include "gmp-entrypoints.h"
|
|
|
|
#include "prlink.h"
|
2015-06-10 05:42:10 +03:00
|
|
|
#include "prenv.h"
|
2014-11-14 11:26:24 +03:00
|
|
|
|
|
|
|
#include <string>
|
|
|
|
|
2015-07-29 13:27:07 +03:00
|
|
|
#ifdef XP_WIN
|
2014-11-14 11:39:24 +03:00
|
|
|
#include "windows.h"
|
2015-10-29 18:27:00 +03:00
|
|
|
#endif
|
|
|
|
|
2016-05-11 10:56:42 +03:00
|
|
|
#include "GMPDeviceBinding.h"
|
2014-11-14 11:39:18 +03:00
|
|
|
|
2014-11-14 11:26:24 +03:00
|
|
|
namespace mozilla {
|
|
|
|
namespace gmp {
|
|
|
|
|
|
|
|
class GMPLoaderImpl : public GMPLoader {
|
|
|
|
public:
|
|
|
|
explicit GMPLoaderImpl(SandboxStarter* aStarter)
|
|
|
|
: mSandboxStarter(aStarter)
|
2016-04-12 07:12:20 +03:00
|
|
|
, mAdapter(nullptr)
|
2014-11-14 11:26:24 +03:00
|
|
|
{}
|
|
|
|
virtual ~GMPLoaderImpl() {}
|
|
|
|
|
2016-01-18 06:40:49 +03:00
|
|
|
bool Load(const char* aUTF8LibPath,
|
|
|
|
uint32_t aUTF8LibPathLen,
|
|
|
|
char* aOriginSalt,
|
|
|
|
uint32_t aOriginSaltLen,
|
2016-04-12 07:12:20 +03:00
|
|
|
const GMPPlatformAPI* aPlatformAPI,
|
|
|
|
GMPAdapter* aAdapter) override;
|
2014-11-14 11:26:24 +03:00
|
|
|
|
2016-01-18 06:40:49 +03:00
|
|
|
GMPErr GetAPI(const char* aAPIName,
|
|
|
|
void* aHostAPI,
|
2016-11-14 06:07:23 +03:00
|
|
|
void** aPluginAPI) override;
|
2014-11-14 11:26:24 +03:00
|
|
|
|
2016-01-18 06:40:49 +03:00
|
|
|
void Shutdown() override;
|
2014-11-14 11:26:24 +03:00
|
|
|
|
2015-05-29 19:07:06 +03:00
|
|
|
#if defined(XP_MACOSX) && defined(MOZ_GMP_SANDBOX)
|
2016-01-18 06:40:49 +03:00
|
|
|
void SetSandboxInfo(MacSandboxInfo* aSandboxInfo) override;
|
2014-11-14 11:26:24 +03:00
|
|
|
#endif
|
|
|
|
|
|
|
|
private:
|
|
|
|
SandboxStarter* mSandboxStarter;
|
2016-04-12 07:12:20 +03:00
|
|
|
UniquePtr<GMPAdapter> mAdapter;
|
2014-11-14 11:26:24 +03:00
|
|
|
};
|
|
|
|
|
2016-05-15 18:35:22 +03:00
|
|
|
UniquePtr<GMPLoader> CreateGMPLoader(SandboxStarter* aStarter) {
|
|
|
|
return MakeUnique<GMPLoaderImpl>(aStarter);
|
2014-11-14 11:26:24 +03:00
|
|
|
}
|
|
|
|
|
2016-04-12 07:12:20 +03:00
|
|
|
class PassThroughGMPAdapter : public GMPAdapter {
|
|
|
|
public:
|
|
|
|
~PassThroughGMPAdapter() {
|
|
|
|
// Ensure we're always shutdown, even if caller forgets to call GMPShutdown().
|
|
|
|
GMPShutdown();
|
|
|
|
}
|
|
|
|
|
|
|
|
void SetAdaptee(PRLibrary* aLib) override
|
|
|
|
{
|
|
|
|
mLib = aLib;
|
|
|
|
}
|
|
|
|
|
|
|
|
GMPErr GMPInit(const GMPPlatformAPI* aPlatformAPI) override
|
|
|
|
{
|
|
|
|
if (!mLib) {
|
|
|
|
return GMPGenericErr;
|
|
|
|
}
|
|
|
|
GMPInitFunc initFunc = reinterpret_cast<GMPInitFunc>(PR_FindFunctionSymbol(mLib, "GMPInit"));
|
|
|
|
if (!initFunc) {
|
|
|
|
return GMPNotImplementedErr;
|
|
|
|
}
|
|
|
|
return initFunc(aPlatformAPI);
|
|
|
|
}
|
|
|
|
|
2016-11-14 06:07:23 +03:00
|
|
|
GMPErr GMPGetAPI(const char* aAPIName, void* aHostAPI, void** aPluginAPI) override
|
2016-04-12 07:12:20 +03:00
|
|
|
{
|
|
|
|
if (!mLib) {
|
|
|
|
return GMPGenericErr;
|
|
|
|
}
|
|
|
|
GMPGetAPIFunc getapiFunc = reinterpret_cast<GMPGetAPIFunc>(PR_FindFunctionSymbol(mLib, "GMPGetAPI"));
|
|
|
|
if (!getapiFunc) {
|
|
|
|
return GMPNotImplementedErr;
|
|
|
|
}
|
|
|
|
return getapiFunc(aAPIName, aHostAPI, aPluginAPI);
|
|
|
|
}
|
|
|
|
|
|
|
|
void GMPShutdown() override
|
|
|
|
{
|
|
|
|
if (mLib) {
|
|
|
|
GMPShutdownFunc shutdownFunc = reinterpret_cast<GMPShutdownFunc>(PR_FindFunctionSymbol(mLib, "GMPShutdown"));
|
|
|
|
if (shutdownFunc) {
|
|
|
|
shutdownFunc();
|
|
|
|
}
|
|
|
|
PR_UnloadLibrary(mLib);
|
|
|
|
mLib = nullptr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void GMPSetNodeId(const char* aNodeId, uint32_t aLength) override
|
|
|
|
{
|
|
|
|
if (!mLib) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
GMPSetNodeIdFunc setNodeIdFunc = reinterpret_cast<GMPSetNodeIdFunc>(PR_FindFunctionSymbol(mLib, "GMPSetNodeId"));
|
|
|
|
if (setNodeIdFunc) {
|
|
|
|
setNodeIdFunc(aNodeId, aLength);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
PRLibrary* mLib = nullptr;
|
|
|
|
};
|
|
|
|
|
2014-11-14 11:26:24 +03:00
|
|
|
bool
|
2015-07-17 02:09:49 +03:00
|
|
|
GMPLoaderImpl::Load(const char* aUTF8LibPath,
|
|
|
|
uint32_t aUTF8LibPathLen,
|
2014-11-14 11:26:24 +03:00
|
|
|
char* aOriginSalt,
|
|
|
|
uint32_t aOriginSaltLen,
|
2016-04-12 07:12:20 +03:00
|
|
|
const GMPPlatformAPI* aPlatformAPI,
|
|
|
|
GMPAdapter* aAdapter)
|
2014-11-14 11:26:24 +03:00
|
|
|
{
|
2014-11-14 11:39:18 +03:00
|
|
|
std::string nodeId;
|
2016-05-11 10:56:42 +03:00
|
|
|
if (!CalculateGMPDeviceId(aOriginSalt, aOriginSaltLen, nodeId)) {
|
|
|
|
return false;
|
2014-11-14 11:39:18 +03:00
|
|
|
}
|
2014-11-14 11:26:24 +03:00
|
|
|
|
2015-10-21 10:46:57 +03:00
|
|
|
// Start the sandbox now that we've generated the device bound node id.
|
|
|
|
// This must happen after the node id is bound to the device id, as
|
|
|
|
// generating the device id requires privileges.
|
|
|
|
if (mSandboxStarter && !mSandboxStarter->Start(aUTF8LibPath)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Load the GMP.
|
|
|
|
PRLibSpec libSpec;
|
2015-07-29 13:27:07 +03:00
|
|
|
#ifdef XP_WIN
|
2015-07-17 02:09:49 +03:00
|
|
|
int pathLen = MultiByteToWideChar(CP_UTF8, 0, aUTF8LibPath, -1, nullptr, 0);
|
2015-01-26 13:14:39 +03:00
|
|
|
if (pathLen == 0) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2015-12-03 02:07:59 +03:00
|
|
|
auto widePath = MakeUnique<wchar_t[]>(pathLen);
|
|
|
|
if (MultiByteToWideChar(CP_UTF8, 0, aUTF8LibPath, -1, widePath.get(), pathLen) == 0) {
|
2015-01-26 13:14:39 +03:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2015-12-03 02:07:59 +03:00
|
|
|
libSpec.value.pathname_u = widePath.get();
|
2015-07-17 02:09:49 +03:00
|
|
|
libSpec.type = PR_LibSpec_PathnameU;
|
|
|
|
#else
|
|
|
|
libSpec.value.pathname = aUTF8LibPath;
|
2014-11-14 11:26:24 +03:00
|
|
|
libSpec.type = PR_LibSpec_Pathname;
|
2015-07-17 02:09:49 +03:00
|
|
|
#endif
|
2016-04-12 07:12:20 +03:00
|
|
|
PRLibrary* lib = PR_LoadLibraryWithFlags(libSpec, 0);
|
|
|
|
if (!lib) {
|
2014-11-14 11:26:24 +03:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-04-12 07:12:20 +03:00
|
|
|
GMPInitFunc initFunc = reinterpret_cast<GMPInitFunc>(PR_FindFunctionSymbol(lib, "GMPInit"));
|
|
|
|
if ((initFunc && aAdapter) ||
|
|
|
|
(!initFunc && !aAdapter)) {
|
|
|
|
// Ensure that if we're dealing with a GMP we do *not* use an adapter
|
|
|
|
// provided from the outside world. This is important as it means we
|
|
|
|
// don't call code not covered by Adobe's plugin-container voucher
|
|
|
|
// before we pass the node Id to Adobe's GMP.
|
2014-11-14 11:26:24 +03:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-04-12 07:12:20 +03:00
|
|
|
// Note: PassThroughGMPAdapter's code must remain in this file so that it's
|
|
|
|
// covered by Adobe's plugin-container voucher.
|
|
|
|
mAdapter.reset((!aAdapter) ? new PassThroughGMPAdapter() : aAdapter);
|
|
|
|
mAdapter->SetAdaptee(lib);
|
2014-11-14 11:26:24 +03:00
|
|
|
|
2016-05-06 05:49:12 +03:00
|
|
|
if (mAdapter->GMPInit(aPlatformAPI) != GMPNoErr) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
mAdapter->GMPSetNodeId(nodeId.c_str(), nodeId.size());
|
2014-11-14 11:26:24 +03:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
GMPErr
|
|
|
|
GMPLoaderImpl::GetAPI(const char* aAPIName,
|
|
|
|
void* aHostAPI,
|
2016-11-14 06:07:23 +03:00
|
|
|
void** aPluginAPI)
|
2014-11-14 11:26:24 +03:00
|
|
|
{
|
2016-11-14 06:07:23 +03:00
|
|
|
return mAdapter->GMPGetAPI(aAPIName, aHostAPI, aPluginAPI);
|
2014-11-14 11:26:24 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
GMPLoaderImpl::Shutdown()
|
|
|
|
{
|
2016-04-12 07:12:20 +03:00
|
|
|
if (mAdapter) {
|
|
|
|
mAdapter->GMPShutdown();
|
2014-11-14 11:26:24 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-05-29 19:07:06 +03:00
|
|
|
#if defined(XP_MACOSX) && defined(MOZ_GMP_SANDBOX)
|
2015-04-03 19:51:41 +03:00
|
|
|
void
|
|
|
|
GMPLoaderImpl::SetSandboxInfo(MacSandboxInfo* aSandboxInfo)
|
|
|
|
{
|
|
|
|
if (mSandboxStarter) {
|
|
|
|
mSandboxStarter->SetSandboxInfo(aSandboxInfo);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
2014-11-14 11:26:24 +03:00
|
|
|
} // namespace gmp
|
|
|
|
} // namespace mozilla
|
|
|
|
|