scoped_dev_zero_fuchsia.h 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. // Copyright 2021 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #ifndef BASE_TEST_SCOPED_DEV_ZERO_FUCHSIA_H_
  5. #define BASE_TEST_SCOPED_DEV_ZERO_FUCHSIA_H_
  6. #include <lib/fdio/namespace.h>
  7. #include "base/memory/ref_counted.h"
  8. #include "base/memory/scoped_refptr.h"
  9. #include "base/threading/sequence_bound.h"
  10. #include "base/threading/thread.h"
  11. namespace base {
  12. // An object that causes /dev/zero to exist during its lifetime. A reference to
  13. // this class may be held by tests that require access to /dev/zero for the
  14. // lifetime of that need.
  15. class ScopedDevZero final : public RefCounted<ScopedDevZero> {
  16. public:
  17. REQUIRE_ADOPTION_FOR_REFCOUNTED_TYPE();
  18. // Returns a reference to the process-global /dev/zero. This must only be
  19. // called, and the returned reference released, on the main thread. Returns
  20. // null in case of failure to create the instance. It is good practice for
  21. // tests to ASSERT the returned pointer.
  22. static scoped_refptr<ScopedDevZero> Get();
  23. ScopedDevZero(const ScopedDevZero&) = delete;
  24. ScopedDevZero operator=(const ScopedDevZero&) = delete;
  25. private:
  26. friend class RefCounted<ScopedDevZero>;
  27. class Server;
  28. ScopedDevZero();
  29. ~ScopedDevZero();
  30. // Spins off the server thread and binds its pesudo-dir to /dev, returning
  31. // true if all goes well, or false in case of any error.
  32. bool Initialize();
  33. // A raw pointer to the process's single instance. Multiple references to this
  34. // instance may be handed out to consumers.
  35. static ScopedDevZero* instance_;
  36. Thread io_thread_;
  37. fdio_ns_t* global_namespace_ = nullptr;
  38. SequenceBound<Server> server_;
  39. };
  40. } // namespace base
  41. #endif // BASE_TEST_SCOPED_DEV_ZERO_FUCHSIA_H_