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