blob: 307edf60a31e4693c72a7c38ff17845aa00be022 [file] [log] [blame] [edit]
// Copyright 2022 The ChromiumOS Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef LIBHWSEC_MIDDLEWARE_MIDDLEWARE_OWNER_H_
#define LIBHWSEC_MIDDLEWARE_MIDDLEWARE_OWNER_H_
#include <atomic>
#include <memory>
#include <optional>
#include <utility>
#include <absl/base/attributes.h>
#include <base/memory/scoped_refptr.h>
#include <base/memory/weak_ptr.h>
#include <base/task/task_runner.h>
#include <base/threading/thread.h>
#include "libhwsec/backend/backend.h"
#include "libhwsec/hwsec_export.h"
#include "libhwsec/middleware/metrics.h"
#include "libhwsec/middleware/middleware_derivative.h"
#include "libhwsec/proxy/proxy.h"
#include "libhwsec/status.h"
#include "libhwsec/structures/threading_mode.h"
#if USE_FUZZER
#include <fuzzer/FuzzedDataProvider.h>
#include "libhwsec/fuzzed/attestation.h"
#include "libhwsec/fuzzed/basic_objects.h"
#include "libhwsec/fuzzed/config.h"
#include "libhwsec/fuzzed/da_mitigation.h"
#include "libhwsec/fuzzed/encryption.h"
#include "libhwsec/fuzzed/event_management.h"
#include "libhwsec/fuzzed/hwsec_objects.h"
#include "libhwsec/fuzzed/key_management.h"
#include "libhwsec/fuzzed/middleware.h"
#include "libhwsec/fuzzed/pinweaver.h"
#include "libhwsec/fuzzed/pinweaver_manager.h"
#include "libhwsec/fuzzed/protobuf.h"
#include "libhwsec/fuzzed/recovery_crypto.h"
#include "libhwsec/fuzzed/sealing.h"
#include "libhwsec/fuzzed/signature_sealing.h"
#include "libhwsec/fuzzed/signing.h"
#include "libhwsec/fuzzed/storage.h"
#include "libhwsec/fuzzed/u2f.h"
#include "libhwsec/fuzzed/vendor.h"
#endif
#ifndef BUILD_LIBHWSEC
#error "Don't include this file outside libhwsec!"
#endif
namespace hwsec {
class Middleware;
class TPMRetryHandler;
class HWSEC_EXPORT MiddlewareOwner {
public:
friend class Middleware;
friend class TPMRetryHandler;
explicit MiddlewareOwner(ThreadingMode mode);
// Constructor for custom backend.
MiddlewareOwner(std::unique_ptr<Backend> custom_backend, ThreadingMode mode);
virtual ~MiddlewareOwner();
MiddlewareDerivative Derive();
#if USE_FUZZER
void set_data_provider(FuzzedDataProvider* data_provider) {
data_provider_ = data_provider;
}
#endif
private:
void InitThreadingMode(ThreadingMode mode);
void InitBackend();
void InitWithCustomBackend(std::unique_ptr<Backend> custom_backend);
void FiniBackend();
// These functions can only be used on the background thread.
// Otherwise it will return nullptr.
Metrics* GetMetrics() const;
Proxy* GetProxy() const;
Backend* GetBackend() const;
std::unique_ptr<base::Thread> background_thread_;
scoped_refptr<base::TaskRunner> task_runner_;
std::atomic<base::PlatformThreadId> thread_id_;
#if USE_FUZZER
FuzzedDataProvider* data_provider_ = nullptr;
#endif
// Member variables should appear before the WeakPtrFactory, to ensure
// that any WeakPtrs to Controller are invalidated before its members
// variable's destructors are executed, rendering them invalid.
base::WeakPtrFactory<MiddlewareOwner> weak_factory_{this};
};
} // namespace hwsec
#endif // LIBHWSEC_MIDDLEWARE_MIDDLEWARE_OWNER_H_