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 scoped_ptr<AppListPrefs::AppListInfo> CreateAppListInfoFromDict( | |
24 const base::DictionaryValue* item_dict) { | |
Matt Giuca
2014/09/30 06:04:42
Any reason you didn't make these two guys methods
calamity
2014/09/30 08:37:08
Done.
| |
25 std::string item_ordinal_string; | |
26 scoped_ptr<AppListPrefs::AppListInfo> info(new AppListPrefs::AppListInfo()); | |
27 int item_type_int = -1; | |
28 if (!item_dict || | |
29 !item_dict->GetString(kModelItemPosition, &item_ordinal_string) || | |
30 !item_dict->GetString(kModelItemParentId, &info->parent_id) || | |
31 !item_dict->GetString(kModelItemName, &info->name) || | |
32 !item_dict->GetInteger(kModelItemType, &item_type_int) || | |
33 item_type_int < AppListPrefs::AppListInfo::ITEM_TYPE_BEGIN || | |
34 item_type_int > AppListPrefs::AppListInfo::ITEM_TYPE_END) { | |
35 return scoped_ptr<AppListPrefs::AppListInfo>(); | |
36 } | |
37 | |
38 info->position = syncer::StringOrdinal(item_ordinal_string); | |
39 info->item_type = | |
40 static_cast<AppListPrefs::AppListInfo::ItemType>(item_type_int); | |
41 return info.Pass(); | |
42 } | |
43 | |
44 scoped_ptr<base::DictionaryValue> CreateDictFromAppListInfo( | |
45 const AppListPrefs::AppListInfo& info) { | |
46 scoped_ptr<base::DictionaryValue> item_dict(new base::DictionaryValue()); | |
47 item_dict->Set(kModelItemPosition, | |
48 new base::StringValue(info.position.ToInternalValue())); | |
49 item_dict->Set(kModelItemParentId, new base::StringValue(info.parent_id)); | |
50 item_dict->Set(kModelItemName, new base::StringValue(info.name)); | |
51 item_dict->Set(kModelItemType, new base::FundamentalValue(info.item_type)); | |
52 return item_dict.Pass(); | |
53 } | |
54 | |
55 } // namespace | |
56 | |
57 // AppListInfo | |
58 | |
59 AppListPrefs::AppListInfo::AppListInfo() : item_type(ITEM_TYPE_INVALID) { | |
60 } | |
61 | |
62 AppListPrefs::AppListInfo::~AppListInfo() { | |
63 } | |
64 | |
65 AppListPrefs::AppListPrefs(PrefService* pref_service) | |
66 : pref_service_(pref_service) { | |
67 } | |
68 | |
69 AppListPrefs::~AppListPrefs() { | |
70 } | |
71 | |
72 // AppListPrefs | |
73 | |
74 // static | |
75 void AppListPrefs::RegisterProfilePrefs( | |
76 user_prefs::PrefRegistrySyncable* registry) { | |
77 registry->RegisterDictionaryPref( | |
78 kPrefModel, user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF); | |
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 AppListPrefs::AppListInfo& info) { | |
93 DictionaryPrefUpdate update(pref_service_, kPrefModel); | |
94 update->Set(id, CreateDictFromAppListInfo(info).release()); | |
95 } | |
96 | |
97 scoped_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 scoped_ptr<AppListInfo>(); | |
105 | |
106 return CreateAppListInfoFromDict(item_dict); | |
107 } | |
108 | |
109 void AppListPrefs::GetAllAppListInfos( | |
110 std::map<std::string, AppListInfo>* out) const { | |
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()] = *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 |