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
{
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
{
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.
63 App(const KioskAppData
& data
,
64 bool is_extension_pending
,
65 bool was_auto_launched_with_zero_delay
);
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.
81 // "auto_login_enabled": true //
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
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
161 void UpdateAppDataFromProfile(const std::string
& app_id
,
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 kiosk apps during their
180 // first time launch.
181 extensions::ExternalLoader
* CreateExternalLoader();
183 // Installs kiosk app with |id| from cache.
184 void InstallFromCache(const std::string
& id
);
186 void UpdateExternalCache();
188 // Monitors kiosk external update from usb stick.
189 void MonitorKioskExternalUpdate();
191 // Invoked when kiosk app cache has been updated.
192 void OnKioskAppCacheUpdated(const std::string
& app_id
);
194 // Invoked when kiosk app updating from usb stick has been completed.
195 // |success| indicates if all the updates are completed successfully.
196 void OnKioskAppExternalUpdateComplete(bool success
);
198 // Installs the validated external extension into cache.
199 void PutValidatedExternalExtension(
200 const std::string
& app_id
,
201 const base::FilePath
& crx_path
,
202 const std::string
& version
,
203 const ExternalCache::PutExternalExtensionCallback
& callback
);
205 bool external_loader_created() const { return external_loader_created_
; }
207 // Notifies the KioskAppManager that a given app was auto-launched
208 // automatically with no delay on startup. Certain privacy-sensitive
209 // kiosk-mode behavior (such as network reporting) is only enabled for
210 // kiosk apps that are immediately auto-launched on startup.
211 void SetAppWasAutoLaunchedWithZeroDelay(const std::string
& app_id
);
214 friend struct base::DefaultLazyInstanceTraits
<KioskAppManager
>;
215 friend struct base::DefaultDeleter
<KioskAppManager
>;
216 friend class KioskAppManagerTest
;
217 friend class KioskTest
;
218 friend class KioskUpdateTest
;
220 enum AutoLoginState
{
222 AUTOLOGIN_REQUESTED
= 1,
223 AUTOLOGIN_APPROVED
= 2,
224 AUTOLOGIN_REJECTED
= 3,
228 ~KioskAppManager() override
;
230 // Stop all data loading and remove its dependency on CrosSettings.
233 // Gets KioskAppData for the given app id.
234 const KioskAppData
* GetAppData(const std::string
& app_id
) const;
235 KioskAppData
* GetAppDataMutable(const std::string
& app_id
);
237 // Updates app data |apps_| based on CrosSettings.
238 void UpdateAppData();
240 // KioskAppDataDelegate overrides:
241 void GetKioskAppIconCacheDir(base::FilePath
* cache_dir
) override
;
242 void OnKioskAppDataChanged(const std::string
& app_id
) override
;
243 void OnKioskAppDataLoadFailure(const std::string
& app_id
) override
;
245 // ExternalCache::Delegate:
246 void OnExtensionListsUpdated(const base::DictionaryValue
* prefs
) override
;
247 void OnExtensionLoadedInCache(const std::string
& id
) override
;
248 void OnExtensionDownloadFailed(
249 const std::string
& id
,
250 extensions::ExtensionDownloaderDelegate::Error error
) override
;
252 // Callback for EnterpriseInstallAttributes::LockDevice() during
253 // EnableConsumerModeKiosk() call.
255 const EnableKioskAutoLaunchCallback
& callback
,
256 policy::EnterpriseInstallAttributes::LockResult result
);
258 // Callback for EnterpriseInstallAttributes::ReadImmutableAttributes() during
259 // GetConsumerKioskModeStatus() call.
260 void OnReadImmutableAttributes(
261 const GetConsumerKioskAutoLaunchStatusCallback
& callback
);
263 // Callback for reading handling checks of the owner public.
264 void OnOwnerFileChecked(
265 const GetConsumerKioskAutoLaunchStatusCallback
& callback
,
266 bool* owner_present
);
268 // Reads/writes auto login state from/to local state.
269 AutoLoginState
GetAutoLoginState() const;
270 void SetAutoLoginState(AutoLoginState state
);
272 void GetCrxCacheDir(base::FilePath
* cache_dir
);
273 void GetCrxUnpackDir(base::FilePath
* unpack_dir
);
275 // True if machine ownership is already established.
276 bool ownership_established_
;
277 ScopedVector
<KioskAppData
> apps_
;
278 std::string auto_launch_app_id_
;
279 std::string currently_auto_launched_with_zero_delay_app_
;
280 base::ObserverList
<KioskAppManagerObserver
, true> observers_
;
282 scoped_ptr
<CrosSettings::ObserverSubscription
>
283 local_accounts_subscription_
;
284 scoped_ptr
<CrosSettings::ObserverSubscription
>
285 local_account_auto_login_id_subscription_
;
287 scoped_ptr
<ExternalCache
> external_cache_
;
289 scoped_ptr
<KioskExternalUpdater
> usb_stick_updater_
;
291 // The extension external loader for installing kiosk app.
292 bool external_loader_created_
;
293 base::WeakPtr
<KioskAppExternalLoader
> external_loader_
;
295 DISALLOW_COPY_AND_ASSIGN(KioskAppManager
);
298 } // namespace chromeos
300 #endif // CHROME_BROWSER_CHROMEOS_APP_MODE_KIOSK_APP_MANAGER_H_