1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146
|
// Copyright 2018 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
// This macro is used in <wrl/module.h>. Since only the COM functionality is
// used here (while WinRT isn't being used), define this macro to optimize
// compilation of <wrl/module.h> for COM-only.
#ifndef __WRL_CLASSIC_COM_STRICT__
#define __WRL_CLASSIC_COM_STRICT__
#endif // __WRL_CLASSIC_COM_STRICT__
#include "chrome/notification_helper/com_server_module.h"
#include <wrl/module.h>
#include <type_traits>
#include "base/metrics/histogram_macros.h"
#include "chrome/install_static/install_util.h"
#include "chrome/notification_helper/notification_activator.h"
#include "chrome/notification_helper/trace_util.h"
namespace mswr = Microsoft::WRL;
namespace {
// These values are persisted to logs. Entries should not be renumbered and
// numeric values should never be reused.
enum class ComServerModuleStatus {
SUCCESS = 0,
FACTORY_CREATION_FAILED = 1,
ICLASSFACTORY_OBJECT_CREATION_FAILED = 2,
REGISTRATION_FAILED = 3,
UNREGISTRATION_FAILED = 4,
COUNT // Must be the final value.
};
void LogComServerModuleHistogram(ComServerModuleStatus status) {
UMA_HISTOGRAM_ENUMERATION(
"Notifications.NotificationHelper.ComServerModuleStatus", status,
ComServerModuleStatus::COUNT);
}
} // namespace
namespace notification_helper {
// The reset policy of the event MUST BE set to MANUAL to avoid signaling the
// event in IsSignaled() itself, which is called by IsEventSignaled().
ComServerModule::ComServerModule()
: object_zero_count_(base::WaitableEvent::ResetPolicy::MANUAL,
base::WaitableEvent::InitialState::NOT_SIGNALED) {}
ComServerModule::~ComServerModule() = default;
HRESULT ComServerModule::Run() {
HRESULT hr = RegisterClassObjects();
if (SUCCEEDED(hr)) {
WaitForZeroObjectCount();
hr = UnregisterClassObjects();
}
if (SUCCEEDED(hr))
LogComServerModuleHistogram(ComServerModuleStatus::SUCCESS);
return hr;
}
HRESULT ComServerModule::RegisterClassObjects() {
// Create an out-of-proc COM module with caching disabled. The supplied
// method is invoked when the last instance object of the module is released.
auto& module = mswr::Module<mswr::OutOfProcDisableCaching>::Create(
this, &ComServerModule::SignalObjectCountZero);
// Usually COM module classes statically define their CLSID at compile time
// through the use of various macros, and WRL::Module internals takes care of
// creating the class objects and registering them. However, we need to
// register the same object with different CLSIDs depending on a runtime
// setting, so we handle that logic here.
mswr::ComPtr<IUnknown> factory;
unsigned int flags = mswr::ModuleType::OutOfProcDisableCaching;
HRESULT hr = mswr::Details::CreateClassFactory<
mswr::SimpleClassFactory<NotificationActivator>>(
&flags, nullptr, __uuidof(IClassFactory), &factory);
if (FAILED(hr)) {
LogComServerModuleHistogram(ComServerModuleStatus::FACTORY_CREATION_FAILED);
Trace(L"%hs(Factory creation failed; hr: 0x%08X)\n", __func__, hr);
return hr;
}
mswr::ComPtr<IClassFactory> class_factory;
hr = factory.As(&class_factory);
if (FAILED(hr)) {
LogComServerModuleHistogram(
ComServerModuleStatus::ICLASSFACTORY_OBJECT_CREATION_FAILED);
Trace(L"%hs(IClassFactory object creation failed; hr: 0x%08X)\n", __func__,
hr);
return hr;
}
// All pointers in this array are unowned. Do not release them.
IClassFactory* class_factories[] = {class_factory.Get()};
static_assert(std::extent<decltype(cookies_)>() == std::size(class_factories),
"Arrays cookies_ and class_factories must be the same size.");
IID class_ids[] = {install_static::GetToastActivatorClsid()};
static_assert(std::extent<decltype(cookies_)>() == std::size(class_ids),
"Arrays cookies_ and class_ids must be the same size.");
hr = module.RegisterCOMObject(nullptr, class_ids, class_factories, cookies_,
std::extent<decltype(cookies_)>());
if (FAILED(hr)) {
LogComServerModuleHistogram(ComServerModuleStatus::REGISTRATION_FAILED);
Trace(L"%hs(NotificationActivator registration failed; hr: 0x%08X)\n",
__func__, hr);
}
return hr;
}
HRESULT ComServerModule::UnregisterClassObjects() {
auto& module = mswr::Module<mswr::OutOfProcDisableCaching>::GetModule();
HRESULT hr = module.UnregisterCOMObject(nullptr, cookies_,
std::extent<decltype(cookies_)>());
if (FAILED(hr)) {
LogComServerModuleHistogram(ComServerModuleStatus::UNREGISTRATION_FAILED);
Trace(L"%hs(NotificationActivator unregistration failed; hr: 0x%08X)\n",
__func__, hr);
}
return hr;
}
bool ComServerModule::IsEventSignaled() {
return object_zero_count_.IsSignaled();
}
void ComServerModule::WaitForZeroObjectCount() {
object_zero_count_.Wait();
}
void ComServerModule::SignalObjectCountZero() {
object_zero_count_.Signal();
}
} // namespace notification_helper
|