Add new certificateProvider extension API.
[chromium-blink-merge.git] / chrome / browser / apps / drive / drive_app_mapping.cc
blob78058f495b955ca4099c5e2bfe24bc70164ac759
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"
13 namespace {
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.
19 // Default is false.
20 const char kKeyGenerated[] = "generated";
22 scoped_ptr<base::DictionaryValue> CreateInfoDict(
23 const std::string& chrome_app_id,
24 bool generated) {
25 scoped_ptr<base::DictionaryValue> dict(new base::DictionaryValue);
26 dict->SetStringWithoutPathExpansion(kKeyChromeApp, chrome_app_id);
28 // Only writes non-default value.
29 if (generated)
30 dict->SetBooleanWithoutPathExpansion(kKeyGenerated, true);
31 return dict.Pass();
34 } // namespace
36 DriveAppMapping::DriveAppMapping(PrefService* prefs) : prefs_(prefs) {
37 GetUninstalledIdsFromPref();
40 DriveAppMapping::~DriveAppMapping() {
43 // static
44 void DriveAppMapping::RegisterProfilePrefs(
45 user_prefs::PrefRegistrySyncable* registry) {
46 registry->RegisterDictionaryPref(prefs::kAppLauncherDriveAppMapping);
47 registry->RegisterListPref(prefs::kAppLauncherUninstalledDriveApps);
50 void DriveAppMapping::Add(const std::string& drive_app_id,
51 const std::string& chrome_app_id,
52 bool generated) {
53 DictionaryPrefUpdate update(prefs_, prefs::kAppLauncherDriveAppMapping);
54 update->SetWithoutPathExpansion(
55 drive_app_id, CreateInfoDict(chrome_app_id, generated).release());
58 void DriveAppMapping::Remove(const std::string& drive_app_id) {
59 DictionaryPrefUpdate update(prefs_, prefs::kAppLauncherDriveAppMapping);
60 update->RemoveWithoutPathExpansion(drive_app_id, NULL);
63 std::string DriveAppMapping::GetChromeApp(
64 const std::string& drive_app_id) const {
65 const base::DictionaryValue* mapping =
66 prefs_->GetDictionary(prefs::kAppLauncherDriveAppMapping);
67 const base::DictionaryValue* info_dict;
68 std::string chrome_app_id;
69 if (mapping->GetDictionaryWithoutPathExpansion(drive_app_id, &info_dict) &&
70 info_dict->GetStringWithoutPathExpansion(kKeyChromeApp, &chrome_app_id)) {
71 return chrome_app_id;
74 return std::string();
77 std::string DriveAppMapping::GetDriveApp(
78 const std::string& chrome_app_id) const {
79 const base::DictionaryValue* mapping =
80 prefs_->GetDictionary(prefs::kAppLauncherDriveAppMapping);
81 for (base::DictionaryValue::Iterator it(*mapping); !it.IsAtEnd();
82 it.Advance()) {
83 const base::DictionaryValue* info_dict;
84 std::string value_string;
85 DCHECK(it.value().IsType(base::Value::TYPE_DICTIONARY));
86 if (it.value().GetAsDictionary(&info_dict) &&
87 info_dict->GetStringWithoutPathExpansion(kKeyChromeApp,
88 &value_string) &&
89 value_string == chrome_app_id) {
90 return it.key();
93 return std::string();
96 bool DriveAppMapping::IsChromeAppGenerated(
97 const std::string& chrome_app_id) const {
98 const base::DictionaryValue* mapping =
99 prefs_->GetDictionary(prefs::kAppLauncherDriveAppMapping);
100 for (base::DictionaryValue::Iterator it(*mapping); !it.IsAtEnd();
101 it.Advance()) {
102 const base::DictionaryValue* info_dict;
103 std::string value_string;
104 bool generated = false;
105 DCHECK(it.value().IsType(base::Value::TYPE_DICTIONARY));
106 if (it.value().GetAsDictionary(&info_dict) &&
107 info_dict->GetStringWithoutPathExpansion(kKeyChromeApp,
108 &value_string) &&
109 value_string == chrome_app_id &&
110 info_dict->GetBooleanWithoutPathExpansion(kKeyGenerated, &generated)) {
111 return generated;
115 return false;
118 std::set<std::string> DriveAppMapping::GetDriveAppIds() const {
119 const base::DictionaryValue* mapping =
120 prefs_->GetDictionary(prefs::kAppLauncherDriveAppMapping);
121 std::set<std::string> keys;
122 for (base::DictionaryValue::Iterator it(*mapping); !it.IsAtEnd();
123 it.Advance()) {
124 keys.insert(it.key());
126 return keys;
129 void DriveAppMapping::AddUninstalledDriveApp(const std::string& drive_app_id) {
130 if (IsUninstalledDriveApp(drive_app_id))
131 return;
132 uninstalled_app_ids_.insert(drive_app_id);
133 UpdateUninstalledList();
136 void DriveAppMapping::RemoveUninstalledDriveApp(
137 const std::string& drive_app_id) {
138 auto it = uninstalled_app_ids_.find(drive_app_id);
139 if (it == uninstalled_app_ids_.end())
140 return;
141 uninstalled_app_ids_.erase(it);
142 UpdateUninstalledList();
145 bool DriveAppMapping::IsUninstalledDriveApp(
146 const std::string& drive_app_id) const {
147 return uninstalled_app_ids_.find(drive_app_id) != uninstalled_app_ids_.end();
150 void DriveAppMapping::GetUninstalledIdsFromPref() {
151 uninstalled_app_ids_.clear();
152 const base::ListValue* list =
153 prefs_->GetList(prefs::kAppLauncherUninstalledDriveApps);
154 for (size_t i = 0; i < list->GetSize(); ++i) {
155 std::string app_id;
156 if (!list->GetString(i, &app_id))
157 continue;
158 uninstalled_app_ids_.insert(app_id);
162 void DriveAppMapping::UpdateUninstalledList() {
163 ListPrefUpdate update(prefs_, prefs::kAppLauncherUninstalledDriveApps);
164 update->Clear();
165 for (const auto& app_id : uninstalled_app_ids_)
166 update->AppendString(app_id);