Microsoft Information Protection SDK - 保護 SDK オブザーバー

保護 SDK には、3 つのオブザーバー クラスが含まれています。 仮想オブザーバー メンバーをオーバーライドして、非同期操作のコールバックを処理できます。

非同期操作が完了すると、SDK は結果に対応する OnXxx() メンバー関数を呼び出します。 例としては、OnLoadSuccess()に対する OnLoadFailure()、OnAddEngineSuccess()、mip::ProtectionProfile::Observer です。

次の例は、promise/future パターンを示しています。 SDK サンプルでもこのパターンが使用され、目的のコールバック動作を実装するように拡張できます。

ProtectionProfile オブザーバ実装

次の例では、mip::ProtectionProfile::Observerから派生するクラス (ProtectionProfileObserverImpl) を作成します。 メンバー関数は、サンプル全体で使用される 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 の実装

実装では、各オブザーバー メンバー関数に対して実行するアクションを定義します。

各メンバーは 2 つのパラメータを受け入れます。 1 つ目は、関数が処理するクラスへの共有ポインターです。 ProtectionObserver::OnLoadSuccess は mip::ProtectionProfile を受け取ることを想定しています。 ProtectionObserver::OnAddEngineSuccess は mip::ProtectionEngineを想定しています。

2 番目は、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 オブジェクトをインスタンス化すると、コンストラクターはパラメーターの 1 つとしてmip::ProtectionProfile::Observerを受け入れます。 次の例は、mip::ProtectionProfile::Settings コンストラクターで使用されるカスタム ProtectionProfileObserverImplを示しています。

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 の部分的な実装

このサンプルには最初の 2 つの関数のみが含まれていますが、残りの関数はこれらの関数と同様のパターンを使用し、 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);
};

次のステップ