OLD | NEW |
| (Empty) |
1 // Copyright 2013 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/chromeos/settings/kiosk_app_local_settings.h" | |
6 | |
7 #include <set> | |
8 | |
9 #include "base/prefs/pref_service.h" | |
10 #include "base/string_util.h" | |
11 #include "chrome/browser/browser_process.h" | |
12 #include "chrome/browser/chromeos/app_mode/kiosk_app_manager.h" | |
13 #include "chrome/browser/chromeos/settings/cros_settings_names.h" | |
14 #include "chrome/browser/prefs/scoped_user_pref_update.h" | |
15 | |
16 namespace chromeos { | |
17 | |
18 KioskAppLocalSettings::KioskAppLocalSettings( | |
19 const NotifyObserversCallback& notify_cb) | |
20 : CrosSettingsProvider(notify_cb), | |
21 default_auto_launch_(""), | |
22 default_disable_bailout_shortcut_(false) { | |
23 // Local state could be NULL in test. | |
24 if (g_browser_process->local_state()) | |
25 ReadApps(); | |
26 } | |
27 | |
28 KioskAppLocalSettings::~KioskAppLocalSettings() {} | |
29 | |
30 void KioskAppLocalSettings::ReadApps() { | |
31 apps_.Clear(); | |
32 | |
33 PrefService* local_state = g_browser_process->local_state(); | |
34 const base::DictionaryValue* dict = | |
35 local_state->GetDictionary(KioskAppManager::kKioskDictionaryName); | |
36 const base::DictionaryValue* apps_dict; | |
37 if (!dict->GetDictionary(KioskAppManager::kKeyApps, &apps_dict)) | |
38 return; | |
39 | |
40 for (base::DictionaryValue::Iterator it(*apps_dict); | |
41 !it.IsAtEnd(); | |
42 it.Advance()) { | |
43 apps_.AppendString(it.key()); | |
44 } | |
45 } | |
46 | |
47 void KioskAppLocalSettings::WriteApps(const base::Value& value) { | |
48 std::set<std::string> old_apps; | |
49 for (base::ListValue::const_iterator it = apps_.begin(); | |
50 it != apps_.end(); | |
51 ++it) { | |
52 std::string app; | |
53 CHECK((*it)->GetAsString(&app)); | |
54 old_apps.insert(app); | |
55 } | |
56 | |
57 PrefService* local_state = g_browser_process->local_state(); | |
58 DictionaryPrefUpdate dict_update(local_state, | |
59 KioskAppManager::kKioskDictionaryName); | |
60 base::DictionaryValue* apps_dict; | |
61 if (!dict_update->GetDictionary(KioskAppManager::kKeyApps, &apps_dict)) { | |
62 apps_dict = new base::DictionaryValue; | |
63 dict_update->Set(KioskAppManager::kKeyApps, apps_dict); | |
64 } | |
65 | |
66 const base::ListValue* list_value; | |
67 CHECK(value.GetAsList(&list_value)); | |
68 | |
69 // Updates "apps" dictionary and |old_apps| set. | |
70 for (base::ListValue::const_iterator new_it = list_value->begin(); | |
71 new_it != list_value->end(); | |
72 ++new_it) { | |
73 std::string app; | |
74 CHECK((*new_it)->GetAsString(&app)); | |
75 | |
76 std::set<std::string>::iterator old_it = old_apps.find(app); | |
77 if (old_it != old_apps.end()) | |
78 old_apps.erase(old_it); | |
79 else | |
80 apps_dict->Set(app, new base::DictionaryValue); | |
81 } | |
82 | |
83 const std::string auto_launch_key = | |
84 std::string(kKioskAutoLaunch).substr(kKioskAppSettingsPrefixLength); | |
85 std::string auto_launch; | |
86 dict_update->GetString(auto_launch_key, &auto_launch); | |
87 bool reset_auto_launch = false; | |
88 | |
89 // Removes remaining |old_apps|. | |
90 for (std::set<std::string>::iterator it = old_apps.begin(); | |
91 it != old_apps.end(); | |
92 ++it) { | |
93 if (*it == auto_launch) | |
94 reset_auto_launch = true; | |
95 | |
96 apps_dict->Remove(*it, NULL); | |
97 } | |
98 | |
99 if (reset_auto_launch) { | |
100 dict_update->Remove(auto_launch_key, NULL); | |
101 NotifyObservers(kKioskAutoLaunch); | |
102 } | |
103 | |
104 // Updates cached value. | |
105 ReadApps(); | |
106 } | |
107 | |
108 bool KioskAppLocalSettings::IsEligibleAutoLaunchAppId( | |
109 const base::Value& value) const { | |
110 if (apps_.Find(value) != apps_.end()) | |
111 return true; | |
112 | |
113 // Empty string means no auto launch app and considered as an acceptable | |
114 // value for auto launch setting. | |
115 const base::StringValue empty(""); | |
116 return value.Equals(&empty); | |
117 } | |
118 | |
119 const base::Value* KioskAppLocalSettings::Get(const std::string& path) const { | |
120 DCHECK(HandlesSetting(path)); | |
121 | |
122 if (path == kKioskApps) | |
123 return &apps_; | |
124 | |
125 if (path == kKioskAutoLaunch || | |
126 path == kKioskDisableBailoutShortcut) { | |
127 PrefService* local_state = g_browser_process->local_state(); | |
128 const base::DictionaryValue* dict = | |
129 local_state->GetDictionary(KioskAppManager::kKioskDictionaryName); | |
130 | |
131 const base::Value* value; | |
132 if (dict->Get(path.substr(kKioskAppSettingsPrefixLength), &value)) | |
133 return value; | |
134 } | |
135 | |
136 if (path == kKioskAutoLaunch) | |
137 return &default_auto_launch_; | |
138 if (path == kKioskDisableBailoutShortcut) | |
139 return &default_disable_bailout_shortcut_; | |
140 | |
141 NOTREACHED() << "Try to get unknown kiosk app setting " << path; | |
142 return NULL; | |
143 } | |
144 | |
145 CrosSettingsProvider::TrustedStatus KioskAppLocalSettings::PrepareTrustedValues( | |
146 const base::Closure& callback) { | |
147 return TRUSTED; | |
148 } | |
149 | |
150 bool KioskAppLocalSettings::HandlesSetting(const std::string& path) const { | |
151 return StartsWithASCII(path, kKioskAppSettingsPrefix, true); | |
152 } | |
153 | |
154 void KioskAppLocalSettings::DoSet(const std::string& path, | |
155 const base::Value& in_value) { | |
156 if (path == kKioskApps) { | |
157 WriteApps(in_value); | |
158 } else if (path == kKioskAutoLaunch || | |
159 path == kKioskDisableBailoutShortcut) { | |
160 if (path == kKioskAutoLaunch && !IsEligibleAutoLaunchAppId(in_value)) | |
161 return; | |
162 | |
163 PrefService* local_state = g_browser_process->local_state(); | |
164 DictionaryPrefUpdate dict_update(local_state, | |
165 KioskAppManager::kKioskDictionaryName); | |
166 dict_update->Set(path.substr(kKioskAppSettingsPrefixLength), | |
167 in_value.DeepCopy()); | |
168 } else { | |
169 NOTREACHED() << "Try to set unknown kiosk app setting " << path; | |
170 return; | |
171 } | |
172 | |
173 NotifyObservers(path); | |
174 } | |
175 | |
176 } // namespace chromeos | |
OLD | NEW |