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 9 #include <base/logging.h> 10 11 #include "policy/device_policy.h" 12 #ifndef __ANDROID__ 13 #include "policy/device_policy_impl.h" 14 #endif 15 16 namespace policy { 17 PolicyProvider()18PolicyProvider::PolicyProvider() { 19 #ifndef __ANDROID__ 20 device_policy_ = std::make_unique<DevicePolicyImpl>(); 21 install_attributes_reader_ = std::make_unique<InstallAttributesReader>(); 22 #endif 23 } 24 PolicyProvider(std::unique_ptr<DevicePolicy> device_policy)25PolicyProvider::PolicyProvider(std::unique_ptr<DevicePolicy> device_policy) 26 : device_policy_(std::move(device_policy)), 27 #ifdef __ANDROID__ 28 device_policy_is_loaded_(true) { 29 } 30 #else 31 device_policy_is_loaded_(true), 32 install_attributes_reader_(std::make_unique<InstallAttributesReader>()) { 33 } 34 #endif // __ANDROID__ 35 ~PolicyProvider()36PolicyProvider::~PolicyProvider() {} 37 Reload()38bool PolicyProvider::Reload() { 39 if (!device_policy_) 40 return false; 41 device_policy_is_loaded_ = device_policy_->LoadPolicy(); 42 if (!device_policy_is_loaded_) { 43 LOG(WARNING) << "Could not load the device policy file."; 44 } 45 return device_policy_is_loaded_; 46 } 47 device_policy_is_loaded() const48bool PolicyProvider::device_policy_is_loaded() const { 49 return device_policy_is_loaded_; 50 } 51 GetDevicePolicy() const52const DevicePolicy& PolicyProvider::GetDevicePolicy() const { 53 DCHECK(device_policy_is_loaded_) 54 << "Trying to get policy data but policy was not loaded!"; 55 return *device_policy_; 56 } 57 IsConsumerDevice() const58bool PolicyProvider::IsConsumerDevice() const { 59 #ifdef __ANDROID__ 60 return true; 61 #else 62 if (!install_attributes_reader_->IsLocked()) 63 return false; 64 65 const std::string& device_mode = install_attributes_reader_->GetAttribute( 66 InstallAttributesReader::kAttrMode); 67 return device_mode != InstallAttributesReader::kDeviceModeEnterprise && 68 device_mode != InstallAttributesReader::kDeviceModeEnterpriseAD; 69 #endif // __ANDROID__ 70 } 71 SetDevicePolicyForTesting(std::unique_ptr<DevicePolicy> device_policy)72void PolicyProvider::SetDevicePolicyForTesting( 73 std::unique_ptr<DevicePolicy> device_policy) { 74 device_policy_ = std::move(device_policy); 75 device_policy_is_loaded_ = true; 76 } 77 SetInstallAttributesReaderForTesting(std::unique_ptr<InstallAttributesReader> install_attributes_reader)78void PolicyProvider::SetInstallAttributesReaderForTesting( 79 std::unique_ptr<InstallAttributesReader> install_attributes_reader) { 80 install_attributes_reader_ = std::move(install_attributes_reader); 81 } 82 83 } // namespace policy 84