File: customization_wallpaper_downloader.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 (112 lines) | stat: -rw-r--r-- 3,644 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
// Copyright 2014 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_CUSTOMIZATION_CUSTOMIZATION_WALLPAPER_DOWNLOADER_H_
#define CHROME_BROWSER_ASH_CUSTOMIZATION_CUSTOMIZATION_WALLPAPER_DOWNLOADER_H_

#include <stddef.h>

#include "base/files/file_path.h"
#include "base/functional/bind.h"
#include "base/memory/weak_ptr.h"
#include "base/time/time.h"
#include "base/timer/timer.h"
#include "services/network/public/cpp/simple_url_loader.h"
#include "url/gurl.h"

namespace ash {

// Download customized wallpaper.
// Owner of this class must provide callback, which will be called on
// finished (either successful or failed) wallpaper download.
class CustomizationWallpaperDownloader {
 public:
  // - |wallpaper_url| - wallpaper URL to download.
  // - |wallpaper_dir| - directory, where wallpaper will be downloaded
  // (it will be created).
  // - |wallpaper_downloaded_file| - full path to local file to store downloaded
  // wallpaper file. File is downloaded to temporary location
  // |wallpaper_downloaded_file| + ".tmp", so directory must be writable.
  // After download is completed, temporary file will be renamed to
  // |wallpaper_downloaded_file|.
  CustomizationWallpaperDownloader(
      const GURL& wallpaper_url,
      const base::FilePath& wallpaper_dir,
      const base::FilePath& wallpaper_downloaded_file,
      base::OnceCallback<void(bool success, const GURL&)>
          on_wallpaper_fetch_completed);

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

  ~CustomizationWallpaperDownloader();

  // Start download.
  void Start();

  // This is called in tests to modify (lower) retry delay.
  void set_retry_delay_for_testing(base::TimeDelta value) {
    retry_delay_ = value;
  }

  base::TimeDelta retry_current_delay_for_testing() const {
    return retry_current_delay_;
  }

 private:
  // Start new request.
  void StartRequest();

  // Schedules retry.
  void Retry();

  // This is called when the download has finished.
  void OnSimpleLoaderComplete(base::FilePath file_path);

  // Called on UI thread.
  void OnWallpaperDirectoryCreated(std::unique_ptr<bool> success);

  // Called on UI thread.
  void OnTemporaryFileRenamed(std::unique_ptr<bool> success);

  // This loader is used to download wallpaper file.
  std::unique_ptr<network::SimpleURLLoader> simple_loader_;

  // The wallpaper URL to fetch.
  const GURL wallpaper_url_;

  // Wallpaper directory (to be created).
  const base::FilePath wallpaper_dir_;

  // Full path to local file to save downloaded wallpaper.
  const base::FilePath wallpaper_downloaded_file_;

  // Full path to temporary file to fetch downloaded wallpper.
  const base::FilePath wallpaper_temporary_file_;

  // Pending retry.
  base::OneShotTimer request_scheduled_;

  // Number of download retries (first attempt is not counted as retry).
  size_t retries_;

  // Sleep between retry requests (increasing, see Retry() method for details).
  // Non-constant value for tests.
  base::TimeDelta retry_delay_;

  // Retry delay of the last attempt. For testing only.
  base::TimeDelta retry_current_delay_;

  // Callback supplied by caller.
  base::OnceCallback<void(bool success, const GURL&)>
      on_wallpaper_fetch_completed_;

  base::WeakPtrFactory<CustomizationWallpaperDownloader> weak_factory_{this};
};

}  // namespace ash

#endif  // CHROME_BROWSER_ASH_CUSTOMIZATION_CUSTOMIZATION_WALLPAPER_DOWNLOADER_H_