123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161 |
- #ifndef BASE_WIN_ASYNC_OPERATION_H_
- #define BASE_WIN_ASYNC_OPERATION_H_
- #include <unknwn.h>
- #include <windows.foundation.h>
- #include <wrl/async.h>
- #include <wrl/client.h>
- #include <type_traits>
- #include <utility>
- #include "base/bind.h"
- #include "base/callback.h"
- #include "base/macros.h"
- #include "base/memory/weak_ptr.h"
- #include "base/optional.h"
- #include "base/threading/thread_checker.h"
- #include "base/win/winrt_foundation_helpers.h"
- namespace base {
- namespace win {
- namespace internal {
- template <typename T>
- using AsyncOperationComplex =
- typename ABI::Windows::Foundation::IAsyncOperation<T>::TResult_complex;
- template <typename T>
- using AsyncOperationAbi = AbiType<AsyncOperationComplex<T>>;
- template <typename T>
- using AsyncOperationOptionalStorage =
- OptionalStorageType<AsyncOperationComplex<T>>;
- template <typename T>
- using AsyncOperationStorage = StorageType<AsyncOperationComplex<T>>;
- }
- template <class T>
- class AsyncOperation
- : public Microsoft::WRL::RuntimeClass<
- Microsoft::WRL::RuntimeClassFlags<
- Microsoft::WRL::WinRt | Microsoft::WRL::InhibitRoOriginateError>,
- ABI::Windows::Foundation::IAsyncOperation<T>> {
- public:
- using AbiT = internal::AsyncOperationAbi<T>;
- using OptionalStorageT = internal::AsyncOperationOptionalStorage<T>;
- using StorageT = internal::AsyncOperationStorage<T>;
- using Handler = ABI::Windows::Foundation::IAsyncOperationCompletedHandler<T>;
- using ResultCallback = base::OnceCallback<void(StorageT)>;
- AsyncOperation() {
-
-
-
- callback_ =
- base::BindOnce(&AsyncOperation::OnResult, weak_factory_.GetWeakPtr());
- }
- ~AsyncOperation() { DCHECK_CALLED_ON_VALID_THREAD(thread_checker_); }
-
- IFACEMETHODIMP put_Completed(Handler* handler) override {
- DCHECK_CALLED_ON_VALID_THREAD(thread_checker_);
- handler_ = handler;
- return S_OK;
- }
- IFACEMETHODIMP get_Completed(Handler** handler) override {
- DCHECK_CALLED_ON_VALID_THREAD(thread_checker_);
- return handler_.CopyTo(handler);
- }
- IFACEMETHODIMP GetResults(AbiT* results) override {
- DCHECK_CALLED_ON_VALID_THREAD(thread_checker_);
- return results_ ? internal::CopyTo(results_, results) : E_PENDING;
- }
- ResultCallback callback() {
- DCHECK_CALLED_ON_VALID_THREAD(thread_checker_);
- DCHECK(!callback_.is_null());
- return std::move(callback_);
- }
- private:
- void InvokeCompletedHandler() {
- handler_->Invoke(this, ABI::Windows::Foundation::AsyncStatus::Completed);
- }
- void OnResult(StorageT result) {
- DCHECK_CALLED_ON_VALID_THREAD(thread_checker_);
- DCHECK(!results_);
- results_ = std::move(result);
- InvokeCompletedHandler();
- }
- ResultCallback callback_;
- Microsoft::WRL::ComPtr<Handler> handler_;
- OptionalStorageT results_;
- THREAD_CHECKER(thread_checker_);
- base::WeakPtrFactory<AsyncOperation> weak_factory_{this};
- DISALLOW_COPY_AND_ASSIGN(AsyncOperation);
- };
- }
- }
- #endif
|