在執行任何原則 SDK 作業之前,必須先載入mip::Profile。
下列兩個範例示範如何建立 profileSettings 物件,使用本地存儲於狀態存儲,以及僅限於記憶體。
載入配置檔
現在 MipContext 和 ProfileObserver 已經定義,我們將使用它們來實例化mip::PolicyProfile。 要來建立mip::PolicyProfile物件,需要mip::PolicyProfile::Settings和mip::MipContext。
Profile::Settings 參數
建 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 。 在此情況下 mip::Profile,產生的物件會以參數的形式傳入觀察者函式,以及內容。
內容是我們建立來處理異步作業的指標std::promise。 函式只會將 promise 的值設定為針對第一個參數傳入的 Profile 物件。 當main函式使用 Future.get()時,結果可以儲存在呼叫線程的新物件中。
//get the future value and store in profile.
auto profile = profileFuture.get();
整合
完全實作觀察者和身份驗證委派者之後,現在可以完全載入設定檔。 下列程式片段假定已經包含所有必要的標頭。
int main()
{
const string userName = "MyTestUser@consoto.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的物件中。
後續步驟
現在已新增配置檔,下一個步驟是將引擎新增至配置檔。