| 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_manager_chromeos.h" | |
| 6 | |
| 7 #include "base/bind.h" | |
| 8 #include "base/bind_helpers.h" | |
| 9 #include "chrome/browser/chromeos/system/statistics_provider.h" | |
| 10 #include "chrome/browser/policy/cloud_policy_constants.h" | |
| 11 #include "chrome/browser/policy/cloud_policy_store.h" | |
| 12 #include "chrome/browser/policy/device_cloud_policy_store_chromeos.h" | |
| 13 #include "chrome/browser/policy/device_management_service.h" | |
| 14 #include "chrome/browser/policy/enrollment_handler_chromeos.h" | |
| 15 #include "chrome/browser/policy/enterprise_install_attributes.h" | |
| 16 #include "chrome/browser/policy/proto/device_management_backend.pb.h" | |
| 17 #include "chrome/common/pref_names.h" | |
| 18 | |
| 19 namespace em = enterprise_management; | |
| 20 | |
| 21 namespace policy { | |
| 22 | |
| 23 namespace { | |
| 24 | |
| 25 // MachineInfo key names. | |
| 26 const char kMachineInfoSystemHwqual[] = "hardware_class"; | |
| 27 | |
| 28 // These are the machine serial number keys that we check in order until we | |
| 29 // find a non-empty serial number. The VPD spec says the serial number should be | |
| 30 // in the "serial_number" key for v2+ VPDs. However, legacy devices used a | |
| 31 // different keys to report their serial number, which we fall back to if | |
| 32 // "serial_number" is not present. | |
| 33 // | |
| 34 // Product_S/N is still special-cased due to inconsistencies with serial | |
| 35 // numbers on Lumpy devices: On these devices, serial_number is identical to | |
| 36 // Product_S/N with an appended checksum. Unfortunately, the sticker on the | |
| 37 // packaging doesn't include that checksum either (the sticker on the device | |
| 38 // does though!). The former sticker is the source of the serial number used by | |
| 39 // device management service, so we prefer Product_S/N over serial number to | |
| 40 // match the server. | |
| 41 // | |
| 42 // TODO(mnissler): Move serial_number back to the top once the server side uses | |
| 43 // the correct serial number. | |
| 44 const char* kMachineInfoSerialNumberKeys[] = { | |
| 45 "Product_S/N", // Lumpy/Alex devices | |
| 46 "serial_number", // VPD v2+ devices | |
| 47 "Product_SN", // Mario | |
| 48 "sn", // old ZGB devices (more recent ones use serial_number) | |
| 49 }; | |
| 50 | |
| 51 } // namespace | |
| 52 | |
| 53 DeviceCloudPolicyManagerChromeOS::DeviceCloudPolicyManagerChromeOS( | |
| 54 scoped_ptr<DeviceCloudPolicyStoreChromeOS> store, | |
| 55 EnterpriseInstallAttributes* install_attributes) | |
| 56 : CloudPolicyManager( | |
| 57 PolicyNamespaceKey(dm_protocol::kChromeDevicePolicyType, | |
| 58 std::string()), | |
| 59 store.get()), | |
| 60 device_store_(store.Pass()), | |
| 61 install_attributes_(install_attributes), | |
| 62 device_management_service_(NULL), | |
| 63 local_state_(NULL) {} | |
| 64 | |
| 65 DeviceCloudPolicyManagerChromeOS::~DeviceCloudPolicyManagerChromeOS() {} | |
| 66 | |
| 67 void DeviceCloudPolicyManagerChromeOS::Connect( | |
| 68 PrefService* local_state, | |
| 69 DeviceManagementService* device_management_service, | |
| 70 scoped_ptr<CloudPolicyClient::StatusProvider> device_status_provider) { | |
| 71 CHECK(!device_management_service_); | |
| 72 CHECK(device_management_service); | |
| 73 CHECK(local_state); | |
| 74 | |
| 75 local_state_ = local_state; | |
| 76 device_management_service_ = device_management_service; | |
| 77 device_status_provider_ = device_status_provider.Pass(); | |
| 78 | |
| 79 StartIfManaged(); | |
| 80 } | |
| 81 | |
| 82 void DeviceCloudPolicyManagerChromeOS::StartEnrollment( | |
| 83 const std::string& auth_token, | |
| 84 bool is_auto_enrollment, | |
| 85 const AllowedDeviceModes& allowed_device_modes, | |
| 86 const EnrollmentCallback& callback) { | |
| 87 CHECK(device_management_service_); | |
| 88 core()->Disconnect(); | |
| 89 | |
| 90 enrollment_handler_.reset( | |
| 91 new EnrollmentHandlerChromeOS( | |
| 92 device_store_.get(), install_attributes_, CreateClient(), auth_token, | |
| 93 install_attributes_->GetDeviceId(), is_auto_enrollment, | |
| 94 allowed_device_modes, | |
| 95 base::Bind(&DeviceCloudPolicyManagerChromeOS::EnrollmentCompleted, | |
| 96 base::Unretained(this), callback))); | |
| 97 enrollment_handler_->StartEnrollment(); | |
| 98 } | |
| 99 | |
| 100 void DeviceCloudPolicyManagerChromeOS::CancelEnrollment() { | |
| 101 if (enrollment_handler_.get()) { | |
| 102 enrollment_handler_.reset(); | |
| 103 StartIfManaged(); | |
| 104 } | |
| 105 } | |
| 106 | |
| 107 void DeviceCloudPolicyManagerChromeOS::OnStoreLoaded(CloudPolicyStore* store) { | |
| 108 CloudPolicyManager::OnStoreLoaded(store); | |
| 109 | |
| 110 if (!enrollment_handler_.get()) | |
| 111 StartIfManaged(); | |
| 112 } | |
| 113 | |
| 114 // static | |
| 115 std::string DeviceCloudPolicyManagerChromeOS::GetMachineID() { | |
| 116 std::string machine_id; | |
| 117 chromeos::system::StatisticsProvider* provider = | |
| 118 chromeos::system::StatisticsProvider::GetInstance(); | |
| 119 for (size_t i = 0; i < arraysize(kMachineInfoSerialNumberKeys); i++) { | |
| 120 if (provider->GetMachineStatistic(kMachineInfoSerialNumberKeys[i], | |
| 121 &machine_id) && | |
| 122 !machine_id.empty()) { | |
| 123 break; | |
| 124 } | |
| 125 } | |
| 126 | |
| 127 if (machine_id.empty()) | |
| 128 LOG(WARNING) << "Failed to get machine id."; | |
| 129 | |
| 130 return machine_id; | |
| 131 } | |
| 132 | |
| 133 // static | |
| 134 std::string DeviceCloudPolicyManagerChromeOS::GetMachineModel() { | |
| 135 std::string machine_model; | |
| 136 chromeos::system::StatisticsProvider* provider = | |
| 137 chromeos::system::StatisticsProvider::GetInstance(); | |
| 138 if (!provider->GetMachineStatistic(kMachineInfoSystemHwqual, &machine_model)) | |
| 139 LOG(WARNING) << "Failed to get machine model."; | |
| 140 | |
| 141 return machine_model; | |
| 142 } | |
| 143 | |
| 144 scoped_ptr<CloudPolicyClient> DeviceCloudPolicyManagerChromeOS::CreateClient() { | |
| 145 return make_scoped_ptr( | |
| 146 new CloudPolicyClient(GetMachineID(), GetMachineModel(), | |
| 147 USER_AFFILIATION_NONE, | |
| 148 device_status_provider_.get(), | |
| 149 device_management_service_)); | |
| 150 } | |
| 151 | |
| 152 void DeviceCloudPolicyManagerChromeOS::EnrollmentCompleted( | |
| 153 const EnrollmentCallback& callback, | |
| 154 EnrollmentStatus status) { | |
| 155 if (status.status() == EnrollmentStatus::STATUS_SUCCESS) { | |
| 156 core()->Connect(enrollment_handler_->ReleaseClient()); | |
| 157 core()->StartRefreshScheduler(); | |
| 158 core()->TrackRefreshDelayPref(local_state_, | |
| 159 prefs::kDevicePolicyRefreshRate); | |
| 160 } else { | |
| 161 StartIfManaged(); | |
| 162 } | |
| 163 | |
| 164 enrollment_handler_.reset(); | |
| 165 if (!callback.is_null()) | |
| 166 callback.Run(status); | |
| 167 } | |
| 168 | |
| 169 void DeviceCloudPolicyManagerChromeOS::StartIfManaged() { | |
| 170 if (device_management_service_ && | |
| 171 local_state_ && | |
| 172 store()->is_initialized() && | |
| 173 store()->is_managed() && | |
| 174 !service()) { | |
| 175 core()->Connect(CreateClient()); | |
| 176 core()->StartRefreshScheduler(); | |
| 177 core()->TrackRefreshDelayPref(local_state_, | |
| 178 prefs::kDevicePolicyRefreshRate); | |
| 179 } | |
| 180 } | |
| 181 | |
| 182 } // namespace policy | |
| OLD | NEW |