| OLD | NEW |
| (Empty) | |
| 1 // Copyright (c) 2011 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/cloud_policy_data.h" |
| 6 |
| 7 #include "base/string_util.h" |
| 8 #include "chrome/browser/policy/proto/device_management_backend.pb.h" |
| 9 #include "chrome/browser/policy/proto/device_management_constants.h" |
| 10 |
| 11 #if defined(OS_CHROMEOS) |
| 12 #include "chrome/browser/chromeos/cros/cros_library.h" |
| 13 #include "chrome/browser/chromeos/login/ownership_service.h" |
| 14 #include "chrome/browser/chromeos/login/user_manager.h" |
| 15 #include "chrome/browser/chromeos/system_access.h" |
| 16 #endif |
| 17 |
| 18 namespace { |
| 19 |
| 20 // MachineInfo key names. |
| 21 static const char kMachineInfoSystemHwqual[] = "hardware_class"; |
| 22 static const char kMachineInfoSerialNumber[] = "serial_number"; |
| 23 |
| 24 } // namespace |
| 25 |
| 26 namespace policy { |
| 27 |
| 28 namespace em = enterprise_management; |
| 29 |
| 30 // static |
| 31 CloudPolicyData* CloudPolicyData::CreateForUserPolicies() { |
| 32 return new CloudPolicyData(em::DeviceRegisterRequest::USER, |
| 33 kChromeUserPolicyType, |
| 34 "", ""); |
| 35 } |
| 36 |
| 37 // static |
| 38 CloudPolicyData* CloudPolicyData::CreateForDevicePolicies() { |
| 39 std::string machine_model; |
| 40 std::string machine_id; |
| 41 #if defined(OS_CHROMEOS) |
| 42 chromeos::SystemAccess* sys_lib = chromeos::SystemAccess::GetInstance(); |
| 43 if (!sys_lib->GetMachineStatistic(kMachineInfoSystemHwqual, |
| 44 &machine_model)) { |
| 45 LOG(ERROR) << "Failed to get machine model."; |
| 46 } |
| 47 if (!sys_lib->GetMachineStatistic(kMachineInfoSerialNumber, |
| 48 &machine_id)) { |
| 49 LOG(ERROR) << "Failed to get machine serial number."; |
| 50 } |
| 51 #endif |
| 52 return new CloudPolicyData(em::DeviceRegisterRequest::DEVICE, |
| 53 kChromeDevicePolicyType, |
| 54 machine_model, |
| 55 machine_id); |
| 56 } |
| 57 |
| 58 CloudPolicyData::CloudPolicyData( |
| 59 const em::DeviceRegisterRequest_Type policy_register_type, |
| 60 const std::string& policy_type, |
| 61 const std::string& machine_model, |
| 62 const std::string& machine_id) |
| 63 : policy_register_type_(policy_register_type), |
| 64 policy_type_(policy_type), |
| 65 machine_model_(machine_model), |
| 66 machine_id_(machine_id), |
| 67 token_cache_loaded_(false) {} |
| 68 |
| 69 CloudPolicyData::~CloudPolicyData() { |
| 70 FOR_EACH_OBSERVER(Observer, observer_list_, OnPolicyDataGoingAway()); |
| 71 } |
| 72 |
| 73 void CloudPolicyData::SetDeviceToken(const std::string& device_token, |
| 74 bool from_cache) { |
| 75 DCHECK(token_cache_loaded_ != from_cache); |
| 76 if (!token_cache_loaded_) { |
| 77 // The cache should be the first to set the token. (It may be "") |
| 78 DCHECK(from_cache); |
| 79 token_cache_loaded_ = true; |
| 80 } else { |
| 81 // The cache should never set the token later. |
| 82 DCHECK(!from_cache); |
| 83 } |
| 84 device_token_ = device_token; |
| 85 token_cache_loaded_ = true; |
| 86 NotifyDeviceTokenChanged(); |
| 87 } |
| 88 |
| 89 void CloudPolicyData::SetGaiaToken(const std::string& gaia_token) { |
| 90 DCHECK(!user_name_.empty()); |
| 91 gaia_token_ = gaia_token; |
| 92 NotifyCredentialsChanged(); |
| 93 } |
| 94 |
| 95 void CloudPolicyData::Reset() { |
| 96 user_name_ = ""; |
| 97 gaia_token_ = ""; |
| 98 device_id_ = ""; |
| 99 device_token_ = ""; |
| 100 } |
| 101 |
| 102 void CloudPolicyData::SetupForTesting(const std::string& device_token, |
| 103 const std::string& device_id, |
| 104 const std::string& user_name, |
| 105 const std::string& gaia_token, |
| 106 bool token_cache_loaded) { |
| 107 device_id_ = device_id; |
| 108 user_name_ = user_name; |
| 109 gaia_token_ = gaia_token; |
| 110 device_token_ = device_token; |
| 111 token_cache_loaded_ = token_cache_loaded; |
| 112 } |
| 113 |
| 114 void CloudPolicyData::set_device_id(const std::string& device_id) { |
| 115 device_id_ = device_id; |
| 116 } |
| 117 |
| 118 std::string CloudPolicyData::device_id() const { |
| 119 return device_id_; |
| 120 } |
| 121 |
| 122 void CloudPolicyData::set_user_name(const std::string& user_name) { |
| 123 user_name_ = user_name; |
| 124 } |
| 125 |
| 126 std::string CloudPolicyData::device_token() const { |
| 127 return device_token_; |
| 128 } |
| 129 |
| 130 std::string CloudPolicyData::gaia_token() const { |
| 131 return gaia_token_; |
| 132 } |
| 133 |
| 134 std::string CloudPolicyData::machine_id() const { |
| 135 return machine_id_; |
| 136 } |
| 137 |
| 138 std::string CloudPolicyData::machine_model() const { |
| 139 return machine_model_; |
| 140 } |
| 141 |
| 142 em::DeviceRegisterRequest_Type |
| 143 CloudPolicyData::policy_register_type() const { |
| 144 return policy_register_type_; |
| 145 } |
| 146 |
| 147 std::string CloudPolicyData::policy_type() const { |
| 148 return policy_type_; |
| 149 } |
| 150 |
| 151 bool CloudPolicyData::token_cache_loaded() const { |
| 152 return token_cache_loaded_; |
| 153 } |
| 154 |
| 155 std::string CloudPolicyData::user_name() const { |
| 156 return user_name_; |
| 157 } |
| 158 |
| 159 void CloudPolicyData::AddObserver(CloudPolicyData::Observer* observer) { |
| 160 observer_list_.AddObserver(observer); |
| 161 } |
| 162 |
| 163 void CloudPolicyData::RemoveObserver(CloudPolicyData::Observer* observer) { |
| 164 observer_list_.RemoveObserver(observer); |
| 165 } |
| 166 |
| 167 void CloudPolicyData::NotifyCredentialsChanged() { |
| 168 FOR_EACH_OBSERVER(Observer, observer_list_, OnCredentialsChanged()); |
| 169 } |
| 170 |
| 171 void CloudPolicyData::NotifyDeviceTokenChanged() { |
| 172 FOR_EACH_OBSERVER(Observer, observer_list_, OnDeviceTokenChanged()); |
| 173 } |
| 174 |
| 175 |
| 176 } // namespace policy |
| OLD | NEW |