ozone: evdev: Sync caps lock LED state to evdev
[chromium-blink-merge.git] / chrome / browser / chromeos / app_mode / kiosk_app_manager.h
blob4d4944dab67eb9d6805bd4f8a31400bf9ce53990
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_
8 #include <string>
9 #include <vector>
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;
24 class Profile;
26 namespace base {
27 class RefCountedString;
30 namespace extensions {
31 class Extension;
32 class ExternalLoader;
35 namespace chromeos {
37 class KioskAppData;
38 class KioskAppExternalLoader;
39 class KioskAppManagerObserver;
40 class KioskExternalUpdater;
42 // KioskAppManager manages cached app data.
43 class KioskAppManager : public KioskAppDataDelegate,
44 public ExternalCache::Delegate {
45 public:
46 enum ConsumerKioskAutoLaunchStatus {
47 // Consumer kiosk mode auto-launch feature can be enabled on this machine.
48 CONSUMER_KIOSK_AUTO_LAUNCH_CONFIGURABLE,
49 // Consumer kiosk auto-launch feature is enabled on this machine.
50 CONSUMER_KIOSK_AUTO_LAUNCH_ENABLED,
51 // Consumer kiosk mode auto-launch feature is disabled and cannot any longer
52 // be enabled on this machine.
53 CONSUMER_KIOSK_AUTO_LAUNCH_DISABLED,
56 typedef base::Callback<void(bool success)> EnableKioskAutoLaunchCallback;
57 typedef base::Callback<void(ConsumerKioskAutoLaunchStatus status)>
58 GetConsumerKioskAutoLaunchStatusCallback;
60 // Struct to hold app info returned from GetApps() call.
61 struct App {
62 App(const KioskAppData& data,
63 bool is_extension_pending,
64 bool was_auto_launched_with_zero_delay);
65 App();
66 ~App();
68 std::string app_id;
69 std::string user_id;
70 std::string name;
71 gfx::ImageSkia icon;
72 bool is_loading;
73 bool was_auto_launched_with_zero_delay;
75 typedef std::vector<App> Apps;
77 // Name of a dictionary that holds kiosk app info in Local State.
78 // Sample layout:
79 // "kiosk": {
80 // "auto_login_enabled": true //
81 // }
82 static const char kKioskDictionaryName[];
83 static const char kKeyApps[];
84 static const char kKeyAutoLoginState[];
86 // Sub directory under DIR_USER_DATA to store cached icon files.
87 static const char kIconCacheDir[];
89 // Sub directory under DIR_USER_DATA to store cached crx files.
90 static const char kCrxCacheDir[];
92 // Sub directory under DIR_USER_DATA to store unpacked crx file for validating
93 // its signature.
94 static const char kCrxUnpackDir[];
96 // Gets the KioskAppManager instance, which is lazily created on first call..
97 static KioskAppManager* Get();
99 // Prepares for shutdown and calls CleanUp() if needed.
100 static void Shutdown();
102 // Registers kiosk app entries in local state.
103 static void RegisterPrefs(PrefRegistrySimple* registry);
105 // Initiates reading of consumer kiosk mode auto-launch status.
106 void GetConsumerKioskAutoLaunchStatus(
107 const GetConsumerKioskAutoLaunchStatusCallback& callback);
109 // Enables consumer kiosk mode app auto-launch feature. Upon completion,
110 // |callback| will be invoked with outcome of this operation.
111 void EnableConsumerKioskAutoLaunch(
112 const EnableKioskAutoLaunchCallback& callback);
114 // Returns true if this device is consumer kiosk auto launch enabled.
115 bool IsConsumerKioskDeviceWithAutoLaunch();
117 // Returns auto launcher app id or an empty string if there is none.
118 std::string GetAutoLaunchApp() const;
120 // Sets |app_id| as the app to auto launch at start up.
121 void SetAutoLaunchApp(const std::string& app_id);
123 // Returns true if there is a pending auto-launch request.
124 bool IsAutoLaunchRequested() const;
126 // Returns true if owner/policy enabled auto launch.
127 bool IsAutoLaunchEnabled() const;
129 // Returns true if current app was auto launched with zero delay.
130 bool IsCurrentAppAutoLaunchedWithZeroDelay() const;
132 // Enable auto launch setter.
133 void SetEnableAutoLaunch(bool value);
135 // Adds/removes a kiosk app by id. When removed, all locally cached data
136 // will be removed as well.
137 void AddApp(const std::string& app_id);
138 void RemoveApp(const std::string& app_id);
140 // Gets info of all apps that have no meta data load error.
141 void GetApps(Apps* apps) const;
143 // Gets app data for the given app id. Returns true if |app_id| is known and
144 // |app| is populated. Otherwise, return false.
145 bool GetApp(const std::string& app_id, App* app) const;
147 // Gets whether the bailout shortcut is disabled.
148 bool GetDisableBailoutShortcut() const;
150 // Clears locally cached app data.
151 void ClearAppData(const std::string& app_id);
153 // Updates app data from the |app| in |profile|. |app| is provided to cover
154 // the case of app update case where |app| is the new version and is not
155 // finished installing (e.g. because old version is still running). Otherwise,
156 // |app| could be NULL and the current installed app in |profile| will be
157 // used.
158 void UpdateAppDataFromProfile(const std::string& app_id,
159 Profile* profile,
160 const extensions::Extension* app);
162 void RetryFailedAppDataFetch();
164 // Returns true if the app is found in cache.
165 bool HasCachedCrx(const std::string& app_id) const;
167 // Gets the path and version of the cached crx with |app_id|.
168 // Returns true if the app is found in cache.
169 bool GetCachedCrx(const std::string& app_id,
170 base::FilePath* file_path,
171 std::string* version) const;
173 void AddObserver(KioskAppManagerObserver* observer);
174 void RemoveObserver(KioskAppManagerObserver* observer);
176 // Creates extensions::ExternalLoader for installing kiosk apps during their
177 // first time launch.
178 extensions::ExternalLoader* CreateExternalLoader();
180 // Installs kiosk app with |id| from cache.
181 void InstallFromCache(const std::string& id);
183 void UpdateExternalCache();
185 // Monitors kiosk external update from usb stick.
186 void MonitorKioskExternalUpdate();
188 // Invoked when kiosk app cache has been updated.
189 void OnKioskAppCacheUpdated(const std::string& app_id);
191 // Invoked when kiosk app updating from usb stick has been completed.
192 // |success| indicates if all the updates are completed successfully.
193 void OnKioskAppExternalUpdateComplete(bool success);
195 // Installs the validated external extension into cache.
196 void PutValidatedExternalExtension(
197 const std::string& app_id,
198 const base::FilePath& crx_path,
199 const std::string& version,
200 const ExternalCache::PutExternalExtensionCallback& callback);
202 bool external_loader_created() const { return external_loader_created_; }
204 // Notifies the KioskAppManager that a given app was auto-launched
205 // automatically with no delay on startup. Certain privacy-sensitive
206 // kiosk-mode behavior (such as network reporting) is only enabled for
207 // kiosk apps that are immediately auto-launched on startup.
208 void SetAppWasAutoLaunchedWithZeroDelay(const std::string& app_id);
210 private:
211 friend struct base::DefaultLazyInstanceTraits<KioskAppManager>;
212 friend struct base::DefaultDeleter<KioskAppManager>;
213 friend class KioskAppManagerTest;
214 friend class KioskTest;
215 friend class KioskUpdateTest;
217 enum AutoLoginState {
218 AUTOLOGIN_NONE = 0,
219 AUTOLOGIN_REQUESTED = 1,
220 AUTOLOGIN_APPROVED = 2,
221 AUTOLOGIN_REJECTED = 3,
224 KioskAppManager();
225 ~KioskAppManager() override;
227 // Stop all data loading and remove its dependency on CrosSettings.
228 void CleanUp();
230 // Gets KioskAppData for the given app id.
231 const KioskAppData* GetAppData(const std::string& app_id) const;
232 KioskAppData* GetAppDataMutable(const std::string& app_id);
234 // Updates app data |apps_| based on CrosSettings.
235 void UpdateAppData();
237 // KioskAppDataDelegate overrides:
238 void GetKioskAppIconCacheDir(base::FilePath* cache_dir) override;
239 void OnKioskAppDataChanged(const std::string& app_id) override;
240 void OnKioskAppDataLoadFailure(const std::string& app_id) override;
242 // ExternalCache::Delegate:
243 void OnExtensionListsUpdated(const base::DictionaryValue* prefs) override;
244 void OnExtensionLoadedInCache(const std::string& id) override;
245 void OnExtensionDownloadFailed(
246 const std::string& id,
247 extensions::ExtensionDownloaderDelegate::Error error) override;
249 // Callback for EnterpriseInstallAttributes::LockDevice() during
250 // EnableConsumerModeKiosk() call.
251 void OnLockDevice(
252 const EnableKioskAutoLaunchCallback& callback,
253 policy::EnterpriseInstallAttributes::LockResult result);
255 // Callback for EnterpriseInstallAttributes::ReadImmutableAttributes() during
256 // GetConsumerKioskModeStatus() call.
257 void OnReadImmutableAttributes(
258 const GetConsumerKioskAutoLaunchStatusCallback& callback);
260 // Callback for reading handling checks of the owner public.
261 void OnOwnerFileChecked(
262 const GetConsumerKioskAutoLaunchStatusCallback& callback,
263 bool* owner_present);
265 // Reads/writes auto login state from/to local state.
266 AutoLoginState GetAutoLoginState() const;
267 void SetAutoLoginState(AutoLoginState state);
269 void GetCrxCacheDir(base::FilePath* cache_dir);
270 void GetCrxUnpackDir(base::FilePath* unpack_dir);
272 // True if machine ownership is already established.
273 bool ownership_established_;
274 ScopedVector<KioskAppData> apps_;
275 std::string auto_launch_app_id_;
276 std::string currently_auto_launched_with_zero_delay_app_;
277 ObserverList<KioskAppManagerObserver, true> observers_;
279 scoped_ptr<CrosSettings::ObserverSubscription>
280 local_accounts_subscription_;
281 scoped_ptr<CrosSettings::ObserverSubscription>
282 local_account_auto_login_id_subscription_;
284 scoped_ptr<ExternalCache> external_cache_;
286 scoped_ptr<KioskExternalUpdater> usb_stick_updater_;
288 // The extension external loader for installing kiosk app.
289 bool external_loader_created_;
290 base::WeakPtr<KioskAppExternalLoader> external_loader_;
292 DISALLOW_COPY_AND_ASSIGN(KioskAppManager);
295 } // namespace chromeos
297 #endif // CHROME_BROWSER_CHROMEOS_APP_MODE_KIOSK_APP_MANAGER_H_