File: drivefs_session.h

package info (click to toggle)
chromium 138.0.7204.183-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 6,071,908 kB
  • sloc: cpp: 34,937,088; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,953; asm: 946,768; xml: 739,971; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,806; php: 13,980; tcl: 13,166; yacc: 8,925; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (127 lines) | stat: -rw-r--r-- 4,209 bytes parent folder | download | duplicates (8)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
// Copyright 2019 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef CHROMEOS_ASH_COMPONENTS_DRIVEFS_DRIVEFS_SESSION_H_
#define CHROMEOS_ASH_COMPONENTS_DRIVEFS_DRIVEFS_SESSION_H_

#include <memory>
#include <string>
#include <vector>

#include "base/component_export.h"
#include "base/memory/raw_ptr.h"
#include "base/memory/weak_ptr.h"
#include "base/timer/timer.h"
#include "base/unguessable_token.h"
#include "chromeos/ash/components/disks/disk_mount_manager.h"
#include "chromeos/ash/components/drivefs/mojom/drivefs.mojom.h"

namespace drivefs {

// Utility class to simplify mounting with DiskMountManager.
class COMPONENT_EXPORT(CHROMEOS_ASH_COMPONENTS_DRIVEFS) DiskMounter {
 public:
  DiskMounter() = default;

  DiskMounter(const DiskMounter&) = delete;
  DiskMounter& operator=(const DiskMounter&) = delete;

  virtual ~DiskMounter() = default;
  virtual void Mount(const base::UnguessableToken& token,
                     const base::FilePath& data_path,
                     const base::FilePath& my_files_path,
                     const std::string& desired_mount_dir_name,
                     base::OnceCallback<void(base::FilePath)> callback) = 0;

  static std::unique_ptr<DiskMounter> Create(
      ash::disks::DiskMountManager* disk_mount_manager);
};

class DriveFsConnection;

// Represents single Drive mount session. Hides the complexity
// of determining whether DriveFs is mounted or not.
class COMPONENT_EXPORT(CHROMEOS_ASH_COMPONENTS_DRIVEFS) DriveFsSession
    : public mojom::DriveFsDelegate {
 public:
  class MountObserver {
   public:
    enum class MountFailure {
      kUnknown,
      kNeedsRestart,
      kIpcDisconnect,
      kInvocation,
      kTimeout,
    };

    MountObserver() = default;

    MountObserver(const MountObserver&) = delete;
    MountObserver& operator=(const MountObserver&) = delete;

    virtual ~MountObserver() = default;
    virtual void OnMounted(const base::FilePath& mount_path) = 0;
    virtual void OnUnmounted(std::optional<base::TimeDelta> remount_delay) = 0;
    virtual void OnMountFailed(
        MountFailure failure,
        std::optional<base::TimeDelta> remount_delay) = 0;
  };

  DriveFsSession(base::OneShotTimer* timer,
                 std::unique_ptr<DiskMounter> disk_mounter,
                 std::unique_ptr<DriveFsConnection> connection,
                 const base::FilePath& data_path,
                 const base::FilePath& my_files_path,
                 const std::string& desired_mount_dir_name,
                 MountObserver* observer);

  DriveFsSession(const DriveFsSession&) = delete;
  DriveFsSession& operator=(const DriveFsSession&) = delete;

  ~DriveFsSession() override;

  // Returns whether DriveFS is mounted.
  bool is_mounted() const { return is_mounted_; }

  // Returns the path where DriveFS is mounted.
  const base::FilePath& mount_path() const { return mount_path_; }

  mojom::DriveFs* drivefs_interface() { return drivefs_; }

 private:
  // mojom::DriveFsDelegate:
  void OnMounted() final;
  void OnMountFailed(std::optional<base::TimeDelta> remount_delay) final;
  void OnUnmounted(std::optional<base::TimeDelta> remount_delay) final;
  void OnHeartbeat() final;

  void OnDiskMountCompleted(base::FilePath mount_path);
  void OnMojoConnectionError();
  void OnMountTimedOut();
  void MaybeNotifyOnMounted();
  void NotifyFailed(MountObserver::MountFailure failure,
                    std::optional<base::TimeDelta> remount_delay);
  void NotifyUnmounted(std::optional<base::TimeDelta> remount_delay);

  SEQUENCE_CHECKER(sequence_checker_);

  const raw_ptr<base::OneShotTimer> timer_;
  std::unique_ptr<DiskMounter> disk_mounter_;
  std::unique_ptr<DriveFsConnection> connection_;
  const raw_ptr<MountObserver> observer_;

  // The path where DriveFS is mounted.
  base::FilePath mount_path_;

  // Mojo interface to the DriveFS process.
  raw_ptr<mojom::DriveFs, DanglingUntriaged> drivefs_ = nullptr;

  bool drivefs_has_started_ = false;
  bool drivefs_has_terminated_ = false;
  bool is_mounted_ = false;
};

}  // namespace drivefs

#endif  // CHROMEOS_ASH_COMPONENTS_DRIVEFS_DRIVEFS_SESSION_H_