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

ash / components / arc / disk_space / arc_disk_space_bridge.cc [blame]

// 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 "ash/components/arc/disk_space/arc_disk_space_bridge.h"

#include <map>
#include <utility>
#include <vector>

#include "ash/components/arc/arc_browser_context_keyed_service_factory_base.h"
#include "ash/components/arc/arc_util.h"
#include "ash/components/arc/session/arc_bridge_service.h"
#include "base/functional/bind.h"
#include "base/memory/singleton.h"
#include "chromeos/ash/components/dbus/spaced/spaced_client.h"
#include "chromeos/ash/components/dbus/userdataauth/userdataauth_client.h"

namespace arc {

namespace {

// Path to query disk space and disk quota for ARC.
constexpr char kArcDiskHome[] = "/home/chronos/user";

// Singleton factory for ArcDiskSpaceBridge.
class ArcDiskSpaceBridgeFactory
    : public internal::ArcBrowserContextKeyedServiceFactoryBase<
          ArcDiskSpaceBridge,
          ArcDiskSpaceBridgeFactory> {
 public:
  // Factory name used by ArcBrowserContextKeyedServiceFactoryBase.
  static constexpr const char* kName = "ArcDiskSpaceBridgeFactory";

  static ArcDiskSpaceBridgeFactory* GetInstance() {
    return base::Singleton<ArcDiskSpaceBridgeFactory>::get();
  }

 private:
  friend base::DefaultSingletonTraits<ArcDiskSpaceBridgeFactory>;
  ArcDiskSpaceBridgeFactory() = default;
  ~ArcDiskSpaceBridgeFactory() override = default;
};

bool IsAndroidUid(uint32_t uid) {
  const uint32_t android_uid_end = GetArcAndroidSdkVersionAsInt() < kArcVersionT
                                       ? kAndroidUidEndBeforeT
                                       : kAndroidUidEndAfterT;
  return kAndroidUidStart <= uid && uid <= android_uid_end;
}

bool IsAndroidGid(uint32_t gid) {
  return kAndroidGidStart <= gid && gid <= kAndroidGidEnd;
}

bool IsAndroidProjectId(uint32_t project_id) {
  const uint32_t project_id_for_android_apps_end =
      GetArcAndroidSdkVersionAsInt() < kArcVersionT
          ? kProjectIdForAndroidAppsEndBeforeT
          : kProjectIdForAndroidAppsEndAfterT;
  return (project_id >= kProjectIdForAndroidFilesStart &&
          project_id <= kProjectIdForAndroidFilesEnd) ||
         (project_id >= kProjectIdForAndroidAppsStart &&
          project_id <= project_id_for_android_apps_end);
}

void IsQuotaSupportedOnArcDiskHome(
    ArcDiskSpaceBridge::IsQuotaSupportedCallback callback) {
  ash::SpacedClient::Get()->IsQuotaSupported(
      kArcDiskHome,
      base::BindOnce(
          [](ArcDiskSpaceBridge::IsQuotaSupportedCallback callback,
             std::optional<bool> reply) {
            LOG_IF(ERROR, !reply.has_value())
                << "Failed to retrieve result from IsQuotaSupported";
            std::move(callback).Run(reply.value_or(false));
          },
          std::move(callback)));
}

bool ValidateIds(const std::vector<uint32_t>& ids,
                 base::RepeatingCallback<bool(uint32_t)> is_in_allowed_range,
                 std::string_view id_type) {
  for (const uint32_t id : ids) {
    if (!is_in_allowed_range.Run(id)) {
      LOG(ERROR) << "Android " << id_type << " " << id
                 << " is outside the allowed query range";
      return false;
    }
  }
  return true;
}

std::vector<uint32_t> GetVecWithShiftedIds(const std::vector<uint32_t>& ids,
                                           uint32_t shift_width) {
  std::vector<uint32_t> shifted_ids(ids.size());
  std::transform(ids.begin(), ids.end(), shifted_ids.begin(),
                 [&shift_width](const auto& id) { return id + shift_width; });
  return shifted_ids;
}

std::vector<int64_t> GetSpacesForIds(const std::map<uint32_t, int64_t>& map,
                                     const std::vector<uint32_t>& ids,
                                     std::string_view id_type) {
  std::vector<int64_t> spaces;
  for (uint32_t id : ids) {
    auto iter = map.find(id);
    if (iter == map.end()) {
      LOG(ERROR) << "Space for " << id_type << " " << id << " is not found in "
                 << "the map returned from spaced";
      // Return an empty list if the result for any ID is missing.
      return std::vector<int64_t>{};
    }
    spaces.push_back(iter->second);
  }
  return spaces;
}

}  // namespace

// static
ArcDiskSpaceBridge* ArcDiskSpaceBridge::GetForBrowserContext(
    content::BrowserContext* context) {
  return ArcDiskSpaceBridgeFactory::GetForBrowserContext(context);
}

// static
ArcDiskSpaceBridge* ArcDiskSpaceBridge::GetForBrowserContextForTesting(
    content::BrowserContext* context) {
  return ArcDiskSpaceBridgeFactory::GetForBrowserContextForTesting(context);
}

ArcDiskSpaceBridge::ArcDiskSpaceBridge(content::BrowserContext* context,
                                       ArcBridgeService* bridge_service)
    : arc_bridge_service_(bridge_service) {
  arc_bridge_service_->disk_space()->AddObserver(this);
  arc_bridge_service_->disk_space()->SetHost(this);
}

ArcDiskSpaceBridge::~ArcDiskSpaceBridge() {
  arc_bridge_service_->disk_space()->SetHost(nullptr);
  arc_bridge_service_->disk_space()->RemoveObserver(this);
}

void ArcDiskSpaceBridge::IsQuotaSupported(IsQuotaSupportedCallback callback) {
  // Whether ARC quota is supported is an AND of the following two booleans:
  // * Whether there are no unmounted Android users (from cryptohome)
  // * Whether |kArcDiskHome| is mounted with quota enabled (from spaced)
  // Query cryptohome first, as the first one is more likely to be false.
  ash::UserDataAuthClient::Get()->GetArcDiskFeatures(
      user_data_auth::GetArcDiskFeaturesRequest(),
      base::BindOnce(
          [](IsQuotaSupportedCallback callback,
             std::optional<user_data_auth::GetArcDiskFeaturesReply> reply) {
            LOG_IF(ERROR, !reply.has_value())
                << "Failed to retrieve result from GetArcDiskFeatures call.";
            if (!reply.has_value() || !reply->quota_supported()) {
              std::move(callback).Run(false);
              return;
            }
            IsQuotaSupportedOnArcDiskHome(std::move(callback));
          },
          std::move(callback)));
}

void ArcDiskSpaceBridge::GetQuotaCurrentSpaceForUid(
    uint32_t android_uid,
    GetQuotaCurrentSpaceForUidCallback callback) {
  if (!IsAndroidUid(android_uid)) {
    LOG(ERROR) << "Android uid " << android_uid
               << " is outside the allowed query range";
    std::move(callback).Run(-1);
    return;
  }

  const uint32_t cros_uid = android_uid + kArcUidShift;
  ash::SpacedClient::Get()->GetQuotaCurrentSpaceForUid(
      kArcDiskHome, cros_uid,
      base::BindOnce(
          [](GetQuotaCurrentSpaceForUidCallback callback, int cros_uid,
             std::optional<int64_t> reply) {
            LOG_IF(ERROR, !reply.has_value())
                << "Failed to retrieve result from GetQuotaCurrentSpaceForUid "
                << "for uid=" << cros_uid;
            std::move(callback).Run(reply.value_or(-1));
          },
          std::move(callback), cros_uid));
}

void ArcDiskSpaceBridge::GetQuotaCurrentSpaceForGid(
    uint32_t android_gid,
    GetQuotaCurrentSpaceForGidCallback callback) {
  if (!IsAndroidGid(android_gid)) {
    LOG(ERROR) << "Android gid " << android_gid
               << " is outside the allowed query range";
    std::move(callback).Run(-1);
    return;
  }

  const uint32_t cros_gid = android_gid + kArcGidShift;
  ash::SpacedClient::Get()->GetQuotaCurrentSpaceForGid(
      kArcDiskHome, cros_gid,
      base::BindOnce(
          [](GetQuotaCurrentSpaceForGidCallback callback, int cros_gid,
             std::optional<int64_t> reply) {
            LOG_IF(ERROR, !reply.has_value())
                << "Failed to retrieve result from GetQuotaCurrentSpaceForGid "
                << "for gid=" << cros_gid;
            std::move(callback).Run(reply.value_or(-1));
          },
          std::move(callback), cros_gid));
}

void ArcDiskSpaceBridge::GetQuotaCurrentSpaceForProjectId(
    uint32_t project_id,
    GetQuotaCurrentSpaceForProjectIdCallback callback) {
  if (!IsAndroidProjectId(project_id)) {
    LOG(ERROR) << "Android project id " << project_id
               << " is outside the allowed query range";
    std::move(callback).Run(-1);
    return;
  }
  ash::SpacedClient::Get()->GetQuotaCurrentSpaceForProjectId(
      kArcDiskHome, project_id,
      base::BindOnce(
          [](GetQuotaCurrentSpaceForProjectIdCallback callback, int project_id,
             std::optional<int64_t> reply) {
            LOG_IF(ERROR, !reply.has_value())
                << "Failed to retrieve result from "
                   "GetQuotaCurrentSpaceForProjectId for project_id="
                << project_id;
            std::move(callback).Run(reply.value_or(-1));
          },
          std::move(callback), project_id));
}

void ArcDiskSpaceBridge::GetQuotaCurrentSpacesForIds(
    const std::vector<uint32_t>& android_uids,
    const std::vector<uint32_t>& android_gids,
    const std::vector<uint32_t>& android_project_ids,
    GetQuotaCurrentSpacesForIdsCallback callback) {
  if (!ValidateIds(android_uids, base::BindRepeating(&IsAndroidUid), "UID") ||
      !ValidateIds(android_gids, base::BindRepeating(&IsAndroidGid), "GID") ||
      !ValidateIds(android_project_ids,
                   base::BindRepeating(&IsAndroidProjectId), "project ID")) {
    std::move(callback).Run(nullptr);
    return;
  }
  const std::vector<uint32_t> cros_uids =
      GetVecWithShiftedIds(android_uids, kArcUidShift);
  const std::vector<uint32_t> cros_gids =
      GetVecWithShiftedIds(android_gids, kArcGidShift);
  ash::SpacedClient::Get()->GetQuotaCurrentSpacesForIds(
      kArcDiskHome, cros_uids, cros_gids, android_project_ids,
      base::BindOnce(
          [](GetQuotaCurrentSpacesForIdsCallback callback,
             const std::vector<uint32_t>& cros_uids,
             const std::vector<uint32_t>& cros_gids,
             const std::vector<uint32_t>& project_ids,
             std::optional<ash::SpacedClient::SpaceMaps> result) {
            if (!result.has_value()) {
              LOG(ERROR) << "SpacedClient::GetQuotaCurrentSpacesForIds failed";
              std::move(callback).Run(nullptr);
              return;
            }
            mojom::QuotaSpacesPtr quota_spaces = mojom::QuotaSpaces::New();
            quota_spaces->curspaces_for_uids =
                GetSpacesForIds(result->curspaces_for_uids, cros_uids, "UID");
            quota_spaces->curspaces_for_gids =
                GetSpacesForIds(result->curspaces_for_gids, cros_gids, "GID");
            quota_spaces->curspaces_for_project_ids = GetSpacesForIds(
                result->curspaces_for_project_ids, project_ids, "project ID");
            std::move(callback).Run(std::move(quota_spaces));
          },
          std::move(callback), cros_uids, cros_gids, android_project_ids));
}

void ArcDiskSpaceBridge::GetFreeDiskSpace(GetFreeDiskSpaceCallback callback) {
  ash::SpacedClient::Get()->GetFreeDiskSpace(
      kArcDiskHome,
      base::BindOnce(
          [](GetFreeDiskSpaceCallback callback, std::optional<int64_t> reply) {
            if (!reply.has_value()) {
              LOG(ERROR) << "spaced::GetFreeDiskSpace failed";
              std::move(callback).Run(nullptr);
              return;
            }

            mojom::DiskSpacePtr disk_space = mojom::DiskSpace::New();
            disk_space->space_in_bytes = reply.value();
            std::move(callback).Run(std::move(disk_space));
          },
          std::move(callback)));
}

bool ArcDiskSpaceBridge::GetApplicationsSize(
    GetApplicationsSizeCallback callback) {
  auto* disk_space_instance = ARC_GET_INSTANCE_FOR_METHOD(
      arc_bridge_service_->disk_space(), GetApplicationsSize);
  if (!disk_space_instance) {
    return false;
  }
  disk_space_instance->GetApplicationsSize(std::move(callback));
  return true;
}

void ArcDiskSpaceBridge::OnConnectionReady() {
  ash::SpacedClient::Get()->AddObserver(this);
  ash::SpacedClient::Get()->GetFreeDiskSpace(
      kArcDiskHome, base::BindOnce(&ArcDiskSpaceBridge::OnGetFreeDiskSpace,
                                   weak_factory_.GetWeakPtr()));
}

void ArcDiskSpaceBridge::OnConnectionClosed() {
  ash::SpacedClient::Get()->RemoveObserver(this);
}

void ArcDiskSpaceBridge::OnGetFreeDiskSpace(std::optional<int64_t> reply) {
  if (!reply.has_value()) {
    LOG(ERROR) << "Failed to call GetFreeDiskSpace from ArcDiskSpaceBridge";
    return;
  }
  SendResizeStorageBalloon(reply.value());
}

void ArcDiskSpaceBridge::OnSpaceUpdate(const SpaceEvent& event) {
  SendResizeStorageBalloon(event.free_space_bytes());
}

void ArcDiskSpaceBridge::SendResizeStorageBalloon(int64_t free_space_bytes) {
  auto* disk_space_instance = ARC_GET_INSTANCE_FOR_METHOD(
      arc_bridge_service_->disk_space(), ResizeStorageBalloon);
  if (!disk_space_instance) {
    return;
  }
  disk_space_instance->ResizeStorageBalloon(std::max(
      int64_t(free_space_bytes - kStorageBalloonFreeSpaceBufferSizeInBytes),
      int64_t(0)));
}

// static
void ArcDiskSpaceBridge::EnsureFactoryBuilt() {
  ArcDiskSpaceBridgeFactory::GetInstance();
}

}  // namespace arc