summaryrefslogtreecommitdiff
path: root/chromium/components/arc/session/arc_data_remover.cc
blob: c6e991147183fa1360e60945d635c4d988528b84 (plain)
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
// Copyright 2017 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "components/arc/session/arc_data_remover.h"

#include <utility>

#include "base/bind.h"
#include "base/callback_helpers.h"
#include "base/logging.h"
#include "chromeos/dbus/dbus_thread_manager.h"
#include "chromeos/dbus/debug_daemon_client.h"
#include "chromeos/dbus/upstart/upstart_client.h"
#include "components/arc/arc_prefs.h"
#include "components/arc/arc_util.h"

namespace arc {
namespace {

chromeos::ConciergeClient* GetConciergeClient() {
  return chromeos::DBusThreadManager::Get()->GetConciergeClient();
}

}  // namespace

// The conversion of upstart job names to dbus object paths is undocumented. See
// function nih_dbus_path in libnih for the implementation.
constexpr char kArcRemoveDataUpstartJob[] = "arc_2dremove_2ddata";

ArcDataRemover::ArcDataRemover(PrefService* prefs,
                               const cryptohome::Identification& cryptohome_id)
    : cryptohome_id_(cryptohome_id), weak_factory_(this) {
  pref_.Init(prefs::kArcDataRemoveRequested, prefs);
}

ArcDataRemover::~ArcDataRemover() = default;

void ArcDataRemover::Schedule() {
  DCHECK_CALLED_ON_VALID_THREAD(thread_checker_);
  pref_.SetValue(true);
}

bool ArcDataRemover::IsScheduledForTesting() const {
  DCHECK_CALLED_ON_VALID_THREAD(thread_checker_);
  return pref_.GetValue();
}

void ArcDataRemover::Run(RunCallback callback) {
  DCHECK_CALLED_ON_VALID_THREAD(thread_checker_);
  if (!pref_.GetValue()) {
    // Data removal is not scheduled.
    std::move(callback).Run(base::nullopt);
    return;
  }

  // TODO(yusukes): Stop special-casing ARCVM once we use virtio-fs for
  // exporting /data.
  if (IsArcVmEnabled()) {
    VLOG(1) << "Starting ARCVM data removal";
    chromeos::DBusThreadManager::Get()->GetDebugDaemonClient()->StartConcierge(
        base::BindOnce(&ArcDataRemover::OnConciergeStarted,
                       weak_factory_.GetWeakPtr(), std::move(callback)));
    return;
  }

  VLOG(1) << "Starting ARC data removal";
  auto* upstart_client = chromeos::UpstartClient::Get();
  if (!upstart_client) {
    // May be null in tests
    std::move(callback).Run(base::nullopt);
    return;
  }
  const std::string account_id =
      cryptohome::CreateAccountIdentifierFromIdentification(cryptohome_id_)
          .account_id();
  upstart_client->StartJob(
      kArcRemoveDataUpstartJob, {"CHROMEOS_USER=" + account_id},
      base::AdaptCallbackForRepeating(
          base::BindOnce(&ArcDataRemover::OnDataRemoved,
                         weak_factory_.GetWeakPtr(), std::move(callback))));
}

void ArcDataRemover::OnDataRemoved(RunCallback callback, bool success) {
  DCHECK_CALLED_ON_VALID_THREAD(thread_checker_);

  if (success) {
    VLOG(1) << "ARC data removal successful";
  } else {
    LOG(ERROR) << "Request for ARC user data removal failed. "
               << "See upstart logs for more details.";
  }
  pref_.SetValue(false);

  std::move(callback).Run(success);
}

void ArcDataRemover::OnConciergeStarted(RunCallback callback, bool success) {
  if (!success) {
    LOG(ERROR) << "Failed to start Concierge service for arcvm";
    OnDataRemoved(std::move(callback), false);
    return;
  }
  vm_tools::concierge::DestroyDiskImageRequest request;
  request.set_cryptohome_id(user_id_hash_);
  request.set_disk_path(kArcVmName);
  GetConciergeClient()->DestroyDiskImage(
      std::move(request),
      base::BindOnce(&ArcDataRemover::OnDiskImageDestroyed,
                     weak_factory_.GetWeakPtr(), std::move(callback)));
}

void ArcDataRemover::OnDiskImageDestroyed(
    RunCallback callback,
    base::Optional<vm_tools::concierge::DestroyDiskImageResponse> reply) {
  if (!reply) {
    LOG(ERROR) << "Failed to destroy disk image. Empty response.";
    OnDataRemoved(std::move(callback), false);
    return;
  }
  if (reply->status() != vm_tools::concierge::DISK_STATUS_DESTROYED) {
    LOG(ERROR) << "Failed to destroy disk image: " << reply->failure_reason();
    OnDataRemoved(std::move(callback), false);
    return;
  }
  OnDataRemoved(std::move(callback), true);
}

}  // namespace arc