drivefs_host.h 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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_HOST_H_
  5. #define ASH_COMPONENTS_DRIVEFS_DRIVEFS_HOST_H_
  6. #include <memory>
  7. #include <string>
  8. #include <vector>
  9. #include "ash/components/disks/disk_mount_manager.h"
  10. #include "ash/components/drivefs/drivefs_auth.h"
  11. #include "ash/components/drivefs/drivefs_session.h"
  12. #include "ash/components/drivefs/mojom/drivefs.mojom.h"
  13. #include "base/component_export.h"
  14. #include "base/files/file_path.h"
  15. #include "base/files/scoped_file.h"
  16. #include "base/observer_list.h"
  17. #include "base/time/clock.h"
  18. #include "base/timer/timer.h"
  19. #include "components/account_id/account_id.h"
  20. #include "mojo/public/cpp/bindings/pending_receiver.h"
  21. #include "mojo/public/cpp/bindings/pending_remote.h"
  22. namespace ash {
  23. namespace disks {
  24. class DiskMountManager;
  25. } // namespace disks
  26. } // namespace ash
  27. namespace drive {
  28. class DriveNotificationManager;
  29. } // namespace drive
  30. namespace network {
  31. class NetworkConnectionTracker;
  32. } // namespace network
  33. namespace drivefs {
  34. class DriveFsBootstrapListener;
  35. class DriveFsHostObserver;
  36. // A host for a DriveFS process. In addition to managing its lifetime via
  37. // mounting and unmounting, it also bridges between the DriveFS process and the
  38. // file manager.
  39. class COMPONENT_EXPORT(DRIVEFS) DriveFsHost {
  40. public:
  41. using MountObserver = DriveFsSession::MountObserver;
  42. using DialogHandler = base::RepeatingCallback<void(
  43. const mojom::DialogReason&,
  44. base::OnceCallback<void(mojom::DialogResult)>)>;
  45. class Delegate : public DriveFsAuth::Delegate {
  46. public:
  47. Delegate() = default;
  48. Delegate(const Delegate&) = delete;
  49. Delegate& operator=(const Delegate&) = delete;
  50. ~Delegate() override = default;
  51. virtual drive::DriveNotificationManager& GetDriveNotificationManager() = 0;
  52. virtual std::unique_ptr<DriveFsBootstrapListener> CreateMojoListener();
  53. virtual base::FilePath GetMyFilesPath() = 0;
  54. virtual std::string GetLostAndFoundDirectoryName() = 0;
  55. virtual bool IsVerboseLoggingEnabled() = 0;
  56. virtual mojom::DriveFsDelegate::ExtensionConnectionStatus
  57. ConnectToExtension(
  58. mojom::ExtensionConnectionParamsPtr params,
  59. mojo::PendingReceiver<mojom::NativeMessagingPort> port,
  60. mojo::PendingRemote<mojom::NativeMessagingHost> host) = 0;
  61. virtual const std::string GetMachineRootID() = 0;
  62. virtual void PersistMachineRootID(const std::string& id) = 0;
  63. };
  64. DriveFsHost(const base::FilePath& profile_path,
  65. Delegate* delegate,
  66. MountObserver* mount_observer,
  67. network::NetworkConnectionTracker* network_connection_tracker,
  68. const base::Clock* clock,
  69. ash::disks::DiskMountManager* disk_mount_manager,
  70. std::unique_ptr<base::OneShotTimer> timer);
  71. DriveFsHost(const DriveFsHost&) = delete;
  72. DriveFsHost& operator=(const DriveFsHost&) = delete;
  73. ~DriveFsHost();
  74. void AddObserver(DriveFsHostObserver* observer);
  75. void RemoveObserver(DriveFsHostObserver* observer);
  76. // Mount DriveFS.
  77. bool Mount();
  78. // Unmount DriveFS.
  79. void Unmount();
  80. // Returns whether DriveFS is mounted.
  81. bool IsMounted() const;
  82. // Returns the path where DriveFS is mounted.
  83. base::FilePath GetMountPath() const;
  84. // Returns the path where DriveFS keeps its data and caches.
  85. base::FilePath GetDataPath() const;
  86. mojom::DriveFs* GetDriveFsInterface() const;
  87. // Starts DriveFs search query and returns whether it will be
  88. // performed localy or remotely. Assumes DriveFS to be mounted.
  89. mojom::QueryParameters::QuerySource PerformSearch(
  90. mojom::QueryParametersPtr query,
  91. mojom::SearchQuery::GetNextPageCallback callback);
  92. void set_dialog_handler(DialogHandler dialog_handler) {
  93. dialog_handler_ = dialog_handler;
  94. }
  95. private:
  96. class AccountTokenDelegate;
  97. class MountState;
  98. std::string GetDefaultMountDirName() const;
  99. SEQUENCE_CHECKER(sequence_checker_);
  100. // The path to the user's profile.
  101. const base::FilePath profile_path_;
  102. Delegate* const delegate_;
  103. MountObserver* const mount_observer_;
  104. network::NetworkConnectionTracker* const network_connection_tracker_;
  105. const base::Clock* const clock_;
  106. ash::disks::DiskMountManager* const disk_mount_manager_;
  107. std::unique_ptr<base::OneShotTimer> timer_;
  108. std::unique_ptr<DriveFsAuth> account_token_delegate_;
  109. // State specific to the current mount, or null if not mounted.
  110. std::unique_ptr<MountState> mount_state_;
  111. base::ObserverList<DriveFsHostObserver>::Unchecked observers_;
  112. DialogHandler dialog_handler_;
  113. };
  114. } // namespace drivefs
  115. #endif // ASH_COMPONENTS_DRIVEFS_DRIVEFS_HOST_H_