File: wallpaper_drivefs_delegate_impl.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 (111 lines) | stat: -rw-r--r-- 4,460 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
// Copyright 2022 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_WALLPAPER_WALLPAPER_DRIVEFS_DELEGATE_IMPL_H_
#define CHROME_BROWSER_ASH_WALLPAPER_WALLPAPER_DRIVEFS_DELEGATE_IMPL_H_

#include "ash/public/cpp/wallpaper/wallpaper_drivefs_delegate.h"

#include <map>
#include <string>
#include <vector>

#include "ash/public/cpp/image_downloader.h"
#include "base/files/file_path.h"
#include "base/functional/callback_forward.h"
#include "base/memory/scoped_refptr.h"
#include "base/memory/weak_ptr.h"
#include "base/task/sequenced_task_runner.h"
#include "base/time/time.h"
#include "chromeos/ash/components/drivefs/drivefs_host.h"
#include "chromeos/ash/components/drivefs/mojom/drivefs.mojom-forward.h"
#include "components/account_id/account_id.h"
#include "components/drive/file_errors.h"
#include "google_apis/common/api_error_codes.h"
#include "url/gurl.h"

namespace ash {

// Observes DriveFs file updates and calls `callback` when the wallpaper file
// changes. `callback` is called immediately if unable to set up an observer on
// DriveFs file changes.
class WallpaperChangeWaiter : drivefs::DriveFsHost::Observer {
 public:
  WallpaperChangeWaiter(
      const AccountId& account_id,
      WallpaperDriveFsDelegate::WaitForWallpaperChangeCallback callback);

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

  ~WallpaperChangeWaiter() override;

  // DriveFsHost::Observer implementation.
  void OnUnmounted() override;
  void OnError(const drivefs::mojom::DriveError& error) override;
  void OnFilesChanged(
      const std::vector<drivefs::mojom::FileChange>& changes) override;

 private:
  const AccountId account_id_;
  const base::FilePath path_to_watch_;
  WallpaperDriveFsDelegate::WaitForWallpaperChangeCallback callback_;
};

class WallpaperDriveFsDelegateImpl : public WallpaperDriveFsDelegate {
 public:
  WallpaperDriveFsDelegateImpl();

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

  ~WallpaperDriveFsDelegateImpl() override;

  // WallpaperDriveFsDelegate:
  base::FilePath GetWallpaperPath(const AccountId& account_id) override;
  void SaveWallpaper(const AccountId& account_id,
                     const base::FilePath& source,
                     base::OnceCallback<void(bool success)> callback) override;
  void GetWallpaperModificationTime(
      const AccountId& account_id,
      base::OnceCallback<void(base::Time modification_time)> callback) override;
  void WaitForWallpaperChange(const AccountId& account_id,
                              WaitForWallpaperChangeCallback callback) override;
  void DownloadAndDecodeWallpaper(
      const AccountId& account_id,
      ImageDownloader::DownloadCallback callback) override;

 private:
  // Called when DriveFS has replied with file metadata that contains a URL to
  // download the wallpaper file. Actually downloading the wallpaper file still
  // requires an authentication token from Drive.
  void OnGetDownloadUrlMetadata(const AccountId& account_id,
                                ImageDownloader::DownloadCallback callback,
                                drive::FileError error,
                                drivefs::mojom::FileMetadataPtr metadata);

  // Called after `OnGetDownloadUrlMetadata` and after attempting to obtain a
  // Drive authentication token. Can now attempt to download the wallpaper image
  // because `download_url` and `authentication_token` are both present.
  void OnGetDownloadUrlAndAuthentication(
      const AccountId& account_id,
      ImageDownloader::DownloadCallback callback,
      const GURL& download_url,
      google_apis::ApiErrorCode error_code,
      const std::string& authentication_token);

  void OnDriveFsWallpaperChanged(const AccountId& account_id,
                                 WaitForWallpaperChangeCallback callback,
                                 bool success);

  std::map<AccountId, WallpaperChangeWaiter> wallpaper_change_waiters_;

  scoped_refptr<base::SequencedTaskRunner> blocking_task_runner_;
  base::WeakPtrFactory<WallpaperDriveFsDelegateImpl> weak_ptr_factory_{this};
};

}  // namespace ash

#endif  // CHROME_BROWSER_ASH_WALLPAPER_WALLPAPER_DRIVEFS_DELEGATE_IMPL_H_