Update To 11.40.268.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / apps / drive / drive_app_mapping.cc
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 "chrome/browser/apps/drive/drive_app_mapping.h"
6
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"
12
13 namespace {
14
15 // Key for a string value that holds the mapped chrome app id.
16 const char kKeyChromeApp[] = "chrome_app";
17
18 // Key for a boolean value of whether the mapped chrome app is auto generated.
19 // Default is false.
20 const char kKeyGenerated[] = "generated";
21
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);
27
28   // Only writes non-default value.
29   if (generated)
30     dict->SetBooleanWithoutPathExpansion(kKeyGenerated, true);
31   return dict.Pass();
32 }
33
34 }  // namespace
35
36 DriveAppMapping::DriveAppMapping(PrefService* prefs) : prefs_(prefs) {
37   GetUninstalledIdsFromPref();
38 }
39
40 DriveAppMapping::~DriveAppMapping() {
41 }
42
43 // static
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);
52 }
53
54 void DriveAppMapping::Add(const std::string& drive_app_id,
55                           const std::string& chrome_app_id,
56                           bool generated) {
57   DictionaryPrefUpdate update(prefs_, prefs::kAppLauncherDriveAppMapping);
58   update->SetWithoutPathExpansion(
59       drive_app_id, CreateInfoDict(chrome_app_id, generated).release());
60 }
61
62 void DriveAppMapping::Remove(const std::string& drive_app_id) {
63   DictionaryPrefUpdate update(prefs_, prefs::kAppLauncherDriveAppMapping);
64   update->RemoveWithoutPathExpansion(drive_app_id, NULL);
65 }
66
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)) {
75     return chrome_app_id;
76   }
77
78   return std::string();
79 }
80
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();
86        it.Advance()) {
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,
92                                                  &value_string) &&
93         value_string == chrome_app_id) {
94       return it.key();
95     }
96   }
97   return std::string();
98 }
99
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();
105        it.Advance()) {
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,
112                                                  &value_string) &&
113         value_string == chrome_app_id &&
114         info_dict->GetBooleanWithoutPathExpansion(kKeyGenerated, &generated)) {
115       return generated;
116     }
117   }
118
119   return false;
120 }
121
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();
127        it.Advance()) {
128     keys.insert(it.key());
129   }
130   return keys;
131 }
132
133 void DriveAppMapping::AddUninstalledDriveApp(const std::string& drive_app_id) {
134   if (IsUninstalledDriveApp(drive_app_id))
135     return;
136   uninstalled_app_ids_.insert(drive_app_id);
137   UpdateUninstalledList();
138 }
139
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())
144     return;
145   uninstalled_app_ids_.erase(it);
146   UpdateUninstalledList();
147 }
148
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();
152 }
153
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) {
159     std::string app_id;
160     if (!list->GetString(i, &app_id))
161       continue;
162     uninstalled_app_ids_.insert(app_id);
163   }
164 }
165
166 void DriveAppMapping::UpdateUninstalledList() {
167   ListPrefUpdate update(prefs_, prefs::kAppLauncherUninstalledDriveApps);
168   update->Clear();
169   for (const auto& app_id : uninstalled_app_ids_)
170     update->AppendString(app_id);
171 }