File: policy_info.cc

package info (click to toggle)
chromium 139.0.7258.127-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 6,122,156 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (180 lines) | stat: -rw-r--r-- 6,879 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
// Copyright 2019 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "components/enterprise/browser/reporting/policy_info.h"

#include <string>

#include "base/json/json_writer.h"
#include "base/strings/string_util.h"
#include "build/build_config.h"
#include "components/policy/core/browser/policy_conversions.h"
#include "components/policy/core/common/cloud/cloud_policy_client.h"
#include "components/policy/core/common/cloud/cloud_policy_constants.h"
#include "components/policy/core/common/cloud/cloud_policy_manager.h"
#include "components/policy/core/common/policy_types.h"
#include "components/strings/grit/components_strings.h"

namespace em = enterprise_management;

namespace enterprise_reporting {

namespace {

em::Policy_PolicyLevel GetLevel(const base::Value& policy) {
  switch (
      static_cast<policy::PolicyLevel>(*policy.GetDict().FindInt("level"))) {
    case policy::POLICY_LEVEL_RECOMMENDED:
      return em::Policy_PolicyLevel_LEVEL_RECOMMENDED;
    case policy::POLICY_LEVEL_MANDATORY:
      return em::Policy_PolicyLevel_LEVEL_MANDATORY;
  }
  NOTREACHED() << "Invalid policy level: "
               << *policy.GetDict().FindInt("level");
}

em::Policy_PolicyScope GetScope(const base::Value& policy) {
  switch (
      static_cast<policy::PolicyScope>(*policy.GetDict().FindInt("scope"))) {
    case policy::POLICY_SCOPE_USER:
      return em::Policy_PolicyScope_SCOPE_USER;
    case policy::POLICY_SCOPE_MACHINE:
      return em::Policy_PolicyScope_SCOPE_MACHINE;
  }
  NOTREACHED() << "Invalid policy scope: "
               << *policy.GetDict().FindInt("scope");
}

em::Policy_PolicySource GetSource(const base::Value& policy) {
  switch (
      static_cast<policy::PolicySource>(*policy.GetDict().FindInt("source"))) {
    case policy::POLICY_SOURCE_ENTERPRISE_DEFAULT:
      return em::Policy_PolicySource_SOURCE_ENTERPRISE_DEFAULT;
    case policy::POLICY_SOURCE_COMMAND_LINE:
      return em::Policy_PolicySource_SOURCE_COMMAND_LINE;
    case policy::POLICY_SOURCE_CLOUD:
      return em::Policy_PolicySource_SOURCE_CLOUD;
    case policy::POLICY_SOURCE_ACTIVE_DIRECTORY:
      return em::Policy_PolicySource_SOURCE_ACTIVE_DIRECTORY;
    case policy::POLICY_SOURCE_DEVICE_LOCAL_ACCOUNT_OVERRIDE_DEPRECATED:
      return em::
          Policy_PolicySource_SOURCE_DEVICE_LOCAL_ACCOUNT_OVERRIDE_DEPRECATED;
    case policy::POLICY_SOURCE_PLATFORM:
      return em::Policy_PolicySource_SOURCE_PLATFORM;
    case policy::POLICY_SOURCE_PRIORITY_CLOUD_DEPRECATED:
      return em::Policy_PolicySource_SOURCE_PRIORITY_CLOUD_DEPRECATED;
    case policy::POLICY_SOURCE_MERGED:
      return em::Policy_PolicySource_SOURCE_MERGED;
    case policy::POLICY_SOURCE_CLOUD_FROM_ASH:
      return em::Policy_PolicySource_SOURCE_CLOUD_FROM_ASH;
    case policy::POLICY_SOURCE_RESTRICTED_MANAGED_GUEST_SESSION_OVERRIDE:
      return em::
          Policy_PolicySource_SOURCE_RESTRICTED_MANAGED_GUEST_SESSION_OVERRIDE;
    case policy::POLICY_SOURCE_COUNT:
      NOTREACHED();
  }
  NOTREACHED() << "Invalid policy source: "
               << *policy.GetDict().FindInt("source");
}

void UpdateConflictedPolicy(em::Policy* policy_info,
                            const base::Value& policy) {
  policy_info->set_level(GetLevel(policy));
  policy_info->set_scope(GetScope(policy));
  policy_info->set_source(GetSource(policy));
}

void UpdateConflictedPolicies(const std::string conflict_key,
                              em::Policy* policy_info,
                              const base::Value::Dict& policy) {
  if (!policy.contains(conflict_key)) {
    return;
  }
  for (const auto& conflicted_policy : *policy.FindList(conflict_key)) {
    UpdateConflictedPolicy(policy_info->add_conflicts(), conflicted_policy);
  }
}

void UpdatePolicyInfo(em::Policy* policy_info,
                      const std::string& policy_name,
                      const base::Value& policy) {
  policy_info->set_name(policy_name);
  policy_info->set_level(GetLevel(policy));
  policy_info->set_scope(GetScope(policy));
  policy_info->set_source(GetSource(policy));
  base::JSONWriter::Write(*policy.GetDict().Find("value"),
                          policy_info->mutable_value());
  const std::string* error = policy.GetDict().FindString("error");
  std::string deprecated_error;
  std::string future_error;
  // Because server side use keyword "deprecated" to determine policy
  // deprecation error. Using l10n string actually causing issue.
  if (policy.GetDict().FindBool("deprecated")) {
    deprecated_error = "This policy has been deprecated";
  }

  if (policy.GetDict().FindBool("future")) {
    future_error = "This policy hasn't been released";
  }

  if (error && !deprecated_error.empty())
    policy_info->set_error(
        base::JoinString({*error, deprecated_error, future_error}, "\n"));
  else if (error)
    policy_info->set_error(*error);
  else if (!deprecated_error.empty())
    policy_info->set_error(deprecated_error);

  UpdateConflictedPolicies("conflicts", policy_info, policy.GetDict());
  UpdateConflictedPolicies("superseded", policy_info, policy.GetDict());
}

}  // namespace

void AppendChromePolicyInfoIntoProfileReport(
    const base::Value::Dict& policies,
    em::ChromeUserProfileInfo* profile_info) {
  for (auto policy_iter : *policies.FindDict("chromePolicies")) {
    UpdatePolicyInfo(profile_info->add_chrome_policies(), policy_iter.first,
                     policy_iter.second);
  }
}

void AppendExtensionPolicyInfoIntoProfileReport(
    const base::Value::Dict& policies,
    em::ChromeUserProfileInfo* profile_info) {
  if (!policies.Find("extensionPolicies")) {
    // Android and iOS don't support extensions and their policies.
    return;
  }

  for (auto extension_iter : *policies.FindDict("extensionPolicies")) {
    const base::Value& policies_value = extension_iter.second;
    if (policies_value.GetDict().size() == 0) {
      continue;
    }

    auto* extension = profile_info->add_extension_policies();
    extension->set_extension_id(extension_iter.first);
    for (auto policy_iter : policies_value.GetDict()) {
      UpdatePolicyInfo(extension->add_policies(), policy_iter.first,
                       policy_iter.second);
    }
  }
}

void AppendCloudPolicyFetchTimestamp(em::ChromeUserProfileInfo* profile_info,
                                     policy::CloudPolicyManager* manager) {
#if !BUILDFLAG(IS_CHROMEOS)
  if (!manager || !manager->IsClientRegistered())
    return;
  auto* timestamp = profile_info->add_policy_fetched_timestamps();
  timestamp->set_timestamp(manager->core()
                               ->client()
                               ->last_policy_timestamp()
                               .InMillisecondsSinceUnixEpoch());
#endif  // !BUILDFLAG(IS_CHROMEOS)
}

}  // namespace enterprise_reporting