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 base::Bind(&DeviceCloudPolicyStoreChromeOS::OnPolicyToStoreValidated, | |
51 weak_factory_.GetWeakPtr())); | |
52 } | |
53 | |
54 void DeviceCloudPolicyStoreChromeOS::Load() { | |
55 device_settings_service_->Load(); | |
56 } | |
57 | |
58 void DeviceCloudPolicyStoreChromeOS::InstallInitialPolicy( | |
59 const em::PolicyFetchResponse& policy) { | |
60 // Cancel all pending requests. | |
61 weak_factory_.InvalidateWeakPtrs(); | |
62 | |
63 if (!install_attributes_->IsEnterpriseDevice() && | |
64 device_settings_service_->status() != | |
65 chromeos::DeviceSettingsService::STORE_NO_POLICY) { | |
66 status_ = STATUS_BAD_STATE; | |
67 NotifyStoreError(); | |
68 return; | |
69 } | |
70 | |
71 scoped_ptr<DeviceCloudPolicyValidator> validator(CreateValidator(policy)); | |
72 validator->ValidateInitialKey(); | |
73 validator.release()->StartValidation( | |
74 base::Bind(&DeviceCloudPolicyStoreChromeOS::OnPolicyToStoreValidated, | |
75 weak_factory_.GetWeakPtr())); | |
76 } | |
77 | |
78 void DeviceCloudPolicyStoreChromeOS::OwnershipStatusChanged() { | |
79 // Nothing to do. | |
80 } | |
81 | |
82 void DeviceCloudPolicyStoreChromeOS::DeviceSettingsUpdated() { | |
83 if (!weak_factory_.HasWeakPtrs()) | |
84 UpdateFromService(); | |
85 } | |
86 | |
87 scoped_ptr<DeviceCloudPolicyValidator> | |
88 DeviceCloudPolicyStoreChromeOS::CreateValidator( | |
89 const em::PolicyFetchResponse& policy) { | |
90 scoped_ptr<DeviceCloudPolicyValidator> validator( | |
91 DeviceCloudPolicyValidator::Create( | |
92 scoped_ptr<em::PolicyFetchResponse>( | |
93 new em::PolicyFetchResponse(policy)))); | |
94 validator->ValidateDomain(install_attributes_->GetDomain()); | |
95 validator->ValidatePolicyType(dm_protocol::kChromeDevicePolicyType); | |
96 validator->ValidatePayload(); | |
97 return validator.Pass(); | |
98 } | |
99 | |
100 void DeviceCloudPolicyStoreChromeOS::OnPolicyToStoreValidated( | |
101 DeviceCloudPolicyValidator* validator) { | |
102 if (!validator->success()) { | |
103 status_ = STATUS_VALIDATION_ERROR; | |
104 validation_status_ = validator->status(); | |
105 NotifyStoreError(); | |
106 return; | |
107 } | |
108 | |
109 device_settings_service_->Store( | |
110 validator->policy().Pass(), | |
111 base::Bind(&DeviceCloudPolicyStoreChromeOS::OnPolicyStored, | |
112 weak_factory_.GetWeakPtr())); | |
113 } | |
114 | |
115 void DeviceCloudPolicyStoreChromeOS::OnPolicyStored() { | |
116 UpdateFromService(); | |
117 } | |
118 | |
119 void DeviceCloudPolicyStoreChromeOS::UpdateFromService() { | |
120 if (!install_attributes_->IsEnterpriseDevice()) { | |
121 status_ = STATUS_BAD_STATE; | |
122 NotifyStoreError(); | |
123 return; | |
124 } | |
125 | |
126 switch (device_settings_service_->status()) { | |
127 case chromeos::DeviceSettingsService::STORE_SUCCESS: { | |
128 status_ = STATUS_OK; | |
129 policy_.reset(new em::PolicyData()); | |
130 if (device_settings_service_->policy_data()) | |
131 policy_->MergeFrom(*device_settings_service_->policy_data()); | |
132 | |
133 PolicyMap new_policy_map; | |
134 if (is_managed()) { | |
135 DecodeDevicePolicy(*device_settings_service_->device_settings(), | |
136 &new_policy_map, install_attributes_); | |
137 } | |
138 policy_map_.Swap(&new_policy_map); | |
139 | |
140 NotifyStoreLoaded(); | |
141 return; | |
142 } | |
143 case chromeos::DeviceSettingsService::STORE_KEY_UNAVAILABLE: | |
144 status_ = STATUS_BAD_STATE; | |
145 break; | |
146 case chromeos::DeviceSettingsService::STORE_POLICY_ERROR: | |
147 case chromeos::DeviceSettingsService::STORE_OPERATION_FAILED: | |
148 status_ = STATUS_STORE_ERROR; | |
149 break; | |
150 case chromeos::DeviceSettingsService::STORE_NO_POLICY: | |
151 case chromeos::DeviceSettingsService::STORE_INVALID_POLICY: | |
152 case chromeos::DeviceSettingsService::STORE_VALIDATION_ERROR: | |
153 case chromeos::DeviceSettingsService::STORE_TEMP_VALIDATION_ERROR: | |
154 status_ = STATUS_LOAD_ERROR; | |
155 break; | |
156 } | |
157 | |
158 NotifyStoreError(); | |
159 } | |
160 | |
161 } // namespace policy | |
OLD | NEW |