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 "chrome/browser/apps/drive/drive_app_mapping.h"
7 #include "base/prefs/pref_service.h"
8 #include "base/prefs/scoped_user_pref_update.h"
9 #include "base/values.h"
10 #include "chrome/common/pref_names.h"
11 #include "components/pref_registry/pref_registry_syncable.h"
15 // Key for a string value that holds the mapped chrome app id.
16 const char kKeyChromeApp
[] = "chrome_app";
18 // Key for a boolean value of whether the mapped chrome app is auto generated.
20 const char kKeyGenerated
[] = "generated";
22 scoped_ptr
<base::DictionaryValue
> CreateInfoDict(
23 const std::string
& chrome_app_id
,
25 scoped_ptr
<base::DictionaryValue
> dict(new base::DictionaryValue
);
26 dict
->SetStringWithoutPathExpansion(kKeyChromeApp
, chrome_app_id
);
28 // Only writes non-default value.
30 dict
->SetBooleanWithoutPathExpansion(kKeyGenerated
, true);
36 DriveAppMapping::DriveAppMapping(PrefService
* prefs
) : prefs_(prefs
) {
37 GetUninstalledIdsFromPref();
40 DriveAppMapping::~DriveAppMapping() {
44 void DriveAppMapping::RegisterProfilePrefs(
45 user_prefs::PrefRegistrySyncable
* registry
) {
46 registry
->RegisterDictionaryPref(
47 prefs::kAppLauncherDriveAppMapping
,
48 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF
);
49 registry
->RegisterListPref(
50 prefs::kAppLauncherUninstalledDriveApps
,
51 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF
);
54 void DriveAppMapping::Add(const std::string
& drive_app_id
,
55 const std::string
& chrome_app_id
,
57 DictionaryPrefUpdate
update(prefs_
, prefs::kAppLauncherDriveAppMapping
);
58 update
->SetWithoutPathExpansion(
59 drive_app_id
, CreateInfoDict(chrome_app_id
, generated
).release());
62 void DriveAppMapping::Remove(const std::string
& drive_app_id
) {
63 DictionaryPrefUpdate
update(prefs_
, prefs::kAppLauncherDriveAppMapping
);
64 update
->RemoveWithoutPathExpansion(drive_app_id
, NULL
);
67 std::string
DriveAppMapping::GetChromeApp(
68 const std::string
& drive_app_id
) const {
69 const base::DictionaryValue
* mapping
=
70 prefs_
->GetDictionary(prefs::kAppLauncherDriveAppMapping
);
71 const base::DictionaryValue
* info_dict
;
72 std::string chrome_app_id
;
73 if (mapping
->GetDictionaryWithoutPathExpansion(drive_app_id
, &info_dict
) &&
74 info_dict
->GetStringWithoutPathExpansion(kKeyChromeApp
, &chrome_app_id
)) {
81 std::string
DriveAppMapping::GetDriveApp(
82 const std::string
& chrome_app_id
) const {
83 const base::DictionaryValue
* mapping
=
84 prefs_
->GetDictionary(prefs::kAppLauncherDriveAppMapping
);
85 for (base::DictionaryValue::Iterator
it(*mapping
); !it
.IsAtEnd();
87 const base::DictionaryValue
* info_dict
;
88 std::string value_string
;
89 DCHECK(it
.value().IsType(base::Value::TYPE_DICTIONARY
));
90 if (it
.value().GetAsDictionary(&info_dict
) &&
91 info_dict
->GetStringWithoutPathExpansion(kKeyChromeApp
,
93 value_string
== chrome_app_id
) {
100 bool DriveAppMapping::IsChromeAppGenerated(
101 const std::string
& chrome_app_id
) const {
102 const base::DictionaryValue
* mapping
=
103 prefs_
->GetDictionary(prefs::kAppLauncherDriveAppMapping
);
104 for (base::DictionaryValue::Iterator
it(*mapping
); !it
.IsAtEnd();
106 const base::DictionaryValue
* info_dict
;
107 std::string value_string
;
108 bool generated
= false;
109 DCHECK(it
.value().IsType(base::Value::TYPE_DICTIONARY
));
110 if (it
.value().GetAsDictionary(&info_dict
) &&
111 info_dict
->GetStringWithoutPathExpansion(kKeyChromeApp
,
113 value_string
== chrome_app_id
&&
114 info_dict
->GetBooleanWithoutPathExpansion(kKeyGenerated
, &generated
)) {
122 std::set
<std::string
> DriveAppMapping::GetDriveAppIds() const {
123 const base::DictionaryValue
* mapping
=
124 prefs_
->GetDictionary(prefs::kAppLauncherDriveAppMapping
);
125 std::set
<std::string
> keys
;
126 for (base::DictionaryValue::Iterator
it(*mapping
); !it
.IsAtEnd();
128 keys
.insert(it
.key());
133 void DriveAppMapping::AddUninstalledDriveApp(const std::string
& drive_app_id
) {
134 if (IsUninstalledDriveApp(drive_app_id
))
136 uninstalled_app_ids_
.insert(drive_app_id
);
137 UpdateUninstalledList();
140 void DriveAppMapping::RemoveUninstalledDriveApp(
141 const std::string
& drive_app_id
) {
142 auto it
= uninstalled_app_ids_
.find(drive_app_id
);
143 if (it
== uninstalled_app_ids_
.end())
145 uninstalled_app_ids_
.erase(it
);
146 UpdateUninstalledList();
149 bool DriveAppMapping::IsUninstalledDriveApp(
150 const std::string
& drive_app_id
) const {
151 return uninstalled_app_ids_
.find(drive_app_id
) != uninstalled_app_ids_
.end();
154 void DriveAppMapping::GetUninstalledIdsFromPref() {
155 uninstalled_app_ids_
.clear();
156 const base::ListValue
* list
=
157 prefs_
->GetList(prefs::kAppLauncherUninstalledDriveApps
);
158 for (size_t i
= 0; i
< list
->GetSize(); ++i
) {
160 if (!list
->GetString(i
, &app_id
))
162 uninstalled_app_ids_
.insert(app_id
);
166 void DriveAppMapping::UpdateUninstalledList() {
167 ListPrefUpdate
update(prefs_
, prefs::kAppLauncherUninstalledDriveApps
);
169 for (const auto& app_id
: uninstalled_app_ids_
)
170 update
->AppendString(app_id
);