drivefs_bootstrap.h 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. // Copyright 2018 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 ASH_COMPONENTS_DRIVEFS_DRIVEFS_BOOTSTRAP_H_
  5. #define ASH_COMPONENTS_DRIVEFS_DRIVEFS_BOOTSTRAP_H_
  6. #include <memory>
  7. #include "ash/components/drivefs/mojom/drivefs.mojom.h"
  8. #include "base/component_export.h"
  9. #include "base/files/scoped_file.h"
  10. #include "base/unguessable_token.h"
  11. #include "mojo/public/cpp/bindings/pending_remote.h"
  12. #include "mojo/public/cpp/system/invitation.h"
  13. namespace drivefs {
  14. // Awaits for connection from DriveFS.
  15. class COMPONENT_EXPORT(DRIVEFS) DriveFsBootstrapListener {
  16. public:
  17. DriveFsBootstrapListener();
  18. DriveFsBootstrapListener(const DriveFsBootstrapListener&) = delete;
  19. DriveFsBootstrapListener& operator=(const DriveFsBootstrapListener&) = delete;
  20. virtual ~DriveFsBootstrapListener();
  21. const base::UnguessableToken& pending_token() const { return pending_token_; }
  22. virtual mojo::PendingRemote<mojom::DriveFsBootstrap> bootstrap();
  23. bool is_connected() const { return connected_; }
  24. protected:
  25. // Protected for stubbing out for testing.
  26. virtual void SendInvitationOverPipe(base::ScopedFD handle);
  27. private:
  28. void AcceptMojoConnection(base::ScopedFD handle);
  29. mojo::OutgoingInvitation invitation_;
  30. mojo::PendingRemote<mojom::DriveFsBootstrap> bootstrap_;
  31. // The token passed to DriveFS as part of 'source path' used to match it to
  32. // this instance.
  33. base::UnguessableToken pending_token_;
  34. bool connected_ = false;
  35. };
  36. // Establishes and holds mojo connection to DriveFS.
  37. class COMPONENT_EXPORT(DRIVEFS) DriveFsConnection {
  38. public:
  39. DriveFsConnection() = default;
  40. DriveFsConnection(const DriveFsConnection&) = delete;
  41. DriveFsConnection& operator=(const DriveFsConnection&) = delete;
  42. virtual ~DriveFsConnection() = default;
  43. virtual base::UnguessableToken Connect(mojom::DriveFsDelegate* delegate,
  44. base::OnceClosure on_disconnected) = 0;
  45. virtual mojom::DriveFs& GetDriveFs() = 0;
  46. static std::unique_ptr<DriveFsConnection> Create(
  47. std::unique_ptr<DriveFsBootstrapListener> bootstrap_listener,
  48. mojom::DriveFsConfigurationPtr config);
  49. };
  50. } // namespace drivefs
  51. #endif // ASH_COMPONENTS_DRIVEFS_DRIVEFS_BOOTSTRAP_H_