2016-10-26 00:04:47 +03:00
|
|
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
|
|
|
|
/* 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 "mozilla/mscom/ActivationContext.h"
|
|
|
|
|
|
|
|
#include "mozilla/Assertions.h"
|
|
|
|
#include "mozilla/DebugOnly.h"
|
2017-06-13 23:50:05 +03:00
|
|
|
#include "mozilla/mscom/Utils.h"
|
2016-10-26 00:04:47 +03:00
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
namespace mscom {
|
|
|
|
|
2017-06-13 23:50:05 +03:00
|
|
|
ActivationContext::ActivationContext(WORD aResourceId)
|
|
|
|
: ActivationContext(reinterpret_cast<HMODULE>(GetContainingModuleHandle()),
|
|
|
|
aResourceId)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
ActivationContext::ActivationContext(HMODULE aLoadFromModule, WORD aResourceId)
|
2016-10-26 00:04:47 +03:00
|
|
|
: mActCtx(INVALID_HANDLE_VALUE)
|
|
|
|
{
|
|
|
|
ACTCTX actCtx = {sizeof(actCtx)};
|
|
|
|
actCtx.dwFlags = ACTCTX_FLAG_RESOURCE_NAME_VALID | ACTCTX_FLAG_HMODULE_VALID;
|
2017-06-13 23:50:05 +03:00
|
|
|
actCtx.lpResourceName = MAKEINTRESOURCE(aResourceId);
|
2016-10-26 00:04:47 +03:00
|
|
|
actCtx.hModule = aLoadFromModule;
|
|
|
|
|
2017-06-13 23:50:05 +03:00
|
|
|
Init(actCtx);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ActivationContext::Init(ACTCTX& aActCtx)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(mActCtx == INVALID_HANDLE_VALUE);
|
|
|
|
mActCtx = ::CreateActCtx(&aActCtx);
|
2016-10-26 00:04:47 +03:00
|
|
|
MOZ_ASSERT(mActCtx != INVALID_HANDLE_VALUE);
|
2017-06-13 23:50:05 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ActivationContext::AddRef()
|
|
|
|
{
|
2016-10-26 00:04:47 +03:00
|
|
|
if (mActCtx == INVALID_HANDLE_VALUE) {
|
|
|
|
return;
|
|
|
|
}
|
2017-06-13 23:50:05 +03:00
|
|
|
::AddRefActCtx(mActCtx);
|
|
|
|
}
|
|
|
|
|
|
|
|
ActivationContext::ActivationContext(ActivationContext&& aOther)
|
|
|
|
: mActCtx(aOther.mActCtx)
|
|
|
|
{
|
|
|
|
aOther.mActCtx = INVALID_HANDLE_VALUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
ActivationContext&
|
|
|
|
ActivationContext::operator=(ActivationContext&& aOther)
|
|
|
|
{
|
|
|
|
Release();
|
|
|
|
|
|
|
|
mActCtx = aOther.mActCtx;
|
|
|
|
aOther.mActCtx = INVALID_HANDLE_VALUE;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
ActivationContext::ActivationContext(const ActivationContext& aOther)
|
|
|
|
: mActCtx(aOther.mActCtx)
|
|
|
|
{
|
|
|
|
AddRef();
|
|
|
|
}
|
|
|
|
|
|
|
|
ActivationContext&
|
|
|
|
ActivationContext::operator=(const ActivationContext& aOther)
|
|
|
|
{
|
|
|
|
Release();
|
|
|
|
mActCtx = aOther.mActCtx;
|
|
|
|
AddRef();
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ActivationContext::Release()
|
|
|
|
{
|
|
|
|
if (mActCtx == INVALID_HANDLE_VALUE) {
|
|
|
|
return;
|
2016-10-26 00:04:47 +03:00
|
|
|
}
|
2017-06-13 23:50:05 +03:00
|
|
|
::ReleaseActCtx(mActCtx);
|
|
|
|
mActCtx = INVALID_HANDLE_VALUE;
|
2016-10-26 00:04:47 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
ActivationContext::~ActivationContext()
|
|
|
|
{
|
2017-06-13 23:50:05 +03:00
|
|
|
Release();
|
|
|
|
}
|
|
|
|
|
2017-09-19 23:35:27 +03:00
|
|
|
#if defined(MOZILLA_INTERNAL_API)
|
|
|
|
|
2017-08-29 00:16:20 +03:00
|
|
|
/* static */ Result<uintptr_t,HRESULT>
|
|
|
|
ActivationContext::GetCurrent()
|
|
|
|
{
|
|
|
|
HANDLE actCtx;
|
|
|
|
if (!::GetCurrentActCtx(&actCtx)) {
|
|
|
|
return Result<uintptr_t,HRESULT>(HRESULT_FROM_WIN32(::GetLastError()));
|
|
|
|
}
|
|
|
|
|
|
|
|
return reinterpret_cast<uintptr_t>(actCtx);
|
|
|
|
}
|
|
|
|
|
2017-09-19 23:35:27 +03:00
|
|
|
/* static */ HRESULT
|
|
|
|
ActivationContext::GetCurrentManifestPath(nsAString& aOutManifestPath)
|
|
|
|
{
|
|
|
|
aOutManifestPath.Truncate();
|
|
|
|
|
|
|
|
SIZE_T bytesNeeded;
|
|
|
|
BOOL ok = ::QueryActCtxW(QUERY_ACTCTX_FLAG_USE_ACTIVE_ACTCTX, nullptr,
|
|
|
|
nullptr, ActivationContextDetailedInformation,
|
|
|
|
nullptr, 0, &bytesNeeded);
|
|
|
|
if (!ok) {
|
|
|
|
DWORD err = ::GetLastError();
|
|
|
|
if (err != ERROR_INSUFFICIENT_BUFFER) {
|
|
|
|
return HRESULT_FROM_WIN32(err);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
auto ctxBuf = MakeUnique<BYTE[]>(bytesNeeded);
|
|
|
|
|
|
|
|
ok = ::QueryActCtxW(QUERY_ACTCTX_FLAG_USE_ACTIVE_ACTCTX, nullptr, nullptr,
|
|
|
|
ActivationContextDetailedInformation, ctxBuf.get(),
|
|
|
|
bytesNeeded, nullptr);
|
|
|
|
if (!ok) {
|
|
|
|
return HRESULT_FROM_WIN32(::GetLastError());
|
|
|
|
}
|
|
|
|
|
|
|
|
auto ctxInfo =
|
|
|
|
reinterpret_cast<ACTIVATION_CONTEXT_DETAILED_INFORMATION*>(ctxBuf.get());
|
|
|
|
|
|
|
|
// assemblyIndex is 1-based, and we want the last index, so we can just copy
|
|
|
|
// ctxInfo->ulAssemblyCount directly.
|
|
|
|
DWORD assemblyIndex = ctxInfo->ulAssemblyCount;
|
|
|
|
ok = ::QueryActCtxW(QUERY_ACTCTX_FLAG_USE_ACTIVE_ACTCTX, nullptr,
|
|
|
|
&assemblyIndex,
|
|
|
|
AssemblyDetailedInformationInActivationContext, nullptr,
|
|
|
|
0, &bytesNeeded);
|
|
|
|
if (!ok) {
|
|
|
|
DWORD err = ::GetLastError();
|
|
|
|
if (err != ERROR_INSUFFICIENT_BUFFER) {
|
|
|
|
return HRESULT_FROM_WIN32(err);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
auto assemblyBuf = MakeUnique<BYTE[]>(bytesNeeded);
|
|
|
|
|
|
|
|
ok = ::QueryActCtxW(QUERY_ACTCTX_FLAG_USE_ACTIVE_ACTCTX, nullptr,
|
|
|
|
&assemblyIndex,
|
|
|
|
AssemblyDetailedInformationInActivationContext,
|
|
|
|
assemblyBuf.get(), bytesNeeded, &bytesNeeded);
|
|
|
|
if (!ok) {
|
|
|
|
return HRESULT_FROM_WIN32(::GetLastError());
|
|
|
|
}
|
|
|
|
|
|
|
|
auto assemblyInfo =
|
|
|
|
reinterpret_cast<ACTIVATION_CONTEXT_ASSEMBLY_DETAILED_INFORMATION*>(assemblyBuf.get());
|
|
|
|
aOutManifestPath = nsDependentString(assemblyInfo->lpAssemblyManifestPath,
|
|
|
|
(assemblyInfo->ulManifestPathLength + 1) / sizeof(wchar_t));
|
|
|
|
|
|
|
|
return S_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif // defined(MOZILLA_INTERNAL_API)
|
|
|
|
|
2017-09-30 00:41:28 +03:00
|
|
|
ActivationContextRegion::ActivationContextRegion()
|
|
|
|
: mActCookie(0)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2017-06-13 23:50:05 +03:00
|
|
|
ActivationContextRegion::ActivationContextRegion(const ActivationContext& aActCtx)
|
|
|
|
: mActCtx(aActCtx)
|
|
|
|
, mActCookie(0)
|
|
|
|
{
|
|
|
|
Activate();
|
|
|
|
}
|
|
|
|
|
2017-09-30 00:41:28 +03:00
|
|
|
ActivationContextRegion&
|
|
|
|
ActivationContextRegion::operator=(const ActivationContext& aActCtx)
|
|
|
|
{
|
|
|
|
Deactivate();
|
|
|
|
mActCtx = aActCtx;
|
|
|
|
Activate();
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2017-06-13 23:50:05 +03:00
|
|
|
ActivationContextRegion::ActivationContextRegion(ActivationContext&& aActCtx)
|
2018-05-30 22:15:35 +03:00
|
|
|
: mActCtx(std::move(aActCtx))
|
2017-06-13 23:50:05 +03:00
|
|
|
, mActCookie(0)
|
|
|
|
{
|
|
|
|
Activate();
|
|
|
|
}
|
|
|
|
|
2017-09-30 00:41:28 +03:00
|
|
|
ActivationContextRegion&
|
|
|
|
ActivationContextRegion::operator=(ActivationContext&& aActCtx)
|
|
|
|
{
|
|
|
|
Deactivate();
|
2018-05-30 22:15:35 +03:00
|
|
|
mActCtx = std::move(aActCtx);
|
2017-09-30 00:41:28 +03:00
|
|
|
Activate();
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
ActivationContextRegion::ActivationContextRegion(ActivationContextRegion&& aRgn)
|
2018-05-30 22:15:35 +03:00
|
|
|
: mActCtx(std::move(aRgn.mActCtx))
|
2017-09-30 00:41:28 +03:00
|
|
|
, mActCookie(aRgn.mActCookie)
|
|
|
|
{
|
|
|
|
aRgn.mActCookie = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
ActivationContextRegion&
|
|
|
|
ActivationContextRegion::operator=(ActivationContextRegion&& aRgn)
|
|
|
|
{
|
|
|
|
Deactivate();
|
2018-05-30 22:15:35 +03:00
|
|
|
mActCtx = std::move(aRgn.mActCtx);
|
2017-09-30 00:41:28 +03:00
|
|
|
mActCookie = aRgn.mActCookie;
|
|
|
|
aRgn.mActCookie = 0;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2017-06-13 23:50:05 +03:00
|
|
|
void
|
|
|
|
ActivationContextRegion::Activate()
|
|
|
|
{
|
|
|
|
if (mActCtx.mActCtx == INVALID_HANDLE_VALUE) {
|
2017-06-09 03:07:24 +03:00
|
|
|
return;
|
|
|
|
}
|
2017-06-13 23:50:05 +03:00
|
|
|
|
2017-09-30 00:41:28 +03:00
|
|
|
BOOL activated = ::ActivateActCtx(mActCtx.mActCtx, &mActCookie);
|
|
|
|
MOZ_DIAGNOSTIC_ASSERT(activated);
|
2017-06-13 23:50:05 +03:00
|
|
|
}
|
|
|
|
|
2017-09-30 00:41:28 +03:00
|
|
|
bool
|
|
|
|
ActivationContextRegion::Deactivate()
|
2017-06-13 23:50:05 +03:00
|
|
|
{
|
|
|
|
if (!mActCookie) {
|
2017-09-30 00:41:28 +03:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
BOOL deactivated = ::DeactivateActCtx(0, mActCookie);
|
|
|
|
MOZ_DIAGNOSTIC_ASSERT(deactivated);
|
|
|
|
if (deactivated) {
|
|
|
|
mActCookie = 0;
|
2017-06-13 23:50:05 +03:00
|
|
|
}
|
|
|
|
|
2017-09-30 00:41:28 +03:00
|
|
|
return !!deactivated;
|
|
|
|
}
|
|
|
|
|
|
|
|
ActivationContextRegion::~ActivationContextRegion()
|
|
|
|
{
|
|
|
|
Deactivate();
|
2016-10-26 00:04:47 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace mscom
|
|
|
|
} // namespace mozilla
|