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/chromeos/login/signed_settings_temp_storage.h" 6 7 #include "base/lazy_instance.h" 8 #include "base/values.h" 9 #include "chrome/browser/chromeos/login/ownership_service.h" 10 #include "chrome/browser/chromeos/login/signed_settings.h" 11 #include "chrome/browser/prefs/pref_service.h" 12 #include "chrome/browser/prefs/scoped_user_pref_update.h" 13 #include "chrome/common/pref_names.h" 14 15 static base::LazyInstance<chromeos::SignedSettings::Delegate<bool> > 16 g_signed_settings_delegate(base::LINKER_INITIALIZED); 17 18 namespace chromeos { 19 20 // static 21 void SignedSettingsTempStorage::RegisterPrefs(PrefService* local_state) { 22 local_state->RegisterDictionaryPref(prefs::kSignedSettingsTempStorage); 23 } 24 25 // static 26 bool SignedSettingsTempStorage::Store(const std::string& name, 27 const std::string& value, 28 PrefService* local_state) { 29 if (local_state) { 30 DictionaryPrefUpdate temp_storage_update( 31 local_state, prefs::kSignedSettingsTempStorage); 32 temp_storage_update->SetWithoutPathExpansion( 33 name, Value::CreateStringValue(value)); 34 return true; 35 } 36 return false; 37 } 38 39 // static 40 bool SignedSettingsTempStorage::Retrieve(const std::string& name, 41 std::string* value, 42 PrefService* local_state) { 43 if (local_state) { 44 const DictionaryValue* temp_storage = 45 local_state->GetDictionary(prefs::kSignedSettingsTempStorage); 46 if (temp_storage && temp_storage->HasKey(name)) { 47 temp_storage->GetStringWithoutPathExpansion(name, value); 48 return true; 49 } 50 } 51 return false; 52 } 53 54 // static 55 void SignedSettingsTempStorage::Finalize(PrefService* local_state) { 56 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 57 if (local_state) { 58 const DictionaryValue* temp_storage = 59 local_state->GetDictionary(prefs::kSignedSettingsTempStorage); 60 if (temp_storage) { 61 // We've stored some settings in transient storage 62 // before owner has been assigned. 63 // Now owner is assigned and key is generated and we should persist 64 // those settings into signed storage. 65 for (DictionaryValue::key_iterator it = temp_storage->begin_keys(); 66 it != temp_storage->end_keys(); 67 ++it) { 68 std::string value; 69 temp_storage->GetStringWithoutPathExpansion(*it, &value); 70 SignedSettings::CreateStorePropertyOp( 71 *it, value, 72 g_signed_settings_delegate.Pointer())->Execute(); 73 } 74 local_state->ClearPref(prefs::kSignedSettingsTempStorage); 75 } 76 } 77 } 78 79 } // namespace chromeos 80