| OLD | NEW |
| (Empty) | |
| 1 // Copyright (c) 2012 The Chromium 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 "chrome/browser/policy/device_cloud_policy_store_chromeos.h" |
| 6 |
| 7 #include "base/bind.h" |
| 8 #include "chrome/browser/policy/device_policy_decoder_chromeos.h" |
| 9 #include "chrome/browser/policy/enterprise_install_attributes.h" |
| 10 #include "chrome/browser/policy/proto/chrome_device_policy.pb.h" |
| 11 #include "chrome/browser/policy/proto/device_management_backend.pb.h" |
| 12 |
| 13 namespace em = enterprise_management; |
| 14 |
| 15 namespace policy { |
| 16 |
| 17 DeviceCloudPolicyStoreChromeOS::DeviceCloudPolicyStoreChromeOS( |
| 18 chromeos::DeviceSettingsService* device_settings_service, |
| 19 EnterpriseInstallAttributes* install_attributes) |
| 20 : device_settings_service_(device_settings_service), |
| 21 install_attributes_(install_attributes), |
| 22 ALLOW_THIS_IN_INITIALIZER_LIST(weak_factory_(this)) { |
| 23 device_settings_service_->AddObserver(this); |
| 24 } |
| 25 |
| 26 DeviceCloudPolicyStoreChromeOS::~DeviceCloudPolicyStoreChromeOS() { |
| 27 device_settings_service_->RemoveObserver(this); |
| 28 } |
| 29 |
| 30 void DeviceCloudPolicyStoreChromeOS::Store( |
| 31 const em::PolicyFetchResponse& policy) { |
| 32 // Cancel all pending requests. |
| 33 weak_factory_.InvalidateWeakPtrs(); |
| 34 |
| 35 scoped_refptr<chromeos::OwnerKey> owner_key( |
| 36 device_settings_service_->GetOwnerKey()); |
| 37 if (!install_attributes_->IsEnterpriseDevice() || |
| 38 !device_settings_service_->policy_data() || |
| 39 !owner_key || !owner_key->public_key()) { |
| 40 status_ = STATUS_BAD_STATE; |
| 41 NotifyStoreError(); |
| 42 return; |
| 43 } |
| 44 |
| 45 scoped_ptr<DeviceCloudPolicyValidator> validator(CreateValidator(policy)); |
| 46 validator->ValidateSignature(*owner_key->public_key(), true); |
| 47 validator->ValidateAgainstCurrentPolicy( |
| 48 device_settings_service_->policy_data(), false); |
| 49 validator.release()->StartValidation(); |
| 50 } |
| 51 |
| 52 void DeviceCloudPolicyStoreChromeOS::Load() { |
| 53 device_settings_service_->Load(); |
| 54 } |
| 55 |
| 56 void DeviceCloudPolicyStoreChromeOS::RemoveStoredPolicy() { |
| 57 // Device policy cannot and should not be removed on Chrome OS. |
| 58 NOTREACHED(); |
| 59 } |
| 60 |
| 61 void DeviceCloudPolicyStoreChromeOS::InstallInitialPolicy( |
| 62 const em::PolicyFetchResponse& policy) { |
| 63 // Cancel all pending requests. |
| 64 weak_factory_.InvalidateWeakPtrs(); |
| 65 |
| 66 if (!install_attributes_->IsEnterpriseDevice() && |
| 67 device_settings_service_->status() != |
| 68 chromeos::DeviceSettingsService::STORE_NO_POLICY) { |
| 69 status_ = STATUS_BAD_STATE; |
| 70 NotifyStoreError(); |
| 71 return; |
| 72 } |
| 73 |
| 74 scoped_ptr<DeviceCloudPolicyValidator> validator(CreateValidator(policy)); |
| 75 validator->ValidateInitialKey(); |
| 76 validator.release()->StartValidation(); |
| 77 } |
| 78 |
| 79 void DeviceCloudPolicyStoreChromeOS::OwnershipStatusChanged() { |
| 80 // Nothing to do. |
| 81 } |
| 82 |
| 83 void DeviceCloudPolicyStoreChromeOS::DeviceSettingsUpdated() { |
| 84 if (!weak_factory_.HasWeakPtrs()) |
| 85 UpdateFromService(); |
| 86 } |
| 87 |
| 88 scoped_ptr<DeviceCloudPolicyValidator> |
| 89 DeviceCloudPolicyStoreChromeOS::CreateValidator( |
| 90 const em::PolicyFetchResponse& policy) { |
| 91 scoped_ptr<DeviceCloudPolicyValidator> validator( |
| 92 DeviceCloudPolicyValidator::Create( |
| 93 scoped_ptr<em::PolicyFetchResponse>( |
| 94 new em::PolicyFetchResponse(policy)), |
| 95 base::Bind(&DeviceCloudPolicyStoreChromeOS::OnPolicyToStoreValidated, |
| 96 weak_factory_.GetWeakPtr()))); |
| 97 validator->ValidateDomain(install_attributes_->GetDomain()); |
| 98 validator->ValidatePolicyType(dm_protocol::kChromeDevicePolicyType); |
| 99 validator->ValidatePayload(); |
| 100 return validator.Pass(); |
| 101 } |
| 102 |
| 103 void DeviceCloudPolicyStoreChromeOS::OnPolicyToStoreValidated( |
| 104 DeviceCloudPolicyValidator* validator) { |
| 105 if (!validator->success()) { |
| 106 status_ = STATUS_VALIDATION_ERROR; |
| 107 validation_status_ = validator->status(); |
| 108 NotifyStoreError(); |
| 109 return; |
| 110 } |
| 111 |
| 112 device_settings_service_->Store( |
| 113 validator->policy().Pass(), |
| 114 base::Bind(&DeviceCloudPolicyStoreChromeOS::OnPolicyStored, |
| 115 weak_factory_.GetWeakPtr())); |
| 116 } |
| 117 |
| 118 void DeviceCloudPolicyStoreChromeOS::OnPolicyStored() { |
| 119 UpdateFromService(); |
| 120 } |
| 121 |
| 122 void DeviceCloudPolicyStoreChromeOS::UpdateFromService() { |
| 123 if (!install_attributes_->IsEnterpriseDevice()) { |
| 124 status_ = STATUS_BAD_STATE; |
| 125 NotifyStoreError(); |
| 126 return; |
| 127 } |
| 128 |
| 129 switch (device_settings_service_->status()) { |
| 130 case chromeos::DeviceSettingsService::STORE_SUCCESS: { |
| 131 status_ = STATUS_OK; |
| 132 policy_.reset(new em::PolicyData()); |
| 133 if (device_settings_service_->policy_data()) |
| 134 policy_->MergeFrom(*device_settings_service_->policy_data()); |
| 135 |
| 136 PolicyMap new_policy_map; |
| 137 if (is_managed()) { |
| 138 DecodeDevicePolicy(*device_settings_service_->device_settings(), |
| 139 &new_policy_map, install_attributes_); |
| 140 } |
| 141 policy_map_.Swap(&new_policy_map); |
| 142 |
| 143 NotifyStoreLoaded(); |
| 144 return; |
| 145 } |
| 146 case chromeos::DeviceSettingsService::STORE_KEY_UNAVAILABLE: |
| 147 status_ = STATUS_BAD_STATE; |
| 148 break; |
| 149 case chromeos::DeviceSettingsService::STORE_POLICY_ERROR: |
| 150 case chromeos::DeviceSettingsService::STORE_OPERATION_FAILED: |
| 151 status_ = STATUS_STORE_ERROR; |
| 152 break; |
| 153 case chromeos::DeviceSettingsService::STORE_NO_POLICY: |
| 154 case chromeos::DeviceSettingsService::STORE_INVALID_POLICY: |
| 155 case chromeos::DeviceSettingsService::STORE_VALIDATION_ERROR: |
| 156 status_ = STATUS_LOAD_ERROR; |
| 157 break; |
| 158 } |
| 159 |
| 160 NotifyStoreError(); |
| 161 } |
| 162 |
| 163 } // namespace policy |
| OLD | NEW |