以下两个示例演示如何将本地存储用于状态存储或仅内存来创建 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>(appInfo,
"mip_data",
mip::LogLevel::Trace,
false, mip::CacheStorageType::OnDisk);
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>(appInfo,
"mip_data",
mip::LogLevel::Trace,
false, mip::CacheStorageType::OnDisk);
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
然后使用承诺/未来模式加载 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 。 该函数将 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>(appInfo,
"mip_data",
mip::LogLevel::Trace,
false, mip::CacheStorageType::OnDisk);
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 保护 SDK C++ 示例。