| 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/cloud_policy_data_store.h" | |
| 6 | |
| 7 #include "base/basictypes.h" | |
| 8 #include "base/compiler_specific.h" | |
| 9 #include "chrome/browser/policy/proto/device_management_backend.pb.h" | |
| 10 | |
| 11 namespace em = enterprise_management; | |
| 12 | |
| 13 namespace policy { | |
| 14 | |
| 15 CloudPolicyDataStore::~CloudPolicyDataStore() {} | |
| 16 | |
| 17 // static | |
| 18 CloudPolicyDataStore* CloudPolicyDataStore::CreateForUserPolicies() { | |
| 19 return new CloudPolicyDataStore(em::DeviceRegisterRequest::USER, | |
| 20 dm_protocol::kChromeUserPolicyType); | |
| 21 } | |
| 22 | |
| 23 // static | |
| 24 CloudPolicyDataStore* CloudPolicyDataStore::CreateForDevicePolicies() { | |
| 25 return new CloudPolicyDataStore(em::DeviceRegisterRequest::DEVICE, | |
| 26 dm_protocol::kChromeDevicePolicyType); | |
| 27 } | |
| 28 | |
| 29 CloudPolicyDataStore::CloudPolicyDataStore( | |
| 30 const em::DeviceRegisterRequest_Type policy_register_type, | |
| 31 const std::string& policy_type) | |
| 32 : user_affiliation_(USER_AFFILIATION_NONE), | |
| 33 policy_register_type_(policy_register_type), | |
| 34 policy_type_(policy_type), | |
| 35 known_machine_id_(false), | |
| 36 reregister_(false), | |
| 37 token_cache_loaded_(false), | |
| 38 policy_fetching_enabled_(true), | |
| 39 device_mode_(DEVICE_MODE_PENDING) {} | |
| 40 | |
| 41 void CloudPolicyDataStore::SetDeviceToken(const std::string& device_token, | |
| 42 bool from_cache) { | |
| 43 DCHECK(token_cache_loaded_ != from_cache); | |
| 44 if (!token_cache_loaded_) { | |
| 45 // The cache should be the first to set the token (it may be empty). | |
| 46 DCHECK(from_cache); | |
| 47 token_cache_loaded_ = true; | |
| 48 } else { | |
| 49 // The cache should never set the token later. | |
| 50 DCHECK(!from_cache); | |
| 51 } | |
| 52 device_token_ = device_token; | |
| 53 token_cache_loaded_ = true; | |
| 54 NotifyDeviceTokenChanged(); | |
| 55 } | |
| 56 | |
| 57 void CloudPolicyDataStore::SetGaiaToken(const std::string& gaia_token) { | |
| 58 DCHECK(!user_name_.empty()); | |
| 59 gaia_token_ = gaia_token; | |
| 60 NotifyCredentialsChanged(); | |
| 61 } | |
| 62 | |
| 63 void CloudPolicyDataStore::SetOAuthToken(const std::string& oauth_token) { | |
| 64 DCHECK(!user_name_.empty()); | |
| 65 oauth_token_ = oauth_token; | |
| 66 NotifyCredentialsChanged(); | |
| 67 } | |
| 68 | |
| 69 void CloudPolicyDataStore::Reset() { | |
| 70 user_name_ = ""; | |
| 71 gaia_token_ = ""; | |
| 72 device_id_ = ""; | |
| 73 device_token_ = ""; | |
| 74 } | |
| 75 | |
| 76 void CloudPolicyDataStore::SetupForTesting(const std::string& device_token, | |
| 77 const std::string& device_id, | |
| 78 const std::string& user_name, | |
| 79 const std::string& gaia_token, | |
| 80 bool token_cache_loaded) { | |
| 81 device_id_ = device_id; | |
| 82 user_name_ = user_name; | |
| 83 gaia_token_ = gaia_token; | |
| 84 device_token_ = device_token; | |
| 85 token_cache_loaded_ = token_cache_loaded; | |
| 86 } | |
| 87 | |
| 88 void CloudPolicyDataStore::set_device_id(const std::string& device_id) { | |
| 89 device_id_ = device_id; | |
| 90 } | |
| 91 | |
| 92 void CloudPolicyDataStore::set_machine_id(const std::string& machine_id) { | |
| 93 machine_id_ = machine_id; | |
| 94 } | |
| 95 | |
| 96 void CloudPolicyDataStore::set_machine_model(const std::string& machine_model) { | |
| 97 machine_model_ = machine_model; | |
| 98 } | |
| 99 | |
| 100 const std::string& CloudPolicyDataStore::device_id() const { | |
| 101 return device_id_; | |
| 102 } | |
| 103 | |
| 104 void CloudPolicyDataStore::set_user_name(const std::string& user_name) { | |
| 105 user_name_ = user_name; | |
| 106 } | |
| 107 | |
| 108 void CloudPolicyDataStore::set_user_affiliation( | |
| 109 UserAffiliation user_affiliation) { | |
| 110 user_affiliation_ = user_affiliation; | |
| 111 } | |
| 112 | |
| 113 void CloudPolicyDataStore::set_known_machine_id(bool known_machine_id) { | |
| 114 known_machine_id_ = known_machine_id; | |
| 115 } | |
| 116 | |
| 117 void CloudPolicyDataStore::set_policy_fetching_enabled( | |
| 118 bool policy_fetching_enabled) { | |
| 119 policy_fetching_enabled_ = policy_fetching_enabled; | |
| 120 } | |
| 121 | |
| 122 void CloudPolicyDataStore::set_device_mode(DeviceMode device_mode) { | |
| 123 device_mode_ = device_mode; | |
| 124 } | |
| 125 | |
| 126 void CloudPolicyDataStore::set_reregister(bool reregister) { | |
| 127 reregister_ = reregister; | |
| 128 } | |
| 129 | |
| 130 const std::string& CloudPolicyDataStore::device_token() const { | |
| 131 return device_token_; | |
| 132 } | |
| 133 | |
| 134 const std::string& CloudPolicyDataStore::gaia_token() const { | |
| 135 return gaia_token_; | |
| 136 } | |
| 137 | |
| 138 const std::string& CloudPolicyDataStore::oauth_token() const { | |
| 139 return oauth_token_; | |
| 140 } | |
| 141 | |
| 142 bool CloudPolicyDataStore::has_auth_token() const { | |
| 143 return !oauth_token_.empty() || !gaia_token_.empty(); | |
| 144 } | |
| 145 | |
| 146 const std::string& CloudPolicyDataStore::machine_id() const { | |
| 147 return machine_id_; | |
| 148 } | |
| 149 | |
| 150 const std::string& CloudPolicyDataStore::machine_model() const { | |
| 151 return machine_model_; | |
| 152 } | |
| 153 | |
| 154 em::DeviceRegisterRequest_Type | |
| 155 CloudPolicyDataStore::policy_register_type() const { | |
| 156 return policy_register_type_; | |
| 157 } | |
| 158 | |
| 159 const std::string& CloudPolicyDataStore::policy_type() const { | |
| 160 return policy_type_; | |
| 161 } | |
| 162 | |
| 163 bool CloudPolicyDataStore::token_cache_loaded() const { | |
| 164 return token_cache_loaded_; | |
| 165 } | |
| 166 | |
| 167 bool CloudPolicyDataStore::policy_fetching_enabled() const { | |
| 168 return policy_fetching_enabled_; | |
| 169 } | |
| 170 | |
| 171 const std::string& CloudPolicyDataStore::user_name() const { | |
| 172 return user_name_; | |
| 173 } | |
| 174 | |
| 175 UserAffiliation CloudPolicyDataStore::user_affiliation() const { | |
| 176 return user_affiliation_; | |
| 177 } | |
| 178 | |
| 179 bool CloudPolicyDataStore::known_machine_id() const { | |
| 180 return known_machine_id_; | |
| 181 } | |
| 182 | |
| 183 DeviceMode CloudPolicyDataStore::device_mode() const { | |
| 184 return device_mode_; | |
| 185 } | |
| 186 | |
| 187 bool CloudPolicyDataStore::reregister() const { | |
| 188 return reregister_; | |
| 189 } | |
| 190 | |
| 191 #if defined(OS_CHROMEOS) | |
| 192 DeviceStatusCollector* | |
| 193 CloudPolicyDataStore::device_status_collector() { | |
| 194 return device_status_collector_.get(); | |
| 195 } | |
| 196 | |
| 197 void CloudPolicyDataStore::set_device_status_collector( | |
| 198 DeviceStatusCollector* collector) { | |
| 199 device_status_collector_.reset(collector); | |
| 200 } | |
| 201 #endif // OS_CHROMEOS | |
| 202 | |
| 203 void CloudPolicyDataStore::AddObserver( | |
| 204 CloudPolicyDataStore::Observer* observer) { | |
| 205 observer_list_.AddObserver(observer); | |
| 206 } | |
| 207 | |
| 208 void CloudPolicyDataStore::RemoveObserver( | |
| 209 CloudPolicyDataStore::Observer* observer) { | |
| 210 observer_list_.RemoveObserver(observer); | |
| 211 } | |
| 212 | |
| 213 void CloudPolicyDataStore::NotifyCredentialsChanged() { | |
| 214 FOR_EACH_OBSERVER(Observer, observer_list_, OnCredentialsChanged()); | |
| 215 } | |
| 216 | |
| 217 void CloudPolicyDataStore::NotifyDeviceTokenChanged() { | |
| 218 FOR_EACH_OBSERVER(Observer, observer_list_, OnDeviceTokenChanged()); | |
| 219 } | |
| 220 | |
| 221 } // namespace policy | |
| OLD | NEW |