Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1018)

Side by Side Diff: chrome/browser/policy/cloud_policy_data_store.cc

Issue 7298012: Consolidate data storage and notifications in the cloud policy subsystem (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: address comments + tweaks Created 9 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(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 "base/string_util.h"
Joao da Silva 2011/07/07 16:54:16 Nit: string_util.h not used
gfeher 2011/07/08 09:19:14 Done.
9 #include "chrome/browser/policy/proto/device_management_backend.pb.h"
10 #include "chrome/browser/policy/proto/device_management_constants.h"
11
12 #if defined(OS_CHROMEOS)
13 #include "chrome/browser/chromeos/cros/cros_library.h"
14 #include "chrome/browser/chromeos/login/ownership_service.h"
15 #include "chrome/browser/chromeos/login/user_manager.h"
Joao da Silva 2011/07/07 16:54:16 Nit: cros_library.h, ownership_service.h and user_
gfeher 2011/07/08 09:19:14 Done.
16 #include "chrome/browser/chromeos/system_access.h"
17 #endif
18
19 namespace {
20
21 // MachineInfo key names.
22 static const char kMachineInfoSystemHwqual[] = "hardware_class";
Joao da Silva 2011/07/07 16:54:16 Is static needed inside the anonymous namespace?
gfeher 2011/07/08 09:19:14 Done.
23 static const char kMachineInfoSerialNumber[] = "serial_number";
24
25 } // namespace
26
27 namespace policy {
28
29 namespace em = enterprise_management;
Joao da Silva 2011/07/07 16:54:16 This is already defined in the .h. Actually, is it
gfeher 2011/07/08 09:19:14 It's done in other .h files as well. I guess the o
30
31 // static
32 CloudPolicyDataStore* CloudPolicyDataStore::CreateForUserPolicies() {
33 return new CloudPolicyDataStore(em::DeviceRegisterRequest::USER,
34 kChromeUserPolicyType,
35 "", "");
36 }
37
38 // static
39 CloudPolicyDataStore* CloudPolicyDataStore::CreateForDevicePolicies() {
40 std::string machine_model;
41 std::string machine_id;
42 #if defined(OS_CHROMEOS)
43 chromeos::SystemAccess* sys_lib = chromeos::SystemAccess::GetInstance();
44 if (!sys_lib->GetMachineStatistic(kMachineInfoSystemHwqual,
45 &machine_model)) {
46 LOG(ERROR) << "Failed to get machine model.";
47 }
48 if (!sys_lib->GetMachineStatistic(kMachineInfoSerialNumber,
49 &machine_id)) {
50 LOG(ERROR) << "Failed to get machine serial number.";
51 }
52 #endif
53 return new CloudPolicyDataStore(em::DeviceRegisterRequest::DEVICE,
54 kChromeDevicePolicyType,
55 machine_model,
56 machine_id);
57 }
58
59 CloudPolicyDataStore::CloudPolicyDataStore(
60 const em::DeviceRegisterRequest_Type policy_register_type,
61 const std::string& policy_type,
62 const std::string& machine_model,
63 const std::string& machine_id)
64 : policy_register_type_(policy_register_type),
65 policy_type_(policy_type),
66 machine_model_(machine_model),
67 machine_id_(machine_id),
68 token_cache_loaded_(false),
69 ALLOW_THIS_IN_INITIALIZER_LIST(weak_ptr_factory_(this)) {}
70
71 CloudPolicyDataStore::~CloudPolicyDataStore() {
72 FOR_EACH_OBSERVER(Observer, observer_list_, OnDataStoreGoingAway());
73 }
74
75 base::WeakPtr<CloudPolicyDataStore> CloudPolicyDataStore::GetWeakPtr() {
76 return weak_ptr_factory_.GetWeakPtr();
77 }
78
79 void CloudPolicyDataStore::SetDeviceToken(const std::string& device_token,
80 bool from_cache) {
81 DCHECK(token_cache_loaded_ != from_cache);
82 if (!token_cache_loaded_) {
83 // The cache should be the first to set the token. (It may be "")
84 DCHECK(from_cache);
85 token_cache_loaded_ = true;
86 } else {
87 // The cache should never set the token later.
88 DCHECK(!from_cache);
89 }
90 device_token_ = device_token;
91 token_cache_loaded_ = true;
92 NotifyDeviceTokenChanged();
93 }
94
95 void CloudPolicyDataStore::SetGaiaToken(const std::string& gaia_token) {
96 DCHECK(!user_name_.empty());
97 gaia_token_ = gaia_token;
98 NotifyCredentialsChanged();
99 }
100
101 void CloudPolicyDataStore::Reset() {
102 user_name_ = "";
103 gaia_token_ = "";
104 device_id_ = "";
105 device_token_ = "";
106 }
107
108 void CloudPolicyDataStore::SetupForTesting(const std::string& device_token,
109 const std::string& device_id,
110 const std::string& user_name,
111 const std::string& gaia_token,
112 bool token_cache_loaded) {
113 device_id_ = device_id;
114 user_name_ = user_name;
115 gaia_token_ = gaia_token;
116 device_token_ = device_token;
117 token_cache_loaded_ = token_cache_loaded;
118 }
119
120 void CloudPolicyDataStore::set_device_id(const std::string& device_id) {
121 device_id_ = device_id;
122 }
123
124 std::string CloudPolicyDataStore::device_id() const {
125 return device_id_;
126 }
127
128 void CloudPolicyDataStore::set_user_name(const std::string& user_name) {
129 user_name_ = user_name;
130 }
131
132 std::string CloudPolicyDataStore::device_token() const {
133 return device_token_;
134 }
135
136 std::string CloudPolicyDataStore::gaia_token() const {
137 return gaia_token_;
138 }
139
140 std::string CloudPolicyDataStore::machine_id() const {
141 return machine_id_;
142 }
143
144 std::string CloudPolicyDataStore::machine_model() const {
145 return machine_model_;
146 }
147
148 em::DeviceRegisterRequest_Type
149 CloudPolicyDataStore::policy_register_type() const {
150 return policy_register_type_;
151 }
152
153 std::string CloudPolicyDataStore::policy_type() const {
154 return policy_type_;
155 }
156
157 bool CloudPolicyDataStore::token_cache_loaded() const {
158 return token_cache_loaded_;
159 }
160
161 std::string CloudPolicyDataStore::user_name() const {
162 return user_name_;
163 }
164
165 void CloudPolicyDataStore::AddObserver(
166 CloudPolicyDataStore::Observer* observer) {
167 observer_list_.AddObserver(observer);
168 }
169
170 void CloudPolicyDataStore::RemoveObserver(
171 CloudPolicyDataStore::Observer* observer) {
172 observer_list_.RemoveObserver(observer);
173 }
174
175 void CloudPolicyDataStore::NotifyCredentialsChanged() {
176 FOR_EACH_OBSERVER(Observer, observer_list_, OnCredentialsChanged());
177 }
178
179 void CloudPolicyDataStore::NotifyDeviceTokenChanged() {
180 FOR_EACH_OBSERVER(Observer, observer_list_, OnDeviceTokenChanged());
181 }
182
183 } // namespace policy
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698