File: install_limiter.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 (105 lines) | stat: -rw-r--r-- 3,673 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
// Copyright 2013 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_EXTENSIONS_INSTALL_LIMITER_H_
#define CHROME_BROWSER_ASH_EXTENSIONS_INSTALL_LIMITER_H_

#include <stdint.h>

#include <optional>
#include <set>

#include "base/containers/queue.h"
#include "base/memory/ref_counted.h"
#include "base/memory/weak_ptr.h"
#include "base/timer/timer.h"
#include "chrome/browser/extensions/crx_installer.h"
#include "components/keyed_service/core/keyed_service.h"

namespace extensions {

// InstallLimiter defers big app installs after all small app installs and then
// runs big app installs one by one. This improves first-time login experience.
// See http://crbug.com/166296
class InstallLimiter : public KeyedService {
 public:
  static InstallLimiter* Get(Profile* profile);

  // Install should be deferred if the size is larger than 1MB and the app is
  // not the screensaver in demo mode (which requires instant installation to
  // avoid visual delay).
  static bool ShouldDeferInstall(int64_t app_size, const std::string& app_id);

  InstallLimiter();

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

  ~InstallLimiter() override;

  void DisableForTest();

  void Add(const scoped_refptr<CrxInstaller>& installer,
           const CRXFileInfo& file_info);

  // Triggers installation of deferred installations if all file sizes for
  // added installations have been determined.
  void OnAllExternalProvidersReady();

 private:
  // DeferredInstall holds info to run a CrxInstaller later.
  struct DeferredInstall {
    DeferredInstall(const scoped_refptr<CrxInstaller>& installer,
                    const CRXFileInfo& file_info);
    DeferredInstall(const DeferredInstall& other);
    ~DeferredInstall();

    const scoped_refptr<CrxInstaller> installer;
    const CRXFileInfo file_info;
  };

  using DeferredInstallList = base::queue<DeferredInstall>;

  // Adds install info with size. If |size| is greater than a certain threshold,
  // it stores the install info into |deferred_installs_| to run it later.
  // Otherwise, it just runs the installer.
  void AddWithSize(const scoped_refptr<CrxInstaller>& installer,
                   const CRXFileInfo& file_info,
                   std::optional<int64_t> size);

  // Checks and runs deferred big app installs when appropriate.
  void CheckAndRunDeferrredInstalls();

  // Starts install using passed-in info and observes |installer|'s done
  // notification.
  void RunInstall(const scoped_refptr<CrxInstaller>& installer,
                  const CRXFileInfo& file_info);

  // Called when CrxInstaller::InstallCrx() finishes.
  void OnInstallerDone(const std::optional<CrxInstallError>& error);

  // Checks that OnAllExternalProvidersReady() has been called and all file
  // sizes for added installations are determined. If this method returns true,
  // we can directly continue installing all remaining extensions, since there
  // will be no more added installations coming.
  bool AllInstallsQueuedWithFileSize() const;

  DeferredInstallList deferred_installs_;
  // Counts how many installs are currently running.
  uint32_t num_running_installs_ = 0;

  // A timer to wait before running deferred big app install.
  base::OneShotTimer wait_timer_;

  bool disabled_for_test_;

  bool all_external_providers_ready_ = false;
  int num_installs_waiting_for_file_size_ = 0;

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

}  // namespace extensions

#endif  // CHROME_BROWSER_ASH_EXTENSIONS_INSTALL_LIMITER_H_