Microsoft 資訊保護 SDK - 保護 SDK 觀察者

保護 SDK 包含三個觀察者類別。 你可以覆寫虛擬觀察者成員,以處理非同步操作的回調。

當非同步操作完成時,SDK 會呼叫 OnXxx() 對應結果的成員函式。 對於 OnLoadSuccess(),範例包括 OnLoadFailure()、OnAddEngineSuccess() 和 mip::ProtectionProfile::Observer。

以下範例展示了承諾與未來的模式。 SDK 範例也使用這個模式,你可以擴展它來實現想要的回調行為。

ProtectionProfile 觀察者實作

以下範例會產生一個類別 ,該類別 ProtectionProfileObserverImpl由 衍生 mip::ProtectionProfile::Observer出。 這些成員函式會覆寫基底類別中的對應函式,以使用整個範例中一貫採用的 promise/future 模式。

ProtectionProfileObserverImpl 類別宣告

標頭檔定義了 ProtectionProfileObserverImpl,使其衍生自 mip::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 OnAddEngineFailure(const exception_ptr& error, const shared_ptr<void>& context) override;
};

ProtectionProfileObserverImpl 實作

實作會為每個觀察者成員函式定義所要採取的動作。

每位成員接受兩個參數。 第一個是指向函式所處理類別的共享指標。 ProtectionObserver::OnLoadSuccess 預期接收 mip::ProtectionProfile。 ProtectionObserver::OnAddEngineSuccess 需要 mip::ProtectionEngine。

第二個是指向 context 的共享指標。 在此實作中,內容脈絡是對 std::promise 的參考,並作為 shared_ptr<void> 傳入。 函數的第一行將此值 std::promise鑄造成 ,然後將其儲存在一個稱為 promise的物件中。

最後,程式碼透過設定 promise->set_value() 並傳遞 mip::ProtectionProfile 物件,讓未來做好準備。

//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::OnAddEngineFailure(
  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 OnCreateProtectionHandlerFailure(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::OnCreateProtectionHandlerFailure(
  const exception_ptr& error,
  const shared_ptr<void>& context) {
  auto createProtectionHandlerPromise = static_cast<promise<shared_ptr<mip::ProtectionHandler>>*>(context.get());
  createProtectionHandlerPromise->set_exception(error);
};

下一步