File: fake_component_manager_ash.cc

package info (click to toggle)
chromium 138.0.7204.183-1~deb12u1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm-proposed-updates
  • size: 6,080,960 kB
  • sloc: cpp: 34,937,079; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,954; asm: 946,768; xml: 739,971; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,811; 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 (273 lines) | stat: -rw-r--r-- 9,721 bytes parent folder | download | duplicates (4)
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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
// Copyright 2018 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "components/component_updater/ash/fake_component_manager_ash.h"

#include <utility>

#include "base/files/file_path.h"
#include "base/functional/bind.h"
#include "base/functional/callback.h"
#include "base/logging.h"
#include "base/task/sequenced_task_runner.h"
#include "base/version.h"

namespace component_updater {

FakeComponentManagerAsh::ComponentInfo::ComponentInfo(
    Error load_response,
    const base::FilePath& install_path,
    const base::FilePath& mount_path)
    : load_response(load_response),
      install_path(install_path),
      mount_path(mount_path) {
  // If component load fails, neither install nor mount path should be set.
  DCHECK(load_response == Error::NONE ||
         (install_path.empty() && mount_path.empty()));
  // Component should have install path set if it's expected to be loaded.
  DCHECK(load_response != Error::NONE || (!install_path.empty()));
}

FakeComponentManagerAsh::ComponentInfo::ComponentInfo(
    Error load_response,
    const base::FilePath& install_path,
    const base::FilePath& mount_path,
    const base::Version& version)
    : load_response(load_response),
      install_path(install_path),
      mount_path(mount_path),
      version(version) {
  // If component load fails, neither install nor mount path should be set and
  // version should be invalid.
  DCHECK(load_response == Error::NONE ||
         (install_path.empty() && mount_path.empty() && !version.IsValid()));
  // Component should have install path and version set if it's expected to be
  // loaded.
  DCHECK(load_response != Error::NONE ||
         (!install_path.empty() && version.IsValid()));
}

FakeComponentManagerAsh::ComponentInfo::ComponentInfo(
    const FakeComponentManagerAsh::ComponentInfo& other) = default;

FakeComponentManagerAsh::ComponentInfo&
FakeComponentManagerAsh::ComponentInfo::operator=(
    const FakeComponentManagerAsh::ComponentInfo& other) = default;

FakeComponentManagerAsh::ComponentInfo::~ComponentInfo() = default;

FakeComponentManagerAsh::FakeComponentManagerAsh() = default;

FakeComponentManagerAsh::~FakeComponentManagerAsh() = default;

bool FakeComponentManagerAsh::FinishLoadRequest(
    const std::string& name,
    const ComponentInfo& component_info) {
  if (!pending_loads_.count(name) || pending_loads_[name].empty()) {
    LOG(ERROR) << "No pending load for " << name;
    return false;
  }

  auto& pending_load = pending_loads_[name].front();
  FinishComponentLoad(name, pending_load.mount_requested, component_info);

  LoadCallback callback = std::move(pending_load.callback);
  pending_loads_[name].pop_front();

  std::move(callback).Run(component_info.load_response,
                          component_info.load_response == Error::NONE
                              ? component_info.mount_path
                              : base::FilePath());
  return true;
}

bool FakeComponentManagerAsh::ResetComponentState(const std::string& name,
                                                   const ComponentInfo& state) {
  if (!supported_components_.count(name)) {
    return false;
  }

  installed_components_.erase(name);
  mounted_components_.erase(name);

  component_infos_.erase(name);
  component_infos_.emplace(name, ComponentInfo(state));
  return true;
}

bool FakeComponentManagerAsh::HasPendingInstall(
    const std::string& name) const {
  DCHECK(queue_load_requests_);

  const auto& it = pending_loads_.find(name);
  return it != pending_loads_.end() && !it->second.empty();
}

bool FakeComponentManagerAsh::UpdateRequested(const std::string& name) const {
  DCHECK(queue_load_requests_);

  const auto& it = pending_loads_.find(name);
  return it != pending_loads_.end() && !it->second.empty() &&
         it->second.front().needs_update;
}

void FakeComponentManagerAsh::SetDelegate(Delegate* delegate) {
  // No-op, not used by the fake.
}

void FakeComponentManagerAsh::Load(const std::string& name,
                                    MountPolicy mount_policy,
                                    UpdatePolicy update_policy,
                                    LoadCallback load_callback) {
  if (!supported_components_.count(name)) {
    base::SequencedTaskRunner::GetCurrentDefault()->PostTask(
        FROM_HERE, base::BindOnce(std::move(load_callback),
                                  Error::UNKNOWN_COMPONENT, base::FilePath()));
    return;
  }

  bool needs_update = update_policy == UpdatePolicy::kForce ||
                      (!installed_components_.count(name) &&
                       update_policy != UpdatePolicy::kSkip);

  // The request has to be handled if the component is not yet installed, or it
  // requires immediate update.
  if (needs_update || !installed_components_.count(name)) {
    HandlePendingRequest(name, mount_policy == MountPolicy::kMount,
                         needs_update, std::move(load_callback));
    return;
  }

  // Handle request if the component has yet to be mounted - e.g. if previous
  // loads installed the component without mounting it.
  if (!mounted_components_.count(name) && mount_policy == MountPolicy::kMount) {
    HandlePendingRequest(name, true /*mount_requested*/, false /*needs_update*/,
                         std::move(load_callback));
    return;
  }

  // The component has been prevoiusly installed, and mounted as required by
  // this load request - run the callback according to the existing state.
  base::SequencedTaskRunner::GetCurrentDefault()->PostTask(
      FROM_HERE, base::BindOnce(std::move(load_callback), Error::NONE,
                                mount_policy == MountPolicy::kMount
                                    ? mounted_components_[name]
                                    : base::FilePath()));
}

bool FakeComponentManagerAsh::Unload(const std::string& name) {
  {
    base::AutoLock lock(registered_components_lock_);
    registered_components_.erase(name);
  }
  mounted_components_.erase(name);
  installed_components_.erase(name);
  return unload_component_result_;
}

void FakeComponentManagerAsh::RegisterCompatiblePath(
    const std::string& name,
    CompatibleComponentInfo info) {
  installed_components_[name] = std::move(info);
}

void FakeComponentManagerAsh::UnregisterCompatiblePath(
    const std::string& name) {
  installed_components_.erase(name);
}

base::FilePath FakeComponentManagerAsh::GetCompatiblePath(
    const std::string& name) const {
  const auto& it = installed_components_.find(name);
  if (it == installed_components_.end()) {
    return base::FilePath();
  }
  return it->second.path;
}

void FakeComponentManagerAsh::SetRegisteredComponents(
    const std::set<std::string>& components) {
  base::AutoLock lock(registered_components_lock_);
  registered_components_ = components;
}

bool FakeComponentManagerAsh::IsRegisteredMayBlock(const std::string& name) {
  base::AutoLock lock(registered_components_lock_);
  return registered_components_.count(name);
}

void FakeComponentManagerAsh::RegisterInstalled() {
  NOTIMPLEMENTED();
}

void FakeComponentManagerAsh::GetVersion(
    const std::string& name,
    base::OnceCallback<void(const base::Version&)> version_callback) const {
  const auto& component_info = component_infos_.find(name);
  if (component_info == component_infos_.end() ||
      !component_info->second.version.has_value()) {
    std::move(version_callback).Run(base::Version());
    return;
  }

  std::move(version_callback).Run(component_info->second.version.value());
}

FakeComponentManagerAsh::LoadRequest::LoadRequest(bool mount_requested,
                                                   bool needs_update,
                                                   LoadCallback callback)
    : mount_requested(mount_requested),
      needs_update(needs_update),
      callback(std::move(callback)) {}

FakeComponentManagerAsh::LoadRequest::~LoadRequest() = default;

void FakeComponentManagerAsh::HandlePendingRequest(const std::string& name,
                                                    bool mount_requested,
                                                    bool needs_update,
                                                    LoadCallback callback) {
  if (queue_load_requests_) {
    pending_loads_[name].emplace_back(mount_requested, needs_update,
                                      std::move(callback));
    return;
  }

  const auto& component_info = component_infos_.find(name);
  if (component_info == component_infos_.end()) {
    base::SequencedTaskRunner::GetCurrentDefault()->PostTask(
        FROM_HERE, base::BindOnce(std::move(callback), Error::INSTALL_FAILURE,
                                  base::FilePath()));
    return;
  }

  FinishComponentLoad(name, mount_requested, component_info->second);

  base::SequencedTaskRunner::GetCurrentDefault()->PostTask(
      FROM_HERE,
      base::BindOnce(std::move(callback), component_info->second.load_response,
                     component_info->second.mount_path));
}

void FakeComponentManagerAsh::FinishComponentLoad(
    const std::string& name,
    bool mount_requested,
    const ComponentInfo& component_info) {
  {
    base::AutoLock lock(registered_components_lock_);
    registered_components_.insert(name);
  }

  if (component_info.load_response != Error::NONE) {
    return;
  }

  DCHECK_EQ(mount_requested, !component_info.mount_path.empty());
  installed_components_[name] = CompatibleComponentInfo(
      component_info.install_path, component_info.version);
  if (mount_requested) {
    mounted_components_[name] = component_info.mount_path;
  }
}

}  // namespace component_updater