File: install_limiter.cc

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 (142 lines) | stat: -rw-r--r-- 4,790 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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
// 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.

#include "chrome/browser/ash/extensions/install_limiter.h"

#include <optional>
#include <string>

#include "base/files/file_util.h"
#include "base/functional/bind.h"
#include "base/task/thread_pool.h"
#include "chrome/browser/ash/extensions/install_limiter_factory.h"
#include "content/public/browser/browser_thread.h"
#include "extensions/browser/extensions_browser_client.h"

namespace extensions {

////////////////////////////////////////////////////////////////////////////////
// InstallLimiter::DeferredInstall

InstallLimiter::DeferredInstall::DeferredInstall(
    const scoped_refptr<CrxInstaller>& installer,
    const CRXFileInfo& file_info)
    : installer(installer), file_info(file_info) {}

InstallLimiter::DeferredInstall::DeferredInstall(const DeferredInstall& other) =
    default;

InstallLimiter::DeferredInstall::~DeferredInstall() = default;

////////////////////////////////////////////////////////////////////////////////
// InstallLimiter

// static
InstallLimiter* InstallLimiter::Get(Profile* profile) {
  return InstallLimiterFactory::GetForProfile(profile);
}

// static
bool InstallLimiter::ShouldDeferInstall(int64_t app_size,
                                        const std::string& app_id) {
  constexpr int64_t kBigAppSizeThreshold = 1048576;  // 1MB in bytes
  return app_size > kBigAppSizeThreshold &&
         !ExtensionsBrowserClient::Get()->IsScreensaverInDemoMode(app_id);
}

InstallLimiter::InstallLimiter() : disabled_for_test_(false) {}

InstallLimiter::~InstallLimiter() = default;

void InstallLimiter::DisableForTest() {
  disabled_for_test_ = true;
}

void InstallLimiter::Add(const scoped_refptr<CrxInstaller>& installer,
                         const CRXFileInfo& file_info) {
  DCHECK_CURRENTLY_ON(content::BrowserThread::UI);

  // No deferred installs when disabled for test.
  if (disabled_for_test_) {
    installer->InstallCrxFile(file_info);
    return;
  }

  num_installs_waiting_for_file_size_++;

  base::ThreadPool::PostTaskAndReplyWithResult(
      FROM_HERE, {base::MayBlock()}, base::GetFileSizeCallback(file_info.path),
      base::BindOnce(&InstallLimiter::AddWithSize,
                     weak_ptr_factory_.GetWeakPtr(), installer, file_info));
}

void InstallLimiter::OnAllExternalProvidersReady() {
  DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
  all_external_providers_ready_ = true;

  if (AllInstallsQueuedWithFileSize()) {
    // Stop wait timer and let install notification drive deferred installs.
    wait_timer_.Stop();
    CheckAndRunDeferrredInstalls();
  }
}

void InstallLimiter::AddWithSize(const scoped_refptr<CrxInstaller>& installer,
                                 const CRXFileInfo& file_info,
                                 std::optional<int64_t> size) {
  DCHECK_CURRENTLY_ON(content::BrowserThread::UI);

  num_installs_waiting_for_file_size_--;

  if (!ShouldDeferInstall(size.value_or(0), installer->expected_id()) ||
      AllInstallsQueuedWithFileSize()) {
    RunInstall(installer, file_info);

    // Stop wait timer and let install notification drive deferred installs.
    wait_timer_.Stop();
    return;
  }

  deferred_installs_.push(DeferredInstall(installer, file_info));

  // When there are no running installs, wait a bit before running deferred
  // installs to allow small app install to take precedence, especially when a
  // big app is the first one in the list.
  if (num_running_installs_ == 0 && !wait_timer_.IsRunning()) {
    const int kMaxWaitTimeInMs = 5000;  // 5 seconds.
    wait_timer_.Start(FROM_HERE, base::Milliseconds(kMaxWaitTimeInMs), this,
                      &InstallLimiter::CheckAndRunDeferrredInstalls);
  }
}

void InstallLimiter::CheckAndRunDeferrredInstalls() {
  if (deferred_installs_.empty() || num_running_installs_ > 0)
    return;

  const DeferredInstall& deferred = deferred_installs_.front();
  RunInstall(deferred.installer, deferred.file_info);
  deferred_installs_.pop();
}

void InstallLimiter::RunInstall(const scoped_refptr<CrxInstaller>& installer,
                                const CRXFileInfo& file_info) {
  installer->AddInstallerCallback(base::BindOnce(
      &InstallLimiter::OnInstallerDone, weak_ptr_factory_.GetWeakPtr()));
  installer->InstallCrxFile(file_info);
  num_running_installs_++;
}

void InstallLimiter::OnInstallerDone(
    const std::optional<CrxInstallError>& error) {
  CHECK(num_running_installs_ > 0);
  num_running_installs_--;
  CheckAndRunDeferrredInstalls();
}

bool InstallLimiter::AllInstallsQueuedWithFileSize() const {
  return all_external_providers_ready_ &&
         num_installs_waiting_for_file_size_ == 0;
}

}  // namespace extensions