Microsoft Information Protection SDK - Osservatori di Protection SDK

Il Protection SDK contiene tre classi di osservatore. È possibile eseguire l'override dei membri dell'osservatore virtuale per gestire i callback per le operazioni asincrone.

Al termine di un'operazione asincrona, l'SDK chiama la OnXxx() funzione membro che corrisponde al risultato. Gli esempi sono OnLoadSuccess(), OnLoadFailure()e OnAddEngineSuccess() per mip::ProtectionProfile::Observer.

Gli esempi seguenti illustrano il modello promise/future. Gli esempi dell'SDK usano anche questo modello ed è possibile estenderlo per implementare il comportamento di callback desiderato.

Implementazione dell'osservatore ProtectionProfile

Nell'esempio seguente viene creata una classe , ProtectionProfileObserverImpl, che deriva da mip::ProtectionProfile::Observer. Le funzioni membro eseguono l'override della classe base per usare il modello promise/futuro usato in tutti gli esempi.

Dichiarazione di classe ProtectionProfileObserverImpl

L'intestazione definisce ProtectionProfileObserverImpl, la deriva da mip::ProtectionProfile::Observer, quindi esegue l'override di ognuna delle funzioni membro.

//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;
};

Implementazione di ProtectionProfileObserverImpl

L'implementazione definisce un'azione da eseguire per ogni funzione membro osservatore.

Ogni membro accetta due parametri. Il primo è un puntatore condiviso alla classe gestita dalla funzione. ProtectionObserver::OnLoadSuccess si aspetta di ricevere un mip::ProtectionProfile. ProtectionObserver::OnAddEngineSuccess si aspetta mip::ProtectionEngine.

Il secondo è un puntatore condiviso al contesto. In questa implementazione, il contesto è un riferimento a un std::promise, passato come shared_ptr<void>. La prima riga della funzione esegue il cast di questo valore su std::promise, quindi la archivia in un oggetto denominato promise.

Infine, il codice rende il futuro pronto impostando promise->set_value() e passando l'oggetto 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);
};

Quando si crea un'istanza di qualsiasi classe SDK o si usa una funzione che esegue operazioni asincrone, passare l'implementazione dell'osservatore al costruttore delle impostazioni o alla funzione asincrona stessa. Quando si crea un'istanza dell'oggetto mip::ProtectionProfile::Settings , il costruttore accetta mip::ProtectionProfile::Observer come uno dei parametri. Nell'esempio seguente viene illustrato l'oggetto personalizzato ProtectionProfileObserverImplusato in un mip::ProtectionProfile::Settings costruttore .

Implementazione dell'osservatore ProtectionHandler

Analogamente all'osservatore protezione, mip::ProtectionHandler implementa una mip::ProtectionHandler::Observer classe per la gestione delle notifiche degli eventi asincrone durante le operazioni di protezione. L'implementazione è simile all'implementazione precedente. L'esempio seguente definisce ProtectionHandlerObserverImplparzialmente . L'implementazione completa si trova nel repository di esempio GitHub.

Dichiarazione di classe 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;
};

Implementazione parziale di ProtectionHandlerObserverImpl

Questo esempio include solo le prime due funzioni, ma le funzioni rimanenti usano un modello simile a queste funzioni e a 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);
};

Passaggi successivi