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.
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"
15 // App list ordering and folder data.
16 const char kPrefModel
[] = "app_list.model";
18 const char kModelItemPosition
[] = "position";
19 const char kModelItemType
[] = "item_type";
20 const char kModelItemParentId
[] = "parent_id";
21 const char kModelItemName
[] = "name";
27 AppListPrefs::AppListInfo::AppListInfo() : item_type(ITEM_TYPE_INVALID
) {
30 AppListPrefs::AppListInfo::~AppListInfo() {
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();
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;
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
>();
59 info
->position
= syncer::StringOrdinal(item_ordinal_string
);
60 info
->item_type
= static_cast<ItemType
>(item_type_int
);
66 AppListPrefs::AppListPrefs(PrefService
* pref_service
)
67 : pref_service_(pref_service
) {
70 AppListPrefs::~AppListPrefs() {
74 void AppListPrefs::RegisterProfilePrefs(
75 user_prefs::PrefRegistrySyncable
* registry
) {
76 registry
->RegisterDictionaryPref(kPrefModel
);
80 AppListPrefs
* AppListPrefs::Create(PrefService
* pref_service
) {
81 return new AppListPrefs(pref_service
);
85 AppListPrefs
* AppListPrefs::Get(content::BrowserContext
* context
) {
86 return AppListPrefsFactory::GetInstance()->GetForBrowserContext(context
);
89 void AppListPrefs::SetAppListInfo(const std::string
& id
,
90 const AppListInfo
& info
) {
91 DictionaryPrefUpdate
update(pref_service_
, kPrefModel
);
92 update
->Set(id
, info
.CreateDictFromAppListInfo().release());
95 scoped_ptr
<AppListPrefs::AppListInfo
> AppListPrefs::GetAppListInfo(
96 const std::string
& id
) const {
97 const base::DictionaryValue
* model_dict
=
98 pref_service_
->GetDictionary(kPrefModel
);
100 const base::DictionaryValue
* item_dict
= NULL
;
101 if (!model_dict
->GetDictionary(id
, &item_dict
))
102 return scoped_ptr
<AppListInfo
>();
104 return AppListInfo::CreateAppListInfoFromDict(item_dict
);
107 void AppListPrefs::GetAllAppListInfos(AppListInfoMap
* out
) const {
109 const base::DictionaryValue
* model_dict
=
110 pref_service_
->GetDictionary(kPrefModel
);
113 for (base::DictionaryValue::Iterator
it(*model_dict
); !it
.IsAtEnd();
115 const base::DictionaryValue
* item_dict
= NULL
;
116 it
.value().GetAsDictionary(&item_dict
);
118 (*out
)[it
.key()] = *AppListInfo::CreateAppListInfoFromDict(item_dict
);
122 void AppListPrefs::DeleteAppListInfo(const std::string
& id
) {
123 DictionaryPrefUpdate
model_dict(pref_service_
, kPrefModel
);
124 model_dict
->Remove(id
, NULL
);
127 } // namespace app_list