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/policy/enterprise_install_attributes.h"
19 #include "chrome/browser/chromeos/settings/cros_settings.h"
20 #include "ui/gfx/image/image_skia.h"
22 class PrefRegistrySimple;
26 class RefCountedString;
29 namespace extensions {
36 class KioskAppManagerObserver;
38 // KioskAppManager manages cached app data.
39 class KioskAppManager : public KioskAppDataDelegate {
41 enum ConsumerKioskAutoLaunchStatus {
42 // Consumer kiosk mode auto-launch feature can be enabled on this machine.
43 CONSUMER_KIOSK_AUTO_LAUNCH_CONFIGURABLE,
44 // Consumer kiosk auto-launch feature is enabled on this machine.
45 CONSUMER_KIOSK_AUTO_LAUNCH_ENABLED,
46 // Consumer kiosk mode auto-launch feature is disabled and cannot any longer
47 // be enabled on this machine.
48 CONSUMER_KIOSK_AUTO_LAUNCH_DISABLED,
51 typedef base::Callback<void(bool success)> EnableKioskAutoLaunchCallback;
52 typedef base::Callback<void(ConsumerKioskAutoLaunchStatus status)>
53 GetConsumerKioskAutoLaunchStatusCallback;
55 // Struct to hold app info returned from GetApps() call.
57 explicit App(const KioskAppData& data);
67 typedef std::vector<App> Apps;
69 // Name of a dictionary that holds kiosk app info in Local State.
72 // "auto_login_enabled": true //
74 static const char kKioskDictionaryName[];
75 static const char kKeyApps[];
76 static const char kKeyAutoLoginState[];
78 // Sub directory under DIR_USER_DATA to store cached icon files.
79 static const char kIconCacheDir[];
81 // Gets the KioskAppManager instance, which is lazily created on first call..
82 static KioskAppManager* Get();
84 // Prepares for shutdown and calls CleanUp() if needed.
85 static void Shutdown();
87 // Registers kiosk app entries in local state.
88 static void RegisterPrefs(PrefRegistrySimple* registry);
90 // Initiates reading of consumer kiosk mode auto-launch status.
91 void GetConsumerKioskAutoLaunchStatus(
92 const GetConsumerKioskAutoLaunchStatusCallback& callback);
94 // Enables consumer kiosk mode app auto-launch feature. Upon completion,
95 // |callback| will be invoked with outcome of this operation.
96 void EnableConsumerKioskAutoLaunch(
97 const EnableKioskAutoLaunchCallback& callback);
99 // Returns auto launcher app id or an empty string if there is none.
100 std::string GetAutoLaunchApp() const;
102 // Sets |app_id| as the app to auto launch at start up.
103 void SetAutoLaunchApp(const std::string& app_id);
105 // Returns true if there is a pending auto-launch request.
106 bool IsAutoLaunchRequested() const;
108 // Returns true if owner/policy enabled auto launch.
109 bool IsAutoLaunchEnabled() const;
111 // Enable auto launch setter.
112 void SetEnableAutoLaunch(bool value);
114 // Adds/removes a kiosk app by id. When removed, all locally cached data
115 // will be removed as well.
116 void AddApp(const std::string& app_id);
117 void RemoveApp(const std::string& app_id);
119 // Gets info of all apps.
120 void GetApps(Apps* apps) const;
122 // Gets app data for the given app id. Returns true if |app_id| is known and
123 // |app| is populated. Otherwise, return false.
124 bool GetApp(const std::string& app_id, App* app) const;
126 // Gets the raw icon data for the given app id. Returns NULL if |app_id|
128 const base::RefCountedString* GetAppRawIcon(const std::string& app_id) const;
130 // Gets whether the bailout shortcut is disabled.
131 bool GetDisableBailoutShortcut() const;
133 // Clears locally cached app data.
134 void ClearAppData(const std::string& app_id);
136 // Updates app data from the |app| in |profile|. |app| is provided to cover
137 // the case of app update case where |app| is the new version and is not
138 // finished installing (e.g. because old version is still running). Otherwise,
139 // |app| could be NULL and the current installed app in |profile| will be
141 void UpdateAppDataFromProfile(const std::string& app_id,
143 const extensions::Extension* app);
145 void AddObserver(KioskAppManagerObserver* observer);
146 void RemoveObserver(KioskAppManagerObserver* observer);
149 friend struct base::DefaultLazyInstanceTraits<KioskAppManager>;
150 friend struct base::DefaultDeleter<KioskAppManager>;
151 friend class KioskAppManagerTest;
152 friend class KioskTest;
154 enum AutoLoginState {
156 AUTOLOGIN_REQUESTED = 1,
157 AUTOLOGIN_APPROVED = 2,
158 AUTOLOGIN_REJECTED = 3,
162 virtual ~KioskAppManager();
164 // Stop all data loading and remove its dependency on CrosSettings.
167 // Gets KioskAppData for the given app id.
168 const KioskAppData* GetAppData(const std::string& app_id) const;
169 KioskAppData* GetAppDataMutable(const std::string& app_id);
171 // Update app data |apps_| based on CrosSettings.
172 void UpdateAppData();
174 // KioskAppDataDelegate overrides:
175 virtual void GetKioskAppIconCacheDir(base::FilePath* cache_dir) OVERRIDE;
176 virtual void OnKioskAppDataChanged(const std::string& app_id) OVERRIDE;
177 virtual void OnKioskAppDataLoadFailure(const std::string& app_id) OVERRIDE;
179 // Callback for EnterpriseInstallAttributes::LockDevice() during
180 // EnableConsumerModeKiosk() call.
182 const EnableKioskAutoLaunchCallback& callback,
183 policy::EnterpriseInstallAttributes::LockResult result);
185 // Callback for EnterpriseInstallAttributes::ReadImmutableAttributes() during
186 // GetConsumerKioskModeStatus() call.
187 void OnReadImmutableAttributes(
188 const GetConsumerKioskAutoLaunchStatusCallback& callback);
190 // Callback for reading handling checks of the owner public.
191 void OnOwnerFileChecked(
192 const GetConsumerKioskAutoLaunchStatusCallback& callback,
193 bool* owner_present);
195 // Reads/writes auto login state from/to local state.
196 AutoLoginState GetAutoLoginState() const;
197 void SetAutoLoginState(AutoLoginState state);
199 // True if machine ownership is already established.
200 bool ownership_established_;
201 ScopedVector<KioskAppData> apps_;
202 std::string auto_launch_app_id_;
203 ObserverList<KioskAppManagerObserver, true> observers_;
205 scoped_ptr<CrosSettings::ObserverSubscription>
206 local_accounts_subscription_;
207 scoped_ptr<CrosSettings::ObserverSubscription>
208 local_account_auto_login_id_subscription_;
210 DISALLOW_COPY_AND_ASSIGN(KioskAppManager);
213 } // namespace chromeos
215 #endif // CHROME_BROWSER_CHROMEOS_APP_MODE_KIOSK_APP_MANAGER_H_