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.
5 #ifndef CHROME_BROWSER_CHROMEOS_APP_MODE_KIOSK_APP_MANAGER_H_
6 #define CHROME_BROWSER_CHROMEOS_APP_MODE_KIOSK_APP_MANAGER_H_
11 #include "base/basictypes.h"
12 #include "base/callback_forward.h"
13 #include "base/lazy_instance.h"
14 #include "base/memory/scoped_ptr.h"
15 #include "base/memory/scoped_vector.h"
16 #include "base/observer_list.h"
17 #include "chrome/browser/chromeos/app_mode/kiosk_app_data_delegate.h"
18 #include "chrome/browser/chromeos/extensions/external_cache.h"
19 #include "chrome/browser/chromeos/policy/enterprise_install_attributes.h"
20 #include "chrome/browser/chromeos/settings/cros_settings.h"
21 #include "ui/gfx/image/image_skia.h"
23 class PrefRegistrySimple;
27 class RefCountedString;
30 namespace extensions {
37 class KioskAppManagerObserver;
39 // KioskAppManager manages cached app data.
40 class KioskAppManager : public KioskAppDataDelegate,
41 public ExternalCache::Delegate {
43 enum ConsumerKioskAutoLaunchStatus {
44 // Consumer kiosk mode auto-launch feature can be enabled on this machine.
45 CONSUMER_KIOSK_AUTO_LAUNCH_CONFIGURABLE,
46 // Consumer kiosk auto-launch feature is enabled on this machine.
47 CONSUMER_KIOSK_AUTO_LAUNCH_ENABLED,
48 // Consumer kiosk mode auto-launch feature is disabled and cannot any longer
49 // be enabled on this machine.
50 CONSUMER_KIOSK_AUTO_LAUNCH_DISABLED,
53 typedef base::Callback<void(bool success)> EnableKioskAutoLaunchCallback;
54 typedef base::Callback<void(ConsumerKioskAutoLaunchStatus status)>
55 GetConsumerKioskAutoLaunchStatusCallback;
57 // Struct to hold app info returned from GetApps() call.
59 App(const KioskAppData& data, bool is_extension_pending);
69 typedef std::vector<App> Apps;
71 // Name of a dictionary that holds kiosk app info in Local State.
74 // "auto_login_enabled": true //
76 static const char kKioskDictionaryName[];
77 static const char kKeyApps[];
78 static const char kKeyAutoLoginState[];
80 // Sub directory under DIR_USER_DATA to store cached icon files.
81 static const char kIconCacheDir[];
83 // Sub directory under DIR_USER_DATA to store cached crx files.
84 static const char kCrxCacheDir[];
86 // Gets the KioskAppManager instance, which is lazily created on first call..
87 static KioskAppManager* Get();
89 // Prepares for shutdown and calls CleanUp() if needed.
90 static void Shutdown();
92 // Registers kiosk app entries in local state.
93 static void RegisterPrefs(PrefRegistrySimple* registry);
95 // Initiates reading of consumer kiosk mode auto-launch status.
96 void GetConsumerKioskAutoLaunchStatus(
97 const GetConsumerKioskAutoLaunchStatusCallback& callback);
99 // Enables consumer kiosk mode app auto-launch feature. Upon completion,
100 // |callback| will be invoked with outcome of this operation.
101 void EnableConsumerKioskAutoLaunch(
102 const EnableKioskAutoLaunchCallback& callback);
104 // Returns true if this device is consumer kiosk auto launch enabled.
105 bool IsConsumerKioskDeviceWithAutoLaunch();
107 // Returns auto launcher app id or an empty string if there is none.
108 std::string GetAutoLaunchApp() const;
110 // Sets |app_id| as the app to auto launch at start up.
111 void SetAutoLaunchApp(const std::string& app_id);
113 // Returns true if there is a pending auto-launch request.
114 bool IsAutoLaunchRequested() const;
116 // Returns true if owner/policy enabled auto launch.
117 bool IsAutoLaunchEnabled() const;
119 // Enable auto launch setter.
120 void SetEnableAutoLaunch(bool value);
122 // Adds/removes a kiosk app by id. When removed, all locally cached data
123 // will be removed as well.
124 void AddApp(const std::string& app_id);
125 void RemoveApp(const std::string& app_id);
127 // Gets info of all apps that have no meta data load error.
128 void GetApps(Apps* apps) const;
130 // Gets app data for the given app id. Returns true if |app_id| is known and
131 // |app| is populated. Otherwise, return false.
132 bool GetApp(const std::string& app_id, App* app) const;
134 // Gets the raw icon data for the given app id. Returns NULL if |app_id|
136 const base::RefCountedString* GetAppRawIcon(const std::string& app_id) const;
138 // Gets whether the bailout shortcut is disabled.
139 bool GetDisableBailoutShortcut() const;
141 // Clears locally cached app data.
142 void ClearAppData(const std::string& app_id);
144 // Updates app data from the |app| in |profile|. |app| is provided to cover
145 // the case of app update case where |app| is the new version and is not
146 // finished installing (e.g. because old version is still running). Otherwise,
147 // |app| could be NULL and the current installed app in |profile| will be
149 void UpdateAppDataFromProfile(const std::string& app_id,
151 const extensions::Extension* app);
153 void RetryFailedAppDataFetch();
155 void AddObserver(KioskAppManagerObserver* observer);
156 void RemoveObserver(KioskAppManagerObserver* observer);
159 friend struct base::DefaultLazyInstanceTraits<KioskAppManager>;
160 friend struct base::DefaultDeleter<KioskAppManager>;
161 friend class KioskAppManagerTest;
162 friend class KioskTest;
164 enum AutoLoginState {
166 AUTOLOGIN_REQUESTED = 1,
167 AUTOLOGIN_APPROVED = 2,
168 AUTOLOGIN_REJECTED = 3,
172 virtual ~KioskAppManager();
174 // Stop all data loading and remove its dependency on CrosSettings.
177 // Gets KioskAppData for the given app id.
178 const KioskAppData* GetAppData(const std::string& app_id) const;
179 KioskAppData* GetAppDataMutable(const std::string& app_id);
181 // Update app data |apps_| based on CrosSettings.
182 void UpdateAppData();
184 // KioskAppDataDelegate overrides:
185 virtual void GetKioskAppIconCacheDir(base::FilePath* cache_dir) OVERRIDE;
186 virtual void OnKioskAppDataChanged(const std::string& app_id) OVERRIDE;
187 virtual void OnKioskAppDataLoadFailure(const std::string& app_id) OVERRIDE;
189 // ExternalCache::Delegate:
190 virtual void OnExtensionListsUpdated(
191 const base::DictionaryValue* prefs) OVERRIDE;
192 virtual void OnExtensionLoadedInCache(const std::string& id) OVERRIDE;
193 virtual void OnExtensionDownloadFailed(
194 const std::string& id,
195 extensions::ExtensionDownloaderDelegate::Error error) OVERRIDE;
197 // Callback for EnterpriseInstallAttributes::LockDevice() during
198 // EnableConsumerModeKiosk() call.
200 const EnableKioskAutoLaunchCallback& callback,
201 policy::EnterpriseInstallAttributes::LockResult result);
203 // Callback for EnterpriseInstallAttributes::ReadImmutableAttributes() during
204 // GetConsumerKioskModeStatus() call.
205 void OnReadImmutableAttributes(
206 const GetConsumerKioskAutoLaunchStatusCallback& callback);
208 // Callback for reading handling checks of the owner public.
209 void OnOwnerFileChecked(
210 const GetConsumerKioskAutoLaunchStatusCallback& callback,
211 bool* owner_present);
213 // Reads/writes auto login state from/to local state.
214 AutoLoginState GetAutoLoginState() const;
215 void SetAutoLoginState(AutoLoginState state);
217 void GetCrxCacheDir(base::FilePath* cache_dir);
219 bool GetCachedCrx(const std::string& app_id,
220 base::FilePath* file_path,
221 std::string* version);
223 // True if machine ownership is already established.
224 bool ownership_established_;
225 ScopedVector<KioskAppData> apps_;
226 std::string auto_launch_app_id_;
227 ObserverList<KioskAppManagerObserver, true> observers_;
229 scoped_ptr<CrosSettings::ObserverSubscription>
230 local_accounts_subscription_;
231 scoped_ptr<CrosSettings::ObserverSubscription>
232 local_account_auto_login_id_subscription_;
234 scoped_ptr<ExternalCache> external_cache_;
236 DISALLOW_COPY_AND_ASSIGN(KioskAppManager);
239 } // namespace chromeos
241 #endif // CHROME_BROWSER_CHROMEOS_APP_MODE_KIOSK_APP_MANAGER_H_