Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / chrome / browser / chromeos / app_mode / kiosk_app_manager.h
blob90e020a0f83a12aee7c7e6222fd86285982d6ab6
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;
41 class OwnerSettingsServiceChromeOS;
43 // KioskAppManager manages cached app data.
44 class KioskAppManager : public KioskAppDataDelegate,
45 public ExternalCache::Delegate {
46 public:
47 enum ConsumerKioskAutoLaunchStatus {
48 // Consumer kiosk mode auto-launch feature can be enabled on this machine.
49 CONSUMER_KIOSK_AUTO_LAUNCH_CONFIGURABLE,
50 // Consumer kiosk auto-launch feature is enabled on this machine.
51 CONSUMER_KIOSK_AUTO_LAUNCH_ENABLED,
52 // Consumer kiosk mode auto-launch feature is disabled and cannot any longer
53 // be enabled on this machine.
54 CONSUMER_KIOSK_AUTO_LAUNCH_DISABLED,
57 typedef base::Callback<void(bool success)> EnableKioskAutoLaunchCallback;
58 typedef base::Callback<void(ConsumerKioskAutoLaunchStatus status)>
59 GetConsumerKioskAutoLaunchStatusCallback;
61 // Struct to hold app info returned from GetApps() call.
62 struct App {
63 App(const KioskAppData& data,
64 bool is_extension_pending,
65 bool was_auto_launched_with_zero_delay);
66 App();
67 ~App();
69 std::string app_id;
70 std::string user_id;
71 std::string name;
72 gfx::ImageSkia icon;
73 bool is_loading;
74 bool was_auto_launched_with_zero_delay;
76 typedef std::vector<App> Apps;
78 // Name of a dictionary that holds kiosk app info in Local State.
79 // Sample layout:
80 // "kiosk": {
81 // "auto_login_enabled": true //
82 // }
83 static const char kKioskDictionaryName[];
84 static const char kKeyApps[];
85 static const char kKeyAutoLoginState[];
87 // Sub directory under DIR_USER_DATA to store cached icon files.
88 static const char kIconCacheDir[];
90 // Sub directory under DIR_USER_DATA to store cached crx files.
91 static const char kCrxCacheDir[];
93 // Sub directory under DIR_USER_DATA to store unpacked crx file for validating
94 // its signature.
95 static const char kCrxUnpackDir[];
97 // Gets the KioskAppManager instance, which is lazily created on first call..
98 static KioskAppManager* Get();
100 // Prepares for shutdown and calls CleanUp() if needed.
101 static void Shutdown();
103 // Registers kiosk app entries in local state.
104 static void RegisterPrefs(PrefRegistrySimple* registry);
106 // Removes cryptohomes which could not be removed during the previous session.
107 static void RemoveObsoleteCryptohomes();
109 // Initiates reading of consumer kiosk mode auto-launch status.
110 void GetConsumerKioskAutoLaunchStatus(
111 const GetConsumerKioskAutoLaunchStatusCallback& callback);
113 // Enables consumer kiosk mode app auto-launch feature. Upon completion,
114 // |callback| will be invoked with outcome of this operation.
115 void EnableConsumerKioskAutoLaunch(
116 const EnableKioskAutoLaunchCallback& callback);
118 // Returns true if this device is consumer kiosk auto launch enabled.
119 bool IsConsumerKioskDeviceWithAutoLaunch();
121 // Returns auto launcher app id or an empty string if there is none.
122 std::string GetAutoLaunchApp() const;
124 // Sets |app_id| as the app to auto launch at start up.
125 void SetAutoLaunchApp(const std::string& app_id,
126 OwnerSettingsServiceChromeOS* service);
128 // Returns true if there is a pending auto-launch request.
129 bool IsAutoLaunchRequested() const;
131 // Returns true if owner/policy enabled auto launch.
132 bool IsAutoLaunchEnabled() const;
134 // Enable auto launch setter.
135 void SetEnableAutoLaunch(bool value);
137 // Adds/removes a kiosk app by id. When removed, all locally cached data
138 // will be removed as well.
139 void AddApp(const std::string& app_id, OwnerSettingsServiceChromeOS* service);
140 void RemoveApp(const std::string& app_id,
141 OwnerSettingsServiceChromeOS* service);
143 // Gets info of all apps that have no meta data load error.
144 void GetApps(Apps* apps) const;
146 // Gets app data for the given app id. Returns true if |app_id| is known and
147 // |app| is populated. Otherwise, return false.
148 bool GetApp(const std::string& app_id, App* app) const;
150 // Gets whether the bailout shortcut is disabled.
151 bool GetDisableBailoutShortcut() const;
153 // Clears locally cached app data.
154 void ClearAppData(const std::string& app_id);
156 // Updates app data from the |app| in |profile|. |app| is provided to cover
157 // the case of app update case where |app| is the new version and is not
158 // finished installing (e.g. because old version is still running). Otherwise,
159 // |app| could be NULL and the current installed app in |profile| will be
160 // used.
161 void UpdateAppDataFromProfile(const std::string& app_id,
162 Profile* profile,
163 const extensions::Extension* app);
165 void RetryFailedAppDataFetch();
167 // Returns true if the app is found in cache.
168 bool HasCachedCrx(const std::string& app_id) const;
170 // Gets the path and version of the cached crx with |app_id|.
171 // Returns true if the app is found in cache.
172 bool GetCachedCrx(const std::string& app_id,
173 base::FilePath* file_path,
174 std::string* version) const;
176 void AddObserver(KioskAppManagerObserver* observer);
177 void RemoveObserver(KioskAppManagerObserver* observer);
179 // Creates extensions::ExternalLoader for installing the primary kiosk app
180 // during its first time launch.
181 extensions::ExternalLoader* CreateExternalLoader();
183 // Creates extensions::ExternalLoader for installing secondary kiosk apps
184 // before launching the primary app for the first time.
185 extensions::ExternalLoader* CreateSecondaryAppExternalLoader();
187 // Installs kiosk app with |id| from cache.
188 void InstallFromCache(const std::string& id);
190 // Installs the secondary apps listed in |ids|.
191 void InstallSecondaryApps(const std::vector<std::string>& ids);
193 void UpdateExternalCache();
195 // Monitors kiosk external update from usb stick.
196 void MonitorKioskExternalUpdate();
198 // Invoked when kiosk app cache has been updated.
199 void OnKioskAppCacheUpdated(const std::string& app_id);
201 // Invoked when kiosk app updating from usb stick has been completed.
202 // |success| indicates if all the updates are completed successfully.
203 void OnKioskAppExternalUpdateComplete(bool success);
205 // Installs the validated external extension into cache.
206 void PutValidatedExternalExtension(
207 const std::string& app_id,
208 const base::FilePath& crx_path,
209 const std::string& version,
210 const ExternalCache::PutExternalExtensionCallback& callback);
212 bool external_loader_created() const { return external_loader_created_; }
213 bool secondary_app_external_loader_created() const {
214 return secondary_app_external_loader_created_;
217 // Notifies the KioskAppManager that a given app was auto-launched
218 // automatically with no delay on startup. Certain privacy-sensitive
219 // kiosk-mode behavior (such as network reporting) is only enabled for
220 // kiosk apps that are immediately auto-launched on startup.
221 void SetAppWasAutoLaunchedWithZeroDelay(const std::string& app_id);
223 private:
224 friend struct base::DefaultLazyInstanceTraits<KioskAppManager>;
225 friend struct base::DefaultDeleter<KioskAppManager>;
226 friend class KioskAppManagerTest;
227 friend class KioskTest;
228 friend class KioskUpdateTest;
230 enum AutoLoginState {
231 AUTOLOGIN_NONE = 0,
232 AUTOLOGIN_REQUESTED = 1,
233 AUTOLOGIN_APPROVED = 2,
234 AUTOLOGIN_REJECTED = 3,
237 KioskAppManager();
238 ~KioskAppManager() override;
240 // Stop all data loading and remove its dependency on CrosSettings.
241 void CleanUp();
243 // Gets KioskAppData for the given app id.
244 const KioskAppData* GetAppData(const std::string& app_id) const;
245 KioskAppData* GetAppDataMutable(const std::string& app_id);
247 // Updates app data |apps_| based on CrosSettings.
248 void UpdateAppData();
250 // KioskAppDataDelegate overrides:
251 void GetKioskAppIconCacheDir(base::FilePath* cache_dir) override;
252 void OnKioskAppDataChanged(const std::string& app_id) override;
253 void OnKioskAppDataLoadFailure(const std::string& app_id) override;
255 // ExternalCache::Delegate:
256 void OnExtensionListsUpdated(const base::DictionaryValue* prefs) override;
257 void OnExtensionLoadedInCache(const std::string& id) override;
258 void OnExtensionDownloadFailed(
259 const std::string& id,
260 extensions::ExtensionDownloaderDelegate::Error error) override;
262 // Callback for EnterpriseInstallAttributes::LockDevice() during
263 // EnableConsumerModeKiosk() call.
264 void OnLockDevice(
265 const EnableKioskAutoLaunchCallback& callback,
266 policy::EnterpriseInstallAttributes::LockResult result);
268 // Callback for EnterpriseInstallAttributes::ReadImmutableAttributes() during
269 // GetConsumerKioskModeStatus() call.
270 void OnReadImmutableAttributes(
271 const GetConsumerKioskAutoLaunchStatusCallback& callback);
273 // Callback for reading handling checks of the owner public.
274 void OnOwnerFileChecked(
275 const GetConsumerKioskAutoLaunchStatusCallback& callback,
276 bool* owner_present);
278 // Reads/writes auto login state from/to local state.
279 AutoLoginState GetAutoLoginState() const;
280 void SetAutoLoginState(AutoLoginState state);
282 void GetCrxCacheDir(base::FilePath* cache_dir);
283 void GetCrxUnpackDir(base::FilePath* unpack_dir);
285 // True if machine ownership is already established.
286 bool ownership_established_;
287 ScopedVector<KioskAppData> apps_;
288 std::string auto_launch_app_id_;
289 std::string currently_auto_launched_with_zero_delay_app_;
290 base::ObserverList<KioskAppManagerObserver, true> observers_;
292 scoped_ptr<CrosSettings::ObserverSubscription>
293 local_accounts_subscription_;
294 scoped_ptr<CrosSettings::ObserverSubscription>
295 local_account_auto_login_id_subscription_;
297 scoped_ptr<ExternalCache> external_cache_;
299 scoped_ptr<KioskExternalUpdater> usb_stick_updater_;
301 // The extension external loader for deploying primary app.
302 bool external_loader_created_;
303 base::WeakPtr<KioskAppExternalLoader> external_loader_;
305 // The extension external loader for deploying secondary apps.
306 bool secondary_app_external_loader_created_;
307 base::WeakPtr<KioskAppExternalLoader> secondary_app_external_loader_;
309 DISALLOW_COPY_AND_ASSIGN(KioskAppManager);
312 } // namespace chromeos
314 #endif // CHROME_BROWSER_CHROMEOS_APP_MODE_KIOSK_APP_MANAGER_H_