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_STARTUP_APP_LAUNCHER_H_
      6 #define CHROME_BROWSER_CHROMEOS_APP_MODE_STARTUP_APP_LAUNCHER_H_
      7 
      8 #include <string>
      9 
     10 #include "base/basictypes.h"
     11 #include "base/memory/ref_counted.h"
     12 #include "base/memory/weak_ptr.h"
     13 #include "chrome/browser/chromeos/app_mode/kiosk_app_launch_error.h"
     14 #include "chrome/browser/chromeos/app_mode/kiosk_app_manager_observer.h"
     15 #include "chrome/browser/extensions/install_observer.h"
     16 #include "google_apis/gaia/oauth2_token_service.h"
     17 
     18 class Profile;
     19 
     20 namespace chromeos {
     21 
     22 // Launches the app at startup. The flow roughly looks like this:
     23 // First call Initialize():
     24 // - Attempts to load oauth token file. Stores the loaded tokens in
     25 //   |auth_params_|.
     26 // - Initialize token service and inject |auth_params_| if needed.
     27 // - Initialize network if app is not installed or not offline_enabled.
     28 // - If network is online, install or update the app as needed.
     29 // - After the app is installed/updated, launch it and finish the flow;
     30 // Report OnLauncherInitialized() or OnLaunchFailed() to observers:
     31 // - If all goes good, launches the app and finish the flow;
     32 class StartupAppLauncher : public base::SupportsWeakPtr<StartupAppLauncher>,
     33                            public OAuth2TokenService::Observer,
     34                            public extensions::InstallObserver,
     35                            public KioskAppManagerObserver {
     36  public:
     37   class Delegate {
     38    public:
     39     // Invoked to perform actual network initialization work. Note the app
     40     // launch flow is paused until ContinueWithNetworkReady is called.
     41     virtual void InitializeNetwork() = 0;
     42 
     43     // Returns true if Internet is online.
     44     virtual bool IsNetworkReady() = 0;
     45 
     46     virtual void OnLoadingOAuthFile() = 0;
     47     virtual void OnInitializingTokenService() = 0;
     48     virtual void OnInstallingApp() = 0;
     49     virtual void OnReadyToLaunch() = 0;
     50     virtual void OnLaunchSucceeded() = 0;
     51     virtual void OnLaunchFailed(KioskAppLaunchError::Error error) = 0;
     52     virtual bool IsShowingNetworkConfigScreen() = 0;
     53 
     54    protected:
     55     virtual ~Delegate() {}
     56   };
     57 
     58   StartupAppLauncher(Profile* profile,
     59                      const std::string& app_id,
     60                      bool diagnostic_mode,
     61                      Delegate* delegate);
     62 
     63   virtual ~StartupAppLauncher();
     64 
     65   // Prepares the environment for an app launch.
     66   void Initialize();
     67 
     68   // Continues the initialization after network is ready.
     69   void ContinueWithNetworkReady();
     70 
     71   // Launches the app after the initialization is successful.
     72   void LaunchApp();
     73 
     74   // Restarts launcher;
     75   void RestartLauncher();
     76 
     77  private:
     78   // OAuth parameters from /home/chronos/kiosk_auth file.
     79   struct KioskOAuthParams {
     80     std::string refresh_token;
     81     std::string client_id;
     82     std::string client_secret;
     83   };
     84 
     85   void OnLaunchSuccess();
     86   void OnLaunchFailure(KioskAppLaunchError::Error error);
     87 
     88   // Callbacks from ExtensionUpdater.
     89   void OnUpdateCheckFinished();
     90 
     91   void BeginInstall();
     92   void OnReadyToLaunch();
     93   void UpdateAppData();
     94 
     95   void InitializeTokenService();
     96   void MaybeInitializeNetwork();
     97   void MaybeLaunchApp();
     98 
     99   void StartLoadingOAuthFile();
    100   static void LoadOAuthFileOnBlockingPool(KioskOAuthParams* auth_params);
    101   void OnOAuthFileLoaded(KioskOAuthParams* auth_params);
    102 
    103   void OnKioskAppDataLoadStatusChanged(const std::string& app_id);
    104 
    105   // OAuth2TokenService::Observer overrides.
    106   virtual void OnRefreshTokenAvailable(const std::string& account_id) OVERRIDE;
    107   virtual void OnRefreshTokensLoaded() OVERRIDE;
    108 
    109   // extensions::InstallObserver overrides.
    110   virtual void OnFinishCrxInstall(const std::string& extension_id,
    111                                   bool success) OVERRIDE;
    112 
    113   // KioskAppManagerObserver overrides.
    114   virtual void OnKioskExtensionLoadedInCache(
    115       const std::string& app_id) OVERRIDE;
    116   virtual void OnKioskExtensionDownloadFailed(
    117       const std::string& app_id) OVERRIDE;
    118 
    119   Profile* profile_;
    120   const std::string app_id_;
    121   const bool diagnostic_mode_;
    122   Delegate* delegate_;
    123   bool network_ready_handled_;
    124   int launch_attempt_;
    125   bool ready_to_launch_;
    126   bool wait_for_crx_update_;
    127 
    128   KioskOAuthParams auth_params_;
    129 
    130   DISALLOW_COPY_AND_ASSIGN(StartupAppLauncher);
    131 };
    132 
    133 }  // namespace chromeos
    134 
    135 #endif  // CHROME_BROWSER_CHROMEOS_APP_MODE_STARTUP_APP_LAUNCHER_H_
    136