File: update_service_internal_impl_qualifying.cc

package info (click to toggle)
chromium 138.0.7204.157-1
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 6,071,864 kB
  • sloc: cpp: 34,936,859; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,953; asm: 946,768; xml: 739,967; 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 (180 lines) | stat: -rw-r--r-- 7,006 bytes parent folder | download | duplicates (2)
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
// Copyright 2021 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/updater/update_service_internal_impl_qualifying.h"

#include <string>
#include <utility>
#include <vector>

#include "base/functional/bind.h"
#include "base/functional/callback.h"
#include "base/functional/callback_helpers.h"
#include "base/logging.h"
#include "base/memory/scoped_refptr.h"
#include "base/sequence_checker.h"
#include "base/strings/string_util.h"
#include "base/task/thread_pool.h"
#include "base/version.h"
#include "chrome/updater/branded_constants.h"
#include "chrome/updater/check_for_updates_task.h"
#include "chrome/updater/configurator.h"
#include "chrome/updater/persisted_data.h"
#include "chrome/updater/prefs.h"
#include "chrome/updater/registration_data.h"
#include "chrome/updater/update_service_impl.h"
#include "chrome/updater/update_service_internal.h"
#include "components/prefs/pref_service.h"
#include "components/update_client/crx_cache.h"

namespace updater {
namespace {

constexpr char kQualificationInitialVersion[] = "0.1";
constexpr char kQualificationUpdatesSuppressedVersion[] = "0.2";

class UpdateServiceInternalQualifyingImpl : public UpdateServiceInternal {
 public:
  UpdateServiceInternalQualifyingImpl(scoped_refptr<Configurator> config,
                                      scoped_refptr<LocalPrefs> local_prefs)
      : config_(config), local_prefs_(local_prefs) {}

  // Overrides for updater::UpdateServiceInternal.
  void Run(base::OnceClosure callback) override {
    VLOG(1) << __func__ << " (Qualifying)";
    DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
    Qualify(std::move(callback));
  }

  void Hello(base::OnceClosure callback) override {
    DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
    VLOG(1) << __func__ << " (Qualifying)";
    std::move(callback).Run();
  }

 private:
  ~UpdateServiceInternalQualifyingImpl() override {
    DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
  }

  void Qualify(base::OnceClosure callback) {
    DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
    if (attempted_qualification_) {
      std::move(callback).Run();
      return;
    }
    attempted_qualification_ = true;
    base::ThreadPool::PostTaskAndReplyWithResult(
        FROM_HERE, {base::MayBlock(), base::WithBaseSyncPrimitives()},
        base::BindOnce(&DoPlatformSpecificHealthChecks, GetUpdaterScope()),
        base::BindOnce(
            &UpdateServiceInternalQualifyingImpl::
                PlatformSpecificHealthChecksDone,
            this,
            base::BindOnce(
                &UpdateServiceInternalQualifyingImpl::QualificationDone, this,
                std::move(callback))));
  }

  void PlatformSpecificHealthChecksDone(base::OnceCallback<void(bool)> callback,
                                        bool success) {
    DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
    if (!success) {
      VLOG(1) << "Platfom-specific qualification checks failed.";
      std::move(callback).Run(false);
      return;
    }
    RegisterQualificationApp(std::move(callback));
  }

  void RegisterQualificationApp(base::OnceCallback<void(bool)> callback) {
    DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
    RegistrationRequest registration;
    registration.app_id = kQualificationAppId;

    // If the update check period is set to zero, the updater is pre-qualified
    // by registering a higher version of the qualification app. This is because
    // the qualification app update will not happen if the update check period
    // is set to zero.
    registration.version =
        base::Version(config_->NextCheckDelay().is_zero()
                          ? kQualificationUpdatesSuppressedVersion
                          : kQualificationInitialVersion);
    base::MakeRefCounted<UpdateServiceImpl>(GetUpdaterScope(), config_)
        ->RegisterApp(registration,
                      base::BindOnce(&UpdateServiceInternalQualifyingImpl::
                                         RegisterQualificationAppDone,
                                     this, std::move(callback)));
  }

  void RegisterQualificationAppDone(base::OnceCallback<void(bool)> callback,
                                    int result) {
    DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
    if (result != kRegistrationSuccess) {
      VLOG(1) << "Registration failed: " << result;
      std::move(callback).Run(false);
      return;
    }
    UpdateCheck(std::move(callback));
  }

  void UpdateCheck(base::OnceCallback<void(bool)> callback) {
    DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
    // Create a `CheckForUpdatesTask` with the local prefs' config and perform
    // an `Update` task for `kQualificationAppId`.
    base::MakeRefCounted<CheckForUpdatesTask>(
        config_, GetUpdaterScope(),
        /*task_name=*/"Update(kQualificationAppId)",
        base::BindOnce(
            &UpdateServiceImpl::Update,
            base::MakeRefCounted<UpdateServiceImpl>(GetUpdaterScope(), config_),
            base::ToLowerASCII(kQualificationAppId), "",
            UpdateService::Priority::kBackground,
            UpdateService::PolicySameVersionUpdate::kNotAllowed,
            /*language=*/"", base::DoNothing()))
        ->Run(base::BindOnce(
            &UpdateServiceInternalQualifyingImpl::UpdateCheckDone, this,
            std::move(callback)));
  }

  void UpdateCheckDone(base::OnceCallback<void(bool)> callback) {
    DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
    const base::Version qualification_app_version =
        config_->GetPersistedData()->GetProductVersion(kQualificationAppId);
    VLOG(2) << "qualification_app_version: " << qualification_app_version;
    std::move(callback).Run(qualification_app_version.CompareTo(base::Version(
                                kQualificationInitialVersion)) == 1);
  }

  void QualificationDone(base::OnceClosure callback, bool qualified) {
    DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
    VLOG(1) << "Qualification complete, qualified = " << qualified;
    local_prefs_->SetQualified(qualified);
    local_prefs_->GetPrefService()->CommitPendingWrite();
    if (qualified) {
      config_->GetCrxCache()->RemoveAll(kQualificationAppId,
                                        std::move(callback));
      return;
    }
    base::SequencedTaskRunner::GetCurrentDefault()->PostTask(
        FROM_HERE, std::move(callback));
  }

  scoped_refptr<Configurator> config_;
  scoped_refptr<LocalPrefs> local_prefs_;
  bool attempted_qualification_ = false;

  SEQUENCE_CHECKER(sequence_checker_);
};

}  // namespace

scoped_refptr<UpdateServiceInternal> MakeQualifyingUpdateServiceInternal(
    scoped_refptr<Configurator> config,
    scoped_refptr<LocalPrefs> local_prefs) {
  return base::MakeRefCounted<UpdateServiceInternalQualifyingImpl>(config,
                                                                   local_prefs);
}

}  // namespace updater