以下兩個範例展示了如何透過本地儲存作為狀態儲存或僅用於記憶體來建立 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);
如果設定檔成功載入,SDK 會呼叫 ProtectionProfileObserverImpl::OnLoadSuccess,即 mip::ProtectionProfile::Observer::OnLoadSuccess 的實作。 SDK 會將產生的物件或例外指標及上下文作為參數傳遞給函式。 上下文是指向為處理非同步作業而建立的 std::promise 的指標。 這個函式會將承諾的值設定給 ProtectionProfile 物件(上下文)。 當主函式使用 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的物件中。
下一步
現在你已經載入了設定檔,下一步就是在設定檔中加入引擎。
- 請在 GitHub 上探索 MIP Protection SDK C++ 範例。