Bug 1774083 - Part 1: Add notificationserver.dll COM Server to handle Window's toast notifications. r=nalexander

This implements a COM Server and returns objects implementing INotificationActivationCallback. This allows Firefox notifications to be acted upon even after the main process exits.

COM objects require a (normally static) CLSID in the registry to be identified by other apps. To prevent CLSID duplication between parallel installs and portable/development builds, this implementation inspects the registry when a COM object CLSID is requested, and returns an object if the CLSID's InprocServer32 key matches the path of the DLL.

Differential Revision: https://phabricator.services.mozilla.com/D149182
This commit is contained in:
Nicholas Rishel 2022-08-02 19:40:39 +00:00
parent 49ce4aba64
commit 94c7867e54
9 changed files with 310 additions and 2 deletions

View file

@ -415,6 +415,12 @@ bin/libfreebl_64int_3.so
;
@BINPATH@/pingsender@BIN_SUFFIX@
; [ Notification COM Server ]
;
#if defined(XP_WIN)
@BINPATH@/@DLL_PREFIX@notificationserver@DLL_SUFFIX@
#endif
; Shutdown Terminator
@RESPATH@/components/terminator.manifest

View file

@ -24,8 +24,10 @@ DIRS += [
"themes",
]
if CONFIG["OS_ARCH"] == "WINNT" and CONFIG["MOZ_DEFAULT_BROWSER_AGENT"]:
DIRS += ["mozapps/defaultagent"]
if CONFIG["OS_ARCH"] == "WINNT":
DIRS += ["mozapps/notificationserver"]
if CONFIG["MOZ_DEFAULT_BROWSER_AGENT"]:
DIRS += ["mozapps/defaultagent"]
if CONFIG["MOZ_UPDATER"] and CONFIG["MOZ_WIDGET_TOOLKIT"] != "android":
DIRS += ["mozapps/update"]

View file

@ -0,0 +1,35 @@
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=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 "NotificationCallback.h"
#include <sstream>
#include <string>
HRESULT STDMETHODCALLTYPE
NotificationCallback::QueryInterface(REFIID riid, void** ppvObject) {
if (!ppvObject) {
return E_POINTER;
}
*ppvObject = nullptr;
if (!(riid == guid || riid == __uuidof(INotificationActivationCallback) ||
riid == __uuidof(IUnknown))) {
return E_NOINTERFACE;
}
AddRef();
*ppvObject = reinterpret_cast<void*>(this);
return S_OK;
}
HRESULT STDMETHODCALLTYPE NotificationCallback::Activate(
LPCWSTR appUserModelId, LPCWSTR invokedArgs,
const NOTIFICATION_USER_INPUT_DATA* data, ULONG dataCount) {
return S_OK;
}

View file

@ -0,0 +1,53 @@
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=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/. */
#ifndef NotificationCallback_h__
#define NotificationCallback_h__
#include <filesystem>
#include <unknwn.h>
#include <wrl.h>
using namespace std::filesystem;
using namespace Microsoft::WRL;
// Windows 10+ declarations.
// TODO remove declarations and add `#include
// <notificationactivationcallback.h>` when Windows 10 is the minimum supported.
typedef struct NOTIFICATION_USER_INPUT_DATA {
LPCWSTR Key;
LPCWSTR Value;
} NOTIFICATION_USER_INPUT_DATA;
MIDL_INTERFACE("53E31837-6600-4A81-9395-75CFFE746F94")
INotificationActivationCallback : public IUnknown {
public:
virtual HRESULT STDMETHODCALLTYPE Activate(
LPCWSTR appUserModelId, LPCWSTR invokedArgs,
const NOTIFICATION_USER_INPUT_DATA* data, ULONG count) = 0;
};
class NotificationCallback final
: public RuntimeClass<RuntimeClassFlags<ClassicCom>,
INotificationActivationCallback> {
public:
HRESULT STDMETHODCALLTYPE QueryInterface(REFIID riid, void** ppvObject) final;
HRESULT STDMETHODCALLTYPE Activate(LPCWSTR appUserModelId,
LPCWSTR invokedArgs,
const NOTIFICATION_USER_INPUT_DATA* data,
ULONG dataCount) final;
explicit NotificationCallback(const GUID& runtimeGuid,
const path& dllInstallDir)
: guid(runtimeGuid), installDir(dllInstallDir) {}
private:
const GUID guid = {};
const path installDir = {};
};
#endif

View file

@ -0,0 +1,117 @@
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=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 <filesystem>
#include <string>
#include "NotificationFactory.h"
using namespace std::filesystem;
static path processDllPath = {};
// Populate the path to this DLL.
bool PopulateDllPath(HINSTANCE dllInstance) {
std::vector<wchar_t> path(MAX_PATH, 0);
DWORD charsWritten =
GetModuleFileNameW(dllInstance, path.data(), path.size());
// GetModuleFileNameW returns the count of characters written including null
// when truncated, excluding null otherwise. Therefore the count will always
// be less than the buffer size when not truncated.
while (charsWritten == path.size()) {
path.resize(path.size() * 2, 0);
charsWritten = GetModuleFileNameW(dllInstance, path.data(), path.size());
}
if (charsWritten == 0) {
return false;
}
processDllPath = path.data();
return true;
}
// Our activator's CLSID is generated once either during install or at runtime
// by the application generating the notification so that notifications work
// with parallel installs and portable/development builds. When a COM object is
// requested we verify the CLSID's InprocServer registry entry matches this
// DLL's path.
bool CheckRuntimeClsid(REFCLSID rclsid) {
std::wstring clsid_str;
{
wchar_t* raw_clsid_str;
if (StringFromCLSID(rclsid, &raw_clsid_str) == S_OK) {
clsid_str += raw_clsid_str;
CoTaskMemFree(raw_clsid_str);
} else {
return false;
}
}
std::wstring key = L"CLSID\\";
key += clsid_str;
key += L"\\InprocServer32";
DWORD bufferLen = 0;
LSTATUS status = RegGetValueW(HKEY_CLASSES_ROOT, key.c_str(), L"",
RRF_RT_REG_SZ, nullptr, nullptr, &bufferLen);
if (status != ERROR_SUCCESS) {
return false;
}
std::vector<wchar_t> clsidDllPathBuffer(bufferLen / sizeof(wchar_t));
// Sanity assignment in case the buffer length found was not an integer
// multiple of `sizeof(wchar_t)`.
bufferLen = clsidDllPathBuffer.size() * sizeof(wchar_t);
status = RegGetValueW(HKEY_CLASSES_ROOT, key.c_str(), L"", RRF_RT_REG_SZ,
nullptr, clsidDllPathBuffer.data(), &bufferLen);
if (status != ERROR_SUCCESS) {
return false;
}
path clsidDllPath = clsidDllPathBuffer.data();
return equivalent(processDllPath, clsidDllPath);
}
extern "C" {
HRESULT STDMETHODCALLTYPE DllGetClassObject(REFCLSID rclsid, REFIID riid,
LPVOID* ppv) {
if (!ppv) {
return E_INVALIDARG;
}
*ppv = nullptr;
if (!CheckRuntimeClsid(rclsid)) {
return CLASS_E_CLASSNOTAVAILABLE;
}
using namespace Microsoft::WRL;
ComPtr<NotificationFactory> factory =
Make<NotificationFactory, const GUID&, const path&>(
rclsid, processDllPath.parent_path());
switch (factory->QueryInterface(riid, ppv)) {
case S_OK:
return S_OK;
case E_NOINTERFACE:
return CLASS_E_CLASSNOTAVAILABLE;
default:
return E_UNEXPECTED;
}
}
BOOL STDMETHODCALLTYPE DllMain(HINSTANCE hinstDLL, DWORD fdwReason,
LPVOID lpReserved) {
if (fdwReason == DLL_PROCESS_ATTACH) {
if (!PopulateDllPath(hinstDLL)) {
return FALSE;
}
}
return TRUE;
}
}

View file

@ -0,0 +1,33 @@
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=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 "NotificationFactory.h"
HRESULT STDMETHODCALLTYPE NotificationFactory::CreateInstance(
IUnknown* pUnkOuter, REFIID riid, void** ppvObject) {
if (pUnkOuter != nullptr) {
return CLASS_E_NOAGGREGATION;
}
if (!ppvObject) {
return E_INVALIDARG;
}
*ppvObject = nullptr;
using namespace Microsoft::WRL;
ComPtr<NotificationCallback> callback =
Make<NotificationCallback, const GUID&, const path&>(notificationGuid,
installDir);
switch (callback->QueryInterface(riid, ppvObject)) {
case S_OK:
return S_OK;
case E_NOINTERFACE:
return E_NOINTERFACE;
default:
return E_UNEXPECTED;
}
}

View file

@ -0,0 +1,31 @@
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=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/. */
#ifndef NotificationFactory_h__
#define NotificationFactory_h__
#include <filesystem>
#include "NotificationCallback.h"
using namespace std::filesystem;
using namespace Microsoft::WRL;
class NotificationFactory final : public ClassFactory<> {
public:
HRESULT STDMETHODCALLTYPE CreateInstance(IUnknown* pUnkOuter, REFIID riid,
void** ppvObject) final;
explicit NotificationFactory(const GUID& runtimeGuid,
const path& dllInstallDir)
: notificationGuid(runtimeGuid), installDir(dllInstallDir) {}
private:
const GUID notificationGuid = {};
const path installDir = {};
};
#endif

View file

@ -0,0 +1,25 @@
# -*- Mode: python; indent-tabs-mode: nil; tab-width: 40 -*-
# vim: set filetype=python:
# 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/.
with Files("**"):
BUG_COMPONENT = ("Toolkit", "Notifications and Alerts")
SharedLibrary("notificationserver")
UNIFIED_SOURCES = [
"NotificationCallback.cpp",
"NotificationComServer.cpp",
"NotificationFactory.cpp",
]
DEFFILE = "notificationserver.def"
OS_LIBS += [
"advapi32",
]
LIBRARY_DEFINES["MOZ_NO_MOZALLOC"] = True
DisableStlWrapping()

View file

@ -0,0 +1,6 @@
;+# 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/.
LIBRARY notificationserver.dll
EXPORTS DllGetClassObject PRIVATE