[Android] Allow multiple --install in bb_device_steps.py.
[chromium-blink-merge.git] / chrome / browser / chromeos / app_mode / kiosk_app_manager.h
bloba1006b822d11784dca416b106a2994b3ff0d46f3
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;
41 #if !defined(USE_ATHENA)
42 class KioskExternalUpdater;
43 #endif
45 // KioskAppManager manages cached app data.
46 class KioskAppManager : public KioskAppDataDelegate,
47 public ExternalCache::Delegate {
48 public:
49 enum ConsumerKioskAutoLaunchStatus {
50 // Consumer kiosk mode auto-launch feature can be enabled on this machine.
51 CONSUMER_KIOSK_AUTO_LAUNCH_CONFIGURABLE,
52 // Consumer kiosk auto-launch feature is enabled on this machine.
53 CONSUMER_KIOSK_AUTO_LAUNCH_ENABLED,
54 // Consumer kiosk mode auto-launch feature is disabled and cannot any longer
55 // be enabled on this machine.
56 CONSUMER_KIOSK_AUTO_LAUNCH_DISABLED,
59 typedef base::Callback<void(bool success)> EnableKioskAutoLaunchCallback;
60 typedef base::Callback<void(ConsumerKioskAutoLaunchStatus status)>
61 GetConsumerKioskAutoLaunchStatusCallback;
63 // Struct to hold app info returned from GetApps() call.
64 struct App {
65 App(const KioskAppData& data, bool is_extension_pending);
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;
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 // Enable auto launch setter.
130 void SetEnableAutoLaunch(bool value);
132 // Adds/removes a kiosk app by id. When removed, all locally cached data
133 // will be removed as well.
134 void AddApp(const std::string& app_id);
135 void RemoveApp(const std::string& app_id);
137 // Gets info of all apps that have no meta data load error.
138 void GetApps(Apps* apps) const;
140 // Gets app data for the given app id. Returns true if |app_id| is known and
141 // |app| is populated. Otherwise, return false.
142 bool GetApp(const std::string& app_id, App* app) const;
144 // Gets the raw icon data for the given app id. Returns NULL if |app_id|
145 // is unknown.
146 const base::RefCountedString* GetAppRawIcon(const std::string& app_id) const;
148 // Gets whether the bailout shortcut is disabled.
149 bool GetDisableBailoutShortcut() const;
151 // Clears locally cached app data.
152 void ClearAppData(const std::string& app_id);
154 // Updates app data from the |app| in |profile|. |app| is provided to cover
155 // the case of app update case where |app| is the new version and is not
156 // finished installing (e.g. because old version is still running). Otherwise,
157 // |app| could be NULL and the current installed app in |profile| will be
158 // used.
159 void UpdateAppDataFromProfile(const std::string& app_id,
160 Profile* profile,
161 const extensions::Extension* app);
163 void RetryFailedAppDataFetch();
165 // Returns true if the app is found in cache.
166 bool HasCachedCrx(const std::string& app_id) const;
168 // Gets the path and version of the cached crx with |app_id|.
169 // Returns true if the app is found in cache.
170 bool GetCachedCrx(const std::string& app_id,
171 base::FilePath* file_path,
172 std::string* version) const;
174 void AddObserver(KioskAppManagerObserver* observer);
175 void RemoveObserver(KioskAppManagerObserver* observer);
177 // Creates extensions::ExternalLoader for installing kiosk apps during their
178 // first time launch.
179 extensions::ExternalLoader* CreateExternalLoader();
181 // Installs kiosk app with |id| from cache.
182 void InstallFromCache(const std::string& id);
184 void UpdateExternalCache();
186 // Monitors kiosk external update from usb stick.
187 void MonitorKioskExternalUpdate();
189 // Invoked when kiosk app cache has been updated.
190 void OnKioskAppCacheUpdated(const std::string& app_id);
192 // Invoked when kiosk app updating from usb stick has been completed.
193 // |success| indicates if all the updates are completed successfully.
194 void OnKioskAppExternalUpdateComplete(bool success);
196 // Installs the validated external extension into cache.
197 void PutValidatedExternalExtension(
198 const std::string& app_id,
199 const base::FilePath& crx_path,
200 const std::string& version,
201 const ExternalCache::PutExternalExtensionCallback& callback);
203 bool external_loader_created() const { return external_loader_created_; }
205 private:
206 friend struct base::DefaultLazyInstanceTraits<KioskAppManager>;
207 friend struct base::DefaultDeleter<KioskAppManager>;
208 friend class KioskAppManagerTest;
209 friend class KioskTest;
210 friend class KioskUpdateTest;
212 enum AutoLoginState {
213 AUTOLOGIN_NONE = 0,
214 AUTOLOGIN_REQUESTED = 1,
215 AUTOLOGIN_APPROVED = 2,
216 AUTOLOGIN_REJECTED = 3,
219 KioskAppManager();
220 virtual ~KioskAppManager();
222 // Stop all data loading and remove its dependency on CrosSettings.
223 void CleanUp();
225 // Gets KioskAppData for the given app id.
226 const KioskAppData* GetAppData(const std::string& app_id) const;
227 KioskAppData* GetAppDataMutable(const std::string& app_id);
229 // Updates app data |apps_| based on CrosSettings.
230 void UpdateAppData();
232 // KioskAppDataDelegate overrides:
233 virtual void GetKioskAppIconCacheDir(base::FilePath* cache_dir) override;
234 virtual void OnKioskAppDataChanged(const std::string& app_id) override;
235 virtual void OnKioskAppDataLoadFailure(const std::string& app_id) override;
237 // ExternalCache::Delegate:
238 virtual void OnExtensionListsUpdated(
239 const base::DictionaryValue* prefs) override;
240 virtual void OnExtensionLoadedInCache(const std::string& id) override;
241 virtual void OnExtensionDownloadFailed(
242 const std::string& id,
243 extensions::ExtensionDownloaderDelegate::Error error) override;
245 // Callback for EnterpriseInstallAttributes::LockDevice() during
246 // EnableConsumerModeKiosk() call.
247 void OnLockDevice(
248 const EnableKioskAutoLaunchCallback& callback,
249 policy::EnterpriseInstallAttributes::LockResult result);
251 // Callback for EnterpriseInstallAttributes::ReadImmutableAttributes() during
252 // GetConsumerKioskModeStatus() call.
253 void OnReadImmutableAttributes(
254 const GetConsumerKioskAutoLaunchStatusCallback& callback);
256 // Callback for reading handling checks of the owner public.
257 void OnOwnerFileChecked(
258 const GetConsumerKioskAutoLaunchStatusCallback& callback,
259 bool* owner_present);
261 // Reads/writes auto login state from/to local state.
262 AutoLoginState GetAutoLoginState() const;
263 void SetAutoLoginState(AutoLoginState state);
265 void GetCrxCacheDir(base::FilePath* cache_dir);
266 void GetCrxUnpackDir(base::FilePath* unpack_dir);
268 // True if machine ownership is already established.
269 bool ownership_established_;
270 ScopedVector<KioskAppData> apps_;
271 std::string auto_launch_app_id_;
272 ObserverList<KioskAppManagerObserver, true> observers_;
274 scoped_ptr<CrosSettings::ObserverSubscription>
275 local_accounts_subscription_;
276 scoped_ptr<CrosSettings::ObserverSubscription>
277 local_account_auto_login_id_subscription_;
279 scoped_ptr<ExternalCache> external_cache_;
281 #if !defined(USE_ATHENA)
282 scoped_ptr<KioskExternalUpdater> usb_stick_updater_;
283 #endif
285 // The extension external loader for installing kiosk app.
286 bool external_loader_created_;
287 base::WeakPtr<KioskAppExternalLoader> external_loader_;
289 DISALLOW_COPY_AND_ASSIGN(KioskAppManager);
292 } // namespace chromeos
294 #endif // CHROME_BROWSER_CHROMEOS_APP_MODE_KIOSK_APP_MANAGER_H_