Microsoft 信息保护 SDK - 保护 SDK 观察程序

保护 SDK 包含三个观察程序类。 观察者成员是虚拟的,可以将其重写以处理对异步操作的回调。

当异步操作完成时,将会调用与结果对应的 OnXxx() 成员函数。 例如,对于 OnLoadSuccess()OnLoadFailure()OnAddEngineSuccess()mip::ProtectionProfile::Observer

下面的示例演示了 SDK 示例也同样使用的承诺/未来模式,并且可以进行扩展以实现所需的回调行为。

ProtectionProfile 观察者实现

在下面的示例中,我们创建了一个类 ProtectionProfileObserverImpl,它派生自 mip::ProtectionProfile::Observer。 成员函数已被重写,以便在整个示例中使用承诺/未来模式。

ProtectionProfileObserverImpl 类声明

在标头中,我们定义了派生自 ProtectionProfileObserverImplmip::ProtectionProfile::Observer,然后重写了每个成员函数。

//ProtectionProfileObserverImpl.h
class ProtectionProfileObserverImpl final : public mip::ProtectionProfile::Observer {
public:
  ProtectionProfileObserverImpl() { }
  void OnLoadSuccess(const shared_ptr<mip::ProtectionProfile>& profile, const shared_ptr<void>& context) override;
  void OnLoadFailure(const exception_ptr& error, const shared_ptr<void>& context) override;
  void OnAddEngineSuccess(const shared_ptr<mip::ProtectionEngine>& engine, const shared_ptr<void>& context) override;
  void OnAddEngineError(const exception_ptr& error, const shared_ptr<void>& context) override;
};

ProtectionProfileObserverImpl 实现

在实现本身中,我们只为每个观察者成员函数定义一个要执行的操作。

每个成员可接受两个参数。 第一个是指向我们在函数中处理的类的共享指针。 ProtectionObserver::OnLoadSuccess 预计会收到 mip::ProtectionProtection,而 ProtectionObserver::OnAddEngineSuccess 预计会收到 mip::ProtectionEngine

第二个是指向“上下文”的共享指针。 在我们的实现中,上下文是对 std::promise 的引用,传递为 shared_ptr<void>。 函数的第一行将此强制转换为 std::promise,然后存储在一个名为 promise 的对象中。

最后,通过设置 promise->set_value() 并传入 mip::ProtectionProtection 对象来准备未来。

//protection_observers.cpp

void ProtectionProfileObserverImpl::OnLoadSuccess(
  const shared_ptr<mip::ProtectionProfile>& profile,
  const shared_ptr<void>& context) {
  auto loadPromise = static_cast<promise<shared_ptr<mip::ProtectionProfile>>*>(context.get());
  loadPromise->set_value(profile);
};

void ProtectionProfileObserverImpl::OnLoadFailure(const exception_ptr& error, const shared_ptr<void>& context) {
  auto loadPromise = static_cast<promise<shared_ptr<mip::ProtectionProfile>>*>(context.get());
  loadPromise->set_exception(error);
};

void ProtectionProfileObserverImpl::OnAddEngineSuccess(
  const shared_ptr<mip::ProtectionEngine>& engine,
  const shared_ptr<void>& context) {
  auto addEnginePromise = static_cast<promise<shared_ptr<mip::ProtectionEngine>>*>(context.get());
  addEnginePromise->set_value(engine);
};

void ProtectionProfileObserverImpl::OnAddEngineError(
  const exception_ptr& error,
  const shared_ptr<void>& context) {
  auto addEnginePromise = static_cast<promise<shared_ptr<mip::ProtectionEngine>>*>(context.get());
  addEnginePromise->set_exception(error);
};

当我们实例化任何 SDK 类或使用执行异步操作的函数时,我们会将观察者实现传递给设置构造函数或异步函数本身。 实例化 mip::ProtectionProfile::Settings 对象时,构造函数会将 mip::ProtectionProfile::Observer 作为一个参数使用。 下面的示例显示了我们在 ProtectionProfileObserverImpl 构造函数中使用的自定义 mip::ProtectionProfile::Settings

ProtectionHandler 观察者实现

与保护观察者类似,mip::ProtectionHandler 将会实现一个 mip::ProtectionHandler::Observer 类,用于在保护操作期间处理异步事件通知。 实现与上面详述的实现类似。 下面部分定义了 ProtectionHandlerObserverImpl。 完整的实现详见我们的 GitHub 示例存储库

ProtectionHandlerObserverImpl 类声明

//protection_observers.h

class ProtectionHandlerObserverImpl final : public mip::ProtectionHandler::Observer {
public:
  ProtectionHandlerObserverImpl() { }
  void OnCreateProtectionHandlerSuccess(const shared_ptr<mip::ProtectionHandler>& protectionHandler, const shared_ptr<void>& context) override;
  void OnCreateProtectionHandlerError(const exception_ptr& error, const shared_ptr<void>& context) override;
};

ProtectionHandlerObserverImpl 部分实现

此示例只包含了前两个函数,剩余的函数使用的模式与这些函数和 ProtectionObserver 类似。

//protection_observers.cpp

void ProtectionHandlerObserverImpl::OnCreateProtectionHandlerSuccess(
  const shared_ptr<mip::ProtectionHandler>& protectionHandler,
  const shared_ptr<void>& context) {
  auto createProtectionHandlerPromise = static_cast<promise<shared_ptr<mip::ProtectionHandler>>*>(context.get());
  createProtectionHandlerPromise->set_value(protectionHandler);
};

void ProtectionHandlerObserverImpl::OnCreateProtectionHandlerError(
  const exception_ptr& error,
  const shared_ptr<void>& context) {
  auto createProtectionHandlerPromise = static_cast<promise<shared_ptr<mip::ProtectionHandler>>*>(context.get());
  createProtectionHandlerPromise->set_exception(error);
};