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 kModelItemFolderId[] = "folder_id"; | |
21 const char kModelItemName[] = "name"; | |
22 | |
23 } // namespace | |
24 | |
25 // static | |
26 void AppListPrefs::RegisterProfilePrefs( | |
27 user_prefs::PrefRegistrySyncable* registry) { | |
28 registry->RegisterDictionaryPref( | |
29 kPrefModel, user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF); | |
30 } | |
31 | |
32 // static | |
33 AppListPrefs* AppListPrefs::Create(PrefService* pref_service) { | |
34 return new AppListPrefs(pref_service); | |
35 } | |
36 | |
37 // static | |
38 AppListPrefs* AppListPrefs::Get(content::BrowserContext* context) { | |
39 return AppListPrefsFactory::GetInstance()->GetForBrowserContext(context); | |
40 } | |
41 | |
42 AppListPrefs::AppListInfo::AppListInfo(const base::DictionaryValue* item_dict) { | |
Matt Giuca
2014/09/30 02:49:46
Add comments delineating the methods of different
calamity
2014/09/30 05:40:08
Done.
| |
43 std::string item_ordinal_string; | |
44 int item_type_int = -1; | |
45 if (!item_dict->GetString(kModelItemPosition, &item_ordinal_string) || | |
46 !item_dict->GetString(kModelItemFolderId, &folder_id) || | |
47 !item_dict->GetString(kModelItemName, &name) || | |
48 !item_dict->GetInteger(kModelItemType, &item_type_int) || | |
49 item_type_int < AppListInfo::ITEM_TYPE_BEGIN || | |
50 item_type_int > AppListInfo::ITEM_TYPE_END) { | |
51 item_type = AppListInfo::ITEM_TYPE_INVALID; | |
Matt Giuca
2014/09/30 02:49:47
Return early; no else.
calamity
2014/09/30 05:40:08
Done.
| |
52 } else { | |
53 position = syncer::StringOrdinal(item_ordinal_string); | |
54 item_type = static_cast<ItemType>(item_type_int); | |
55 } | |
56 } | |
57 | |
58 AppListPrefs::AppListInfo::AppListInfo() : item_type(ITEM_TYPE_INVALID) { | |
59 } | |
60 | |
61 AppListPrefs::AppListInfo::~AppListInfo() { | |
62 } | |
63 | |
64 AppListPrefs::AppListPrefs(PrefService* pref_service) | |
65 : pref_service_(pref_service) { | |
66 } | |
67 | |
68 AppListPrefs::~AppListPrefs() { | |
69 } | |
70 | |
71 void AppListPrefs::SetAppListInfo(const std::string& id, | |
72 const AppListPrefs::AppListInfo& info) { | |
73 DictionaryPrefUpdate update(pref_service_, kPrefModel); | |
74 scoped_ptr<base::DictionaryValue> info_dict(new base::DictionaryValue()); | |
Matt Giuca
2014/09/30 02:49:46
I think this should be a method on AppListInfo:
s
calamity
2014/09/30 05:40:08
Done.
| |
75 info_dict->Set(kModelItemPosition, | |
76 new base::StringValue(info.position.ToInternalValue())); | |
77 info_dict->Set(kModelItemFolderId, new base::StringValue(info.folder_id)); | |
78 info_dict->Set(kModelItemName, new base::StringValue(info.name)); | |
79 info_dict->Set(kModelItemType, new base::FundamentalValue(info.item_type)); | |
Matt Giuca
2014/09/30 02:49:46
Should this be static_cast too?
calamity
2014/09/30 05:40:07
Hmm. Doesn't seem to need to be.
| |
80 update->Set(id, info_dict.release()); | |
81 } | |
82 | |
83 scoped_ptr<AppListPrefs::AppListInfo> AppListPrefs::GetAppListInfo( | |
84 const std::string& id) const { | |
85 const base::DictionaryValue* model_dict = | |
86 pref_service_->GetDictionary(kPrefModel); | |
87 const base::DictionaryValue* item_dict = NULL; | |
88 if (!model_dict || !model_dict->GetDictionary(id, &item_dict)) { | |
89 return scoped_ptr<AppListInfo>(); | |
90 } | |
91 | |
92 scoped_ptr<AppListInfo> info(new AppListInfo(item_dict)); | |
93 if (info->item_type == AppListInfo::ITEM_TYPE_INVALID) | |
94 return scoped_ptr<AppListInfo>(); | |
95 | |
96 return info.Pass(); | |
97 } | |
98 | |
99 std::map<std::string, AppListPrefs::AppListInfo> | |
100 AppListPrefs::GetAllAppListInfos() const { | |
101 const base::DictionaryValue* model_dict = | |
102 pref_service_->GetDictionary(kPrefModel); | |
103 std::map<std::string, AppListInfo> infos; | |
104 if (!model_dict) | |
105 return infos; | |
106 | |
107 for (base::DictionaryValue::Iterator it(*model_dict); !it.IsAtEnd(); | |
108 it.Advance()) { | |
109 const base::DictionaryValue* item_dict = NULL; | |
110 it.value().GetAsDictionary(&item_dict); | |
111 DCHECK(item_dict); | |
112 infos[it.key()] = AppListInfo(item_dict); | |
113 } | |
114 return infos; | |
115 } | |
116 | |
117 void AppListPrefs::DeleteAppListInfo(const std::string& id) { | |
118 DictionaryPrefUpdate model_dict(pref_service_, kPrefModel); | |
119 model_dict->Remove(id, NULL); | |
120 } | |
121 | |
122 } // namespace app_list | |
OLD | NEW |