| 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/enrollment_handler_chromeos.h" |
| 6 |
| 7 #include "base/bind.h" |
| 8 #include "base/logging.h" |
| 9 #include "base/message_loop.h" |
| 10 #include "chrome/browser/policy/cloud_policy_constants.h" |
| 11 #include "chrome/browser/policy/device_cloud_policy_store_chromeos.h" |
| 12 #include "chrome/browser/policy/enterprise_install_attributes.h" |
| 13 #include "chrome/browser/policy/proto/device_management_backend.pb.h" |
| 14 |
| 15 namespace em = enterprise_management; |
| 16 |
| 17 namespace policy { |
| 18 |
| 19 namespace { |
| 20 |
| 21 // Retry for InstallAttrs initialization every 500ms. |
| 22 const int kLockRetryIntervalMs = 500; |
| 23 // Maximum time to retry InstallAttrs initialization before we give up. |
| 24 const int kLockRetryTimeoutMs = 10 * 60 * 1000; // 10 minutes. |
| 25 |
| 26 } // namespace |
| 27 |
| 28 EnrollmentHandlerChromeOS::EnrollmentHandlerChromeOS( |
| 29 DeviceCloudPolicyStoreChromeOS* store, |
| 30 EnterpriseInstallAttributes* install_attributes, |
| 31 scoped_ptr<CloudPolicyClient> client, |
| 32 const std::string& auth_token, |
| 33 const AllowedDeviceModes& allowed_device_modes, |
| 34 const CompletionCallback& completion_callback) |
| 35 : store_(store), |
| 36 install_attributes_(install_attributes), |
| 37 client_(client.Pass()), |
| 38 auth_token_(auth_token), |
| 39 allowed_device_modes_(allowed_device_modes), |
| 40 completion_callback_(completion_callback), |
| 41 device_mode_(DEVICE_MODE_NOT_SET), |
| 42 enrollment_step_(STEP_PENDING), |
| 43 lockbox_init_duration_(0), |
| 44 ALLOW_THIS_IN_INITIALIZER_LIST(weak_factory_(this)) { |
| 45 CHECK(!client_->is_registered()); |
| 46 CHECK_EQ(DM_STATUS_SUCCESS, client_->status()); |
| 47 store_->AddObserver(this); |
| 48 client_->AddObserver(this); |
| 49 } |
| 50 |
| 51 EnrollmentHandlerChromeOS::~EnrollmentHandlerChromeOS() { |
| 52 Stop(); |
| 53 store_->RemoveObserver(this); |
| 54 } |
| 55 |
| 56 void EnrollmentHandlerChromeOS::StartEnrollment() { |
| 57 CHECK_EQ(STEP_PENDING, enrollment_step_); |
| 58 AttemptRegistration(); |
| 59 } |
| 60 |
| 61 scoped_ptr<CloudPolicyClient> EnrollmentHandlerChromeOS::ReleaseClient() { |
| 62 Stop(); |
| 63 return client_.Pass(); |
| 64 } |
| 65 |
| 66 void EnrollmentHandlerChromeOS::OnPolicyFetched(CloudPolicyClient* client) { |
| 67 DCHECK_EQ(client_.get(), client); |
| 68 CHECK_EQ(STEP_POLICY_FETCH, enrollment_step_); |
| 69 |
| 70 enrollment_step_ = STEP_VALIDATION; |
| 71 |
| 72 // Validate the policy. |
| 73 scoped_ptr<DeviceCloudPolicyValidator> validator( |
| 74 DeviceCloudPolicyValidator::Create( |
| 75 scoped_ptr<em::PolicyFetchResponse>( |
| 76 new em::PolicyFetchResponse(*client_->policy())), |
| 77 base::Bind(&EnrollmentHandlerChromeOS::PolicyValidated, |
| 78 weak_factory_.GetWeakPtr()))); |
| 79 |
| 80 if (install_attributes_->IsEnterpriseDevice()) |
| 81 validator->ValidateDomain(install_attributes_->GetDomain()); |
| 82 validator->ValidatePolicyType(dm_protocol::kChromeDevicePolicyType); |
| 83 validator->ValidatePayload(); |
| 84 validator->ValidateInitialKey(); |
| 85 validator.release()->StartValidation(); |
| 86 } |
| 87 |
| 88 void EnrollmentHandlerChromeOS::OnRegistrationStateChanged( |
| 89 CloudPolicyClient* client) { |
| 90 DCHECK_EQ(client_.get(), client); |
| 91 |
| 92 if (enrollment_step_ == STEP_REGISTRATION && client_->is_registered()) { |
| 93 enrollment_step_ = STEP_POLICY_FETCH, |
| 94 device_mode_ = client_->device_mode(); |
| 95 if (device_mode_ == DEVICE_MODE_NOT_SET) |
| 96 device_mode_ = DEVICE_MODE_ENTERPRISE; |
| 97 if (device_mode_ >= allowed_device_modes_.size() || |
| 98 !allowed_device_modes_[device_mode_]) { |
| 99 LOG(ERROR) << "Bad device mode " << device_mode_; |
| 100 ReportResult(EnrollmentStatus::ForStatus( |
| 101 EnrollmentStatus::STATUS_REGISTRATION_BAD_MODE)); |
| 102 return; |
| 103 } |
| 104 client_->FetchPolicy(); |
| 105 } else { |
| 106 LOG(FATAL) << "Registration state changed to " << client_->is_registered() |
| 107 << " in step " << enrollment_step_; |
| 108 } |
| 109 } |
| 110 |
| 111 void EnrollmentHandlerChromeOS::OnClientError(CloudPolicyClient* client) { |
| 112 DCHECK_EQ(client_.get(), client); |
| 113 |
| 114 if (!enrollment_step_ < STEP_POLICY_FETCH) |
| 115 ReportResult(EnrollmentStatus::ForRegistrationError(client_->status())); |
| 116 else |
| 117 ReportResult(EnrollmentStatus::ForFetchError(client_->status())); |
| 118 } |
| 119 |
| 120 void EnrollmentHandlerChromeOS::OnStoreLoaded(CloudPolicyStore* store) { |
| 121 DCHECK_EQ(store_, store); |
| 122 |
| 123 if (enrollment_step_ == STEP_PENDING) { |
| 124 AttemptRegistration(); |
| 125 } else if (enrollment_step_ == STEP_STORE_POLICY) { |
| 126 ReportResult(EnrollmentStatus::ForStatus(EnrollmentStatus::STATUS_SUCCESS)); |
| 127 } |
| 128 } |
| 129 |
| 130 void EnrollmentHandlerChromeOS::OnStoreError(CloudPolicyStore* store) { |
| 131 DCHECK_EQ(store_, store); |
| 132 ReportResult(EnrollmentStatus::ForStoreError(store_->status(), |
| 133 store_->validation_status())); |
| 134 } |
| 135 |
| 136 void EnrollmentHandlerChromeOS::AttemptRegistration() { |
| 137 if (store_->is_initialized()) { |
| 138 enrollment_step_ = STEP_REGISTRATION; |
| 139 client_->Register(auth_token_); |
| 140 } |
| 141 } |
| 142 |
| 143 void EnrollmentHandlerChromeOS::PolicyValidated( |
| 144 DeviceCloudPolicyValidator* validator) { |
| 145 CHECK_EQ(STEP_VALIDATION, enrollment_step_); |
| 146 if (validator->success()) { |
| 147 policy_ = validator->policy().Pass(); |
| 148 enrollment_step_ = STEP_LOCK_DEVICE; |
| 149 WriteInstallAttributes(validator->policy_data()->username(), device_mode_, |
| 150 validator->policy_data()->device_id()); |
| 151 } else { |
| 152 ReportResult(EnrollmentStatus::ForValidationError(validator->status())); |
| 153 } |
| 154 } |
| 155 |
| 156 void EnrollmentHandlerChromeOS::WriteInstallAttributes( |
| 157 const std::string& user, |
| 158 DeviceMode device_mode, |
| 159 const std::string& device_id) { |
| 160 CHECK_EQ(STEP_LOCK_DEVICE, enrollment_step_); |
| 161 // Since this method is also called directly. |
| 162 weak_factory_.InvalidateWeakPtrs(); |
| 163 |
| 164 switch (install_attributes_->LockDevice(user, device_mode, device_id)) { |
| 165 case EnterpriseInstallAttributes::LOCK_SUCCESS: |
| 166 enrollment_step_ = STEP_STORE_POLICY; |
| 167 store_->InstallInitialPolicy(*policy_); |
| 168 return; |
| 169 case EnterpriseInstallAttributes::LOCK_NOT_READY: |
| 170 // We wait up to |kLockRetryTimeoutMs| milliseconds and if it hasn't |
| 171 // succeeded by then show an error to the user and stop the enrollment. |
| 172 if (lockbox_init_duration_ < kLockRetryTimeoutMs) { |
| 173 // InstallAttributes not ready yet, retry later. |
| 174 LOG(WARNING) << "Install Attributes not ready yet will retry in " |
| 175 << kLockRetryIntervalMs << "ms."; |
| 176 MessageLoop::current()->PostDelayedTask( |
| 177 FROM_HERE, |
| 178 base::Bind(&EnrollmentHandlerChromeOS::WriteInstallAttributes, |
| 179 weak_factory_.GetWeakPtr(), |
| 180 user, device_mode, device_id), |
| 181 base::TimeDelta::FromMilliseconds(kLockRetryIntervalMs)); |
| 182 lockbox_init_duration_ += kLockRetryIntervalMs; |
| 183 } else { |
| 184 ReportResult(EnrollmentStatus::ForStatus( |
| 185 EnrollmentStatus::STATUS_LOCK_TIMEOUT)); |
| 186 } |
| 187 return; |
| 188 case EnterpriseInstallAttributes::LOCK_BACKEND_ERROR: |
| 189 ReportResult(EnrollmentStatus::ForStatus( |
| 190 EnrollmentStatus::STATUS_LOCK_ERROR)); |
| 191 return; |
| 192 case EnterpriseInstallAttributes::LOCK_WRONG_USER: |
| 193 LOG(ERROR) << "Enrollment cannot proceed because the InstallAttrs " |
| 194 << "has been locked already!"; |
| 195 ReportResult(EnrollmentStatus::ForStatus( |
| 196 EnrollmentStatus::STATUS_LOCK_WRONG_USER)); |
| 197 return; |
| 198 } |
| 199 |
| 200 NOTREACHED(); |
| 201 } |
| 202 |
| 203 void EnrollmentHandlerChromeOS::Stop() { |
| 204 if (client_.get()) |
| 205 client_->RemoveObserver(this); |
| 206 enrollment_step_ = STEP_FINISHED; |
| 207 weak_factory_.InvalidateWeakPtrs(); |
| 208 completion_callback_.Reset(); |
| 209 } |
| 210 |
| 211 void EnrollmentHandlerChromeOS::ReportResult(EnrollmentStatus status) { |
| 212 CompletionCallback callback = completion_callback_; |
| 213 Stop(); |
| 214 |
| 215 if (status.status() != EnrollmentStatus::STATUS_SUCCESS) { |
| 216 LOG(WARNING) << "Enrollment failed: " << status.status() |
| 217 << " " << status.client_status() |
| 218 << " " << status.validation_status() |
| 219 << " " << status.store_status(); |
| 220 } |
| 221 |
| 222 if (!callback.is_null()) |
| 223 callback.Run(status); |
| 224 } |
| 225 |
| 226 } // namespace policy |
| OLD | NEW |