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_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 
     23 class PrefRegistrySimple;
     24 class Profile;
     25 
     26 namespace base {
     27 class RefCountedString;
     28 }
     29 
     30 namespace extensions {
     31 class Extension;
     32 }
     33 
     34 namespace chromeos {
     35 
     36 class KioskAppData;
     37 class KioskAppManagerObserver;
     38 
     39 // KioskAppManager manages cached app data.
     40 class 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 RetryFailedAppDataFetch();
    154 
    155   void AddObserver(KioskAppManagerObserver* observer);
    156   void RemoveObserver(KioskAppManagerObserver* observer);
    157 
    158  private:
    159   friend struct base::DefaultLazyInstanceTraits<KioskAppManager>;
    160   friend struct base::DefaultDeleter<KioskAppManager>;
    161   friend class KioskAppManagerTest;
    162   friend class KioskTest;
    163 
    164   enum AutoLoginState {
    165     AUTOLOGIN_NONE      = 0,
    166     AUTOLOGIN_REQUESTED = 1,
    167     AUTOLOGIN_APPROVED  = 2,
    168     AUTOLOGIN_REJECTED  = 3,
    169   };
    170 
    171   KioskAppManager();
    172   virtual ~KioskAppManager();
    173 
    174   // Stop all data loading and remove its dependency on CrosSettings.
    175   void CleanUp();
    176 
    177   // Gets KioskAppData for the given app id.
    178   const KioskAppData* GetAppData(const std::string& app_id) const;
    179   KioskAppData* GetAppDataMutable(const std::string& app_id);
    180 
    181   // Update app data |apps_| based on CrosSettings.
    182   void UpdateAppData();
    183 
    184   // KioskAppDataDelegate overrides:
    185   virtual void GetKioskAppIconCacheDir(base::FilePath* cache_dir) OVERRIDE;
    186   virtual void OnKioskAppDataChanged(const std::string& app_id) OVERRIDE;
    187   virtual void OnKioskAppDataLoadFailure(const std::string& app_id) OVERRIDE;
    188 
    189   // ExternalCache::Delegate:
    190   virtual void OnExtensionListsUpdated(
    191       const base::DictionaryValue* prefs) OVERRIDE;
    192   virtual void OnExtensionLoadedInCache(const std::string& id) OVERRIDE;
    193   virtual void OnExtensionDownloadFailed(
    194       const std::string& id,
    195       extensions::ExtensionDownloaderDelegate::Error error) OVERRIDE;
    196 
    197   // Callback for EnterpriseInstallAttributes::LockDevice() during
    198   // EnableConsumerModeKiosk() call.
    199   void OnLockDevice(
    200       const EnableKioskAutoLaunchCallback& callback,
    201       policy::EnterpriseInstallAttributes::LockResult result);
    202 
    203   // Callback for EnterpriseInstallAttributes::ReadImmutableAttributes() during
    204   // GetConsumerKioskModeStatus() call.
    205   void OnReadImmutableAttributes(
    206       const GetConsumerKioskAutoLaunchStatusCallback& callback);
    207 
    208   // Callback for reading handling checks of the owner public.
    209   void OnOwnerFileChecked(
    210       const GetConsumerKioskAutoLaunchStatusCallback& callback,
    211       bool* owner_present);
    212 
    213   // Reads/writes auto login state from/to local state.
    214   AutoLoginState GetAutoLoginState() const;
    215   void SetAutoLoginState(AutoLoginState state);
    216 
    217   void GetCrxCacheDir(base::FilePath* cache_dir);
    218 
    219   bool GetCachedCrx(const std::string& app_id,
    220                     base::FilePath* file_path,
    221                     std::string* version);
    222 
    223   // True if machine ownership is already established.
    224   bool ownership_established_;
    225   ScopedVector<KioskAppData> apps_;
    226   std::string auto_launch_app_id_;
    227   ObserverList<KioskAppManagerObserver, true> observers_;
    228 
    229   scoped_ptr<CrosSettings::ObserverSubscription>
    230       local_accounts_subscription_;
    231   scoped_ptr<CrosSettings::ObserverSubscription>
    232       local_account_auto_login_id_subscription_;
    233 
    234   scoped_ptr<ExternalCache> external_cache_;
    235 
    236   DISALLOW_COPY_AND_ASSIGN(KioskAppManager);
    237 };
    238 
    239 }  // namespace chromeos
    240 
    241 #endif  // CHROME_BROWSER_CHROMEOS_APP_MODE_KIOSK_APP_MANAGER_H_
    242