Microsoft 信息保护 SDK - 文件 SDK 观察器

文件 SDK 包含两个观察程序类。 可以重写虚拟观察程序成员来处理事件回调。

异步操作完成后,SDK 将调用 OnXxx() 与结果相对应的成员函数。 例如,对于 OnLoadSuccess() 为 OnLoadFailure()、OnAddEngineSuccess() 和 mip::FileProfile::Observer。

这些示例演示承诺/未来模式。 SDK 示例还使用此模式,并且可以扩展它以实现所需的回调行为。

文件配置文件观察程序实现

以下示例创建派生自mip::FileProfile::Observer的类ProfileObserver。 成员函数将重写基类,以使用整个示例中使用的未来/承诺模式。

Note

这些示例仅实现观察程序类的一部分,未包含与 mip::FileEngine 相关的观察程序的替代。

profile_observer.h

该头文件定义了 ProfileObserver,它从 mip::FileProfile::Observer 派生,然后重写其中的每个成员函数。

class ProfileObserver final : public mip::FileProfile::Observer {
public:
ProfileObserver() { }
  void OnLoadSuccess(const std::shared_ptr<mip::FileProfile>& profile, const std::shared_ptr<void>& context) override;
  void OnLoadFailure(const std::exception_ptr& error, const std::shared_ptr<void>& context) override;
  //TODO: Implement mip::FileEngine related observers.
};

profile_observer.cpp

该实现定义要对每个观察者成员函数执行的操作。

每个成员可接受两个参数。 第一个是指向函数处理的类的共享指针。 ProfileObserver::OnLoadSuccess 预期会收到一个 mip::FileProfile。 ProfileObserver::OnAddEngineSuccess 需要 mip::FileEngine。

第二个是指向“上下文”的共享指针。 在此实现中,上下文是对 std::promise 的引用,并以 std::shared_ptr<void> 的形式按引用传递。 函数的第一行将此值强制转换为 std::promise,然后将其存储在一个名为 promise 的对象中。

最后,代码通过设置 promise->set_value() 和传入 mip::FileProfile 对象使未来准备就绪。

#include "profile_observer.h"
#include <future>

//Called when FileProfile is successfully loaded
void ProfileObserver::OnLoadSuccess(const std::shared_ptr<mip::FileProfile>& profile, const std::shared_ptr<void>& context) {
  //cast context to promise
  auto promise = 
  std::static_pointer_cast<std::promise<std::shared_ptr<mip::FileProfile>>>(context);
  //set promise value to profile
  promise->set_value(profile);
}

//Called when FileProfile fails to load
void ProfileObserver::OnLoadFailure(const std::exception_ptr& error, const std::shared_ptr<void>& context) {
  auto promise = std::static_pointer_cast<std::promise<std::shared_ptr<mip::FileProfile>>>(context);
  promise->set_exception(error);
}

//TODO: Implement mip::FileEngine related observers.

实例化任何 SDK 类或使用执行异步操作的函数时,请将观察程序实现传递给设置构造函数或异步函数本身。 实例化 mip::FileProfile::Settings 对象时,构造函数接受 mip::FileProfile::Observer 为其中一个参数。 此示例展示了在ProfileObserver构造函数中使用的自定义mip::FileProfile::Settings。

FileHandler 观察器实现

与配置文件观察程序类似, mip::FileHandler 实现一个 mip::FileHandler::Observer 类,用于在文件操作期间处理异步事件通知。 该实现类似于前面的实现。 下面的示例部分定义 FileHandlerObserver。

file_handler_observer.h

#include "mip/file/file_handler.h"

class FileHandlerObserver final : public mip::FileHandler::Observer {
public:
  void OnCreateFileHandlerSuccess(
      const std::shared_ptr<mip::FileHandler>& fileHandler,
      const std::shared_ptr<void>& context) override;

  void OnCreateFileHandlerFailure(
      const std::exception_ptr& error,
      const std::shared_ptr<void>& context) override;

  //TODO: override remaining member functions inherited from mip::FileHandler::Observer
};

file_handler_observer.cpp

此示例仅包括前两个函数,但其余函数使用与这些函数和函数 ProfileObserver类似的模式。

#include "file_handler_observer.h"

void FileHandlerObserver::OnCreateFileHandlerSuccess(const std::shared_ptr<mip::FileHandler>& fileHandler, const std::shared_ptr<void>& context) {
    auto promise = std::static_pointer_cast<std::promise<std::shared_ptr<mip::FileHandler>>>(context);
    promise->set_value(fileHandler);
}

void FileHandlerObserver::OnCreateFileHandlerFailure(const std::exception_ptr& error, const std::shared_ptr<void>& context) {
    auto promise = std::static_pointer_cast<std::promise<std::shared_ptr<mip::FileHandler>>>(context);
    promise->set_exception(error);
}

//TODO: override remaining member functions inherited from mip::FileHandler::Observer

后续步骤