1 // Copyright (c) 2011 The Chromium OS Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #include "policy/libpolicy.h"
6 
7 #include <memory>
8 #include <utility>
9 
10 #include <base/logging.h>
11 
12 #include "policy/device_policy.h"
13 #ifndef __ANDROID__
14 #include "policy/device_policy_impl.h"
15 #endif
16 
17 namespace policy {
18 
PolicyProvider()19 PolicyProvider::PolicyProvider() {
20 #ifndef __ANDROID__
21   device_policy_ = std::make_unique<DevicePolicyImpl>();
22   install_attributes_reader_ = std::make_unique<InstallAttributesReader>();
23 #endif
24 }
25 
PolicyProvider(std::unique_ptr<DevicePolicy> device_policy)26 PolicyProvider::PolicyProvider(std::unique_ptr<DevicePolicy> device_policy)
27     : device_policy_(std::move(device_policy)),
28 #ifdef __ANDROID__
29       device_policy_is_loaded_(true) {
30 }
31 #else
32       device_policy_is_loaded_(true),
33       install_attributes_reader_(std::make_unique<InstallAttributesReader>()) {
34 }
35 #endif  // __ANDROID__
36 
~PolicyProvider()37 PolicyProvider::~PolicyProvider() {}
38 
Reload()39 bool PolicyProvider::Reload() {
40   if (!device_policy_)
41     return false;
42   device_policy_is_loaded_ = device_policy_->LoadPolicy();
43   if (!device_policy_is_loaded_) {
44     LOG(WARNING) << "Could not load the device policy file.";
45   }
46   return device_policy_is_loaded_;
47 }
48 
device_policy_is_loaded() const49 bool PolicyProvider::device_policy_is_loaded() const {
50   return device_policy_is_loaded_;
51 }
52 
GetDevicePolicy() const53 const DevicePolicy& PolicyProvider::GetDevicePolicy() const {
54   DCHECK(device_policy_is_loaded_)
55       << "Trying to get policy data but policy was not loaded!";
56   return *device_policy_;
57 }
58 
IsConsumerDevice() const59 bool PolicyProvider::IsConsumerDevice() const {
60 #ifdef __ANDROID__
61   return true;
62 #else
63   if (!install_attributes_reader_->IsLocked())
64     return false;
65 
66   const std::string& device_mode = install_attributes_reader_->GetAttribute(
67       InstallAttributesReader::kAttrMode);
68   return device_mode != InstallAttributesReader::kDeviceModeEnterprise &&
69          device_mode != InstallAttributesReader::kDeviceModeEnterpriseAD;
70 #endif  // __ANDROID__
71 }
72 
SetDevicePolicyForTesting(std::unique_ptr<DevicePolicy> device_policy)73 void PolicyProvider::SetDevicePolicyForTesting(
74     std::unique_ptr<DevicePolicy> device_policy) {
75   device_policy_ = std::move(device_policy);
76   device_policy_is_loaded_ = true;
77 }
78 
SetInstallAttributesReaderForTesting(std::unique_ptr<InstallAttributesReader> install_attributes_reader)79 void PolicyProvider::SetInstallAttributesReaderForTesting(
80     std::unique_ptr<InstallAttributesReader> install_attributes_reader) {
81   install_attributes_reader_ = std::move(install_attributes_reader);
82 }
83 
84 }  // namespace policy
85