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

Side by Side Diff: chrome/browser/chromeos/ownership/owner_settings_service_chromeos.cc

Issue 723163002: Added key-value storage for pending changes to OwnerSettingsServiceChromeOS. This CL is an alternat… (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@2214
Patch Set: Created 6 years, 1 month 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
OLDNEW
1 // Copyright 2014 The Chromium Authors. All rights reserved. 1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "chrome/browser/chromeos/ownership/owner_settings_service_chromeos.h" 5 #include "chrome/browser/chromeos/ownership/owner_settings_service_chromeos.h"
6 6
7 #include <string> 7 #include <string>
8 8
9 #include "base/bind.h" 9 #include "base/bind.h"
10 #include "base/bind_helpers.h" 10 #include "base/bind_helpers.h"
(...skipping 139 matching lines...) Expand 10 before | Expand all | Expand 10 after
150 150
151 OwnerSettingsServiceChromeOS::OwnerSettingsServiceChromeOS( 151 OwnerSettingsServiceChromeOS::OwnerSettingsServiceChromeOS(
152 DeviceSettingsService* device_settings_service, 152 DeviceSettingsService* device_settings_service,
153 Profile* profile, 153 Profile* profile,
154 const scoped_refptr<OwnerKeyUtil>& owner_key_util) 154 const scoped_refptr<OwnerKeyUtil>& owner_key_util)
155 : ownership::OwnerSettingsService(owner_key_util), 155 : ownership::OwnerSettingsService(owner_key_util),
156 device_settings_service_(device_settings_service), 156 device_settings_service_(device_settings_service),
157 profile_(profile), 157 profile_(profile),
158 waiting_for_profile_creation_(true), 158 waiting_for_profile_creation_(true),
159 waiting_for_tpm_token_(true), 159 waiting_for_tpm_token_(true),
160 has_pending_changes_(false),
161 weak_factory_(this), 160 weak_factory_(this),
162 store_settings_factory_(this) { 161 store_settings_factory_(this) {
163 if (TPMTokenLoader::IsInitialized()) { 162 if (TPMTokenLoader::IsInitialized()) {
164 TPMTokenLoader::TPMTokenStatus tpm_token_status = 163 TPMTokenLoader::TPMTokenStatus tpm_token_status =
165 TPMTokenLoader::Get()->IsTPMTokenEnabled( 164 TPMTokenLoader::Get()->IsTPMTokenEnabled(
166 base::Bind(&OwnerSettingsServiceChromeOS::OnTPMTokenReady, 165 base::Bind(&OwnerSettingsServiceChromeOS::OnTPMTokenReady,
167 weak_factory_.GetWeakPtr())); 166 weak_factory_.GetWeakPtr()));
168 waiting_for_tpm_token_ = 167 waiting_for_tpm_token_ =
169 tpm_token_status == TPMTokenLoader::TPM_TOKEN_STATUS_UNDETERMINED; 168 tpm_token_status == TPMTokenLoader::TPM_TOKEN_STATUS_UNDETERMINED;
170 } 169 }
171 170
172 if (DBusThreadManager::IsInitialized() && 171 if (DBusThreadManager::IsInitialized() &&
173 DBusThreadManager::Get()->GetSessionManagerClient()) { 172 DBusThreadManager::Get()->GetSessionManagerClient()) {
174 DBusThreadManager::Get()->GetSessionManagerClient()->AddObserver(this); 173 DBusThreadManager::Get()->GetSessionManagerClient()->AddObserver(this);
175 } 174 }
176 175
177 if (device_settings_service_) 176 if (device_settings_service_)
178 device_settings_service_->AddObserver(this); 177 device_settings_service_->AddObserver(this);
179 178
180 registrar_.Add(this, 179 registrar_.Add(this,
181 chrome::NOTIFICATION_PROFILE_CREATED, 180 chrome::NOTIFICATION_PROFILE_CREATED,
182 content::Source<Profile>(profile_)); 181 content::Source<Profile>(profile_));
183
184 UpdateFromService();
185 } 182 }
186 183
187 OwnerSettingsServiceChromeOS::~OwnerSettingsServiceChromeOS() { 184 OwnerSettingsServiceChromeOS::~OwnerSettingsServiceChromeOS() {
188 DCHECK(thread_checker_.CalledOnValidThread()); 185 DCHECK(thread_checker_.CalledOnValidThread());
189 186
190 if (device_settings_service_) 187 if (device_settings_service_)
191 device_settings_service_->RemoveObserver(this); 188 device_settings_service_->RemoveObserver(this);
192 189
193 if (DBusThreadManager::IsInitialized() && 190 if (DBusThreadManager::IsInitialized() &&
194 DBusThreadManager::Get()->GetSessionManagerClient()) { 191 DBusThreadManager::Get()->GetSessionManagerClient()) {
(...skipping 13 matching lines...) Expand all
208 205
209 bool OwnerSettingsServiceChromeOS::HandlesSetting(const std::string& setting) { 206 bool OwnerSettingsServiceChromeOS::HandlesSetting(const std::string& setting) {
210 return DeviceSettingsProvider::IsDeviceSetting(setting); 207 return DeviceSettingsProvider::IsDeviceSetting(setting);
211 } 208 }
212 209
213 bool OwnerSettingsServiceChromeOS::Set(const std::string& setting, 210 bool OwnerSettingsServiceChromeOS::Set(const std::string& setting,
214 const base::Value& value) { 211 const base::Value& value) {
215 if (!IsOwner() && !IsOwnerInTests(user_id_)) 212 if (!IsOwner() && !IsOwnerInTests(user_id_))
216 return false; 213 return false;
217 214
218 UpdateDeviceSettings(setting, value, device_settings_); 215 pending_changes_.add(setting, make_scoped_ptr(value.DeepCopy()));
216
217 em::ChromeDeviceSettingsProto settings;
218 if (tentative_settings_.get()) {
219 settings = *tentative_settings_;
220 } else if (device_settings_service_->status() ==
221 DeviceSettingsService::STORE_SUCCESS &&
222 device_settings_service_->device_settings()) {
223 settings = *device_settings_service_->device_settings();
224 }
225 UpdateDeviceSettings(setting, value, settings);
219 em::PolicyData policy_data; 226 em::PolicyData policy_data;
220 policy_data.set_username(user_id_); 227 policy_data.set_username(user_id_);
221 CHECK(device_settings_.SerializeToString(policy_data.mutable_policy_value())); 228 CHECK(settings.SerializeToString(policy_data.mutable_policy_value()));
222 FOR_EACH_OBSERVER(OwnerSettingsService::Observer, 229 FOR_EACH_OBSERVER(OwnerSettingsService::Observer, observers_,
223 observers_,
224 OnTentativeChangesInPolicy(policy_data)); 230 OnTentativeChangesInPolicy(policy_data));
225 has_pending_changes_ = true; 231 StorePendingChanges();
226 StoreDeviceSettings();
227 return true; 232 return true;
228 } 233 }
229 234
230 bool OwnerSettingsServiceChromeOS::CommitTentativeDeviceSettings( 235 bool OwnerSettingsServiceChromeOS::CommitTentativeDeviceSettings(
231 scoped_ptr<enterprise_management::PolicyData> policy) { 236 scoped_ptr<enterprise_management::PolicyData> policy) {
232 if (!IsOwner() && !IsOwnerInTests(user_id_)) 237 if (!IsOwner() && !IsOwnerInTests(user_id_))
233 return false; 238 return false;
234 if (policy->username() != user_id_) { 239 if (policy->username() != user_id_) {
235 LOG(ERROR) << "Username mismatch: " << policy->username() << " vs. " 240 LOG(ERROR) << "Username mismatch: " << policy->username() << " vs. "
236 << user_id_; 241 << user_id_;
237 return false; 242 return false;
238 } 243 }
239 CHECK(device_settings_.ParseFromString(policy->policy_value())); 244 tentative_settings_.reset(new em::ChromeDeviceSettingsProto);
240 FOR_EACH_OBSERVER(OwnerSettingsService::Observer, 245 CHECK(tentative_settings_->ParseFromString(policy->policy_value()));
241 observers_, 246 StorePendingChanges();
242 OnTentativeChangesInPolicy(*policy));
243 has_pending_changes_ = true;
244 StoreDeviceSettings();
245 return true; 247 return true;
246 } 248 }
247 249
248 void OwnerSettingsServiceChromeOS::Observe( 250 void OwnerSettingsServiceChromeOS::Observe(
249 int type, 251 int type,
250 const content::NotificationSource& source, 252 const content::NotificationSource& source,
251 const content::NotificationDetails& details) { 253 const content::NotificationDetails& details) {
252 DCHECK(thread_checker_.CalledOnValidThread()); 254 DCHECK(thread_checker_.CalledOnValidThread());
253 if (type != chrome::NOTIFICATION_PROFILE_CREATED) { 255 if (type != chrome::NOTIFICATION_PROFILE_CREATED) {
254 NOTREACHED(); 256 NOTREACHED();
(...skipping 11 matching lines...) Expand all
266 } 268 }
267 269
268 void OwnerSettingsServiceChromeOS::OwnerKeySet(bool success) { 270 void OwnerSettingsServiceChromeOS::OwnerKeySet(bool success) {
269 DCHECK(thread_checker_.CalledOnValidThread()); 271 DCHECK(thread_checker_.CalledOnValidThread());
270 if (success) 272 if (success)
271 ReloadKeypair(); 273 ReloadKeypair();
272 } 274 }
273 275
274 void OwnerSettingsServiceChromeOS::OwnershipStatusChanged() { 276 void OwnerSettingsServiceChromeOS::OwnershipStatusChanged() {
275 DCHECK(thread_checker_.CalledOnValidThread()); 277 DCHECK(thread_checker_.CalledOnValidThread());
276 StoreDeviceSettings(); 278 StorePendingChanges();
277 } 279 }
278 280
279 void OwnerSettingsServiceChromeOS::DeviceSettingsUpdated() { 281 void OwnerSettingsServiceChromeOS::DeviceSettingsUpdated() {
280 DCHECK(thread_checker_.CalledOnValidThread()); 282 DCHECK(thread_checker_.CalledOnValidThread());
281 StoreDeviceSettings(); 283 StorePendingChanges();
282 } 284 }
283 285
284 void OwnerSettingsServiceChromeOS::OnDeviceSettingsServiceShutdown() { 286 void OwnerSettingsServiceChromeOS::OnDeviceSettingsServiceShutdown() {
285 device_settings_service_ = nullptr; 287 device_settings_service_ = nullptr;
286 } 288 }
287 289
288 // static 290 // static
289 void OwnerSettingsServiceChromeOS::IsOwnerForSafeModeAsync( 291 void OwnerSettingsServiceChromeOS::IsOwnerForSafeModeAsync(
290 const std::string& user_hash, 292 const std::string& user_hash,
291 const scoped_refptr<OwnerKeyUtil>& owner_key_util, 293 const scoped_refptr<OwnerKeyUtil>& owner_key_util,
(...skipping 287 matching lines...) Expand 10 before | Expand all | Expand 10 after
579 BrowserThread::GetBlockingPool()->GetTaskRunnerWithShutdownBehavior( 581 BrowserThread::GetBlockingPool()->GetTaskRunnerWithShutdownBehavior(
580 base::SequencedWorkerPool::SKIP_ON_SHUTDOWN); 582 base::SequencedWorkerPool::SKIP_ON_SHUTDOWN);
581 task_runner->PostTask( 583 task_runner->PostTask(
582 FROM_HERE, 584 FROM_HERE,
583 base::Bind(&LoadPrivateKey, 585 base::Bind(&LoadPrivateKey,
584 owner_key_util_, 586 owner_key_util_,
585 ProfileHelper::GetUserIdHashFromProfile(profile_), 587 ProfileHelper::GetUserIdHashFromProfile(profile_),
586 callback)); 588 callback));
587 } 589 }
588 590
589 void OwnerSettingsServiceChromeOS::StoreDeviceSettings() { 591 void OwnerSettingsServiceChromeOS::StorePendingChanges() {
590 if (!has_pending_changes_ || store_settings_factory_.HasWeakPtrs()) 592 if (!has_pending_changes() || store_settings_factory_.HasWeakPtrs() ||
593 !device_settings_service_) {
591 return; 594 return;
592 if (!UpdateFromService()) 595 }
596
597 em::ChromeDeviceSettingsProto settings;
598 if (tentative_settings_.get()) {
599 settings.Swap(tentative_settings_.get());
600 tentative_settings_.reset();
601 } else if (device_settings_service_->status() ==
602 DeviceSettingsService::STORE_SUCCESS &&
603 device_settings_service_->device_settings()) {
604 settings = *device_settings_service_->device_settings();
605 } else {
593 return; 606 return;
607 }
608
609 for (const auto& change : pending_changes_)
610 UpdateDeviceSettings(change.first, *change.second, settings);
611 pending_changes_.clear();
612
594 scoped_ptr<em::PolicyData> policy = AssemblePolicy( 613 scoped_ptr<em::PolicyData> policy = AssemblePolicy(
595 user_id_, device_settings_service_->policy_data(), &device_settings_); 614 user_id_, device_settings_service_->policy_data(), &settings);
596 has_pending_changes_ = false;
597 bool rv = AssembleAndSignPolicyAsync( 615 bool rv = AssembleAndSignPolicyAsync(
598 content::BrowserThread::GetBlockingPool(), 616 content::BrowserThread::GetBlockingPool(), policy.Pass(),
599 policy.Pass(),
600 base::Bind(&OwnerSettingsServiceChromeOS::OnPolicyAssembledAndSigned, 617 base::Bind(&OwnerSettingsServiceChromeOS::OnPolicyAssembledAndSigned,
601 store_settings_factory_.GetWeakPtr())); 618 store_settings_factory_.GetWeakPtr()));
602 if (!rv) 619 if (!rv)
603 OnSignedPolicyStored(false /* success */); 620 OnSignedPolicyStored(false /* success */);
604 } 621 }
605 622
606 void OwnerSettingsServiceChromeOS::OnPolicyAssembledAndSigned( 623 void OwnerSettingsServiceChromeOS::OnPolicyAssembledAndSigned(
607 scoped_ptr<em::PolicyFetchResponse> policy_response) { 624 scoped_ptr<em::PolicyFetchResponse> policy_response) {
608 if (!policy_response.get() || !device_settings_service_) { 625 if (!policy_response.get() || !device_settings_service_) {
609 OnSignedPolicyStored(false /* success */); 626 OnSignedPolicyStored(false /* success */);
610 return; 627 return;
611 } 628 }
612 device_settings_service_->Store( 629 device_settings_service_->Store(
613 policy_response.Pass(), 630 policy_response.Pass(),
614 base::Bind(&OwnerSettingsServiceChromeOS::OnSignedPolicyStored, 631 base::Bind(&OwnerSettingsServiceChromeOS::OnSignedPolicyStored,
615 store_settings_factory_.GetWeakPtr(), 632 store_settings_factory_.GetWeakPtr(),
616 true /* success */)); 633 true /* success */));
617 } 634 }
618 635
619 void OwnerSettingsServiceChromeOS::OnSignedPolicyStored(bool success) { 636 void OwnerSettingsServiceChromeOS::OnSignedPolicyStored(bool success) {
620 store_settings_factory_.InvalidateWeakPtrs(); 637 store_settings_factory_.InvalidateWeakPtrs();
621 FOR_EACH_OBSERVER(OwnerSettingsService::Observer, 638 FOR_EACH_OBSERVER(OwnerSettingsService::Observer,
622 observers_, 639 observers_,
623 OnSignedPolicyStored(success)); 640 OnSignedPolicyStored(success));
624 StoreDeviceSettings(); 641 StorePendingChanges();
625 if (!success)
626 has_pending_changes_ = true;
627 }
628
629 bool OwnerSettingsServiceChromeOS::UpdateFromService() {
630 if (!device_settings_service_ ||
631 device_settings_service_->status() !=
632 DeviceSettingsService::STORE_SUCCESS ||
633 !device_settings_service_->device_settings()) {
634 return false;
635 }
636 enterprise_management::ChromeDeviceSettingsProto settings =
637 *device_settings_service_->device_settings();
638 settings.MergeFrom(device_settings_);
639 device_settings_.Swap(&settings);
640 return true;
641 } 642 }
642 643
643 } // namespace chromeos 644 } // namespace chromeos
OLDNEW
« no previous file with comments | « chrome/browser/chromeos/ownership/owner_settings_service_chromeos.h ('k') | components/ownership/owner_settings_service.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698