Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "base/prefs/pref_service.h" | |
| 6 #include "base/prefs/scoped_user_pref_update.h" | |
| 7 #include "chrome/browser/ui/app_list/app_list_prefs.h" | |
| 8 #include "chrome/browser/ui/app_list/app_list_prefs_factory.h" | |
| 9 #include "components/pref_registry/pref_registry_syncable.h" | |
| 10 | |
| 11 namespace app_list { | |
| 12 | |
| 13 namespace { | |
| 14 | |
| 15 // App list ordering and folder data. | |
| 16 const char kPrefModel[] = "app_list.model"; | |
|
battre
2014/09/30 13:36:42
I think this should go to chrome/common/pref_names
calamity
2014/10/01 08:46:15
Those are local state prefs. Since this is a profi
battre
2014/10/01 08:53:18
Acknowledged.
| |
| 17 | |
| 18 const char kModelItemPosition[] = "position"; | |
| 19 const char kModelItemType[] = "item_type"; | |
| 20 const char kModelItemParentId[] = "parent_id"; | |
| 21 const char kModelItemName[] = "name"; | |
| 22 | |
| 23 } // namespace | |
| 24 | |
| 25 // AppListInfo | |
| 26 | |
| 27 AppListPrefs::AppListInfo::AppListInfo() : item_type(ITEM_TYPE_INVALID) { | |
| 28 } | |
| 29 | |
| 30 AppListPrefs::AppListInfo::~AppListInfo() { | |
| 31 } | |
| 32 | |
| 33 scoped_ptr<base::DictionaryValue> | |
| 34 AppListPrefs::AppListInfo::CreateDictFromAppListInfo() const { | |
| 35 scoped_ptr<base::DictionaryValue> item_dict(new base::DictionaryValue()); | |
| 36 item_dict->Set(kModelItemPosition, | |
| 37 new base::StringValue(position.ToInternalValue())); | |
|
stevenjb
2014/09/30 18:16:24
nit: item_dict->SetString here and below
calamity
2014/10/01 08:46:15
Done.
| |
| 38 item_dict->Set(kModelItemParentId, new base::StringValue(parent_id)); | |
| 39 item_dict->Set(kModelItemName, new base::StringValue(name)); | |
| 40 item_dict->Set(kModelItemType, new base::FundamentalValue(item_type)); | |
|
stevenjb
2014/09/30 18:16:23
nit: SetInteger
calamity
2014/10/01 08:46:15
Done.
| |
| 41 return item_dict.Pass(); | |
| 42 } | |
| 43 | |
| 44 // static | |
| 45 scoped_ptr<AppListPrefs::AppListInfo> | |
| 46 AppListPrefs::AppListInfo::CreateAppListInfoFromDict( | |
| 47 const base::DictionaryValue* item_dict) { | |
| 48 std::string item_ordinal_string; | |
| 49 scoped_ptr<AppListPrefs::AppListInfo> info(new AppListPrefs::AppListInfo()); | |
|
stevenjb
2014/09/30 18:16:23
nit: AppListPrefs:: unnecessary inside this method
calamity
2014/10/01 08:46:15
Done.
| |
| 50 int item_type_int = -1; | |
| 51 if (!item_dict || | |
| 52 !item_dict->GetString(kModelItemPosition, &item_ordinal_string) || | |
| 53 !item_dict->GetString(kModelItemParentId, &info->parent_id) || | |
| 54 !item_dict->GetString(kModelItemName, &info->name) || | |
| 55 !item_dict->GetInteger(kModelItemType, &item_type_int) || | |
| 56 item_type_int < AppListPrefs::AppListInfo::ITEM_TYPE_BEGIN || | |
| 57 item_type_int > AppListPrefs::AppListInfo::ITEM_TYPE_END) { | |
| 58 return scoped_ptr<AppListPrefs::AppListInfo>(); | |
| 59 } | |
| 60 | |
| 61 info->position = syncer::StringOrdinal(item_ordinal_string); | |
| 62 info->item_type = | |
| 63 static_cast<AppListPrefs::AppListInfo::ItemType>(item_type_int); | |
| 64 return info.Pass(); | |
| 65 } | |
| 66 | |
| 67 // AppListPrefs | |
| 68 | |
| 69 AppListPrefs::AppListPrefs(PrefService* pref_service) | |
| 70 : pref_service_(pref_service) { | |
| 71 } | |
| 72 | |
| 73 AppListPrefs::~AppListPrefs() { | |
| 74 } | |
| 75 | |
| 76 // static | |
| 77 void AppListPrefs::RegisterProfilePrefs( | |
| 78 user_prefs::PrefRegistrySyncable* registry) { | |
| 79 registry->RegisterDictionaryPref( | |
| 80 kPrefModel, user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF); | |
| 81 } | |
| 82 | |
| 83 // static | |
| 84 AppListPrefs* AppListPrefs::Create(PrefService* pref_service) { | |
| 85 return new AppListPrefs(pref_service); | |
| 86 } | |
| 87 | |
| 88 // static | |
| 89 AppListPrefs* AppListPrefs::Get(content::BrowserContext* context) { | |
| 90 return AppListPrefsFactory::GetInstance()->GetForBrowserContext(context); | |
| 91 } | |
| 92 | |
| 93 void AppListPrefs::SetAppListInfo(const std::string& id, | |
| 94 const AppListPrefs::AppListInfo& info) { | |
| 95 DictionaryPrefUpdate update(pref_service_, kPrefModel); | |
| 96 update->Set(id, info.CreateDictFromAppListInfo().release()); | |
| 97 } | |
| 98 | |
| 99 scoped_ptr<AppListPrefs::AppListInfo> AppListPrefs::GetAppListInfo( | |
| 100 const std::string& id) const { | |
| 101 const base::DictionaryValue* model_dict = | |
| 102 pref_service_->GetDictionary(kPrefModel); | |
| 103 DCHECK(model_dict); | |
| 104 const base::DictionaryValue* item_dict = NULL; | |
| 105 if (!model_dict->GetDictionary(id, &item_dict)) | |
| 106 return scoped_ptr<AppListInfo>(); | |
| 107 | |
| 108 return AppListInfo::CreateAppListInfoFromDict(item_dict); | |
| 109 } | |
| 110 | |
| 111 void AppListPrefs::GetAllAppListInfos( | |
| 112 std::map<std::string, AppListInfo>* out) const { | |
|
stevenjb
2014/09/30 18:16:23
nit: This would be more readable with a typedef fo
calamity
2014/10/01 08:46:15
Done.
| |
| 113 const base::DictionaryValue* model_dict = | |
| 114 pref_service_->GetDictionary(kPrefModel); | |
| 115 DCHECK(model_dict); | |
| 116 | |
| 117 for (base::DictionaryValue::Iterator it(*model_dict); !it.IsAtEnd(); | |
| 118 it.Advance()) { | |
| 119 const base::DictionaryValue* item_dict = NULL; | |
| 120 it.value().GetAsDictionary(&item_dict); | |
| 121 DCHECK(item_dict); | |
| 122 (*out)[it.key()] = *AppListInfo::CreateAppListInfoFromDict(item_dict); | |
| 123 } | |
| 124 } | |
| 125 | |
| 126 void AppListPrefs::DeleteAppListInfo(const std::string& id) { | |
| 127 DictionaryPrefUpdate model_dict(pref_service_, kPrefModel); | |
| 128 model_dict->Remove(id, NULL); | |
| 129 } | |
| 130 | |
| 131 } // namespace app_list | |
| OLD | NEW |