| 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_store.h" |
| 6 |
| 7 #include "base/compiler_specific.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/system_access.h" |
| 13 #endif |
| 14 |
| 15 namespace { |
| 16 |
| 17 // MachineInfo key names. |
| 18 const char kMachineInfoSystemHwqual[] = "hardware_class"; |
| 19 const char kMachineInfoSerialNumber[] = "serial_number"; |
| 20 |
| 21 } // namespace |
| 22 |
| 23 namespace policy { |
| 24 |
| 25 // static |
| 26 CloudPolicyDataStore* CloudPolicyDataStore::CreateForUserPolicies() { |
| 27 return new CloudPolicyDataStore(em::DeviceRegisterRequest::USER, |
| 28 kChromeUserPolicyType, |
| 29 "", ""); |
| 30 } |
| 31 |
| 32 // static |
| 33 CloudPolicyDataStore* CloudPolicyDataStore::CreateForDevicePolicies() { |
| 34 std::string machine_model; |
| 35 std::string machine_id; |
| 36 #if defined(OS_CHROMEOS) |
| 37 chromeos::SystemAccess* sys_lib = chromeos::SystemAccess::GetInstance(); |
| 38 if (!sys_lib->GetMachineStatistic(kMachineInfoSystemHwqual, |
| 39 &machine_model)) { |
| 40 LOG(ERROR) << "Failed to get machine model."; |
| 41 } |
| 42 if (!sys_lib->GetMachineStatistic(kMachineInfoSerialNumber, |
| 43 &machine_id)) { |
| 44 LOG(ERROR) << "Failed to get machine serial number."; |
| 45 } |
| 46 #endif |
| 47 return new CloudPolicyDataStore(em::DeviceRegisterRequest::DEVICE, |
| 48 kChromeDevicePolicyType, |
| 49 machine_model, |
| 50 machine_id); |
| 51 } |
| 52 |
| 53 CloudPolicyDataStore::CloudPolicyDataStore( |
| 54 const em::DeviceRegisterRequest_Type policy_register_type, |
| 55 const std::string& policy_type, |
| 56 const std::string& machine_model, |
| 57 const std::string& machine_id) |
| 58 : policy_register_type_(policy_register_type), |
| 59 policy_type_(policy_type), |
| 60 machine_model_(machine_model), |
| 61 machine_id_(machine_id), |
| 62 token_cache_loaded_(false), |
| 63 ALLOW_THIS_IN_INITIALIZER_LIST(weak_ptr_factory_(this)) {} |
| 64 |
| 65 CloudPolicyDataStore::~CloudPolicyDataStore() { |
| 66 FOR_EACH_OBSERVER(Observer, observer_list_, OnDataStoreGoingAway()); |
| 67 } |
| 68 |
| 69 base::WeakPtr<CloudPolicyDataStore> CloudPolicyDataStore::GetWeakPtr() { |
| 70 return weak_ptr_factory_.GetWeakPtr(); |
| 71 } |
| 72 |
| 73 void CloudPolicyDataStore::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 CloudPolicyDataStore::SetGaiaToken(const std::string& gaia_token) { |
| 90 DCHECK(!user_name_.empty()); |
| 91 gaia_token_ = gaia_token; |
| 92 NotifyCredentialsChanged(); |
| 93 } |
| 94 |
| 95 void CloudPolicyDataStore::Reset() { |
| 96 user_name_ = ""; |
| 97 gaia_token_ = ""; |
| 98 device_id_ = ""; |
| 99 device_token_ = ""; |
| 100 } |
| 101 |
| 102 void CloudPolicyDataStore::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 CloudPolicyDataStore::set_device_id(const std::string& device_id) { |
| 115 device_id_ = device_id; |
| 116 } |
| 117 |
| 118 std::string CloudPolicyDataStore::device_id() const { |
| 119 return device_id_; |
| 120 } |
| 121 |
| 122 void CloudPolicyDataStore::set_user_name(const std::string& user_name) { |
| 123 user_name_ = user_name; |
| 124 } |
| 125 |
| 126 std::string CloudPolicyDataStore::device_token() const { |
| 127 return device_token_; |
| 128 } |
| 129 |
| 130 std::string CloudPolicyDataStore::gaia_token() const { |
| 131 return gaia_token_; |
| 132 } |
| 133 |
| 134 std::string CloudPolicyDataStore::machine_id() const { |
| 135 return machine_id_; |
| 136 } |
| 137 |
| 138 std::string CloudPolicyDataStore::machine_model() const { |
| 139 return machine_model_; |
| 140 } |
| 141 |
| 142 em::DeviceRegisterRequest_Type |
| 143 CloudPolicyDataStore::policy_register_type() const { |
| 144 return policy_register_type_; |
| 145 } |
| 146 |
| 147 std::string CloudPolicyDataStore::policy_type() const { |
| 148 return policy_type_; |
| 149 } |
| 150 |
| 151 bool CloudPolicyDataStore::token_cache_loaded() const { |
| 152 return token_cache_loaded_; |
| 153 } |
| 154 |
| 155 std::string CloudPolicyDataStore::user_name() const { |
| 156 return user_name_; |
| 157 } |
| 158 |
| 159 void CloudPolicyDataStore::AddObserver( |
| 160 CloudPolicyDataStore::Observer* observer) { |
| 161 observer_list_.AddObserver(observer); |
| 162 } |
| 163 |
| 164 void CloudPolicyDataStore::RemoveObserver( |
| 165 CloudPolicyDataStore::Observer* observer) { |
| 166 observer_list_.RemoveObserver(observer); |
| 167 } |
| 168 |
| 169 void CloudPolicyDataStore::NotifyCredentialsChanged() { |
| 170 FOR_EACH_OBSERVER(Observer, observer_list_, OnCredentialsChanged()); |
| 171 } |
| 172 |
| 173 void CloudPolicyDataStore::NotifyDeviceTokenChanged() { |
| 174 FOR_EACH_OBSERVER(Observer, observer_list_, OnDeviceTokenChanged()); |
| 175 } |
| 176 |
| 177 } // namespace policy |
| OLD | NEW |