lin
2025-08-01 633231e833e21d5b8b1c00cb15aedb62b3b78e8f
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
// Copyright (c) 2011 The Chromium OS 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 "policy/libpolicy.h"
 
#include <memory>
 
#include <base/logging.h>
 
#include "policy/device_policy.h"
#ifndef __ANDROID__
#include "policy/device_policy_impl.h"
#endif
 
namespace policy {
 
PolicyProvider::PolicyProvider() {
#ifndef __ANDROID__
  device_policy_ = std::make_unique<DevicePolicyImpl>();
  install_attributes_reader_ = std::make_unique<InstallAttributesReader>();
#endif
}
 
PolicyProvider::PolicyProvider(std::unique_ptr<DevicePolicy> device_policy)
    : device_policy_(std::move(device_policy)),
#ifdef __ANDROID__
      device_policy_is_loaded_(true) {
}
#else
      device_policy_is_loaded_(true),
      install_attributes_reader_(std::make_unique<InstallAttributesReader>()) {
}
#endif  // __ANDROID__
 
PolicyProvider::~PolicyProvider() {}
 
bool PolicyProvider::Reload() {
  if (!device_policy_)
    return false;
  device_policy_is_loaded_ = device_policy_->LoadPolicy();
  if (!device_policy_is_loaded_) {
    LOG(WARNING) << "Could not load the device policy file.";
  }
  return device_policy_is_loaded_;
}
 
bool PolicyProvider::device_policy_is_loaded() const {
  return device_policy_is_loaded_;
}
 
const DevicePolicy& PolicyProvider::GetDevicePolicy() const {
  DCHECK(device_policy_is_loaded_)
      << "Trying to get policy data but policy was not loaded!";
  return *device_policy_;
}
 
bool PolicyProvider::IsConsumerDevice() const {
#ifdef __ANDROID__
  return true;
#else
  if (!install_attributes_reader_->IsLocked())
    return false;
 
  const std::string& device_mode = install_attributes_reader_->GetAttribute(
      InstallAttributesReader::kAttrMode);
  return device_mode != InstallAttributesReader::kDeviceModeEnterprise &&
         device_mode != InstallAttributesReader::kDeviceModeEnterpriseAD;
#endif  // __ANDROID__
}
 
void PolicyProvider::SetDevicePolicyForTesting(
    std::unique_ptr<DevicePolicy> device_policy) {
  device_policy_ = std::move(device_policy);
  device_policy_is_loaded_ = true;
}
 
void PolicyProvider::SetInstallAttributesReaderForTesting(
    std::unique_ptr<InstallAttributesReader> install_attributes_reader) {
  install_attributes_reader_ = std::move(install_attributes_reader);
}
 
}  // namespace policy