Microsoft Information Protection SDK - Koruma SDK'sı Gözlemcileri
Koruma SDK'sı üç gözlemci sınıfı içerir. Gözlemci üyeler sanaldır ve zaman uyumsuz işlemler için geri çağırmaları işlemek üzere geçersiz kılınabilir.
- Koruma Profili:
mip::ProtectionProfile::Observer
- Koruma Altyapısı:
mip::ProtectionEngine::Observer
- Koruma İşleyicisi:
mip::ProtectionHandler::Observer
Zaman uyumsuz bir işlem tamamlandığında, OnXxx()
sonuca karşılık gelen üye işlevi çağrılır. Örnek olarak OnLoadSuccess()
, OnLoadFailure()
ve OnAddEngineSuccess()
verilebilir mip::ProtectionProfile::Observer
.
Aşağıdaki örneklerde, SDK örnekleri tarafından da kullanılan ve istenen geri çağırma davranışını uygulamak için genişletilebilen promise/future deseni gösterilmektedir.
ProtectionProfile Gözlemci Uygulaması
Aşağıdaki örnekte, öğesinden mip::ProtectionProfile::Observer
türetilen bir sınıf ProtectionProfileObserverImpl
oluşturduk. Örneklerde kullanılan promise/future desenini kullanmak için üye işlevleri geçersiz kılındı.
ProtectionProfileObserverImpl Sınıf Bildirimi
Üst bilgide, öğesinden mip::ProtectionProfile::Observer
türetilen öğesini tanımlar ProtectionProfileObserverImpl
ve ardından üye işlevlerin her birini geçersiz kılarız.
//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 Uygulaması
Uygulamanın kendisinde, her gözlemci üye işlevi için gerçekleştirecek bir eylem tanımlamamız yeterlidir.
Her üye iki parametre kabul eder. birincisi, işlevde ele aldığımız sınıfın paylaşılan bir işaretçisidir. ProtectionObserver::OnLoadSuccess
bir almayı mip::ProtectionProtection
beklerdimip::ProtectionEngine
. ProtectionObserver::OnAddEngineSuccess
İkincisi, bağlama ilişkin paylaşılan bir işaretçidir. Uygulamamızda bağlam olarak geçirilen shared_ptr<void>
bir std::promise
öğesine başvurudur. İşlevin ilk satırı bunu öğesine std::promise
atar ve adlı promise
bir nesnede depolanır.
Son olarak, ve nesnesi geçirilerek promise->set_value()
mip::ProtectionProtection
gelecek hazır hale getiriliyor.
//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);
};
Herhangi bir SDK sınıfı örneği oluşturduğumuz veya zaman uyumsuz işlemler gerçekleştiren bir işlev kullandığımızda, gözlemci uygulamasını ayarlar oluşturucusunun veya zaman uyumsuz işlevin kendisine geçiririz. Nesne örneği mip::ProtectionProfile::Settings
oluştururken, oluşturucu parametrelerden biri olarak alır mip::ProtectionProfile::Observer
. Aşağıdaki örnekte, bir mip::ProtectionProfile::Settings
oluşturucuda kullanılan özel ProtectionProfileObserverImpl
örneğimiz gösterilmektedir.
ProtectionHandler Gözlemci Uygulaması
Koruma gözlemcisine benzer şekilde, mip::ProtectionHandler
koruma işlemleri sırasında zaman uyumsuz olay bildirimlerini işlemek için bir mip::ProtectionHandler::Observer
sınıf uygular. Uygulama, yukarıda ayrıntılarıyla belirtilene benzer. ProtectionHandlerObserverImpl
aşağıda kısmen tanımlanmıştır. Uygulamanın tamamı GitHub örnek depomuzda bulunabilir.
ProtectionHandlerObserverImpl Sınıf Bildirimi
//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 Kısmi Uygulama
Bu örnek yalnızca ilk iki işlevdir, ancak kalan işlevler ve ProtectionObserver
işlevlerine benzer bir desen kullanır.
//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);
};