File: arc_policy_util.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 (219 lines) | stat: -rw-r--r-- 8,650 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
// Copyright 2016 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/arc/policy/arc_policy_util.h"

#include <memory>

#include "ash/constants/ash_switches.h"
#include "base/command_line.h"
#include "base/json/json_reader.h"
#include "base/logging.h"
#include "base/metrics/histogram_macros.h"
#include "chrome/browser/ash/policy/handlers/configuration_policy_handler_ash.h"
#include "chrome/browser/policy/profile_policy_connector.h"
#include "chrome/browser/profiles/profile.h"
#include "chromeos/ash/experiences/arc/arc_prefs.h"
#include "components/policy/core/common/policy_map.h"
#include "components/policy/policy_constants.h"
#include "components/policy/proto/cloud_policy.pb.h"
#include "components/prefs/pref_value_map.h"

namespace arc {
namespace policy_util {

namespace {

// Constants used to parse ARC++ JSON policy.
constexpr char kApplicationsKey[] = "applications";
constexpr char kInstallTypeKey[] = "installType";
constexpr char kPackageNameKey[] = "packageName";
constexpr char kInstallTypeRequired[] = "REQUIRED";
constexpr char kInstallTypeForceInstalled[] = "FORCE_INSTALLED";

}  // namespace

bool IsAccountManaged(const Profile* profile) {
  return profile->GetProfilePolicyConnector()->IsManaged();
}

bool IsArcDisabledForEnterprise() {
  return base::CommandLine::ForCurrentProcess()->HasSwitch(
      ash::switches::kEnterpriseDisableArc);
}

std::set<std::string> GetRequestedPackagesFromArcPolicy(
    const std::string& arc_policy) {
  std::optional<base::Value> dict = ParsePolicyJson(arc_policy);
  if (!dict.has_value() || !dict.value().is_dict()) {
    return {};
  }

  std::map<std::string, std::set<std::string>> install_type_map =
      CreateInstallTypeMap(dict.value());
  std::set<std::string> required_packages =
      install_type_map[kInstallTypeRequired];
  std::set<std::string> force_installed_packages =
      install_type_map[kInstallTypeForceInstalled];

  // Only required and force installed packages are considered "requested"
  std::set<std::string> requested_packages;
  requested_packages.insert(required_packages.begin(), required_packages.end());
  requested_packages.insert(force_installed_packages.begin(),
                            force_installed_packages.end());
  return requested_packages;
}

void RecordPolicyMetrics(const std::string& arc_policy) {
  std::optional<base::Value> dict = ParsePolicyJson(arc_policy);
  if (!dict.has_value() || !dict.value().is_dict()) {
    return;
  }

  for (const auto it : dict.value().GetDict()) {
    std::optional<ArcPolicyKey> key = GetPolicyKeyFromString(it.first);
    if (key.has_value()) {
      UMA_HISTOGRAM_ENUMERATION("Arc.Policy.Keys", key.value());
    }
  }

  std::map<std::string, std::set<std::string>> install_type_map =
      CreateInstallTypeMap(dict.value());

  for (auto& it : install_type_map) {
    InstallType install_type_enum = GetInstallTypeEnumFromString(it.first);
    UMA_HISTOGRAM_ENUMERATION("Arc.Policy.InstallTypesOnDevice",
                              install_type_enum);
  }
}

std::optional<base::Value> ParsePolicyJson(const std::string& arc_policy) {
  return base::JSONReader::Read(
      arc_policy, base::JSONParserOptions::JSON_ALLOW_TRAILING_COMMAS);
}

std::map<std::string, std::set<std::string>> CreateInstallTypeMap(
    const base::Value& dict) {
  const base::Value::List* const packages =
      dict.GetDict().FindList(kApplicationsKey);
  if (!packages)
    return {};

  std::map<std::string, std::set<std::string>> install_type_map;
  for (const auto& package : *packages) {
    const base::Value::Dict* package_dict = package.GetIfDict();
    if (!package_dict) {
      continue;
    }
    const std::string* const install_type =
        package_dict->FindString(kInstallTypeKey);
    if (!install_type)
      continue;

    const std::string* const package_name =
        package_dict->FindString(kPackageNameKey);
    if (!package_name || package_name->empty()) {
      continue;
    }
    install_type_map[*install_type].insert(*package_name);
  }
  return install_type_map;
}

std::optional<ArcPolicyKey> GetPolicyKeyFromString(
    const std::string& policy_key) {
  if (policy_key == kArcPolicyKeyAvailableAppSetPolicyDeprecated ||
      policy_key == kArcPolicyKeyWorkAccountAppWhitelistDeprecated ||
      policy_key == kArcPolicyKeyGuid ||
      policy_key == kArcPolicyKeyMountPhysicalMediaDisabled ||
      policy_key == kArcPolicyKeyDpsInteractionsDisabled) {
    // Ignore keys that are always set or represent server side flags.
    return std::nullopt;
  }

  if (policy_key == kArcPolicyKeyAccountTypesWithManagementDisabled) {
    return ArcPolicyKey::kAccountTypesWithManagementDisabled;
  } else if (policy_key == kArcPolicyKeyAlwaysOnVpnPackage) {
    return ArcPolicyKey::kAlwaysOnVpnPackage;
  } else if (policy_key == kArcPolicyKeyApplications) {
    return ArcPolicyKey::kApplications;
  } else if (policy_key == kArcPolicyKeyComplianceRules) {
    return ArcPolicyKey::kComplianceRules;
  } else if (policy_key == kArcPolicyKeyInstallUnknownSourcesDisabled) {
    return ArcPolicyKey::kInstallUnknownSourcesDisabled;
  } else if (policy_key == kArcPolicyKeyMaintenanceWindow) {
    return ArcPolicyKey::kMaintenanceWindow;
  } else if (policy_key == kArcPolicyKeyModifyAccountsDisabled) {
    return ArcPolicyKey::kModifyAccountsDisabled;
  } else if (policy_key == kArcPolicyKeyPermissionGrants) {
    return ArcPolicyKey::kPermissionGrants;
  } else if (policy_key == kArcPolicyKeyPermittedAccessibilityServices) {
    return ArcPolicyKey::kPermittedAccessibilityServices;
  } else if (policy_key == kArcPolicyKeyPlayStoreMode) {
    return ArcPolicyKey::kPlayStoreMode;
  } else if (policy_key == kArcPolicyKeyShortSupportMessage) {
    return ArcPolicyKey::kShortSupportMessage;
  } else if (policy_key == kArcPolicyKeyStatusReportingSettings) {
    return ArcPolicyKey::kStatusReportingSettings;
  } else if (policy_key == kArcPolicyKeyApkCacheEnabled) {
    return ArcPolicyKey::kApkCacheEnabled;
  } else if (policy_key == kArcPolicyKeyDebuggingFeaturesDisabled) {
    return ArcPolicyKey::kDebuggingFeaturesDisabled;
  } else if (policy_key == kArcPolicyKeyCameraDisabled) {
    return ArcPolicyKey::kCameraDisabled;
  } else if (policy_key == kArcPolicyKeyPrintingDisabled) {
    return ArcPolicyKey::kPrintingDisabled;
  } else if (policy_key == kArcPolicyKeyScreenCaptureDisabled) {
    return ArcPolicyKey::kScreenCaptureDisabled;
  } else if (policy_key == kArcPolicyKeyShareLocationDisabled) {
    return ArcPolicyKey::kShareLocationDisabled;
  } else if (policy_key == kArcPolicyKeyUnmuteMicrophoneDisabled) {
    return ArcPolicyKey::kUnmuteMicrophoneDisabled;
  } else if (policy_key == kArcPolicyKeySetWallpaperDisabled) {
    return ArcPolicyKey::kSetWallpaperDisabled;
  } else if (policy_key == kArcPolicyKeyVpnConfigDisabled) {
    return ArcPolicyKey::kVpnConfigDisabled;
  } else if (policy_key == kArcPolicyKeyPrivateKeySelectionEnabled) {
    return ArcPolicyKey::kPrivateKeySelectionEnabled;
  } else if (policy_key == kArcPolicyKeyChoosePrivateKeyRules) {
    return ArcPolicyKey::kChoosePrivateKeyRules;
  } else if (policy_key == kArcPolicyKeyCredentialsConfigDisabled) {
    return ArcPolicyKey::kCredentialsConfigDisabled;
  } else if (policy_key == kArcPolicyKeyCaCerts) {
    return ArcPolicyKey::kCaCerts;
  } else if (policy_key == kArcPolicyKeyRequiredKeyPairs) {
    return ArcPolicyKey::kRequiredKeyPairs;
  } else if (policy_key == kArcPolicyKeyEnabledSystemAppPackageNames) {
    return ArcPolicyKey::kEnabledSystemAppPackageNames;
  }

  LOG(WARNING) << "Unknown policy key: " << policy_key;
  return ArcPolicyKey::kUnknown;
}

InstallType GetInstallTypeEnumFromString(const std::string& install_type) {
  if (install_type == "OPTIONAL") {
    return InstallType::kOptional;
  } else if (install_type == "REQUIRED") {
    return InstallType::kRequired;
  } else if (install_type == "PRELOAD") {
    return InstallType::kPreload;
  } else if (install_type == "FORCE_INSTALLED") {
    return InstallType::kForceInstalled;
  } else if (install_type == "BLOCKED") {
    return InstallType::kBlocked;
  } else if (install_type == "AVAILABLE") {
    return InstallType::kAvailable;
  } else if (install_type == "REQUIRED_FOR_SETUP") {
    return InstallType::kRequiredForSetup;
  } else if (install_type == "KIOSK") {
    return InstallType::kKiosk;
  }

  LOG(WARNING) << "Unknown app install type in the policy: " << install_type;
  return InstallType::kUnknown;
}

}  // namespace policy_util
}  // namespace arc