OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/browser/chromeos/settings/device_settings_service.h" | 5 #include "chrome/browser/chromeos/settings/device_settings_service.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/logging.h" | 8 #include "base/logging.h" |
9 #include "base/message_loop/message_loop.h" | 9 #include "base/message_loop/message_loop.h" |
10 #include "base/stl_util.h" | 10 #include "base/stl_util.h" |
11 #include "base/time/time.h" | 11 #include "base/time/time.h" |
| 12 #include "base/values.h" |
12 #include "chrome/browser/chrome_notification_types.h" | 13 #include "chrome/browser/chrome_notification_types.h" |
13 #include "chrome/browser/chromeos/policy/proto/chrome_device_policy.pb.h" | 14 #include "chrome/browser/chromeos/policy/proto/chrome_device_policy.pb.h" |
14 #include "chrome/browser/chromeos/settings/session_manager_operation.h" | 15 #include "chrome/browser/chromeos/settings/session_manager_operation.h" |
| 16 #include "chromeos/settings/cros_settings_names.h" |
15 #include "components/ownership/owner_key_util.h" | 17 #include "components/ownership/owner_key_util.h" |
16 #include "components/policy/core/common/cloud/cloud_policy_constants.h" | 18 #include "components/policy/core/common/cloud/cloud_policy_constants.h" |
17 #include "content/public/browser/browser_thread.h" | 19 #include "content/public/browser/browser_thread.h" |
18 #include "content/public/browser/notification_service.h" | 20 #include "content/public/browser/notification_service.h" |
19 #include "content/public/browser/notification_source.h" | 21 #include "content/public/browser/notification_source.h" |
20 #include "crypto/rsa_private_key.h" | 22 #include "crypto/rsa_private_key.h" |
21 | 23 |
22 namespace em = enterprise_management; | 24 namespace em = enterprise_management; |
23 | 25 |
24 using ownership::OwnerKeyUtil; | 26 using ownership::OwnerKeyUtil; |
25 using ownership::PublicKey; | 27 using ownership::PublicKey; |
26 | 28 |
27 namespace { | 29 namespace { |
28 | 30 |
29 // Delay between load retries when there was a validation error. | 31 // Delay between load retries when there was a validation error. |
30 // NOTE: This code is here to mitigate clock loss on some devices where policy | 32 // NOTE: This code is here to mitigate clock loss on some devices where policy |
31 // loads will fail with a validation error caused by RTC clock being reset when | 33 // loads will fail with a validation error caused by RTC clock being reset when |
32 // the battery is drained. | 34 // the battery is drained. |
33 int kLoadRetryDelayMs = 1000 * 5; | 35 int kLoadRetryDelayMs = 1000 * 5; |
34 // Maximal number of retries before we give up. Calculated to allow for 10 min | 36 // Maximal number of retries before we give up. Calculated to allow for 10 min |
35 // of retry time. | 37 // of retry time. |
36 int kMaxLoadRetries = (1000 * 60 * 10) / kLoadRetryDelayMs; | 38 int kMaxLoadRetries = (1000 * 60 * 10) / kLoadRetryDelayMs; |
37 | 39 |
38 // Assembles PolicyData based on |settings|, |policy_data| and | |
39 // |user_id|. | |
40 scoped_ptr<em::PolicyData> AssemblePolicy( | |
41 const std::string& user_id, | |
42 const em::PolicyData* policy_data, | |
43 const em::ChromeDeviceSettingsProto* settings) { | |
44 scoped_ptr<em::PolicyData> policy(new em::PolicyData()); | |
45 if (policy_data) { | |
46 // Preserve management settings. | |
47 if (policy_data->has_management_mode()) | |
48 policy->set_management_mode(policy_data->management_mode()); | |
49 if (policy_data->has_request_token()) | |
50 policy->set_request_token(policy_data->request_token()); | |
51 if (policy_data->has_device_id()) | |
52 policy->set_device_id(policy_data->device_id()); | |
53 } else { | |
54 // If there's no previous policy data, this is the first time the device | |
55 // setting is set. We set the management mode to NOT_MANAGED initially. | |
56 policy->set_management_mode(em::PolicyData::NOT_MANAGED); | |
57 } | |
58 policy->set_policy_type(policy::dm_protocol::kChromeDevicePolicyType); | |
59 policy->set_timestamp( | |
60 (base::Time::Now() - base::Time::UnixEpoch()).InMilliseconds()); | |
61 policy->set_username(user_id); | |
62 if (!settings->SerializeToString(policy->mutable_policy_value())) | |
63 return scoped_ptr<em::PolicyData>(); | |
64 | |
65 return policy.Pass(); | |
66 } | |
67 | |
68 // Returns true if it is okay to transfer from the current mode to the new | 40 // Returns true if it is okay to transfer from the current mode to the new |
69 // mode. This function should be called in SetManagementMode(). | 41 // mode. This function should be called in SetManagementMode(). |
70 bool CheckManagementModeTransition(em::PolicyData::ManagementMode current_mode, | 42 bool CheckManagementModeTransition(em::PolicyData::ManagementMode current_mode, |
71 em::PolicyData::ManagementMode new_mode) { | 43 em::PolicyData::ManagementMode new_mode) { |
72 // Mode is not changed. | 44 // Mode is not changed. |
73 if (current_mode == new_mode) | 45 if (current_mode == new_mode) |
74 return true; | 46 return true; |
75 | 47 |
76 switch (current_mode) { | 48 switch (current_mode) { |
77 case em::PolicyData::NOT_MANAGED: | 49 case em::PolicyData::NOT_MANAGED: |
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
116 delete g_device_settings_service; | 88 delete g_device_settings_service; |
117 g_device_settings_service = NULL; | 89 g_device_settings_service = NULL; |
118 } | 90 } |
119 | 91 |
120 // static | 92 // static |
121 DeviceSettingsService* DeviceSettingsService::Get() { | 93 DeviceSettingsService* DeviceSettingsService::Get() { |
122 CHECK(g_device_settings_service); | 94 CHECK(g_device_settings_service); |
123 return g_device_settings_service; | 95 return g_device_settings_service; |
124 } | 96 } |
125 | 97 |
| 98 // static |
| 99 void DeviceSettingsService::UpdateDeviceSettings( |
| 100 const std::string& path, |
| 101 const base::Value& value, |
| 102 enterprise_management::ChromeDeviceSettingsProto& settings) { |
| 103 if (path == kAccountsPrefAllowNewUser) { |
| 104 em::AllowNewUsersProto* allow = settings.mutable_allow_new_users(); |
| 105 bool allow_value; |
| 106 if (value.GetAsBoolean(&allow_value)) { |
| 107 allow->set_allow_new_users(allow_value); |
| 108 } else { |
| 109 NOTREACHED(); |
| 110 } |
| 111 } else if (path == kAccountsPrefAllowGuest) { |
| 112 em::GuestModeEnabledProto* guest = settings.mutable_guest_mode_enabled(); |
| 113 bool guest_value; |
| 114 if (value.GetAsBoolean(&guest_value)) |
| 115 guest->set_guest_mode_enabled(guest_value); |
| 116 else |
| 117 NOTREACHED(); |
| 118 } else if (path == kAccountsPrefSupervisedUsersEnabled) { |
| 119 em::SupervisedUsersSettingsProto* supervised = |
| 120 settings.mutable_supervised_users_settings(); |
| 121 bool supervised_value; |
| 122 if (value.GetAsBoolean(&supervised_value)) |
| 123 supervised->set_supervised_users_enabled(supervised_value); |
| 124 else |
| 125 NOTREACHED(); |
| 126 } else if (path == kAccountsPrefShowUserNamesOnSignIn) { |
| 127 em::ShowUserNamesOnSigninProto* show = settings.mutable_show_user_names(); |
| 128 bool show_value; |
| 129 if (value.GetAsBoolean(&show_value)) |
| 130 show->set_show_user_names(show_value); |
| 131 else |
| 132 NOTREACHED(); |
| 133 } else if (path == kAccountsPrefDeviceLocalAccounts) { |
| 134 em::DeviceLocalAccountsProto* device_local_accounts = |
| 135 settings.mutable_device_local_accounts(); |
| 136 device_local_accounts->clear_account(); |
| 137 const base::ListValue* accounts_list = NULL; |
| 138 if (value.GetAsList(&accounts_list)) { |
| 139 for (base::ListValue::const_iterator entry(accounts_list->begin()); |
| 140 entry != accounts_list->end(); |
| 141 ++entry) { |
| 142 const base::DictionaryValue* entry_dict = NULL; |
| 143 if ((*entry)->GetAsDictionary(&entry_dict)) { |
| 144 em::DeviceLocalAccountInfoProto* account = |
| 145 device_local_accounts->add_account(); |
| 146 std::string account_id; |
| 147 if (entry_dict->GetStringWithoutPathExpansion( |
| 148 kAccountsPrefDeviceLocalAccountsKeyId, &account_id)) { |
| 149 account->set_account_id(account_id); |
| 150 } |
| 151 int type; |
| 152 if (entry_dict->GetIntegerWithoutPathExpansion( |
| 153 kAccountsPrefDeviceLocalAccountsKeyType, &type)) { |
| 154 account->set_type( |
| 155 static_cast<em::DeviceLocalAccountInfoProto::AccountType>( |
| 156 type)); |
| 157 } |
| 158 std::string kiosk_app_id; |
| 159 if (entry_dict->GetStringWithoutPathExpansion( |
| 160 kAccountsPrefDeviceLocalAccountsKeyKioskAppId, |
| 161 &kiosk_app_id)) { |
| 162 account->mutable_kiosk_app()->set_app_id(kiosk_app_id); |
| 163 } |
| 164 } else { |
| 165 NOTREACHED(); |
| 166 } |
| 167 } |
| 168 } else { |
| 169 NOTREACHED(); |
| 170 } |
| 171 } else if (path == kAccountsPrefDeviceLocalAccountAutoLoginId) { |
| 172 em::DeviceLocalAccountsProto* device_local_accounts = |
| 173 settings.mutable_device_local_accounts(); |
| 174 std::string id; |
| 175 if (value.GetAsString(&id)) |
| 176 device_local_accounts->set_auto_login_id(id); |
| 177 else |
| 178 NOTREACHED(); |
| 179 } else if (path == kAccountsPrefDeviceLocalAccountAutoLoginDelay) { |
| 180 em::DeviceLocalAccountsProto* device_local_accounts = |
| 181 settings.mutable_device_local_accounts(); |
| 182 int delay; |
| 183 if (value.GetAsInteger(&delay)) |
| 184 device_local_accounts->set_auto_login_delay(delay); |
| 185 else |
| 186 NOTREACHED(); |
| 187 } else if (path == kAccountsPrefDeviceLocalAccountAutoLoginBailoutEnabled) { |
| 188 em::DeviceLocalAccountsProto* device_local_accounts = |
| 189 settings.mutable_device_local_accounts(); |
| 190 bool enabled; |
| 191 if (value.GetAsBoolean(&enabled)) |
| 192 device_local_accounts->set_enable_auto_login_bailout(enabled); |
| 193 else |
| 194 NOTREACHED(); |
| 195 } else if (path == |
| 196 kAccountsPrefDeviceLocalAccountPromptForNetworkWhenOffline) { |
| 197 em::DeviceLocalAccountsProto* device_local_accounts = |
| 198 settings.mutable_device_local_accounts(); |
| 199 bool should_prompt; |
| 200 if (value.GetAsBoolean(&should_prompt)) |
| 201 device_local_accounts->set_prompt_for_network_when_offline(should_prompt); |
| 202 else |
| 203 NOTREACHED(); |
| 204 } else if (path == kSignedDataRoamingEnabled) { |
| 205 em::DataRoamingEnabledProto* roam = settings.mutable_data_roaming_enabled(); |
| 206 bool roaming_value = false; |
| 207 if (value.GetAsBoolean(&roaming_value)) |
| 208 roam->set_data_roaming_enabled(roaming_value); |
| 209 else |
| 210 NOTREACHED(); |
| 211 } else if (path == kReleaseChannel) { |
| 212 em::ReleaseChannelProto* release_channel = |
| 213 settings.mutable_release_channel(); |
| 214 std::string channel_value; |
| 215 if (value.GetAsString(&channel_value)) |
| 216 release_channel->set_release_channel(channel_value); |
| 217 else |
| 218 NOTREACHED(); |
| 219 } else if (path == kStatsReportingPref) { |
| 220 em::MetricsEnabledProto* metrics = settings.mutable_metrics_enabled(); |
| 221 bool metrics_value = false; |
| 222 if (value.GetAsBoolean(&metrics_value)) |
| 223 metrics->set_metrics_enabled(metrics_value); |
| 224 else |
| 225 NOTREACHED(); |
| 226 } else if (path == kAccountsPrefUsers) { |
| 227 em::UserWhitelistProto* whitelist_proto = settings.mutable_user_whitelist(); |
| 228 whitelist_proto->clear_user_whitelist(); |
| 229 const base::ListValue* users; |
| 230 if (value.GetAsList(&users)) { |
| 231 for (base::ListValue::const_iterator i = users->begin(); |
| 232 i != users->end(); |
| 233 ++i) { |
| 234 std::string email; |
| 235 if ((*i)->GetAsString(&email)) |
| 236 whitelist_proto->add_user_whitelist(email); |
| 237 } |
| 238 } |
| 239 } else if (path == kAccountsPrefEphemeralUsersEnabled) { |
| 240 em::EphemeralUsersEnabledProto* ephemeral_users_enabled = |
| 241 settings.mutable_ephemeral_users_enabled(); |
| 242 bool ephemeral_users_enabled_value = false; |
| 243 if (value.GetAsBoolean(&ephemeral_users_enabled_value)) { |
| 244 ephemeral_users_enabled->set_ephemeral_users_enabled( |
| 245 ephemeral_users_enabled_value); |
| 246 } else { |
| 247 NOTREACHED(); |
| 248 } |
| 249 } else if (path == kAllowRedeemChromeOsRegistrationOffers) { |
| 250 em::AllowRedeemChromeOsRegistrationOffersProto* allow_redeem_offers = |
| 251 settings.mutable_allow_redeem_offers(); |
| 252 bool allow_redeem_offers_value; |
| 253 if (value.GetAsBoolean(&allow_redeem_offers_value)) { |
| 254 allow_redeem_offers->set_allow_redeem_offers(allow_redeem_offers_value); |
| 255 } else { |
| 256 NOTREACHED(); |
| 257 } |
| 258 } else if (path == kStartUpFlags) { |
| 259 em::StartUpFlagsProto* flags_proto = settings.mutable_start_up_flags(); |
| 260 flags_proto->Clear(); |
| 261 const base::ListValue* flags; |
| 262 if (value.GetAsList(&flags)) { |
| 263 for (base::ListValue::const_iterator i = flags->begin(); |
| 264 i != flags->end(); |
| 265 ++i) { |
| 266 std::string flag; |
| 267 if ((*i)->GetAsString(&flag)) |
| 268 flags_proto->add_flags(flag); |
| 269 } |
| 270 } |
| 271 } else if (path == kSystemUse24HourClock) { |
| 272 em::SystemUse24HourClockProto* use_24hour_clock_proto = |
| 273 settings.mutable_use_24hour_clock(); |
| 274 use_24hour_clock_proto->Clear(); |
| 275 bool use_24hour_clock_value; |
| 276 if (value.GetAsBoolean(&use_24hour_clock_value)) { |
| 277 use_24hour_clock_proto->set_use_24hour_clock(use_24hour_clock_value); |
| 278 } else { |
| 279 NOTREACHED(); |
| 280 } |
| 281 } else if (path == kAttestationForContentProtectionEnabled) { |
| 282 em::AttestationSettingsProto* attestation_settings = |
| 283 settings.mutable_attestation_settings(); |
| 284 bool setting_enabled; |
| 285 if (value.GetAsBoolean(&setting_enabled)) { |
| 286 attestation_settings->set_content_protection_enabled(setting_enabled); |
| 287 } else { |
| 288 NOTREACHED(); |
| 289 } |
| 290 } else { |
| 291 // The remaining settings don't support Set(), since they are not |
| 292 // intended to be customizable by the user: |
| 293 // kAccountsPrefTransferSAMLCookies |
| 294 // kAppPack |
| 295 // kDeviceAttestationEnabled |
| 296 // kDeviceOwner |
| 297 // kIdleLogoutTimeout |
| 298 // kIdleLogoutWarningDuration |
| 299 // kReleaseChannelDelegated |
| 300 // kReportDeviceActivityTimes |
| 301 // kReportDeviceBootMode |
| 302 // kReportDeviceLocation |
| 303 // kReportDeviceVersionInfo |
| 304 // kReportDeviceNetworkInterfaces |
| 305 // kReportDeviceUsers |
| 306 // kScreenSaverExtensionId |
| 307 // kScreenSaverTimeout |
| 308 // kServiceAccountIdentity |
| 309 // kStartUpUrls |
| 310 // kSystemTimezonePolicy |
| 311 // kVariationsRestrictParameter |
| 312 |
| 313 LOG(FATAL) << "Device setting " << path << " is read-only."; |
| 314 } |
| 315 } |
| 316 |
| 317 // static |
| 318 scoped_ptr<em::PolicyData> DeviceSettingsService::AssemblePolicy( |
| 319 const std::string& user_id, |
| 320 const em::PolicyData* policy_data, |
| 321 const em::ChromeDeviceSettingsProto* settings) { |
| 322 scoped_ptr<em::PolicyData> policy(new em::PolicyData()); |
| 323 if (policy_data) { |
| 324 // Preserve management settings. |
| 325 if (policy_data->has_management_mode()) |
| 326 policy->set_management_mode(policy_data->management_mode()); |
| 327 if (policy_data->has_request_token()) |
| 328 policy->set_request_token(policy_data->request_token()); |
| 329 if (policy_data->has_device_id()) |
| 330 policy->set_device_id(policy_data->device_id()); |
| 331 } else { |
| 332 // If there's no previous policy data, this is the first time the device |
| 333 // setting is set. We set the management mode to NOT_MANAGED initially. |
| 334 policy->set_management_mode(em::PolicyData::NOT_MANAGED); |
| 335 } |
| 336 policy->set_policy_type(policy::dm_protocol::kChromeDevicePolicyType); |
| 337 policy->set_timestamp( |
| 338 (base::Time::Now() - base::Time::UnixEpoch()).InMilliseconds()); |
| 339 policy->set_username(user_id); |
| 340 if (!settings->SerializeToString(policy->mutable_policy_value())) |
| 341 return scoped_ptr<em::PolicyData>(); |
| 342 |
| 343 return policy.Pass(); |
| 344 } |
| 345 |
126 DeviceSettingsService::DeviceSettingsService() | 346 DeviceSettingsService::DeviceSettingsService() |
127 : session_manager_client_(NULL), | 347 : session_manager_client_(NULL), |
128 store_status_(STORE_SUCCESS), | 348 store_status_(STORE_SUCCESS), |
| 349 processing_loop_enabled_(true), |
129 load_retries_left_(kMaxLoadRetries), | 350 load_retries_left_(kMaxLoadRetries), |
130 weak_factory_(this) { | 351 weak_factory_(this) { |
131 } | 352 } |
132 | 353 |
133 DeviceSettingsService::~DeviceSettingsService() { | 354 DeviceSettingsService::~DeviceSettingsService() { |
134 DCHECK(pending_operations_.empty()); | 355 DCHECK(pending_operations_.empty()); |
135 } | 356 } |
136 | 357 |
137 void DeviceSettingsService::SetSessionManager( | 358 void DeviceSettingsService::SetSessionManager( |
138 SessionManagerClient* session_manager_client, | 359 SessionManagerClient* session_manager_client, |
139 scoped_refptr<OwnerKeyUtil> owner_key_util) { | 360 scoped_refptr<OwnerKeyUtil> owner_key_util) { |
140 DCHECK(session_manager_client); | 361 DCHECK(session_manager_client); |
141 DCHECK(owner_key_util.get()); | 362 DCHECK(owner_key_util.get()); |
142 DCHECK(!session_manager_client_); | 363 DCHECK(!session_manager_client_); |
143 DCHECK(!owner_key_util_.get()); | 364 DCHECK(!owner_key_util_.get()); |
144 | 365 |
145 session_manager_client_ = session_manager_client; | 366 session_manager_client_ = session_manager_client; |
146 owner_key_util_ = owner_key_util; | 367 owner_key_util_ = owner_key_util; |
147 | 368 |
148 session_manager_client_->AddObserver(this); | 369 session_manager_client_->AddObserver(this); |
149 | 370 |
150 StartNextOperation(); | 371 StartNextOperation(); |
151 } | 372 } |
152 | 373 |
153 void DeviceSettingsService::UnsetSessionManager() { | 374 void DeviceSettingsService::UnsetSessionManager() { |
154 STLDeleteContainerPointers(pending_operations_.begin(), | |
155 pending_operations_.end()); | |
156 pending_operations_.clear(); | 375 pending_operations_.clear(); |
157 | 376 |
158 if (session_manager_client_) | 377 if (session_manager_client_) |
159 session_manager_client_->RemoveObserver(this); | 378 session_manager_client_->RemoveObserver(this); |
160 session_manager_client_ = NULL; | 379 session_manager_client_ = NULL; |
161 owner_key_util_ = NULL; | 380 owner_key_util_ = NULL; |
162 } | 381 } |
163 | 382 |
164 scoped_refptr<PublicKey> DeviceSettingsService::GetPublicKey() { | 383 scoped_refptr<PublicKey> DeviceSettingsService::GetPublicKey() { |
165 return public_key_; | 384 return public_key_; |
166 } | 385 } |
167 | 386 |
168 void DeviceSettingsService::Load() { | 387 void DeviceSettingsService::Load() { |
169 EnqueueLoad(false); | 388 EnqueueLoad(false); |
170 } | 389 } |
171 | 390 |
172 void DeviceSettingsService::SignAndStore( | 391 void DeviceSettingsService::SignAndStore( |
173 scoped_ptr<em::ChromeDeviceSettingsProto> new_settings, | 392 scoped_ptr<em::ChromeDeviceSettingsProto> new_settings, |
174 const base::Closure& callback) { | 393 const base::Closure& callback) { |
175 if (!owner_settings_service_) { | |
176 HandleError(STORE_KEY_UNAVAILABLE, callback); | |
177 return; | |
178 } | |
179 scoped_ptr<em::PolicyData> policy = | 394 scoped_ptr<em::PolicyData> policy = |
180 AssemblePolicy(GetUsername(), policy_data(), new_settings.get()); | 395 AssemblePolicy(GetUsername(), policy_data(), new_settings.get()); |
181 if (!policy) { | 396 EnqueueSignAndStore(policy.Pass(), callback); |
182 HandleError(STORE_POLICY_ERROR, callback); | |
183 return; | |
184 } | |
185 | |
186 owner_settings_service_->SignAndStorePolicyAsync(policy.Pass(), callback); | |
187 } | 397 } |
188 | 398 |
189 void DeviceSettingsService::SetManagementSettings( | 399 void DeviceSettingsService::SetManagementSettings( |
190 em::PolicyData::ManagementMode management_mode, | 400 em::PolicyData::ManagementMode management_mode, |
191 const std::string& request_token, | 401 const std::string& request_token, |
192 const std::string& device_id, | 402 const std::string& device_id, |
193 const base::Closure& callback) { | 403 const base::Closure& callback) { |
194 if (!owner_settings_service_) { | 404 if (!owner_settings_service_) { |
195 HandleError(STORE_KEY_UNAVAILABLE, callback); | 405 HandleError(STORE_KEY_UNAVAILABLE, callback); |
196 return; | 406 return; |
(...skipping 14 matching lines...) Expand all Loading... |
211 AssemblePolicy(GetUsername(), policy_data(), device_settings()); | 421 AssemblePolicy(GetUsername(), policy_data(), device_settings()); |
212 if (!policy) { | 422 if (!policy) { |
213 HandleError(DeviceSettingsService::STORE_POLICY_ERROR, callback); | 423 HandleError(DeviceSettingsService::STORE_POLICY_ERROR, callback); |
214 return; | 424 return; |
215 } | 425 } |
216 | 426 |
217 policy->set_management_mode(management_mode); | 427 policy->set_management_mode(management_mode); |
218 policy->set_request_token(request_token); | 428 policy->set_request_token(request_token); |
219 policy->set_device_id(device_id); | 429 policy->set_device_id(device_id); |
220 | 430 |
221 owner_settings_service_->SignAndStorePolicyAsync(policy.Pass(), callback); | 431 EnqueueSignAndStore(policy.Pass(), callback); |
222 } | 432 } |
223 | 433 |
224 void DeviceSettingsService::Store(scoped_ptr<em::PolicyFetchResponse> policy, | 434 void DeviceSettingsService::Store(scoped_ptr<em::PolicyFetchResponse> policy, |
225 const base::Closure& callback) { | 435 const base::Closure& callback) { |
226 Enqueue( | 436 Enqueue(linked_ptr<SessionManagerOperation>(new StoreSettingsOperation( |
227 new StoreSettingsOperation( | 437 base::Bind(&DeviceSettingsService::HandleCompletedOperation, |
228 base::Bind(&DeviceSettingsService::HandleCompletedOperation, | 438 weak_factory_.GetWeakPtr(), |
229 weak_factory_.GetWeakPtr(), | 439 callback), |
230 callback), | 440 policy.Pass()))); |
231 policy.Pass())); | |
232 } | 441 } |
233 | 442 |
234 DeviceSettingsService::OwnershipStatus | 443 DeviceSettingsService::OwnershipStatus |
235 DeviceSettingsService::GetOwnershipStatus() { | 444 DeviceSettingsService::GetOwnershipStatus() { |
236 if (public_key_.get()) | 445 if (public_key_.get()) |
237 return public_key_->is_loaded() ? OWNERSHIP_TAKEN : OWNERSHIP_NONE; | 446 return public_key_->is_loaded() ? OWNERSHIP_TAKEN : OWNERSHIP_NONE; |
238 return OWNERSHIP_UNKNOWN; | 447 return OWNERSHIP_UNKNOWN; |
239 } | 448 } |
240 | 449 |
241 void DeviceSettingsService::GetOwnershipStatusAsync( | 450 void DeviceSettingsService::GetOwnershipStatusAsync( |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
277 } | 486 } |
278 | 487 |
279 void DeviceSettingsService::AddObserver(Observer* observer) { | 488 void DeviceSettingsService::AddObserver(Observer* observer) { |
280 observers_.AddObserver(observer); | 489 observers_.AddObserver(observer); |
281 } | 490 } |
282 | 491 |
283 void DeviceSettingsService::RemoveObserver(Observer* observer) { | 492 void DeviceSettingsService::RemoveObserver(Observer* observer) { |
284 observers_.RemoveObserver(observer); | 493 observers_.RemoveObserver(observer); |
285 } | 494 } |
286 | 495 |
| 496 bool DeviceSettingsService::HasPendingOperations() const { |
| 497 return weak_factory_.HasWeakPtrs() || !pending_operations_.empty(); |
| 498 } |
| 499 |
| 500 bool DeviceSettingsService::EnableProcessingLoop(bool enabled) { |
| 501 if (HasPendingOperations() && !enabled) |
| 502 return false; |
| 503 bool trigger_processing_loop = !processing_loop_enabled_ && enabled; |
| 504 processing_loop_enabled_ = enabled; |
| 505 if (trigger_processing_loop) |
| 506 StartNextOperation(); |
| 507 return true; |
| 508 } |
| 509 |
287 void DeviceSettingsService::OwnerKeySet(bool success) { | 510 void DeviceSettingsService::OwnerKeySet(bool success) { |
288 if (!success) { | 511 if (!success) { |
289 LOG(ERROR) << "Owner key change failed."; | 512 LOG(ERROR) << "Owner key change failed."; |
290 return; | 513 return; |
291 } | 514 } |
292 | 515 |
293 public_key_ = NULL; | 516 public_key_ = NULL; |
294 EnsureReload(true); | 517 EnsureReload(true); |
295 } | 518 } |
296 | 519 |
297 void DeviceSettingsService::PropertyChangeComplete(bool success) { | 520 void DeviceSettingsService::PropertyChangeComplete(bool success) { |
298 if (!success) { | 521 if (!success) { |
299 LOG(ERROR) << "Policy update failed."; | 522 LOG(ERROR) << "Policy update failed."; |
300 return; | 523 return; |
301 } | 524 } |
302 | 525 |
303 EnsureReload(false); | 526 EnsureReload(false); |
304 } | 527 } |
305 | 528 |
306 void DeviceSettingsService::Enqueue(SessionManagerOperation* operation) { | 529 void DeviceSettingsService::Enqueue( |
| 530 const linked_ptr<SessionManagerOperation>& operation) { |
307 pending_operations_.push_back(operation); | 531 pending_operations_.push_back(operation); |
308 if (pending_operations_.front() == operation) | 532 if (pending_operations_.front().get() == operation.get()) |
309 StartNextOperation(); | 533 StartNextOperation(); |
310 } | 534 } |
311 | 535 |
312 void DeviceSettingsService::EnqueueLoad(bool force_key_load) { | 536 void DeviceSettingsService::EnqueueLoad(bool force_key_load) { |
313 SessionManagerOperation* operation = | 537 linked_ptr<SessionManagerOperation> operation(new LoadSettingsOperation( |
314 new LoadSettingsOperation( | 538 base::Bind(&DeviceSettingsService::HandleCompletedOperation, |
| 539 weak_factory_.GetWeakPtr(), |
| 540 base::Closure()))); |
| 541 operation->set_force_key_load(force_key_load); |
| 542 operation->set_username(username_); |
| 543 operation->set_owner_settings_service(owner_settings_service_); |
| 544 Enqueue(operation); |
| 545 } |
| 546 |
| 547 void DeviceSettingsService::EnqueueSignAndStore( |
| 548 scoped_ptr<enterprise_management::PolicyData> policy, |
| 549 const base::Closure& callback) { |
| 550 linked_ptr<SessionManagerOperation> operation( |
| 551 new SignAndStoreSettingsOperation( |
315 base::Bind(&DeviceSettingsService::HandleCompletedOperation, | 552 base::Bind(&DeviceSettingsService::HandleCompletedOperation, |
316 weak_factory_.GetWeakPtr(), | 553 weak_factory_.GetWeakPtr(), |
317 base::Closure())); | 554 callback), |
318 operation->set_force_key_load(force_key_load); | 555 policy.Pass())); |
319 operation->set_username(username_); | |
320 operation->set_owner_settings_service(owner_settings_service_); | 556 operation->set_owner_settings_service(owner_settings_service_); |
321 Enqueue(operation); | 557 Enqueue(operation); |
322 } | 558 } |
323 | 559 |
324 void DeviceSettingsService::EnsureReload(bool force_key_load) { | 560 void DeviceSettingsService::EnsureReload(bool force_key_load) { |
325 if (!pending_operations_.empty()) { | 561 if (!pending_operations_.empty()) { |
326 pending_operations_.front()->set_username(username_); | 562 pending_operations_.front()->set_username(username_); |
327 pending_operations_.front()->set_owner_settings_service( | 563 pending_operations_.front()->set_owner_settings_service( |
328 owner_settings_service_); | 564 owner_settings_service_); |
329 pending_operations_.front()->RestartLoad(force_key_load); | 565 pending_operations_.front()->RestartLoad(force_key_load); |
330 } else { | 566 } else { |
331 EnqueueLoad(force_key_load); | 567 EnqueueLoad(force_key_load); |
332 } | 568 } |
333 } | 569 } |
334 | 570 |
335 void DeviceSettingsService::StartNextOperation() { | 571 void DeviceSettingsService::StartNextOperation() { |
336 if (!pending_operations_.empty() && | 572 if (!pending_operations_.empty() && session_manager_client_ && |
337 session_manager_client_ && | 573 owner_key_util_.get() && processing_loop_enabled_) { |
338 owner_key_util_.get()) { | |
339 pending_operations_.front()->Start( | 574 pending_operations_.front()->Start( |
340 session_manager_client_, owner_key_util_, public_key_); | 575 session_manager_client_, owner_key_util_, public_key_); |
341 } | 576 } |
342 } | 577 } |
343 | 578 |
344 void DeviceSettingsService::HandleCompletedOperation( | 579 void DeviceSettingsService::HandleCompletedOperation( |
345 const base::Closure& callback, | 580 const base::Closure& callback, |
346 SessionManagerOperation* operation, | 581 SessionManagerOperation* operation, |
347 Status status) { | 582 Status status) { |
348 DCHECK_EQ(operation, pending_operations_.front()); | 583 DCHECK_EQ(operation, pending_operations_.front().get()); |
349 store_status_ = status; | 584 store_status_ = status; |
350 | 585 |
351 OwnershipStatus ownership_status = OWNERSHIP_UNKNOWN; | 586 OwnershipStatus ownership_status = OWNERSHIP_UNKNOWN; |
352 scoped_refptr<PublicKey> new_key(operation->public_key()); | 587 scoped_refptr<PublicKey> new_key(operation->public_key()); |
353 if (new_key.get()) { | 588 if (new_key.get()) { |
354 ownership_status = new_key->is_loaded() ? OWNERSHIP_TAKEN : OWNERSHIP_NONE; | 589 ownership_status = new_key->is_loaded() ? OWNERSHIP_TAKEN : OWNERSHIP_NONE; |
355 } else { | 590 } else { |
356 NOTREACHED() << "Failed to determine key status."; | 591 NOTREACHED() << "Failed to determine key status."; |
357 } | 592 } |
358 | 593 |
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
405 } | 640 } |
406 | 641 |
407 // The completion callback happens after the notification so clients can | 642 // The completion callback happens after the notification so clients can |
408 // filter self-triggered updates. | 643 // filter self-triggered updates. |
409 if (!callback.is_null()) | 644 if (!callback.is_null()) |
410 callback.Run(); | 645 callback.Run(); |
411 | 646 |
412 // Only remove the pending operation here, so new operations triggered by any | 647 // Only remove the pending operation here, so new operations triggered by any |
413 // of the callbacks above are queued up properly. | 648 // of the callbacks above are queued up properly. |
414 pending_operations_.pop_front(); | 649 pending_operations_.pop_front(); |
415 delete operation; | |
416 | 650 |
417 StartNextOperation(); | 651 StartNextOperation(); |
418 } | 652 } |
419 | 653 |
420 void DeviceSettingsService::HandleError(Status status, | 654 void DeviceSettingsService::HandleError(Status status, |
421 const base::Closure& callback) { | 655 const base::Closure& callback) { |
422 store_status_ = status; | 656 store_status_ = status; |
423 | 657 |
424 LOG(ERROR) << "Session manager operation failed: " << status; | 658 LOG(ERROR) << "Session manager operation failed: " << status; |
425 | 659 |
426 FOR_EACH_OBSERVER(Observer, observers_, DeviceSettingsUpdated()); | 660 FOR_EACH_OBSERVER(Observer, observers_, DeviceSettingsUpdated()); |
427 | 661 |
428 // The completion callback happens after the notification so clients can | 662 // The completion callback happens after the notification so clients can |
429 // filter self-triggered updates. | 663 // filter self-triggered updates. |
430 if (!callback.is_null()) | 664 if (!callback.is_null()) |
431 callback.Run(); | 665 callback.Run(); |
432 } | 666 } |
433 | 667 |
434 void DeviceSettingsService::OnSignAndStoreOperationCompleted(Status status) { | |
435 store_status_ = status; | |
436 FOR_EACH_OBSERVER(Observer, observers_, DeviceSettingsUpdated()); | |
437 } | |
438 | |
439 ScopedTestDeviceSettingsService::ScopedTestDeviceSettingsService() { | 668 ScopedTestDeviceSettingsService::ScopedTestDeviceSettingsService() { |
440 DeviceSettingsService::Initialize(); | 669 DeviceSettingsService::Initialize(); |
441 } | 670 } |
442 | 671 |
443 ScopedTestDeviceSettingsService::~ScopedTestDeviceSettingsService() { | 672 ScopedTestDeviceSettingsService::~ScopedTestDeviceSettingsService() { |
444 // Clean pending operations. | 673 // Clean pending operations. |
445 DeviceSettingsService::Get()->UnsetSessionManager(); | 674 DeviceSettingsService::Get()->UnsetSessionManager(); |
446 DeviceSettingsService::Shutdown(); | 675 DeviceSettingsService::Shutdown(); |
447 } | 676 } |
448 | 677 |
449 } // namespace chromeos | 678 } // namespace chromeos |
OLD | NEW |