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/device_local_account_policy_service.h" | |
6 | |
7 #include "base/logging.h" | |
8 #include "base/message_loop.h" | |
9 #include "chrome/browser/policy/cloud_policy_client.h" | |
10 #include "chrome/browser/policy/cloud_policy_constants.h" | |
11 #include "chrome/browser/policy/cloud_policy_refresh_scheduler.h" | |
12 #include "chrome/browser/policy/device_local_account_policy_store.h" | |
13 #include "chrome/browser/policy/device_management_service.h" | |
14 #include "chrome/browser/policy/proto/chrome_device_policy.pb.h" | |
15 #include "chrome/browser/policy/proto/device_management_backend.pb.h" | |
16 #include "chromeos/dbus/session_manager_client.h" | |
17 #include "policy/policy_constants.h" | |
18 | |
19 namespace em = enterprise_management; | |
20 | |
21 namespace policy { | |
22 | |
23 DeviceLocalAccountPolicyBroker::DeviceLocalAccountPolicyBroker( | |
24 scoped_ptr<DeviceLocalAccountPolicyStore> store) | |
25 : store_(store.Pass()), | |
26 core_(PolicyNamespaceKey(dm_protocol::kChromePublicAccountPolicyType, | |
27 store_->account_id()), | |
28 store_.get()) {} | |
29 | |
30 DeviceLocalAccountPolicyBroker::~DeviceLocalAccountPolicyBroker() {} | |
31 | |
32 const std::string& DeviceLocalAccountPolicyBroker::account_id() const { | |
33 return store_->account_id(); | |
34 } | |
35 | |
36 void DeviceLocalAccountPolicyBroker::Connect( | |
37 scoped_ptr<CloudPolicyClient> client) { | |
38 core_.Connect(client.Pass()); | |
39 core_.StartRefreshScheduler(); | |
40 UpdateRefreshDelay(); | |
41 } | |
42 | |
43 void DeviceLocalAccountPolicyBroker::Disconnect() { | |
44 core_.Disconnect(); | |
45 } | |
46 | |
47 void DeviceLocalAccountPolicyBroker::UpdateRefreshDelay() { | |
48 if (core_.refresh_scheduler()) { | |
49 const Value* policy_value = | |
50 store_->policy_map().GetValue(key::kPolicyRefreshRate); | |
51 int delay = 0; | |
52 if (policy_value && policy_value->GetAsInteger(&delay)) | |
53 core_.refresh_scheduler()->SetRefreshDelay(delay); | |
54 } | |
55 } | |
56 | |
57 std::string DeviceLocalAccountPolicyBroker::GetDisplayName() const { | |
58 std::string display_name; | |
59 const base::Value* display_name_value = | |
60 store_->policy_map().GetValue(policy::key::kUserDisplayName); | |
61 if (display_name_value) | |
62 display_name_value->GetAsString(&display_name); | |
63 return display_name; | |
64 } | |
65 | |
66 DeviceLocalAccountPolicyService::DeviceLocalAccountPolicyService( | |
67 chromeos::SessionManagerClient* session_manager_client, | |
68 chromeos::DeviceSettingsService* device_settings_service) | |
69 : session_manager_client_(session_manager_client), | |
70 device_settings_service_(device_settings_service), | |
71 device_management_service_(NULL) { | |
72 device_settings_service_->AddObserver(this); | |
73 DeviceSettingsUpdated(); | |
74 } | |
75 | |
76 DeviceLocalAccountPolicyService::~DeviceLocalAccountPolicyService() { | |
77 device_settings_service_->RemoveObserver(this); | |
78 DeleteBrokers(&policy_brokers_); | |
79 } | |
80 | |
81 void DeviceLocalAccountPolicyService::Connect( | |
82 DeviceManagementService* device_management_service) { | |
83 DCHECK(!device_management_service_); | |
84 device_management_service_ = device_management_service; | |
85 | |
86 // Connect the brokers. | |
87 for (PolicyBrokerMap::iterator broker(policy_brokers_.begin()); | |
88 broker != policy_brokers_.end(); ++broker) { | |
89 DCHECK(!broker->second->core()->client()); | |
90 broker->second->Connect( | |
91 CreateClientForAccount(broker->second->account_id()).Pass()); | |
92 } | |
93 } | |
94 | |
95 void DeviceLocalAccountPolicyService::Disconnect() { | |
96 DCHECK(device_management_service_); | |
97 device_management_service_ = NULL; | |
98 | |
99 // Disconnect the brokers. | |
100 for (PolicyBrokerMap::iterator broker(policy_brokers_.begin()); | |
101 broker != policy_brokers_.end(); ++broker) { | |
102 broker->second->Disconnect(); | |
103 } | |
104 } | |
105 | |
106 DeviceLocalAccountPolicyBroker* | |
107 DeviceLocalAccountPolicyService::GetBrokerForAccount( | |
108 const std::string& account_id) { | |
109 PolicyBrokerMap::iterator entry = policy_brokers_.find(account_id); | |
110 if (entry == policy_brokers_.end()) | |
111 return NULL; | |
112 | |
113 if (!entry->second) | |
114 entry->second = CreateBroker(account_id).release(); | |
115 | |
116 return entry->second; | |
117 } | |
118 | |
119 bool DeviceLocalAccountPolicyService::IsPolicyAvailableForAccount( | |
120 const std::string& account_id) { | |
121 DeviceLocalAccountPolicyBroker* broker = GetBrokerForAccount(account_id); | |
122 return broker && broker->core()->store()->is_managed(); | |
123 } | |
124 | |
125 void DeviceLocalAccountPolicyService::AddObserver(Observer* observer) { | |
126 observers_.AddObserver(observer); | |
127 } | |
128 | |
129 void DeviceLocalAccountPolicyService::RemoveObserver(Observer* observer) { | |
130 observers_.RemoveObserver(observer); | |
131 } | |
132 | |
133 void DeviceLocalAccountPolicyService::OwnershipStatusChanged() { | |
134 // TODO(mnissler): The policy key has changed, re-fetch policy. For | |
135 // consumer devices, re-sign the current settings and send updates to | |
136 // session_manager. | |
137 } | |
138 | |
139 void DeviceLocalAccountPolicyService::DeviceSettingsUpdated() { | |
140 const em::ChromeDeviceSettingsProto* device_settings = | |
141 device_settings_service_->device_settings(); | |
142 if (device_settings) | |
143 UpdateAccountList(*device_settings); | |
144 } | |
145 | |
146 void DeviceLocalAccountPolicyService::OnStoreLoaded(CloudPolicyStore* store) { | |
147 DeviceLocalAccountPolicyBroker* broker = GetBrokerForStore(store); | |
148 broker->UpdateRefreshDelay(); | |
149 FOR_EACH_OBSERVER(Observer, observers_, | |
150 OnPolicyUpdated(broker->account_id())); | |
151 } | |
152 | |
153 void DeviceLocalAccountPolicyService::OnStoreError(CloudPolicyStore* store) { | |
154 DeviceLocalAccountPolicyBroker* broker = GetBrokerForStore(store); | |
155 FOR_EACH_OBSERVER(Observer, observers_, | |
156 OnPolicyUpdated(broker->account_id())); | |
157 } | |
158 | |
159 void DeviceLocalAccountPolicyService::UpdateAccountList( | |
160 const em::ChromeDeviceSettingsProto& device_settings) { | |
161 using google::protobuf::RepeatedPtrField; | |
162 | |
163 // Update |policy_brokers_|, keeping existing entries. | |
164 PolicyBrokerMap new_policy_brokers; | |
165 const RepeatedPtrField<em::DeviceLocalAccountInfoProto>& accounts = | |
166 device_settings.device_local_accounts().account(); | |
167 RepeatedPtrField<em::DeviceLocalAccountInfoProto>::const_iterator entry; | |
168 for (entry = accounts.begin(); entry != accounts.end(); ++entry) { | |
169 if (entry->has_id()) { | |
170 // Sanity check for whether this account ID has already been processed. | |
171 DeviceLocalAccountPolicyBroker*& new_broker = | |
172 new_policy_brokers[entry->id()]; | |
173 if (new_broker) { | |
174 LOG(WARNING) << "Duplicate public account " << entry->id(); | |
175 continue; | |
176 } | |
177 | |
178 // Reuse the existing broker if present. | |
179 DeviceLocalAccountPolicyBroker*& existing_broker = | |
180 policy_brokers_[entry->id()]; | |
181 new_broker = existing_broker; | |
182 existing_broker = NULL; | |
183 | |
184 // Fire up the cloud connection for fetching policy for the account from | |
185 // the cloud if this is an enterprise-managed device. | |
186 if (!new_broker || !new_broker->core()->client()) { | |
187 scoped_ptr<CloudPolicyClient> client( | |
188 CreateClientForAccount(entry->id())); | |
189 if (client.get()) { | |
190 if (!new_broker) | |
191 new_broker = CreateBroker(entry->id()).release(); | |
192 new_broker->Connect(client.Pass()); | |
193 } | |
194 } | |
195 } | |
196 } | |
197 policy_brokers_.swap(new_policy_brokers); | |
198 DeleteBrokers(&new_policy_brokers); | |
199 | |
200 FOR_EACH_OBSERVER(Observer, observers_, OnDeviceLocalAccountsChanged()); | |
201 } | |
202 | |
203 scoped_ptr<DeviceLocalAccountPolicyBroker> | |
204 DeviceLocalAccountPolicyService::CreateBroker( | |
205 const std::string& account_id) { | |
206 scoped_ptr<DeviceLocalAccountPolicyStore> store( | |
207 new DeviceLocalAccountPolicyStore(account_id, session_manager_client_, | |
208 device_settings_service_)); | |
209 scoped_ptr<DeviceLocalAccountPolicyBroker> broker( | |
210 new DeviceLocalAccountPolicyBroker(store.Pass())); | |
211 broker->core()->store()->AddObserver(this); | |
212 broker->core()->store()->Load(); | |
213 return broker.Pass(); | |
214 } | |
215 | |
216 void DeviceLocalAccountPolicyService::DeleteBrokers(PolicyBrokerMap* map) { | |
217 for (PolicyBrokerMap::iterator broker = map->begin(); broker != map->end(); | |
218 ++broker) { | |
219 if (broker->second) { | |
220 broker->second->core()->store()->RemoveObserver(this); | |
221 delete broker->second; | |
222 } | |
223 } | |
224 map->clear(); | |
225 } | |
226 | |
227 DeviceLocalAccountPolicyBroker* | |
228 DeviceLocalAccountPolicyService::GetBrokerForStore( | |
229 CloudPolicyStore* store) { | |
230 for (PolicyBrokerMap::iterator broker(policy_brokers_.begin()); | |
231 broker != policy_brokers_.end(); ++broker) { | |
232 if (broker->second->core()->store() == store) | |
233 return broker->second; | |
234 } | |
235 return NULL; | |
236 } | |
237 | |
238 scoped_ptr<CloudPolicyClient> | |
239 DeviceLocalAccountPolicyService::CreateClientForAccount( | |
240 const std::string& account_id) { | |
241 const em::PolicyData* policy_data = device_settings_service_->policy_data(); | |
242 if (!policy_data || | |
243 !policy_data->has_request_token() || | |
244 !policy_data->has_device_id() || | |
245 !device_management_service_) { | |
246 return scoped_ptr<CloudPolicyClient>(); | |
247 } | |
248 | |
249 scoped_ptr<CloudPolicyClient> client( | |
250 new CloudPolicyClient(std::string(), std::string(), | |
251 USER_AFFILIATION_MANAGED, | |
252 NULL, device_management_service_)); | |
253 client->SetupRegistration(policy_data->request_token(), | |
254 policy_data->device_id()); | |
255 return client.Pass(); | |
256 } | |
257 | |
258 } // namespace policy | |
OLD | NEW |