下列兩個範例示範如何使用本地存儲建立 profileSettings 物件,以用於狀態儲存,以及僅限記憶體。
載入配置檔
現在 ProtectionProfileObserverImpl 已經定義,我們將使用它來實例化 mip::ProtectionProfile。 建立mip::ProtectionProfile物件需要mip::ProtectionProfile::Settings。
ProtectionProfile::Settings 參數
-
std::shared_ptr<MipContext>mip::MipContext:初始化以儲存應用程式資訊、狀態路徑等的物件。 -
mip::CacheStorageType:定義如何儲存狀態:在記憶體、磁碟或磁碟上,以及加密。 -
std::shared_ptr<mip::ConsentDelegate>:類別mip::ConsentDelegate的共享指標。 -
std::shared_ptr<mip::ProtectionProfile::Observer> observer:配置文件Observer實作的共享指標(在PolicyProfile、ProtectionProfile和FileProfile中)。
下列兩個範例示範如何使用本地儲存和僅使用記憶體來建立 profileSettings 物件。
僅將狀態儲存在記憶體中
mip::ApplicationInfo appInfo {clientId, "APP NAME", "1.2.3" };
std::shared_ptr<mip::MipConfiguration> mipConfiguration = std::make_shared<mip::MipConfiguration>(mAppInfo,
"mip_data",
mip::LogLevel::Trace,
false);
std::shared_ptr<mip::MipContext> mMipContext = mip::MipContext::Create(mipConfiguration);
ProtectionProfile::Settings profileSettings(
mMipContext, // mipContext object
mip::CacheStorageType::InMemory, // use in memory storage
std::make_shared<ConsentDelegateImpl>(), // new consent delegate
std::make_shared<ProtectionProfileObserverImpl>()); // new protection profile observer
從磁碟上的記憶體路徑讀取/寫入設定檔設定
mip::ApplicationInfo appInfo {clientId, "APP NAME", "1.2.3" };
std::shared_ptr<mip::MipConfiguration> mipConfiguration = std::make_shared<mip::MipConfiguration>(mAppInfo,
"mip_data",
mip::LogLevel::Trace,
false);
std::shared_ptr<mip::MipContext> mMipContext = mip::MipContext::Create(mipConfiguration);
ProtectionProfile::Settings profileSettings(
mMipContext, // mipContext object
mip::CacheStorageType::OnDisk, // use on disk storage
std::make_shared<ConsentDelegateImpl>(), // new consent delegate
std::make_shared<ProtectionProfileObserverImpl>()); // new protection profile
接下來,使用 promise/future 模式來載入 ProtectionProfile。
auto profilePromise = std::make_shared<std::promise<std::shared_ptr<ProtectionProfile>>>();
auto profileFuture = profilePromise->get_future();
ProtectionProfile::LoadAsync(profileSettings, profilePromise);
如果我們已載入配置檔,且該作業成功, ProtectionProfileObserverImpl::OnLoadSuccess則會呼叫 我們的實 mip::ProtectionProfile::Observer::OnLoadSuccess 作。 產生的物件或例外狀況指標以及內容會以參數的形式傳入函式。 上下文是我們為處理非同步操作而建立的指標 std::promise 。 函式只會將 Promise 的值設定為 ProtectionProfile 物件 (context)。 當main函式使用 Future.get()時,結果可以儲存在新的物件中。
//get the future value and store in profile.
auto profile = profileFuture.get();
組合在一起
完全實作觀察者和身份驗證委派之後,現在可以完全載入個人檔案。 下列程式碼片段假定已經包含所有必要的標頭檔。
int main()
{
const string userName = "MyTestUser@contoso.com";
const string password = "P@ssw0rd!";
const string clientId = "MyClientId";
mip::ApplicationInfo appInfo {clientId, "APP NAME", "1.2.3" };
std::shared_ptr<mip::MipConfiguration> mipConfiguration = std::make_shared<mip::MipConfiguration>(mAppInfo,
"mip_data",
mip::LogLevel::Trace,
false);
std::shared_ptr<mip::MipContext> mMipContext = mip::MipContext::Create(mipConfiguration);
ProtectionProfile::Settings profileSettings(
mMipContext, // mipContext object
mip::CacheStorageType::OnDisk, // use on disk storage
std::make_shared<ConsentDelegateImpl>(), // new consent delegate
std::make_shared<ProfileObserver>()); // new protection profile observer
auto profilePromise = std::make_shared<promise<shared_ptr<ProtectionProfile>>>();
auto profileFuture = profilePromise->get_future();
ProtectionProfile::LoadAsync(profileSettings, profilePromise);
auto profile = profileFuture.get();
}
最終結果是我們已成功載入配置檔,並儲存在名為 profile的物件中。
後續步驟
現在已新增配置檔,下一個步驟是將引擎新增至配置檔。