Condividi tramite


Microsoft Information Protection SDK - Osservatori dell'SDK di Protezione

Il Protection SDK contiene tre classi di osservatore. I membri observer sono virtuali e possono essere sottoposti a override per gestire i callback per le operazioni asincrone.

Al termine di un'operazione asincrona, viene chiamata la OnXxx() funzione membro corrispondente al risultato. Gli esempi sono OnLoadSuccess(), OnLoadFailure()e OnAddEngineSuccess() per mip::ProtectionProfile::Observer.

Gli esempi seguenti illustrano il modello promise/future, che viene usato anche dagli esempi dell'SDK e possono essere estesi per implementare il comportamento di callback desiderato.

Implementazione di ProtectionProfile Observer

Nell'esempio seguente è stata creata una classe derivata ProtectionProfileObserverImpl da mip::ProtectionProfile::Observer. Le funzioni membro sono state ridefinite per usare il modello promise/future utilizzato in tutti i campioni.

Dichiarazione di classe ProtectionProfileObserverImpl

Nell'intestazione si definisce ProtectionProfileObserverImpl, derivando da mip::ProtectionProfile::Observer, quindi si 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 OnAddEngineError(const exception_ptr& error, const shared_ptr<void>& context) override;
};

Implementazione di ProtectionProfileObserverImpl

Nell'implementazione stessa si definisce semplicemente un'azione da eseguire per ogni funzione membro osservatore.

Ogni membro accetta due parametri. Il primo è un puntatore condiviso alla classe gestita nella funzione. ProtectionObserver::OnLoadSuccess si aspetterebbe di ricevere un mip::ProtectionProtection, ProtectionObserver::OnAddEngineSuccess si aspetterebbe mip::ProtectionEngine.

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

Infine, il futuro viene preparato tramite l'impostazione di promise->set_value() e il passaggio dell'oggetto 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);
};

Quando si crea un'istanza di qualsiasi classe SDK o si usa una funzione che esegue operazioni asincrone, si passerà 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. L'esempio seguente mostra l'oggetto personalizzato ProtectionProfileObserverImplusato in un mip::ProtectionProfile::Settings costruttore.

Implementazione di ProtectionHandler Observer

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 a quella descritta in precedenza. ProtectionHandlerObserverImpl è parzialmente definito di seguito. L'implementazione completa è disponibile 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 OnCreateProtectionHandlerError(const exception_ptr& error, const shared_ptr<void>& context) override;
};

Implementazione parziale di ProtectionHandlerObserverImpl

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