File: drive_upload_observer.h

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (128 lines) | stat: -rw-r--r-- 4,899 bytes parent folder | download | duplicates (6)
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
// Copyright 2024 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef CHROME_BROWSER_ASH_POLICY_SKYVAULT_DRIVE_UPLOAD_OBSERVER_H_
#define CHROME_BROWSER_ASH_POLICY_SKYVAULT_DRIVE_UPLOAD_OBSERVER_H_

#include "base/files/file_path.h"
#include "base/functional/callback.h"
#include "base/memory/raw_ptr.h"
#include "base/memory/ref_counted.h"
#include "chrome/browser/ash/drive/drive_integration_service.h"
#include "chrome/browser/ash/file_manager/io_task.h"
#include "chrome/browser/ash/file_manager/io_task_controller.h"
#include "chrome/browser/ash/policy/skyvault/policy_utils.h"
#include "chromeos/ash/components/drivefs/drivefs_host.h"
#include "storage/browser/file_system/file_system_context.h"

class Profile;

namespace ash::cloud_upload {

using policy::local_user_files::UploadTrigger;

// Observes the "upload to Google Drive" after the file is written to the local
// cache of Google Drive. Immediately uploads the file to Google Drive if the
// file is still queued. Instantiated by the static `Observe` method. Gets
// upload status by observing Drive events. Calls the `upload_callback_` when
// the file is successfully uploaded, which is when `DriveUploadObserver` goes
// out of scope. If the upload to Drive fails, it'll delete the file from the
// local cache.
class DriveUploadObserver
    : public base::RefCounted<DriveUploadObserver>,
      drivefs::DriveFsHost::Observer,
      drive::DriveIntegrationService::Observer,
      ::file_manager::io_task::IOTaskController::Observer {
 public:
  // Starts observing the upload of the file specified at construct time.
  static void Observe(Profile* profile,
                      base::FilePath file_path,
                      UploadTrigger trigger,
                      int64_t file_bytes,
                      base::RepeatingCallback<void(int64_t)> progress_callback,
                      base::OnceCallback<void(bool)> upload_callback);

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

  FRIEND_TEST_ALL_PREFIXES(DriveUploadObserverTest, NoSyncUpdates);
  FRIEND_TEST_ALL_PREFIXES(DriveUploadObserverTest, NoFileMetadata);

 private:
  friend base::RefCounted<DriveUploadObserver>;

  DriveUploadObserver(Profile* profile,
                      base::FilePath file_path,
                      UploadTrigger trigger,
                      int64_t file_bytes,
                      base::RepeatingCallback<void(int64_t)> progress_callback);
  ~DriveUploadObserver() override;

  void Run(base::OnceCallback<void(bool)> upload_callback);

  void OnEndUpload(bool success);

  // DriveFsHost::Observer implementation.
  void OnUnmounted() override;
  void OnSyncingStatusUpdate(
      const drivefs::mojom::SyncingStatus& status) override;
  void OnError(const drivefs::mojom::DriveError& error) override;

  // DriveIntegrationService::Observer implementation.
  void OnDriveConnectionStatusChanged(
      drive::util::ConnectionStatus status) override;

  // IOTaskController::Observer implementation.
  void OnIOTaskStatus(
      const ::file_manager::io_task::ProgressStatus& status) override;

  void OnImmediatelyUploadDone(int64_t bytes_transferred,
                               drive::FileError error);

  void StartNoSyncUpdateTimer();

  void NoSyncTimedOut();

  void OnGetDriveMetadata(drive::FileError error,
                          drivefs::mojom::FileMetadataPtr metadata);

  raw_ptr<Profile> profile_;
  scoped_refptr<storage::FileSystemContext> file_system_context_;
  const raw_ptr<drive::DriveIntegrationService> drive_integration_service_;

  // The Id of the delete task. It'll have a value only if the upload fails.
  std::optional<::file_manager::io_task::IOTaskId> observed_delete_task_id_ =
      std::nullopt;

  // The observed file local path.
  base::FilePath observed_local_path_;

  // The observed file Drive path.
  base::FilePath observed_drive_path_;

  // The size of the observed file.
  int64_t file_bytes_;

  // The event or action that initiated the file upload.
  const UploadTrigger trigger_;

  // Progress callback repeatedly run with progress updates.
  base::RepeatingCallback<void(int64_t)> progress_callback_;

  // Upload callback run once with upload success/failure.
  base::OnceCallback<void(bool)> upload_callback_;

  // If there's no sync updates received, the timer will timeout.
  base::OneShotTimer no_sync_update_timeout_;

  base::ScopedObservation<::file_manager::io_task::IOTaskController,
                          ::file_manager::io_task::IOTaskController::Observer>
      io_task_controller_observer_{this};

  base::WeakPtrFactory<DriveUploadObserver> weak_ptr_factory_{this};
};

}  // namespace ash::cloud_upload

#endif  // CHROME_BROWSER_ASH_POLICY_SKYVAULT_DRIVE_UPLOAD_OBSERVER_H_