scoped_file_access.h 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. // Copyright 2022 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 COMPONENTS_FILE_ACCESS_SCOPED_FILE_ACCESS_H_
  5. #define COMPONENTS_FILE_ACCESS_SCOPED_FILE_ACCESS_H_
  6. #include "base/component_export.h"
  7. #include "build/build_config.h"
  8. #if BUILDFLAG(IS_POSIX) || BUILDFLAG(IS_FUCHSIA)
  9. #include "base/files/scoped_file.h"
  10. #endif
  11. namespace file_access {
  12. // Move-only object to handle access token to open files.
  13. // After the object destruction, the caller may lose access to open some of the
  14. // requested files.
  15. // Platform-dependant as holds ScopedFD as a token, when supported.
  16. class COMPONENT_EXPORT(FILE_ACCESS) ScopedFileAccess {
  17. public:
  18. ScopedFileAccess(ScopedFileAccess&& other);
  19. ScopedFileAccess& operator=(ScopedFileAccess&& other);
  20. ScopedFileAccess(const ScopedFileAccess&) = delete;
  21. ScopedFileAccess& operator=(const ScopedFileAccess&) = delete;
  22. ~ScopedFileAccess();
  23. bool is_allowed() const { return allowed_; }
  24. #if BUILDFLAG(IS_POSIX) || BUILDFLAG(IS_FUCHSIA)
  25. ScopedFileAccess(bool allowed, base::ScopedFD fd);
  26. #else
  27. ScopedFileAccess(bool allowed);
  28. #endif
  29. // Object identifying allowed access.
  30. static ScopedFileAccess Allowed();
  31. private:
  32. bool allowed_;
  33. #if BUILDFLAG(IS_POSIX) || BUILDFLAG(IS_FUCHSIA)
  34. // Holds access token. When closed, access may be revoked.
  35. base::ScopedFD lifeline_fd_;
  36. #endif
  37. };
  38. } // namespace file_access
  39. #endif // COMPONENTS_FILE_ACCESS_SCOPED_FILE_ACCESS_H_