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