File: drivefs_host.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 (219 lines) | stat: -rw-r--r-- 7,045 bytes parent folder | download | duplicates (7)
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
// Copyright 2018 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_HOST_H_
#define CHROMEOS_ASH_COMPONENTS_DRIVEFS_DRIVEFS_HOST_H_

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

#include "base/component_export.h"
#include "base/files/file_path.h"
#include "base/memory/raw_ptr.h"
#include "base/observer_list.h"
#include "base/observer_list_types.h"
#include "base/time/clock.h"
#include "base/time/time.h"
#include "base/timer/timer.h"
#include "chromeos/ash/components/disks/disk_mount_manager.h"
#include "chromeos/ash/components/drivefs/drivefs_auth.h"
#include "chromeos/ash/components/drivefs/drivefs_session.h"
#include "chromeos/ash/components/drivefs/mojom/drivefs.mojom.h"
#include "chromeos/components/drivefs/mojom/drivefs_native_messaging.mojom.h"
#include "mojo/public/cpp/bindings/pending_receiver.h"
#include "mojo/public/cpp/bindings/pending_remote.h"

namespace ash::disks {
class DiskMountManager;
}  // namespace ash::disks

namespace network {
class NetworkConnectionTracker;
}  // namespace network

namespace drivefs {
namespace mojom {

class DriveError;
class FileChange;
class ProgressEvent;
class SyncingStatus;

}  // namespace mojom

class DriveFsBootstrapListener;
class DriveFsSearchQuery;

enum class SyncStatus {
  kNotFound,
  kCompleted,
  kQueued,
  kInProgress,
  kError,
};
COMPONENT_EXPORT(CHROMEOS_ASH_COMPONENTS_DRIVEFS)
std::ostream& operator<<(std::ostream& os, const SyncStatus& status);

struct SyncState {
  SyncStatus status;
  float progress;  // Range: 0 to 1.
  base::FilePath path;
  base::Time last_updated;

  friend std::ostream& operator<<(std::ostream& os, const SyncState& state) {
    return os << "('" << state.path << "', " << state.status << ", "
              << (int)(state.progress * 100.f) << "%"
              << ") ";
  }
  bool operator==(const SyncState& state) const {
    return state.path == path && state.status == status &&
           std::fabs(state.progress - progress) < 1e-4;
  }

  inline static SyncState CreateNotFound(const base::FilePath path) {
    return {SyncStatus::kNotFound, 0, std::move(path)};
  }
};

// A host for a DriveFS process. In addition to managing its lifetime via
// mounting and unmounting, it also bridges between the DriveFS process and the
// file manager.
class COMPONENT_EXPORT(CHROMEOS_ASH_COMPONENTS_DRIVEFS) DriveFsHost {
 public:
  using MountObserver = DriveFsSession::MountObserver;
  using DialogHandler = base::RepeatingCallback<void(
      const mojom::DialogReason&,
      base::OnceCallback<void(mojom::DialogResult)>)>;

  class Delegate : public DriveFsAuth::Delegate {
   public:
    Delegate() = default;

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

    ~Delegate() override = default;

    virtual std::unique_ptr<DriveFsBootstrapListener> CreateMojoListener();
    virtual base::FilePath GetMyFilesPath() = 0;
    virtual std::string GetLostAndFoundDirectoryName() = 0;
    virtual bool IsVerboseLoggingEnabled() = 0;
    virtual void ConnectToExtension(
        mojom::ExtensionConnectionParamsPtr params,
        mojo::PendingReceiver<mojom::NativeMessagingPort> port,
        mojo::PendingRemote<mojom::NativeMessagingHost> host,
        mojom::DriveFsDelegate::ConnectToExtensionCallback callback) = 0;
    virtual const std::string GetMachineRootID() = 0;
    virtual void PersistMachineRootID(const std::string& id) = 0;
    virtual void PersistNotification(
        mojom::DriveFsNotificationPtr notification) = 0;
    virtual void PersistSyncErrors(
        mojom::MirrorSyncErrorListPtr error_list) = 0;
  };

  DriveFsHost(const base::FilePath& profile_path,
              Delegate* delegate,
              MountObserver* mount_observer,
              network::NetworkConnectionTracker* network_connection_tracker,
              const base::Clock* clock,
              ash::disks::DiskMountManager* disk_mount_manager,
              std::unique_ptr<base::OneShotTimer> timer);

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

  ~DriveFsHost();

  class Observer : public base::CheckedObserver {
   public:
    ~Observer() override;

    // Triggered when the observed DriveFsHost is being destroyed.
    virtual void OnHostDestroyed() {}

    virtual void OnUnmounted() {}
    virtual void OnSyncingStatusUpdate(const mojom::SyncingStatus& status) {}
    virtual void OnMirrorSyncingStatusUpdate(
        const mojom::SyncingStatus& status) {}
    virtual void OnFilesChanged(const std::vector<mojom::FileChange>& changes) {
    }
    virtual void OnError(const mojom::DriveError& error) {}
    virtual void OnItemProgress(const mojom::ProgressEvent& event) {}

    // Starts observing the given host.
    void Observe(DriveFsHost* host);

    // Stops observing the host.
    void Reset();

    // Gets a pointer to the host being observed.
    DriveFsHost* GetHost() const { return host_; }

   private:
    // The host being observed.
    raw_ptr<DriveFsHost> host_ = nullptr;
  };

  // Mount DriveFS.
  bool Mount();

  // Unmount DriveFS.
  void Unmount();

  // Returns whether DriveFS is mounted.
  bool IsMounted() const;

  // Returns the path where DriveFS is mounted.
  base::FilePath GetMountPath() const;

  // Returns the path where DriveFS keeps its data and caches.
  base::FilePath GetDataPath() const;

  mojom::DriveFs* GetDriveFsInterface() const;

  // Creates a `DriveFsSearchQuery` for the given query.
  // Returns nullptr if DriveFS is not mounted.
  std::unique_ptr<DriveFsSearchQuery> CreateSearchQuery(
      mojom::QueryParametersPtr query);
  // Starts DriveFs search query and returns whether it will be
  // performed localy or remotely. Assumes DriveFS to be mounted.
  mojom::QueryParameters::QuerySource PerformSearch(
      mojom::QueryParametersPtr query,
      mojom::SearchQuery::GetNextPageCallback callback);

  void set_dialog_handler(DialogHandler dialog_handler) {
    dialog_handler_ = dialog_handler;
  }

 private:
  class AccountTokenDelegate;
  class MountState;

  std::string GetDefaultMountDirName() const;

  SEQUENCE_CHECKER(sequence_checker_);

  // The path to the user's profile.
  const base::FilePath profile_path_;

  const raw_ptr<Delegate, DanglingUntriaged> delegate_;
  const raw_ptr<MountObserver, DanglingUntriaged> mount_observer_;
  const raw_ptr<network::NetworkConnectionTracker> network_connection_tracker_;
  const raw_ptr<const base::Clock> clock_;
  const raw_ptr<ash::disks::DiskMountManager> disk_mount_manager_;
  std::unique_ptr<base::OneShotTimer> timer_;

  std::unique_ptr<DriveFsAuth> account_token_delegate_;

  // State specific to the current mount, or null if not mounted.
  std::unique_ptr<MountState> mount_state_;

  base::ObserverList<Observer, true> observers_;
  DialogHandler dialog_handler_;
};

}  // namespace drivefs

#endif  // CHROMEOS_ASH_COMPONENTS_DRIVEFS_DRIVEFS_HOST_H_