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