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

ash / components / arc / arc_features_parser.h [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.

#ifndef ASH_COMPONENTS_ARC_ARC_FEATURES_PARSER_H_
#define ASH_COMPONENTS_ARC_ARC_FEATURES_PARSER_H_

#include <map>
#include <optional>
#include <string>
#include <string_view>
#include <vector>

#include "base/functional/callback_forward.h"

namespace arc {

// Build properties that have been extracted from the ARC feature file.
struct BuildPropsMapping {
  BuildPropsMapping();

  BuildPropsMapping(const BuildPropsMapping&);
  BuildPropsMapping& operator=(const BuildPropsMapping&);

  BuildPropsMapping(BuildPropsMapping&& other);
  BuildPropsMapping& operator=(BuildPropsMapping&& other);

  ~BuildPropsMapping();

  // Build fingerprint from the "ro.build.fingerprint" property. e.g.
  // "google/hatch/hatch_cheets:13/R118-15602.0.0/10753998:userdebug/dev-keys".
  std::string fingerprint;

  // SDK version from the "ro.build.version.sdk" property. e.g. "33".
  std::string sdk_version;

  // Human-readable release version number from the "ro.build.version.release"
  // property. e.g. "13".
  std::string release_version;

  // ABI list from the "ro.product.cpu.abilist" property. e.g.
  // "x86_64,arm64-v8a,x86,armeabi-v7a,armeabi"
  std::string abi_list;
};

// This struct contains an ARC available feature map, unavailable feature set
// and ARC build property map.
struct ArcFeatures {
  // Key is the feature name. Value is the feature version.
  using FeatureVersionMapping = std::map<std::string, int>;

  // Each item in the vector is the feature name.
  using FeatureList = std::vector<std::string>;

  ArcFeatures();

  ArcFeatures(const ArcFeatures&) = delete;
  ArcFeatures& operator=(const ArcFeatures&) = delete;

  ArcFeatures(ArcFeatures&& other);
  ArcFeatures& operator=(ArcFeatures&& other);

  ~ArcFeatures();

  // This map contains all ARC system available features. For each feature, it
  // has the name and version. Unavailable features have been filtered out from
  // this map.
  FeatureVersionMapping feature_map;

  // This list contains all ARC unavailable feature names.
  FeatureList unavailable_features;

  // Struct for build properties that are referenced in Ash.
  BuildPropsMapping build_props;

  std::string play_store_version;
};

// Parses JSON files for Android system available features and build properties.
//
// A feature JSON file looks like this:
// {
//   "features": [
//     {
//       "name": "android.hardware.location",
//       "version: 2
//     },
//     {
//       "name": "android.hardware.location.network",
//       "version": 0
//     }
//   ],
//   "unavailable_features": [
//     "android.hardware.usb.accessory",
//     "android.software.live_tv"
//   ],
//   "properties": {
//     "ro.product.cpu.abilist": "x86_64,x86,armeabi-v7a,armeabi",
//     "ro.build.version.sdk": "25"
//   },
//   "play_store_version": "81010860"
// }
class ArcFeaturesParser {
 public:
  ArcFeaturesParser(const ArcFeaturesParser&) = delete;
  ArcFeaturesParser& operator=(const ArcFeaturesParser&) = delete;

  // Get ARC system available features.
  static void GetArcFeatures(
      base::OnceCallback<void(std::optional<ArcFeatures>)> callback);

  // Given an input feature JSON, return ARC features. This method is for
  // testing only.
  static std::optional<ArcFeatures> ParseFeaturesJsonForTesting(
      std::string_view input_json);

  // Overrides the ArcFeatures returned by GetArcFeatures, for testing only.
  // Does not take ownership of |getter|, it must be alive when GetArcFeatures
  // is called.
  static void SetArcFeaturesGetterForTesting(
      base::RepeatingCallback<std::optional<ArcFeatures>()>* getter);
};

}  // namespace arc

#endif  // ASH_COMPONENTS_ARC_ARC_FEATURES_PARSER_H_