| 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 "chrome/browser/ui/app_list/app_list_prefs.h" | |
| 6 #include "chrome/browser/ui/app_list/app_list_prefs_factory.h" | |
| 7 #include "components/pref_registry/pref_registry_syncable.h" | |
| 8 #include "components/prefs/pref_service.h" | |
| 9 #include "components/prefs/scoped_user_pref_update.h" | |
| 10 | |
| 11 namespace app_list { | |
| 12 | |
| 13 namespace { | |
| 14 | |
| 15 // App list ordering and folder data. | |
| 16 const char kPrefModel[] = "app_list.model"; | |
| 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(const AppListInfo& other) = default; | |
| 31 | |
| 32 AppListPrefs::AppListInfo::~AppListInfo() { | |
| 33 } | |
| 34 | |
| 35 std::unique_ptr<base::DictionaryValue> | |
| 36 AppListPrefs::AppListInfo::CreateDictFromAppListInfo() const { | |
| 37 std::unique_ptr<base::DictionaryValue> item_dict(new base::DictionaryValue()); | |
| 38 item_dict->SetString(kModelItemPosition, position.ToInternalValue()); | |
| 39 item_dict->SetString(kModelItemParentId, parent_id); | |
| 40 item_dict->SetString(kModelItemName, name); | |
| 41 item_dict->SetInteger(kModelItemType, item_type); | |
| 42 return item_dict; | |
| 43 } | |
| 44 | |
| 45 // static | |
| 46 std::unique_ptr<AppListPrefs::AppListInfo> | |
| 47 AppListPrefs::AppListInfo::CreateAppListInfoFromDict( | |
| 48 const base::DictionaryValue* item_dict) { | |
| 49 std::string item_ordinal_string; | |
| 50 std::unique_ptr<AppListInfo> info(new AppListPrefs::AppListInfo()); | |
| 51 int item_type_int = -1; | |
| 52 if (!item_dict || | |
| 53 !item_dict->GetString(kModelItemPosition, &item_ordinal_string) || | |
| 54 !item_dict->GetString(kModelItemParentId, &info->parent_id) || | |
| 55 !item_dict->GetString(kModelItemName, &info->name) || | |
| 56 !item_dict->GetInteger(kModelItemType, &item_type_int) || | |
| 57 item_type_int < ITEM_TYPE_BEGIN || item_type_int > ITEM_TYPE_END) { | |
| 58 return std::unique_ptr<AppListInfo>(); | |
| 59 } | |
| 60 | |
| 61 info->position = syncer::StringOrdinal(item_ordinal_string); | |
| 62 info->item_type = static_cast<ItemType>(item_type_int); | |
| 63 return info; | |
| 64 } | |
| 65 | |
| 66 // AppListPrefs | |
| 67 | |
| 68 AppListPrefs::AppListPrefs(PrefService* pref_service) | |
| 69 : pref_service_(pref_service) { | |
| 70 } | |
| 71 | |
| 72 AppListPrefs::~AppListPrefs() { | |
| 73 } | |
| 74 | |
| 75 // static | |
| 76 void AppListPrefs::RegisterProfilePrefs( | |
| 77 user_prefs::PrefRegistrySyncable* registry) { | |
| 78 registry->RegisterDictionaryPref(kPrefModel); | |
| 79 } | |
| 80 | |
| 81 // static | |
| 82 AppListPrefs* AppListPrefs::Create(PrefService* pref_service) { | |
| 83 return new AppListPrefs(pref_service); | |
| 84 } | |
| 85 | |
| 86 // static | |
| 87 AppListPrefs* AppListPrefs::Get(content::BrowserContext* context) { | |
| 88 return AppListPrefsFactory::GetInstance()->GetForBrowserContext(context); | |
| 89 } | |
| 90 | |
| 91 void AppListPrefs::SetAppListInfo(const std::string& id, | |
| 92 const AppListInfo& info) { | |
| 93 DictionaryPrefUpdate update(pref_service_, kPrefModel); | |
| 94 update->Set(id, info.CreateDictFromAppListInfo().release()); | |
| 95 } | |
| 96 | |
| 97 std::unique_ptr<AppListPrefs::AppListInfo> AppListPrefs::GetAppListInfo( | |
| 98 const std::string& id) const { | |
| 99 const base::DictionaryValue* model_dict = | |
| 100 pref_service_->GetDictionary(kPrefModel); | |
| 101 DCHECK(model_dict); | |
| 102 const base::DictionaryValue* item_dict = NULL; | |
| 103 if (!model_dict->GetDictionary(id, &item_dict)) | |
| 104 return std::unique_ptr<AppListInfo>(); | |
| 105 | |
| 106 return AppListInfo::CreateAppListInfoFromDict(item_dict); | |
| 107 } | |
| 108 | |
| 109 void AppListPrefs::GetAllAppListInfos(AppListInfoMap* out) const { | |
| 110 out->clear(); | |
| 111 const base::DictionaryValue* model_dict = | |
| 112 pref_service_->GetDictionary(kPrefModel); | |
| 113 DCHECK(model_dict); | |
| 114 | |
| 115 for (base::DictionaryValue::Iterator it(*model_dict); !it.IsAtEnd(); | |
| 116 it.Advance()) { | |
| 117 const base::DictionaryValue* item_dict = NULL; | |
| 118 it.value().GetAsDictionary(&item_dict); | |
| 119 DCHECK(item_dict); | |
| 120 (*out)[it.key()] = *AppListInfo::CreateAppListInfoFromDict(item_dict); | |
| 121 } | |
| 122 } | |
| 123 | |
| 124 void AppListPrefs::DeleteAppListInfo(const std::string& id) { | |
| 125 DictionaryPrefUpdate model_dict(pref_service_, kPrefModel); | |
| 126 model_dict->Remove(id, NULL); | |
| 127 } | |
| 128 | |
| 129 } // namespace app_list | |
| OLD | NEW |