在执行任何策略 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 的实现。 SDK 将生成的对象(在本例中为一个 mip::Profile)和上下文作为参数传递给观察程序函数。
上下文是指向为处理异步操作而创建的std::promise的指针。 该函数将 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 的对象中。
后续步骤
现在已添加配置文件,下一步是为该配置文件添加引擎。