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