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. 4 5 #ifndef CHROME_BROWSER_CHROMEOS_APP_MODE_KIOSK_APP_MANAGER_H_ 6 #define CHROME_BROWSER_CHROMEOS_APP_MODE_KIOSK_APP_MANAGER_H_ 7 8 #include <string> 9 #include <vector> 10 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" 21 22 class PrefRegistrySimple; 23 class Profile; 24 25 namespace base { 26 class RefCountedString; 27 } 28 29 namespace extensions { 30 class Extension; 31 } 32 33 namespace chromeos { 34 35 class KioskAppData; 36 class KioskAppManagerObserver; 37 38 // KioskAppManager manages cached app data. 39 class KioskAppManager : public KioskAppDataDelegate { 40 public: 41 enum ConsumerKioskModeStatus { 42 // Consumer kiosk mode can be enabled on this machine. 43 CONSUMER_KIOSK_MODE_CONFIGURABLE, 44 // Consumer kiosk is enabled on this machine. 45 CONSUMER_KIOSK_MODE_ENABLED, 46 // Consumer kiosk mode is disabled ans cannot any longer be enabled on 47 // this machine. 48 CONSUMER_KIOSK_MODE_DISABLED, 49 }; 50 51 typedef base::Callback<void(bool success)> EnableKioskModeCallback; 52 typedef base::Callback<void(ConsumerKioskModeStatus status)> 53 GetConsumerKioskModeStatusCallback; 54 55 // Struct to hold app info returned from GetApps() call. 56 struct App { 57 explicit App(const KioskAppData& data); 58 App(); 59 ~App(); 60 61 std::string app_id; 62 std::string user_id; 63 std::string name; 64 gfx::ImageSkia icon; 65 bool is_loading; 66 }; 67 typedef std::vector<App> Apps; 68 69 // Name of a dictionary that holds kiosk app info in Local State. 70 // Sample layout: 71 // "kiosk": { 72 // "auto_login_enabled": true // 73 // } 74 static const char kKioskDictionaryName[]; 75 static const char kKeyApps[]; 76 static const char kKeyAutoLoginState[]; 77 78 // Sub directory under DIR_USER_DATA to store cached icon files. 79 static const char kIconCacheDir[]; 80 81 // Gets the KioskAppManager instance, which is lazily created on first call.. 82 static KioskAppManager* Get(); 83 84 // Prepares for shutdown and calls CleanUp() if needed. 85 static void Shutdown(); 86 87 // Registers kiosk app entries in local state. 88 static void RegisterPrefs(PrefRegistrySimple* registry); 89 90 // Initiates reading of consumer kiosk mode status. 91 void GetConsumerKioskModeStatus( 92 const GetConsumerKioskModeStatusCallback& callback); 93 94 // Enables consumer kiosk mode feature. Upon completion, |callback| will be 95 // invoked with outcome of this operation. 96 void EnableConsumerModeKiosk(const EnableKioskModeCallback& callback); 97 98 // Returns auto launcher app id or an empty string if there is none. 99 std::string GetAutoLaunchApp() const; 100 101 // Sets |app_id| as the app to auto launch at start up. 102 void SetAutoLaunchApp(const std::string& app_id); 103 104 // Returns true if there is a pending auto-launch request. 105 bool IsAutoLaunchRequested() const; 106 107 // Returns true if owner/policy enabled auto launch. 108 bool IsAutoLaunchEnabled() const; 109 110 // Enable auto launch setter. 111 void SetEnableAutoLaunch(bool value); 112 113 // Adds/removes a kiosk app by id. When removed, all locally cached data 114 // will be removed as well. 115 void AddApp(const std::string& app_id); 116 void RemoveApp(const std::string& app_id); 117 118 // Gets info of all apps. 119 void GetApps(Apps* apps) const; 120 121 // Gets app data for the given app id. Returns true if |app_id| is known and 122 // |app| is populated. Otherwise, return false. 123 bool GetApp(const std::string& app_id, App* app) const; 124 125 // Gets the raw icon data for the given app id. Returns NULL if |app_id| 126 // is unknown. 127 const base::RefCountedString* GetAppRawIcon(const std::string& app_id) const; 128 129 // Gets whether the bailout shortcut is disabled. 130 bool GetDisableBailoutShortcut() const; 131 132 // Clears locally cached app data. 133 void ClearAppData(const std::string& app_id); 134 135 // Updates app data from the |app| in |profile|. |app| is provided to cover 136 // the case of app update case where |app| is the new version and is not 137 // finished installing (e.g. because old version is still running). Otherwise, 138 // |app| could be NULL and the current installed app in |profile| will be 139 // used. 140 void UpdateAppDataFromProfile(const std::string& app_id, 141 Profile* profile, 142 const extensions::Extension* app); 143 144 void AddObserver(KioskAppManagerObserver* observer); 145 void RemoveObserver(KioskAppManagerObserver* observer); 146 147 private: 148 friend struct base::DefaultLazyInstanceTraits<KioskAppManager>; 149 friend struct base::DefaultDeleter<KioskAppManager>; 150 friend class KioskAppManagerTest; 151 friend class KioskTest; 152 153 enum AutoLoginState { 154 AUTOLOGIN_NONE = 0, 155 AUTOLOGIN_REQUESTED = 1, 156 AUTOLOGIN_APPROVED = 2, 157 AUTOLOGIN_REJECTED = 3, 158 }; 159 160 KioskAppManager(); 161 virtual ~KioskAppManager(); 162 163 // Stop all data loading and remove its dependency on CrosSettings. 164 void CleanUp(); 165 166 // Gets KioskAppData for the given app id. 167 const KioskAppData* GetAppData(const std::string& app_id) const; 168 KioskAppData* GetAppDataMutable(const std::string& app_id); 169 170 // Update app data |apps_| based on CrosSettings. 171 void UpdateAppData(); 172 173 // KioskAppDataDelegate overrides: 174 virtual void GetKioskAppIconCacheDir(base::FilePath* cache_dir) OVERRIDE; 175 virtual void OnKioskAppDataChanged(const std::string& app_id) OVERRIDE; 176 virtual void OnKioskAppDataLoadFailure(const std::string& app_id) OVERRIDE; 177 178 // Callback for EnterpriseInstallAttributes::LockDevice() during 179 // EnableConsumerModeKiosk() call. 180 void OnLockDevice( 181 const EnableKioskModeCallback& callback, 182 policy::EnterpriseInstallAttributes::LockResult result); 183 184 // Callback for EnterpriseInstallAttributes::ReadImmutableAttributes() during 185 // GetConsumerKioskModeStatus() call. 186 void OnReadImmutableAttributes( 187 const GetConsumerKioskModeStatusCallback& callback); 188 189 // Callback for reading handling checks of the owner public. 190 void OnOwnerFileChecked( 191 const GetConsumerKioskModeStatusCallback& callback, 192 bool* owner_present); 193 194 // Reads/writes auto login state from/to local state. 195 AutoLoginState GetAutoLoginState() const; 196 void SetAutoLoginState(AutoLoginState state); 197 198 // True if machine ownership is already established. 199 bool ownership_established_; 200 ScopedVector<KioskAppData> apps_; 201 std::string auto_launch_app_id_; 202 ObserverList<KioskAppManagerObserver, true> observers_; 203 204 scoped_ptr<CrosSettings::ObserverSubscription> 205 local_accounts_subscription_; 206 scoped_ptr<CrosSettings::ObserverSubscription> 207 local_account_auto_login_id_subscription_; 208 209 DISALLOW_COPY_AND_ASSIGN(KioskAppManager); 210 }; 211 212 } // namespace chromeos 213 214 #endif // CHROME_BROWSER_CHROMEOS_APP_MODE_KIOSK_APP_MANAGER_H_ 215