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