File: screen_ai_install_state.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 (108 lines) | stat: -rw-r--r-- 3,409 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
// 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_SCREEN_AI_SCREEN_AI_INSTALL_STATE_H_
#define CHROME_BROWSER_SCREEN_AI_SCREEN_AI_INSTALL_STATE_H_

#include <memory>
#include <vector>

#include "base/files/file_path.h"
#include "base/functional/callback.h"
#include "base/memory/raw_ptr.h"
#include "base/observer_list.h"
#include "base/version.h"

class PrefService;

namespace screen_ai {

class ScreenAIInstallState {
 public:
  enum class State {
    // Component does not exist on device.
    kNotDownloaded,
    // Component download is in progress.
    kDownloading,
    // Component download failed.
    kDownloadFailed,
    // Component is downloaded but not loaded yet.
    kDownloaded
  };

  class Observer : public base::CheckedObserver {
   public:
    virtual void StateChanged(State state) {}
    virtual void DownloadProgressChanged(double progress) {}
  };

  ScreenAIInstallState();
  ScreenAIInstallState(const ScreenAIInstallState&) = delete;
  ScreenAIInstallState& operator=(const ScreenAIInstallState&) = delete;
  virtual ~ScreenAIInstallState();

  static ScreenAIInstallState* GetInstance();

  // These functions are implemented in `ScreenAIDownloaderChromeOS` and
  // `ScreenAIDownloaderNonChromeOS`.
  static std::unique_ptr<ScreenAIInstallState> Create();
  static ScreenAIInstallState* CreateForTesting();

  // Verifies that the library version is compatible with current Chromium
  // version. Will be used to avoid accepting the library if a newer version is
  // expected.
  static bool VerifyLibraryVersion(const base::Version& version);

  // Returns true if the library is used recently and we need to keep it on
  // device and updated.
  static bool ShouldInstall(PrefService* local_state);

  // Stores current time in a local state preference as the last time that the
  // service is used.
  virtual void SetLastUsageTime() = 0;

  void AddObserver(Observer* observer);
  void RemoveObserver(Observer* observer);

  // Returns true if current state is suitable for trying to download.
  bool MayTryDownload();

  // Returns true if the component is downloaded and not failed to initialize.
  bool IsComponentAvailable();

  // Sets the component state and informs the observers.
  void SetState(State state);

  // Triggers component download if it's not already downloaded or is in
  // progress.
  void DownloadComponent();

  // Called by component downloaders to set download progress.
  void SetDownloadProgress(double progress);

  // Stores the path the component folder and sets the state to ready.
  void SetComponentFolder(const base::FilePath& component_folder);

  base::FilePath get_component_binary_path() { return component_binary_path_; }

  State get_state() { return state_; }

  void ResetForTesting();
  void SetStateForTesting(State state);

 private:
  // This function depends on component updater or DLC downloader and since they
  // need have dependencies on browser thread, we need to move it to another
  // build target to avoid circular dependency.
  virtual void DownloadComponentInternal() = 0;

  base::FilePath component_binary_path_;
  State state_ = State::kNotDownloaded;

  base::ObserverList<Observer> observers_;
};

}  // namespace screen_ai

#endif  // CHROME_BROWSER_SCREEN_AI_SCREEN_AI_INSTALL_STATE_H_