在執行任何政策 SDK 操作前先載入 mip::Profile 。
以下兩個範例說明如何建立 profileSettings 物件,並使用本機儲存進行狀態儲存,或僅使用記憶體。
載入設定檔
在定義 MipContext 和 ProfileObserver後,使用它們來實例化 mip::PolicyProfile。 要來建立mip::PolicyProfile物件,需要mip::PolicyProfile::Settings和mip::MipContext。
Profile::設定參數
PolicyProfile::Settings建構子接受以下四個參數:
-
const std::shared_ptr<MipContext>mip::MipContext:初始化以儲存應用程式資訊、狀態路徑等的物件。 -
mip::CacheStorageType:定義如何儲存狀態:在記憶體、磁碟或磁碟上,以及加密。 如需詳細資訊,請參閱 快取儲存概念。 -
std::shared_ptr<mip::PolicyProfile::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);
PolicyProfile::Settings profileSettings(
mMipContext, // mipContext object
mip::CacheStorageType::InMemory, // use in memory storage
std::make_shared<PolicyProfileObserverImpl>()); // 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);
PolicyProfile::Settings profileSettings(
mipContext, // mipContext object
mip::CacheStorageType::OnDisk, // use on disk storage
std::make_shared<PolicyProfileObserverImpl>()); // new protection profile observer
接下來,使用 promise/future 模式來載入 Profile。
auto profilePromise = std::make_shared<std::promise<std::shared_ptr<Profile>>>();
auto profileFuture = profilePromise->get_future();
Profile::LoadAsync(profileSettings, profilePromise);
若應用程式成功載入設定檔,則會通知 ProfileObserver::OnLoadSuccess(即 mip::Profile::Observer::OnLoadSuccess 的實作)。 SDK 會將產生的物件(此例為 a mip::Profile)及上下文作為參數傳遞給觀察者函式。
內容是指向為處理非同步作業而建立的 std::promise 的指標。 函式將承諾的值設定為第一個參數所傳遞的 Profile 物件。 當主函式使用 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);
PolicyProfile::Settings profileSettings(
mMipContext, // mipContext object
mip::CacheStorageType::OnDisk, // use on disk storage
std::make_shared<PolicyProfileObserverImpl>()); // new protection profile observer
auto profilePromise = std::make_shared<promise<shared_ptr<PolicyProfile>>>();
auto profileFuture = profilePromise->get_future();
Profile::LoadAsync(profileSettings, profilePromise);
auto profile = profileFuture.get();
}
程式碼載入設定檔並將其儲存在名為 profile的物件中。
下一步
現在已新增設定檔,下一步是將引擎新增至設定檔。