Upstream version 5.34.104.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / chromeos / app_mode / kiosk_app_manager.h
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 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,
49   };
50
51   typedef base::Callback<void(bool success)> EnableKioskAutoLaunchCallback;
52   typedef base::Callback<void(ConsumerKioskAutoLaunchStatus status)>
53       GetConsumerKioskAutoLaunchStatusCallback;
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 auto-launch status.
91   void GetConsumerKioskAutoLaunchStatus(
92       const GetConsumerKioskAutoLaunchStatusCallback& callback);
93
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);
98
99   // Returns auto launcher app id or an empty string if there is none.
100   std::string GetAutoLaunchApp() const;
101
102   // Sets |app_id| as the app to auto launch at start up.
103   void SetAutoLaunchApp(const std::string& app_id);
104
105   // Returns true if there is a pending auto-launch request.
106   bool IsAutoLaunchRequested() const;
107
108   // Returns true if owner/policy enabled auto launch.
109   bool IsAutoLaunchEnabled() const;
110
111   // Enable auto launch setter.
112   void SetEnableAutoLaunch(bool value);
113
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);
118
119   // Gets info of all apps.
120   void GetApps(Apps* apps) const;
121
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;
125
126   // Gets the raw icon data for the given app id. Returns NULL if |app_id|
127   // is unknown.
128   const base::RefCountedString* GetAppRawIcon(const std::string& app_id) const;
129
130   // Gets whether the bailout shortcut is disabled.
131   bool GetDisableBailoutShortcut() const;
132
133   // Clears locally cached app data.
134   void ClearAppData(const std::string& app_id);
135
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
140   // used.
141   void UpdateAppDataFromProfile(const std::string& app_id,
142                                 Profile* profile,
143                                 const extensions::Extension* app);
144
145   void AddObserver(KioskAppManagerObserver* observer);
146   void RemoveObserver(KioskAppManagerObserver* observer);
147
148  private:
149   friend struct base::DefaultLazyInstanceTraits<KioskAppManager>;
150   friend struct base::DefaultDeleter<KioskAppManager>;
151   friend class KioskAppManagerTest;
152   friend class KioskTest;
153
154   enum AutoLoginState {
155     AUTOLOGIN_NONE      = 0,
156     AUTOLOGIN_REQUESTED = 1,
157     AUTOLOGIN_APPROVED  = 2,
158     AUTOLOGIN_REJECTED  = 3,
159   };
160
161   KioskAppManager();
162   virtual ~KioskAppManager();
163
164   // Stop all data loading and remove its dependency on CrosSettings.
165   void CleanUp();
166
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);
170
171   // Update app data |apps_| based on CrosSettings.
172   void UpdateAppData();
173
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;
178
179   // Callback for EnterpriseInstallAttributes::LockDevice() during
180   // EnableConsumerModeKiosk() call.
181   void OnLockDevice(
182       const EnableKioskAutoLaunchCallback& callback,
183       policy::EnterpriseInstallAttributes::LockResult result);
184
185   // Callback for EnterpriseInstallAttributes::ReadImmutableAttributes() during
186   // GetConsumerKioskModeStatus() call.
187   void OnReadImmutableAttributes(
188       const GetConsumerKioskAutoLaunchStatusCallback& callback);
189
190   // Callback for reading handling checks of the owner public.
191   void OnOwnerFileChecked(
192       const GetConsumerKioskAutoLaunchStatusCallback& callback,
193       bool* owner_present);
194
195   // Reads/writes auto login state from/to local state.
196   AutoLoginState GetAutoLoginState() const;
197   void SetAutoLoginState(AutoLoginState state);
198
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_;
204
205   scoped_ptr<CrosSettings::ObserverSubscription>
206       local_accounts_subscription_;
207   scoped_ptr<CrosSettings::ObserverSubscription>
208       local_account_auto_login_id_subscription_;
209
210   DISALLOW_COPY_AND_ASSIGN(KioskAppManager);
211 };
212
213 }  // namespace chromeos
214
215 #endif  // CHROME_BROWSER_CHROMEOS_APP_MODE_KIOSK_APP_MANAGER_H_