File: crostini_package_service.cc

package info (click to toggle)
chromium 138.0.7204.183-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 6,071,908 kB
  • sloc: cpp: 34,937,088; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,953; 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,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 (575 lines) | stat: -rw-r--r-- 21,997 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
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
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
// 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 "chrome/browser/ash/crostini/crostini_package_service.h"

#include "base/containers/contains.h"
#include "base/files/file_path.h"
#include "base/functional/bind.h"
#include "base/no_destructor.h"
#include "base/strings/stringprintf.h"
#include "base/strings/utf_string_conversions.h"
#include "chrome/browser/ash/crostini/crostini_features.h"
#include "chrome/browser/ash/file_manager/path_util.h"
#include "chrome/browser/ash/guest_os/guest_os_registry_service_factory.h"
#include "chrome/browser/ash/guest_os/guest_os_share_path.h"
#include "chrome/browser/profiles/profile.h"

namespace crostini {

namespace {

PackageOperationStatus InstallStatusToOperationStatus(
    InstallLinuxPackageProgressStatus status) {
  switch (status) {
    case InstallLinuxPackageProgressStatus::SUCCEEDED:
      return PackageOperationStatus::SUCCEEDED;
    case InstallLinuxPackageProgressStatus::FAILED:
      return PackageOperationStatus::FAILED;
    case InstallLinuxPackageProgressStatus::DOWNLOADING:
    case InstallLinuxPackageProgressStatus::INSTALLING:
      return PackageOperationStatus::RUNNING;
    default:
      NOTREACHED();
  }
}

PackageOperationStatus UninstallStatusToOperationStatus(
    UninstallPackageProgressStatus status) {
  switch (status) {
    case UninstallPackageProgressStatus::SUCCEEDED:
      return PackageOperationStatus::SUCCEEDED;
    case UninstallPackageProgressStatus::FAILED:
      return PackageOperationStatus::FAILED;
    case UninstallPackageProgressStatus::UNINSTALLING:
      return PackageOperationStatus::RUNNING;
    default:
      NOTREACHED();
  }
}

}  // namespace

struct CrostiniPackageService::QueuedInstall {
  QueuedInstall(const std::string& package_path,
                CrostiniManager::InstallLinuxPackageCallback callback,
                std::unique_ptr<CrostiniPackageNotification> notification)
      : package_path(package_path),
        callback(std::move(callback)),
        notification(std::move(notification)) {}
  ~QueuedInstall() = default;

  std::string package_path;
  CrostiniManager::InstallLinuxPackageCallback callback;

  // Notification displaying "install queued"
  std::unique_ptr<CrostiniPackageNotification> notification;
};

struct CrostiniPackageService::QueuedUninstall {
  QueuedUninstall(
      const std::string& app_id,
      std::unique_ptr<CrostiniPackageNotification> notification_argument)
      : app_id(app_id), notification(std::move(notification_argument)) {}
  ~QueuedUninstall() = default;

  // App to uninstall
  std::string app_id;

  // Notification displaying "uninstall queued"
  std::unique_ptr<CrostiniPackageNotification> notification;
};

CrostiniPackageService::CrostiniPackageService(Profile* profile)
    : profile_(profile) {
  CrostiniManager* manager = CrostiniManager::GetForProfile(profile);

  manager->AddLinuxPackageOperationProgressObserver(this);
  manager->AddPendingAppListUpdatesObserver(this);
  manager->AddVmShutdownObserver(this);
}

CrostiniPackageService::~CrostiniPackageService() = default;

void CrostiniPackageService::Shutdown() {
  CrostiniManager* manager = CrostiniManager::GetForProfile(profile_);
  manager->RemoveLinuxPackageOperationProgressObserver(this);
  manager->RemovePendingAppListUpdatesObserver(this);
  manager->RemoveVmShutdownObserver(this);

  // CrostiniPackageNotification registers itself as a GuestOsRegistryService
  // observer, so they need to be destroyed here while the
  // GuestOsRegistryService still exists.
  running_notifications_.clear();
  queued_installs_.clear();
  queued_uninstalls_.clear();
  finished_notifications_.clear();
}

void CrostiniPackageService::SetNotificationStateChangeCallbackForTesting(
    StateChangeCallback state_change_callback) {
  testing_state_change_callback_ = std::move(state_change_callback);
}

void CrostiniPackageService::NotificationCompleted(
    CrostiniPackageNotification* notification) {
  for (auto it = finished_notifications_.begin();
       it != finished_notifications_.end(); ++it) {
    if (it->get() == notification) {
      finished_notifications_.erase(it);
      return;
    }
  }
  // Notifications should never delete themselves while queued or running.
  NOTREACHED();
}

void CrostiniPackageService::GetLinuxPackageInfo(
    const guest_os::GuestId& container_id,
    const storage::FileSystemURL& package_url,
    CrostiniManager::GetLinuxPackageInfoCallback callback) {
  base::FilePath path;
  if (!file_manager::util::ConvertFileSystemURLToPathInsideCrostini(
          profile_, package_url, &path)) {
    LinuxPackageInfo info;
    info.success = false;
    info.failure_reason = "Invalid package url: " + package_url.DebugString();
    return std::move(callback).Run(info);
  }

  // Share path if it is not in crostini.
  if (package_url.mount_filesystem_id() !=
      file_manager::util::GetCrostiniMountPointName(profile_)) {
    CrostiniManager::RestartOptions options;
    options.share_paths.push_back(package_url.path());
    restart_id_for_testing_ =
        CrostiniManager::GetForProfile(profile_)->RestartCrostiniWithOptions(
            container_id, std::move(options),
            base::BindOnce(
                &CrostiniPackageService::OnSharePathForGetLinuxPackageInfo,
                weak_ptr_factory_.GetWeakPtr(), container_id, package_url, path,
                std::move(callback)));
  } else {
    OnSharePathForGetLinuxPackageInfo(container_id, package_url, path,
                                      std::move(callback),
                                      CrostiniResult::SUCCESS);
  }
}

void CrostiniPackageService::OnSharePathForGetLinuxPackageInfo(
    const guest_os::GuestId& container_id,
    const storage::FileSystemURL& package_url,
    const base::FilePath& package_path,
    CrostiniManager::GetLinuxPackageInfoCallback callback,
    CrostiniResult result) {
  if (result != CrostiniResult::SUCCESS) {
    LinuxPackageInfo info;
    info.success = false;
    info.failure_reason = "Error sharing package " + package_url.DebugString() +
                          ": " + CrostiniResultString(result);
    return std::move(callback).Run(info);
  }
  CrostiniManager::GetForProfile(profile_)->GetLinuxPackageInfo(
      container_id, package_path.value(),
      base::BindOnce(&CrostiniPackageService::OnGetLinuxPackageInfo,
                     weak_ptr_factory_.GetWeakPtr(), container_id,
                     std::move(callback)));
}

void CrostiniPackageService::QueueInstallLinuxPackage(
    const guest_os::GuestId& container_id,
    const storage::FileSystemURL& package_url,
    CrostiniManager::InstallLinuxPackageCallback callback) {
  base::FilePath path;
  if (!file_manager::util::ConvertFileSystemURLToPathInsideCrostini(
          profile_, package_url, &path)) {
    LOG(ERROR) << "Invalid install linux package: "
               << package_url.DebugString();
    return std::move(callback).Run(
        CrostiniResult::INSTALL_LINUX_PACKAGE_FAILED);
  }

  if (ContainerHasRunningOperation(container_id)) {
    CreateQueuedInstall(container_id, path.value(), std::move(callback));
    return;
  }

  CreateRunningNotification(
      container_id,
      CrostiniPackageNotification::NotificationType::PACKAGE_INSTALL,
      /*app_name=*/"");

  CrostiniManager::GetForProfile(profile_)->InstallLinuxPackage(
      container_id, path.value(),
      base::BindOnce(&CrostiniPackageService::OnInstallLinuxPackage,
                     weak_ptr_factory_.GetWeakPtr(), container_id,
                     std::move(callback)));
}

void CrostiniPackageService::OnInstallLinuxPackageProgress(
    const guest_os::GuestId& container_id,
    InstallLinuxPackageProgressStatus status,
    int progress_percent,
    const std::string& error_message) {
  // Linux package install has two phases, downloading and installing, which we
  // map to a single progess percentage amount by dividing the range in half --
  // 0-50% for the downloading phase, 51-100% for the installing phase.
  int display_progress = progress_percent / 2;
  if (status == InstallLinuxPackageProgressStatus::INSTALLING) {
    display_progress += 50;  // Second phase
  }

  UpdatePackageOperationStatus(container_id,
                               InstallStatusToOperationStatus(status),
                               display_progress, error_message);
}

void CrostiniPackageService::OnUninstallPackageProgress(
    const guest_os::GuestId& container_id,
    UninstallPackageProgressStatus status,
    int progress_percent) {
  UpdatePackageOperationStatus(
      container_id, UninstallStatusToOperationStatus(status), progress_percent);
}

void CrostiniPackageService::OnVmShutdown(const std::string& vm_name) {
  // Making a notification as failed removes it from |running_notifications_|,
  // which invalidates the iterators. To avoid this, we record all the
  // containers that just shut down before removing any notifications.
  std::vector<guest_os::GuestId> to_remove;
  for (auto& running_notification : running_notifications_) {
    if (running_notification.first.vm_name == vm_name) {
      to_remove.push_back(running_notification.first);
    }
  }
  for (auto iter : to_remove) {
    // Use a loop because removing a notification from the running set can cause
    // a queued operation to start, which will also need to be removed.
    while (ContainerHasRunningOperation(iter)) {
      UpdatePackageOperationStatus(iter, PackageOperationStatus::FAILED, 0);
    }
  }
}

void CrostiniPackageService::QueueUninstallApplication(
    const std::string& app_id) {
  auto registration =
      guest_os::GuestOsRegistryServiceFactory::GetForProfile(profile_)
          ->GetRegistration(app_id);
  if (!registration.has_value()) {
    LOG(ERROR)
        << "Tried to uninstall application that has already been uninstalled";
    return;
  }

  const guest_os::GuestId container_id(registration->VmType(),
                                       registration->VmName(),
                                       registration->ContainerName());
  const std::string app_name = registration->Name();
  if (ContainerHasRunningOperation(container_id)) {
    CreateQueuedUninstall(container_id, app_id, app_name);
    return;
  }

  CreateRunningNotification(
      container_id,
      CrostiniPackageNotification::NotificationType::APPLICATION_UNINSTALL,
      app_name);

  UninstallApplication(*registration, app_id);
}

bool CrostiniPackageService::ContainerHasRunningOperation(
    const guest_os::GuestId& container_id) const {
  return base::Contains(running_notifications_, container_id);
}

bool CrostiniPackageService::ContainerHasQueuedOperation(
    const guest_os::GuestId& container_id) const {
  return (base::Contains(queued_installs_, container_id) &&
          !queued_installs_.at(container_id).empty()) ||
         (base::Contains(queued_uninstalls_, container_id) &&
          !queued_uninstalls_.at(container_id).empty());
}

void CrostiniPackageService::CreateRunningNotification(
    const guest_os::GuestId& container_id,
    CrostiniPackageNotification::NotificationType notification_type,
    const std::string& app_name) {
  {  // Scope limit for |it|, which will become invalid shortly.
    auto it = running_notifications_.find(container_id);
    if (it != running_notifications_.end()) {
      // We could reach this if the final progress update signal from a previous
      // operation doesn't get sent, so we wouldn't end up moving the
      // previous notification out of running_notifications_. Clear it out by
      // moving to finished_notifications_.
      LOG(ERROR) << "Notification for package operation already exists.";
      it->second->ForceAllowAutoHide();
      finished_notifications_.emplace_back(std::move(it->second));
      running_notifications_.erase(it);
    }
  }

  running_notifications_[container_id] =
      std::make_unique<CrostiniPackageNotification>(
          profile_, notification_type, PackageOperationStatus::RUNNING,
          container_id, base::UTF8ToUTF16(app_name), GetUniqueNotificationId(),
          this);
}

void CrostiniPackageService::CreateQueuedUninstall(
    const guest_os::GuestId& container_id,
    const std::string& app_id,
    const std::string& app_name) {
  queued_uninstalls_[container_id].emplace(
      app_id,
      std::make_unique<CrostiniPackageNotification>(
          profile_,
          CrostiniPackageNotification::NotificationType::APPLICATION_UNINSTALL,
          PackageOperationStatus::QUEUED, container_id,
          base::UTF8ToUTF16(app_name), GetUniqueNotificationId(), this));
}

void CrostiniPackageService::CreateQueuedInstall(
    const guest_os::GuestId& container_id,
    const std::string& package,
    CrostiniManager::InstallLinuxPackageCallback callback) {
  queued_installs_[container_id].emplace(
      package, std::move(callback),
      std::make_unique<CrostiniPackageNotification>(
          profile_,
          CrostiniPackageNotification::NotificationType::PACKAGE_INSTALL,
          PackageOperationStatus::QUEUED, container_id,
          /*app_name=*/std::u16string(), GetUniqueNotificationId(), this));
}

void CrostiniPackageService::UpdatePackageOperationStatus(
    const guest_os::GuestId& container_id,
    PackageOperationStatus status,
    int progress_percent,
    const std::string& error_message) {
  // Update the notification window, if any.
  auto it = running_notifications_.find(container_id);
  if (it == running_notifications_.end()) {
    LOG(ERROR) << container_id << " has no notification to update";
    return;
  }
  if (it->second == nullptr) {
    LOG(ERROR) << container_id << " has null notification pointer";
    running_notifications_.erase(it);
    return;
  }

  // If an operation has finished, but there are still app list updates pending,
  // don't finish the flow yet.
  if (status == PackageOperationStatus::SUCCEEDED &&
      has_pending_app_list_updates_.count(container_id)) {
    status = PackageOperationStatus::WAITING_FOR_APP_REGISTRY_UPDATE;
  }

  it->second->UpdateProgress(status, progress_percent, error_message);

  if (status == PackageOperationStatus::SUCCEEDED ||
      status == PackageOperationStatus::FAILED) {
    finished_notifications_.emplace_back(std::move(it->second));
    running_notifications_.erase(it);

    // Kick off the next operation if we just finished one.
    if (ContainerHasQueuedOperation(container_id)) {
      StartQueuedOperation(container_id);
    }
  }
  if (testing_state_change_callback_) {
    testing_state_change_callback_.Run(status);
  }
}

void CrostiniPackageService::OnPendingAppListUpdates(
    const guest_os::GuestId& container_id,
    int count) {
  if (count != 0) {
    has_pending_app_list_updates_.insert(container_id);
  } else {
    has_pending_app_list_updates_.erase(container_id);
  }

  auto it = running_notifications_.find(container_id);
  if (it != running_notifications_.end()) {
    if (it->second->GetOperationStatus() ==
            PackageOperationStatus::WAITING_FOR_APP_REGISTRY_UPDATE &&
        count == 0) {
      UpdatePackageOperationStatus(container_id,
                                   PackageOperationStatus::SUCCEEDED, 100);
    }
  }
}

void CrostiniPackageService::OnGetLinuxPackageInfo(
    const guest_os::GuestId& container_id,
    CrostiniManager::GetLinuxPackageInfoCallback callback,
    const LinuxPackageInfo& linux_package_info) {
  std::move(callback).Run(linux_package_info);
}

void CrostiniPackageService::OnInstallLinuxPackage(
    const guest_os::GuestId& container_id,
    CrostiniManager::InstallLinuxPackageCallback callback,
    CrostiniResult result) {
  std::move(callback).Run(result);
  if (result != CrostiniResult::SUCCESS) {
    UpdatePackageOperationStatus(container_id, PackageOperationStatus::FAILED,
                                 0);
  }
}

void CrostiniPackageService::UninstallApplication(
    const guest_os::GuestOsRegistryService::Registration& registration,
    const std::string& app_id) {
  const guest_os::GuestId container_id(registration.VmType(),
                                       registration.VmName(),
                                       registration.ContainerName());

  // Policies can change under us, and crostini may now be forbidden.
  if (!CrostiniFeatures::Get()->IsAllowedNow(profile_)) {
    LOG(ERROR) << "Can't uninstall because policy no longer allows Crostini";
    UpdatePackageOperationStatus(container_id, PackageOperationStatus::FAILED,
                                 0);
    return;
  }

  // If Crostini is not running, launch it. This is a no-op if Crostini is
  // already running.
  CrostiniManager::GetForProfile(profile_)->RestartCrostini(
      container_id,
      base::BindOnce(&CrostiniPackageService::OnCrostiniRunningForUninstall,
                     weak_ptr_factory_.GetWeakPtr(), container_id,
                     registration.DesktopFileId()));
}

void CrostiniPackageService::OnCrostiniRunningForUninstall(
    const guest_os::GuestId& container_id,
    const std::string& desktop_file_id,
    CrostiniResult result) {
  if (result != CrostiniResult::SUCCESS) {
    LOG(ERROR) << "Failed to launch Crostini; uninstall aborted";
    UpdatePackageOperationStatus(container_id, PackageOperationStatus::FAILED,
                                 0);
    return;
  }
  CrostiniManager::GetForProfile(profile_)->UninstallPackageOwningFile(
      container_id, desktop_file_id,
      base::BindOnce(&CrostiniPackageService::OnUninstallPackageOwningFile,
                     weak_ptr_factory_.GetWeakPtr(), container_id));
}

void CrostiniPackageService::OnUninstallPackageOwningFile(
    const guest_os::GuestId& container_id,
    CrostiniResult result) {
  if (result != CrostiniResult::SUCCESS) {
    // Let user know the uninstall failed.
    UpdatePackageOperationStatus(container_id, PackageOperationStatus::FAILED,
                                 0);
    return;
  }
  // Otherwise, just leave the notification alone in the "running" state.
  // SUCCESS just means we successfully *started* the uninstall.
}

void CrostiniPackageService::StartQueuedOperation(
    const guest_os::GuestId& container_id) {
  auto uninstall_queue_iter = queued_uninstalls_.find(container_id);
  if (uninstall_queue_iter != queued_uninstalls_.end() &&
      !uninstall_queue_iter->second.empty()) {
    std::string app_id;
    std::queue<QueuedUninstall>& uninstall_queue = uninstall_queue_iter->second;
    {  // Scope |next|; it becomes an invalid reference when we pop()
      QueuedUninstall& next = uninstall_queue.front();

      next.notification->UpdateProgress(PackageOperationStatus::RUNNING, 0);
      running_notifications_.emplace(container_id,
                                     std::move(next.notification));

      app_id = next.app_id;
      uninstall_queue.pop();  // Invalidates |next|
    }

    auto registration =
        guest_os::GuestOsRegistryServiceFactory::GetForProfile(profile_)
            ->GetRegistration(app_id);

    // It's possible that some other process has uninstalled this application
    // already. If this happens, we want to skip the notification directly to
    // the success state.
    if (registration.has_value()) {
      UninstallApplication(*registration, app_id);
    } else {
      // Note that this may call StartQueuedOperation, so we must allow for
      // potential re-entrancy.
      UpdatePackageOperationStatus(container_id,
                                   PackageOperationStatus::SUCCEEDED, 100);
    }

    // As recursive calls to StartQueuedOperation might delete |uninstall_queue|
    // and invalidate |uninstall_queue_iter| we must look it up again.
    uninstall_queue_iter = queued_uninstalls_.find(container_id);
    if (uninstall_queue_iter != queued_uninstalls_.end() &&
        uninstall_queue_iter->second.empty()) {
      // Clean up memory.
      queued_uninstalls_.erase(uninstall_queue_iter);
      // Invalidates |uninstall_queue_iter|.
    }
    return;
  }

  auto install_queue_iter = queued_installs_.find(container_id);
  if (install_queue_iter != queued_installs_.end() &&
      !install_queue_iter->second.empty()) {
    std::string package_path;
    CrostiniManager::InstallLinuxPackageCallback callback;

    std::queue<QueuedInstall>& install_queue = install_queue_iter->second;
    {  // Scope |next|; it becomes an invalid reference when we pop()
      QueuedInstall& next = install_queue.front();

      next.notification->UpdateProgress(PackageOperationStatus::RUNNING, 0);
      running_notifications_.emplace(container_id,
                                     std::move(next.notification));

      package_path = std::move(next.package_path);
      callback = std::move(next.callback);
      install_queue.pop();  // Invalidates |next|
    }

    CrostiniManager::GetForProfile(profile_)->InstallLinuxPackage(
        container_id, package_path,
        base::BindOnce(&CrostiniPackageService::OnInstallLinuxPackage,
                       weak_ptr_factory_.GetWeakPtr(), container_id,
                       std::move(callback)));

    // InstallLinuxPackage shouldn't be able to recursively call this method,
    // but as future proofing consider |install_queue_iter| to be invalidated
    // anyway.
    install_queue_iter = queued_installs_.find(container_id);
    if (install_queue_iter != queued_installs_.end() &&
        install_queue_iter->second.empty()) {
      // Clean up memory.
      queued_installs_.erase(install_queue_iter);
      // Invalidates |install_queue_iter|.
    }
    return;
  }

  NOTREACHED();
}

std::string CrostiniPackageService::GetUniqueNotificationId() {
  return base::StringPrintf("crostini_package_operation_%d",
                            next_notification_id_++);
}

CrostiniManager::RestartId CrostiniPackageService::GetRestartIdForTesting() {
  return restart_id_for_testing_;
}

}  // namespace crostini